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/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/interrupt.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/of_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/platform_device.h>
24 #include <linux/phy/phy.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/reset.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
30 #include "pcie-designware.h"
32 #define PCIE20_PARF_SYS_CTRL 0x00
33 #define MST_WAKEUP_EN BIT(13)
34 #define SLV_WAKEUP_EN BIT(12)
35 #define MSTR_ACLK_CGC_DIS BIT(10)
36 #define SLV_ACLK_CGC_DIS BIT(9)
37 #define CORE_CLK_CGC_DIS BIT(6)
38 #define AUX_PWR_DET BIT(4)
39 #define L23_CLK_RMV_DIS BIT(2)
40 #define L1_CLK_RMV_DIS BIT(1)
42 #define PCIE20_COMMAND_STATUS 0x04
43 #define CMD_BME_VAL 0x4
44 #define PCIE20_DEVICE_CONTROL2_STATUS2 0x98
45 #define PCIE_CAP_CPL_TIMEOUT_DISABLE 0x10
47 #define PCIE20_PARF_PHY_CTRL 0x40
48 #define PCIE20_PARF_PHY_REFCLK 0x4C
49 #define PCIE20_PARF_DBI_BASE_ADDR 0x168
50 #define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
51 #define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
52 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
53 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
54 #define PCIE20_PARF_LTSSM 0x1B0
55 #define PCIE20_PARF_SID_OFFSET 0x234
56 #define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
58 #define PCIE20_ELBI_SYS_CTRL 0x04
59 #define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
61 #define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
62 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
63 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
64 #define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
65 #define CFG_BRIDGE_SB_INIT BIT(0)
67 #define PCIE20_CAP 0x70
68 #define PCIE20_CAP_LINK_CAPABILITIES (PCIE20_CAP + 0xC)
69 #define PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT (BIT(10) | BIT(11))
70 #define PCIE20_CAP_LINK_1 (PCIE20_CAP + 0x14)
71 #define PCIE_CAP_LINK1_VAL 0x2FD7F
73 #define PCIE20_PARF_Q2A_FLUSH 0x1AC
75 #define PCIE20_MISC_CONTROL_1_REG 0x8BC
76 #define DBI_RO_WR_EN 1
78 #define PERST_DELAY_US 1000
80 #define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
81 #define SLV_ADDR_SPACE_SZ 0x10000000
83 #define QCOM_PCIE_2_1_0_MAX_SUPPLY 3
84 struct qcom_pcie_resources_2_1_0
{
85 struct clk
*iface_clk
;
88 struct reset_control
*pci_reset
;
89 struct reset_control
*axi_reset
;
90 struct reset_control
*ahb_reset
;
91 struct reset_control
*por_reset
;
92 struct reset_control
*phy_reset
;
93 struct regulator_bulk_data supplies
[QCOM_PCIE_2_1_0_MAX_SUPPLY
];
96 struct qcom_pcie_resources_1_0_0
{
99 struct clk
*master_bus
;
100 struct clk
*slave_bus
;
101 struct reset_control
*core
;
102 struct regulator
*vdda
;
105 #define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
106 struct qcom_pcie_resources_2_3_2
{
108 struct clk
*master_clk
;
109 struct clk
*slave_clk
;
111 struct clk
*pipe_clk
;
112 struct regulator_bulk_data supplies
[QCOM_PCIE_2_3_2_MAX_SUPPLY
];
115 struct qcom_pcie_resources_2_4_0
{
117 struct clk
*master_clk
;
118 struct clk
*slave_clk
;
119 struct reset_control
*axi_m_reset
;
120 struct reset_control
*axi_s_reset
;
121 struct reset_control
*pipe_reset
;
122 struct reset_control
*axi_m_vmid_reset
;
123 struct reset_control
*axi_s_xpu_reset
;
124 struct reset_control
*parf_reset
;
125 struct reset_control
*phy_reset
;
126 struct reset_control
*axi_m_sticky_reset
;
127 struct reset_control
*pipe_sticky_reset
;
128 struct reset_control
*pwr_reset
;
129 struct reset_control
*ahb_reset
;
130 struct reset_control
*phy_ahb_reset
;
133 struct qcom_pcie_resources_2_3_3
{
135 struct clk
*axi_m_clk
;
136 struct clk
*axi_s_clk
;
139 struct reset_control
*rst
[7];
142 union qcom_pcie_resources
{
143 struct qcom_pcie_resources_1_0_0 v1_0_0
;
144 struct qcom_pcie_resources_2_1_0 v2_1_0
;
145 struct qcom_pcie_resources_2_3_2 v2_3_2
;
146 struct qcom_pcie_resources_2_3_3 v2_3_3
;
147 struct qcom_pcie_resources_2_4_0 v2_4_0
;
152 struct qcom_pcie_ops
{
153 int (*get_resources
)(struct qcom_pcie
*pcie
);
154 int (*init
)(struct qcom_pcie
*pcie
);
155 int (*post_init
)(struct qcom_pcie
*pcie
);
156 void (*deinit
)(struct qcom_pcie
*pcie
);
157 void (*post_deinit
)(struct qcom_pcie
*pcie
);
158 void (*ltssm_enable
)(struct qcom_pcie
*pcie
);
163 void __iomem
*parf
; /* DT parf */
164 void __iomem
*elbi
; /* DT elbi */
165 union qcom_pcie_resources res
;
167 struct gpio_desc
*reset
;
168 const struct qcom_pcie_ops
*ops
;
171 #define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
173 static void qcom_ep_reset_assert(struct qcom_pcie
*pcie
)
175 gpiod_set_value_cansleep(pcie
->reset
, 1);
176 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
179 static void qcom_ep_reset_deassert(struct qcom_pcie
*pcie
)
181 gpiod_set_value_cansleep(pcie
->reset
, 0);
182 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
185 static int qcom_pcie_establish_link(struct qcom_pcie
*pcie
)
187 struct dw_pcie
*pci
= pcie
->pci
;
189 if (dw_pcie_link_up(pci
))
192 /* Enable Link Training state machine */
193 if (pcie
->ops
->ltssm_enable
)
194 pcie
->ops
->ltssm_enable(pcie
);
196 return dw_pcie_wait_for_link(pci
);
199 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie
*pcie
)
203 /* enable link training */
204 val
= readl(pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
205 val
|= PCIE20_ELBI_SYS_CTRL_LT_ENABLE
;
206 writel(val
, pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
209 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie
*pcie
)
211 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
212 struct dw_pcie
*pci
= pcie
->pci
;
213 struct device
*dev
= pci
->dev
;
216 res
->supplies
[0].supply
= "vdda";
217 res
->supplies
[1].supply
= "vdda_phy";
218 res
->supplies
[2].supply
= "vdda_refclk";
219 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(res
->supplies
),
224 res
->iface_clk
= devm_clk_get(dev
, "iface");
225 if (IS_ERR(res
->iface_clk
))
226 return PTR_ERR(res
->iface_clk
);
228 res
->core_clk
= devm_clk_get(dev
, "core");
229 if (IS_ERR(res
->core_clk
))
230 return PTR_ERR(res
->core_clk
);
232 res
->phy_clk
= devm_clk_get(dev
, "phy");
233 if (IS_ERR(res
->phy_clk
))
234 return PTR_ERR(res
->phy_clk
);
236 res
->pci_reset
= devm_reset_control_get_exclusive(dev
, "pci");
237 if (IS_ERR(res
->pci_reset
))
238 return PTR_ERR(res
->pci_reset
);
240 res
->axi_reset
= devm_reset_control_get_exclusive(dev
, "axi");
241 if (IS_ERR(res
->axi_reset
))
242 return PTR_ERR(res
->axi_reset
);
244 res
->ahb_reset
= devm_reset_control_get_exclusive(dev
, "ahb");
245 if (IS_ERR(res
->ahb_reset
))
246 return PTR_ERR(res
->ahb_reset
);
248 res
->por_reset
= devm_reset_control_get_exclusive(dev
, "por");
249 if (IS_ERR(res
->por_reset
))
250 return PTR_ERR(res
->por_reset
);
252 res
->phy_reset
= devm_reset_control_get_exclusive(dev
, "phy");
253 return PTR_ERR_OR_ZERO(res
->phy_reset
);
256 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie
*pcie
)
258 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
260 reset_control_assert(res
->pci_reset
);
261 reset_control_assert(res
->axi_reset
);
262 reset_control_assert(res
->ahb_reset
);
263 reset_control_assert(res
->por_reset
);
264 reset_control_assert(res
->pci_reset
);
265 clk_disable_unprepare(res
->iface_clk
);
266 clk_disable_unprepare(res
->core_clk
);
267 clk_disable_unprepare(res
->phy_clk
);
268 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
271 static int qcom_pcie_init_2_1_0(struct qcom_pcie
*pcie
)
273 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
274 struct dw_pcie
*pci
= pcie
->pci
;
275 struct device
*dev
= pci
->dev
;
279 ret
= regulator_bulk_enable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
281 dev_err(dev
, "cannot enable regulators\n");
285 ret
= reset_control_assert(res
->ahb_reset
);
287 dev_err(dev
, "cannot assert ahb reset\n");
291 ret
= clk_prepare_enable(res
->iface_clk
);
293 dev_err(dev
, "cannot prepare/enable iface clock\n");
297 ret
= clk_prepare_enable(res
->phy_clk
);
299 dev_err(dev
, "cannot prepare/enable phy clock\n");
303 ret
= clk_prepare_enable(res
->core_clk
);
305 dev_err(dev
, "cannot prepare/enable core clock\n");
309 ret
= reset_control_deassert(res
->ahb_reset
);
311 dev_err(dev
, "cannot deassert ahb reset\n");
312 goto err_deassert_ahb
;
315 /* enable PCIe clocks and resets */
316 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
318 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
320 /* enable external reference clock */
321 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
323 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
325 ret
= reset_control_deassert(res
->phy_reset
);
327 dev_err(dev
, "cannot deassert phy reset\n");
331 ret
= reset_control_deassert(res
->pci_reset
);
333 dev_err(dev
, "cannot deassert pci reset\n");
337 ret
= reset_control_deassert(res
->por_reset
);
339 dev_err(dev
, "cannot deassert por reset\n");
343 ret
= reset_control_deassert(res
->axi_reset
);
345 dev_err(dev
, "cannot deassert axi reset\n");
349 /* wait for clock acquisition */
350 usleep_range(1000, 1500);
353 /* Set the Max TLP size to 2K, instead of using default of 4K */
354 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K
,
355 pci
->dbi_base
+ PCIE20_AXI_MSTR_RESP_COMP_CTRL0
);
356 writel(CFG_BRIDGE_SB_INIT
,
357 pci
->dbi_base
+ PCIE20_AXI_MSTR_RESP_COMP_CTRL1
);
362 clk_disable_unprepare(res
->core_clk
);
364 clk_disable_unprepare(res
->phy_clk
);
366 clk_disable_unprepare(res
->iface_clk
);
368 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
373 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie
*pcie
)
375 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
376 struct dw_pcie
*pci
= pcie
->pci
;
377 struct device
*dev
= pci
->dev
;
379 res
->vdda
= devm_regulator_get(dev
, "vdda");
380 if (IS_ERR(res
->vdda
))
381 return PTR_ERR(res
->vdda
);
383 res
->iface
= devm_clk_get(dev
, "iface");
384 if (IS_ERR(res
->iface
))
385 return PTR_ERR(res
->iface
);
387 res
->aux
= devm_clk_get(dev
, "aux");
388 if (IS_ERR(res
->aux
))
389 return PTR_ERR(res
->aux
);
391 res
->master_bus
= devm_clk_get(dev
, "master_bus");
392 if (IS_ERR(res
->master_bus
))
393 return PTR_ERR(res
->master_bus
);
395 res
->slave_bus
= devm_clk_get(dev
, "slave_bus");
396 if (IS_ERR(res
->slave_bus
))
397 return PTR_ERR(res
->slave_bus
);
399 res
->core
= devm_reset_control_get_exclusive(dev
, "core");
400 return PTR_ERR_OR_ZERO(res
->core
);
403 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie
*pcie
)
405 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
407 reset_control_assert(res
->core
);
408 clk_disable_unprepare(res
->slave_bus
);
409 clk_disable_unprepare(res
->master_bus
);
410 clk_disable_unprepare(res
->iface
);
411 clk_disable_unprepare(res
->aux
);
412 regulator_disable(res
->vdda
);
415 static int qcom_pcie_init_1_0_0(struct qcom_pcie
*pcie
)
417 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
418 struct dw_pcie
*pci
= pcie
->pci
;
419 struct device
*dev
= pci
->dev
;
422 ret
= reset_control_deassert(res
->core
);
424 dev_err(dev
, "cannot deassert core reset\n");
428 ret
= clk_prepare_enable(res
->aux
);
430 dev_err(dev
, "cannot prepare/enable aux clock\n");
434 ret
= clk_prepare_enable(res
->iface
);
436 dev_err(dev
, "cannot prepare/enable iface clock\n");
440 ret
= clk_prepare_enable(res
->master_bus
);
442 dev_err(dev
, "cannot prepare/enable master_bus clock\n");
446 ret
= clk_prepare_enable(res
->slave_bus
);
448 dev_err(dev
, "cannot prepare/enable slave_bus clock\n");
452 ret
= regulator_enable(res
->vdda
);
454 dev_err(dev
, "cannot enable vdda regulator\n");
458 /* change DBI base address */
459 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
461 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
462 u32 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
465 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
470 clk_disable_unprepare(res
->slave_bus
);
472 clk_disable_unprepare(res
->master_bus
);
474 clk_disable_unprepare(res
->iface
);
476 clk_disable_unprepare(res
->aux
);
478 reset_control_assert(res
->core
);
483 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie
*pcie
)
487 /* enable link training */
488 val
= readl(pcie
->parf
+ PCIE20_PARF_LTSSM
);
490 writel(val
, pcie
->parf
+ PCIE20_PARF_LTSSM
);
493 static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie
*pcie
)
495 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
496 struct dw_pcie
*pci
= pcie
->pci
;
497 struct device
*dev
= pci
->dev
;
500 res
->supplies
[0].supply
= "vdda";
501 res
->supplies
[1].supply
= "vddpe-3v3";
502 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(res
->supplies
),
507 res
->aux_clk
= devm_clk_get(dev
, "aux");
508 if (IS_ERR(res
->aux_clk
))
509 return PTR_ERR(res
->aux_clk
);
511 res
->cfg_clk
= devm_clk_get(dev
, "cfg");
512 if (IS_ERR(res
->cfg_clk
))
513 return PTR_ERR(res
->cfg_clk
);
515 res
->master_clk
= devm_clk_get(dev
, "bus_master");
516 if (IS_ERR(res
->master_clk
))
517 return PTR_ERR(res
->master_clk
);
519 res
->slave_clk
= devm_clk_get(dev
, "bus_slave");
520 if (IS_ERR(res
->slave_clk
))
521 return PTR_ERR(res
->slave_clk
);
523 res
->pipe_clk
= devm_clk_get(dev
, "pipe");
524 return PTR_ERR_OR_ZERO(res
->pipe_clk
);
527 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie
*pcie
)
529 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
531 clk_disable_unprepare(res
->slave_clk
);
532 clk_disable_unprepare(res
->master_clk
);
533 clk_disable_unprepare(res
->cfg_clk
);
534 clk_disable_unprepare(res
->aux_clk
);
536 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
539 static void qcom_pcie_post_deinit_2_3_2(struct qcom_pcie
*pcie
)
541 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
543 clk_disable_unprepare(res
->pipe_clk
);
546 static int qcom_pcie_init_2_3_2(struct qcom_pcie
*pcie
)
548 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
549 struct dw_pcie
*pci
= pcie
->pci
;
550 struct device
*dev
= pci
->dev
;
554 ret
= regulator_bulk_enable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
556 dev_err(dev
, "cannot enable regulators\n");
560 ret
= clk_prepare_enable(res
->aux_clk
);
562 dev_err(dev
, "cannot prepare/enable aux clock\n");
566 ret
= clk_prepare_enable(res
->cfg_clk
);
568 dev_err(dev
, "cannot prepare/enable cfg clock\n");
572 ret
= clk_prepare_enable(res
->master_clk
);
574 dev_err(dev
, "cannot prepare/enable master clock\n");
578 ret
= clk_prepare_enable(res
->slave_clk
);
580 dev_err(dev
, "cannot prepare/enable slave clock\n");
584 /* enable PCIe clocks and resets */
585 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
587 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
589 /* change DBI base address */
590 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
592 /* MAC PHY_POWERDOWN MUX DISABLE */
593 val
= readl(pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
595 writel(val
, pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
597 val
= readl(pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
599 writel(val
, pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
601 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
603 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
608 clk_disable_unprepare(res
->master_clk
);
610 clk_disable_unprepare(res
->cfg_clk
);
612 clk_disable_unprepare(res
->aux_clk
);
615 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
620 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie
*pcie
)
622 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
623 struct dw_pcie
*pci
= pcie
->pci
;
624 struct device
*dev
= pci
->dev
;
627 ret
= clk_prepare_enable(res
->pipe_clk
);
629 dev_err(dev
, "cannot prepare/enable pipe clock\n");
636 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie
*pcie
)
638 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
639 struct dw_pcie
*pci
= pcie
->pci
;
640 struct device
*dev
= pci
->dev
;
642 res
->aux_clk
= devm_clk_get(dev
, "aux");
643 if (IS_ERR(res
->aux_clk
))
644 return PTR_ERR(res
->aux_clk
);
646 res
->master_clk
= devm_clk_get(dev
, "master_bus");
647 if (IS_ERR(res
->master_clk
))
648 return PTR_ERR(res
->master_clk
);
650 res
->slave_clk
= devm_clk_get(dev
, "slave_bus");
651 if (IS_ERR(res
->slave_clk
))
652 return PTR_ERR(res
->slave_clk
);
654 res
->axi_m_reset
= devm_reset_control_get_exclusive(dev
, "axi_m");
655 if (IS_ERR(res
->axi_m_reset
))
656 return PTR_ERR(res
->axi_m_reset
);
658 res
->axi_s_reset
= devm_reset_control_get_exclusive(dev
, "axi_s");
659 if (IS_ERR(res
->axi_s_reset
))
660 return PTR_ERR(res
->axi_s_reset
);
662 res
->pipe_reset
= devm_reset_control_get_exclusive(dev
, "pipe");
663 if (IS_ERR(res
->pipe_reset
))
664 return PTR_ERR(res
->pipe_reset
);
666 res
->axi_m_vmid_reset
= devm_reset_control_get_exclusive(dev
,
668 if (IS_ERR(res
->axi_m_vmid_reset
))
669 return PTR_ERR(res
->axi_m_vmid_reset
);
671 res
->axi_s_xpu_reset
= devm_reset_control_get_exclusive(dev
,
673 if (IS_ERR(res
->axi_s_xpu_reset
))
674 return PTR_ERR(res
->axi_s_xpu_reset
);
676 res
->parf_reset
= devm_reset_control_get_exclusive(dev
, "parf");
677 if (IS_ERR(res
->parf_reset
))
678 return PTR_ERR(res
->parf_reset
);
680 res
->phy_reset
= devm_reset_control_get_exclusive(dev
, "phy");
681 if (IS_ERR(res
->phy_reset
))
682 return PTR_ERR(res
->phy_reset
);
684 res
->axi_m_sticky_reset
= devm_reset_control_get_exclusive(dev
,
686 if (IS_ERR(res
->axi_m_sticky_reset
))
687 return PTR_ERR(res
->axi_m_sticky_reset
);
689 res
->pipe_sticky_reset
= devm_reset_control_get_exclusive(dev
,
691 if (IS_ERR(res
->pipe_sticky_reset
))
692 return PTR_ERR(res
->pipe_sticky_reset
);
694 res
->pwr_reset
= devm_reset_control_get_exclusive(dev
, "pwr");
695 if (IS_ERR(res
->pwr_reset
))
696 return PTR_ERR(res
->pwr_reset
);
698 res
->ahb_reset
= devm_reset_control_get_exclusive(dev
, "ahb");
699 if (IS_ERR(res
->ahb_reset
))
700 return PTR_ERR(res
->ahb_reset
);
702 res
->phy_ahb_reset
= devm_reset_control_get_exclusive(dev
, "phy_ahb");
703 if (IS_ERR(res
->phy_ahb_reset
))
704 return PTR_ERR(res
->phy_ahb_reset
);
709 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie
*pcie
)
711 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
713 reset_control_assert(res
->axi_m_reset
);
714 reset_control_assert(res
->axi_s_reset
);
715 reset_control_assert(res
->pipe_reset
);
716 reset_control_assert(res
->pipe_sticky_reset
);
717 reset_control_assert(res
->phy_reset
);
718 reset_control_assert(res
->phy_ahb_reset
);
719 reset_control_assert(res
->axi_m_sticky_reset
);
720 reset_control_assert(res
->pwr_reset
);
721 reset_control_assert(res
->ahb_reset
);
722 clk_disable_unprepare(res
->aux_clk
);
723 clk_disable_unprepare(res
->master_clk
);
724 clk_disable_unprepare(res
->slave_clk
);
727 static int qcom_pcie_init_2_4_0(struct qcom_pcie
*pcie
)
729 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
730 struct dw_pcie
*pci
= pcie
->pci
;
731 struct device
*dev
= pci
->dev
;
735 ret
= reset_control_assert(res
->axi_m_reset
);
737 dev_err(dev
, "cannot assert axi master reset\n");
741 ret
= reset_control_assert(res
->axi_s_reset
);
743 dev_err(dev
, "cannot assert axi slave reset\n");
747 usleep_range(10000, 12000);
749 ret
= reset_control_assert(res
->pipe_reset
);
751 dev_err(dev
, "cannot assert pipe reset\n");
755 ret
= reset_control_assert(res
->pipe_sticky_reset
);
757 dev_err(dev
, "cannot assert pipe sticky reset\n");
761 ret
= reset_control_assert(res
->phy_reset
);
763 dev_err(dev
, "cannot assert phy reset\n");
767 ret
= reset_control_assert(res
->phy_ahb_reset
);
769 dev_err(dev
, "cannot assert phy ahb reset\n");
773 usleep_range(10000, 12000);
775 ret
= reset_control_assert(res
->axi_m_sticky_reset
);
777 dev_err(dev
, "cannot assert axi master sticky reset\n");
781 ret
= reset_control_assert(res
->pwr_reset
);
783 dev_err(dev
, "cannot assert power reset\n");
787 ret
= reset_control_assert(res
->ahb_reset
);
789 dev_err(dev
, "cannot assert ahb reset\n");
793 usleep_range(10000, 12000);
795 ret
= reset_control_deassert(res
->phy_ahb_reset
);
797 dev_err(dev
, "cannot deassert phy ahb reset\n");
801 ret
= reset_control_deassert(res
->phy_reset
);
803 dev_err(dev
, "cannot deassert phy reset\n");
807 ret
= reset_control_deassert(res
->pipe_reset
);
809 dev_err(dev
, "cannot deassert pipe reset\n");
813 ret
= reset_control_deassert(res
->pipe_sticky_reset
);
815 dev_err(dev
, "cannot deassert pipe sticky reset\n");
816 goto err_rst_pipe_sticky
;
819 usleep_range(10000, 12000);
821 ret
= reset_control_deassert(res
->axi_m_reset
);
823 dev_err(dev
, "cannot deassert axi master reset\n");
827 ret
= reset_control_deassert(res
->axi_m_sticky_reset
);
829 dev_err(dev
, "cannot deassert axi master sticky reset\n");
830 goto err_rst_axi_m_sticky
;
833 ret
= reset_control_deassert(res
->axi_s_reset
);
835 dev_err(dev
, "cannot deassert axi slave reset\n");
839 ret
= reset_control_deassert(res
->pwr_reset
);
841 dev_err(dev
, "cannot deassert power reset\n");
845 ret
= reset_control_deassert(res
->ahb_reset
);
847 dev_err(dev
, "cannot deassert ahb reset\n");
851 usleep_range(10000, 12000);
853 ret
= clk_prepare_enable(res
->aux_clk
);
855 dev_err(dev
, "cannot prepare/enable iface clock\n");
859 ret
= clk_prepare_enable(res
->master_clk
);
861 dev_err(dev
, "cannot prepare/enable core clock\n");
865 ret
= clk_prepare_enable(res
->slave_clk
);
867 dev_err(dev
, "cannot prepare/enable phy clock\n");
871 /* enable PCIe clocks and resets */
872 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
874 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
876 /* change DBI base address */
877 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
879 /* MAC PHY_POWERDOWN MUX DISABLE */
880 val
= readl(pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
882 writel(val
, pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
884 val
= readl(pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
886 writel(val
, pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
888 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
890 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
895 clk_disable_unprepare(res
->master_clk
);
897 clk_disable_unprepare(res
->aux_clk
);
899 reset_control_assert(res
->ahb_reset
);
901 reset_control_assert(res
->pwr_reset
);
903 reset_control_assert(res
->axi_s_reset
);
905 reset_control_assert(res
->axi_m_sticky_reset
);
906 err_rst_axi_m_sticky
:
907 reset_control_assert(res
->axi_m_reset
);
909 reset_control_assert(res
->pipe_sticky_reset
);
911 reset_control_assert(res
->pipe_reset
);
913 reset_control_assert(res
->phy_reset
);
915 reset_control_assert(res
->phy_ahb_reset
);
919 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie
*pcie
)
921 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
922 struct dw_pcie
*pci
= pcie
->pci
;
923 struct device
*dev
= pci
->dev
;
925 const char *rst_names
[] = { "axi_m", "axi_s", "pipe",
926 "axi_m_sticky", "sticky",
929 res
->iface
= devm_clk_get(dev
, "iface");
930 if (IS_ERR(res
->iface
))
931 return PTR_ERR(res
->iface
);
933 res
->axi_m_clk
= devm_clk_get(dev
, "axi_m");
934 if (IS_ERR(res
->axi_m_clk
))
935 return PTR_ERR(res
->axi_m_clk
);
937 res
->axi_s_clk
= devm_clk_get(dev
, "axi_s");
938 if (IS_ERR(res
->axi_s_clk
))
939 return PTR_ERR(res
->axi_s_clk
);
941 res
->ahb_clk
= devm_clk_get(dev
, "ahb");
942 if (IS_ERR(res
->ahb_clk
))
943 return PTR_ERR(res
->ahb_clk
);
945 res
->aux_clk
= devm_clk_get(dev
, "aux");
946 if (IS_ERR(res
->aux_clk
))
947 return PTR_ERR(res
->aux_clk
);
949 for (i
= 0; i
< ARRAY_SIZE(rst_names
); i
++) {
950 res
->rst
[i
] = devm_reset_control_get(dev
, rst_names
[i
]);
951 if (IS_ERR(res
->rst
[i
]))
952 return PTR_ERR(res
->rst
[i
]);
958 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie
*pcie
)
960 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
962 clk_disable_unprepare(res
->iface
);
963 clk_disable_unprepare(res
->axi_m_clk
);
964 clk_disable_unprepare(res
->axi_s_clk
);
965 clk_disable_unprepare(res
->ahb_clk
);
966 clk_disable_unprepare(res
->aux_clk
);
969 static int qcom_pcie_init_2_3_3(struct qcom_pcie
*pcie
)
971 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
972 struct dw_pcie
*pci
= pcie
->pci
;
973 struct device
*dev
= pci
->dev
;
977 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++) {
978 ret
= reset_control_assert(res
->rst
[i
]);
980 dev_err(dev
, "reset #%d assert failed (%d)\n", i
, ret
);
985 usleep_range(2000, 2500);
987 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++) {
988 ret
= reset_control_deassert(res
->rst
[i
]);
990 dev_err(dev
, "reset #%d deassert failed (%d)\n", i
,
997 * Don't have a way to see if the reset has completed.
998 * Wait for some time.
1000 usleep_range(2000, 2500);
1002 ret
= clk_prepare_enable(res
->iface
);
1004 dev_err(dev
, "cannot prepare/enable core clock\n");
1008 ret
= clk_prepare_enable(res
->axi_m_clk
);
1010 dev_err(dev
, "cannot prepare/enable core clock\n");
1014 ret
= clk_prepare_enable(res
->axi_s_clk
);
1016 dev_err(dev
, "cannot prepare/enable axi slave clock\n");
1020 ret
= clk_prepare_enable(res
->ahb_clk
);
1022 dev_err(dev
, "cannot prepare/enable ahb clock\n");
1026 ret
= clk_prepare_enable(res
->aux_clk
);
1028 dev_err(dev
, "cannot prepare/enable aux clock\n");
1032 writel(SLV_ADDR_SPACE_SZ
,
1033 pcie
->parf
+ PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE
);
1035 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
1037 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
1039 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
1041 writel(MST_WAKEUP_EN
| SLV_WAKEUP_EN
| MSTR_ACLK_CGC_DIS
1042 | SLV_ACLK_CGC_DIS
| CORE_CLK_CGC_DIS
|
1043 AUX_PWR_DET
| L23_CLK_RMV_DIS
| L1_CLK_RMV_DIS
,
1044 pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
1045 writel(0, pcie
->parf
+ PCIE20_PARF_Q2A_FLUSH
);
1047 writel(CMD_BME_VAL
, pci
->dbi_base
+ PCIE20_COMMAND_STATUS
);
1048 writel(DBI_RO_WR_EN
, pci
->dbi_base
+ PCIE20_MISC_CONTROL_1_REG
);
1049 writel(PCIE_CAP_LINK1_VAL
, pci
->dbi_base
+ PCIE20_CAP_LINK_1
);
1051 val
= readl(pci
->dbi_base
+ PCIE20_CAP_LINK_CAPABILITIES
);
1052 val
&= ~PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT
;
1053 writel(val
, pci
->dbi_base
+ PCIE20_CAP_LINK_CAPABILITIES
);
1055 writel(PCIE_CAP_CPL_TIMEOUT_DISABLE
, pci
->dbi_base
+
1056 PCIE20_DEVICE_CONTROL2_STATUS2
);
1061 clk_disable_unprepare(res
->ahb_clk
);
1063 clk_disable_unprepare(res
->axi_s_clk
);
1065 clk_disable_unprepare(res
->axi_m_clk
);
1067 clk_disable_unprepare(res
->iface
);
1070 * Not checking for failure, will anyway return
1071 * the original failure in 'ret'.
1073 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++)
1074 reset_control_assert(res
->rst
[i
]);
1079 static int qcom_pcie_link_up(struct dw_pcie
*pci
)
1081 u16 val
= readw(pci
->dbi_base
+ PCIE20_CAP
+ PCI_EXP_LNKSTA
);
1083 return !!(val
& PCI_EXP_LNKSTA_DLLLA
);
1086 static int qcom_pcie_host_init(struct pcie_port
*pp
)
1088 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
1089 struct qcom_pcie
*pcie
= to_qcom_pcie(pci
);
1092 qcom_ep_reset_assert(pcie
);
1094 ret
= pcie
->ops
->init(pcie
);
1098 ret
= phy_power_on(pcie
->phy
);
1102 if (pcie
->ops
->post_init
) {
1103 ret
= pcie
->ops
->post_init(pcie
);
1105 goto err_disable_phy
;
1108 dw_pcie_setup_rc(pp
);
1110 if (IS_ENABLED(CONFIG_PCI_MSI
))
1111 dw_pcie_msi_init(pp
);
1113 qcom_ep_reset_deassert(pcie
);
1115 ret
= qcom_pcie_establish_link(pcie
);
1121 qcom_ep_reset_assert(pcie
);
1122 if (pcie
->ops
->post_deinit
)
1123 pcie
->ops
->post_deinit(pcie
);
1125 phy_power_off(pcie
->phy
);
1127 pcie
->ops
->deinit(pcie
);
1132 static int qcom_pcie_rd_own_conf(struct pcie_port
*pp
, int where
, int size
,
1135 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
1137 /* the device class is not reported correctly from the register */
1138 if (where
== PCI_CLASS_REVISION
&& size
== 4) {
1139 *val
= readl(pci
->dbi_base
+ PCI_CLASS_REVISION
);
1140 *val
&= 0xff; /* keep revision id */
1141 *val
|= PCI_CLASS_BRIDGE_PCI
<< 16;
1142 return PCIBIOS_SUCCESSFUL
;
1145 return dw_pcie_read(pci
->dbi_base
+ where
, size
, val
);
1148 static const struct dw_pcie_host_ops qcom_pcie_dw_ops
= {
1149 .host_init
= qcom_pcie_host_init
,
1150 .rd_own_conf
= qcom_pcie_rd_own_conf
,
1153 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1154 static const struct qcom_pcie_ops ops_2_1_0
= {
1155 .get_resources
= qcom_pcie_get_resources_2_1_0
,
1156 .init
= qcom_pcie_init_2_1_0
,
1157 .deinit
= qcom_pcie_deinit_2_1_0
,
1158 .ltssm_enable
= qcom_pcie_2_1_0_ltssm_enable
,
1161 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1162 static const struct qcom_pcie_ops ops_1_0_0
= {
1163 .get_resources
= qcom_pcie_get_resources_1_0_0
,
1164 .init
= qcom_pcie_init_1_0_0
,
1165 .deinit
= qcom_pcie_deinit_1_0_0
,
1166 .ltssm_enable
= qcom_pcie_2_1_0_ltssm_enable
,
1169 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1170 static const struct qcom_pcie_ops ops_2_3_2
= {
1171 .get_resources
= qcom_pcie_get_resources_2_3_2
,
1172 .init
= qcom_pcie_init_2_3_2
,
1173 .post_init
= qcom_pcie_post_init_2_3_2
,
1174 .deinit
= qcom_pcie_deinit_2_3_2
,
1175 .post_deinit
= qcom_pcie_post_deinit_2_3_2
,
1176 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1179 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1180 static const struct qcom_pcie_ops ops_2_4_0
= {
1181 .get_resources
= qcom_pcie_get_resources_2_4_0
,
1182 .init
= qcom_pcie_init_2_4_0
,
1183 .deinit
= qcom_pcie_deinit_2_4_0
,
1184 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1187 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1188 static const struct qcom_pcie_ops ops_2_3_3
= {
1189 .get_resources
= qcom_pcie_get_resources_2_3_3
,
1190 .init
= qcom_pcie_init_2_3_3
,
1191 .deinit
= qcom_pcie_deinit_2_3_3
,
1192 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1195 static const struct dw_pcie_ops dw_pcie_ops
= {
1196 .link_up
= qcom_pcie_link_up
,
1199 static int qcom_pcie_probe(struct platform_device
*pdev
)
1201 struct device
*dev
= &pdev
->dev
;
1202 struct resource
*res
;
1203 struct pcie_port
*pp
;
1204 struct dw_pcie
*pci
;
1205 struct qcom_pcie
*pcie
;
1208 pcie
= devm_kzalloc(dev
, sizeof(*pcie
), GFP_KERNEL
);
1212 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
1216 pm_runtime_enable(dev
);
1217 ret
= pm_runtime_get_sync(dev
);
1219 pm_runtime_disable(dev
);
1224 pci
->ops
= &dw_pcie_ops
;
1229 pcie
->ops
= of_device_get_match_data(dev
);
1231 pcie
->reset
= devm_gpiod_get_optional(dev
, "perst", GPIOD_OUT_HIGH
);
1232 if (IS_ERR(pcie
->reset
)) {
1233 ret
= PTR_ERR(pcie
->reset
);
1234 goto err_pm_runtime_put
;
1237 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "parf");
1238 pcie
->parf
= devm_ioremap_resource(dev
, res
);
1239 if (IS_ERR(pcie
->parf
)) {
1240 ret
= PTR_ERR(pcie
->parf
);
1241 goto err_pm_runtime_put
;
1244 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbi");
1245 pci
->dbi_base
= devm_pci_remap_cfg_resource(dev
, res
);
1246 if (IS_ERR(pci
->dbi_base
)) {
1247 ret
= PTR_ERR(pci
->dbi_base
);
1248 goto err_pm_runtime_put
;
1251 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "elbi");
1252 pcie
->elbi
= devm_ioremap_resource(dev
, res
);
1253 if (IS_ERR(pcie
->elbi
)) {
1254 ret
= PTR_ERR(pcie
->elbi
);
1255 goto err_pm_runtime_put
;
1258 pcie
->phy
= devm_phy_optional_get(dev
, "pciephy");
1259 if (IS_ERR(pcie
->phy
)) {
1260 ret
= PTR_ERR(pcie
->phy
);
1261 goto err_pm_runtime_put
;
1264 ret
= pcie
->ops
->get_resources(pcie
);
1266 goto err_pm_runtime_put
;
1268 pp
->ops
= &qcom_pcie_dw_ops
;
1270 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
1271 pp
->msi_irq
= platform_get_irq_byname(pdev
, "msi");
1272 if (pp
->msi_irq
< 0) {
1274 goto err_pm_runtime_put
;
1278 ret
= phy_init(pcie
->phy
);
1280 pm_runtime_disable(&pdev
->dev
);
1281 goto err_pm_runtime_put
;
1284 platform_set_drvdata(pdev
, pcie
);
1286 ret
= dw_pcie_host_init(pp
);
1288 dev_err(dev
, "cannot initialize host\n");
1289 pm_runtime_disable(&pdev
->dev
);
1290 goto err_pm_runtime_put
;
1296 pm_runtime_put(dev
);
1297 pm_runtime_disable(dev
);
1302 static const struct of_device_id qcom_pcie_match
[] = {
1303 { .compatible
= "qcom,pcie-apq8084", .data
= &ops_1_0_0
},
1304 { .compatible
= "qcom,pcie-ipq8064", .data
= &ops_2_1_0
},
1305 { .compatible
= "qcom,pcie-apq8064", .data
= &ops_2_1_0
},
1306 { .compatible
= "qcom,pcie-msm8996", .data
= &ops_2_3_2
},
1307 { .compatible
= "qcom,pcie-ipq8074", .data
= &ops_2_3_3
},
1308 { .compatible
= "qcom,pcie-ipq4019", .data
= &ops_2_4_0
},
1312 static struct platform_driver qcom_pcie_driver
= {
1313 .probe
= qcom_pcie_probe
,
1315 .name
= "qcom-pcie",
1316 .suppress_bind_attrs
= true,
1317 .of_match_table
= qcom_pcie_match
,
1320 builtin_platform_driver(qcom_pcie_driver
);