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.
8 * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
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"
37 #define PARF_SYS_CTRL 0x00
38 #define PARF_PM_CTRL 0x20
39 #define PARF_PCS_DEEMPH 0x34
40 #define PARF_PCS_SWING 0x38
41 #define PARF_PHY_CTRL 0x40
42 #define PARF_PHY_REFCLK 0x4c
43 #define PARF_CONFIG_BITS 0x50
44 #define PARF_DBI_BASE_ADDR 0x168
45 #define PARF_SLV_ADDR_SPACE_SIZE_2_3_3 0x16c /* Register offset specific to IP ver 2.3.3 */
46 #define PARF_MHI_CLOCK_RESET_CTRL 0x174
47 #define PARF_AXI_MSTR_WR_ADDR_HALT 0x178
48 #define PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1a8
49 #define PARF_Q2A_FLUSH 0x1ac
50 #define PARF_LTSSM 0x1b0
51 #define PARF_SID_OFFSET 0x234
52 #define PARF_BDF_TRANSLATE_CFG 0x24c
53 #define PARF_SLV_ADDR_SPACE_SIZE 0x358
54 #define PARF_DEVICE_TYPE 0x1000
55 #define PARF_BDF_TO_SID_TABLE_N 0x2000
58 #define ELBI_SYS_CTRL 0x04
61 #define AXI_MSTR_RESP_COMP_CTRL0 0x818
62 #define AXI_MSTR_RESP_COMP_CTRL1 0x81c
63 #define MISC_CONTROL_1_REG 0x8bc
65 /* PARF_SYS_CTRL register fields */
66 #define MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN BIT(29)
67 #define MST_WAKEUP_EN BIT(13)
68 #define SLV_WAKEUP_EN BIT(12)
69 #define MSTR_ACLK_CGC_DIS BIT(10)
70 #define SLV_ACLK_CGC_DIS BIT(9)
71 #define CORE_CLK_CGC_DIS BIT(6)
72 #define AUX_PWR_DET BIT(4)
73 #define L23_CLK_RMV_DIS BIT(2)
74 #define L1_CLK_RMV_DIS BIT(1)
76 /* PARF_PM_CTRL register fields */
77 #define REQ_NOT_ENTR_L1 BIT(5)
79 /* PARF_PCS_DEEMPH register fields */
80 #define PCS_DEEMPH_TX_DEEMPH_GEN1(x) FIELD_PREP(GENMASK(21, 16), x)
81 #define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x) FIELD_PREP(GENMASK(13, 8), x)
82 #define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x) FIELD_PREP(GENMASK(5, 0), x)
84 /* PARF_PCS_SWING register fields */
85 #define PCS_SWING_TX_SWING_FULL(x) FIELD_PREP(GENMASK(14, 8), x)
86 #define PCS_SWING_TX_SWING_LOW(x) FIELD_PREP(GENMASK(6, 0), x)
88 /* PARF_PHY_CTRL register fields */
89 #define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK GENMASK(20, 16)
90 #define PHY_CTRL_PHY_TX0_TERM_OFFSET(x) FIELD_PREP(PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK, x)
91 #define PHY_TEST_PWR_DOWN BIT(0)
93 /* PARF_PHY_REFCLK register fields */
94 #define PHY_REFCLK_SSP_EN BIT(16)
95 #define PHY_REFCLK_USE_PAD BIT(12)
97 /* PARF_CONFIG_BITS register fields */
98 #define PHY_RX0_EQ(x) FIELD_PREP(GENMASK(26, 24), x)
100 /* PARF_SLV_ADDR_SPACE_SIZE register value */
101 #define SLV_ADDR_SPACE_SZ 0x10000000
103 /* PARF_MHI_CLOCK_RESET_CTRL register fields */
104 #define AHB_CLK_EN BIT(0)
105 #define MSTR_AXI_CLK_EN BIT(1)
106 #define BYPASS BIT(4)
108 /* PARF_AXI_MSTR_WR_ADDR_HALT register fields */
111 /* PARF_LTSSM register fields */
112 #define LTSSM_EN BIT(8)
114 /* PARF_DEVICE_TYPE register fields */
115 #define DEVICE_TYPE_RC 0x4
117 /* ELBI_SYS_CTRL register fields */
118 #define ELBI_SYS_CTRL_LT_ENABLE BIT(0)
120 /* AXI_MSTR_RESP_COMP_CTRL0 register fields */
121 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
122 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
124 /* AXI_MSTR_RESP_COMP_CTRL1 register fields */
125 #define CFG_BRIDGE_SB_INIT BIT(0)
127 /* MISC_CONTROL_1_REG register fields */
128 #define DBI_RO_WR_EN 1
130 /* PCI_EXP_SLTCAP register fields */
131 #define PCIE_CAP_SLOT_POWER_LIMIT_VAL FIELD_PREP(PCI_EXP_SLTCAP_SPLV, 250)
132 #define PCIE_CAP_SLOT_POWER_LIMIT_SCALE FIELD_PREP(PCI_EXP_SLTCAP_SPLS, 1)
133 #define PCIE_CAP_SLOT_VAL (PCI_EXP_SLTCAP_ABP | \
134 PCI_EXP_SLTCAP_PCP | \
135 PCI_EXP_SLTCAP_MRLSP | \
136 PCI_EXP_SLTCAP_AIP | \
137 PCI_EXP_SLTCAP_PIP | \
138 PCI_EXP_SLTCAP_HPS | \
139 PCI_EXP_SLTCAP_HPC | \
140 PCI_EXP_SLTCAP_EIP | \
141 PCIE_CAP_SLOT_POWER_LIMIT_VAL | \
142 PCIE_CAP_SLOT_POWER_LIMIT_SCALE)
144 #define PERST_DELAY_US 1000
146 #define QCOM_PCIE_CRC8_POLYNOMIAL (BIT(2) | BIT(1) | BIT(0))
148 #define QCOM_PCIE_1_0_0_MAX_CLOCKS 4
149 struct qcom_pcie_resources_1_0_0 {
150 struct clk_bulk_data clks[QCOM_PCIE_1_0_0_MAX_CLOCKS];
151 struct reset_control *core;
152 struct regulator *vdda;
155 #define QCOM_PCIE_2_1_0_MAX_CLOCKS 5
156 #define QCOM_PCIE_2_1_0_MAX_RESETS 6
157 #define QCOM_PCIE_2_1_0_MAX_SUPPLY 3
158 struct qcom_pcie_resources_2_1_0 {
159 struct clk_bulk_data clks[QCOM_PCIE_2_1_0_MAX_CLOCKS];
160 struct reset_control_bulk_data resets[QCOM_PCIE_2_1_0_MAX_RESETS];
162 struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
165 #define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
166 struct qcom_pcie_resources_2_3_2 {
168 struct clk *master_clk;
169 struct clk *slave_clk;
171 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
174 #define QCOM_PCIE_2_4_0_MAX_CLOCKS 4
175 struct qcom_pcie_resources_2_4_0 {
176 struct clk_bulk_data clks[QCOM_PCIE_2_4_0_MAX_CLOCKS];
178 struct reset_control *axi_m_reset;
179 struct reset_control *axi_s_reset;
180 struct reset_control *pipe_reset;
181 struct reset_control *axi_m_vmid_reset;
182 struct reset_control *axi_s_xpu_reset;
183 struct reset_control *parf_reset;
184 struct reset_control *phy_reset;
185 struct reset_control *axi_m_sticky_reset;
186 struct reset_control *pipe_sticky_reset;
187 struct reset_control *pwr_reset;
188 struct reset_control *ahb_reset;
189 struct reset_control *phy_ahb_reset;
192 struct qcom_pcie_resources_2_3_3 {
194 struct clk *axi_m_clk;
195 struct clk *axi_s_clk;
198 struct reset_control *rst[7];
201 /* 6 clocks typically, 7 for sm8250 */
202 struct qcom_pcie_resources_2_7_0 {
203 struct clk_bulk_data clks[12];
205 struct regulator_bulk_data supplies[2];
206 struct reset_control *pci_reset;
209 struct qcom_pcie_resources_2_9_0 {
210 struct clk_bulk_data clks[5];
211 struct reset_control *rst;
214 union qcom_pcie_resources {
215 struct qcom_pcie_resources_1_0_0 v1_0_0;
216 struct qcom_pcie_resources_2_1_0 v2_1_0;
217 struct qcom_pcie_resources_2_3_2 v2_3_2;
218 struct qcom_pcie_resources_2_3_3 v2_3_3;
219 struct qcom_pcie_resources_2_4_0 v2_4_0;
220 struct qcom_pcie_resources_2_7_0 v2_7_0;
221 struct qcom_pcie_resources_2_9_0 v2_9_0;
226 struct qcom_pcie_ops {
227 int (*get_resources)(struct qcom_pcie *pcie);
228 int (*init)(struct qcom_pcie *pcie);
229 int (*post_init)(struct qcom_pcie *pcie);
230 void (*deinit)(struct qcom_pcie *pcie);
231 void (*ltssm_enable)(struct qcom_pcie *pcie);
232 int (*config_sid)(struct qcom_pcie *pcie);
235 struct qcom_pcie_cfg {
236 const struct qcom_pcie_ops *ops;
241 void __iomem *parf; /* DT parf */
242 void __iomem *elbi; /* DT elbi */
243 union qcom_pcie_resources res;
245 struct gpio_desc *reset;
246 struct icc_path *icc_mem;
247 const struct qcom_pcie_cfg *cfg;
250 #define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
252 static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
254 gpiod_set_value_cansleep(pcie->reset, 1);
255 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
258 static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
260 /* Ensure that PERST has been asserted for at least 100 ms */
262 gpiod_set_value_cansleep(pcie->reset, 0);
263 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
266 static int qcom_pcie_start_link(struct dw_pcie *pci)
268 struct qcom_pcie *pcie = to_qcom_pcie(pci);
270 /* Enable Link Training state machine */
271 if (pcie->cfg->ops->ltssm_enable)
272 pcie->cfg->ops->ltssm_enable(pcie);
277 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
281 /* enable link training */
282 val = readl(pcie->elbi + ELBI_SYS_CTRL);
283 val |= ELBI_SYS_CTRL_LT_ENABLE;
284 writel(val, pcie->elbi + ELBI_SYS_CTRL);
287 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
289 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
290 struct dw_pcie *pci = pcie->pci;
291 struct device *dev = pci->dev;
292 bool is_apq = of_device_is_compatible(dev->of_node, "qcom,pcie-apq8064");
295 res->supplies[0].supply = "vdda";
296 res->supplies[1].supply = "vdda_phy";
297 res->supplies[2].supply = "vdda_refclk";
298 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
303 res->clks[0].id = "iface";
304 res->clks[1].id = "core";
305 res->clks[2].id = "phy";
306 res->clks[3].id = "aux";
307 res->clks[4].id = "ref";
309 /* iface, core, phy are required */
310 ret = devm_clk_bulk_get(dev, 3, res->clks);
314 /* aux, ref are optional */
315 ret = devm_clk_bulk_get_optional(dev, 2, res->clks + 3);
319 res->resets[0].id = "pci";
320 res->resets[1].id = "axi";
321 res->resets[2].id = "ahb";
322 res->resets[3].id = "por";
323 res->resets[4].id = "phy";
324 res->resets[5].id = "ext";
326 /* ext is optional on APQ8016 */
327 res->num_resets = is_apq ? 5 : 6;
328 ret = devm_reset_control_bulk_get_exclusive(dev, res->num_resets, res->resets);
335 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
337 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
339 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
340 reset_control_bulk_assert(res->num_resets, res->resets);
342 writel(1, pcie->parf + PARF_PHY_CTRL);
344 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
347 static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
349 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
350 struct dw_pcie *pci = pcie->pci;
351 struct device *dev = pci->dev;
354 /* reset the PCIe interface as uboot can leave it undefined state */
355 ret = reset_control_bulk_assert(res->num_resets, res->resets);
357 dev_err(dev, "cannot assert resets\n");
361 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
363 dev_err(dev, "cannot enable regulators\n");
367 ret = reset_control_bulk_deassert(res->num_resets, res->resets);
369 dev_err(dev, "cannot deassert resets\n");
370 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
377 static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie)
379 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
380 struct dw_pcie *pci = pcie->pci;
381 struct device *dev = pci->dev;
382 struct device_node *node = dev->of_node;
386 /* enable PCIe clocks and resets */
387 val = readl(pcie->parf + PARF_PHY_CTRL);
388 val &= ~PHY_TEST_PWR_DOWN;
389 writel(val, pcie->parf + PARF_PHY_CTRL);
391 ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
395 if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
396 of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
397 writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
398 PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
399 PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
400 pcie->parf + PARF_PCS_DEEMPH);
401 writel(PCS_SWING_TX_SWING_FULL(120) |
402 PCS_SWING_TX_SWING_LOW(120),
403 pcie->parf + PARF_PCS_SWING);
404 writel(PHY_RX0_EQ(4), pcie->parf + PARF_CONFIG_BITS);
407 if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
408 /* set TX termination offset */
409 val = readl(pcie->parf + PARF_PHY_CTRL);
410 val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
411 val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
412 writel(val, pcie->parf + PARF_PHY_CTRL);
415 /* enable external reference clock */
416 val = readl(pcie->parf + PARF_PHY_REFCLK);
417 /* USE_PAD is required only for ipq806x */
418 if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
419 val &= ~PHY_REFCLK_USE_PAD;
420 val |= PHY_REFCLK_SSP_EN;
421 writel(val, pcie->parf + PARF_PHY_REFCLK);
423 /* wait for clock acquisition */
424 usleep_range(1000, 1500);
426 /* Set the Max TLP size to 2K, instead of using default of 4K */
427 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
428 pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL0);
429 writel(CFG_BRIDGE_SB_INIT,
430 pci->dbi_base + AXI_MSTR_RESP_COMP_CTRL1);
435 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
437 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
438 struct dw_pcie *pci = pcie->pci;
439 struct device *dev = pci->dev;
442 res->vdda = devm_regulator_get(dev, "vdda");
443 if (IS_ERR(res->vdda))
444 return PTR_ERR(res->vdda);
446 res->clks[0].id = "iface";
447 res->clks[1].id = "aux";
448 res->clks[2].id = "master_bus";
449 res->clks[3].id = "slave_bus";
451 ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
455 res->core = devm_reset_control_get_exclusive(dev, "core");
456 return PTR_ERR_OR_ZERO(res->core);
459 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
461 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
463 reset_control_assert(res->core);
464 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
465 regulator_disable(res->vdda);
468 static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
470 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
471 struct dw_pcie *pci = pcie->pci;
472 struct device *dev = pci->dev;
475 ret = reset_control_deassert(res->core);
477 dev_err(dev, "cannot deassert core reset\n");
481 ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
483 dev_err(dev, "cannot prepare/enable clocks\n");
484 goto err_assert_reset;
487 ret = regulator_enable(res->vdda);
489 dev_err(dev, "cannot enable vdda regulator\n");
490 goto err_disable_clks;
496 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
498 reset_control_assert(res->core);
503 static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie)
505 /* change DBI base address */
506 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
508 if (IS_ENABLED(CONFIG_PCI_MSI)) {
509 u32 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
512 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT);
518 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
522 /* enable link training */
523 val = readl(pcie->parf + PARF_LTSSM);
525 writel(val, pcie->parf + PARF_LTSSM);
528 static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
530 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
531 struct dw_pcie *pci = pcie->pci;
532 struct device *dev = pci->dev;
535 res->supplies[0].supply = "vdda";
536 res->supplies[1].supply = "vddpe-3v3";
537 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
542 res->aux_clk = devm_clk_get(dev, "aux");
543 if (IS_ERR(res->aux_clk))
544 return PTR_ERR(res->aux_clk);
546 res->cfg_clk = devm_clk_get(dev, "cfg");
547 if (IS_ERR(res->cfg_clk))
548 return PTR_ERR(res->cfg_clk);
550 res->master_clk = devm_clk_get(dev, "bus_master");
551 if (IS_ERR(res->master_clk))
552 return PTR_ERR(res->master_clk);
554 res->slave_clk = devm_clk_get(dev, "bus_slave");
555 if (IS_ERR(res->slave_clk))
556 return PTR_ERR(res->slave_clk);
561 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
563 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
565 clk_disable_unprepare(res->slave_clk);
566 clk_disable_unprepare(res->master_clk);
567 clk_disable_unprepare(res->cfg_clk);
568 clk_disable_unprepare(res->aux_clk);
570 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
573 static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
575 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
576 struct dw_pcie *pci = pcie->pci;
577 struct device *dev = pci->dev;
580 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
582 dev_err(dev, "cannot enable regulators\n");
586 ret = clk_prepare_enable(res->aux_clk);
588 dev_err(dev, "cannot prepare/enable aux clock\n");
592 ret = clk_prepare_enable(res->cfg_clk);
594 dev_err(dev, "cannot prepare/enable cfg clock\n");
598 ret = clk_prepare_enable(res->master_clk);
600 dev_err(dev, "cannot prepare/enable master clock\n");
604 ret = clk_prepare_enable(res->slave_clk);
606 dev_err(dev, "cannot prepare/enable slave clock\n");
613 clk_disable_unprepare(res->master_clk);
615 clk_disable_unprepare(res->cfg_clk);
617 clk_disable_unprepare(res->aux_clk);
620 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
625 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
629 /* enable PCIe clocks and resets */
630 val = readl(pcie->parf + PARF_PHY_CTRL);
631 val &= ~PHY_TEST_PWR_DOWN;
632 writel(val, pcie->parf + PARF_PHY_CTRL);
634 /* change DBI base address */
635 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
637 /* MAC PHY_POWERDOWN MUX DISABLE */
638 val = readl(pcie->parf + PARF_SYS_CTRL);
639 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
640 writel(val, pcie->parf + PARF_SYS_CTRL);
642 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
644 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
646 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
648 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
653 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
655 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
656 struct dw_pcie *pci = pcie->pci;
657 struct device *dev = pci->dev;
658 bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
661 res->clks[0].id = "aux";
662 res->clks[1].id = "master_bus";
663 res->clks[2].id = "slave_bus";
664 res->clks[3].id = "iface";
666 /* qcom,pcie-ipq4019 is defined without "iface" */
667 res->num_clks = is_ipq ? 3 : 4;
669 ret = devm_clk_bulk_get(dev, res->num_clks, res->clks);
673 res->axi_m_reset = devm_reset_control_get_exclusive(dev, "axi_m");
674 if (IS_ERR(res->axi_m_reset))
675 return PTR_ERR(res->axi_m_reset);
677 res->axi_s_reset = devm_reset_control_get_exclusive(dev, "axi_s");
678 if (IS_ERR(res->axi_s_reset))
679 return PTR_ERR(res->axi_s_reset);
683 * These resources relates to the PHY or are secure clocks, but
684 * are controlled here for IPQ4019
686 res->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
687 if (IS_ERR(res->pipe_reset))
688 return PTR_ERR(res->pipe_reset);
690 res->axi_m_vmid_reset = devm_reset_control_get_exclusive(dev,
692 if (IS_ERR(res->axi_m_vmid_reset))
693 return PTR_ERR(res->axi_m_vmid_reset);
695 res->axi_s_xpu_reset = devm_reset_control_get_exclusive(dev,
697 if (IS_ERR(res->axi_s_xpu_reset))
698 return PTR_ERR(res->axi_s_xpu_reset);
700 res->parf_reset = devm_reset_control_get_exclusive(dev, "parf");
701 if (IS_ERR(res->parf_reset))
702 return PTR_ERR(res->parf_reset);
704 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
705 if (IS_ERR(res->phy_reset))
706 return PTR_ERR(res->phy_reset);
709 res->axi_m_sticky_reset = devm_reset_control_get_exclusive(dev,
711 if (IS_ERR(res->axi_m_sticky_reset))
712 return PTR_ERR(res->axi_m_sticky_reset);
714 res->pipe_sticky_reset = devm_reset_control_get_exclusive(dev,
716 if (IS_ERR(res->pipe_sticky_reset))
717 return PTR_ERR(res->pipe_sticky_reset);
719 res->pwr_reset = devm_reset_control_get_exclusive(dev, "pwr");
720 if (IS_ERR(res->pwr_reset))
721 return PTR_ERR(res->pwr_reset);
723 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
724 if (IS_ERR(res->ahb_reset))
725 return PTR_ERR(res->ahb_reset);
728 res->phy_ahb_reset = devm_reset_control_get_exclusive(dev, "phy_ahb");
729 if (IS_ERR(res->phy_ahb_reset))
730 return PTR_ERR(res->phy_ahb_reset);
736 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
738 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
740 reset_control_assert(res->axi_m_reset);
741 reset_control_assert(res->axi_s_reset);
742 reset_control_assert(res->pipe_reset);
743 reset_control_assert(res->pipe_sticky_reset);
744 reset_control_assert(res->phy_reset);
745 reset_control_assert(res->phy_ahb_reset);
746 reset_control_assert(res->axi_m_sticky_reset);
747 reset_control_assert(res->pwr_reset);
748 reset_control_assert(res->ahb_reset);
749 clk_bulk_disable_unprepare(res->num_clks, res->clks);
752 static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
754 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
755 struct dw_pcie *pci = pcie->pci;
756 struct device *dev = pci->dev;
759 ret = reset_control_assert(res->axi_m_reset);
761 dev_err(dev, "cannot assert axi master reset\n");
765 ret = reset_control_assert(res->axi_s_reset);
767 dev_err(dev, "cannot assert axi slave reset\n");
771 usleep_range(10000, 12000);
773 ret = reset_control_assert(res->pipe_reset);
775 dev_err(dev, "cannot assert pipe reset\n");
779 ret = reset_control_assert(res->pipe_sticky_reset);
781 dev_err(dev, "cannot assert pipe sticky reset\n");
785 ret = reset_control_assert(res->phy_reset);
787 dev_err(dev, "cannot assert phy reset\n");
791 ret = reset_control_assert(res->phy_ahb_reset);
793 dev_err(dev, "cannot assert phy ahb reset\n");
797 usleep_range(10000, 12000);
799 ret = reset_control_assert(res->axi_m_sticky_reset);
801 dev_err(dev, "cannot assert axi master sticky reset\n");
805 ret = reset_control_assert(res->pwr_reset);
807 dev_err(dev, "cannot assert power reset\n");
811 ret = reset_control_assert(res->ahb_reset);
813 dev_err(dev, "cannot assert ahb reset\n");
817 usleep_range(10000, 12000);
819 ret = reset_control_deassert(res->phy_ahb_reset);
821 dev_err(dev, "cannot deassert phy ahb reset\n");
825 ret = reset_control_deassert(res->phy_reset);
827 dev_err(dev, "cannot deassert phy reset\n");
831 ret = reset_control_deassert(res->pipe_reset);
833 dev_err(dev, "cannot deassert pipe reset\n");
837 ret = reset_control_deassert(res->pipe_sticky_reset);
839 dev_err(dev, "cannot deassert pipe sticky reset\n");
840 goto err_rst_pipe_sticky;
843 usleep_range(10000, 12000);
845 ret = reset_control_deassert(res->axi_m_reset);
847 dev_err(dev, "cannot deassert axi master reset\n");
851 ret = reset_control_deassert(res->axi_m_sticky_reset);
853 dev_err(dev, "cannot deassert axi master sticky reset\n");
854 goto err_rst_axi_m_sticky;
857 ret = reset_control_deassert(res->axi_s_reset);
859 dev_err(dev, "cannot deassert axi slave reset\n");
863 ret = reset_control_deassert(res->pwr_reset);
865 dev_err(dev, "cannot deassert power reset\n");
869 ret = reset_control_deassert(res->ahb_reset);
871 dev_err(dev, "cannot deassert ahb reset\n");
875 usleep_range(10000, 12000);
877 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
884 reset_control_assert(res->ahb_reset);
886 reset_control_assert(res->pwr_reset);
888 reset_control_assert(res->axi_s_reset);
890 reset_control_assert(res->axi_m_sticky_reset);
891 err_rst_axi_m_sticky:
892 reset_control_assert(res->axi_m_reset);
894 reset_control_assert(res->pipe_sticky_reset);
896 reset_control_assert(res->pipe_reset);
898 reset_control_assert(res->phy_reset);
900 reset_control_assert(res->phy_ahb_reset);
904 static int qcom_pcie_post_init_2_4_0(struct qcom_pcie *pcie)
908 /* enable PCIe clocks and resets */
909 val = readl(pcie->parf + PARF_PHY_CTRL);
910 val &= ~PHY_TEST_PWR_DOWN;
911 writel(val, pcie->parf + PARF_PHY_CTRL);
913 /* change DBI base address */
914 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
916 /* MAC PHY_POWERDOWN MUX DISABLE */
917 val = readl(pcie->parf + PARF_SYS_CTRL);
918 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
919 writel(val, pcie->parf + PARF_SYS_CTRL);
921 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
923 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
925 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
927 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
932 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
934 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
935 struct dw_pcie *pci = pcie->pci;
936 struct device *dev = pci->dev;
938 const char *rst_names[] = { "axi_m", "axi_s", "pipe",
939 "axi_m_sticky", "sticky",
942 res->iface = devm_clk_get(dev, "iface");
943 if (IS_ERR(res->iface))
944 return PTR_ERR(res->iface);
946 res->axi_m_clk = devm_clk_get(dev, "axi_m");
947 if (IS_ERR(res->axi_m_clk))
948 return PTR_ERR(res->axi_m_clk);
950 res->axi_s_clk = devm_clk_get(dev, "axi_s");
951 if (IS_ERR(res->axi_s_clk))
952 return PTR_ERR(res->axi_s_clk);
954 res->ahb_clk = devm_clk_get(dev, "ahb");
955 if (IS_ERR(res->ahb_clk))
956 return PTR_ERR(res->ahb_clk);
958 res->aux_clk = devm_clk_get(dev, "aux");
959 if (IS_ERR(res->aux_clk))
960 return PTR_ERR(res->aux_clk);
962 for (i = 0; i < ARRAY_SIZE(rst_names); i++) {
963 res->rst[i] = devm_reset_control_get(dev, rst_names[i]);
964 if (IS_ERR(res->rst[i]))
965 return PTR_ERR(res->rst[i]);
971 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
973 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
975 clk_disable_unprepare(res->iface);
976 clk_disable_unprepare(res->axi_m_clk);
977 clk_disable_unprepare(res->axi_s_clk);
978 clk_disable_unprepare(res->ahb_clk);
979 clk_disable_unprepare(res->aux_clk);
982 static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
984 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
985 struct dw_pcie *pci = pcie->pci;
986 struct device *dev = pci->dev;
989 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
990 ret = reset_control_assert(res->rst[i]);
992 dev_err(dev, "reset #%d assert failed (%d)\n", i, ret);
997 usleep_range(2000, 2500);
999 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1000 ret = reset_control_deassert(res->rst[i]);
1002 dev_err(dev, "reset #%d deassert failed (%d)\n", i,
1009 * Don't have a way to see if the reset has completed.
1010 * Wait for some time.
1012 usleep_range(2000, 2500);
1014 ret = clk_prepare_enable(res->iface);
1016 dev_err(dev, "cannot prepare/enable core clock\n");
1020 ret = clk_prepare_enable(res->axi_m_clk);
1022 dev_err(dev, "cannot prepare/enable core clock\n");
1026 ret = clk_prepare_enable(res->axi_s_clk);
1028 dev_err(dev, "cannot prepare/enable axi slave clock\n");
1032 ret = clk_prepare_enable(res->ahb_clk);
1034 dev_err(dev, "cannot prepare/enable ahb clock\n");
1038 ret = clk_prepare_enable(res->aux_clk);
1040 dev_err(dev, "cannot prepare/enable aux clock\n");
1047 clk_disable_unprepare(res->ahb_clk);
1049 clk_disable_unprepare(res->axi_s_clk);
1051 clk_disable_unprepare(res->axi_m_clk);
1053 clk_disable_unprepare(res->iface);
1056 * Not checking for failure, will anyway return
1057 * the original failure in 'ret'.
1059 for (i = 0; i < ARRAY_SIZE(res->rst); i++)
1060 reset_control_assert(res->rst[i]);
1065 static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie)
1067 struct dw_pcie *pci = pcie->pci;
1068 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1071 writel(SLV_ADDR_SPACE_SZ,
1072 pcie->parf + PARF_SLV_ADDR_SPACE_SIZE_2_3_3);
1074 val = readl(pcie->parf + PARF_PHY_CTRL);
1075 val &= ~PHY_TEST_PWR_DOWN;
1076 writel(val, pcie->parf + PARF_PHY_CTRL);
1078 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
1080 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
1081 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1082 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1083 pcie->parf + PARF_SYS_CTRL);
1084 writel(0, pcie->parf + PARF_Q2A_FLUSH);
1086 writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
1087 writel(DBI_RO_WR_EN, pci->dbi_base + MISC_CONTROL_1_REG);
1088 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1090 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1091 val &= ~PCI_EXP_LNKCAP_ASPMS;
1092 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1094 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1100 static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
1102 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1103 struct dw_pcie *pci = pcie->pci;
1104 struct device *dev = pci->dev;
1105 unsigned int num_clks, num_opt_clks;
1109 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
1110 if (IS_ERR(res->pci_reset))
1111 return PTR_ERR(res->pci_reset);
1113 res->supplies[0].supply = "vdda";
1114 res->supplies[1].supply = "vddpe-3v3";
1115 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
1121 res->clks[idx++].id = "aux";
1122 res->clks[idx++].id = "cfg";
1123 res->clks[idx++].id = "bus_master";
1124 res->clks[idx++].id = "bus_slave";
1125 res->clks[idx++].id = "slave_q2a";
1129 ret = devm_clk_bulk_get(dev, num_clks, res->clks);
1133 res->clks[idx++].id = "tbu";
1134 res->clks[idx++].id = "ddrss_sf_tbu";
1135 res->clks[idx++].id = "aggre0";
1136 res->clks[idx++].id = "aggre1";
1137 res->clks[idx++].id = "noc_aggr_4";
1138 res->clks[idx++].id = "noc_aggr_south_sf";
1139 res->clks[idx++].id = "cnoc_qx";
1141 num_opt_clks = idx - num_clks;
1142 res->num_clks = idx;
1144 ret = devm_clk_bulk_get_optional(dev, num_opt_clks, res->clks + num_clks);
1151 static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
1153 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1154 struct dw_pcie *pci = pcie->pci;
1155 struct device *dev = pci->dev;
1159 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
1161 dev_err(dev, "cannot enable regulators\n");
1165 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
1167 goto err_disable_regulators;
1169 ret = reset_control_assert(res->pci_reset);
1171 dev_err(dev, "cannot assert pci reset\n");
1172 goto err_disable_clocks;
1175 usleep_range(1000, 1500);
1177 ret = reset_control_deassert(res->pci_reset);
1179 dev_err(dev, "cannot deassert pci reset\n");
1180 goto err_disable_clocks;
1183 /* Wait for reset to complete, required on SM8450 */
1184 usleep_range(1000, 1500);
1186 /* configure PCIe to RC mode */
1187 writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
1189 /* enable PCIe clocks and resets */
1190 val = readl(pcie->parf + PARF_PHY_CTRL);
1191 val &= ~PHY_TEST_PWR_DOWN;
1192 writel(val, pcie->parf + PARF_PHY_CTRL);
1194 /* change DBI base address */
1195 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
1197 /* MAC PHY_POWERDOWN MUX DISABLE */
1198 val = readl(pcie->parf + PARF_SYS_CTRL);
1199 val &= ~MAC_PHY_POWERDOWN_IN_P2_D_MUX_EN;
1200 writel(val, pcie->parf + PARF_SYS_CTRL);
1202 val = readl(pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
1204 writel(val, pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
1206 /* Enable L1 and L1SS */
1207 val = readl(pcie->parf + PARF_PM_CTRL);
1208 val &= ~REQ_NOT_ENTR_L1;
1209 writel(val, pcie->parf + PARF_PM_CTRL);
1211 val = readl(pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
1213 writel(val, pcie->parf + PARF_AXI_MSTR_WR_ADDR_HALT_V2);
1217 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1218 err_disable_regulators:
1219 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1224 static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
1226 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1228 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1230 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1233 static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie)
1235 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1236 struct dw_pcie *pci = pcie->pci;
1237 struct device *dev = pci->dev;
1240 res->clks[0].id = "iface";
1241 res->clks[1].id = "axi_m";
1242 res->clks[2].id = "axi_s";
1243 res->clks[3].id = "axi_bridge";
1244 res->clks[4].id = "rchng";
1246 ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
1250 res->rst = devm_reset_control_array_get_exclusive(dev);
1251 if (IS_ERR(res->rst))
1252 return PTR_ERR(res->rst);
1257 static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie)
1259 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1261 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
1264 static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie)
1266 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1267 struct device *dev = pcie->pci->dev;
1270 ret = reset_control_assert(res->rst);
1272 dev_err(dev, "reset assert failed (%d)\n", ret);
1277 * Delay periods before and after reset deassert are working values
1278 * from downstream Codeaurora kernel
1280 usleep_range(2000, 2500);
1282 ret = reset_control_deassert(res->rst);
1284 dev_err(dev, "reset deassert failed (%d)\n", ret);
1288 usleep_range(2000, 2500);
1290 return clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
1293 static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie)
1295 struct dw_pcie *pci = pcie->pci;
1296 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1300 writel(SLV_ADDR_SPACE_SZ,
1301 pcie->parf + PARF_SLV_ADDR_SPACE_SIZE);
1303 val = readl(pcie->parf + PARF_PHY_CTRL);
1304 val &= ~PHY_TEST_PWR_DOWN;
1305 writel(val, pcie->parf + PARF_PHY_CTRL);
1307 writel(0, pcie->parf + PARF_DBI_BASE_ADDR);
1309 writel(DEVICE_TYPE_RC, pcie->parf + PARF_DEVICE_TYPE);
1310 writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN,
1311 pcie->parf + PARF_MHI_CLOCK_RESET_CTRL);
1312 writel(GEN3_RELATED_OFF_RXEQ_RGRDLESS_RXTS |
1313 GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL,
1314 pci->dbi_base + GEN3_RELATED_OFF);
1316 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS |
1317 SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1318 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1319 pcie->parf + PARF_SYS_CTRL);
1321 writel(0, pcie->parf + PARF_Q2A_FLUSH);
1323 dw_pcie_dbi_ro_wr_en(pci);
1324 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1326 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1327 val &= ~PCI_EXP_LNKCAP_ASPMS;
1328 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1330 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1333 for (i = 0; i < 256; i++)
1334 writel(0, pcie->parf + PARF_BDF_TO_SID_TABLE_N + (4 * i));
1339 static int qcom_pcie_link_up(struct dw_pcie *pci)
1341 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1342 u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1344 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1347 static int qcom_pcie_config_sid_sm8250(struct qcom_pcie *pcie)
1349 /* iommu map structure */
1356 void __iomem *bdf_to_sid_base = pcie->parf + PARF_BDF_TO_SID_TABLE_N;
1357 struct device *dev = pcie->pci->dev;
1358 u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1359 int i, nr_map, size = 0;
1362 of_get_property(dev->of_node, "iommu-map", &size);
1366 map = kzalloc(size, GFP_KERNEL);
1370 of_property_read_u32_array(dev->of_node,
1371 "iommu-map", (u32 *)map, size / sizeof(u32));
1373 nr_map = size / (sizeof(*map));
1375 crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1377 /* Registers need to be zero out first */
1378 memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1380 /* Extract the SMMU SID base from the first entry of iommu-map */
1381 smmu_sid_base = map[0].smmu_sid;
1383 /* Look for an available entry to hold the mapping */
1384 for (i = 0; i < nr_map; i++) {
1385 __be16 bdf_be = cpu_to_be16(map[i].bdf);
1389 hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be),
1392 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1394 /* If the register is already populated, look for next available entry */
1396 u8 current_hash = hash++;
1397 u8 next_mask = 0xff;
1399 /* If NEXT field is NULL then update it with next hash */
1400 if (!(val & next_mask)) {
1402 writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1405 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1408 /* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1409 val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1410 writel(val, bdf_to_sid_base + hash * sizeof(u32));
1418 static int qcom_pcie_host_init(struct dw_pcie_rp *pp)
1420 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1421 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1424 qcom_ep_reset_assert(pcie);
1426 ret = pcie->cfg->ops->init(pcie);
1430 ret = phy_set_mode_ext(pcie->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
1434 ret = phy_power_on(pcie->phy);
1438 if (pcie->cfg->ops->post_init) {
1439 ret = pcie->cfg->ops->post_init(pcie);
1441 goto err_disable_phy;
1444 qcom_ep_reset_deassert(pcie);
1446 if (pcie->cfg->ops->config_sid) {
1447 ret = pcie->cfg->ops->config_sid(pcie);
1449 goto err_assert_reset;
1455 qcom_ep_reset_assert(pcie);
1457 phy_power_off(pcie->phy);
1459 pcie->cfg->ops->deinit(pcie);
1464 static void qcom_pcie_host_deinit(struct dw_pcie_rp *pp)
1466 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1467 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1469 qcom_ep_reset_assert(pcie);
1470 phy_power_off(pcie->phy);
1471 pcie->cfg->ops->deinit(pcie);
1474 static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1475 .host_init = qcom_pcie_host_init,
1476 .host_deinit = qcom_pcie_host_deinit,
1479 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1480 static const struct qcom_pcie_ops ops_2_1_0 = {
1481 .get_resources = qcom_pcie_get_resources_2_1_0,
1482 .init = qcom_pcie_init_2_1_0,
1483 .post_init = qcom_pcie_post_init_2_1_0,
1484 .deinit = qcom_pcie_deinit_2_1_0,
1485 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1488 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1489 static const struct qcom_pcie_ops ops_1_0_0 = {
1490 .get_resources = qcom_pcie_get_resources_1_0_0,
1491 .init = qcom_pcie_init_1_0_0,
1492 .post_init = qcom_pcie_post_init_1_0_0,
1493 .deinit = qcom_pcie_deinit_1_0_0,
1494 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1497 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1498 static const struct qcom_pcie_ops ops_2_3_2 = {
1499 .get_resources = qcom_pcie_get_resources_2_3_2,
1500 .init = qcom_pcie_init_2_3_2,
1501 .post_init = qcom_pcie_post_init_2_3_2,
1502 .deinit = qcom_pcie_deinit_2_3_2,
1503 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1506 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1507 static const struct qcom_pcie_ops ops_2_4_0 = {
1508 .get_resources = qcom_pcie_get_resources_2_4_0,
1509 .init = qcom_pcie_init_2_4_0,
1510 .post_init = qcom_pcie_post_init_2_4_0,
1511 .deinit = qcom_pcie_deinit_2_4_0,
1512 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1515 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1516 static const struct qcom_pcie_ops ops_2_3_3 = {
1517 .get_resources = qcom_pcie_get_resources_2_3_3,
1518 .init = qcom_pcie_init_2_3_3,
1519 .post_init = qcom_pcie_post_init_2_3_3,
1520 .deinit = qcom_pcie_deinit_2_3_3,
1521 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1524 /* Qcom IP rev.: 2.7.0 Synopsys IP rev.: 4.30a */
1525 static const struct qcom_pcie_ops ops_2_7_0 = {
1526 .get_resources = qcom_pcie_get_resources_2_7_0,
1527 .init = qcom_pcie_init_2_7_0,
1528 .deinit = qcom_pcie_deinit_2_7_0,
1529 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1532 /* Qcom IP rev.: 1.9.0 */
1533 static const struct qcom_pcie_ops ops_1_9_0 = {
1534 .get_resources = qcom_pcie_get_resources_2_7_0,
1535 .init = qcom_pcie_init_2_7_0,
1536 .deinit = qcom_pcie_deinit_2_7_0,
1537 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1538 .config_sid = qcom_pcie_config_sid_sm8250,
1541 /* Qcom IP rev.: 2.9.0 Synopsys IP rev.: 5.00a */
1542 static const struct qcom_pcie_ops ops_2_9_0 = {
1543 .get_resources = qcom_pcie_get_resources_2_9_0,
1544 .init = qcom_pcie_init_2_9_0,
1545 .post_init = qcom_pcie_post_init_2_9_0,
1546 .deinit = qcom_pcie_deinit_2_9_0,
1547 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1550 static const struct qcom_pcie_cfg cfg_1_0_0 = {
1554 static const struct qcom_pcie_cfg cfg_1_9_0 = {
1558 static const struct qcom_pcie_cfg cfg_2_1_0 = {
1562 static const struct qcom_pcie_cfg cfg_2_3_2 = {
1566 static const struct qcom_pcie_cfg cfg_2_3_3 = {
1570 static const struct qcom_pcie_cfg cfg_2_4_0 = {
1574 static const struct qcom_pcie_cfg cfg_2_7_0 = {
1578 static const struct qcom_pcie_cfg cfg_2_9_0 = {
1582 static const struct dw_pcie_ops dw_pcie_ops = {
1583 .link_up = qcom_pcie_link_up,
1584 .start_link = qcom_pcie_start_link,
1587 static int qcom_pcie_icc_init(struct qcom_pcie *pcie)
1589 struct dw_pcie *pci = pcie->pci;
1592 pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem");
1593 if (IS_ERR(pcie->icc_mem))
1594 return PTR_ERR(pcie->icc_mem);
1597 * Some Qualcomm platforms require interconnect bandwidth constraints
1598 * to be set before enabling interconnect clocks.
1600 * Set an initial peak bandwidth corresponding to single-lane Gen 1
1601 * for the pcie-mem path.
1603 ret = icc_set_bw(pcie->icc_mem, 0, MBps_to_icc(250));
1605 dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1613 static void qcom_pcie_icc_update(struct qcom_pcie *pcie)
1615 struct dw_pcie *pci = pcie->pci;
1616 u32 offset, status, bw;
1623 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1624 status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1626 /* Only update constraints if link is up. */
1627 if (!(status & PCI_EXP_LNKSTA_DLLLA))
1630 speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status);
1631 width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status);
1635 bw = MBps_to_icc(250);
1638 bw = MBps_to_icc(500);
1644 bw = MBps_to_icc(985);
1648 ret = icc_set_bw(pcie->icc_mem, 0, width * bw);
1650 dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1655 static int qcom_pcie_probe(struct platform_device *pdev)
1657 struct device *dev = &pdev->dev;
1658 struct dw_pcie_rp *pp;
1659 struct dw_pcie *pci;
1660 struct qcom_pcie *pcie;
1661 const struct qcom_pcie_cfg *pcie_cfg;
1664 pcie_cfg = of_device_get_match_data(dev);
1665 if (!pcie_cfg || !pcie_cfg->ops) {
1666 dev_err(dev, "Invalid platform data\n");
1670 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1674 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1678 pm_runtime_enable(dev);
1679 ret = pm_runtime_get_sync(dev);
1681 goto err_pm_runtime_put;
1684 pci->ops = &dw_pcie_ops;
1689 pcie->cfg = pcie_cfg;
1691 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1692 if (IS_ERR(pcie->reset)) {
1693 ret = PTR_ERR(pcie->reset);
1694 goto err_pm_runtime_put;
1697 pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1698 if (IS_ERR(pcie->parf)) {
1699 ret = PTR_ERR(pcie->parf);
1700 goto err_pm_runtime_put;
1703 pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1704 if (IS_ERR(pcie->elbi)) {
1705 ret = PTR_ERR(pcie->elbi);
1706 goto err_pm_runtime_put;
1709 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1710 if (IS_ERR(pcie->phy)) {
1711 ret = PTR_ERR(pcie->phy);
1712 goto err_pm_runtime_put;
1715 ret = qcom_pcie_icc_init(pcie);
1717 goto err_pm_runtime_put;
1719 ret = pcie->cfg->ops->get_resources(pcie);
1721 goto err_pm_runtime_put;
1723 pp->ops = &qcom_pcie_dw_ops;
1725 ret = phy_init(pcie->phy);
1727 goto err_pm_runtime_put;
1729 platform_set_drvdata(pdev, pcie);
1731 ret = dw_pcie_host_init(pp);
1733 dev_err(dev, "cannot initialize host\n");
1737 qcom_pcie_icc_update(pcie);
1742 phy_exit(pcie->phy);
1744 pm_runtime_put(dev);
1745 pm_runtime_disable(dev);
1750 static const struct of_device_id qcom_pcie_match[] = {
1751 { .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 },
1752 { .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 },
1753 { .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 },
1754 { .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 },
1755 { .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 },
1756 { .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 },
1757 { .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 },
1758 { .compatible = "qcom,pcie-ipq8074-gen3", .data = &cfg_2_9_0 },
1759 { .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 },
1760 { .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 },
1761 { .compatible = "qcom,pcie-sa8540p", .data = &cfg_1_9_0 },
1762 { .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 },
1763 { .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 },
1764 { .compatible = "qcom,pcie-sc8280xp", .data = &cfg_1_9_0 },
1765 { .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 },
1766 { .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 },
1767 { .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 },
1768 { .compatible = "qcom,pcie-sm8350", .data = &cfg_1_9_0 },
1769 { .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 },
1770 { .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 },
1774 static void qcom_fixup_class(struct pci_dev *dev)
1776 dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1778 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1779 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1780 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1781 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1782 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1783 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1784 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1786 static struct platform_driver qcom_pcie_driver = {
1787 .probe = qcom_pcie_probe,
1789 .name = "qcom-pcie",
1790 .suppress_bind_attrs = true,
1791 .of_match_table = qcom_pcie_match,
1794 builtin_platform_driver(qcom_pcie_driver);