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.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/platform_device.h>
23 #include <linux/phy/phy.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/reset.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
29 #include "pcie-designware.h"
31 #define PCIE20_PARF_SYS_CTRL 0x00
32 #define MST_WAKEUP_EN BIT(13)
33 #define SLV_WAKEUP_EN BIT(12)
34 #define MSTR_ACLK_CGC_DIS BIT(10)
35 #define SLV_ACLK_CGC_DIS BIT(9)
36 #define CORE_CLK_CGC_DIS BIT(6)
37 #define AUX_PWR_DET BIT(4)
38 #define L23_CLK_RMV_DIS BIT(2)
39 #define L1_CLK_RMV_DIS BIT(1)
41 #define PCIE20_COMMAND_STATUS 0x04
42 #define CMD_BME_VAL 0x4
43 #define PCIE20_DEVICE_CONTROL2_STATUS2 0x98
44 #define PCIE_CAP_CPL_TIMEOUT_DISABLE 0x10
46 #define PCIE20_PARF_PHY_CTRL 0x40
47 #define PCIE20_PARF_PHY_REFCLK 0x4C
48 #define PCIE20_PARF_DBI_BASE_ADDR 0x168
49 #define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
50 #define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
51 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
52 #define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
53 #define PCIE20_PARF_LTSSM 0x1B0
54 #define PCIE20_PARF_SID_OFFSET 0x234
55 #define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
57 #define PCIE20_ELBI_SYS_CTRL 0x04
58 #define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
60 #define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
61 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
62 #define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
63 #define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
64 #define CFG_BRIDGE_SB_INIT BIT(0)
66 #define PCIE20_CAP 0x70
67 #define PCIE20_CAP_LINK_CAPABILITIES (PCIE20_CAP + 0xC)
68 #define PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT (BIT(10) | BIT(11))
69 #define PCIE20_CAP_LINK_1 (PCIE20_CAP + 0x14)
70 #define PCIE_CAP_LINK1_VAL 0x2FD7F
72 #define PCIE20_PARF_Q2A_FLUSH 0x1AC
74 #define PCIE20_MISC_CONTROL_1_REG 0x8BC
75 #define DBI_RO_WR_EN 1
77 #define PERST_DELAY_US 1000
79 #define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
80 #define SLV_ADDR_SPACE_SZ 0x10000000
82 struct qcom_pcie_resources_2_1_0
{
83 struct clk
*iface_clk
;
86 struct reset_control
*pci_reset
;
87 struct reset_control
*axi_reset
;
88 struct reset_control
*ahb_reset
;
89 struct reset_control
*por_reset
;
90 struct reset_control
*phy_reset
;
91 struct regulator
*vdda
;
92 struct regulator
*vdda_phy
;
93 struct regulator
*vdda_refclk
;
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 struct qcom_pcie_resources_2_3_2
{
107 struct clk
*master_clk
;
108 struct clk
*slave_clk
;
110 struct clk
*pipe_clk
;
113 struct qcom_pcie_resources_2_4_0
{
115 struct clk
*master_clk
;
116 struct clk
*slave_clk
;
117 struct reset_control
*axi_m_reset
;
118 struct reset_control
*axi_s_reset
;
119 struct reset_control
*pipe_reset
;
120 struct reset_control
*axi_m_vmid_reset
;
121 struct reset_control
*axi_s_xpu_reset
;
122 struct reset_control
*parf_reset
;
123 struct reset_control
*phy_reset
;
124 struct reset_control
*axi_m_sticky_reset
;
125 struct reset_control
*pipe_sticky_reset
;
126 struct reset_control
*pwr_reset
;
127 struct reset_control
*ahb_reset
;
128 struct reset_control
*phy_ahb_reset
;
131 struct qcom_pcie_resources_2_3_3
{
133 struct clk
*axi_m_clk
;
134 struct clk
*axi_s_clk
;
137 struct reset_control
*rst
[7];
140 union qcom_pcie_resources
{
141 struct qcom_pcie_resources_1_0_0 v1_0_0
;
142 struct qcom_pcie_resources_2_1_0 v2_1_0
;
143 struct qcom_pcie_resources_2_3_2 v2_3_2
;
144 struct qcom_pcie_resources_2_3_3 v2_3_3
;
145 struct qcom_pcie_resources_2_4_0 v2_4_0
;
150 struct qcom_pcie_ops
{
151 int (*get_resources
)(struct qcom_pcie
*pcie
);
152 int (*init
)(struct qcom_pcie
*pcie
);
153 int (*post_init
)(struct qcom_pcie
*pcie
);
154 void (*deinit
)(struct qcom_pcie
*pcie
);
155 void (*post_deinit
)(struct qcom_pcie
*pcie
);
156 void (*ltssm_enable
)(struct qcom_pcie
*pcie
);
161 void __iomem
*parf
; /* DT parf */
162 void __iomem
*elbi
; /* DT elbi */
163 union qcom_pcie_resources res
;
165 struct gpio_desc
*reset
;
166 const struct qcom_pcie_ops
*ops
;
169 #define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
171 static void qcom_ep_reset_assert(struct qcom_pcie
*pcie
)
173 gpiod_set_value_cansleep(pcie
->reset
, 1);
174 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
177 static void qcom_ep_reset_deassert(struct qcom_pcie
*pcie
)
179 gpiod_set_value_cansleep(pcie
->reset
, 0);
180 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
183 static irqreturn_t
qcom_pcie_msi_irq_handler(int irq
, void *arg
)
185 struct pcie_port
*pp
= arg
;
187 return dw_handle_msi_irq(pp
);
190 static int qcom_pcie_establish_link(struct qcom_pcie
*pcie
)
192 struct dw_pcie
*pci
= pcie
->pci
;
194 if (dw_pcie_link_up(pci
))
197 /* Enable Link Training state machine */
198 if (pcie
->ops
->ltssm_enable
)
199 pcie
->ops
->ltssm_enable(pcie
);
201 return dw_pcie_wait_for_link(pci
);
204 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie
*pcie
)
208 /* enable link training */
209 val
= readl(pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
210 val
|= PCIE20_ELBI_SYS_CTRL_LT_ENABLE
;
211 writel(val
, pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
214 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie
*pcie
)
216 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
217 struct dw_pcie
*pci
= pcie
->pci
;
218 struct device
*dev
= pci
->dev
;
220 res
->vdda
= devm_regulator_get(dev
, "vdda");
221 if (IS_ERR(res
->vdda
))
222 return PTR_ERR(res
->vdda
);
224 res
->vdda_phy
= devm_regulator_get(dev
, "vdda_phy");
225 if (IS_ERR(res
->vdda_phy
))
226 return PTR_ERR(res
->vdda_phy
);
228 res
->vdda_refclk
= devm_regulator_get(dev
, "vdda_refclk");
229 if (IS_ERR(res
->vdda_refclk
))
230 return PTR_ERR(res
->vdda_refclk
);
232 res
->iface_clk
= devm_clk_get(dev
, "iface");
233 if (IS_ERR(res
->iface_clk
))
234 return PTR_ERR(res
->iface_clk
);
236 res
->core_clk
= devm_clk_get(dev
, "core");
237 if (IS_ERR(res
->core_clk
))
238 return PTR_ERR(res
->core_clk
);
240 res
->phy_clk
= devm_clk_get(dev
, "phy");
241 if (IS_ERR(res
->phy_clk
))
242 return PTR_ERR(res
->phy_clk
);
244 res
->pci_reset
= devm_reset_control_get_exclusive(dev
, "pci");
245 if (IS_ERR(res
->pci_reset
))
246 return PTR_ERR(res
->pci_reset
);
248 res
->axi_reset
= devm_reset_control_get_exclusive(dev
, "axi");
249 if (IS_ERR(res
->axi_reset
))
250 return PTR_ERR(res
->axi_reset
);
252 res
->ahb_reset
= devm_reset_control_get_exclusive(dev
, "ahb");
253 if (IS_ERR(res
->ahb_reset
))
254 return PTR_ERR(res
->ahb_reset
);
256 res
->por_reset
= devm_reset_control_get_exclusive(dev
, "por");
257 if (IS_ERR(res
->por_reset
))
258 return PTR_ERR(res
->por_reset
);
260 res
->phy_reset
= devm_reset_control_get_exclusive(dev
, "phy");
261 return PTR_ERR_OR_ZERO(res
->phy_reset
);
264 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie
*pcie
)
266 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
268 reset_control_assert(res
->pci_reset
);
269 reset_control_assert(res
->axi_reset
);
270 reset_control_assert(res
->ahb_reset
);
271 reset_control_assert(res
->por_reset
);
272 reset_control_assert(res
->pci_reset
);
273 clk_disable_unprepare(res
->iface_clk
);
274 clk_disable_unprepare(res
->core_clk
);
275 clk_disable_unprepare(res
->phy_clk
);
276 regulator_disable(res
->vdda
);
277 regulator_disable(res
->vdda_phy
);
278 regulator_disable(res
->vdda_refclk
);
281 static int qcom_pcie_init_2_1_0(struct qcom_pcie
*pcie
)
283 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
284 struct dw_pcie
*pci
= pcie
->pci
;
285 struct device
*dev
= pci
->dev
;
289 ret
= regulator_enable(res
->vdda
);
291 dev_err(dev
, "cannot enable vdda regulator\n");
295 ret
= regulator_enable(res
->vdda_refclk
);
297 dev_err(dev
, "cannot enable vdda_refclk regulator\n");
301 ret
= regulator_enable(res
->vdda_phy
);
303 dev_err(dev
, "cannot enable vdda_phy regulator\n");
307 ret
= reset_control_assert(res
->ahb_reset
);
309 dev_err(dev
, "cannot assert ahb reset\n");
313 ret
= clk_prepare_enable(res
->iface_clk
);
315 dev_err(dev
, "cannot prepare/enable iface clock\n");
319 ret
= clk_prepare_enable(res
->phy_clk
);
321 dev_err(dev
, "cannot prepare/enable phy clock\n");
325 ret
= clk_prepare_enable(res
->core_clk
);
327 dev_err(dev
, "cannot prepare/enable core clock\n");
331 ret
= reset_control_deassert(res
->ahb_reset
);
333 dev_err(dev
, "cannot deassert ahb reset\n");
334 goto err_deassert_ahb
;
337 /* enable PCIe clocks and resets */
338 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
340 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
342 /* enable external reference clock */
343 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
345 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
347 ret
= reset_control_deassert(res
->phy_reset
);
349 dev_err(dev
, "cannot deassert phy reset\n");
353 ret
= reset_control_deassert(res
->pci_reset
);
355 dev_err(dev
, "cannot deassert pci reset\n");
359 ret
= reset_control_deassert(res
->por_reset
);
361 dev_err(dev
, "cannot deassert por reset\n");
365 ret
= reset_control_deassert(res
->axi_reset
);
367 dev_err(dev
, "cannot deassert axi reset\n");
371 /* wait for clock acquisition */
372 usleep_range(1000, 1500);
375 /* Set the Max TLP size to 2K, instead of using default of 4K */
376 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K
,
377 pci
->dbi_base
+ PCIE20_AXI_MSTR_RESP_COMP_CTRL0
);
378 writel(CFG_BRIDGE_SB_INIT
,
379 pci
->dbi_base
+ PCIE20_AXI_MSTR_RESP_COMP_CTRL1
);
384 clk_disable_unprepare(res
->core_clk
);
386 clk_disable_unprepare(res
->phy_clk
);
388 clk_disable_unprepare(res
->iface_clk
);
390 regulator_disable(res
->vdda_phy
);
392 regulator_disable(res
->vdda_refclk
);
394 regulator_disable(res
->vdda
);
399 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie
*pcie
)
401 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
402 struct dw_pcie
*pci
= pcie
->pci
;
403 struct device
*dev
= pci
->dev
;
405 res
->vdda
= devm_regulator_get(dev
, "vdda");
406 if (IS_ERR(res
->vdda
))
407 return PTR_ERR(res
->vdda
);
409 res
->iface
= devm_clk_get(dev
, "iface");
410 if (IS_ERR(res
->iface
))
411 return PTR_ERR(res
->iface
);
413 res
->aux
= devm_clk_get(dev
, "aux");
414 if (IS_ERR(res
->aux
))
415 return PTR_ERR(res
->aux
);
417 res
->master_bus
= devm_clk_get(dev
, "master_bus");
418 if (IS_ERR(res
->master_bus
))
419 return PTR_ERR(res
->master_bus
);
421 res
->slave_bus
= devm_clk_get(dev
, "slave_bus");
422 if (IS_ERR(res
->slave_bus
))
423 return PTR_ERR(res
->slave_bus
);
425 res
->core
= devm_reset_control_get_exclusive(dev
, "core");
426 return PTR_ERR_OR_ZERO(res
->core
);
429 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie
*pcie
)
431 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
433 reset_control_assert(res
->core
);
434 clk_disable_unprepare(res
->slave_bus
);
435 clk_disable_unprepare(res
->master_bus
);
436 clk_disable_unprepare(res
->iface
);
437 clk_disable_unprepare(res
->aux
);
438 regulator_disable(res
->vdda
);
441 static int qcom_pcie_init_1_0_0(struct qcom_pcie
*pcie
)
443 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
444 struct dw_pcie
*pci
= pcie
->pci
;
445 struct device
*dev
= pci
->dev
;
448 ret
= reset_control_deassert(res
->core
);
450 dev_err(dev
, "cannot deassert core reset\n");
454 ret
= clk_prepare_enable(res
->aux
);
456 dev_err(dev
, "cannot prepare/enable aux clock\n");
460 ret
= clk_prepare_enable(res
->iface
);
462 dev_err(dev
, "cannot prepare/enable iface clock\n");
466 ret
= clk_prepare_enable(res
->master_bus
);
468 dev_err(dev
, "cannot prepare/enable master_bus clock\n");
472 ret
= clk_prepare_enable(res
->slave_bus
);
474 dev_err(dev
, "cannot prepare/enable slave_bus clock\n");
478 ret
= regulator_enable(res
->vdda
);
480 dev_err(dev
, "cannot enable vdda regulator\n");
484 /* change DBI base address */
485 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
487 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
488 u32 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
491 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
496 clk_disable_unprepare(res
->slave_bus
);
498 clk_disable_unprepare(res
->master_bus
);
500 clk_disable_unprepare(res
->iface
);
502 clk_disable_unprepare(res
->aux
);
504 reset_control_assert(res
->core
);
509 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie
*pcie
)
513 /* enable link training */
514 val
= readl(pcie
->parf
+ PCIE20_PARF_LTSSM
);
516 writel(val
, pcie
->parf
+ PCIE20_PARF_LTSSM
);
519 static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie
*pcie
)
521 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
522 struct dw_pcie
*pci
= pcie
->pci
;
523 struct device
*dev
= pci
->dev
;
525 res
->aux_clk
= devm_clk_get(dev
, "aux");
526 if (IS_ERR(res
->aux_clk
))
527 return PTR_ERR(res
->aux_clk
);
529 res
->cfg_clk
= devm_clk_get(dev
, "cfg");
530 if (IS_ERR(res
->cfg_clk
))
531 return PTR_ERR(res
->cfg_clk
);
533 res
->master_clk
= devm_clk_get(dev
, "bus_master");
534 if (IS_ERR(res
->master_clk
))
535 return PTR_ERR(res
->master_clk
);
537 res
->slave_clk
= devm_clk_get(dev
, "bus_slave");
538 if (IS_ERR(res
->slave_clk
))
539 return PTR_ERR(res
->slave_clk
);
541 res
->pipe_clk
= devm_clk_get(dev
, "pipe");
542 return PTR_ERR_OR_ZERO(res
->pipe_clk
);
545 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie
*pcie
)
547 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
549 clk_disable_unprepare(res
->slave_clk
);
550 clk_disable_unprepare(res
->master_clk
);
551 clk_disable_unprepare(res
->cfg_clk
);
552 clk_disable_unprepare(res
->aux_clk
);
555 static void qcom_pcie_post_deinit_2_3_2(struct qcom_pcie
*pcie
)
557 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
559 clk_disable_unprepare(res
->pipe_clk
);
562 static int qcom_pcie_init_2_3_2(struct qcom_pcie
*pcie
)
564 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
565 struct dw_pcie
*pci
= pcie
->pci
;
566 struct device
*dev
= pci
->dev
;
570 ret
= clk_prepare_enable(res
->aux_clk
);
572 dev_err(dev
, "cannot prepare/enable aux clock\n");
576 ret
= clk_prepare_enable(res
->cfg_clk
);
578 dev_err(dev
, "cannot prepare/enable cfg clock\n");
582 ret
= clk_prepare_enable(res
->master_clk
);
584 dev_err(dev
, "cannot prepare/enable master clock\n");
588 ret
= clk_prepare_enable(res
->slave_clk
);
590 dev_err(dev
, "cannot prepare/enable slave clock\n");
594 /* enable PCIe clocks and resets */
595 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
597 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
599 /* change DBI base address */
600 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
602 /* MAC PHY_POWERDOWN MUX DISABLE */
603 val
= readl(pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
605 writel(val
, pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
607 val
= readl(pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
609 writel(val
, pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
611 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
613 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
618 clk_disable_unprepare(res
->master_clk
);
620 clk_disable_unprepare(res
->cfg_clk
);
622 clk_disable_unprepare(res
->aux_clk
);
627 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie
*pcie
)
629 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
630 struct dw_pcie
*pci
= pcie
->pci
;
631 struct device
*dev
= pci
->dev
;
634 ret
= clk_prepare_enable(res
->pipe_clk
);
636 dev_err(dev
, "cannot prepare/enable pipe clock\n");
643 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie
*pcie
)
645 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
646 struct dw_pcie
*pci
= pcie
->pci
;
647 struct device
*dev
= pci
->dev
;
649 res
->aux_clk
= devm_clk_get(dev
, "aux");
650 if (IS_ERR(res
->aux_clk
))
651 return PTR_ERR(res
->aux_clk
);
653 res
->master_clk
= devm_clk_get(dev
, "master_bus");
654 if (IS_ERR(res
->master_clk
))
655 return PTR_ERR(res
->master_clk
);
657 res
->slave_clk
= devm_clk_get(dev
, "slave_bus");
658 if (IS_ERR(res
->slave_clk
))
659 return PTR_ERR(res
->slave_clk
);
661 res
->axi_m_reset
= devm_reset_control_get_exclusive(dev
, "axi_m");
662 if (IS_ERR(res
->axi_m_reset
))
663 return PTR_ERR(res
->axi_m_reset
);
665 res
->axi_s_reset
= devm_reset_control_get_exclusive(dev
, "axi_s");
666 if (IS_ERR(res
->axi_s_reset
))
667 return PTR_ERR(res
->axi_s_reset
);
669 res
->pipe_reset
= devm_reset_control_get_exclusive(dev
, "pipe");
670 if (IS_ERR(res
->pipe_reset
))
671 return PTR_ERR(res
->pipe_reset
);
673 res
->axi_m_vmid_reset
= devm_reset_control_get_exclusive(dev
,
675 if (IS_ERR(res
->axi_m_vmid_reset
))
676 return PTR_ERR(res
->axi_m_vmid_reset
);
678 res
->axi_s_xpu_reset
= devm_reset_control_get_exclusive(dev
,
680 if (IS_ERR(res
->axi_s_xpu_reset
))
681 return PTR_ERR(res
->axi_s_xpu_reset
);
683 res
->parf_reset
= devm_reset_control_get_exclusive(dev
, "parf");
684 if (IS_ERR(res
->parf_reset
))
685 return PTR_ERR(res
->parf_reset
);
687 res
->phy_reset
= devm_reset_control_get_exclusive(dev
, "phy");
688 if (IS_ERR(res
->phy_reset
))
689 return PTR_ERR(res
->phy_reset
);
691 res
->axi_m_sticky_reset
= devm_reset_control_get_exclusive(dev
,
693 if (IS_ERR(res
->axi_m_sticky_reset
))
694 return PTR_ERR(res
->axi_m_sticky_reset
);
696 res
->pipe_sticky_reset
= devm_reset_control_get_exclusive(dev
,
698 if (IS_ERR(res
->pipe_sticky_reset
))
699 return PTR_ERR(res
->pipe_sticky_reset
);
701 res
->pwr_reset
= devm_reset_control_get_exclusive(dev
, "pwr");
702 if (IS_ERR(res
->pwr_reset
))
703 return PTR_ERR(res
->pwr_reset
);
705 res
->ahb_reset
= devm_reset_control_get_exclusive(dev
, "ahb");
706 if (IS_ERR(res
->ahb_reset
))
707 return PTR_ERR(res
->ahb_reset
);
709 res
->phy_ahb_reset
= devm_reset_control_get_exclusive(dev
, "phy_ahb");
710 if (IS_ERR(res
->phy_ahb_reset
))
711 return PTR_ERR(res
->phy_ahb_reset
);
716 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie
*pcie
)
718 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
720 reset_control_assert(res
->axi_m_reset
);
721 reset_control_assert(res
->axi_s_reset
);
722 reset_control_assert(res
->pipe_reset
);
723 reset_control_assert(res
->pipe_sticky_reset
);
724 reset_control_assert(res
->phy_reset
);
725 reset_control_assert(res
->phy_ahb_reset
);
726 reset_control_assert(res
->axi_m_sticky_reset
);
727 reset_control_assert(res
->pwr_reset
);
728 reset_control_assert(res
->ahb_reset
);
729 clk_disable_unprepare(res
->aux_clk
);
730 clk_disable_unprepare(res
->master_clk
);
731 clk_disable_unprepare(res
->slave_clk
);
734 static int qcom_pcie_init_2_4_0(struct qcom_pcie
*pcie
)
736 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
737 struct dw_pcie
*pci
= pcie
->pci
;
738 struct device
*dev
= pci
->dev
;
742 ret
= reset_control_assert(res
->axi_m_reset
);
744 dev_err(dev
, "cannot assert axi master reset\n");
748 ret
= reset_control_assert(res
->axi_s_reset
);
750 dev_err(dev
, "cannot assert axi slave reset\n");
754 usleep_range(10000, 12000);
756 ret
= reset_control_assert(res
->pipe_reset
);
758 dev_err(dev
, "cannot assert pipe reset\n");
762 ret
= reset_control_assert(res
->pipe_sticky_reset
);
764 dev_err(dev
, "cannot assert pipe sticky reset\n");
768 ret
= reset_control_assert(res
->phy_reset
);
770 dev_err(dev
, "cannot assert phy reset\n");
774 ret
= reset_control_assert(res
->phy_ahb_reset
);
776 dev_err(dev
, "cannot assert phy ahb reset\n");
780 usleep_range(10000, 12000);
782 ret
= reset_control_assert(res
->axi_m_sticky_reset
);
784 dev_err(dev
, "cannot assert axi master sticky reset\n");
788 ret
= reset_control_assert(res
->pwr_reset
);
790 dev_err(dev
, "cannot assert power reset\n");
794 ret
= reset_control_assert(res
->ahb_reset
);
796 dev_err(dev
, "cannot assert ahb reset\n");
800 usleep_range(10000, 12000);
802 ret
= reset_control_deassert(res
->phy_ahb_reset
);
804 dev_err(dev
, "cannot deassert phy ahb reset\n");
808 ret
= reset_control_deassert(res
->phy_reset
);
810 dev_err(dev
, "cannot deassert phy reset\n");
814 ret
= reset_control_deassert(res
->pipe_reset
);
816 dev_err(dev
, "cannot deassert pipe reset\n");
820 ret
= reset_control_deassert(res
->pipe_sticky_reset
);
822 dev_err(dev
, "cannot deassert pipe sticky reset\n");
823 goto err_rst_pipe_sticky
;
826 usleep_range(10000, 12000);
828 ret
= reset_control_deassert(res
->axi_m_reset
);
830 dev_err(dev
, "cannot deassert axi master reset\n");
834 ret
= reset_control_deassert(res
->axi_m_sticky_reset
);
836 dev_err(dev
, "cannot deassert axi master sticky reset\n");
837 goto err_rst_axi_m_sticky
;
840 ret
= reset_control_deassert(res
->axi_s_reset
);
842 dev_err(dev
, "cannot deassert axi slave reset\n");
846 ret
= reset_control_deassert(res
->pwr_reset
);
848 dev_err(dev
, "cannot deassert power reset\n");
852 ret
= reset_control_deassert(res
->ahb_reset
);
854 dev_err(dev
, "cannot deassert ahb reset\n");
858 usleep_range(10000, 12000);
860 ret
= clk_prepare_enable(res
->aux_clk
);
862 dev_err(dev
, "cannot prepare/enable iface clock\n");
866 ret
= clk_prepare_enable(res
->master_clk
);
868 dev_err(dev
, "cannot prepare/enable core clock\n");
872 ret
= clk_prepare_enable(res
->slave_clk
);
874 dev_err(dev
, "cannot prepare/enable phy clock\n");
878 /* enable PCIe clocks and resets */
879 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
881 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
883 /* change DBI base address */
884 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
886 /* MAC PHY_POWERDOWN MUX DISABLE */
887 val
= readl(pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
889 writel(val
, pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
891 val
= readl(pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
893 writel(val
, pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
895 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
897 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
902 clk_disable_unprepare(res
->master_clk
);
904 clk_disable_unprepare(res
->aux_clk
);
906 reset_control_assert(res
->ahb_reset
);
908 reset_control_assert(res
->pwr_reset
);
910 reset_control_assert(res
->axi_s_reset
);
912 reset_control_assert(res
->axi_m_sticky_reset
);
913 err_rst_axi_m_sticky
:
914 reset_control_assert(res
->axi_m_reset
);
916 reset_control_assert(res
->pipe_sticky_reset
);
918 reset_control_assert(res
->pipe_reset
);
920 reset_control_assert(res
->phy_reset
);
922 reset_control_assert(res
->phy_ahb_reset
);
926 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie
*pcie
)
928 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
929 struct dw_pcie
*pci
= pcie
->pci
;
930 struct device
*dev
= pci
->dev
;
932 const char *rst_names
[] = { "axi_m", "axi_s", "pipe",
933 "axi_m_sticky", "sticky",
936 res
->iface
= devm_clk_get(dev
, "iface");
937 if (IS_ERR(res
->iface
))
938 return PTR_ERR(res
->iface
);
940 res
->axi_m_clk
= devm_clk_get(dev
, "axi_m");
941 if (IS_ERR(res
->axi_m_clk
))
942 return PTR_ERR(res
->axi_m_clk
);
944 res
->axi_s_clk
= devm_clk_get(dev
, "axi_s");
945 if (IS_ERR(res
->axi_s_clk
))
946 return PTR_ERR(res
->axi_s_clk
);
948 res
->ahb_clk
= devm_clk_get(dev
, "ahb");
949 if (IS_ERR(res
->ahb_clk
))
950 return PTR_ERR(res
->ahb_clk
);
952 res
->aux_clk
= devm_clk_get(dev
, "aux");
953 if (IS_ERR(res
->aux_clk
))
954 return PTR_ERR(res
->aux_clk
);
956 for (i
= 0; i
< ARRAY_SIZE(rst_names
); i
++) {
957 res
->rst
[i
] = devm_reset_control_get(dev
, rst_names
[i
]);
958 if (IS_ERR(res
->rst
[i
]))
959 return PTR_ERR(res
->rst
[i
]);
965 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie
*pcie
)
967 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
969 clk_disable_unprepare(res
->iface
);
970 clk_disable_unprepare(res
->axi_m_clk
);
971 clk_disable_unprepare(res
->axi_s_clk
);
972 clk_disable_unprepare(res
->ahb_clk
);
973 clk_disable_unprepare(res
->aux_clk
);
976 static int qcom_pcie_init_2_3_3(struct qcom_pcie
*pcie
)
978 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
979 struct dw_pcie
*pci
= pcie
->pci
;
980 struct device
*dev
= pci
->dev
;
984 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++) {
985 ret
= reset_control_assert(res
->rst
[i
]);
987 dev_err(dev
, "reset #%d assert failed (%d)\n", i
, ret
);
992 usleep_range(2000, 2500);
994 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++) {
995 ret
= reset_control_deassert(res
->rst
[i
]);
997 dev_err(dev
, "reset #%d deassert failed (%d)\n", i
,
1004 * Don't have a way to see if the reset has completed.
1005 * Wait for some time.
1007 usleep_range(2000, 2500);
1009 ret
= clk_prepare_enable(res
->iface
);
1011 dev_err(dev
, "cannot prepare/enable core clock\n");
1015 ret
= clk_prepare_enable(res
->axi_m_clk
);
1017 dev_err(dev
, "cannot prepare/enable core clock\n");
1021 ret
= clk_prepare_enable(res
->axi_s_clk
);
1023 dev_err(dev
, "cannot prepare/enable axi slave clock\n");
1027 ret
= clk_prepare_enable(res
->ahb_clk
);
1029 dev_err(dev
, "cannot prepare/enable ahb clock\n");
1033 ret
= clk_prepare_enable(res
->aux_clk
);
1035 dev_err(dev
, "cannot prepare/enable aux clock\n");
1039 writel(SLV_ADDR_SPACE_SZ
,
1040 pcie
->parf
+ PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE
);
1042 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
1044 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
1046 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
1048 writel(MST_WAKEUP_EN
| SLV_WAKEUP_EN
| MSTR_ACLK_CGC_DIS
1049 | SLV_ACLK_CGC_DIS
| CORE_CLK_CGC_DIS
|
1050 AUX_PWR_DET
| L23_CLK_RMV_DIS
| L1_CLK_RMV_DIS
,
1051 pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
1052 writel(0, pcie
->parf
+ PCIE20_PARF_Q2A_FLUSH
);
1054 writel(CMD_BME_VAL
, pci
->dbi_base
+ PCIE20_COMMAND_STATUS
);
1055 writel(DBI_RO_WR_EN
, pci
->dbi_base
+ PCIE20_MISC_CONTROL_1_REG
);
1056 writel(PCIE_CAP_LINK1_VAL
, pci
->dbi_base
+ PCIE20_CAP_LINK_1
);
1058 val
= readl(pci
->dbi_base
+ PCIE20_CAP_LINK_CAPABILITIES
);
1059 val
&= ~PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT
;
1060 writel(val
, pci
->dbi_base
+ PCIE20_CAP_LINK_CAPABILITIES
);
1062 writel(PCIE_CAP_CPL_TIMEOUT_DISABLE
, pci
->dbi_base
+
1063 PCIE20_DEVICE_CONTROL2_STATUS2
);
1068 clk_disable_unprepare(res
->ahb_clk
);
1070 clk_disable_unprepare(res
->axi_s_clk
);
1072 clk_disable_unprepare(res
->axi_m_clk
);
1074 clk_disable_unprepare(res
->iface
);
1077 * Not checking for failure, will anyway return
1078 * the original failure in 'ret'.
1080 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++)
1081 reset_control_assert(res
->rst
[i
]);
1086 static int qcom_pcie_link_up(struct dw_pcie
*pci
)
1088 u16 val
= readw(pci
->dbi_base
+ PCIE20_CAP
+ PCI_EXP_LNKSTA
);
1090 return !!(val
& PCI_EXP_LNKSTA_DLLLA
);
1093 static int qcom_pcie_host_init(struct pcie_port
*pp
)
1095 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
1096 struct qcom_pcie
*pcie
= to_qcom_pcie(pci
);
1099 qcom_ep_reset_assert(pcie
);
1101 ret
= pcie
->ops
->init(pcie
);
1105 ret
= phy_power_on(pcie
->phy
);
1109 if (pcie
->ops
->post_init
) {
1110 ret
= pcie
->ops
->post_init(pcie
);
1112 goto err_disable_phy
;
1115 dw_pcie_setup_rc(pp
);
1117 if (IS_ENABLED(CONFIG_PCI_MSI
))
1118 dw_pcie_msi_init(pp
);
1120 qcom_ep_reset_deassert(pcie
);
1122 ret
= qcom_pcie_establish_link(pcie
);
1128 qcom_ep_reset_assert(pcie
);
1129 if (pcie
->ops
->post_deinit
)
1130 pcie
->ops
->post_deinit(pcie
);
1132 phy_power_off(pcie
->phy
);
1134 pcie
->ops
->deinit(pcie
);
1139 static int qcom_pcie_rd_own_conf(struct pcie_port
*pp
, int where
, int size
,
1142 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
1144 /* the device class is not reported correctly from the register */
1145 if (where
== PCI_CLASS_REVISION
&& size
== 4) {
1146 *val
= readl(pci
->dbi_base
+ PCI_CLASS_REVISION
);
1147 *val
&= 0xff; /* keep revision id */
1148 *val
|= PCI_CLASS_BRIDGE_PCI
<< 16;
1149 return PCIBIOS_SUCCESSFUL
;
1152 return dw_pcie_read(pci
->dbi_base
+ where
, size
, val
);
1155 static const struct dw_pcie_host_ops qcom_pcie_dw_ops
= {
1156 .host_init
= qcom_pcie_host_init
,
1157 .rd_own_conf
= qcom_pcie_rd_own_conf
,
1160 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1161 static const struct qcom_pcie_ops ops_2_1_0
= {
1162 .get_resources
= qcom_pcie_get_resources_2_1_0
,
1163 .init
= qcom_pcie_init_2_1_0
,
1164 .deinit
= qcom_pcie_deinit_2_1_0
,
1165 .ltssm_enable
= qcom_pcie_2_1_0_ltssm_enable
,
1168 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1169 static const struct qcom_pcie_ops ops_1_0_0
= {
1170 .get_resources
= qcom_pcie_get_resources_1_0_0
,
1171 .init
= qcom_pcie_init_1_0_0
,
1172 .deinit
= qcom_pcie_deinit_1_0_0
,
1173 .ltssm_enable
= qcom_pcie_2_1_0_ltssm_enable
,
1176 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1177 static const struct qcom_pcie_ops ops_2_3_2
= {
1178 .get_resources
= qcom_pcie_get_resources_2_3_2
,
1179 .init
= qcom_pcie_init_2_3_2
,
1180 .post_init
= qcom_pcie_post_init_2_3_2
,
1181 .deinit
= qcom_pcie_deinit_2_3_2
,
1182 .post_deinit
= qcom_pcie_post_deinit_2_3_2
,
1183 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1186 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1187 static const struct qcom_pcie_ops ops_2_4_0
= {
1188 .get_resources
= qcom_pcie_get_resources_2_4_0
,
1189 .init
= qcom_pcie_init_2_4_0
,
1190 .deinit
= qcom_pcie_deinit_2_4_0
,
1191 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1194 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1195 static const struct qcom_pcie_ops ops_2_3_3
= {
1196 .get_resources
= qcom_pcie_get_resources_2_3_3
,
1197 .init
= qcom_pcie_init_2_3_3
,
1198 .deinit
= qcom_pcie_deinit_2_3_3
,
1199 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1202 static const struct dw_pcie_ops dw_pcie_ops
= {
1203 .link_up
= qcom_pcie_link_up
,
1206 static int qcom_pcie_probe(struct platform_device
*pdev
)
1208 struct device
*dev
= &pdev
->dev
;
1209 struct resource
*res
;
1210 struct pcie_port
*pp
;
1211 struct dw_pcie
*pci
;
1212 struct qcom_pcie
*pcie
;
1215 pcie
= devm_kzalloc(dev
, sizeof(*pcie
), GFP_KERNEL
);
1219 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
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_LOW
);
1232 if (IS_ERR(pcie
->reset
))
1233 return PTR_ERR(pcie
->reset
);
1235 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "parf");
1236 pcie
->parf
= devm_ioremap_resource(dev
, res
);
1237 if (IS_ERR(pcie
->parf
))
1238 return PTR_ERR(pcie
->parf
);
1240 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbi");
1241 pci
->dbi_base
= devm_pci_remap_cfg_resource(dev
, res
);
1242 if (IS_ERR(pci
->dbi_base
))
1243 return PTR_ERR(pci
->dbi_base
);
1245 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "elbi");
1246 pcie
->elbi
= devm_ioremap_resource(dev
, res
);
1247 if (IS_ERR(pcie
->elbi
))
1248 return PTR_ERR(pcie
->elbi
);
1250 pcie
->phy
= devm_phy_optional_get(dev
, "pciephy");
1251 if (IS_ERR(pcie
->phy
))
1252 return PTR_ERR(pcie
->phy
);
1254 ret
= pcie
->ops
->get_resources(pcie
);
1258 pp
->root_bus_nr
= -1;
1259 pp
->ops
= &qcom_pcie_dw_ops
;
1261 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
1262 pp
->msi_irq
= platform_get_irq_byname(pdev
, "msi");
1263 if (pp
->msi_irq
< 0)
1266 ret
= devm_request_irq(dev
, pp
->msi_irq
,
1267 qcom_pcie_msi_irq_handler
,
1268 IRQF_SHARED
| IRQF_NO_THREAD
,
1269 "qcom-pcie-msi", pp
);
1271 dev_err(dev
, "cannot request msi irq\n");
1276 ret
= phy_init(pcie
->phy
);
1280 platform_set_drvdata(pdev
, pcie
);
1282 ret
= dw_pcie_host_init(pp
);
1284 dev_err(dev
, "cannot initialize host\n");
1291 static const struct of_device_id qcom_pcie_match
[] = {
1292 { .compatible
= "qcom,pcie-apq8084", .data
= &ops_1_0_0
},
1293 { .compatible
= "qcom,pcie-ipq8064", .data
= &ops_2_1_0
},
1294 { .compatible
= "qcom,pcie-apq8064", .data
= &ops_2_1_0
},
1295 { .compatible
= "qcom,pcie-msm8996", .data
= &ops_2_3_2
},
1296 { .compatible
= "qcom,pcie-ipq8074", .data
= &ops_2_3_3
},
1297 { .compatible
= "qcom,pcie-ipq4019", .data
= &ops_2_4_0
},
1301 static struct platform_driver qcom_pcie_driver
= {
1302 .probe
= qcom_pcie_probe
,
1304 .name
= "qcom-pcie",
1305 .suppress_bind_attrs
= true,
1306 .of_match_table
= qcom_pcie_match
,
1309 builtin_platform_driver(qcom_pcie_driver
);