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 /* Ensure that PERST has been asserted for at least 100 ms */
183 gpiod_set_value_cansleep(pcie
->reset
, 0);
184 usleep_range(PERST_DELAY_US
, PERST_DELAY_US
+ 500);
187 static int qcom_pcie_establish_link(struct qcom_pcie
*pcie
)
189 struct dw_pcie
*pci
= pcie
->pci
;
191 if (dw_pcie_link_up(pci
))
194 /* Enable Link Training state machine */
195 if (pcie
->ops
->ltssm_enable
)
196 pcie
->ops
->ltssm_enable(pcie
);
198 return dw_pcie_wait_for_link(pci
);
201 static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie
*pcie
)
205 /* enable link training */
206 val
= readl(pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
207 val
|= PCIE20_ELBI_SYS_CTRL_LT_ENABLE
;
208 writel(val
, pcie
->elbi
+ PCIE20_ELBI_SYS_CTRL
);
211 static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie
*pcie
)
213 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
214 struct dw_pcie
*pci
= pcie
->pci
;
215 struct device
*dev
= pci
->dev
;
218 res
->supplies
[0].supply
= "vdda";
219 res
->supplies
[1].supply
= "vdda_phy";
220 res
->supplies
[2].supply
= "vdda_refclk";
221 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(res
->supplies
),
226 res
->iface_clk
= devm_clk_get(dev
, "iface");
227 if (IS_ERR(res
->iface_clk
))
228 return PTR_ERR(res
->iface_clk
);
230 res
->core_clk
= devm_clk_get(dev
, "core");
231 if (IS_ERR(res
->core_clk
))
232 return PTR_ERR(res
->core_clk
);
234 res
->phy_clk
= devm_clk_get(dev
, "phy");
235 if (IS_ERR(res
->phy_clk
))
236 return PTR_ERR(res
->phy_clk
);
238 res
->pci_reset
= devm_reset_control_get_exclusive(dev
, "pci");
239 if (IS_ERR(res
->pci_reset
))
240 return PTR_ERR(res
->pci_reset
);
242 res
->axi_reset
= devm_reset_control_get_exclusive(dev
, "axi");
243 if (IS_ERR(res
->axi_reset
))
244 return PTR_ERR(res
->axi_reset
);
246 res
->ahb_reset
= devm_reset_control_get_exclusive(dev
, "ahb");
247 if (IS_ERR(res
->ahb_reset
))
248 return PTR_ERR(res
->ahb_reset
);
250 res
->por_reset
= devm_reset_control_get_exclusive(dev
, "por");
251 if (IS_ERR(res
->por_reset
))
252 return PTR_ERR(res
->por_reset
);
254 res
->phy_reset
= devm_reset_control_get_exclusive(dev
, "phy");
255 return PTR_ERR_OR_ZERO(res
->phy_reset
);
258 static void qcom_pcie_deinit_2_1_0(struct qcom_pcie
*pcie
)
260 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
262 reset_control_assert(res
->pci_reset
);
263 reset_control_assert(res
->axi_reset
);
264 reset_control_assert(res
->ahb_reset
);
265 reset_control_assert(res
->por_reset
);
266 reset_control_assert(res
->pci_reset
);
267 clk_disable_unprepare(res
->iface_clk
);
268 clk_disable_unprepare(res
->core_clk
);
269 clk_disable_unprepare(res
->phy_clk
);
270 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
273 static int qcom_pcie_init_2_1_0(struct qcom_pcie
*pcie
)
275 struct qcom_pcie_resources_2_1_0
*res
= &pcie
->res
.v2_1_0
;
276 struct dw_pcie
*pci
= pcie
->pci
;
277 struct device
*dev
= pci
->dev
;
281 ret
= regulator_bulk_enable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
283 dev_err(dev
, "cannot enable regulators\n");
287 ret
= reset_control_assert(res
->ahb_reset
);
289 dev_err(dev
, "cannot assert ahb reset\n");
293 ret
= clk_prepare_enable(res
->iface_clk
);
295 dev_err(dev
, "cannot prepare/enable iface clock\n");
299 ret
= clk_prepare_enable(res
->phy_clk
);
301 dev_err(dev
, "cannot prepare/enable phy clock\n");
305 ret
= clk_prepare_enable(res
->core_clk
);
307 dev_err(dev
, "cannot prepare/enable core clock\n");
311 ret
= reset_control_deassert(res
->ahb_reset
);
313 dev_err(dev
, "cannot deassert ahb reset\n");
314 goto err_deassert_ahb
;
317 /* enable PCIe clocks and resets */
318 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
320 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
322 /* enable external reference clock */
323 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
325 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_REFCLK
);
327 ret
= reset_control_deassert(res
->phy_reset
);
329 dev_err(dev
, "cannot deassert phy reset\n");
333 ret
= reset_control_deassert(res
->pci_reset
);
335 dev_err(dev
, "cannot deassert pci reset\n");
339 ret
= reset_control_deassert(res
->por_reset
);
341 dev_err(dev
, "cannot deassert por reset\n");
345 ret
= reset_control_deassert(res
->axi_reset
);
347 dev_err(dev
, "cannot deassert axi reset\n");
351 /* wait for clock acquisition */
352 usleep_range(1000, 1500);
355 /* Set the Max TLP size to 2K, instead of using default of 4K */
356 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K
,
357 pci
->dbi_base
+ PCIE20_AXI_MSTR_RESP_COMP_CTRL0
);
358 writel(CFG_BRIDGE_SB_INIT
,
359 pci
->dbi_base
+ PCIE20_AXI_MSTR_RESP_COMP_CTRL1
);
364 clk_disable_unprepare(res
->core_clk
);
366 clk_disable_unprepare(res
->phy_clk
);
368 clk_disable_unprepare(res
->iface_clk
);
370 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
375 static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie
*pcie
)
377 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
378 struct dw_pcie
*pci
= pcie
->pci
;
379 struct device
*dev
= pci
->dev
;
381 res
->vdda
= devm_regulator_get(dev
, "vdda");
382 if (IS_ERR(res
->vdda
))
383 return PTR_ERR(res
->vdda
);
385 res
->iface
= devm_clk_get(dev
, "iface");
386 if (IS_ERR(res
->iface
))
387 return PTR_ERR(res
->iface
);
389 res
->aux
= devm_clk_get(dev
, "aux");
390 if (IS_ERR(res
->aux
))
391 return PTR_ERR(res
->aux
);
393 res
->master_bus
= devm_clk_get(dev
, "master_bus");
394 if (IS_ERR(res
->master_bus
))
395 return PTR_ERR(res
->master_bus
);
397 res
->slave_bus
= devm_clk_get(dev
, "slave_bus");
398 if (IS_ERR(res
->slave_bus
))
399 return PTR_ERR(res
->slave_bus
);
401 res
->core
= devm_reset_control_get_exclusive(dev
, "core");
402 return PTR_ERR_OR_ZERO(res
->core
);
405 static void qcom_pcie_deinit_1_0_0(struct qcom_pcie
*pcie
)
407 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
409 reset_control_assert(res
->core
);
410 clk_disable_unprepare(res
->slave_bus
);
411 clk_disable_unprepare(res
->master_bus
);
412 clk_disable_unprepare(res
->iface
);
413 clk_disable_unprepare(res
->aux
);
414 regulator_disable(res
->vdda
);
417 static int qcom_pcie_init_1_0_0(struct qcom_pcie
*pcie
)
419 struct qcom_pcie_resources_1_0_0
*res
= &pcie
->res
.v1_0_0
;
420 struct dw_pcie
*pci
= pcie
->pci
;
421 struct device
*dev
= pci
->dev
;
424 ret
= reset_control_deassert(res
->core
);
426 dev_err(dev
, "cannot deassert core reset\n");
430 ret
= clk_prepare_enable(res
->aux
);
432 dev_err(dev
, "cannot prepare/enable aux clock\n");
436 ret
= clk_prepare_enable(res
->iface
);
438 dev_err(dev
, "cannot prepare/enable iface clock\n");
442 ret
= clk_prepare_enable(res
->master_bus
);
444 dev_err(dev
, "cannot prepare/enable master_bus clock\n");
448 ret
= clk_prepare_enable(res
->slave_bus
);
450 dev_err(dev
, "cannot prepare/enable slave_bus clock\n");
454 ret
= regulator_enable(res
->vdda
);
456 dev_err(dev
, "cannot enable vdda regulator\n");
460 /* change DBI base address */
461 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
463 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
464 u32 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
467 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT
);
472 clk_disable_unprepare(res
->slave_bus
);
474 clk_disable_unprepare(res
->master_bus
);
476 clk_disable_unprepare(res
->iface
);
478 clk_disable_unprepare(res
->aux
);
480 reset_control_assert(res
->core
);
485 static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie
*pcie
)
489 /* enable link training */
490 val
= readl(pcie
->parf
+ PCIE20_PARF_LTSSM
);
492 writel(val
, pcie
->parf
+ PCIE20_PARF_LTSSM
);
495 static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie
*pcie
)
497 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
498 struct dw_pcie
*pci
= pcie
->pci
;
499 struct device
*dev
= pci
->dev
;
502 res
->supplies
[0].supply
= "vdda";
503 res
->supplies
[1].supply
= "vddpe-3v3";
504 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(res
->supplies
),
509 res
->aux_clk
= devm_clk_get(dev
, "aux");
510 if (IS_ERR(res
->aux_clk
))
511 return PTR_ERR(res
->aux_clk
);
513 res
->cfg_clk
= devm_clk_get(dev
, "cfg");
514 if (IS_ERR(res
->cfg_clk
))
515 return PTR_ERR(res
->cfg_clk
);
517 res
->master_clk
= devm_clk_get(dev
, "bus_master");
518 if (IS_ERR(res
->master_clk
))
519 return PTR_ERR(res
->master_clk
);
521 res
->slave_clk
= devm_clk_get(dev
, "bus_slave");
522 if (IS_ERR(res
->slave_clk
))
523 return PTR_ERR(res
->slave_clk
);
525 res
->pipe_clk
= devm_clk_get(dev
, "pipe");
526 return PTR_ERR_OR_ZERO(res
->pipe_clk
);
529 static void qcom_pcie_deinit_2_3_2(struct qcom_pcie
*pcie
)
531 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
533 clk_disable_unprepare(res
->slave_clk
);
534 clk_disable_unprepare(res
->master_clk
);
535 clk_disable_unprepare(res
->cfg_clk
);
536 clk_disable_unprepare(res
->aux_clk
);
538 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
541 static void qcom_pcie_post_deinit_2_3_2(struct qcom_pcie
*pcie
)
543 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
545 clk_disable_unprepare(res
->pipe_clk
);
548 static int qcom_pcie_init_2_3_2(struct qcom_pcie
*pcie
)
550 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
551 struct dw_pcie
*pci
= pcie
->pci
;
552 struct device
*dev
= pci
->dev
;
556 ret
= regulator_bulk_enable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
558 dev_err(dev
, "cannot enable regulators\n");
562 ret
= clk_prepare_enable(res
->aux_clk
);
564 dev_err(dev
, "cannot prepare/enable aux clock\n");
568 ret
= clk_prepare_enable(res
->cfg_clk
);
570 dev_err(dev
, "cannot prepare/enable cfg clock\n");
574 ret
= clk_prepare_enable(res
->master_clk
);
576 dev_err(dev
, "cannot prepare/enable master clock\n");
580 ret
= clk_prepare_enable(res
->slave_clk
);
582 dev_err(dev
, "cannot prepare/enable slave clock\n");
586 /* enable PCIe clocks and resets */
587 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
589 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
591 /* change DBI base address */
592 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
594 /* MAC PHY_POWERDOWN MUX DISABLE */
595 val
= readl(pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
597 writel(val
, pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
599 val
= readl(pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
601 writel(val
, pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
603 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
605 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
610 clk_disable_unprepare(res
->master_clk
);
612 clk_disable_unprepare(res
->cfg_clk
);
614 clk_disable_unprepare(res
->aux_clk
);
617 regulator_bulk_disable(ARRAY_SIZE(res
->supplies
), res
->supplies
);
622 static int qcom_pcie_post_init_2_3_2(struct qcom_pcie
*pcie
)
624 struct qcom_pcie_resources_2_3_2
*res
= &pcie
->res
.v2_3_2
;
625 struct dw_pcie
*pci
= pcie
->pci
;
626 struct device
*dev
= pci
->dev
;
629 ret
= clk_prepare_enable(res
->pipe_clk
);
631 dev_err(dev
, "cannot prepare/enable pipe clock\n");
638 static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie
*pcie
)
640 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
641 struct dw_pcie
*pci
= pcie
->pci
;
642 struct device
*dev
= pci
->dev
;
644 res
->aux_clk
= devm_clk_get(dev
, "aux");
645 if (IS_ERR(res
->aux_clk
))
646 return PTR_ERR(res
->aux_clk
);
648 res
->master_clk
= devm_clk_get(dev
, "master_bus");
649 if (IS_ERR(res
->master_clk
))
650 return PTR_ERR(res
->master_clk
);
652 res
->slave_clk
= devm_clk_get(dev
, "slave_bus");
653 if (IS_ERR(res
->slave_clk
))
654 return PTR_ERR(res
->slave_clk
);
656 res
->axi_m_reset
= devm_reset_control_get_exclusive(dev
, "axi_m");
657 if (IS_ERR(res
->axi_m_reset
))
658 return PTR_ERR(res
->axi_m_reset
);
660 res
->axi_s_reset
= devm_reset_control_get_exclusive(dev
, "axi_s");
661 if (IS_ERR(res
->axi_s_reset
))
662 return PTR_ERR(res
->axi_s_reset
);
664 res
->pipe_reset
= devm_reset_control_get_exclusive(dev
, "pipe");
665 if (IS_ERR(res
->pipe_reset
))
666 return PTR_ERR(res
->pipe_reset
);
668 res
->axi_m_vmid_reset
= devm_reset_control_get_exclusive(dev
,
670 if (IS_ERR(res
->axi_m_vmid_reset
))
671 return PTR_ERR(res
->axi_m_vmid_reset
);
673 res
->axi_s_xpu_reset
= devm_reset_control_get_exclusive(dev
,
675 if (IS_ERR(res
->axi_s_xpu_reset
))
676 return PTR_ERR(res
->axi_s_xpu_reset
);
678 res
->parf_reset
= devm_reset_control_get_exclusive(dev
, "parf");
679 if (IS_ERR(res
->parf_reset
))
680 return PTR_ERR(res
->parf_reset
);
682 res
->phy_reset
= devm_reset_control_get_exclusive(dev
, "phy");
683 if (IS_ERR(res
->phy_reset
))
684 return PTR_ERR(res
->phy_reset
);
686 res
->axi_m_sticky_reset
= devm_reset_control_get_exclusive(dev
,
688 if (IS_ERR(res
->axi_m_sticky_reset
))
689 return PTR_ERR(res
->axi_m_sticky_reset
);
691 res
->pipe_sticky_reset
= devm_reset_control_get_exclusive(dev
,
693 if (IS_ERR(res
->pipe_sticky_reset
))
694 return PTR_ERR(res
->pipe_sticky_reset
);
696 res
->pwr_reset
= devm_reset_control_get_exclusive(dev
, "pwr");
697 if (IS_ERR(res
->pwr_reset
))
698 return PTR_ERR(res
->pwr_reset
);
700 res
->ahb_reset
= devm_reset_control_get_exclusive(dev
, "ahb");
701 if (IS_ERR(res
->ahb_reset
))
702 return PTR_ERR(res
->ahb_reset
);
704 res
->phy_ahb_reset
= devm_reset_control_get_exclusive(dev
, "phy_ahb");
705 if (IS_ERR(res
->phy_ahb_reset
))
706 return PTR_ERR(res
->phy_ahb_reset
);
711 static void qcom_pcie_deinit_2_4_0(struct qcom_pcie
*pcie
)
713 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
715 reset_control_assert(res
->axi_m_reset
);
716 reset_control_assert(res
->axi_s_reset
);
717 reset_control_assert(res
->pipe_reset
);
718 reset_control_assert(res
->pipe_sticky_reset
);
719 reset_control_assert(res
->phy_reset
);
720 reset_control_assert(res
->phy_ahb_reset
);
721 reset_control_assert(res
->axi_m_sticky_reset
);
722 reset_control_assert(res
->pwr_reset
);
723 reset_control_assert(res
->ahb_reset
);
724 clk_disable_unprepare(res
->aux_clk
);
725 clk_disable_unprepare(res
->master_clk
);
726 clk_disable_unprepare(res
->slave_clk
);
729 static int qcom_pcie_init_2_4_0(struct qcom_pcie
*pcie
)
731 struct qcom_pcie_resources_2_4_0
*res
= &pcie
->res
.v2_4_0
;
732 struct dw_pcie
*pci
= pcie
->pci
;
733 struct device
*dev
= pci
->dev
;
737 ret
= reset_control_assert(res
->axi_m_reset
);
739 dev_err(dev
, "cannot assert axi master reset\n");
743 ret
= reset_control_assert(res
->axi_s_reset
);
745 dev_err(dev
, "cannot assert axi slave reset\n");
749 usleep_range(10000, 12000);
751 ret
= reset_control_assert(res
->pipe_reset
);
753 dev_err(dev
, "cannot assert pipe reset\n");
757 ret
= reset_control_assert(res
->pipe_sticky_reset
);
759 dev_err(dev
, "cannot assert pipe sticky reset\n");
763 ret
= reset_control_assert(res
->phy_reset
);
765 dev_err(dev
, "cannot assert phy reset\n");
769 ret
= reset_control_assert(res
->phy_ahb_reset
);
771 dev_err(dev
, "cannot assert phy ahb reset\n");
775 usleep_range(10000, 12000);
777 ret
= reset_control_assert(res
->axi_m_sticky_reset
);
779 dev_err(dev
, "cannot assert axi master sticky reset\n");
783 ret
= reset_control_assert(res
->pwr_reset
);
785 dev_err(dev
, "cannot assert power reset\n");
789 ret
= reset_control_assert(res
->ahb_reset
);
791 dev_err(dev
, "cannot assert ahb reset\n");
795 usleep_range(10000, 12000);
797 ret
= reset_control_deassert(res
->phy_ahb_reset
);
799 dev_err(dev
, "cannot deassert phy ahb reset\n");
803 ret
= reset_control_deassert(res
->phy_reset
);
805 dev_err(dev
, "cannot deassert phy reset\n");
809 ret
= reset_control_deassert(res
->pipe_reset
);
811 dev_err(dev
, "cannot deassert pipe reset\n");
815 ret
= reset_control_deassert(res
->pipe_sticky_reset
);
817 dev_err(dev
, "cannot deassert pipe sticky reset\n");
818 goto err_rst_pipe_sticky
;
821 usleep_range(10000, 12000);
823 ret
= reset_control_deassert(res
->axi_m_reset
);
825 dev_err(dev
, "cannot deassert axi master reset\n");
829 ret
= reset_control_deassert(res
->axi_m_sticky_reset
);
831 dev_err(dev
, "cannot deassert axi master sticky reset\n");
832 goto err_rst_axi_m_sticky
;
835 ret
= reset_control_deassert(res
->axi_s_reset
);
837 dev_err(dev
, "cannot deassert axi slave reset\n");
841 ret
= reset_control_deassert(res
->pwr_reset
);
843 dev_err(dev
, "cannot deassert power reset\n");
847 ret
= reset_control_deassert(res
->ahb_reset
);
849 dev_err(dev
, "cannot deassert ahb reset\n");
853 usleep_range(10000, 12000);
855 ret
= clk_prepare_enable(res
->aux_clk
);
857 dev_err(dev
, "cannot prepare/enable iface clock\n");
861 ret
= clk_prepare_enable(res
->master_clk
);
863 dev_err(dev
, "cannot prepare/enable core clock\n");
867 ret
= clk_prepare_enable(res
->slave_clk
);
869 dev_err(dev
, "cannot prepare/enable phy clock\n");
873 /* enable PCIe clocks and resets */
874 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
876 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
878 /* change DBI base address */
879 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
881 /* MAC PHY_POWERDOWN MUX DISABLE */
882 val
= readl(pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
884 writel(val
, pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
886 val
= readl(pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
888 writel(val
, pcie
->parf
+ PCIE20_PARF_MHI_CLOCK_RESET_CTRL
);
890 val
= readl(pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
892 writel(val
, pcie
->parf
+ PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2
);
897 clk_disable_unprepare(res
->master_clk
);
899 clk_disable_unprepare(res
->aux_clk
);
901 reset_control_assert(res
->ahb_reset
);
903 reset_control_assert(res
->pwr_reset
);
905 reset_control_assert(res
->axi_s_reset
);
907 reset_control_assert(res
->axi_m_sticky_reset
);
908 err_rst_axi_m_sticky
:
909 reset_control_assert(res
->axi_m_reset
);
911 reset_control_assert(res
->pipe_sticky_reset
);
913 reset_control_assert(res
->pipe_reset
);
915 reset_control_assert(res
->phy_reset
);
917 reset_control_assert(res
->phy_ahb_reset
);
921 static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie
*pcie
)
923 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
924 struct dw_pcie
*pci
= pcie
->pci
;
925 struct device
*dev
= pci
->dev
;
927 const char *rst_names
[] = { "axi_m", "axi_s", "pipe",
928 "axi_m_sticky", "sticky",
931 res
->iface
= devm_clk_get(dev
, "iface");
932 if (IS_ERR(res
->iface
))
933 return PTR_ERR(res
->iface
);
935 res
->axi_m_clk
= devm_clk_get(dev
, "axi_m");
936 if (IS_ERR(res
->axi_m_clk
))
937 return PTR_ERR(res
->axi_m_clk
);
939 res
->axi_s_clk
= devm_clk_get(dev
, "axi_s");
940 if (IS_ERR(res
->axi_s_clk
))
941 return PTR_ERR(res
->axi_s_clk
);
943 res
->ahb_clk
= devm_clk_get(dev
, "ahb");
944 if (IS_ERR(res
->ahb_clk
))
945 return PTR_ERR(res
->ahb_clk
);
947 res
->aux_clk
= devm_clk_get(dev
, "aux");
948 if (IS_ERR(res
->aux_clk
))
949 return PTR_ERR(res
->aux_clk
);
951 for (i
= 0; i
< ARRAY_SIZE(rst_names
); i
++) {
952 res
->rst
[i
] = devm_reset_control_get(dev
, rst_names
[i
]);
953 if (IS_ERR(res
->rst
[i
]))
954 return PTR_ERR(res
->rst
[i
]);
960 static void qcom_pcie_deinit_2_3_3(struct qcom_pcie
*pcie
)
962 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
964 clk_disable_unprepare(res
->iface
);
965 clk_disable_unprepare(res
->axi_m_clk
);
966 clk_disable_unprepare(res
->axi_s_clk
);
967 clk_disable_unprepare(res
->ahb_clk
);
968 clk_disable_unprepare(res
->aux_clk
);
971 static int qcom_pcie_init_2_3_3(struct qcom_pcie
*pcie
)
973 struct qcom_pcie_resources_2_3_3
*res
= &pcie
->res
.v2_3_3
;
974 struct dw_pcie
*pci
= pcie
->pci
;
975 struct device
*dev
= pci
->dev
;
979 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++) {
980 ret
= reset_control_assert(res
->rst
[i
]);
982 dev_err(dev
, "reset #%d assert failed (%d)\n", i
, ret
);
987 usleep_range(2000, 2500);
989 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++) {
990 ret
= reset_control_deassert(res
->rst
[i
]);
992 dev_err(dev
, "reset #%d deassert failed (%d)\n", i
,
999 * Don't have a way to see if the reset has completed.
1000 * Wait for some time.
1002 usleep_range(2000, 2500);
1004 ret
= clk_prepare_enable(res
->iface
);
1006 dev_err(dev
, "cannot prepare/enable core clock\n");
1010 ret
= clk_prepare_enable(res
->axi_m_clk
);
1012 dev_err(dev
, "cannot prepare/enable core clock\n");
1016 ret
= clk_prepare_enable(res
->axi_s_clk
);
1018 dev_err(dev
, "cannot prepare/enable axi slave clock\n");
1022 ret
= clk_prepare_enable(res
->ahb_clk
);
1024 dev_err(dev
, "cannot prepare/enable ahb clock\n");
1028 ret
= clk_prepare_enable(res
->aux_clk
);
1030 dev_err(dev
, "cannot prepare/enable aux clock\n");
1034 writel(SLV_ADDR_SPACE_SZ
,
1035 pcie
->parf
+ PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE
);
1037 val
= readl(pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
1039 writel(val
, pcie
->parf
+ PCIE20_PARF_PHY_CTRL
);
1041 writel(0, pcie
->parf
+ PCIE20_PARF_DBI_BASE_ADDR
);
1043 writel(MST_WAKEUP_EN
| SLV_WAKEUP_EN
| MSTR_ACLK_CGC_DIS
1044 | SLV_ACLK_CGC_DIS
| CORE_CLK_CGC_DIS
|
1045 AUX_PWR_DET
| L23_CLK_RMV_DIS
| L1_CLK_RMV_DIS
,
1046 pcie
->parf
+ PCIE20_PARF_SYS_CTRL
);
1047 writel(0, pcie
->parf
+ PCIE20_PARF_Q2A_FLUSH
);
1049 writel(CMD_BME_VAL
, pci
->dbi_base
+ PCIE20_COMMAND_STATUS
);
1050 writel(DBI_RO_WR_EN
, pci
->dbi_base
+ PCIE20_MISC_CONTROL_1_REG
);
1051 writel(PCIE_CAP_LINK1_VAL
, pci
->dbi_base
+ PCIE20_CAP_LINK_1
);
1053 val
= readl(pci
->dbi_base
+ PCIE20_CAP_LINK_CAPABILITIES
);
1054 val
&= ~PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT
;
1055 writel(val
, pci
->dbi_base
+ PCIE20_CAP_LINK_CAPABILITIES
);
1057 writel(PCIE_CAP_CPL_TIMEOUT_DISABLE
, pci
->dbi_base
+
1058 PCIE20_DEVICE_CONTROL2_STATUS2
);
1063 clk_disable_unprepare(res
->ahb_clk
);
1065 clk_disable_unprepare(res
->axi_s_clk
);
1067 clk_disable_unprepare(res
->axi_m_clk
);
1069 clk_disable_unprepare(res
->iface
);
1072 * Not checking for failure, will anyway return
1073 * the original failure in 'ret'.
1075 for (i
= 0; i
< ARRAY_SIZE(res
->rst
); i
++)
1076 reset_control_assert(res
->rst
[i
]);
1081 static int qcom_pcie_link_up(struct dw_pcie
*pci
)
1083 u16 val
= readw(pci
->dbi_base
+ PCIE20_CAP
+ PCI_EXP_LNKSTA
);
1085 return !!(val
& PCI_EXP_LNKSTA_DLLLA
);
1088 static int qcom_pcie_host_init(struct pcie_port
*pp
)
1090 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
1091 struct qcom_pcie
*pcie
= to_qcom_pcie(pci
);
1094 qcom_ep_reset_assert(pcie
);
1096 ret
= pcie
->ops
->init(pcie
);
1100 ret
= phy_power_on(pcie
->phy
);
1104 if (pcie
->ops
->post_init
) {
1105 ret
= pcie
->ops
->post_init(pcie
);
1107 goto err_disable_phy
;
1110 dw_pcie_setup_rc(pp
);
1112 if (IS_ENABLED(CONFIG_PCI_MSI
))
1113 dw_pcie_msi_init(pp
);
1115 qcom_ep_reset_deassert(pcie
);
1117 ret
= qcom_pcie_establish_link(pcie
);
1123 qcom_ep_reset_assert(pcie
);
1124 if (pcie
->ops
->post_deinit
)
1125 pcie
->ops
->post_deinit(pcie
);
1127 phy_power_off(pcie
->phy
);
1129 pcie
->ops
->deinit(pcie
);
1134 static int qcom_pcie_rd_own_conf(struct pcie_port
*pp
, int where
, int size
,
1137 struct dw_pcie
*pci
= to_dw_pcie_from_pp(pp
);
1139 /* the device class is not reported correctly from the register */
1140 if (where
== PCI_CLASS_REVISION
&& size
== 4) {
1141 *val
= readl(pci
->dbi_base
+ PCI_CLASS_REVISION
);
1142 *val
&= 0xff; /* keep revision id */
1143 *val
|= PCI_CLASS_BRIDGE_PCI
<< 16;
1144 return PCIBIOS_SUCCESSFUL
;
1147 return dw_pcie_read(pci
->dbi_base
+ where
, size
, val
);
1150 static const struct dw_pcie_host_ops qcom_pcie_dw_ops
= {
1151 .host_init
= qcom_pcie_host_init
,
1152 .rd_own_conf
= qcom_pcie_rd_own_conf
,
1155 /* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1156 static const struct qcom_pcie_ops ops_2_1_0
= {
1157 .get_resources
= qcom_pcie_get_resources_2_1_0
,
1158 .init
= qcom_pcie_init_2_1_0
,
1159 .deinit
= qcom_pcie_deinit_2_1_0
,
1160 .ltssm_enable
= qcom_pcie_2_1_0_ltssm_enable
,
1163 /* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1164 static const struct qcom_pcie_ops ops_1_0_0
= {
1165 .get_resources
= qcom_pcie_get_resources_1_0_0
,
1166 .init
= qcom_pcie_init_1_0_0
,
1167 .deinit
= qcom_pcie_deinit_1_0_0
,
1168 .ltssm_enable
= qcom_pcie_2_1_0_ltssm_enable
,
1171 /* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1172 static const struct qcom_pcie_ops ops_2_3_2
= {
1173 .get_resources
= qcom_pcie_get_resources_2_3_2
,
1174 .init
= qcom_pcie_init_2_3_2
,
1175 .post_init
= qcom_pcie_post_init_2_3_2
,
1176 .deinit
= qcom_pcie_deinit_2_3_2
,
1177 .post_deinit
= qcom_pcie_post_deinit_2_3_2
,
1178 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1181 /* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1182 static const struct qcom_pcie_ops ops_2_4_0
= {
1183 .get_resources
= qcom_pcie_get_resources_2_4_0
,
1184 .init
= qcom_pcie_init_2_4_0
,
1185 .deinit
= qcom_pcie_deinit_2_4_0
,
1186 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1189 /* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1190 static const struct qcom_pcie_ops ops_2_3_3
= {
1191 .get_resources
= qcom_pcie_get_resources_2_3_3
,
1192 .init
= qcom_pcie_init_2_3_3
,
1193 .deinit
= qcom_pcie_deinit_2_3_3
,
1194 .ltssm_enable
= qcom_pcie_2_3_2_ltssm_enable
,
1197 static const struct dw_pcie_ops dw_pcie_ops
= {
1198 .link_up
= qcom_pcie_link_up
,
1201 static int qcom_pcie_probe(struct platform_device
*pdev
)
1203 struct device
*dev
= &pdev
->dev
;
1204 struct resource
*res
;
1205 struct pcie_port
*pp
;
1206 struct dw_pcie
*pci
;
1207 struct qcom_pcie
*pcie
;
1210 pcie
= devm_kzalloc(dev
, sizeof(*pcie
), GFP_KERNEL
);
1214 pci
= devm_kzalloc(dev
, sizeof(*pci
), GFP_KERNEL
);
1218 pm_runtime_enable(dev
);
1219 ret
= pm_runtime_get_sync(dev
);
1221 pm_runtime_disable(dev
);
1226 pci
->ops
= &dw_pcie_ops
;
1231 pcie
->ops
= of_device_get_match_data(dev
);
1233 pcie
->reset
= devm_gpiod_get_optional(dev
, "perst", GPIOD_OUT_HIGH
);
1234 if (IS_ERR(pcie
->reset
)) {
1235 ret
= PTR_ERR(pcie
->reset
);
1236 goto err_pm_runtime_put
;
1239 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "parf");
1240 pcie
->parf
= devm_ioremap_resource(dev
, res
);
1241 if (IS_ERR(pcie
->parf
)) {
1242 ret
= PTR_ERR(pcie
->parf
);
1243 goto err_pm_runtime_put
;
1246 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dbi");
1247 pci
->dbi_base
= devm_pci_remap_cfg_resource(dev
, res
);
1248 if (IS_ERR(pci
->dbi_base
)) {
1249 ret
= PTR_ERR(pci
->dbi_base
);
1250 goto err_pm_runtime_put
;
1253 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "elbi");
1254 pcie
->elbi
= devm_ioremap_resource(dev
, res
);
1255 if (IS_ERR(pcie
->elbi
)) {
1256 ret
= PTR_ERR(pcie
->elbi
);
1257 goto err_pm_runtime_put
;
1260 pcie
->phy
= devm_phy_optional_get(dev
, "pciephy");
1261 if (IS_ERR(pcie
->phy
)) {
1262 ret
= PTR_ERR(pcie
->phy
);
1263 goto err_pm_runtime_put
;
1266 ret
= pcie
->ops
->get_resources(pcie
);
1268 goto err_pm_runtime_put
;
1270 pp
->ops
= &qcom_pcie_dw_ops
;
1272 if (IS_ENABLED(CONFIG_PCI_MSI
)) {
1273 pp
->msi_irq
= platform_get_irq_byname(pdev
, "msi");
1274 if (pp
->msi_irq
< 0) {
1276 goto err_pm_runtime_put
;
1280 ret
= phy_init(pcie
->phy
);
1282 pm_runtime_disable(&pdev
->dev
);
1283 goto err_pm_runtime_put
;
1286 platform_set_drvdata(pdev
, pcie
);
1288 ret
= dw_pcie_host_init(pp
);
1290 dev_err(dev
, "cannot initialize host\n");
1291 pm_runtime_disable(&pdev
->dev
);
1292 goto err_pm_runtime_put
;
1298 pm_runtime_put(dev
);
1299 pm_runtime_disable(dev
);
1304 static const struct of_device_id qcom_pcie_match
[] = {
1305 { .compatible
= "qcom,pcie-apq8084", .data
= &ops_1_0_0
},
1306 { .compatible
= "qcom,pcie-ipq8064", .data
= &ops_2_1_0
},
1307 { .compatible
= "qcom,pcie-apq8064", .data
= &ops_2_1_0
},
1308 { .compatible
= "qcom,pcie-msm8996", .data
= &ops_2_3_2
},
1309 { .compatible
= "qcom,pcie-ipq8074", .data
= &ops_2_3_3
},
1310 { .compatible
= "qcom,pcie-ipq4019", .data
= &ops_2_4_0
},
1314 static struct platform_driver qcom_pcie_driver
= {
1315 .probe
= qcom_pcie_probe
,
1317 .name
= "qcom-pcie",
1318 .suppress_bind_attrs
= true,
1319 .of_match_table
= qcom_pcie_match
,
1322 builtin_platform_driver(qcom_pcie_driver
);