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/delay.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/interconnect.h>
16 #include <linux/interrupt.h>
18 #include <linux/iopoll.h>
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/of_device.h>
22 #include <linux/of_gpio.h>
23 #include <linux/pci.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/platform_device.h>
26 #include <linux/phy/pcie.h>
27 #include <linux/phy/phy.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/reset.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
33 #include "../../pci.h"
34 #include "pcie-designware.h"
36 #define PCIE20_PARF_SYS_CTRL 0x00
37 #define MST_WAKEUP_EN BIT(13)
38 #define SLV_WAKEUP_EN BIT(12)
39 #define MSTR_ACLK_CGC_DIS BIT(10)
40 #define SLV_ACLK_CGC_DIS BIT(9)
41 #define CORE_CLK_CGC_DIS BIT(6)
42 #define AUX_PWR_DET BIT(4)
43 #define L23_CLK_RMV_DIS BIT(2)
44 #define L1_CLK_RMV_DIS BIT(1)
46 #define PCIE20_PARF_PM_CTRL 0x20
47 #define REQ_NOT_ENTR_L1 BIT(5)
49 #define PCIE20_PARF_PHY_CTRL 0x40
50 #define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK GENMASK(20, 16)
51 #define PHY_CTRL_PHY_TX0_TERM_OFFSET(x) ((x) << 16)
53 #define PCIE20_PARF_PHY_REFCLK 0x4C
54 #define PHY_REFCLK_SSP_EN BIT(16)
55 #define PHY_REFCLK_USE_PAD BIT(12)
57 #define PCIE20_PARF_DBI_BASE_ADDR 0x168
58 #define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
59 #define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
60 #define AHB_CLK_EN BIT(0)
61 #define MSTR_AXI_CLK_EN BIT(1)
64 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
65 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
66 #define PCIE20_PARF_LTSSM 0x1B0
67 #define PCIE20_PARF_SID_OFFSET 0x234
68 #define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
69 #define PCIE20_PARF_DEVICE_TYPE 0x1000
70 #define PCIE20_PARF_BDF_TO_SID_TABLE_N 0x2000
72 #define PCIE20_ELBI_SYS_CTRL 0x04
73 #define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
75 #define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
76 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
77 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
78 #define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
79 #define CFG_BRIDGE_SB_INIT BIT(0)
81 #define PCIE_CAP_SLOT_POWER_LIMIT_VAL FIELD_PREP(PCI_EXP_SLTCAP_SPLV, \
83 #define PCIE_CAP_SLOT_POWER_LIMIT_SCALE FIELD_PREP(PCI_EXP_SLTCAP_SPLS, \
85 #define PCIE_CAP_SLOT_VAL (PCI_EXP_SLTCAP_ABP | \
86 PCI_EXP_SLTCAP_PCP | \
87 PCI_EXP_SLTCAP_MRLSP | \
88 PCI_EXP_SLTCAP_AIP | \
89 PCI_EXP_SLTCAP_PIP | \
90 PCI_EXP_SLTCAP_HPS | \
91 PCI_EXP_SLTCAP_HPC | \
92 PCI_EXP_SLTCAP_EIP | \
93 PCIE_CAP_SLOT_POWER_LIMIT_VAL | \
94 PCIE_CAP_SLOT_POWER_LIMIT_SCALE)
96 #define PCIE20_PARF_Q2A_FLUSH 0x1AC
98 #define PCIE20_MISC_CONTROL_1_REG 0x8BC
99 #define DBI_RO_WR_EN 1
101 #define PERST_DELAY_US 1000
103 #define PCIE20_PARF_PCS_DEEMPH 0x34
104 #define PCS_DEEMPH_TX_DEEMPH_GEN1(x) ((x) << 16)
105 #define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x) ((x) << 8)
106 #define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x) ((x) << 0)
108 #define PCIE20_PARF_PCS_SWING 0x38
109 #define PCS_SWING_TX_SWING_FULL(x) ((x) << 8)
110 #define PCS_SWING_TX_SWING_LOW(x) ((x) << 0)
112 #define PCIE20_PARF_CONFIG_BITS 0x50
113 #define PHY_RX0_EQ(x) ((x) << 24)
115 #define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
116 #define SLV_ADDR_SPACE_SZ 0x10000000
118 #define PCIE20_LNK_CONTROL2_LINK_STATUS2 0xa0
120 #define DEVICE_TYPE_RC 0x4
122 #define QCOM_PCIE_2_1_0_MAX_SUPPLY 3
123 #define QCOM_PCIE_2_1_0_MAX_CLOCKS 5
125 #define QCOM_PCIE_CRC8_POLYNOMIAL (BIT(2) | BIT(1) | BIT(0))
127 struct qcom_pcie_resources_2_1_0 {
128 struct clk_bulk_data clks[QCOM_PCIE_2_1_0_MAX_CLOCKS];
129 struct reset_control *pci_reset;
130 struct reset_control *axi_reset;
131 struct reset_control *ahb_reset;
132 struct reset_control *por_reset;
133 struct reset_control *phy_reset;
134 struct reset_control *ext_reset;
135 struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
138 struct qcom_pcie_resources_1_0_0 {
141 struct clk *master_bus;
142 struct clk *slave_bus;
143 struct reset_control *core;
144 struct regulator *vdda;
147 #define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
148 struct qcom_pcie_resources_2_3_2 {
150 struct clk *master_clk;
151 struct clk *slave_clk;
153 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
156 #define QCOM_PCIE_2_4_0_MAX_CLOCKS 4
157 struct qcom_pcie_resources_2_4_0 {
158 struct clk_bulk_data clks[QCOM_PCIE_2_4_0_MAX_CLOCKS];
160 struct reset_control *axi_m_reset;
161 struct reset_control *axi_s_reset;
162 struct reset_control *pipe_reset;
163 struct reset_control *axi_m_vmid_reset;
164 struct reset_control *axi_s_xpu_reset;
165 struct reset_control *parf_reset;
166 struct reset_control *phy_reset;
167 struct reset_control *axi_m_sticky_reset;
168 struct reset_control *pipe_sticky_reset;
169 struct reset_control *pwr_reset;
170 struct reset_control *ahb_reset;
171 struct reset_control *phy_ahb_reset;
174 struct qcom_pcie_resources_2_3_3 {
176 struct clk *axi_m_clk;
177 struct clk *axi_s_clk;
180 struct reset_control *rst[7];
183 /* 6 clocks typically, 7 for sm8250 */
184 struct qcom_pcie_resources_2_7_0 {
185 struct clk_bulk_data clks[12];
187 struct regulator_bulk_data supplies[2];
188 struct reset_control *pci_reset;
191 struct qcom_pcie_resources_2_9_0 {
192 struct clk_bulk_data clks[5];
193 struct reset_control *rst;
196 union qcom_pcie_resources {
197 struct qcom_pcie_resources_1_0_0 v1_0_0;
198 struct qcom_pcie_resources_2_1_0 v2_1_0;
199 struct qcom_pcie_resources_2_3_2 v2_3_2;
200 struct qcom_pcie_resources_2_3_3 v2_3_3;
201 struct qcom_pcie_resources_2_4_0 v2_4_0;
202 struct qcom_pcie_resources_2_7_0 v2_7_0;
203 struct qcom_pcie_resources_2_9_0 v2_9_0;
208 struct qcom_pcie_ops {
209 int (*get_resources)(struct qcom_pcie *pcie);
210 int (*init)(struct qcom_pcie *pcie);
211 int (*post_init)(struct qcom_pcie *pcie);
212 void (*deinit)(struct qcom_pcie *pcie);
213 void (*ltssm_enable)(struct qcom_pcie *pcie);
214 int (*config_sid)(struct qcom_pcie *pcie);
217 struct qcom_pcie_cfg {
218 const struct qcom_pcie_ops *ops;
223 void __iomem *parf; /* DT parf */
224 void __iomem *elbi; /* DT elbi */
225 union qcom_pcie_resources res;
227 struct gpio_desc *reset;
228 struct icc_path *icc_mem;
229 const struct qcom_pcie_cfg *cfg;
232 #define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
234 static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
236 gpiod_set_value_cansleep(pcie->reset, 1);
237 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
240 static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
242 /* Ensure that PERST has been asserted for at least 100 ms */
244 gpiod_set_value_cansleep(pcie->reset, 0);
245 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
248 static int qcom_pcie_start_link(struct dw_pcie *pci)
250 struct qcom_pcie *pcie = to_qcom_pcie(pci);
252 /* Enable Link Training state machine */
253 if (pcie->cfg->ops->ltssm_enable)
254 pcie->cfg->ops->ltssm_enable(pcie);
259 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
263 /* enable link training */
264 val = readl(pcie->elbi + PCIE20_ELBI_SYS_CTRL);
265 val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
266 writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
269 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
271 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
272 struct dw_pcie *pci = pcie->pci;
273 struct device *dev = pci->dev;
276 res->supplies[0].supply = "vdda";
277 res->supplies[1].supply = "vdda_phy";
278 res->supplies[2].supply = "vdda_refclk";
279 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
284 res->clks[0].id = "iface";
285 res->clks[1].id = "core";
286 res->clks[2].id = "phy";
287 res->clks[3].id = "aux";
288 res->clks[4].id = "ref";
290 /* iface, core, phy are required */
291 ret = devm_clk_bulk_get(dev, 3, res->clks);
295 /* aux, ref are optional */
296 ret = devm_clk_bulk_get_optional(dev, 2, res->clks + 3);
300 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
301 if (IS_ERR(res->pci_reset))
302 return PTR_ERR(res->pci_reset);
304 res->axi_reset = devm_reset_control_get_exclusive(dev, "axi");
305 if (IS_ERR(res->axi_reset))
306 return PTR_ERR(res->axi_reset);
308 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
309 if (IS_ERR(res->ahb_reset))
310 return PTR_ERR(res->ahb_reset);
312 res->por_reset = devm_reset_control_get_exclusive(dev, "por");
313 if (IS_ERR(res->por_reset))
314 return PTR_ERR(res->por_reset);
316 res->ext_reset = devm_reset_control_get_optional_exclusive(dev, "ext");
317 if (IS_ERR(res->ext_reset))
318 return PTR_ERR(res->ext_reset);
320 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
321 return PTR_ERR_OR_ZERO(res->phy_reset);
324 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
326 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
328 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
329 reset_control_assert(res->pci_reset);
330 reset_control_assert(res->axi_reset);
331 reset_control_assert(res->ahb_reset);
332 reset_control_assert(res->por_reset);
333 reset_control_assert(res->ext_reset);
334 reset_control_assert(res->phy_reset);
336 writel(1, pcie->parf + PCIE20_PARF_PHY_CTRL);
338 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
341 static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
343 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
344 struct dw_pcie *pci = pcie->pci;
345 struct device *dev = pci->dev;
348 /* reset the PCIe interface as uboot can leave it undefined state */
349 reset_control_assert(res->pci_reset);
350 reset_control_assert(res->axi_reset);
351 reset_control_assert(res->ahb_reset);
352 reset_control_assert(res->por_reset);
353 reset_control_assert(res->ext_reset);
354 reset_control_assert(res->phy_reset);
356 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
358 dev_err(dev, "cannot enable regulators\n");
362 ret = reset_control_deassert(res->ahb_reset);
364 dev_err(dev, "cannot deassert ahb reset\n");
365 goto err_deassert_ahb;
368 ret = reset_control_deassert(res->ext_reset);
370 dev_err(dev, "cannot deassert ext reset\n");
371 goto err_deassert_ext;
374 ret = reset_control_deassert(res->phy_reset);
376 dev_err(dev, "cannot deassert phy reset\n");
377 goto err_deassert_phy;
380 ret = reset_control_deassert(res->pci_reset);
382 dev_err(dev, "cannot deassert pci reset\n");
383 goto err_deassert_pci;
386 ret = reset_control_deassert(res->por_reset);
388 dev_err(dev, "cannot deassert por reset\n");
389 goto err_deassert_por;
392 ret = reset_control_deassert(res->axi_reset);
394 dev_err(dev, "cannot deassert axi reset\n");
395 goto err_deassert_axi;
401 reset_control_assert(res->por_reset);
403 reset_control_assert(res->pci_reset);
405 reset_control_assert(res->phy_reset);
407 reset_control_assert(res->ext_reset);
409 reset_control_assert(res->ahb_reset);
411 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
416 static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie)
418 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
419 struct dw_pcie *pci = pcie->pci;
420 struct device *dev = pci->dev;
421 struct device_node *node = dev->of_node;
425 /* enable PCIe clocks and resets */
426 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
428 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
430 ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
434 if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
435 of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
436 writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
437 PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
438 PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
439 pcie->parf + PCIE20_PARF_PCS_DEEMPH);
440 writel(PCS_SWING_TX_SWING_FULL(120) |
441 PCS_SWING_TX_SWING_LOW(120),
442 pcie->parf + PCIE20_PARF_PCS_SWING);
443 writel(PHY_RX0_EQ(4), pcie->parf + PCIE20_PARF_CONFIG_BITS);
446 if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
447 /* set TX termination offset */
448 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
449 val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
450 val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
451 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
454 /* enable external reference clock */
455 val = readl(pcie->parf + PCIE20_PARF_PHY_REFCLK);
456 /* USE_PAD is required only for ipq806x */
457 if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
458 val &= ~PHY_REFCLK_USE_PAD;
459 val |= PHY_REFCLK_SSP_EN;
460 writel(val, pcie->parf + PCIE20_PARF_PHY_REFCLK);
462 /* wait for clock acquisition */
463 usleep_range(1000, 1500);
465 /* Set the Max TLP size to 2K, instead of using default of 4K */
466 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
467 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL0);
468 writel(CFG_BRIDGE_SB_INIT,
469 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL1);
474 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
476 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
477 struct dw_pcie *pci = pcie->pci;
478 struct device *dev = pci->dev;
480 res->vdda = devm_regulator_get(dev, "vdda");
481 if (IS_ERR(res->vdda))
482 return PTR_ERR(res->vdda);
484 res->iface = devm_clk_get(dev, "iface");
485 if (IS_ERR(res->iface))
486 return PTR_ERR(res->iface);
488 res->aux = devm_clk_get(dev, "aux");
489 if (IS_ERR(res->aux))
490 return PTR_ERR(res->aux);
492 res->master_bus = devm_clk_get(dev, "master_bus");
493 if (IS_ERR(res->master_bus))
494 return PTR_ERR(res->master_bus);
496 res->slave_bus = devm_clk_get(dev, "slave_bus");
497 if (IS_ERR(res->slave_bus))
498 return PTR_ERR(res->slave_bus);
500 res->core = devm_reset_control_get_exclusive(dev, "core");
501 return PTR_ERR_OR_ZERO(res->core);
504 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
506 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
508 reset_control_assert(res->core);
509 clk_disable_unprepare(res->slave_bus);
510 clk_disable_unprepare(res->master_bus);
511 clk_disable_unprepare(res->iface);
512 clk_disable_unprepare(res->aux);
513 regulator_disable(res->vdda);
516 static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
518 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
519 struct dw_pcie *pci = pcie->pci;
520 struct device *dev = pci->dev;
523 ret = reset_control_deassert(res->core);
525 dev_err(dev, "cannot deassert core reset\n");
529 ret = clk_prepare_enable(res->aux);
531 dev_err(dev, "cannot prepare/enable aux clock\n");
535 ret = clk_prepare_enable(res->iface);
537 dev_err(dev, "cannot prepare/enable iface clock\n");
541 ret = clk_prepare_enable(res->master_bus);
543 dev_err(dev, "cannot prepare/enable master_bus clock\n");
547 ret = clk_prepare_enable(res->slave_bus);
549 dev_err(dev, "cannot prepare/enable slave_bus clock\n");
553 ret = regulator_enable(res->vdda);
555 dev_err(dev, "cannot enable vdda regulator\n");
561 clk_disable_unprepare(res->slave_bus);
563 clk_disable_unprepare(res->master_bus);
565 clk_disable_unprepare(res->iface);
567 clk_disable_unprepare(res->aux);
569 reset_control_assert(res->core);
574 static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie)
576 /* change DBI base address */
577 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
579 if (IS_ENABLED(CONFIG_PCI_MSI)) {
580 u32 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
583 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
589 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
593 /* enable link training */
594 val = readl(pcie->parf + PCIE20_PARF_LTSSM);
596 writel(val, pcie->parf + PCIE20_PARF_LTSSM);
599 static int qcom_pcie_get_resources_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 res->supplies[0].supply = "vdda";
607 res->supplies[1].supply = "vddpe-3v3";
608 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
613 res->aux_clk = devm_clk_get(dev, "aux");
614 if (IS_ERR(res->aux_clk))
615 return PTR_ERR(res->aux_clk);
617 res->cfg_clk = devm_clk_get(dev, "cfg");
618 if (IS_ERR(res->cfg_clk))
619 return PTR_ERR(res->cfg_clk);
621 res->master_clk = devm_clk_get(dev, "bus_master");
622 if (IS_ERR(res->master_clk))
623 return PTR_ERR(res->master_clk);
625 res->slave_clk = devm_clk_get(dev, "bus_slave");
626 if (IS_ERR(res->slave_clk))
627 return PTR_ERR(res->slave_clk);
632 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
634 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
636 clk_disable_unprepare(res->slave_clk);
637 clk_disable_unprepare(res->master_clk);
638 clk_disable_unprepare(res->cfg_clk);
639 clk_disable_unprepare(res->aux_clk);
641 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
644 static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
646 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
647 struct dw_pcie *pci = pcie->pci;
648 struct device *dev = pci->dev;
651 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
653 dev_err(dev, "cannot enable regulators\n");
657 ret = clk_prepare_enable(res->aux_clk);
659 dev_err(dev, "cannot prepare/enable aux clock\n");
663 ret = clk_prepare_enable(res->cfg_clk);
665 dev_err(dev, "cannot prepare/enable cfg clock\n");
669 ret = clk_prepare_enable(res->master_clk);
671 dev_err(dev, "cannot prepare/enable master clock\n");
675 ret = clk_prepare_enable(res->slave_clk);
677 dev_err(dev, "cannot prepare/enable slave clock\n");
684 clk_disable_unprepare(res->master_clk);
686 clk_disable_unprepare(res->cfg_clk);
688 clk_disable_unprepare(res->aux_clk);
691 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
696 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
700 /* enable PCIe clocks and resets */
701 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
703 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
705 /* change DBI base address */
706 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
708 /* MAC PHY_POWERDOWN MUX DISABLE */
709 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
711 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
713 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
715 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
717 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
719 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
724 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
726 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
727 struct dw_pcie *pci = pcie->pci;
728 struct device *dev = pci->dev;
729 bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
732 res->clks[0].id = "aux";
733 res->clks[1].id = "master_bus";
734 res->clks[2].id = "slave_bus";
735 res->clks[3].id = "iface";
737 /* qcom,pcie-ipq4019 is defined without "iface" */
738 res->num_clks = is_ipq ? 3 : 4;
740 ret = devm_clk_bulk_get(dev, res->num_clks, res->clks);
744 res->axi_m_reset = devm_reset_control_get_exclusive(dev, "axi_m");
745 if (IS_ERR(res->axi_m_reset))
746 return PTR_ERR(res->axi_m_reset);
748 res->axi_s_reset = devm_reset_control_get_exclusive(dev, "axi_s");
749 if (IS_ERR(res->axi_s_reset))
750 return PTR_ERR(res->axi_s_reset);
754 * These resources relates to the PHY or are secure clocks, but
755 * are controlled here for IPQ4019
757 res->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
758 if (IS_ERR(res->pipe_reset))
759 return PTR_ERR(res->pipe_reset);
761 res->axi_m_vmid_reset = devm_reset_control_get_exclusive(dev,
763 if (IS_ERR(res->axi_m_vmid_reset))
764 return PTR_ERR(res->axi_m_vmid_reset);
766 res->axi_s_xpu_reset = devm_reset_control_get_exclusive(dev,
768 if (IS_ERR(res->axi_s_xpu_reset))
769 return PTR_ERR(res->axi_s_xpu_reset);
771 res->parf_reset = devm_reset_control_get_exclusive(dev, "parf");
772 if (IS_ERR(res->parf_reset))
773 return PTR_ERR(res->parf_reset);
775 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
776 if (IS_ERR(res->phy_reset))
777 return PTR_ERR(res->phy_reset);
780 res->axi_m_sticky_reset = devm_reset_control_get_exclusive(dev,
782 if (IS_ERR(res->axi_m_sticky_reset))
783 return PTR_ERR(res->axi_m_sticky_reset);
785 res->pipe_sticky_reset = devm_reset_control_get_exclusive(dev,
787 if (IS_ERR(res->pipe_sticky_reset))
788 return PTR_ERR(res->pipe_sticky_reset);
790 res->pwr_reset = devm_reset_control_get_exclusive(dev, "pwr");
791 if (IS_ERR(res->pwr_reset))
792 return PTR_ERR(res->pwr_reset);
794 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
795 if (IS_ERR(res->ahb_reset))
796 return PTR_ERR(res->ahb_reset);
799 res->phy_ahb_reset = devm_reset_control_get_exclusive(dev, "phy_ahb");
800 if (IS_ERR(res->phy_ahb_reset))
801 return PTR_ERR(res->phy_ahb_reset);
807 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
809 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
811 reset_control_assert(res->axi_m_reset);
812 reset_control_assert(res->axi_s_reset);
813 reset_control_assert(res->pipe_reset);
814 reset_control_assert(res->pipe_sticky_reset);
815 reset_control_assert(res->phy_reset);
816 reset_control_assert(res->phy_ahb_reset);
817 reset_control_assert(res->axi_m_sticky_reset);
818 reset_control_assert(res->pwr_reset);
819 reset_control_assert(res->ahb_reset);
820 clk_bulk_disable_unprepare(res->num_clks, res->clks);
823 static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
825 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
826 struct dw_pcie *pci = pcie->pci;
827 struct device *dev = pci->dev;
830 ret = reset_control_assert(res->axi_m_reset);
832 dev_err(dev, "cannot assert axi master reset\n");
836 ret = reset_control_assert(res->axi_s_reset);
838 dev_err(dev, "cannot assert axi slave reset\n");
842 usleep_range(10000, 12000);
844 ret = reset_control_assert(res->pipe_reset);
846 dev_err(dev, "cannot assert pipe reset\n");
850 ret = reset_control_assert(res->pipe_sticky_reset);
852 dev_err(dev, "cannot assert pipe sticky reset\n");
856 ret = reset_control_assert(res->phy_reset);
858 dev_err(dev, "cannot assert phy reset\n");
862 ret = reset_control_assert(res->phy_ahb_reset);
864 dev_err(dev, "cannot assert phy ahb reset\n");
868 usleep_range(10000, 12000);
870 ret = reset_control_assert(res->axi_m_sticky_reset);
872 dev_err(dev, "cannot assert axi master sticky reset\n");
876 ret = reset_control_assert(res->pwr_reset);
878 dev_err(dev, "cannot assert power reset\n");
882 ret = reset_control_assert(res->ahb_reset);
884 dev_err(dev, "cannot assert ahb reset\n");
888 usleep_range(10000, 12000);
890 ret = reset_control_deassert(res->phy_ahb_reset);
892 dev_err(dev, "cannot deassert phy ahb reset\n");
896 ret = reset_control_deassert(res->phy_reset);
898 dev_err(dev, "cannot deassert phy reset\n");
902 ret = reset_control_deassert(res->pipe_reset);
904 dev_err(dev, "cannot deassert pipe reset\n");
908 ret = reset_control_deassert(res->pipe_sticky_reset);
910 dev_err(dev, "cannot deassert pipe sticky reset\n");
911 goto err_rst_pipe_sticky;
914 usleep_range(10000, 12000);
916 ret = reset_control_deassert(res->axi_m_reset);
918 dev_err(dev, "cannot deassert axi master reset\n");
922 ret = reset_control_deassert(res->axi_m_sticky_reset);
924 dev_err(dev, "cannot deassert axi master sticky reset\n");
925 goto err_rst_axi_m_sticky;
928 ret = reset_control_deassert(res->axi_s_reset);
930 dev_err(dev, "cannot deassert axi slave reset\n");
934 ret = reset_control_deassert(res->pwr_reset);
936 dev_err(dev, "cannot deassert power reset\n");
940 ret = reset_control_deassert(res->ahb_reset);
942 dev_err(dev, "cannot deassert ahb reset\n");
946 usleep_range(10000, 12000);
948 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
955 reset_control_assert(res->ahb_reset);
957 reset_control_assert(res->pwr_reset);
959 reset_control_assert(res->axi_s_reset);
961 reset_control_assert(res->axi_m_sticky_reset);
962 err_rst_axi_m_sticky:
963 reset_control_assert(res->axi_m_reset);
965 reset_control_assert(res->pipe_sticky_reset);
967 reset_control_assert(res->pipe_reset);
969 reset_control_assert(res->phy_reset);
971 reset_control_assert(res->phy_ahb_reset);
975 static int qcom_pcie_post_init_2_4_0(struct qcom_pcie *pcie)
979 /* enable PCIe clocks and resets */
980 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
982 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
984 /* change DBI base address */
985 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
987 /* MAC PHY_POWERDOWN MUX DISABLE */
988 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
990 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
992 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
994 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
996 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
998 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
1003 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
1005 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1006 struct dw_pcie *pci = pcie->pci;
1007 struct device *dev = pci->dev;
1009 const char *rst_names[] = { "axi_m", "axi_s", "pipe",
1010 "axi_m_sticky", "sticky",
1013 res->iface = devm_clk_get(dev, "iface");
1014 if (IS_ERR(res->iface))
1015 return PTR_ERR(res->iface);
1017 res->axi_m_clk = devm_clk_get(dev, "axi_m");
1018 if (IS_ERR(res->axi_m_clk))
1019 return PTR_ERR(res->axi_m_clk);
1021 res->axi_s_clk = devm_clk_get(dev, "axi_s");
1022 if (IS_ERR(res->axi_s_clk))
1023 return PTR_ERR(res->axi_s_clk);
1025 res->ahb_clk = devm_clk_get(dev, "ahb");
1026 if (IS_ERR(res->ahb_clk))
1027 return PTR_ERR(res->ahb_clk);
1029 res->aux_clk = devm_clk_get(dev, "aux");
1030 if (IS_ERR(res->aux_clk))
1031 return PTR_ERR(res->aux_clk);
1033 for (i = 0; i < ARRAY_SIZE(rst_names); i++) {
1034 res->rst[i] = devm_reset_control_get(dev, rst_names[i]);
1035 if (IS_ERR(res->rst[i]))
1036 return PTR_ERR(res->rst[i]);
1042 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
1044 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1046 clk_disable_unprepare(res->iface);
1047 clk_disable_unprepare(res->axi_m_clk);
1048 clk_disable_unprepare(res->axi_s_clk);
1049 clk_disable_unprepare(res->ahb_clk);
1050 clk_disable_unprepare(res->aux_clk);
1053 static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
1055 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1056 struct dw_pcie *pci = pcie->pci;
1057 struct device *dev = pci->dev;
1060 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1061 ret = reset_control_assert(res->rst[i]);
1063 dev_err(dev, "reset #%d assert failed (%d)\n", i, ret);
1068 usleep_range(2000, 2500);
1070 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1071 ret = reset_control_deassert(res->rst[i]);
1073 dev_err(dev, "reset #%d deassert failed (%d)\n", i,
1080 * Don't have a way to see if the reset has completed.
1081 * Wait for some time.
1083 usleep_range(2000, 2500);
1085 ret = clk_prepare_enable(res->iface);
1087 dev_err(dev, "cannot prepare/enable core clock\n");
1091 ret = clk_prepare_enable(res->axi_m_clk);
1093 dev_err(dev, "cannot prepare/enable core clock\n");
1097 ret = clk_prepare_enable(res->axi_s_clk);
1099 dev_err(dev, "cannot prepare/enable axi slave clock\n");
1103 ret = clk_prepare_enable(res->ahb_clk);
1105 dev_err(dev, "cannot prepare/enable ahb clock\n");
1109 ret = clk_prepare_enable(res->aux_clk);
1111 dev_err(dev, "cannot prepare/enable aux clock\n");
1118 clk_disable_unprepare(res->ahb_clk);
1120 clk_disable_unprepare(res->axi_s_clk);
1122 clk_disable_unprepare(res->axi_m_clk);
1124 clk_disable_unprepare(res->iface);
1127 * Not checking for failure, will anyway return
1128 * the original failure in 'ret'.
1130 for (i = 0; i < ARRAY_SIZE(res->rst); i++)
1131 reset_control_assert(res->rst[i]);
1136 static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie)
1138 struct dw_pcie *pci = pcie->pci;
1139 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1142 writel(SLV_ADDR_SPACE_SZ,
1143 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1145 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1147 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1149 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1151 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
1152 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1153 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1154 pcie->parf + PCIE20_PARF_SYS_CTRL);
1155 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1157 writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
1158 writel(DBI_RO_WR_EN, pci->dbi_base + PCIE20_MISC_CONTROL_1_REG);
1159 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1161 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1162 val &= ~PCI_EXP_LNKCAP_ASPMS;
1163 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1165 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1171 static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
1173 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1174 struct dw_pcie *pci = pcie->pci;
1175 struct device *dev = pci->dev;
1176 unsigned int num_clks, num_opt_clks;
1180 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
1181 if (IS_ERR(res->pci_reset))
1182 return PTR_ERR(res->pci_reset);
1184 res->supplies[0].supply = "vdda";
1185 res->supplies[1].supply = "vddpe-3v3";
1186 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
1192 res->clks[idx++].id = "aux";
1193 res->clks[idx++].id = "cfg";
1194 res->clks[idx++].id = "bus_master";
1195 res->clks[idx++].id = "bus_slave";
1196 res->clks[idx++].id = "slave_q2a";
1200 ret = devm_clk_bulk_get(dev, num_clks, res->clks);
1204 res->clks[idx++].id = "tbu";
1205 res->clks[idx++].id = "ddrss_sf_tbu";
1206 res->clks[idx++].id = "aggre0";
1207 res->clks[idx++].id = "aggre1";
1208 res->clks[idx++].id = "noc_aggr_4";
1209 res->clks[idx++].id = "noc_aggr_south_sf";
1210 res->clks[idx++].id = "cnoc_qx";
1212 num_opt_clks = idx - num_clks;
1213 res->num_clks = idx;
1215 ret = devm_clk_bulk_get_optional(dev, num_opt_clks, res->clks + num_clks);
1222 static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
1224 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1225 struct dw_pcie *pci = pcie->pci;
1226 struct device *dev = pci->dev;
1230 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
1232 dev_err(dev, "cannot enable regulators\n");
1236 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
1238 goto err_disable_regulators;
1240 ret = reset_control_assert(res->pci_reset);
1242 dev_err(dev, "cannot assert pci reset\n");
1243 goto err_disable_clocks;
1246 usleep_range(1000, 1500);
1248 ret = reset_control_deassert(res->pci_reset);
1250 dev_err(dev, "cannot deassert pci reset\n");
1251 goto err_disable_clocks;
1254 /* Wait for reset to complete, required on SM8450 */
1255 usleep_range(1000, 1500);
1257 /* configure PCIe to RC mode */
1258 writel(DEVICE_TYPE_RC, pcie->parf + PCIE20_PARF_DEVICE_TYPE);
1260 /* enable PCIe clocks and resets */
1261 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1263 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1265 /* change DBI base address */
1266 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1268 /* MAC PHY_POWERDOWN MUX DISABLE */
1269 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
1271 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
1273 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1275 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1277 /* Enable L1 and L1SS */
1278 val = readl(pcie->parf + PCIE20_PARF_PM_CTRL);
1279 val &= ~REQ_NOT_ENTR_L1;
1280 writel(val, pcie->parf + PCIE20_PARF_PM_CTRL);
1282 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1283 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
1285 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
1290 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1291 err_disable_regulators:
1292 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1297 static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
1299 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1301 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1303 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1306 static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie)
1308 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1309 struct dw_pcie *pci = pcie->pci;
1310 struct device *dev = pci->dev;
1313 res->clks[0].id = "iface";
1314 res->clks[1].id = "axi_m";
1315 res->clks[2].id = "axi_s";
1316 res->clks[3].id = "axi_bridge";
1317 res->clks[4].id = "rchng";
1319 ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
1323 res->rst = devm_reset_control_array_get_exclusive(dev);
1324 if (IS_ERR(res->rst))
1325 return PTR_ERR(res->rst);
1330 static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie)
1332 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1334 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
1337 static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie)
1339 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1340 struct device *dev = pcie->pci->dev;
1343 ret = reset_control_assert(res->rst);
1345 dev_err(dev, "reset assert failed (%d)\n", ret);
1350 * Delay periods before and after reset deassert are working values
1351 * from downstream Codeaurora kernel
1353 usleep_range(2000, 2500);
1355 ret = reset_control_deassert(res->rst);
1357 dev_err(dev, "reset deassert failed (%d)\n", ret);
1361 usleep_range(2000, 2500);
1363 return clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
1366 static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie)
1368 struct dw_pcie *pci = pcie->pci;
1369 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1373 writel(SLV_ADDR_SPACE_SZ,
1374 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1376 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1378 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1380 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1382 writel(DEVICE_TYPE_RC, pcie->parf + PCIE20_PARF_DEVICE_TYPE);
1383 writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN,
1384 pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1385 writel(GEN3_RELATED_OFF_RXEQ_RGRDLESS_RXTS |
1386 GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL,
1387 pci->dbi_base + GEN3_RELATED_OFF);
1389 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS |
1390 SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1391 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1392 pcie->parf + PCIE20_PARF_SYS_CTRL);
1394 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1396 dw_pcie_dbi_ro_wr_en(pci);
1397 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1399 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1400 val &= ~PCI_EXP_LNKCAP_ASPMS;
1401 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1403 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1406 for (i = 0; i < 256; i++)
1407 writel(0, pcie->parf + PCIE20_PARF_BDF_TO_SID_TABLE_N + (4 * i));
1412 static int qcom_pcie_link_up(struct dw_pcie *pci)
1414 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1415 u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1417 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1420 static int qcom_pcie_config_sid_sm8250(struct qcom_pcie *pcie)
1422 /* iommu map structure */
1429 void __iomem *bdf_to_sid_base = pcie->parf + PCIE20_PARF_BDF_TO_SID_TABLE_N;
1430 struct device *dev = pcie->pci->dev;
1431 u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1432 int i, nr_map, size = 0;
1435 of_get_property(dev->of_node, "iommu-map", &size);
1439 map = kzalloc(size, GFP_KERNEL);
1443 of_property_read_u32_array(dev->of_node,
1444 "iommu-map", (u32 *)map, size / sizeof(u32));
1446 nr_map = size / (sizeof(*map));
1448 crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1450 /* Registers need to be zero out first */
1451 memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1453 /* Extract the SMMU SID base from the first entry of iommu-map */
1454 smmu_sid_base = map[0].smmu_sid;
1456 /* Look for an available entry to hold the mapping */
1457 for (i = 0; i < nr_map; i++) {
1458 __be16 bdf_be = cpu_to_be16(map[i].bdf);
1462 hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be),
1465 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1467 /* If the register is already populated, look for next available entry */
1469 u8 current_hash = hash++;
1470 u8 next_mask = 0xff;
1472 /* If NEXT field is NULL then update it with next hash */
1473 if (!(val & next_mask)) {
1475 writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1478 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1481 /* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1482 val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1483 writel(val, bdf_to_sid_base + hash * sizeof(u32));
1491 static int qcom_pcie_host_init(struct dw_pcie_rp *pp)
1493 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1494 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1497 qcom_ep_reset_assert(pcie);
1499 ret = pcie->cfg->ops->init(pcie);
1503 ret = phy_set_mode_ext(pcie->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
1507 ret = phy_power_on(pcie->phy);
1511 if (pcie->cfg->ops->post_init) {
1512 ret = pcie->cfg->ops->post_init(pcie);
1514 goto err_disable_phy;
1517 qcom_ep_reset_deassert(pcie);
1519 if (pcie->cfg->ops->config_sid) {
1520 ret = pcie->cfg->ops->config_sid(pcie);
1522 goto err_assert_reset;
1528 qcom_ep_reset_assert(pcie);
1530 phy_power_off(pcie->phy);
1532 pcie->cfg->ops->deinit(pcie);
1537 static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1538 .host_init = qcom_pcie_host_init,
1541 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1542 static const struct qcom_pcie_ops ops_2_1_0 = {
1543 .get_resources = qcom_pcie_get_resources_2_1_0,
1544 .init = qcom_pcie_init_2_1_0,
1545 .post_init = qcom_pcie_post_init_2_1_0,
1546 .deinit = qcom_pcie_deinit_2_1_0,
1547 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1550 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1551 static const struct qcom_pcie_ops ops_1_0_0 = {
1552 .get_resources = qcom_pcie_get_resources_1_0_0,
1553 .init = qcom_pcie_init_1_0_0,
1554 .post_init = qcom_pcie_post_init_1_0_0,
1555 .deinit = qcom_pcie_deinit_1_0_0,
1556 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1559 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1560 static const struct qcom_pcie_ops ops_2_3_2 = {
1561 .get_resources = qcom_pcie_get_resources_2_3_2,
1562 .init = qcom_pcie_init_2_3_2,
1563 .post_init = qcom_pcie_post_init_2_3_2,
1564 .deinit = qcom_pcie_deinit_2_3_2,
1565 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1568 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1569 static const struct qcom_pcie_ops ops_2_4_0 = {
1570 .get_resources = qcom_pcie_get_resources_2_4_0,
1571 .init = qcom_pcie_init_2_4_0,
1572 .post_init = qcom_pcie_post_init_2_4_0,
1573 .deinit = qcom_pcie_deinit_2_4_0,
1574 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1577 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1578 static const struct qcom_pcie_ops ops_2_3_3 = {
1579 .get_resources = qcom_pcie_get_resources_2_3_3,
1580 .init = qcom_pcie_init_2_3_3,
1581 .post_init = qcom_pcie_post_init_2_3_3,
1582 .deinit = qcom_pcie_deinit_2_3_3,
1583 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1586 /* Qcom IP rev.: 2.7.0 Synopsys IP rev.: 4.30a */
1587 static const struct qcom_pcie_ops ops_2_7_0 = {
1588 .get_resources = qcom_pcie_get_resources_2_7_0,
1589 .init = qcom_pcie_init_2_7_0,
1590 .deinit = qcom_pcie_deinit_2_7_0,
1591 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1594 /* Qcom IP rev.: 1.9.0 */
1595 static const struct qcom_pcie_ops ops_1_9_0 = {
1596 .get_resources = qcom_pcie_get_resources_2_7_0,
1597 .init = qcom_pcie_init_2_7_0,
1598 .deinit = qcom_pcie_deinit_2_7_0,
1599 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1600 .config_sid = qcom_pcie_config_sid_sm8250,
1603 /* Qcom IP rev.: 2.9.0 Synopsys IP rev.: 5.00a */
1604 static const struct qcom_pcie_ops ops_2_9_0 = {
1605 .get_resources = qcom_pcie_get_resources_2_9_0,
1606 .init = qcom_pcie_init_2_9_0,
1607 .post_init = qcom_pcie_post_init_2_9_0,
1608 .deinit = qcom_pcie_deinit_2_9_0,
1609 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1612 static const struct qcom_pcie_cfg cfg_1_0_0 = {
1616 static const struct qcom_pcie_cfg cfg_1_9_0 = {
1620 static const struct qcom_pcie_cfg cfg_2_1_0 = {
1624 static const struct qcom_pcie_cfg cfg_2_3_2 = {
1628 static const struct qcom_pcie_cfg cfg_2_3_3 = {
1632 static const struct qcom_pcie_cfg cfg_2_4_0 = {
1636 static const struct qcom_pcie_cfg cfg_2_7_0 = {
1640 static const struct qcom_pcie_cfg cfg_2_9_0 = {
1644 static const struct dw_pcie_ops dw_pcie_ops = {
1645 .link_up = qcom_pcie_link_up,
1646 .start_link = qcom_pcie_start_link,
1649 static int qcom_pcie_icc_init(struct qcom_pcie *pcie)
1651 struct dw_pcie *pci = pcie->pci;
1654 pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem");
1655 if (IS_ERR(pcie->icc_mem))
1656 return PTR_ERR(pcie->icc_mem);
1659 * Some Qualcomm platforms require interconnect bandwidth constraints
1660 * to be set before enabling interconnect clocks.
1662 * Set an initial peak bandwidth corresponding to single-lane Gen 1
1663 * for the pcie-mem path.
1665 ret = icc_set_bw(pcie->icc_mem, 0, MBps_to_icc(250));
1667 dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1675 static void qcom_pcie_icc_update(struct qcom_pcie *pcie)
1677 struct dw_pcie *pci = pcie->pci;
1678 u32 offset, status, bw;
1685 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1686 status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1688 /* Only update constraints if link is up. */
1689 if (!(status & PCI_EXP_LNKSTA_DLLLA))
1692 speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status);
1693 width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status);
1697 bw = MBps_to_icc(250);
1700 bw = MBps_to_icc(500);
1706 bw = MBps_to_icc(985);
1710 ret = icc_set_bw(pcie->icc_mem, 0, width * bw);
1712 dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1717 static int qcom_pcie_probe(struct platform_device *pdev)
1719 struct device *dev = &pdev->dev;
1720 struct dw_pcie_rp *pp;
1721 struct dw_pcie *pci;
1722 struct qcom_pcie *pcie;
1723 const struct qcom_pcie_cfg *pcie_cfg;
1726 pcie_cfg = of_device_get_match_data(dev);
1727 if (!pcie_cfg || !pcie_cfg->ops) {
1728 dev_err(dev, "Invalid platform data\n");
1732 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1736 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1740 pm_runtime_enable(dev);
1741 ret = pm_runtime_get_sync(dev);
1743 goto err_pm_runtime_put;
1746 pci->ops = &dw_pcie_ops;
1751 pcie->cfg = pcie_cfg;
1753 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1754 if (IS_ERR(pcie->reset)) {
1755 ret = PTR_ERR(pcie->reset);
1756 goto err_pm_runtime_put;
1759 pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1760 if (IS_ERR(pcie->parf)) {
1761 ret = PTR_ERR(pcie->parf);
1762 goto err_pm_runtime_put;
1765 pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1766 if (IS_ERR(pcie->elbi)) {
1767 ret = PTR_ERR(pcie->elbi);
1768 goto err_pm_runtime_put;
1771 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1772 if (IS_ERR(pcie->phy)) {
1773 ret = PTR_ERR(pcie->phy);
1774 goto err_pm_runtime_put;
1777 ret = qcom_pcie_icc_init(pcie);
1779 goto err_pm_runtime_put;
1781 ret = pcie->cfg->ops->get_resources(pcie);
1783 goto err_pm_runtime_put;
1785 pp->ops = &qcom_pcie_dw_ops;
1787 ret = phy_init(pcie->phy);
1789 goto err_pm_runtime_put;
1791 platform_set_drvdata(pdev, pcie);
1793 ret = dw_pcie_host_init(pp);
1795 dev_err(dev, "cannot initialize host\n");
1799 qcom_pcie_icc_update(pcie);
1804 phy_exit(pcie->phy);
1806 pm_runtime_put(dev);
1807 pm_runtime_disable(dev);
1812 static const struct of_device_id qcom_pcie_match[] = {
1813 { .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 },
1814 { .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 },
1815 { .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 },
1816 { .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 },
1817 { .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 },
1818 { .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 },
1819 { .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 },
1820 { .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 },
1821 { .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 },
1822 { .compatible = "qcom,pcie-sa8540p", .data = &cfg_1_9_0 },
1823 { .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 },
1824 { .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 },
1825 { .compatible = "qcom,pcie-sc8280xp", .data = &cfg_1_9_0 },
1826 { .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 },
1827 { .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 },
1828 { .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 },
1829 { .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 },
1830 { .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 },
1834 static void qcom_fixup_class(struct pci_dev *dev)
1836 dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1838 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1839 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1840 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1841 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1842 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1843 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1844 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1846 static struct platform_driver qcom_pcie_driver = {
1847 .probe = qcom_pcie_probe,
1849 .name = "qcom-pcie",
1850 .suppress_bind_attrs = true,
1851 .of_match_table = qcom_pcie_match,
1854 builtin_platform_driver(qcom_pcie_driver);