1 // SPDX-License-Identifier: GPL-2.0
3 * Qualcomm PCIe root complex driver
5 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
6 * Copyright 2015 Linaro Limited.
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>
19 #include <linux/iopoll.h>
20 #include <linux/kernel.h>
21 #include <linux/limits.h>
22 #include <linux/init.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>
36 #include "../../pci.h"
37 #include "pcie-designware.h"
40 #define PARF_SYS_CTRL 0x00
41 #define PARF_PM_CTRL 0x20
42 #define PARF_PCS_DEEMPH 0x34
43 #define PARF_PCS_SWING 0x38
44 #define PARF_PHY_CTRL 0x40
45 #define PARF_PHY_REFCLK 0x4c
46 #define PARF_CONFIG_BITS 0x50
47 #define PARF_DBI_BASE_ADDR 0x168
48 #define PARF_MHI_CLOCK_RESET_CTRL 0x174
49 #define PARF_AXI_MSTR_WR_ADDR_HALT 0x178
50 #define PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1a8
51 #define PARF_Q2A_FLUSH 0x1ac
52 #define PARF_LTSSM 0x1b0
53 #define PARF_SID_OFFSET 0x234
54 #define PARF_BDF_TRANSLATE_CFG 0x24c
55 #define PARF_SLV_ADDR_SPACE_SIZE 0x358
56 #define PARF_NO_SNOOP_OVERIDE 0x3d4
57 #define PARF_DEVICE_TYPE 0x1000
58 #define PARF_BDF_TO_SID_TABLE_N 0x2000
59 #define PARF_BDF_TO_SID_CFG 0x2c00
62 #define ELBI_SYS_CTRL 0x04
65 #define AXI_MSTR_RESP_COMP_CTRL0 0x818
66 #define AXI_MSTR_RESP_COMP_CTRL1 0x81c
69 #define PARF_DEBUG_CNT_PM_LINKST_IN_L2 0xc04
70 #define PARF_DEBUG_CNT_PM_LINKST_IN_L1 0xc0c
71 #define PARF_DEBUG_CNT_PM_LINKST_IN_L0S 0xc10
72 #define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1 0xc84
73 #define PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2 0xc88
75 /* PARF_SYS_CTRL register fields */
76 #define MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN BIT(29)
77 #define MST_WAKEUP_EN BIT(13)
78 #define SLV_WAKEUP_EN BIT(12)
79 #define MSTR_ACLK_CGC_DIS BIT(10)
80 #define SLV_ACLK_CGC_DIS BIT(9)
81 #define CORE_CLK_CGC_DIS BIT(6)
82 #define AUX_PWR_DET BIT(4)
83 #define L23_CLK_RMV_DIS BIT(2)
84 #define L1_CLK_RMV_DIS BIT(1)
86 /* PARF_PM_CTRL register fields */
87 #define REQ_NOT_ENTR_L1 BIT(5)
89 /* PARF_PCS_DEEMPH register fields */
90 #define PCS_DEEMPH_TX_DEEMPH_GEN1(x) FIELD_PREP(GENMASK(21, 16), x)
91 #define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x) FIELD_PREP(GENMASK(13, 8), x)
92 #define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x) FIELD_PREP(GENMASK(5, 0), x)
94 /* PARF_PCS_SWING register fields */
95 #define PCS_SWING_TX_SWING_FULL(x) FIELD_PREP(GENMASK(14, 8), x)
96 #define PCS_SWING_TX_SWING_LOW(x) FIELD_PREP(GENMASK(6, 0), x)
98 /* PARF_PHY_CTRL register fields */
99 #define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK GENMASK(20, 16)
100 #define PHY_CTRL_PHY_TX0_TERM_OFFSET(x) FIELD_PREP(PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK, x)
101 #define PHY_TEST_PWR_DOWN BIT(0)
103 /* PARF_PHY_REFCLK register fields */
104 #define PHY_REFCLK_SSP_EN BIT(16)
105 #define PHY_REFCLK_USE_PAD BIT(12)
107 /* PARF_CONFIG_BITS register fields */
108 #define PHY_RX0_EQ(x) FIELD_PREP(GENMASK(26, 24), x)
110 /* PARF_SLV_ADDR_SPACE_SIZE register value */
111 #define SLV_ADDR_SPACE_SZ 0x10000000
113 /* PARF_MHI_CLOCK_RESET_CTRL register fields */
114 #define AHB_CLK_EN BIT(0)
115 #define MSTR_AXI_CLK_EN BIT(1)
116 #define BYPASS BIT(4)
118 /* PARF_AXI_MSTR_WR_ADDR_HALT register fields */
121 /* PARF_LTSSM register fields */
122 #define LTSSM_EN BIT(8)
124 /* PARF_NO_SNOOP_OVERIDE register fields */
125 #define WR_NO_SNOOP_OVERIDE_EN BIT(1)
126 #define RD_NO_SNOOP_OVERIDE_EN BIT(3)
128 /* PARF_DEVICE_TYPE register fields */
129 #define DEVICE_TYPE_RC 0x4
131 /* PARF_BDF_TO_SID_CFG fields */
132 #define BDF_TO_SID_BYPASS BIT(0)
134 /* ELBI_SYS_CTRL register fields */
135 #define ELBI_SYS_CTRL_LT_ENABLE BIT(0)
137 /* AXI_MSTR_RESP_COMP_CTRL0 register fields */
138 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
139 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
141 /* AXI_MSTR_RESP_COMP_CTRL1 register fields */
142 #define CFG_BRIDGE_SB_INIT BIT(0)
144 /* PCI_EXP_SLTCAP register fields */
145 #define PCIE_CAP_SLOT_POWER_LIMIT_VAL FIELD_PREP(PCI_EXP_SLTCAP_SPLV, 250)
146 #define PCIE_CAP_SLOT_POWER_LIMIT_SCALE FIELD_PREP(PCI_EXP_SLTCAP_SPLS, 1)
147 #define PCIE_CAP_SLOT_VAL (PCI_EXP_SLTCAP_ABP | \
148 PCI_EXP_SLTCAP_PCP | \
149 PCI_EXP_SLTCAP_MRLSP | \
150 PCI_EXP_SLTCAP_AIP | \
151 PCI_EXP_SLTCAP_PIP | \
152 PCI_EXP_SLTCAP_HPS | \
153 PCI_EXP_SLTCAP_EIP | \
154 PCIE_CAP_SLOT_POWER_LIMIT_VAL | \
155 PCIE_CAP_SLOT_POWER_LIMIT_SCALE)
157 #define PERST_DELAY_US 1000
159 #define QCOM_PCIE_CRC8_POLYNOMIAL (BIT(2) | BIT(1) | BIT(0))
161 #define QCOM_PCIE_LINK_SPEED_TO_BW(speed) \
162 Mbps_to_icc(PCIE_SPEED2MBS_ENC(pcie_link_speed[speed]))
164 struct qcom_pcie_resources_1_0_0 {
165 struct clk_bulk_data *clks;
167 struct reset_control *core;
168 struct regulator *vdda;
171 #define QCOM_PCIE_2_1_0_MAX_RESETS 6
172 #define QCOM_PCIE_2_1_0_MAX_SUPPLY 3
173 struct qcom_pcie_resources_2_1_0 {
174 struct clk_bulk_data *clks;
176 struct reset_control_bulk_data resets[QCOM_PCIE_2_1_0_MAX_RESETS];
178 struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
181 #define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
182 struct qcom_pcie_resources_2_3_2 {
183 struct clk_bulk_data *clks;
185 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
188 #define QCOM_PCIE_2_3_3_MAX_RESETS 7
189 struct qcom_pcie_resources_2_3_3 {
190 struct clk_bulk_data *clks;
192 struct reset_control_bulk_data rst[QCOM_PCIE_2_3_3_MAX_RESETS];
195 #define QCOM_PCIE_2_4_0_MAX_RESETS 12
196 struct qcom_pcie_resources_2_4_0 {
197 struct clk_bulk_data *clks;
199 struct reset_control_bulk_data resets[QCOM_PCIE_2_4_0_MAX_RESETS];
203 #define QCOM_PCIE_2_7_0_MAX_SUPPLIES 2
204 struct qcom_pcie_resources_2_7_0 {
205 struct clk_bulk_data *clks;
207 struct regulator_bulk_data supplies[QCOM_PCIE_2_7_0_MAX_SUPPLIES];
208 struct reset_control *rst;
211 struct qcom_pcie_resources_2_9_0 {
212 struct clk_bulk_data *clks;
214 struct reset_control *rst;
217 union qcom_pcie_resources {
218 struct qcom_pcie_resources_1_0_0 v1_0_0;
219 struct qcom_pcie_resources_2_1_0 v2_1_0;
220 struct qcom_pcie_resources_2_3_2 v2_3_2;
221 struct qcom_pcie_resources_2_3_3 v2_3_3;
222 struct qcom_pcie_resources_2_4_0 v2_4_0;
223 struct qcom_pcie_resources_2_7_0 v2_7_0;
224 struct qcom_pcie_resources_2_9_0 v2_9_0;
229 struct qcom_pcie_ops {
230 int (*get_resources)(struct qcom_pcie *pcie);
231 int (*init)(struct qcom_pcie *pcie);
232 int (*post_init)(struct qcom_pcie *pcie);
233 void (*host_post_init)(struct qcom_pcie *pcie);
234 void (*deinit)(struct qcom_pcie *pcie);
235 void (*ltssm_enable)(struct qcom_pcie *pcie);
236 int (*config_sid)(struct qcom_pcie *pcie);
240 * struct qcom_pcie_cfg - Per SoC config struct
241 * @ops: qcom PCIe ops structure
242 * @override_no_snoop: Override NO_SNOOP attribute in TLP to enable cache
245 struct qcom_pcie_cfg {
246 const struct qcom_pcie_ops *ops;
247 bool override_no_snoop;
253 void __iomem *parf; /* DT parf */
254 void __iomem *elbi; /* DT elbi */
256 union qcom_pcie_resources res;
258 struct gpio_desc *reset;
259 struct icc_path *icc_mem;
260 struct icc_path *icc_cpu;
261 const struct qcom_pcie_cfg *cfg;
262 struct dentry *debugfs;
266 #define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
268 static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
270 gpiod_set_value_cansleep(pcie->reset, 1);
271 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
274 static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
276 /* Ensure that PERST has been asserted for at least 100 ms */
278 gpiod_set_value_cansleep(pcie->reset, 0);
279 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
282 static int qcom_pcie_start_link(struct dw_pcie *pci)
284 struct qcom_pcie *pcie = to_qcom_pcie(pci);
286 /* Enable Link Training state machine */
287 if (pcie->cfg->ops->ltssm_enable)
288 pcie->cfg->ops->ltssm_enable(pcie);
293 static void qcom_pcie_clear_aspm_l0s(struct dw_pcie *pci)
295 struct qcom_pcie *pcie = to_qcom_pcie(pci);
299 if (!pcie->cfg->no_l0s)
302 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
304 dw_pcie_dbi_ro_wr_en(pci);
306 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
307 val &= ~PCI_EXP_LNKCAP_ASPM_L0S;
308 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
310 dw_pcie_dbi_ro_wr_dis(pci);
313 static void qcom_pcie_clear_hpc(struct dw_pcie *pci)
315 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
318 dw_pcie_dbi_ro_wr_en(pci);
320 val = readl(pci->dbi_base + offset + PCI_EXP_SLTCAP);
321 val &= ~PCI_EXP_SLTCAP_HPC;
322 writel(val, pci->dbi_base + offset + PCI_EXP_SLTCAP);
324 dw_pcie_dbi_ro_wr_dis(pci);
327 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
331 /* enable link training */
332 val = readl(pcie->elbi + ELBI_SYS_CTRL);
333 val |= ELBI_SYS_CTRL_LT_ENABLE;
334 writel(val, pcie->elbi + ELBI_SYS_CTRL);
337 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
339 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
340 struct dw_pcie *pci = pcie->pci;
341 struct device *dev = pci->dev;
342 bool is_apq = of_device_is_compatible(dev->of_node, "qcom,pcie-apq8064");
345 res->supplies[0].supply = "vdda";
346 res->supplies[1].supply = "vdda_phy";
347 res->supplies[2].supply = "vdda_refclk";
348 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
353 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
354 if (res->num_clks < 0) {
355 dev_err(dev, "Failed to get clocks\n");
356 return res->num_clks;
359 res->resets[0].id = "pci";
360 res->resets[1].id = "axi";
361 res->resets[2].id = "ahb";
362 res->resets[3].id = "por";
363 res->resets[4].id = "phy";
364 res->resets[5].id = "ext";
366 /* ext is optional on APQ8016 */
367 res->num_resets = is_apq ? 5 : 6;
368 ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
375 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
377 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
379 clk_bulk_disable_unprepare(res->num_clks, res->clks);
380 reset_control_bulk_assert(res->num_resets, res->resets);
382 writel(1, pcie->parf + PARF_PHY_CTRL);
384 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
387 static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
389 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
390 struct dw_pcie *pci = pcie->pci;
391 struct device *dev = pci->dev;
394 /* reset the PCIe interface as uboot can leave it undefined state */
395 ret = reset_control_bulk_assert(res->num_resets, res->resets);
397 dev_err(dev, "cannot assert resets\n");
401 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
403 dev_err(dev, "cannot enable regulators\n");
407 ret = reset_control_bulk_deassert(res->num_resets, res->resets);
409 dev_err(dev, "cannot deassert resets\n");
410 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
417 static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie)
419 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
420 struct dw_pcie *pci = pcie->pci;
421 struct device *dev = pci->dev;
422 struct device_node *node = dev->of_node;
426 /* enable PCIe clocks and resets */
427 val = readl(pcie->parf + PARF_PHY_CTRL);
428 val &= ~PHY_TEST_PWR_DOWN;
429 writel(val, pcie->parf + PARF_PHY_CTRL);
431 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
435 if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
436 of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
437 writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
438 PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
439 PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
440 pcie->parf + PARF_PCS_DEEMPH);
441 writel(PCS_SWING_TX_SWING_FULL(120) |
442 PCS_SWING_TX_SWING_LOW(120),
443 pcie->parf + PARF_PCS_SWING);
444 writel(PHY_RX0_EQ(4), pcie->parf + PARF_CONFIG_BITS);
447 if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
448 /* set TX termination offset */
449 val = readl(pcie->parf + PARF_PHY_CTRL);
450 val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
451 val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
452 writel(val, pcie->parf + PARF_PHY_CTRL);
455 /* enable external reference clock */
456 val = readl(pcie->parf + PARF_PHY_REFCLK);
457 /* USE_PAD is required only for ipq806x */
458 if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
459 val &= ~PHY_REFCLK_USE_PAD;
460 val |= PHY_REFCLK_SSP_EN;
461 writel(val, pcie->parf + PARF_PHY_REFCLK);
463 /* wait for clock acquisition */
464 usleep_range(1000, 1500);
466 /* Set the Max TLP size to 2K, instead of using default of 4K */
467 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
468 pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL0);
469 writel(CFG_BRIDGE_SB_INIT,
470 pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL1);
472 qcom_pcie_clear_hpc(pcie->pci);
477 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
479 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
480 struct dw_pcie *pci = pcie->pci;
481 struct device *dev = pci->dev;
483 res->vdda = devm_regulator_get(dev, "vdda");
484 if (IS_ERR(res->vdda))
485 return PTR_ERR(res->vdda);
487 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
488 if (res->num_clks < 0) {
489 dev_err(dev, "Failed to get clocks\n");
490 return res->num_clks;
493 res->core = devm_reset_control_get_exclusive(dev, "core");
494 return PTR_ERR_OR_ZERO(res->core);
497 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
499 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
501 reset_control_assert(res->core);
502 clk_bulk_disable_unprepare(res->num_clks, res->clks);
503 regulator_disable(res->vdda);
506 static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
508 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
509 struct dw_pcie *pci = pcie->pci;
510 struct device *dev = pci->dev;
513 ret = reset_control_deassert(res->core);
515 dev_err(dev, "cannot deassert core reset\n");
519 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
521 dev_err(dev, "cannot prepare/enable clocks\n");
522 goto err_assert_reset;
525 ret = regulator_enable(res->vdda);
527 dev_err(dev, "cannot enable vdda regulator\n");
528 goto err_disable_clks;
534 clk_bulk_disable_unprepare(res->num_clks, res->clks);
536 reset_control_assert(res->core);
541 static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie)
543 /* change DBI base address */
544 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
546 if (IS_ENABLED(CONFIG_PCI_MSI)) {
547 u32 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
550 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
553 qcom_pcie_clear_hpc(pcie->pci);
558 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
562 /* enable link training */
563 val = readl(pcie->parf + PARF_LTSSM);
565 writel(val, pcie->parf + PARF_LTSSM);
568 static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
570 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
571 struct dw_pcie *pci = pcie->pci;
572 struct device *dev = pci->dev;
575 res->supplies[0].supply = "vdda";
576 res->supplies[1].supply = "vddpe-3v3";
577 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
582 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
583 if (res->num_clks < 0) {
584 dev_err(dev, "Failed to get clocks\n");
585 return res->num_clks;
591 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
593 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
595 clk_bulk_disable_unprepare(res->num_clks, res->clks);
596 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
599 static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
601 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
602 struct dw_pcie *pci = pcie->pci;
603 struct device *dev = pci->dev;
606 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
608 dev_err(dev, "cannot enable regulators\n");
612 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
614 dev_err(dev, "cannot prepare/enable clocks\n");
615 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
622 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
626 /* enable PCIe clocks and resets */
627 val = readl(pcie->parf + PARF_PHY_CTRL);
628 val &= ~PHY_TEST_PWR_DOWN;
629 writel(val, pcie->parf + PARF_PHY_CTRL);
631 /* change DBI base address */
632 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
634 /* MAC PHY_POWERDOWN MUX DISABLE */
635 val = readl(pcie->parf + PARF_SYS_CTRL);
636 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
637 writel(val, pcie->parf + PARF_SYS_CTRL);
639 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
641 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
643 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
645 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
647 qcom_pcie_clear_hpc(pcie->pci);
652 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
654 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
655 struct dw_pcie *pci = pcie->pci;
656 struct device *dev = pci->dev;
657 bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
660 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
661 if (res->num_clks < 0) {
662 dev_err(dev, "Failed to get clocks\n");
663 return res->num_clks;
666 res->resets[0].id = "axi_m";
667 res->resets[1].id = "axi_s";
668 res->resets[2].id = "axi_m_sticky";
669 res->resets[3].id = "pipe_sticky";
670 res->resets[4].id = "pwr";
671 res->resets[5].id = "ahb";
672 res->resets[6].id = "pipe";
673 res->resets[7].id = "axi_m_vmid";
674 res->resets[8].id = "axi_s_xpu";
675 res->resets[9].id = "parf";
676 res->resets[10].id = "phy";
677 res->resets[11].id = "phy_ahb";
679 res->num_resets = is_ipq ? 12 : 6;
681 ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
688 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
690 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
692 reset_control_bulk_assert(res->num_resets, res->resets);
693 clk_bulk_disable_unprepare(res->num_clks, res->clks);
696 static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
698 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
699 struct dw_pcie *pci = pcie->pci;
700 struct device *dev = pci->dev;
703 ret = reset_control_bulk_assert(res->num_resets, res->resets);
705 dev_err(dev, "cannot assert resets\n");
709 usleep_range(10000, 12000);
711 ret = reset_control_bulk_deassert(res->num_resets, res->resets);
713 dev_err(dev, "cannot deassert resets\n");
717 usleep_range(10000, 12000);
719 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
721 reset_control_bulk_assert(res->num_resets, res->resets);
728 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
730 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
731 struct dw_pcie *pci = pcie->pci;
732 struct device *dev = pci->dev;
735 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
736 if (res->num_clks < 0) {
737 dev_err(dev, "Failed to get clocks\n");
738 return res->num_clks;
741 res->rst[0].id = "axi_m";
742 res->rst[1].id = "axi_s";
743 res->rst[2].id = "pipe";
744 res->rst[3].id = "axi_m_sticky";
745 res->rst[4].id = "sticky";
746 res->rst[5].id = "ahb";
747 res->rst[6].id = "sleep";
749 ret = devm_reset_control_bulk_get_exclusive(dev, ARRAY_SIZE(res->rst), res->rst);
756 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
758 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
760 clk_bulk_disable_unprepare(res->num_clks, res->clks);
763 static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
765 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
766 struct dw_pcie *pci = pcie->pci;
767 struct device *dev = pci->dev;
770 ret = reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst);
772 dev_err(dev, "cannot assert resets\n");
776 usleep_range(2000, 2500);
778 ret = reset_control_bulk_deassert(ARRAY_SIZE(res->rst), res->rst);
780 dev_err(dev, "cannot deassert resets\n");
785 * Don't have a way to see if the reset has completed.
786 * Wait for some time.
788 usleep_range(2000, 2500);
790 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
792 dev_err(dev, "cannot prepare/enable clocks\n");
793 goto err_assert_resets;
800 * Not checking for failure, will anyway return
801 * the original failure in 'ret'.
803 reset_control_bulk_assert(ARRAY_SIZE(res->rst), res->rst);
808 static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie)
810 struct dw_pcie *pci = pcie->pci;
811 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
814 writel(SLV_ADDR_SPACE_SZ, pcie->parf + PARF_SLV_ADDR_SPACE_SIZE);
816 val = readl(pcie->parf + PARF_PHY_CTRL);
817 val &= ~PHY_TEST_PWR_DOWN;
818 writel(val, pcie->parf + PARF_PHY_CTRL);
820 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
822 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
823 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
824 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
825 pcie->parf + PARF_SYS_CTRL);
826 writel(0, pcie->parf + PARF_Q2A_FLUSH);
828 writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
830 dw_pcie_dbi_ro_wr_en(pci);
832 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
834 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
835 val &= ~PCI_EXP_LNKCAP_ASPMS;
836 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
838 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
841 dw_pcie_dbi_ro_wr_dis(pci);
846 static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
848 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
849 struct dw_pcie *pci = pcie->pci;
850 struct device *dev = pci->dev;
853 res->rst = devm_reset_control_array_get_exclusive(dev);
854 if (IS_ERR(res->rst))
855 return PTR_ERR(res->rst);
857 res->supplies[0].supply = "vdda";
858 res->supplies[1].supply = "vddpe-3v3";
859 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
864 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
865 if (res->num_clks < 0) {
866 dev_err(dev, "Failed to get clocks\n");
867 return res->num_clks;
873 static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
875 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
876 struct dw_pcie *pci = pcie->pci;
877 struct device *dev = pci->dev;
881 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
883 dev_err(dev, "cannot enable regulators\n");
887 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
889 goto err_disable_regulators;
891 ret = reset_control_assert(res->rst);
893 dev_err(dev, "reset assert failed (%d)\n", ret);
894 goto err_disable_clocks;
897 usleep_range(1000, 1500);
899 ret = reset_control_deassert(res->rst);
901 dev_err(dev, "reset deassert failed (%d)\n", ret);
902 goto err_disable_clocks;
905 /* Wait for reset to complete, required on SM8450 */
906 usleep_range(1000, 1500);
908 /* configure PCIe to RC mode */
909 writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
911 /* enable PCIe clocks and resets */
912 val = readl(pcie->parf + PARF_PHY_CTRL);
913 val &= ~PHY_TEST_PWR_DOWN;
914 writel(val, pcie->parf + PARF_PHY_CTRL);
916 /* change DBI base address */
917 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
919 /* MAC PHY_POWERDOWN MUX DISABLE */
920 val = readl(pcie->parf + PARF_SYS_CTRL);
921 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
922 writel(val, pcie->parf + PARF_SYS_CTRL);
924 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
926 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
928 /* Enable L1 and L1SS */
929 val = readl(pcie->parf + PARF_PM_CTRL);
930 val &= ~REQ_NOT_ENTR_L1;
931 writel(val, pcie->parf + PARF_PM_CTRL);
933 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
935 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
939 clk_bulk_disable_unprepare(res->num_clks, res->clks);
940 err_disable_regulators:
941 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
946 static int qcom_pcie_post_init_2_7_0(struct qcom_pcie *pcie)
948 const struct qcom_pcie_cfg *pcie_cfg = pcie->cfg;
950 if (pcie_cfg->override_no_snoop)
951 writel(WR_NO_SNOOP_OVERIDE_EN | RD_NO_SNOOP_OVERIDE_EN,
952 pcie->parf + PARF_NO_SNOOP_OVERIDE);
954 qcom_pcie_clear_aspm_l0s(pcie->pci);
955 qcom_pcie_clear_hpc(pcie->pci);
960 static int qcom_pcie_enable_aspm(struct pci_dev *pdev, void *userdata)
963 * Downstream devices need to be in D0 state before enabling PCI PM
966 pci_set_power_state_locked(pdev, PCI_D0);
967 pci_enable_link_state_locked(pdev, PCIE_LINK_STATE_ALL);
972 static void qcom_pcie_host_post_init_2_7_0(struct qcom_pcie *pcie)
974 struct dw_pcie_rp *pp = &pcie->pci->pp;
976 pci_walk_bus(pp->bridge->bus, qcom_pcie_enable_aspm, NULL);
979 static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
981 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
983 clk_bulk_disable_unprepare(res->num_clks, res->clks);
985 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
988 static int qcom_pcie_config_sid_1_9_0(struct qcom_pcie *pcie)
990 /* iommu map structure */
997 void __iomem *bdf_to_sid_base = pcie->parf + PARF_BDF_TO_SID_TABLE_N;
998 struct device *dev = pcie->pci->dev;
999 u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1000 int i, nr_map, size = 0;
1004 of_get_property(dev->of_node, "iommu-map", &size);
1008 /* Enable BDF to SID translation by disabling bypass mode (default) */
1009 val = readl(pcie->parf + PARF_BDF_TO_SID_CFG);
1010 val &= ~BDF_TO_SID_BYPASS;
1011 writel(val, pcie->parf + PARF_BDF_TO_SID_CFG);
1013 map = kzalloc(size, GFP_KERNEL);
1017 of_property_read_u32_array(dev->of_node, "iommu-map", (u32 *)map,
1018 size / sizeof(u32));
1020 nr_map = size / (sizeof(*map));
1022 crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1024 /* Registers need to be zero out first */
1025 memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1027 /* Extract the SMMU SID base from the first entry of iommu-map */
1028 smmu_sid_base = map[0].smmu_sid;
1030 /* Look for an available entry to hold the mapping */
1031 for (i = 0; i < nr_map; i++) {
1032 __be16 bdf_be = cpu_to_be16(map[i].bdf);
1036 hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be), 0);
1038 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1040 /* If the register is already populated, look for next available entry */
1042 u8 current_hash = hash++;
1043 u8 next_mask = 0xff;
1045 /* If NEXT field is NULL then update it with next hash */
1046 if (!(val & next_mask)) {
1048 writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1051 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1054 /* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1055 val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1056 writel(val, bdf_to_sid_base + hash * sizeof(u32));
1064 static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie)
1066 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1067 struct dw_pcie *pci = pcie->pci;
1068 struct device *dev = pci->dev;
1070 res->num_clks = devm_clk_bulk_get_all(dev, &res->clks);
1071 if (res->num_clks < 0) {
1072 dev_err(dev, "Failed to get clocks\n");
1073 return res->num_clks;
1076 res->rst = devm_reset_control_array_get_exclusive(dev);
1077 if (IS_ERR(res->rst))
1078 return PTR_ERR(res->rst);
1083 static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie)
1085 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1087 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1090 static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie)
1092 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1093 struct device *dev = pcie->pci->dev;
1096 ret = reset_control_assert(res->rst);
1098 dev_err(dev, "reset assert failed (%d)\n", ret);
1103 * Delay periods before and after reset deassert are working values
1104 * from downstream Codeaurora kernel
1106 usleep_range(2000, 2500);
1108 ret = reset_control_deassert(res->rst);
1110 dev_err(dev, "reset deassert failed (%d)\n", ret);
1114 usleep_range(2000, 2500);
1116 return clk_bulk_prepare_enable(res->num_clks, res->clks);
1119 static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie)
1121 struct dw_pcie *pci = pcie->pci;
1122 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1126 writel(SLV_ADDR_SPACE_SZ,
1127 pcie->parf + PARF_SLV_ADDR_SPACE_SIZE);
1129 val = readl(pcie->parf + PARF_PHY_CTRL);
1130 val &= ~PHY_TEST_PWR_DOWN;
1131 writel(val, pcie->parf + PARF_PHY_CTRL);
1133 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
1135 writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
1136 writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN,
1137 pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
1138 writel(GEN3_RELATED_OFF_RXEQ_RGRDLESS_RXTS |
1139 GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL,
1140 pci->dbi_base + GEN3_RELATED_OFF);
1142 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS |
1143 SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1144 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1145 pcie->parf + PARF_SYS_CTRL);
1147 writel(0, pcie->parf + PARF_Q2A_FLUSH);
1149 dw_pcie_dbi_ro_wr_en(pci);
1151 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1153 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1154 val &= ~PCI_EXP_LNKCAP_ASPMS;
1155 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1157 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1160 dw_pcie_dbi_ro_wr_dis(pci);
1162 for (i = 0; i < 256; i++)
1163 writel(0, pcie->parf + PARF_BDF_TO_SID_TABLE_N + (4 * i));
1168 static int qcom_pcie_link_up(struct dw_pcie *pci)
1170 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1171 u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1173 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1176 static int qcom_pcie_host_init(struct dw_pcie_rp *pp)
1178 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1179 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1182 qcom_ep_reset_assert(pcie);
1184 ret = pcie->cfg->ops->init(pcie);
1188 ret = phy_set_mode_ext(pcie->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
1192 ret = phy_power_on(pcie->phy);
1196 if (pcie->cfg->ops->post_init) {
1197 ret = pcie->cfg->ops->post_init(pcie);
1199 goto err_disable_phy;
1202 qcom_ep_reset_deassert(pcie);
1204 if (pcie->cfg->ops->config_sid) {
1205 ret = pcie->cfg->ops->config_sid(pcie);
1207 goto err_assert_reset;
1213 qcom_ep_reset_assert(pcie);
1215 phy_power_off(pcie->phy);
1217 pcie->cfg->ops->deinit(pcie);
1222 static void qcom_pcie_host_deinit(struct dw_pcie_rp *pp)
1224 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1225 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1227 qcom_ep_reset_assert(pcie);
1228 phy_power_off(pcie->phy);
1229 pcie->cfg->ops->deinit(pcie);
1232 static void qcom_pcie_host_post_init(struct dw_pcie_rp *pp)
1234 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1235 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1237 if (pcie->cfg->ops->host_post_init)
1238 pcie->cfg->ops->host_post_init(pcie);
1241 static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1242 .init = qcom_pcie_host_init,
1243 .deinit = qcom_pcie_host_deinit,
1244 .post_init = qcom_pcie_host_post_init,
1247 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1248 static const struct qcom_pcie_ops ops_2_1_0 = {
1249 .get_resources = qcom_pcie_get_resources_2_1_0,
1250 .init = qcom_pcie_init_2_1_0,
1251 .post_init = qcom_pcie_post_init_2_1_0,
1252 .deinit = qcom_pcie_deinit_2_1_0,
1253 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1256 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1257 static const struct qcom_pcie_ops ops_1_0_0 = {
1258 .get_resources = qcom_pcie_get_resources_1_0_0,
1259 .init = qcom_pcie_init_1_0_0,
1260 .post_init = qcom_pcie_post_init_1_0_0,
1261 .deinit = qcom_pcie_deinit_1_0_0,
1262 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1265 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1266 static const struct qcom_pcie_ops ops_2_3_2 = {
1267 .get_resources = qcom_pcie_get_resources_2_3_2,
1268 .init = qcom_pcie_init_2_3_2,
1269 .post_init = qcom_pcie_post_init_2_3_2,
1270 .deinit = qcom_pcie_deinit_2_3_2,
1271 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1274 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1275 static const struct qcom_pcie_ops ops_2_4_0 = {
1276 .get_resources = qcom_pcie_get_resources_2_4_0,
1277 .init = qcom_pcie_init_2_4_0,
1278 .post_init = qcom_pcie_post_init_2_3_2,
1279 .deinit = qcom_pcie_deinit_2_4_0,
1280 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1283 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1284 static const struct qcom_pcie_ops ops_2_3_3 = {
1285 .get_resources = qcom_pcie_get_resources_2_3_3,
1286 .init = qcom_pcie_init_2_3_3,
1287 .post_init = qcom_pcie_post_init_2_3_3,
1288 .deinit = qcom_pcie_deinit_2_3_3,
1289 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1292 /* Qcom IP rev.: 2.7.0 Synopsys IP rev.: 4.30a */
1293 static const struct qcom_pcie_ops ops_2_7_0 = {
1294 .get_resources = qcom_pcie_get_resources_2_7_0,
1295 .init = qcom_pcie_init_2_7_0,
1296 .post_init = qcom_pcie_post_init_2_7_0,
1297 .deinit = qcom_pcie_deinit_2_7_0,
1298 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1301 /* Qcom IP rev.: 1.9.0 */
1302 static const struct qcom_pcie_ops ops_1_9_0 = {
1303 .get_resources = qcom_pcie_get_resources_2_7_0,
1304 .init = qcom_pcie_init_2_7_0,
1305 .post_init = qcom_pcie_post_init_2_7_0,
1306 .host_post_init = qcom_pcie_host_post_init_2_7_0,
1307 .deinit = qcom_pcie_deinit_2_7_0,
1308 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1309 .config_sid = qcom_pcie_config_sid_1_9_0,
1312 /* Qcom IP rev.: 2.9.0 Synopsys IP rev.: 5.00a */
1313 static const struct qcom_pcie_ops ops_2_9_0 = {
1314 .get_resources = qcom_pcie_get_resources_2_9_0,
1315 .init = qcom_pcie_init_2_9_0,
1316 .post_init = qcom_pcie_post_init_2_9_0,
1317 .deinit = qcom_pcie_deinit_2_9_0,
1318 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1321 static const struct qcom_pcie_cfg cfg_1_0_0 = {
1325 static const struct qcom_pcie_cfg cfg_1_9_0 = {
1329 static const struct qcom_pcie_cfg cfg_1_34_0 = {
1331 .override_no_snoop = true,
1334 static const struct qcom_pcie_cfg cfg_2_1_0 = {
1338 static const struct qcom_pcie_cfg cfg_2_3_2 = {
1342 static const struct qcom_pcie_cfg cfg_2_3_3 = {
1346 static const struct qcom_pcie_cfg cfg_2_4_0 = {
1350 static const struct qcom_pcie_cfg cfg_2_7_0 = {
1354 static const struct qcom_pcie_cfg cfg_2_9_0 = {
1358 static const struct qcom_pcie_cfg cfg_sc8280xp = {
1363 static const struct dw_pcie_ops dw_pcie_ops = {
1364 .link_up = qcom_pcie_link_up,
1365 .start_link = qcom_pcie_start_link,
1368 static int qcom_pcie_icc_init(struct qcom_pcie *pcie)
1370 struct dw_pcie *pci = pcie->pci;
1373 pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem");
1374 if (IS_ERR(pcie->icc_mem))
1375 return PTR_ERR(pcie->icc_mem);
1377 pcie->icc_cpu = devm_of_icc_get(pci->dev, "cpu-pcie");
1378 if (IS_ERR(pcie->icc_cpu))
1379 return PTR_ERR(pcie->icc_cpu);
1381 * Some Qualcomm platforms require interconnect bandwidth constraints
1382 * to be set before enabling interconnect clocks.
1384 * Set an initial peak bandwidth corresponding to single-lane Gen 1
1385 * for the pcie-mem path.
1387 ret = icc_set_bw(pcie->icc_mem, 0, QCOM_PCIE_LINK_SPEED_TO_BW(1));
1389 dev_err(pci->dev, "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n",
1395 * Since the CPU-PCIe path is only used for activities like register
1396 * access of the host controller and endpoint Config/BAR space access,
1397 * HW team has recommended to use a minimal bandwidth of 1KBps just to
1398 * keep the path active.
1400 ret = icc_set_bw(pcie->icc_cpu, 0, kBps_to_icc(1));
1402 dev_err(pci->dev, "Failed to set bandwidth for CPU-PCIe interconnect path: %d\n",
1404 icc_set_bw(pcie->icc_mem, 0, 0);
1411 static void qcom_pcie_icc_opp_update(struct qcom_pcie *pcie)
1413 u32 offset, status, width, speed;
1414 struct dw_pcie *pci = pcie->pci;
1415 unsigned long freq_kbps;
1416 struct dev_pm_opp *opp;
1419 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1420 status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1422 /* Only update constraints if link is up. */
1423 if (!(status & PCI_EXP_LNKSTA_DLLLA))
1426 speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status);
1427 width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status);
1429 if (pcie->icc_mem) {
1430 ret = icc_set_bw(pcie->icc_mem, 0,
1431 width * QCOM_PCIE_LINK_SPEED_TO_BW(speed));
1433 dev_err(pci->dev, "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n",
1437 freq_mbps = pcie_dev_speed_mbps(pcie_link_speed[speed]);
1441 freq_kbps = freq_mbps * KILO;
1442 opp = dev_pm_opp_find_freq_exact(pci->dev, freq_kbps * width,
1445 ret = dev_pm_opp_set_opp(pci->dev, opp);
1447 dev_err(pci->dev, "Failed to set OPP for freq (%lu): %d\n",
1448 freq_kbps * width, ret);
1449 dev_pm_opp_put(opp);
1454 static int qcom_pcie_link_transition_count(struct seq_file *s, void *data)
1456 struct qcom_pcie *pcie = (struct qcom_pcie *)dev_get_drvdata(s->private);
1458 seq_printf(s, "L0s transition count: %u\n",
1459 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L0S));
1461 seq_printf(s, "L1 transition count: %u\n",
1462 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L1));
1464 seq_printf(s, "L1.1 transition count: %u\n",
1465 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L1));
1467 seq_printf(s, "L1.2 transition count: %u\n",
1468 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_AUX_CLK_IN_L1SUB_L2));
1470 seq_printf(s, "L2 transition count: %u\n",
1471 readl_relaxed(pcie->mhi + PARF_DEBUG_CNT_PM_LINKST_IN_L2));
1476 static void qcom_pcie_init_debugfs(struct qcom_pcie *pcie)
1478 struct dw_pcie *pci = pcie->pci;
1479 struct device *dev = pci->dev;
1482 name = devm_kasprintf(dev, GFP_KERNEL, "%pOFP", dev->of_node);
1486 pcie->debugfs = debugfs_create_dir(name, NULL);
1487 debugfs_create_devm_seqfile(dev, "link_transition_count", pcie->debugfs,
1488 qcom_pcie_link_transition_count);
1491 static int qcom_pcie_probe(struct platform_device *pdev)
1493 const struct qcom_pcie_cfg *pcie_cfg;
1494 unsigned long max_freq = ULONG_MAX;
1495 struct device *dev = &pdev->dev;
1496 struct dev_pm_opp *opp;
1497 struct qcom_pcie *pcie;
1498 struct dw_pcie_rp *pp;
1499 struct resource *res;
1500 struct dw_pcie *pci;
1503 pcie_cfg = of_device_get_match_data(dev);
1504 if (!pcie_cfg || !pcie_cfg->ops) {
1505 dev_err(dev, "Invalid platform data\n");
1509 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1513 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1517 pm_runtime_enable(dev);
1518 ret = pm_runtime_get_sync(dev);
1520 goto err_pm_runtime_put;
1523 pci->ops = &dw_pcie_ops;
1528 pcie->cfg = pcie_cfg;
1530 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1531 if (IS_ERR(pcie->reset)) {
1532 ret = PTR_ERR(pcie->reset);
1533 goto err_pm_runtime_put;
1536 pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1537 if (IS_ERR(pcie->parf)) {
1538 ret = PTR_ERR(pcie->parf);
1539 goto err_pm_runtime_put;
1542 pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1543 if (IS_ERR(pcie->elbi)) {
1544 ret = PTR_ERR(pcie->elbi);
1545 goto err_pm_runtime_put;
1548 /* MHI region is optional */
1549 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mhi");
1551 pcie->mhi = devm_ioremap_resource(dev, res);
1552 if (IS_ERR(pcie->mhi)) {
1553 ret = PTR_ERR(pcie->mhi);
1554 goto err_pm_runtime_put;
1558 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1559 if (IS_ERR(pcie->phy)) {
1560 ret = PTR_ERR(pcie->phy);
1561 goto err_pm_runtime_put;
1564 /* OPP table is optional */
1565 ret = devm_pm_opp_of_add_table(dev);
1566 if (ret && ret != -ENODEV) {
1567 dev_err_probe(dev, ret, "Failed to add OPP table\n");
1568 goto err_pm_runtime_put;
1572 * Before the PCIe link is initialized, vote for highest OPP in the OPP
1573 * table, so that we are voting for maximum voltage corner for the
1574 * link to come up in maximum supported speed. At the end of the
1575 * probe(), OPP will be updated using qcom_pcie_icc_opp_update().
1578 opp = dev_pm_opp_find_freq_floor(dev, &max_freq);
1581 dev_err_probe(pci->dev, ret,
1582 "Unable to find max freq OPP\n");
1583 goto err_pm_runtime_put;
1585 ret = dev_pm_opp_set_opp(dev, opp);
1588 dev_pm_opp_put(opp);
1590 dev_err_probe(pci->dev, ret,
1591 "Failed to set OPP for freq %lu\n",
1593 goto err_pm_runtime_put;
1596 /* Skip ICC init if OPP is supported as it is handled by OPP */
1597 ret = qcom_pcie_icc_init(pcie);
1599 goto err_pm_runtime_put;
1602 ret = pcie->cfg->ops->get_resources(pcie);
1604 goto err_pm_runtime_put;
1606 pp->ops = &qcom_pcie_dw_ops;
1608 ret = phy_init(pcie->phy);
1610 goto err_pm_runtime_put;
1612 platform_set_drvdata(pdev, pcie);
1614 ret = dw_pcie_host_init(pp);
1616 dev_err(dev, "cannot initialize host\n");
1620 qcom_pcie_icc_opp_update(pcie);
1623 qcom_pcie_init_debugfs(pcie);
1628 phy_exit(pcie->phy);
1630 pm_runtime_put(dev);
1631 pm_runtime_disable(dev);
1636 static int qcom_pcie_suspend_noirq(struct device *dev)
1638 struct qcom_pcie *pcie = dev_get_drvdata(dev);
1642 * Set minimum bandwidth required to keep data path functional during
1645 if (pcie->icc_mem) {
1646 ret = icc_set_bw(pcie->icc_mem, 0, kBps_to_icc(1));
1649 "Failed to set bandwidth for PCIe-MEM interconnect path: %d\n",
1656 * Turn OFF the resources only for controllers without active PCIe
1657 * devices. For controllers with active devices, the resources are kept
1658 * ON and the link is expected to be in L0/L1 (sub)states.
1660 * Turning OFF the resources for controllers with active PCIe devices
1661 * will trigger access violation during the end of the suspend cycle,
1662 * as kernel tries to access the PCIe devices config space for masking
1665 * Also, it is not desirable to put the link into L2/L3 state as that
1666 * implies VDD supply will be removed and the devices may go into
1667 * powerdown state. This will affect the lifetime of the storage devices
1670 if (!dw_pcie_link_up(pcie->pci)) {
1671 qcom_pcie_host_deinit(&pcie->pci->pp);
1672 pcie->suspended = true;
1676 * Only disable CPU-PCIe interconnect path if the suspend is non-S2RAM.
1677 * Because on some platforms, DBI access can happen very late during the
1678 * S2RAM and a non-active CPU-PCIe interconnect path may lead to NoC
1681 if (pm_suspend_target_state != PM_SUSPEND_MEM) {
1682 ret = icc_disable(pcie->icc_cpu);
1684 dev_err(dev, "Failed to disable CPU-PCIe interconnect path: %d\n", ret);
1687 dev_pm_opp_set_opp(pcie->pci->dev, NULL);
1692 static int qcom_pcie_resume_noirq(struct device *dev)
1694 struct qcom_pcie *pcie = dev_get_drvdata(dev);
1697 if (pm_suspend_target_state != PM_SUSPEND_MEM) {
1698 ret = icc_enable(pcie->icc_cpu);
1700 dev_err(dev, "Failed to enable CPU-PCIe interconnect path: %d\n", ret);
1705 if (pcie->suspended) {
1706 ret = qcom_pcie_host_init(&pcie->pci->pp);
1710 pcie->suspended = false;
1713 qcom_pcie_icc_opp_update(pcie);
1718 static const struct of_device_id qcom_pcie_match[] = {
1719 { .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 },
1720 { .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 },
1721 { .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 },
1722 { .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 },
1723 { .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 },
1724 { .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 },
1725 { .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 },
1726 { .compatible = "qcom,pcie-ipq8074-gen3", .data = &cfg_2_9_0 },
1727 { .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 },
1728 { .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 },
1729 { .compatible = "qcom,pcie-sa8540p", .data = &cfg_sc8280xp },
1730 { .compatible = "qcom,pcie-sa8775p", .data = &cfg_1_34_0},
1731 { .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 },
1732 { .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 },
1733 { .compatible = "qcom,pcie-sc8280xp", .data = &cfg_sc8280xp },
1734 { .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 },
1735 { .compatible = "qcom,pcie-sdx55", .data = &cfg_1_9_0 },
1736 { .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 },
1737 { .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 },
1738 { .compatible = "qcom,pcie-sm8350", .data = &cfg_1_9_0 },
1739 { .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 },
1740 { .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 },
1741 { .compatible = "qcom,pcie-sm8550", .data = &cfg_1_9_0 },
1742 { .compatible = "qcom,pcie-x1e80100", .data = &cfg_1_9_0 },
1746 static void qcom_fixup_class(struct pci_dev *dev)
1748 dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1750 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1751 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1752 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1753 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1754 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1755 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1756 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1758 static const struct dev_pm_ops qcom_pcie_pm_ops = {
1759 NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_pcie_suspend_noirq, qcom_pcie_resume_noirq)
1762 static struct platform_driver qcom_pcie_driver = {
1763 .probe = qcom_pcie_probe,
1765 .name = "qcom-pcie",
1766 .suppress_bind_attrs = true,
1767 .of_match_table = qcom_pcie_match,
1768 .pm = &qcom_pcie_pm_ops,
1769 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
1772 builtin_platform_driver(qcom_pcie_driver);