Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Qualcomm PCIe root complex driver
4 *
5 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
6 * Copyright 2015 Linaro Limited.
7 *
8 * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
9 */
10
11#include <linux/clk.h>
12#include <linux/crc8.h>
13#include <linux/delay.h>
14#include <linux/gpio/consumer.h>
15#include <linux/interconnect.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/iopoll.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/of_device.h>
22#include <linux/of_gpio.h>
23#include <linux/pci.h>
24#include <linux/pm_runtime.h>
25#include <linux/platform_device.h>
26#include <linux/phy/pcie.h>
27#include <linux/phy/phy.h>
28#include <linux/regulator/consumer.h>
29#include <linux/reset.h>
30#include <linux/slab.h>
31#include <linux/types.h>
32
33#include "../../pci.h"
34#include "pcie-designware.h"
35
36#define PCIE20_PARF_SYS_CTRL 0x00
37#define MST_WAKEUP_EN BIT(13)
38#define SLV_WAKEUP_EN BIT(12)
39#define MSTR_ACLK_CGC_DIS BIT(10)
40#define SLV_ACLK_CGC_DIS BIT(9)
41#define CORE_CLK_CGC_DIS BIT(6)
42#define AUX_PWR_DET BIT(4)
43#define L23_CLK_RMV_DIS BIT(2)
44#define L1_CLK_RMV_DIS BIT(1)
45
46#define PCIE20_PARF_PM_CTRL 0x20
47#define REQ_NOT_ENTR_L1 BIT(5)
48
49#define PCIE20_PARF_PHY_CTRL 0x40
50#define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK GENMASK(20, 16)
51#define PHY_CTRL_PHY_TX0_TERM_OFFSET(x) ((x) << 16)
52
53#define PCIE20_PARF_PHY_REFCLK 0x4C
54#define PHY_REFCLK_SSP_EN BIT(16)
55#define PHY_REFCLK_USE_PAD BIT(12)
56
57#define PCIE20_PARF_DBI_BASE_ADDR 0x168
58#define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
59#define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
60#define AHB_CLK_EN BIT(0)
61#define MSTR_AXI_CLK_EN BIT(1)
62#define BYPASS BIT(4)
63
64#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
65#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
66#define PCIE20_PARF_LTSSM 0x1B0
67#define PCIE20_PARF_SID_OFFSET 0x234
68#define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
69#define PCIE20_PARF_DEVICE_TYPE 0x1000
70#define PCIE20_PARF_BDF_TO_SID_TABLE_N 0x2000
71
72#define PCIE20_ELBI_SYS_CTRL 0x04
73#define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
74
75#define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
76#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
77#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
78#define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
79#define CFG_BRIDGE_SB_INIT BIT(0)
80
81#define PCIE_CAP_SLOT_POWER_LIMIT_VAL FIELD_PREP(PCI_EXP_SLTCAP_SPLV, \
82 250)
83#define PCIE_CAP_SLOT_POWER_LIMIT_SCALE FIELD_PREP(PCI_EXP_SLTCAP_SPLS, \
84 1)
85#define PCIE_CAP_SLOT_VAL (PCI_EXP_SLTCAP_ABP | \
86 PCI_EXP_SLTCAP_PCP | \
87 PCI_EXP_SLTCAP_MRLSP | \
88 PCI_EXP_SLTCAP_AIP | \
89 PCI_EXP_SLTCAP_PIP | \
90 PCI_EXP_SLTCAP_HPS | \
91 PCI_EXP_SLTCAP_HPC | \
92 PCI_EXP_SLTCAP_EIP | \
93 PCIE_CAP_SLOT_POWER_LIMIT_VAL | \
94 PCIE_CAP_SLOT_POWER_LIMIT_SCALE)
95
96#define PCIE20_PARF_Q2A_FLUSH 0x1AC
97
98#define PCIE20_MISC_CONTROL_1_REG 0x8BC
99#define DBI_RO_WR_EN 1
100
101#define PERST_DELAY_US 1000
102/* PARF registers */
103#define PCIE20_PARF_PCS_DEEMPH 0x34
104#define PCS_DEEMPH_TX_DEEMPH_GEN1(x) ((x) << 16)
105#define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x) ((x) << 8)
106#define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x) ((x) << 0)
107
108#define PCIE20_PARF_PCS_SWING 0x38
109#define PCS_SWING_TX_SWING_FULL(x) ((x) << 8)
110#define PCS_SWING_TX_SWING_LOW(x) ((x) << 0)
111
112#define PCIE20_PARF_CONFIG_BITS 0x50
113#define PHY_RX0_EQ(x) ((x) << 24)
114
115#define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
116#define SLV_ADDR_SPACE_SZ 0x10000000
117
118#define PCIE20_LNK_CONTROL2_LINK_STATUS2 0xa0
119
120#define DEVICE_TYPE_RC 0x4
121
122#define QCOM_PCIE_2_1_0_MAX_SUPPLY 3
123#define QCOM_PCIE_2_1_0_MAX_CLOCKS 5
124
125#define QCOM_PCIE_CRC8_POLYNOMIAL (BIT(2) | BIT(1) | BIT(0))
126
127struct qcom_pcie_resources_2_1_0 {
128 struct clk_bulk_data clks[QCOM_PCIE_2_1_0_MAX_CLOCKS];
129 struct reset_control *pci_reset;
130 struct reset_control *axi_reset;
131 struct reset_control *ahb_reset;
132 struct reset_control *por_reset;
133 struct reset_control *phy_reset;
134 struct reset_control *ext_reset;
135 struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
136};
137
138struct qcom_pcie_resources_1_0_0 {
139 struct clk *iface;
140 struct clk *aux;
141 struct clk *master_bus;
142 struct clk *slave_bus;
143 struct reset_control *core;
144 struct regulator *vdda;
145};
146
147#define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
148struct qcom_pcie_resources_2_3_2 {
149 struct clk *aux_clk;
150 struct clk *master_clk;
151 struct clk *slave_clk;
152 struct clk *cfg_clk;
153 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
154};
155
156#define QCOM_PCIE_2_4_0_MAX_CLOCKS 4
157struct qcom_pcie_resources_2_4_0 {
158 struct clk_bulk_data clks[QCOM_PCIE_2_4_0_MAX_CLOCKS];
159 int num_clks;
160 struct reset_control *axi_m_reset;
161 struct reset_control *axi_s_reset;
162 struct reset_control *pipe_reset;
163 struct reset_control *axi_m_vmid_reset;
164 struct reset_control *axi_s_xpu_reset;
165 struct reset_control *parf_reset;
166 struct reset_control *phy_reset;
167 struct reset_control *axi_m_sticky_reset;
168 struct reset_control *pipe_sticky_reset;
169 struct reset_control *pwr_reset;
170 struct reset_control *ahb_reset;
171 struct reset_control *phy_ahb_reset;
172};
173
174struct qcom_pcie_resources_2_3_3 {
175 struct clk *iface;
176 struct clk *axi_m_clk;
177 struct clk *axi_s_clk;
178 struct clk *ahb_clk;
179 struct clk *aux_clk;
180 struct reset_control *rst[7];
181};
182
183/* 6 clocks typically, 7 for sm8250 */
184struct qcom_pcie_resources_2_7_0 {
185 struct clk_bulk_data clks[12];
186 int num_clks;
187 struct regulator_bulk_data supplies[2];
188 struct reset_control *pci_reset;
189};
190
191struct qcom_pcie_resources_2_9_0 {
192 struct clk_bulk_data clks[5];
193 struct reset_control *rst;
194};
195
196union qcom_pcie_resources {
197 struct qcom_pcie_resources_1_0_0 v1_0_0;
198 struct qcom_pcie_resources_2_1_0 v2_1_0;
199 struct qcom_pcie_resources_2_3_2 v2_3_2;
200 struct qcom_pcie_resources_2_3_3 v2_3_3;
201 struct qcom_pcie_resources_2_4_0 v2_4_0;
202 struct qcom_pcie_resources_2_7_0 v2_7_0;
203 struct qcom_pcie_resources_2_9_0 v2_9_0;
204};
205
206struct qcom_pcie;
207
208struct qcom_pcie_ops {
209 int (*get_resources)(struct qcom_pcie *pcie);
210 int (*init)(struct qcom_pcie *pcie);
211 int (*post_init)(struct qcom_pcie *pcie);
212 void (*deinit)(struct qcom_pcie *pcie);
213 void (*ltssm_enable)(struct qcom_pcie *pcie);
214 int (*config_sid)(struct qcom_pcie *pcie);
215};
216
217struct qcom_pcie_cfg {
218 const struct qcom_pcie_ops *ops;
219};
220
221struct qcom_pcie {
222 struct dw_pcie *pci;
223 void __iomem *parf; /* DT parf */
224 void __iomem *elbi; /* DT elbi */
225 union qcom_pcie_resources res;
226 struct phy *phy;
227 struct gpio_desc *reset;
228 struct icc_path *icc_mem;
229 const struct qcom_pcie_cfg *cfg;
230};
231
232#define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
233
234static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
235{
236 gpiod_set_value_cansleep(pcie->reset, 1);
237 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
238}
239
240static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
241{
242 /* Ensure that PERST has been asserted for at least 100 ms */
243 msleep(100);
244 gpiod_set_value_cansleep(pcie->reset, 0);
245 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
246}
247
248static int qcom_pcie_start_link(struct dw_pcie *pci)
249{
250 struct qcom_pcie *pcie = to_qcom_pcie(pci);
251
252 /* Enable Link Training state machine */
253 if (pcie->cfg->ops->ltssm_enable)
254 pcie->cfg->ops->ltssm_enable(pcie);
255
256 return 0;
257}
258
259static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
260{
261 u32 val;
262
263 /* enable link training */
264 val = readl(pcie->elbi + PCIE20_ELBI_SYS_CTRL);
265 val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
266 writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
267}
268
269static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
270{
271 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
272 struct dw_pcie *pci = pcie->pci;
273 struct device *dev = pci->dev;
274 int ret;
275
276 res->supplies[0].supply = "vdda";
277 res->supplies[1].supply = "vdda_phy";
278 res->supplies[2].supply = "vdda_refclk";
279 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
280 res->supplies);
281 if (ret)
282 return ret;
283
284 res->clks[0].id = "iface";
285 res->clks[1].id = "core";
286 res->clks[2].id = "phy";
287 res->clks[3].id = "aux";
288 res->clks[4].id = "ref";
289
290 /* iface, core, phy are required */
291 ret = devm_clk_bulk_get(dev, 3, res->clks);
292 if (ret < 0)
293 return ret;
294
295 /* aux, ref are optional */
296 ret = devm_clk_bulk_get_optional(dev, 2, res->clks + 3);
297 if (ret < 0)
298 return ret;
299
300 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
301 if (IS_ERR(res->pci_reset))
302 return PTR_ERR(res->pci_reset);
303
304 res->axi_reset = devm_reset_control_get_exclusive(dev, "axi");
305 if (IS_ERR(res->axi_reset))
306 return PTR_ERR(res->axi_reset);
307
308 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
309 if (IS_ERR(res->ahb_reset))
310 return PTR_ERR(res->ahb_reset);
311
312 res->por_reset = devm_reset_control_get_exclusive(dev, "por");
313 if (IS_ERR(res->por_reset))
314 return PTR_ERR(res->por_reset);
315
316 res->ext_reset = devm_reset_control_get_optional_exclusive(dev, "ext");
317 if (IS_ERR(res->ext_reset))
318 return PTR_ERR(res->ext_reset);
319
320 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
321 return PTR_ERR_OR_ZERO(res->phy_reset);
322}
323
324static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
325{
326 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
327
328 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
329 reset_control_assert(res->pci_reset);
330 reset_control_assert(res->axi_reset);
331 reset_control_assert(res->ahb_reset);
332 reset_control_assert(res->por_reset);
333 reset_control_assert(res->ext_reset);
334 reset_control_assert(res->phy_reset);
335
336 writel(1, pcie->parf + PCIE20_PARF_PHY_CTRL);
337
338 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
339}
340
341static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
342{
343 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
344 struct dw_pcie *pci = pcie->pci;
345 struct device *dev = pci->dev;
346 int ret;
347
348 /* reset the PCIe interface as uboot can leave it undefined state */
349 reset_control_assert(res->pci_reset);
350 reset_control_assert(res->axi_reset);
351 reset_control_assert(res->ahb_reset);
352 reset_control_assert(res->por_reset);
353 reset_control_assert(res->ext_reset);
354 reset_control_assert(res->phy_reset);
355
356 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
357 if (ret < 0) {
358 dev_err(dev, "cannot enable regulators\n");
359 return ret;
360 }
361
362 ret = reset_control_deassert(res->ahb_reset);
363 if (ret) {
364 dev_err(dev, "cannot deassert ahb reset\n");
365 goto err_deassert_ahb;
366 }
367
368 ret = reset_control_deassert(res->ext_reset);
369 if (ret) {
370 dev_err(dev, "cannot deassert ext reset\n");
371 goto err_deassert_ext;
372 }
373
374 ret = reset_control_deassert(res->phy_reset);
375 if (ret) {
376 dev_err(dev, "cannot deassert phy reset\n");
377 goto err_deassert_phy;
378 }
379
380 ret = reset_control_deassert(res->pci_reset);
381 if (ret) {
382 dev_err(dev, "cannot deassert pci reset\n");
383 goto err_deassert_pci;
384 }
385
386 ret = reset_control_deassert(res->por_reset);
387 if (ret) {
388 dev_err(dev, "cannot deassert por reset\n");
389 goto err_deassert_por;
390 }
391
392 ret = reset_control_deassert(res->axi_reset);
393 if (ret) {
394 dev_err(dev, "cannot deassert axi reset\n");
395 goto err_deassert_axi;
396 }
397
398 return 0;
399
400err_deassert_axi:
401 reset_control_assert(res->por_reset);
402err_deassert_por:
403 reset_control_assert(res->pci_reset);
404err_deassert_pci:
405 reset_control_assert(res->phy_reset);
406err_deassert_phy:
407 reset_control_assert(res->ext_reset);
408err_deassert_ext:
409 reset_control_assert(res->ahb_reset);
410err_deassert_ahb:
411 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
412
413 return ret;
414}
415
416static int qcom_pcie_post_init_2_1_0(struct qcom_pcie *pcie)
417{
418 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
419 struct dw_pcie *pci = pcie->pci;
420 struct device *dev = pci->dev;
421 struct device_node *node = dev->of_node;
422 u32 val;
423 int ret;
424
425 /* enable PCIe clocks and resets */
426 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
427 val &= ~BIT(0);
428 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
429
430 ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
431 if (ret)
432 return ret;
433
434 if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
435 of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
436 writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
437 PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
438 PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
439 pcie->parf + PCIE20_PARF_PCS_DEEMPH);
440 writel(PCS_SWING_TX_SWING_FULL(120) |
441 PCS_SWING_TX_SWING_LOW(120),
442 pcie->parf + PCIE20_PARF_PCS_SWING);
443 writel(PHY_RX0_EQ(4), pcie->parf + PCIE20_PARF_CONFIG_BITS);
444 }
445
446 if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
447 /* set TX termination offset */
448 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
449 val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
450 val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
451 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
452 }
453
454 /* enable external reference clock */
455 val = readl(pcie->parf + PCIE20_PARF_PHY_REFCLK);
456 /* USE_PAD is required only for ipq806x */
457 if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
458 val &= ~PHY_REFCLK_USE_PAD;
459 val |= PHY_REFCLK_SSP_EN;
460 writel(val, pcie->parf + PCIE20_PARF_PHY_REFCLK);
461
462 /* wait for clock acquisition */
463 usleep_range(1000, 1500);
464
465 /* Set the Max TLP size to 2K, instead of using default of 4K */
466 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
467 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL0);
468 writel(CFG_BRIDGE_SB_INIT,
469 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL1);
470
471 return 0;
472}
473
474static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
475{
476 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
477 struct dw_pcie *pci = pcie->pci;
478 struct device *dev = pci->dev;
479
480 res->vdda = devm_regulator_get(dev, "vdda");
481 if (IS_ERR(res->vdda))
482 return PTR_ERR(res->vdda);
483
484 res->iface = devm_clk_get(dev, "iface");
485 if (IS_ERR(res->iface))
486 return PTR_ERR(res->iface);
487
488 res->aux = devm_clk_get(dev, "aux");
489 if (IS_ERR(res->aux))
490 return PTR_ERR(res->aux);
491
492 res->master_bus = devm_clk_get(dev, "master_bus");
493 if (IS_ERR(res->master_bus))
494 return PTR_ERR(res->master_bus);
495
496 res->slave_bus = devm_clk_get(dev, "slave_bus");
497 if (IS_ERR(res->slave_bus))
498 return PTR_ERR(res->slave_bus);
499
500 res->core = devm_reset_control_get_exclusive(dev, "core");
501 return PTR_ERR_OR_ZERO(res->core);
502}
503
504static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
505{
506 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
507
508 reset_control_assert(res->core);
509 clk_disable_unprepare(res->slave_bus);
510 clk_disable_unprepare(res->master_bus);
511 clk_disable_unprepare(res->iface);
512 clk_disable_unprepare(res->aux);
513 regulator_disable(res->vdda);
514}
515
516static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
517{
518 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
519 struct dw_pcie *pci = pcie->pci;
520 struct device *dev = pci->dev;
521 int ret;
522
523 ret = reset_control_deassert(res->core);
524 if (ret) {
525 dev_err(dev, "cannot deassert core reset\n");
526 return ret;
527 }
528
529 ret = clk_prepare_enable(res->aux);
530 if (ret) {
531 dev_err(dev, "cannot prepare/enable aux clock\n");
532 goto err_res;
533 }
534
535 ret = clk_prepare_enable(res->iface);
536 if (ret) {
537 dev_err(dev, "cannot prepare/enable iface clock\n");
538 goto err_aux;
539 }
540
541 ret = clk_prepare_enable(res->master_bus);
542 if (ret) {
543 dev_err(dev, "cannot prepare/enable master_bus clock\n");
544 goto err_iface;
545 }
546
547 ret = clk_prepare_enable(res->slave_bus);
548 if (ret) {
549 dev_err(dev, "cannot prepare/enable slave_bus clock\n");
550 goto err_master;
551 }
552
553 ret = regulator_enable(res->vdda);
554 if (ret) {
555 dev_err(dev, "cannot enable vdda regulator\n");
556 goto err_slave;
557 }
558
559 return 0;
560err_slave:
561 clk_disable_unprepare(res->slave_bus);
562err_master:
563 clk_disable_unprepare(res->master_bus);
564err_iface:
565 clk_disable_unprepare(res->iface);
566err_aux:
567 clk_disable_unprepare(res->aux);
568err_res:
569 reset_control_assert(res->core);
570
571 return ret;
572}
573
574static int qcom_pcie_post_init_1_0_0(struct qcom_pcie *pcie)
575{
576 /* change DBI base address */
577 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
578
579 if (IS_ENABLED(CONFIG_PCI_MSI)) {
580 u32 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
581
582 val |= BIT(31);
583 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
584 }
585
586 return 0;
587}
588
589static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
590{
591 u32 val;
592
593 /* enable link training */
594 val = readl(pcie->parf + PCIE20_PARF_LTSSM);
595 val |= BIT(8);
596 writel(val, pcie->parf + PCIE20_PARF_LTSSM);
597}
598
599static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
600{
601 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
602 struct dw_pcie *pci = pcie->pci;
603 struct device *dev = pci->dev;
604 int ret;
605
606 res->supplies[0].supply = "vdda";
607 res->supplies[1].supply = "vddpe-3v3";
608 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
609 res->supplies);
610 if (ret)
611 return ret;
612
613 res->aux_clk = devm_clk_get(dev, "aux");
614 if (IS_ERR(res->aux_clk))
615 return PTR_ERR(res->aux_clk);
616
617 res->cfg_clk = devm_clk_get(dev, "cfg");
618 if (IS_ERR(res->cfg_clk))
619 return PTR_ERR(res->cfg_clk);
620
621 res->master_clk = devm_clk_get(dev, "bus_master");
622 if (IS_ERR(res->master_clk))
623 return PTR_ERR(res->master_clk);
624
625 res->slave_clk = devm_clk_get(dev, "bus_slave");
626 if (IS_ERR(res->slave_clk))
627 return PTR_ERR(res->slave_clk);
628
629 return 0;
630}
631
632static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
633{
634 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
635
636 clk_disable_unprepare(res->slave_clk);
637 clk_disable_unprepare(res->master_clk);
638 clk_disable_unprepare(res->cfg_clk);
639 clk_disable_unprepare(res->aux_clk);
640
641 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
642}
643
644static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
645{
646 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
647 struct dw_pcie *pci = pcie->pci;
648 struct device *dev = pci->dev;
649 int ret;
650
651 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
652 if (ret < 0) {
653 dev_err(dev, "cannot enable regulators\n");
654 return ret;
655 }
656
657 ret = clk_prepare_enable(res->aux_clk);
658 if (ret) {
659 dev_err(dev, "cannot prepare/enable aux clock\n");
660 goto err_aux_clk;
661 }
662
663 ret = clk_prepare_enable(res->cfg_clk);
664 if (ret) {
665 dev_err(dev, "cannot prepare/enable cfg clock\n");
666 goto err_cfg_clk;
667 }
668
669 ret = clk_prepare_enable(res->master_clk);
670 if (ret) {
671 dev_err(dev, "cannot prepare/enable master clock\n");
672 goto err_master_clk;
673 }
674
675 ret = clk_prepare_enable(res->slave_clk);
676 if (ret) {
677 dev_err(dev, "cannot prepare/enable slave clock\n");
678 goto err_slave_clk;
679 }
680
681 return 0;
682
683err_slave_clk:
684 clk_disable_unprepare(res->master_clk);
685err_master_clk:
686 clk_disable_unprepare(res->cfg_clk);
687err_cfg_clk:
688 clk_disable_unprepare(res->aux_clk);
689
690err_aux_clk:
691 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
692
693 return ret;
694}
695
696static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
697{
698 u32 val;
699
700 /* enable PCIe clocks and resets */
701 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
702 val &= ~BIT(0);
703 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
704
705 /* change DBI base address */
706 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
707
708 /* MAC PHY_POWERDOWN MUX DISABLE */
709 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
710 val &= ~BIT(29);
711 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
712
713 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
714 val |= BIT(4);
715 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
716
717 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
718 val |= BIT(31);
719 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
720
721 return 0;
722}
723
724static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
725{
726 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
727 struct dw_pcie *pci = pcie->pci;
728 struct device *dev = pci->dev;
729 bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
730 int ret;
731
732 res->clks[0].id = "aux";
733 res->clks[1].id = "master_bus";
734 res->clks[2].id = "slave_bus";
735 res->clks[3].id = "iface";
736
737 /* qcom,pcie-ipq4019 is defined without "iface" */
738 res->num_clks = is_ipq ? 3 : 4;
739
740 ret = devm_clk_bulk_get(dev, res->num_clks, res->clks);
741 if (ret < 0)
742 return ret;
743
744 res->axi_m_reset = devm_reset_control_get_exclusive(dev, "axi_m");
745 if (IS_ERR(res->axi_m_reset))
746 return PTR_ERR(res->axi_m_reset);
747
748 res->axi_s_reset = devm_reset_control_get_exclusive(dev, "axi_s");
749 if (IS_ERR(res->axi_s_reset))
750 return PTR_ERR(res->axi_s_reset);
751
752 if (is_ipq) {
753 /*
754 * These resources relates to the PHY or are secure clocks, but
755 * are controlled here for IPQ4019
756 */
757 res->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
758 if (IS_ERR(res->pipe_reset))
759 return PTR_ERR(res->pipe_reset);
760
761 res->axi_m_vmid_reset = devm_reset_control_get_exclusive(dev,
762 "axi_m_vmid");
763 if (IS_ERR(res->axi_m_vmid_reset))
764 return PTR_ERR(res->axi_m_vmid_reset);
765
766 res->axi_s_xpu_reset = devm_reset_control_get_exclusive(dev,
767 "axi_s_xpu");
768 if (IS_ERR(res->axi_s_xpu_reset))
769 return PTR_ERR(res->axi_s_xpu_reset);
770
771 res->parf_reset = devm_reset_control_get_exclusive(dev, "parf");
772 if (IS_ERR(res->parf_reset))
773 return PTR_ERR(res->parf_reset);
774
775 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
776 if (IS_ERR(res->phy_reset))
777 return PTR_ERR(res->phy_reset);
778 }
779
780 res->axi_m_sticky_reset = devm_reset_control_get_exclusive(dev,
781 "axi_m_sticky");
782 if (IS_ERR(res->axi_m_sticky_reset))
783 return PTR_ERR(res->axi_m_sticky_reset);
784
785 res->pipe_sticky_reset = devm_reset_control_get_exclusive(dev,
786 "pipe_sticky");
787 if (IS_ERR(res->pipe_sticky_reset))
788 return PTR_ERR(res->pipe_sticky_reset);
789
790 res->pwr_reset = devm_reset_control_get_exclusive(dev, "pwr");
791 if (IS_ERR(res->pwr_reset))
792 return PTR_ERR(res->pwr_reset);
793
794 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
795 if (IS_ERR(res->ahb_reset))
796 return PTR_ERR(res->ahb_reset);
797
798 if (is_ipq) {
799 res->phy_ahb_reset = devm_reset_control_get_exclusive(dev, "phy_ahb");
800 if (IS_ERR(res->phy_ahb_reset))
801 return PTR_ERR(res->phy_ahb_reset);
802 }
803
804 return 0;
805}
806
807static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
808{
809 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
810
811 reset_control_assert(res->axi_m_reset);
812 reset_control_assert(res->axi_s_reset);
813 reset_control_assert(res->pipe_reset);
814 reset_control_assert(res->pipe_sticky_reset);
815 reset_control_assert(res->phy_reset);
816 reset_control_assert(res->phy_ahb_reset);
817 reset_control_assert(res->axi_m_sticky_reset);
818 reset_control_assert(res->pwr_reset);
819 reset_control_assert(res->ahb_reset);
820 clk_bulk_disable_unprepare(res->num_clks, res->clks);
821}
822
823static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
824{
825 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
826 struct dw_pcie *pci = pcie->pci;
827 struct device *dev = pci->dev;
828 int ret;
829
830 ret = reset_control_assert(res->axi_m_reset);
831 if (ret) {
832 dev_err(dev, "cannot assert axi master reset\n");
833 return ret;
834 }
835
836 ret = reset_control_assert(res->axi_s_reset);
837 if (ret) {
838 dev_err(dev, "cannot assert axi slave reset\n");
839 return ret;
840 }
841
842 usleep_range(10000, 12000);
843
844 ret = reset_control_assert(res->pipe_reset);
845 if (ret) {
846 dev_err(dev, "cannot assert pipe reset\n");
847 return ret;
848 }
849
850 ret = reset_control_assert(res->pipe_sticky_reset);
851 if (ret) {
852 dev_err(dev, "cannot assert pipe sticky reset\n");
853 return ret;
854 }
855
856 ret = reset_control_assert(res->phy_reset);
857 if (ret) {
858 dev_err(dev, "cannot assert phy reset\n");
859 return ret;
860 }
861
862 ret = reset_control_assert(res->phy_ahb_reset);
863 if (ret) {
864 dev_err(dev, "cannot assert phy ahb reset\n");
865 return ret;
866 }
867
868 usleep_range(10000, 12000);
869
870 ret = reset_control_assert(res->axi_m_sticky_reset);
871 if (ret) {
872 dev_err(dev, "cannot assert axi master sticky reset\n");
873 return ret;
874 }
875
876 ret = reset_control_assert(res->pwr_reset);
877 if (ret) {
878 dev_err(dev, "cannot assert power reset\n");
879 return ret;
880 }
881
882 ret = reset_control_assert(res->ahb_reset);
883 if (ret) {
884 dev_err(dev, "cannot assert ahb reset\n");
885 return ret;
886 }
887
888 usleep_range(10000, 12000);
889
890 ret = reset_control_deassert(res->phy_ahb_reset);
891 if (ret) {
892 dev_err(dev, "cannot deassert phy ahb reset\n");
893 return ret;
894 }
895
896 ret = reset_control_deassert(res->phy_reset);
897 if (ret) {
898 dev_err(dev, "cannot deassert phy reset\n");
899 goto err_rst_phy;
900 }
901
902 ret = reset_control_deassert(res->pipe_reset);
903 if (ret) {
904 dev_err(dev, "cannot deassert pipe reset\n");
905 goto err_rst_pipe;
906 }
907
908 ret = reset_control_deassert(res->pipe_sticky_reset);
909 if (ret) {
910 dev_err(dev, "cannot deassert pipe sticky reset\n");
911 goto err_rst_pipe_sticky;
912 }
913
914 usleep_range(10000, 12000);
915
916 ret = reset_control_deassert(res->axi_m_reset);
917 if (ret) {
918 dev_err(dev, "cannot deassert axi master reset\n");
919 goto err_rst_axi_m;
920 }
921
922 ret = reset_control_deassert(res->axi_m_sticky_reset);
923 if (ret) {
924 dev_err(dev, "cannot deassert axi master sticky reset\n");
925 goto err_rst_axi_m_sticky;
926 }
927
928 ret = reset_control_deassert(res->axi_s_reset);
929 if (ret) {
930 dev_err(dev, "cannot deassert axi slave reset\n");
931 goto err_rst_axi_s;
932 }
933
934 ret = reset_control_deassert(res->pwr_reset);
935 if (ret) {
936 dev_err(dev, "cannot deassert power reset\n");
937 goto err_rst_pwr;
938 }
939
940 ret = reset_control_deassert(res->ahb_reset);
941 if (ret) {
942 dev_err(dev, "cannot deassert ahb reset\n");
943 goto err_rst_ahb;
944 }
945
946 usleep_range(10000, 12000);
947
948 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
949 if (ret)
950 goto err_clks;
951
952 return 0;
953
954err_clks:
955 reset_control_assert(res->ahb_reset);
956err_rst_ahb:
957 reset_control_assert(res->pwr_reset);
958err_rst_pwr:
959 reset_control_assert(res->axi_s_reset);
960err_rst_axi_s:
961 reset_control_assert(res->axi_m_sticky_reset);
962err_rst_axi_m_sticky:
963 reset_control_assert(res->axi_m_reset);
964err_rst_axi_m:
965 reset_control_assert(res->pipe_sticky_reset);
966err_rst_pipe_sticky:
967 reset_control_assert(res->pipe_reset);
968err_rst_pipe:
969 reset_control_assert(res->phy_reset);
970err_rst_phy:
971 reset_control_assert(res->phy_ahb_reset);
972 return ret;
973}
974
975static int qcom_pcie_post_init_2_4_0(struct qcom_pcie *pcie)
976{
977 u32 val;
978
979 /* enable PCIe clocks and resets */
980 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
981 val &= ~BIT(0);
982 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
983
984 /* change DBI base address */
985 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
986
987 /* MAC PHY_POWERDOWN MUX DISABLE */
988 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
989 val &= ~BIT(29);
990 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
991
992 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
993 val |= BIT(4);
994 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
995
996 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
997 val |= BIT(31);
998 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
999
1000 return 0;
1001}
1002
1003static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
1004{
1005 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1006 struct dw_pcie *pci = pcie->pci;
1007 struct device *dev = pci->dev;
1008 int i;
1009 const char *rst_names[] = { "axi_m", "axi_s", "pipe",
1010 "axi_m_sticky", "sticky",
1011 "ahb", "sleep", };
1012
1013 res->iface = devm_clk_get(dev, "iface");
1014 if (IS_ERR(res->iface))
1015 return PTR_ERR(res->iface);
1016
1017 res->axi_m_clk = devm_clk_get(dev, "axi_m");
1018 if (IS_ERR(res->axi_m_clk))
1019 return PTR_ERR(res->axi_m_clk);
1020
1021 res->axi_s_clk = devm_clk_get(dev, "axi_s");
1022 if (IS_ERR(res->axi_s_clk))
1023 return PTR_ERR(res->axi_s_clk);
1024
1025 res->ahb_clk = devm_clk_get(dev, "ahb");
1026 if (IS_ERR(res->ahb_clk))
1027 return PTR_ERR(res->ahb_clk);
1028
1029 res->aux_clk = devm_clk_get(dev, "aux");
1030 if (IS_ERR(res->aux_clk))
1031 return PTR_ERR(res->aux_clk);
1032
1033 for (i = 0; i < ARRAY_SIZE(rst_names); i++) {
1034 res->rst[i] = devm_reset_control_get(dev, rst_names[i]);
1035 if (IS_ERR(res->rst[i]))
1036 return PTR_ERR(res->rst[i]);
1037 }
1038
1039 return 0;
1040}
1041
1042static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
1043{
1044 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1045
1046 clk_disable_unprepare(res->iface);
1047 clk_disable_unprepare(res->axi_m_clk);
1048 clk_disable_unprepare(res->axi_s_clk);
1049 clk_disable_unprepare(res->ahb_clk);
1050 clk_disable_unprepare(res->aux_clk);
1051}
1052
1053static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
1054{
1055 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1056 struct dw_pcie *pci = pcie->pci;
1057 struct device *dev = pci->dev;
1058 int i, ret;
1059
1060 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1061 ret = reset_control_assert(res->rst[i]);
1062 if (ret) {
1063 dev_err(dev, "reset #%d assert failed (%d)\n", i, ret);
1064 return ret;
1065 }
1066 }
1067
1068 usleep_range(2000, 2500);
1069
1070 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1071 ret = reset_control_deassert(res->rst[i]);
1072 if (ret) {
1073 dev_err(dev, "reset #%d deassert failed (%d)\n", i,
1074 ret);
1075 return ret;
1076 }
1077 }
1078
1079 /*
1080 * Don't have a way to see if the reset has completed.
1081 * Wait for some time.
1082 */
1083 usleep_range(2000, 2500);
1084
1085 ret = clk_prepare_enable(res->iface);
1086 if (ret) {
1087 dev_err(dev, "cannot prepare/enable core clock\n");
1088 goto err_clk_iface;
1089 }
1090
1091 ret = clk_prepare_enable(res->axi_m_clk);
1092 if (ret) {
1093 dev_err(dev, "cannot prepare/enable core clock\n");
1094 goto err_clk_axi_m;
1095 }
1096
1097 ret = clk_prepare_enable(res->axi_s_clk);
1098 if (ret) {
1099 dev_err(dev, "cannot prepare/enable axi slave clock\n");
1100 goto err_clk_axi_s;
1101 }
1102
1103 ret = clk_prepare_enable(res->ahb_clk);
1104 if (ret) {
1105 dev_err(dev, "cannot prepare/enable ahb clock\n");
1106 goto err_clk_ahb;
1107 }
1108
1109 ret = clk_prepare_enable(res->aux_clk);
1110 if (ret) {
1111 dev_err(dev, "cannot prepare/enable aux clock\n");
1112 goto err_clk_aux;
1113 }
1114
1115 return 0;
1116
1117err_clk_aux:
1118 clk_disable_unprepare(res->ahb_clk);
1119err_clk_ahb:
1120 clk_disable_unprepare(res->axi_s_clk);
1121err_clk_axi_s:
1122 clk_disable_unprepare(res->axi_m_clk);
1123err_clk_axi_m:
1124 clk_disable_unprepare(res->iface);
1125err_clk_iface:
1126 /*
1127 * Not checking for failure, will anyway return
1128 * the original failure in 'ret'.
1129 */
1130 for (i = 0; i < ARRAY_SIZE(res->rst); i++)
1131 reset_control_assert(res->rst[i]);
1132
1133 return ret;
1134}
1135
1136static int qcom_pcie_post_init_2_3_3(struct qcom_pcie *pcie)
1137{
1138 struct dw_pcie *pci = pcie->pci;
1139 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1140 u32 val;
1141
1142 writel(SLV_ADDR_SPACE_SZ,
1143 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1144
1145 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1146 val &= ~BIT(0);
1147 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1148
1149 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1150
1151 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
1152 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1153 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1154 pcie->parf + PCIE20_PARF_SYS_CTRL);
1155 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1156
1157 writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
1158 writel(DBI_RO_WR_EN, pci->dbi_base + PCIE20_MISC_CONTROL_1_REG);
1159 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1160
1161 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1162 val &= ~PCI_EXP_LNKCAP_ASPMS;
1163 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1164
1165 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1166 PCI_EXP_DEVCTL2);
1167
1168 return 0;
1169}
1170
1171static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
1172{
1173 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1174 struct dw_pcie *pci = pcie->pci;
1175 struct device *dev = pci->dev;
1176 unsigned int num_clks, num_opt_clks;
1177 unsigned int idx;
1178 int ret;
1179
1180 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
1181 if (IS_ERR(res->pci_reset))
1182 return PTR_ERR(res->pci_reset);
1183
1184 res->supplies[0].supply = "vdda";
1185 res->supplies[1].supply = "vddpe-3v3";
1186 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
1187 res->supplies);
1188 if (ret)
1189 return ret;
1190
1191 idx = 0;
1192 res->clks[idx++].id = "aux";
1193 res->clks[idx++].id = "cfg";
1194 res->clks[idx++].id = "bus_master";
1195 res->clks[idx++].id = "bus_slave";
1196 res->clks[idx++].id = "slave_q2a";
1197
1198 num_clks = idx;
1199
1200 ret = devm_clk_bulk_get(dev, num_clks, res->clks);
1201 if (ret < 0)
1202 return ret;
1203
1204 res->clks[idx++].id = "tbu";
1205 res->clks[idx++].id = "ddrss_sf_tbu";
1206 res->clks[idx++].id = "aggre0";
1207 res->clks[idx++].id = "aggre1";
1208 res->clks[idx++].id = "noc_aggr_4";
1209 res->clks[idx++].id = "noc_aggr_south_sf";
1210 res->clks[idx++].id = "cnoc_qx";
1211
1212 num_opt_clks = idx - num_clks;
1213 res->num_clks = idx;
1214
1215 ret = devm_clk_bulk_get_optional(dev, num_opt_clks, res->clks + num_clks);
1216 if (ret < 0)
1217 return ret;
1218
1219 return 0;
1220}
1221
1222static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
1223{
1224 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1225 struct dw_pcie *pci = pcie->pci;
1226 struct device *dev = pci->dev;
1227 u32 val;
1228 int ret;
1229
1230 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
1231 if (ret < 0) {
1232 dev_err(dev, "cannot enable regulators\n");
1233 return ret;
1234 }
1235
1236 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
1237 if (ret < 0)
1238 goto err_disable_regulators;
1239
1240 ret = reset_control_assert(res->pci_reset);
1241 if (ret < 0) {
1242 dev_err(dev, "cannot assert pci reset\n");
1243 goto err_disable_clocks;
1244 }
1245
1246 usleep_range(1000, 1500);
1247
1248 ret = reset_control_deassert(res->pci_reset);
1249 if (ret < 0) {
1250 dev_err(dev, "cannot deassert pci reset\n");
1251 goto err_disable_clocks;
1252 }
1253
1254 /* Wait for reset to complete, required on SM8450 */
1255 usleep_range(1000, 1500);
1256
1257 /* configure PCIe to RC mode */
1258 writel(DEVICE_TYPE_RC, pcie->parf + PCIE20_PARF_DEVICE_TYPE);
1259
1260 /* enable PCIe clocks and resets */
1261 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1262 val &= ~BIT(0);
1263 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1264
1265 /* change DBI base address */
1266 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1267
1268 /* MAC PHY_POWERDOWN MUX DISABLE */
1269 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
1270 val &= ~BIT(29);
1271 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
1272
1273 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1274 val |= BIT(4);
1275 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1276
1277 /* Enable L1 and L1SS */
1278 val = readl(pcie->parf + PCIE20_PARF_PM_CTRL);
1279 val &= ~REQ_NOT_ENTR_L1;
1280 writel(val, pcie->parf + PCIE20_PARF_PM_CTRL);
1281
1282 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1283 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
1284 val |= BIT(31);
1285 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
1286 }
1287
1288 return 0;
1289err_disable_clocks:
1290 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1291err_disable_regulators:
1292 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1293
1294 return ret;
1295}
1296
1297static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
1298{
1299 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1300
1301 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1302
1303 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1304}
1305
1306static int qcom_pcie_get_resources_2_9_0(struct qcom_pcie *pcie)
1307{
1308 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1309 struct dw_pcie *pci = pcie->pci;
1310 struct device *dev = pci->dev;
1311 int ret;
1312
1313 res->clks[0].id = "iface";
1314 res->clks[1].id = "axi_m";
1315 res->clks[2].id = "axi_s";
1316 res->clks[3].id = "axi_bridge";
1317 res->clks[4].id = "rchng";
1318
1319 ret = devm_clk_bulk_get(dev, ARRAY_SIZE(res->clks), res->clks);
1320 if (ret < 0)
1321 return ret;
1322
1323 res->rst = devm_reset_control_array_get_exclusive(dev);
1324 if (IS_ERR(res->rst))
1325 return PTR_ERR(res->rst);
1326
1327 return 0;
1328}
1329
1330static void qcom_pcie_deinit_2_9_0(struct qcom_pcie *pcie)
1331{
1332 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1333
1334 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
1335}
1336
1337static int qcom_pcie_init_2_9_0(struct qcom_pcie *pcie)
1338{
1339 struct qcom_pcie_resources_2_9_0 *res = &pcie->res.v2_9_0;
1340 struct device *dev = pcie->pci->dev;
1341 int ret;
1342
1343 ret = reset_control_assert(res->rst);
1344 if (ret) {
1345 dev_err(dev, "reset assert failed (%d)\n", ret);
1346 return ret;
1347 }
1348
1349 /*
1350 * Delay periods before and after reset deassert are working values
1351 * from downstream Codeaurora kernel
1352 */
1353 usleep_range(2000, 2500);
1354
1355 ret = reset_control_deassert(res->rst);
1356 if (ret) {
1357 dev_err(dev, "reset deassert failed (%d)\n", ret);
1358 return ret;
1359 }
1360
1361 usleep_range(2000, 2500);
1362
1363 return clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
1364}
1365
1366static int qcom_pcie_post_init_2_9_0(struct qcom_pcie *pcie)
1367{
1368 struct dw_pcie *pci = pcie->pci;
1369 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1370 u32 val;
1371 int i;
1372
1373 writel(SLV_ADDR_SPACE_SZ,
1374 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1375
1376 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1377 val &= ~BIT(0);
1378 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1379
1380 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1381
1382 writel(DEVICE_TYPE_RC, pcie->parf + PCIE20_PARF_DEVICE_TYPE);
1383 writel(BYPASS | MSTR_AXI_CLK_EN | AHB_CLK_EN,
1384 pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1385 writel(GEN3_RELATED_OFF_RXEQ_RGRDLESS_RXTS |
1386 GEN3_RELATED_OFF_GEN3_ZRXDC_NONCOMPL,
1387 pci->dbi_base + GEN3_RELATED_OFF);
1388
1389 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS |
1390 SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1391 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1392 pcie->parf + PCIE20_PARF_SYS_CTRL);
1393
1394 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1395
1396 dw_pcie_dbi_ro_wr_en(pci);
1397 writel(PCIE_CAP_SLOT_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1398
1399 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1400 val &= ~PCI_EXP_LNKCAP_ASPMS;
1401 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1402
1403 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1404 PCI_EXP_DEVCTL2);
1405
1406 for (i = 0; i < 256; i++)
1407 writel(0, pcie->parf + PCIE20_PARF_BDF_TO_SID_TABLE_N + (4 * i));
1408
1409 return 0;
1410}
1411
1412static int qcom_pcie_link_up(struct dw_pcie *pci)
1413{
1414 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1415 u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1416
1417 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1418}
1419
1420static int qcom_pcie_config_sid_sm8250(struct qcom_pcie *pcie)
1421{
1422 /* iommu map structure */
1423 struct {
1424 u32 bdf;
1425 u32 phandle;
1426 u32 smmu_sid;
1427 u32 smmu_sid_len;
1428 } *map;
1429 void __iomem *bdf_to_sid_base = pcie->parf + PCIE20_PARF_BDF_TO_SID_TABLE_N;
1430 struct device *dev = pcie->pci->dev;
1431 u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1432 int i, nr_map, size = 0;
1433 u32 smmu_sid_base;
1434
1435 of_get_property(dev->of_node, "iommu-map", &size);
1436 if (!size)
1437 return 0;
1438
1439 map = kzalloc(size, GFP_KERNEL);
1440 if (!map)
1441 return -ENOMEM;
1442
1443 of_property_read_u32_array(dev->of_node,
1444 "iommu-map", (u32 *)map, size / sizeof(u32));
1445
1446 nr_map = size / (sizeof(*map));
1447
1448 crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1449
1450 /* Registers need to be zero out first */
1451 memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1452
1453 /* Extract the SMMU SID base from the first entry of iommu-map */
1454 smmu_sid_base = map[0].smmu_sid;
1455
1456 /* Look for an available entry to hold the mapping */
1457 for (i = 0; i < nr_map; i++) {
1458 __be16 bdf_be = cpu_to_be16(map[i].bdf);
1459 u32 val;
1460 u8 hash;
1461
1462 hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be),
1463 0);
1464
1465 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1466
1467 /* If the register is already populated, look for next available entry */
1468 while (val) {
1469 u8 current_hash = hash++;
1470 u8 next_mask = 0xff;
1471
1472 /* If NEXT field is NULL then update it with next hash */
1473 if (!(val & next_mask)) {
1474 val |= (u32)hash;
1475 writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1476 }
1477
1478 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1479 }
1480
1481 /* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1482 val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1483 writel(val, bdf_to_sid_base + hash * sizeof(u32));
1484 }
1485
1486 kfree(map);
1487
1488 return 0;
1489}
1490
1491static int qcom_pcie_host_init(struct dw_pcie_rp *pp)
1492{
1493 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1494 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1495 int ret;
1496
1497 qcom_ep_reset_assert(pcie);
1498
1499 ret = pcie->cfg->ops->init(pcie);
1500 if (ret)
1501 return ret;
1502
1503 ret = phy_set_mode_ext(pcie->phy, PHY_MODE_PCIE, PHY_MODE_PCIE_RC);
1504 if (ret)
1505 goto err_deinit;
1506
1507 ret = phy_power_on(pcie->phy);
1508 if (ret)
1509 goto err_deinit;
1510
1511 if (pcie->cfg->ops->post_init) {
1512 ret = pcie->cfg->ops->post_init(pcie);
1513 if (ret)
1514 goto err_disable_phy;
1515 }
1516
1517 qcom_ep_reset_deassert(pcie);
1518
1519 if (pcie->cfg->ops->config_sid) {
1520 ret = pcie->cfg->ops->config_sid(pcie);
1521 if (ret)
1522 goto err_assert_reset;
1523 }
1524
1525 return 0;
1526
1527err_assert_reset:
1528 qcom_ep_reset_assert(pcie);
1529err_disable_phy:
1530 phy_power_off(pcie->phy);
1531err_deinit:
1532 pcie->cfg->ops->deinit(pcie);
1533
1534 return ret;
1535}
1536
1537static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1538 .host_init = qcom_pcie_host_init,
1539};
1540
1541/* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1542static const struct qcom_pcie_ops ops_2_1_0 = {
1543 .get_resources = qcom_pcie_get_resources_2_1_0,
1544 .init = qcom_pcie_init_2_1_0,
1545 .post_init = qcom_pcie_post_init_2_1_0,
1546 .deinit = qcom_pcie_deinit_2_1_0,
1547 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1548};
1549
1550/* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1551static const struct qcom_pcie_ops ops_1_0_0 = {
1552 .get_resources = qcom_pcie_get_resources_1_0_0,
1553 .init = qcom_pcie_init_1_0_0,
1554 .post_init = qcom_pcie_post_init_1_0_0,
1555 .deinit = qcom_pcie_deinit_1_0_0,
1556 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1557};
1558
1559/* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1560static const struct qcom_pcie_ops ops_2_3_2 = {
1561 .get_resources = qcom_pcie_get_resources_2_3_2,
1562 .init = qcom_pcie_init_2_3_2,
1563 .post_init = qcom_pcie_post_init_2_3_2,
1564 .deinit = qcom_pcie_deinit_2_3_2,
1565 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1566};
1567
1568/* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1569static const struct qcom_pcie_ops ops_2_4_0 = {
1570 .get_resources = qcom_pcie_get_resources_2_4_0,
1571 .init = qcom_pcie_init_2_4_0,
1572 .post_init = qcom_pcie_post_init_2_4_0,
1573 .deinit = qcom_pcie_deinit_2_4_0,
1574 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1575};
1576
1577/* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1578static const struct qcom_pcie_ops ops_2_3_3 = {
1579 .get_resources = qcom_pcie_get_resources_2_3_3,
1580 .init = qcom_pcie_init_2_3_3,
1581 .post_init = qcom_pcie_post_init_2_3_3,
1582 .deinit = qcom_pcie_deinit_2_3_3,
1583 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1584};
1585
1586/* Qcom IP rev.: 2.7.0 Synopsys IP rev.: 4.30a */
1587static const struct qcom_pcie_ops ops_2_7_0 = {
1588 .get_resources = qcom_pcie_get_resources_2_7_0,
1589 .init = qcom_pcie_init_2_7_0,
1590 .deinit = qcom_pcie_deinit_2_7_0,
1591 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1592};
1593
1594/* Qcom IP rev.: 1.9.0 */
1595static const struct qcom_pcie_ops ops_1_9_0 = {
1596 .get_resources = qcom_pcie_get_resources_2_7_0,
1597 .init = qcom_pcie_init_2_7_0,
1598 .deinit = qcom_pcie_deinit_2_7_0,
1599 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1600 .config_sid = qcom_pcie_config_sid_sm8250,
1601};
1602
1603/* Qcom IP rev.: 2.9.0 Synopsys IP rev.: 5.00a */
1604static const struct qcom_pcie_ops ops_2_9_0 = {
1605 .get_resources = qcom_pcie_get_resources_2_9_0,
1606 .init = qcom_pcie_init_2_9_0,
1607 .post_init = qcom_pcie_post_init_2_9_0,
1608 .deinit = qcom_pcie_deinit_2_9_0,
1609 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1610};
1611
1612static const struct qcom_pcie_cfg cfg_1_0_0 = {
1613 .ops = &ops_1_0_0,
1614};
1615
1616static const struct qcom_pcie_cfg cfg_1_9_0 = {
1617 .ops = &ops_1_9_0,
1618};
1619
1620static const struct qcom_pcie_cfg cfg_2_1_0 = {
1621 .ops = &ops_2_1_0,
1622};
1623
1624static const struct qcom_pcie_cfg cfg_2_3_2 = {
1625 .ops = &ops_2_3_2,
1626};
1627
1628static const struct qcom_pcie_cfg cfg_2_3_3 = {
1629 .ops = &ops_2_3_3,
1630};
1631
1632static const struct qcom_pcie_cfg cfg_2_4_0 = {
1633 .ops = &ops_2_4_0,
1634};
1635
1636static const struct qcom_pcie_cfg cfg_2_7_0 = {
1637 .ops = &ops_2_7_0,
1638};
1639
1640static const struct qcom_pcie_cfg cfg_2_9_0 = {
1641 .ops = &ops_2_9_0,
1642};
1643
1644static const struct dw_pcie_ops dw_pcie_ops = {
1645 .link_up = qcom_pcie_link_up,
1646 .start_link = qcom_pcie_start_link,
1647};
1648
1649static int qcom_pcie_icc_init(struct qcom_pcie *pcie)
1650{
1651 struct dw_pcie *pci = pcie->pci;
1652 int ret;
1653
1654 pcie->icc_mem = devm_of_icc_get(pci->dev, "pcie-mem");
1655 if (IS_ERR(pcie->icc_mem))
1656 return PTR_ERR(pcie->icc_mem);
1657
1658 /*
1659 * Some Qualcomm platforms require interconnect bandwidth constraints
1660 * to be set before enabling interconnect clocks.
1661 *
1662 * Set an initial peak bandwidth corresponding to single-lane Gen 1
1663 * for the pcie-mem path.
1664 */
1665 ret = icc_set_bw(pcie->icc_mem, 0, MBps_to_icc(250));
1666 if (ret) {
1667 dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1668 ret);
1669 return ret;
1670 }
1671
1672 return 0;
1673}
1674
1675static void qcom_pcie_icc_update(struct qcom_pcie *pcie)
1676{
1677 struct dw_pcie *pci = pcie->pci;
1678 u32 offset, status, bw;
1679 int speed, width;
1680 int ret;
1681
1682 if (!pcie->icc_mem)
1683 return;
1684
1685 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1686 status = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1687
1688 /* Only update constraints if link is up. */
1689 if (!(status & PCI_EXP_LNKSTA_DLLLA))
1690 return;
1691
1692 speed = FIELD_GET(PCI_EXP_LNKSTA_CLS, status);
1693 width = FIELD_GET(PCI_EXP_LNKSTA_NLW, status);
1694
1695 switch (speed) {
1696 case 1:
1697 bw = MBps_to_icc(250);
1698 break;
1699 case 2:
1700 bw = MBps_to_icc(500);
1701 break;
1702 default:
1703 WARN_ON_ONCE(1);
1704 fallthrough;
1705 case 3:
1706 bw = MBps_to_icc(985);
1707 break;
1708 }
1709
1710 ret = icc_set_bw(pcie->icc_mem, 0, width * bw);
1711 if (ret) {
1712 dev_err(pci->dev, "failed to set interconnect bandwidth: %d\n",
1713 ret);
1714 }
1715}
1716
1717static int qcom_pcie_probe(struct platform_device *pdev)
1718{
1719 struct device *dev = &pdev->dev;
1720 struct dw_pcie_rp *pp;
1721 struct dw_pcie *pci;
1722 struct qcom_pcie *pcie;
1723 const struct qcom_pcie_cfg *pcie_cfg;
1724 int ret;
1725
1726 pcie_cfg = of_device_get_match_data(dev);
1727 if (!pcie_cfg || !pcie_cfg->ops) {
1728 dev_err(dev, "Invalid platform data\n");
1729 return -EINVAL;
1730 }
1731
1732 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1733 if (!pcie)
1734 return -ENOMEM;
1735
1736 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1737 if (!pci)
1738 return -ENOMEM;
1739
1740 pm_runtime_enable(dev);
1741 ret = pm_runtime_get_sync(dev);
1742 if (ret < 0)
1743 goto err_pm_runtime_put;
1744
1745 pci->dev = dev;
1746 pci->ops = &dw_pcie_ops;
1747 pp = &pci->pp;
1748
1749 pcie->pci = pci;
1750
1751 pcie->cfg = pcie_cfg;
1752
1753 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1754 if (IS_ERR(pcie->reset)) {
1755 ret = PTR_ERR(pcie->reset);
1756 goto err_pm_runtime_put;
1757 }
1758
1759 pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1760 if (IS_ERR(pcie->parf)) {
1761 ret = PTR_ERR(pcie->parf);
1762 goto err_pm_runtime_put;
1763 }
1764
1765 pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1766 if (IS_ERR(pcie->elbi)) {
1767 ret = PTR_ERR(pcie->elbi);
1768 goto err_pm_runtime_put;
1769 }
1770
1771 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1772 if (IS_ERR(pcie->phy)) {
1773 ret = PTR_ERR(pcie->phy);
1774 goto err_pm_runtime_put;
1775 }
1776
1777 ret = qcom_pcie_icc_init(pcie);
1778 if (ret)
1779 goto err_pm_runtime_put;
1780
1781 ret = pcie->cfg->ops->get_resources(pcie);
1782 if (ret)
1783 goto err_pm_runtime_put;
1784
1785 pp->ops = &qcom_pcie_dw_ops;
1786
1787 ret = phy_init(pcie->phy);
1788 if (ret)
1789 goto err_pm_runtime_put;
1790
1791 platform_set_drvdata(pdev, pcie);
1792
1793 ret = dw_pcie_host_init(pp);
1794 if (ret) {
1795 dev_err(dev, "cannot initialize host\n");
1796 goto err_phy_exit;
1797 }
1798
1799 qcom_pcie_icc_update(pcie);
1800
1801 return 0;
1802
1803err_phy_exit:
1804 phy_exit(pcie->phy);
1805err_pm_runtime_put:
1806 pm_runtime_put(dev);
1807 pm_runtime_disable(dev);
1808
1809 return ret;
1810}
1811
1812static const struct of_device_id qcom_pcie_match[] = {
1813 { .compatible = "qcom,pcie-apq8064", .data = &cfg_2_1_0 },
1814 { .compatible = "qcom,pcie-apq8084", .data = &cfg_1_0_0 },
1815 { .compatible = "qcom,pcie-ipq4019", .data = &cfg_2_4_0 },
1816 { .compatible = "qcom,pcie-ipq6018", .data = &cfg_2_9_0 },
1817 { .compatible = "qcom,pcie-ipq8064", .data = &cfg_2_1_0 },
1818 { .compatible = "qcom,pcie-ipq8064-v2", .data = &cfg_2_1_0 },
1819 { .compatible = "qcom,pcie-ipq8074", .data = &cfg_2_3_3 },
1820 { .compatible = "qcom,pcie-msm8996", .data = &cfg_2_3_2 },
1821 { .compatible = "qcom,pcie-qcs404", .data = &cfg_2_4_0 },
1822 { .compatible = "qcom,pcie-sa8540p", .data = &cfg_1_9_0 },
1823 { .compatible = "qcom,pcie-sc7280", .data = &cfg_1_9_0 },
1824 { .compatible = "qcom,pcie-sc8180x", .data = &cfg_1_9_0 },
1825 { .compatible = "qcom,pcie-sc8280xp", .data = &cfg_1_9_0 },
1826 { .compatible = "qcom,pcie-sdm845", .data = &cfg_2_7_0 },
1827 { .compatible = "qcom,pcie-sm8150", .data = &cfg_1_9_0 },
1828 { .compatible = "qcom,pcie-sm8250", .data = &cfg_1_9_0 },
1829 { .compatible = "qcom,pcie-sm8450-pcie0", .data = &cfg_1_9_0 },
1830 { .compatible = "qcom,pcie-sm8450-pcie1", .data = &cfg_1_9_0 },
1831 { }
1832};
1833
1834static void qcom_fixup_class(struct pci_dev *dev)
1835{
1836 dev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1837}
1838DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1839DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1840DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1841DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1842DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1843DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1844DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1845
1846static struct platform_driver qcom_pcie_driver = {
1847 .probe = qcom_pcie_probe,
1848 .driver = {
1849 .name = "qcom-pcie",
1850 .suppress_bind_attrs = true,
1851 .of_match_table = qcom_pcie_match,
1852 },
1853};
1854builtin_platform_driver(qcom_pcie_driver);
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Qualcomm PCIe root complex driver
4 *
5 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
6 * Copyright 2015 Linaro Limited.
7 *
8 * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
9 */
10
11#include <linux/clk.h>
12#include <linux/crc8.h>
13#include <linux/delay.h>
14#include <linux/gpio/consumer.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/iopoll.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/of_device.h>
21#include <linux/of_gpio.h>
22#include <linux/pci.h>
23#include <linux/pm_runtime.h>
24#include <linux/platform_device.h>
25#include <linux/phy/phy.h>
26#include <linux/regulator/consumer.h>
27#include <linux/reset.h>
28#include <linux/slab.h>
29#include <linux/types.h>
30
31#include "../../pci.h"
32#include "pcie-designware.h"
33
34#define PCIE20_PARF_SYS_CTRL 0x00
35#define MST_WAKEUP_EN BIT(13)
36#define SLV_WAKEUP_EN BIT(12)
37#define MSTR_ACLK_CGC_DIS BIT(10)
38#define SLV_ACLK_CGC_DIS BIT(9)
39#define CORE_CLK_CGC_DIS BIT(6)
40#define AUX_PWR_DET BIT(4)
41#define L23_CLK_RMV_DIS BIT(2)
42#define L1_CLK_RMV_DIS BIT(1)
43
44#define PCIE20_PARF_PHY_CTRL 0x40
45#define PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK GENMASK(20, 16)
46#define PHY_CTRL_PHY_TX0_TERM_OFFSET(x) ((x) << 16)
47
48#define PCIE20_PARF_PHY_REFCLK 0x4C
49#define PHY_REFCLK_SSP_EN BIT(16)
50#define PHY_REFCLK_USE_PAD BIT(12)
51
52#define PCIE20_PARF_DBI_BASE_ADDR 0x168
53#define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
54#define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
55#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
56#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
57#define PCIE20_PARF_LTSSM 0x1B0
58#define PCIE20_PARF_SID_OFFSET 0x234
59#define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
60#define PCIE20_PARF_DEVICE_TYPE 0x1000
61#define PCIE20_PARF_BDF_TO_SID_TABLE_N 0x2000
62
63#define PCIE20_ELBI_SYS_CTRL 0x04
64#define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
65
66#define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
67#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
68#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
69#define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
70#define CFG_BRIDGE_SB_INIT BIT(0)
71
72#define PCIE_CAP_LINK1_VAL 0x2FD7F
73
74#define PCIE20_PARF_Q2A_FLUSH 0x1AC
75
76#define PCIE20_MISC_CONTROL_1_REG 0x8BC
77#define DBI_RO_WR_EN 1
78
79#define PERST_DELAY_US 1000
80/* PARF registers */
81#define PCIE20_PARF_PCS_DEEMPH 0x34
82#define PCS_DEEMPH_TX_DEEMPH_GEN1(x) ((x) << 16)
83#define PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(x) ((x) << 8)
84#define PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(x) ((x) << 0)
85
86#define PCIE20_PARF_PCS_SWING 0x38
87#define PCS_SWING_TX_SWING_FULL(x) ((x) << 8)
88#define PCS_SWING_TX_SWING_LOW(x) ((x) << 0)
89
90#define PCIE20_PARF_CONFIG_BITS 0x50
91#define PHY_RX0_EQ(x) ((x) << 24)
92
93#define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
94#define SLV_ADDR_SPACE_SZ 0x10000000
95
96#define PCIE20_LNK_CONTROL2_LINK_STATUS2 0xa0
97
98#define DEVICE_TYPE_RC 0x4
99
100#define QCOM_PCIE_2_1_0_MAX_SUPPLY 3
101#define QCOM_PCIE_2_1_0_MAX_CLOCKS 5
102
103#define QCOM_PCIE_CRC8_POLYNOMIAL (BIT(2) | BIT(1) | BIT(0))
104
105struct qcom_pcie_resources_2_1_0 {
106 struct clk_bulk_data clks[QCOM_PCIE_2_1_0_MAX_CLOCKS];
107 struct reset_control *pci_reset;
108 struct reset_control *axi_reset;
109 struct reset_control *ahb_reset;
110 struct reset_control *por_reset;
111 struct reset_control *phy_reset;
112 struct reset_control *ext_reset;
113 struct regulator_bulk_data supplies[QCOM_PCIE_2_1_0_MAX_SUPPLY];
114};
115
116struct qcom_pcie_resources_1_0_0 {
117 struct clk *iface;
118 struct clk *aux;
119 struct clk *master_bus;
120 struct clk *slave_bus;
121 struct reset_control *core;
122 struct regulator *vdda;
123};
124
125#define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
126struct qcom_pcie_resources_2_3_2 {
127 struct clk *aux_clk;
128 struct clk *master_clk;
129 struct clk *slave_clk;
130 struct clk *cfg_clk;
131 struct clk *pipe_clk;
132 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
133};
134
135#define QCOM_PCIE_2_4_0_MAX_CLOCKS 4
136struct qcom_pcie_resources_2_4_0 {
137 struct clk_bulk_data clks[QCOM_PCIE_2_4_0_MAX_CLOCKS];
138 int num_clks;
139 struct reset_control *axi_m_reset;
140 struct reset_control *axi_s_reset;
141 struct reset_control *pipe_reset;
142 struct reset_control *axi_m_vmid_reset;
143 struct reset_control *axi_s_xpu_reset;
144 struct reset_control *parf_reset;
145 struct reset_control *phy_reset;
146 struct reset_control *axi_m_sticky_reset;
147 struct reset_control *pipe_sticky_reset;
148 struct reset_control *pwr_reset;
149 struct reset_control *ahb_reset;
150 struct reset_control *phy_ahb_reset;
151};
152
153struct qcom_pcie_resources_2_3_3 {
154 struct clk *iface;
155 struct clk *axi_m_clk;
156 struct clk *axi_s_clk;
157 struct clk *ahb_clk;
158 struct clk *aux_clk;
159 struct reset_control *rst[7];
160};
161
162/* 6 clocks typically, 7 for sm8250 */
163struct qcom_pcie_resources_2_7_0 {
164 struct clk_bulk_data clks[7];
165 int num_clks;
166 struct regulator_bulk_data supplies[2];
167 struct reset_control *pci_reset;
168 struct clk *pipe_clk;
169};
170
171union qcom_pcie_resources {
172 struct qcom_pcie_resources_1_0_0 v1_0_0;
173 struct qcom_pcie_resources_2_1_0 v2_1_0;
174 struct qcom_pcie_resources_2_3_2 v2_3_2;
175 struct qcom_pcie_resources_2_3_3 v2_3_3;
176 struct qcom_pcie_resources_2_4_0 v2_4_0;
177 struct qcom_pcie_resources_2_7_0 v2_7_0;
178};
179
180struct qcom_pcie;
181
182struct qcom_pcie_ops {
183 int (*get_resources)(struct qcom_pcie *pcie);
184 int (*init)(struct qcom_pcie *pcie);
185 int (*post_init)(struct qcom_pcie *pcie);
186 void (*deinit)(struct qcom_pcie *pcie);
187 void (*post_deinit)(struct qcom_pcie *pcie);
188 void (*ltssm_enable)(struct qcom_pcie *pcie);
189 int (*config_sid)(struct qcom_pcie *pcie);
190};
191
192struct qcom_pcie {
193 struct dw_pcie *pci;
194 void __iomem *parf; /* DT parf */
195 void __iomem *elbi; /* DT elbi */
196 union qcom_pcie_resources res;
197 struct phy *phy;
198 struct gpio_desc *reset;
199 const struct qcom_pcie_ops *ops;
200};
201
202#define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
203
204static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
205{
206 gpiod_set_value_cansleep(pcie->reset, 1);
207 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
208}
209
210static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
211{
212 /* Ensure that PERST has been asserted for at least 100 ms */
213 msleep(100);
214 gpiod_set_value_cansleep(pcie->reset, 0);
215 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
216}
217
218static int qcom_pcie_start_link(struct dw_pcie *pci)
219{
220 struct qcom_pcie *pcie = to_qcom_pcie(pci);
221
222 /* Enable Link Training state machine */
223 if (pcie->ops->ltssm_enable)
224 pcie->ops->ltssm_enable(pcie);
225
226 return 0;
227}
228
229static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
230{
231 u32 val;
232
233 /* enable link training */
234 val = readl(pcie->elbi + PCIE20_ELBI_SYS_CTRL);
235 val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
236 writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
237}
238
239static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
240{
241 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
242 struct dw_pcie *pci = pcie->pci;
243 struct device *dev = pci->dev;
244 int ret;
245
246 res->supplies[0].supply = "vdda";
247 res->supplies[1].supply = "vdda_phy";
248 res->supplies[2].supply = "vdda_refclk";
249 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
250 res->supplies);
251 if (ret)
252 return ret;
253
254 res->clks[0].id = "iface";
255 res->clks[1].id = "core";
256 res->clks[2].id = "phy";
257 res->clks[3].id = "aux";
258 res->clks[4].id = "ref";
259
260 /* iface, core, phy are required */
261 ret = devm_clk_bulk_get(dev, 3, res->clks);
262 if (ret < 0)
263 return ret;
264
265 /* aux, ref are optional */
266 ret = devm_clk_bulk_get_optional(dev, 2, res->clks + 3);
267 if (ret < 0)
268 return ret;
269
270 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
271 if (IS_ERR(res->pci_reset))
272 return PTR_ERR(res->pci_reset);
273
274 res->axi_reset = devm_reset_control_get_exclusive(dev, "axi");
275 if (IS_ERR(res->axi_reset))
276 return PTR_ERR(res->axi_reset);
277
278 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
279 if (IS_ERR(res->ahb_reset))
280 return PTR_ERR(res->ahb_reset);
281
282 res->por_reset = devm_reset_control_get_exclusive(dev, "por");
283 if (IS_ERR(res->por_reset))
284 return PTR_ERR(res->por_reset);
285
286 res->ext_reset = devm_reset_control_get_optional_exclusive(dev, "ext");
287 if (IS_ERR(res->ext_reset))
288 return PTR_ERR(res->ext_reset);
289
290 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
291 return PTR_ERR_OR_ZERO(res->phy_reset);
292}
293
294static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
295{
296 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
297
298 clk_bulk_disable_unprepare(ARRAY_SIZE(res->clks), res->clks);
299 reset_control_assert(res->pci_reset);
300 reset_control_assert(res->axi_reset);
301 reset_control_assert(res->ahb_reset);
302 reset_control_assert(res->por_reset);
303 reset_control_assert(res->ext_reset);
304 reset_control_assert(res->phy_reset);
305
306 writel(1, pcie->parf + PCIE20_PARF_PHY_CTRL);
307
308 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
309}
310
311static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
312{
313 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
314 struct dw_pcie *pci = pcie->pci;
315 struct device *dev = pci->dev;
316 struct device_node *node = dev->of_node;
317 u32 val;
318 int ret;
319
320 /* reset the PCIe interface as uboot can leave it undefined state */
321 reset_control_assert(res->pci_reset);
322 reset_control_assert(res->axi_reset);
323 reset_control_assert(res->ahb_reset);
324 reset_control_assert(res->por_reset);
325 reset_control_assert(res->ext_reset);
326 reset_control_assert(res->phy_reset);
327
328 writel(1, pcie->parf + PCIE20_PARF_PHY_CTRL);
329
330 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
331 if (ret < 0) {
332 dev_err(dev, "cannot enable regulators\n");
333 return ret;
334 }
335
336 ret = reset_control_deassert(res->ahb_reset);
337 if (ret) {
338 dev_err(dev, "cannot deassert ahb reset\n");
339 goto err_deassert_ahb;
340 }
341
342 ret = reset_control_deassert(res->ext_reset);
343 if (ret) {
344 dev_err(dev, "cannot deassert ext reset\n");
345 goto err_deassert_ext;
346 }
347
348 ret = reset_control_deassert(res->phy_reset);
349 if (ret) {
350 dev_err(dev, "cannot deassert phy reset\n");
351 goto err_deassert_phy;
352 }
353
354 ret = reset_control_deassert(res->pci_reset);
355 if (ret) {
356 dev_err(dev, "cannot deassert pci reset\n");
357 goto err_deassert_pci;
358 }
359
360 ret = reset_control_deassert(res->por_reset);
361 if (ret) {
362 dev_err(dev, "cannot deassert por reset\n");
363 goto err_deassert_por;
364 }
365
366 ret = reset_control_deassert(res->axi_reset);
367 if (ret) {
368 dev_err(dev, "cannot deassert axi reset\n");
369 goto err_deassert_axi;
370 }
371
372 ret = clk_bulk_prepare_enable(ARRAY_SIZE(res->clks), res->clks);
373 if (ret)
374 goto err_clks;
375
376 /* enable PCIe clocks and resets */
377 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
378 val &= ~BIT(0);
379 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
380
381 if (of_device_is_compatible(node, "qcom,pcie-ipq8064") ||
382 of_device_is_compatible(node, "qcom,pcie-ipq8064-v2")) {
383 writel(PCS_DEEMPH_TX_DEEMPH_GEN1(24) |
384 PCS_DEEMPH_TX_DEEMPH_GEN2_3_5DB(24) |
385 PCS_DEEMPH_TX_DEEMPH_GEN2_6DB(34),
386 pcie->parf + PCIE20_PARF_PCS_DEEMPH);
387 writel(PCS_SWING_TX_SWING_FULL(120) |
388 PCS_SWING_TX_SWING_LOW(120),
389 pcie->parf + PCIE20_PARF_PCS_SWING);
390 writel(PHY_RX0_EQ(4), pcie->parf + PCIE20_PARF_CONFIG_BITS);
391 }
392
393 if (of_device_is_compatible(node, "qcom,pcie-ipq8064")) {
394 /* set TX termination offset */
395 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
396 val &= ~PHY_CTRL_PHY_TX0_TERM_OFFSET_MASK;
397 val |= PHY_CTRL_PHY_TX0_TERM_OFFSET(7);
398 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
399 }
400
401 /* enable external reference clock */
402 val = readl(pcie->parf + PCIE20_PARF_PHY_REFCLK);
403 /* USE_PAD is required only for ipq806x */
404 if (!of_device_is_compatible(node, "qcom,pcie-apq8064"))
405 val &= ~PHY_REFCLK_USE_PAD;
406 val |= PHY_REFCLK_SSP_EN;
407 writel(val, pcie->parf + PCIE20_PARF_PHY_REFCLK);
408
409 /* wait for clock acquisition */
410 usleep_range(1000, 1500);
411
412 /* Set the Max TLP size to 2K, instead of using default of 4K */
413 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
414 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL0);
415 writel(CFG_BRIDGE_SB_INIT,
416 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL1);
417
418 return 0;
419
420err_clks:
421 reset_control_assert(res->axi_reset);
422err_deassert_axi:
423 reset_control_assert(res->por_reset);
424err_deassert_por:
425 reset_control_assert(res->pci_reset);
426err_deassert_pci:
427 reset_control_assert(res->phy_reset);
428err_deassert_phy:
429 reset_control_assert(res->ext_reset);
430err_deassert_ext:
431 reset_control_assert(res->ahb_reset);
432err_deassert_ahb:
433 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
434
435 return ret;
436}
437
438static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
439{
440 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
441 struct dw_pcie *pci = pcie->pci;
442 struct device *dev = pci->dev;
443
444 res->vdda = devm_regulator_get(dev, "vdda");
445 if (IS_ERR(res->vdda))
446 return PTR_ERR(res->vdda);
447
448 res->iface = devm_clk_get(dev, "iface");
449 if (IS_ERR(res->iface))
450 return PTR_ERR(res->iface);
451
452 res->aux = devm_clk_get(dev, "aux");
453 if (IS_ERR(res->aux))
454 return PTR_ERR(res->aux);
455
456 res->master_bus = devm_clk_get(dev, "master_bus");
457 if (IS_ERR(res->master_bus))
458 return PTR_ERR(res->master_bus);
459
460 res->slave_bus = devm_clk_get(dev, "slave_bus");
461 if (IS_ERR(res->slave_bus))
462 return PTR_ERR(res->slave_bus);
463
464 res->core = devm_reset_control_get_exclusive(dev, "core");
465 return PTR_ERR_OR_ZERO(res->core);
466}
467
468static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
469{
470 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
471
472 reset_control_assert(res->core);
473 clk_disable_unprepare(res->slave_bus);
474 clk_disable_unprepare(res->master_bus);
475 clk_disable_unprepare(res->iface);
476 clk_disable_unprepare(res->aux);
477 regulator_disable(res->vdda);
478}
479
480static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
481{
482 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
483 struct dw_pcie *pci = pcie->pci;
484 struct device *dev = pci->dev;
485 int ret;
486
487 ret = reset_control_deassert(res->core);
488 if (ret) {
489 dev_err(dev, "cannot deassert core reset\n");
490 return ret;
491 }
492
493 ret = clk_prepare_enable(res->aux);
494 if (ret) {
495 dev_err(dev, "cannot prepare/enable aux clock\n");
496 goto err_res;
497 }
498
499 ret = clk_prepare_enable(res->iface);
500 if (ret) {
501 dev_err(dev, "cannot prepare/enable iface clock\n");
502 goto err_aux;
503 }
504
505 ret = clk_prepare_enable(res->master_bus);
506 if (ret) {
507 dev_err(dev, "cannot prepare/enable master_bus clock\n");
508 goto err_iface;
509 }
510
511 ret = clk_prepare_enable(res->slave_bus);
512 if (ret) {
513 dev_err(dev, "cannot prepare/enable slave_bus clock\n");
514 goto err_master;
515 }
516
517 ret = regulator_enable(res->vdda);
518 if (ret) {
519 dev_err(dev, "cannot enable vdda regulator\n");
520 goto err_slave;
521 }
522
523 /* change DBI base address */
524 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
525
526 if (IS_ENABLED(CONFIG_PCI_MSI)) {
527 u32 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
528
529 val |= BIT(31);
530 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
531 }
532
533 return 0;
534err_slave:
535 clk_disable_unprepare(res->slave_bus);
536err_master:
537 clk_disable_unprepare(res->master_bus);
538err_iface:
539 clk_disable_unprepare(res->iface);
540err_aux:
541 clk_disable_unprepare(res->aux);
542err_res:
543 reset_control_assert(res->core);
544
545 return ret;
546}
547
548static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
549{
550 u32 val;
551
552 /* enable link training */
553 val = readl(pcie->parf + PCIE20_PARF_LTSSM);
554 val |= BIT(8);
555 writel(val, pcie->parf + PCIE20_PARF_LTSSM);
556}
557
558static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
559{
560 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
561 struct dw_pcie *pci = pcie->pci;
562 struct device *dev = pci->dev;
563 int ret;
564
565 res->supplies[0].supply = "vdda";
566 res->supplies[1].supply = "vddpe-3v3";
567 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
568 res->supplies);
569 if (ret)
570 return ret;
571
572 res->aux_clk = devm_clk_get(dev, "aux");
573 if (IS_ERR(res->aux_clk))
574 return PTR_ERR(res->aux_clk);
575
576 res->cfg_clk = devm_clk_get(dev, "cfg");
577 if (IS_ERR(res->cfg_clk))
578 return PTR_ERR(res->cfg_clk);
579
580 res->master_clk = devm_clk_get(dev, "bus_master");
581 if (IS_ERR(res->master_clk))
582 return PTR_ERR(res->master_clk);
583
584 res->slave_clk = devm_clk_get(dev, "bus_slave");
585 if (IS_ERR(res->slave_clk))
586 return PTR_ERR(res->slave_clk);
587
588 res->pipe_clk = devm_clk_get(dev, "pipe");
589 return PTR_ERR_OR_ZERO(res->pipe_clk);
590}
591
592static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
593{
594 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
595
596 clk_disable_unprepare(res->slave_clk);
597 clk_disable_unprepare(res->master_clk);
598 clk_disable_unprepare(res->cfg_clk);
599 clk_disable_unprepare(res->aux_clk);
600
601 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
602}
603
604static void qcom_pcie_post_deinit_2_3_2(struct qcom_pcie *pcie)
605{
606 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
607
608 clk_disable_unprepare(res->pipe_clk);
609}
610
611static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
612{
613 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
614 struct dw_pcie *pci = pcie->pci;
615 struct device *dev = pci->dev;
616 u32 val;
617 int ret;
618
619 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
620 if (ret < 0) {
621 dev_err(dev, "cannot enable regulators\n");
622 return ret;
623 }
624
625 ret = clk_prepare_enable(res->aux_clk);
626 if (ret) {
627 dev_err(dev, "cannot prepare/enable aux clock\n");
628 goto err_aux_clk;
629 }
630
631 ret = clk_prepare_enable(res->cfg_clk);
632 if (ret) {
633 dev_err(dev, "cannot prepare/enable cfg clock\n");
634 goto err_cfg_clk;
635 }
636
637 ret = clk_prepare_enable(res->master_clk);
638 if (ret) {
639 dev_err(dev, "cannot prepare/enable master clock\n");
640 goto err_master_clk;
641 }
642
643 ret = clk_prepare_enable(res->slave_clk);
644 if (ret) {
645 dev_err(dev, "cannot prepare/enable slave clock\n");
646 goto err_slave_clk;
647 }
648
649 /* enable PCIe clocks and resets */
650 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
651 val &= ~BIT(0);
652 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
653
654 /* change DBI base address */
655 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
656
657 /* MAC PHY_POWERDOWN MUX DISABLE */
658 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
659 val &= ~BIT(29);
660 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
661
662 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
663 val |= BIT(4);
664 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
665
666 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
667 val |= BIT(31);
668 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
669
670 return 0;
671
672err_slave_clk:
673 clk_disable_unprepare(res->master_clk);
674err_master_clk:
675 clk_disable_unprepare(res->cfg_clk);
676err_cfg_clk:
677 clk_disable_unprepare(res->aux_clk);
678
679err_aux_clk:
680 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
681
682 return ret;
683}
684
685static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
686{
687 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
688 struct dw_pcie *pci = pcie->pci;
689 struct device *dev = pci->dev;
690 int ret;
691
692 ret = clk_prepare_enable(res->pipe_clk);
693 if (ret) {
694 dev_err(dev, "cannot prepare/enable pipe clock\n");
695 return ret;
696 }
697
698 return 0;
699}
700
701static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
702{
703 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
704 struct dw_pcie *pci = pcie->pci;
705 struct device *dev = pci->dev;
706 bool is_ipq = of_device_is_compatible(dev->of_node, "qcom,pcie-ipq4019");
707 int ret;
708
709 res->clks[0].id = "aux";
710 res->clks[1].id = "master_bus";
711 res->clks[2].id = "slave_bus";
712 res->clks[3].id = "iface";
713
714 /* qcom,pcie-ipq4019 is defined without "iface" */
715 res->num_clks = is_ipq ? 3 : 4;
716
717 ret = devm_clk_bulk_get(dev, res->num_clks, res->clks);
718 if (ret < 0)
719 return ret;
720
721 res->axi_m_reset = devm_reset_control_get_exclusive(dev, "axi_m");
722 if (IS_ERR(res->axi_m_reset))
723 return PTR_ERR(res->axi_m_reset);
724
725 res->axi_s_reset = devm_reset_control_get_exclusive(dev, "axi_s");
726 if (IS_ERR(res->axi_s_reset))
727 return PTR_ERR(res->axi_s_reset);
728
729 if (is_ipq) {
730 /*
731 * These resources relates to the PHY or are secure clocks, but
732 * are controlled here for IPQ4019
733 */
734 res->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
735 if (IS_ERR(res->pipe_reset))
736 return PTR_ERR(res->pipe_reset);
737
738 res->axi_m_vmid_reset = devm_reset_control_get_exclusive(dev,
739 "axi_m_vmid");
740 if (IS_ERR(res->axi_m_vmid_reset))
741 return PTR_ERR(res->axi_m_vmid_reset);
742
743 res->axi_s_xpu_reset = devm_reset_control_get_exclusive(dev,
744 "axi_s_xpu");
745 if (IS_ERR(res->axi_s_xpu_reset))
746 return PTR_ERR(res->axi_s_xpu_reset);
747
748 res->parf_reset = devm_reset_control_get_exclusive(dev, "parf");
749 if (IS_ERR(res->parf_reset))
750 return PTR_ERR(res->parf_reset);
751
752 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
753 if (IS_ERR(res->phy_reset))
754 return PTR_ERR(res->phy_reset);
755 }
756
757 res->axi_m_sticky_reset = devm_reset_control_get_exclusive(dev,
758 "axi_m_sticky");
759 if (IS_ERR(res->axi_m_sticky_reset))
760 return PTR_ERR(res->axi_m_sticky_reset);
761
762 res->pipe_sticky_reset = devm_reset_control_get_exclusive(dev,
763 "pipe_sticky");
764 if (IS_ERR(res->pipe_sticky_reset))
765 return PTR_ERR(res->pipe_sticky_reset);
766
767 res->pwr_reset = devm_reset_control_get_exclusive(dev, "pwr");
768 if (IS_ERR(res->pwr_reset))
769 return PTR_ERR(res->pwr_reset);
770
771 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
772 if (IS_ERR(res->ahb_reset))
773 return PTR_ERR(res->ahb_reset);
774
775 if (is_ipq) {
776 res->phy_ahb_reset = devm_reset_control_get_exclusive(dev, "phy_ahb");
777 if (IS_ERR(res->phy_ahb_reset))
778 return PTR_ERR(res->phy_ahb_reset);
779 }
780
781 return 0;
782}
783
784static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
785{
786 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
787
788 reset_control_assert(res->axi_m_reset);
789 reset_control_assert(res->axi_s_reset);
790 reset_control_assert(res->pipe_reset);
791 reset_control_assert(res->pipe_sticky_reset);
792 reset_control_assert(res->phy_reset);
793 reset_control_assert(res->phy_ahb_reset);
794 reset_control_assert(res->axi_m_sticky_reset);
795 reset_control_assert(res->pwr_reset);
796 reset_control_assert(res->ahb_reset);
797 clk_bulk_disable_unprepare(res->num_clks, res->clks);
798}
799
800static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
801{
802 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
803 struct dw_pcie *pci = pcie->pci;
804 struct device *dev = pci->dev;
805 u32 val;
806 int ret;
807
808 ret = reset_control_assert(res->axi_m_reset);
809 if (ret) {
810 dev_err(dev, "cannot assert axi master reset\n");
811 return ret;
812 }
813
814 ret = reset_control_assert(res->axi_s_reset);
815 if (ret) {
816 dev_err(dev, "cannot assert axi slave reset\n");
817 return ret;
818 }
819
820 usleep_range(10000, 12000);
821
822 ret = reset_control_assert(res->pipe_reset);
823 if (ret) {
824 dev_err(dev, "cannot assert pipe reset\n");
825 return ret;
826 }
827
828 ret = reset_control_assert(res->pipe_sticky_reset);
829 if (ret) {
830 dev_err(dev, "cannot assert pipe sticky reset\n");
831 return ret;
832 }
833
834 ret = reset_control_assert(res->phy_reset);
835 if (ret) {
836 dev_err(dev, "cannot assert phy reset\n");
837 return ret;
838 }
839
840 ret = reset_control_assert(res->phy_ahb_reset);
841 if (ret) {
842 dev_err(dev, "cannot assert phy ahb reset\n");
843 return ret;
844 }
845
846 usleep_range(10000, 12000);
847
848 ret = reset_control_assert(res->axi_m_sticky_reset);
849 if (ret) {
850 dev_err(dev, "cannot assert axi master sticky reset\n");
851 return ret;
852 }
853
854 ret = reset_control_assert(res->pwr_reset);
855 if (ret) {
856 dev_err(dev, "cannot assert power reset\n");
857 return ret;
858 }
859
860 ret = reset_control_assert(res->ahb_reset);
861 if (ret) {
862 dev_err(dev, "cannot assert ahb reset\n");
863 return ret;
864 }
865
866 usleep_range(10000, 12000);
867
868 ret = reset_control_deassert(res->phy_ahb_reset);
869 if (ret) {
870 dev_err(dev, "cannot deassert phy ahb reset\n");
871 return ret;
872 }
873
874 ret = reset_control_deassert(res->phy_reset);
875 if (ret) {
876 dev_err(dev, "cannot deassert phy reset\n");
877 goto err_rst_phy;
878 }
879
880 ret = reset_control_deassert(res->pipe_reset);
881 if (ret) {
882 dev_err(dev, "cannot deassert pipe reset\n");
883 goto err_rst_pipe;
884 }
885
886 ret = reset_control_deassert(res->pipe_sticky_reset);
887 if (ret) {
888 dev_err(dev, "cannot deassert pipe sticky reset\n");
889 goto err_rst_pipe_sticky;
890 }
891
892 usleep_range(10000, 12000);
893
894 ret = reset_control_deassert(res->axi_m_reset);
895 if (ret) {
896 dev_err(dev, "cannot deassert axi master reset\n");
897 goto err_rst_axi_m;
898 }
899
900 ret = reset_control_deassert(res->axi_m_sticky_reset);
901 if (ret) {
902 dev_err(dev, "cannot deassert axi master sticky reset\n");
903 goto err_rst_axi_m_sticky;
904 }
905
906 ret = reset_control_deassert(res->axi_s_reset);
907 if (ret) {
908 dev_err(dev, "cannot deassert axi slave reset\n");
909 goto err_rst_axi_s;
910 }
911
912 ret = reset_control_deassert(res->pwr_reset);
913 if (ret) {
914 dev_err(dev, "cannot deassert power reset\n");
915 goto err_rst_pwr;
916 }
917
918 ret = reset_control_deassert(res->ahb_reset);
919 if (ret) {
920 dev_err(dev, "cannot deassert ahb reset\n");
921 goto err_rst_ahb;
922 }
923
924 usleep_range(10000, 12000);
925
926 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
927 if (ret)
928 goto err_clks;
929
930 /* enable PCIe clocks and resets */
931 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
932 val &= ~BIT(0);
933 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
934
935 /* change DBI base address */
936 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
937
938 /* MAC PHY_POWERDOWN MUX DISABLE */
939 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
940 val &= ~BIT(29);
941 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
942
943 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
944 val |= BIT(4);
945 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
946
947 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
948 val |= BIT(31);
949 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
950
951 return 0;
952
953err_clks:
954 reset_control_assert(res->ahb_reset);
955err_rst_ahb:
956 reset_control_assert(res->pwr_reset);
957err_rst_pwr:
958 reset_control_assert(res->axi_s_reset);
959err_rst_axi_s:
960 reset_control_assert(res->axi_m_sticky_reset);
961err_rst_axi_m_sticky:
962 reset_control_assert(res->axi_m_reset);
963err_rst_axi_m:
964 reset_control_assert(res->pipe_sticky_reset);
965err_rst_pipe_sticky:
966 reset_control_assert(res->pipe_reset);
967err_rst_pipe:
968 reset_control_assert(res->phy_reset);
969err_rst_phy:
970 reset_control_assert(res->phy_ahb_reset);
971 return ret;
972}
973
974static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
975{
976 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
977 struct dw_pcie *pci = pcie->pci;
978 struct device *dev = pci->dev;
979 int i;
980 const char *rst_names[] = { "axi_m", "axi_s", "pipe",
981 "axi_m_sticky", "sticky",
982 "ahb", "sleep", };
983
984 res->iface = devm_clk_get(dev, "iface");
985 if (IS_ERR(res->iface))
986 return PTR_ERR(res->iface);
987
988 res->axi_m_clk = devm_clk_get(dev, "axi_m");
989 if (IS_ERR(res->axi_m_clk))
990 return PTR_ERR(res->axi_m_clk);
991
992 res->axi_s_clk = devm_clk_get(dev, "axi_s");
993 if (IS_ERR(res->axi_s_clk))
994 return PTR_ERR(res->axi_s_clk);
995
996 res->ahb_clk = devm_clk_get(dev, "ahb");
997 if (IS_ERR(res->ahb_clk))
998 return PTR_ERR(res->ahb_clk);
999
1000 res->aux_clk = devm_clk_get(dev, "aux");
1001 if (IS_ERR(res->aux_clk))
1002 return PTR_ERR(res->aux_clk);
1003
1004 for (i = 0; i < ARRAY_SIZE(rst_names); i++) {
1005 res->rst[i] = devm_reset_control_get(dev, rst_names[i]);
1006 if (IS_ERR(res->rst[i]))
1007 return PTR_ERR(res->rst[i]);
1008 }
1009
1010 return 0;
1011}
1012
1013static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
1014{
1015 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1016
1017 clk_disable_unprepare(res->iface);
1018 clk_disable_unprepare(res->axi_m_clk);
1019 clk_disable_unprepare(res->axi_s_clk);
1020 clk_disable_unprepare(res->ahb_clk);
1021 clk_disable_unprepare(res->aux_clk);
1022}
1023
1024static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
1025{
1026 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1027 struct dw_pcie *pci = pcie->pci;
1028 struct device *dev = pci->dev;
1029 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1030 int i, ret;
1031 u32 val;
1032
1033 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1034 ret = reset_control_assert(res->rst[i]);
1035 if (ret) {
1036 dev_err(dev, "reset #%d assert failed (%d)\n", i, ret);
1037 return ret;
1038 }
1039 }
1040
1041 usleep_range(2000, 2500);
1042
1043 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1044 ret = reset_control_deassert(res->rst[i]);
1045 if (ret) {
1046 dev_err(dev, "reset #%d deassert failed (%d)\n", i,
1047 ret);
1048 return ret;
1049 }
1050 }
1051
1052 /*
1053 * Don't have a way to see if the reset has completed.
1054 * Wait for some time.
1055 */
1056 usleep_range(2000, 2500);
1057
1058 ret = clk_prepare_enable(res->iface);
1059 if (ret) {
1060 dev_err(dev, "cannot prepare/enable core clock\n");
1061 goto err_clk_iface;
1062 }
1063
1064 ret = clk_prepare_enable(res->axi_m_clk);
1065 if (ret) {
1066 dev_err(dev, "cannot prepare/enable core clock\n");
1067 goto err_clk_axi_m;
1068 }
1069
1070 ret = clk_prepare_enable(res->axi_s_clk);
1071 if (ret) {
1072 dev_err(dev, "cannot prepare/enable axi slave clock\n");
1073 goto err_clk_axi_s;
1074 }
1075
1076 ret = clk_prepare_enable(res->ahb_clk);
1077 if (ret) {
1078 dev_err(dev, "cannot prepare/enable ahb clock\n");
1079 goto err_clk_ahb;
1080 }
1081
1082 ret = clk_prepare_enable(res->aux_clk);
1083 if (ret) {
1084 dev_err(dev, "cannot prepare/enable aux clock\n");
1085 goto err_clk_aux;
1086 }
1087
1088 writel(SLV_ADDR_SPACE_SZ,
1089 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1090
1091 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1092 val &= ~BIT(0);
1093 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1094
1095 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1096
1097 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
1098 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1099 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1100 pcie->parf + PCIE20_PARF_SYS_CTRL);
1101 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1102
1103 writel(PCI_COMMAND_MASTER, pci->dbi_base + PCI_COMMAND);
1104 writel(DBI_RO_WR_EN, pci->dbi_base + PCIE20_MISC_CONTROL_1_REG);
1105 writel(PCIE_CAP_LINK1_VAL, pci->dbi_base + offset + PCI_EXP_SLTCAP);
1106
1107 val = readl(pci->dbi_base + offset + PCI_EXP_LNKCAP);
1108 val &= ~PCI_EXP_LNKCAP_ASPMS;
1109 writel(val, pci->dbi_base + offset + PCI_EXP_LNKCAP);
1110
1111 writel(PCI_EXP_DEVCTL2_COMP_TMOUT_DIS, pci->dbi_base + offset +
1112 PCI_EXP_DEVCTL2);
1113
1114 return 0;
1115
1116err_clk_aux:
1117 clk_disable_unprepare(res->ahb_clk);
1118err_clk_ahb:
1119 clk_disable_unprepare(res->axi_s_clk);
1120err_clk_axi_s:
1121 clk_disable_unprepare(res->axi_m_clk);
1122err_clk_axi_m:
1123 clk_disable_unprepare(res->iface);
1124err_clk_iface:
1125 /*
1126 * Not checking for failure, will anyway return
1127 * the original failure in 'ret'.
1128 */
1129 for (i = 0; i < ARRAY_SIZE(res->rst); i++)
1130 reset_control_assert(res->rst[i]);
1131
1132 return ret;
1133}
1134
1135static int qcom_pcie_get_resources_2_7_0(struct qcom_pcie *pcie)
1136{
1137 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1138 struct dw_pcie *pci = pcie->pci;
1139 struct device *dev = pci->dev;
1140 int ret;
1141
1142 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
1143 if (IS_ERR(res->pci_reset))
1144 return PTR_ERR(res->pci_reset);
1145
1146 res->supplies[0].supply = "vdda";
1147 res->supplies[1].supply = "vddpe-3v3";
1148 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
1149 res->supplies);
1150 if (ret)
1151 return ret;
1152
1153 res->clks[0].id = "aux";
1154 res->clks[1].id = "cfg";
1155 res->clks[2].id = "bus_master";
1156 res->clks[3].id = "bus_slave";
1157 res->clks[4].id = "slave_q2a";
1158 res->clks[5].id = "tbu";
1159 if (of_device_is_compatible(dev->of_node, "qcom,pcie-sm8250")) {
1160 res->clks[6].id = "ddrss_sf_tbu";
1161 res->num_clks = 7;
1162 } else {
1163 res->num_clks = 6;
1164 }
1165
1166 ret = devm_clk_bulk_get(dev, res->num_clks, res->clks);
1167 if (ret < 0)
1168 return ret;
1169
1170 res->pipe_clk = devm_clk_get(dev, "pipe");
1171 return PTR_ERR_OR_ZERO(res->pipe_clk);
1172}
1173
1174static int qcom_pcie_init_2_7_0(struct qcom_pcie *pcie)
1175{
1176 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1177 struct dw_pcie *pci = pcie->pci;
1178 struct device *dev = pci->dev;
1179 u32 val;
1180 int ret;
1181
1182 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
1183 if (ret < 0) {
1184 dev_err(dev, "cannot enable regulators\n");
1185 return ret;
1186 }
1187
1188 ret = clk_bulk_prepare_enable(res->num_clks, res->clks);
1189 if (ret < 0)
1190 goto err_disable_regulators;
1191
1192 ret = reset_control_assert(res->pci_reset);
1193 if (ret < 0) {
1194 dev_err(dev, "cannot deassert pci reset\n");
1195 goto err_disable_clocks;
1196 }
1197
1198 usleep_range(1000, 1500);
1199
1200 ret = reset_control_deassert(res->pci_reset);
1201 if (ret < 0) {
1202 dev_err(dev, "cannot deassert pci reset\n");
1203 goto err_disable_clocks;
1204 }
1205
1206 ret = clk_prepare_enable(res->pipe_clk);
1207 if (ret) {
1208 dev_err(dev, "cannot prepare/enable pipe clock\n");
1209 goto err_disable_clocks;
1210 }
1211
1212 /* configure PCIe to RC mode */
1213 writel(DEVICE_TYPE_RC, pcie->parf + PCIE20_PARF_DEVICE_TYPE);
1214
1215 /* enable PCIe clocks and resets */
1216 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1217 val &= ~BIT(0);
1218 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1219
1220 /* change DBI base address */
1221 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1222
1223 /* MAC PHY_POWERDOWN MUX DISABLE */
1224 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
1225 val &= ~BIT(29);
1226 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
1227
1228 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1229 val |= BIT(4);
1230 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
1231
1232 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1233 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
1234 val |= BIT(31);
1235 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
1236 }
1237
1238 return 0;
1239err_disable_clocks:
1240 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1241err_disable_regulators:
1242 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1243
1244 return ret;
1245}
1246
1247static void qcom_pcie_deinit_2_7_0(struct qcom_pcie *pcie)
1248{
1249 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1250
1251 clk_bulk_disable_unprepare(res->num_clks, res->clks);
1252 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
1253}
1254
1255static int qcom_pcie_post_init_2_7_0(struct qcom_pcie *pcie)
1256{
1257 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1258
1259 return clk_prepare_enable(res->pipe_clk);
1260}
1261
1262static void qcom_pcie_post_deinit_2_7_0(struct qcom_pcie *pcie)
1263{
1264 struct qcom_pcie_resources_2_7_0 *res = &pcie->res.v2_7_0;
1265
1266 clk_disable_unprepare(res->pipe_clk);
1267}
1268
1269static int qcom_pcie_link_up(struct dw_pcie *pci)
1270{
1271 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
1272 u16 val = readw(pci->dbi_base + offset + PCI_EXP_LNKSTA);
1273
1274 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1275}
1276
1277static int qcom_pcie_config_sid_sm8250(struct qcom_pcie *pcie)
1278{
1279 /* iommu map structure */
1280 struct {
1281 u32 bdf;
1282 u32 phandle;
1283 u32 smmu_sid;
1284 u32 smmu_sid_len;
1285 } *map;
1286 void __iomem *bdf_to_sid_base = pcie->parf + PCIE20_PARF_BDF_TO_SID_TABLE_N;
1287 struct device *dev = pcie->pci->dev;
1288 u8 qcom_pcie_crc8_table[CRC8_TABLE_SIZE];
1289 int i, nr_map, size = 0;
1290 u32 smmu_sid_base;
1291
1292 of_get_property(dev->of_node, "iommu-map", &size);
1293 if (!size)
1294 return 0;
1295
1296 map = kzalloc(size, GFP_KERNEL);
1297 if (!map)
1298 return -ENOMEM;
1299
1300 of_property_read_u32_array(dev->of_node,
1301 "iommu-map", (u32 *)map, size / sizeof(u32));
1302
1303 nr_map = size / (sizeof(*map));
1304
1305 crc8_populate_msb(qcom_pcie_crc8_table, QCOM_PCIE_CRC8_POLYNOMIAL);
1306
1307 /* Registers need to be zero out first */
1308 memset_io(bdf_to_sid_base, 0, CRC8_TABLE_SIZE * sizeof(u32));
1309
1310 /* Extract the SMMU SID base from the first entry of iommu-map */
1311 smmu_sid_base = map[0].smmu_sid;
1312
1313 /* Look for an available entry to hold the mapping */
1314 for (i = 0; i < nr_map; i++) {
1315 u16 bdf_be = cpu_to_be16(map[i].bdf);
1316 u32 val;
1317 u8 hash;
1318
1319 hash = crc8(qcom_pcie_crc8_table, (u8 *)&bdf_be, sizeof(bdf_be),
1320 0);
1321
1322 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1323
1324 /* If the register is already populated, look for next available entry */
1325 while (val) {
1326 u8 current_hash = hash++;
1327 u8 next_mask = 0xff;
1328
1329 /* If NEXT field is NULL then update it with next hash */
1330 if (!(val & next_mask)) {
1331 val |= (u32)hash;
1332 writel(val, bdf_to_sid_base + current_hash * sizeof(u32));
1333 }
1334
1335 val = readl(bdf_to_sid_base + hash * sizeof(u32));
1336 }
1337
1338 /* BDF [31:16] | SID [15:8] | NEXT [7:0] */
1339 val = map[i].bdf << 16 | (map[i].smmu_sid - smmu_sid_base) << 8 | 0;
1340 writel(val, bdf_to_sid_base + hash * sizeof(u32));
1341 }
1342
1343 kfree(map);
1344
1345 return 0;
1346}
1347
1348static int qcom_pcie_host_init(struct pcie_port *pp)
1349{
1350 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1351 struct qcom_pcie *pcie = to_qcom_pcie(pci);
1352 int ret;
1353
1354 qcom_ep_reset_assert(pcie);
1355
1356 ret = pcie->ops->init(pcie);
1357 if (ret)
1358 return ret;
1359
1360 ret = phy_power_on(pcie->phy);
1361 if (ret)
1362 goto err_deinit;
1363
1364 if (pcie->ops->post_init) {
1365 ret = pcie->ops->post_init(pcie);
1366 if (ret)
1367 goto err_disable_phy;
1368 }
1369
1370 qcom_ep_reset_deassert(pcie);
1371
1372 if (pcie->ops->config_sid) {
1373 ret = pcie->ops->config_sid(pcie);
1374 if (ret)
1375 goto err;
1376 }
1377
1378 return 0;
1379
1380err:
1381 qcom_ep_reset_assert(pcie);
1382 if (pcie->ops->post_deinit)
1383 pcie->ops->post_deinit(pcie);
1384err_disable_phy:
1385 phy_power_off(pcie->phy);
1386err_deinit:
1387 pcie->ops->deinit(pcie);
1388
1389 return ret;
1390}
1391
1392static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
1393 .host_init = qcom_pcie_host_init,
1394};
1395
1396/* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1397static const struct qcom_pcie_ops ops_2_1_0 = {
1398 .get_resources = qcom_pcie_get_resources_2_1_0,
1399 .init = qcom_pcie_init_2_1_0,
1400 .deinit = qcom_pcie_deinit_2_1_0,
1401 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1402};
1403
1404/* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1405static const struct qcom_pcie_ops ops_1_0_0 = {
1406 .get_resources = qcom_pcie_get_resources_1_0_0,
1407 .init = qcom_pcie_init_1_0_0,
1408 .deinit = qcom_pcie_deinit_1_0_0,
1409 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
1410};
1411
1412/* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1413static const struct qcom_pcie_ops ops_2_3_2 = {
1414 .get_resources = qcom_pcie_get_resources_2_3_2,
1415 .init = qcom_pcie_init_2_3_2,
1416 .post_init = qcom_pcie_post_init_2_3_2,
1417 .deinit = qcom_pcie_deinit_2_3_2,
1418 .post_deinit = qcom_pcie_post_deinit_2_3_2,
1419 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1420};
1421
1422/* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1423static const struct qcom_pcie_ops ops_2_4_0 = {
1424 .get_resources = qcom_pcie_get_resources_2_4_0,
1425 .init = qcom_pcie_init_2_4_0,
1426 .deinit = qcom_pcie_deinit_2_4_0,
1427 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1428};
1429
1430/* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1431static const struct qcom_pcie_ops ops_2_3_3 = {
1432 .get_resources = qcom_pcie_get_resources_2_3_3,
1433 .init = qcom_pcie_init_2_3_3,
1434 .deinit = qcom_pcie_deinit_2_3_3,
1435 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1436};
1437
1438/* Qcom IP rev.: 2.7.0 Synopsys IP rev.: 4.30a */
1439static const struct qcom_pcie_ops ops_2_7_0 = {
1440 .get_resources = qcom_pcie_get_resources_2_7_0,
1441 .init = qcom_pcie_init_2_7_0,
1442 .deinit = qcom_pcie_deinit_2_7_0,
1443 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1444 .post_init = qcom_pcie_post_init_2_7_0,
1445 .post_deinit = qcom_pcie_post_deinit_2_7_0,
1446};
1447
1448/* Qcom IP rev.: 1.9.0 */
1449static const struct qcom_pcie_ops ops_1_9_0 = {
1450 .get_resources = qcom_pcie_get_resources_2_7_0,
1451 .init = qcom_pcie_init_2_7_0,
1452 .deinit = qcom_pcie_deinit_2_7_0,
1453 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1454 .post_init = qcom_pcie_post_init_2_7_0,
1455 .post_deinit = qcom_pcie_post_deinit_2_7_0,
1456 .config_sid = qcom_pcie_config_sid_sm8250,
1457};
1458
1459static const struct dw_pcie_ops dw_pcie_ops = {
1460 .link_up = qcom_pcie_link_up,
1461 .start_link = qcom_pcie_start_link,
1462};
1463
1464static int qcom_pcie_probe(struct platform_device *pdev)
1465{
1466 struct device *dev = &pdev->dev;
1467 struct pcie_port *pp;
1468 struct dw_pcie *pci;
1469 struct qcom_pcie *pcie;
1470 int ret;
1471
1472 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1473 if (!pcie)
1474 return -ENOMEM;
1475
1476 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1477 if (!pci)
1478 return -ENOMEM;
1479
1480 pm_runtime_enable(dev);
1481 ret = pm_runtime_get_sync(dev);
1482 if (ret < 0)
1483 goto err_pm_runtime_put;
1484
1485 pci->dev = dev;
1486 pci->ops = &dw_pcie_ops;
1487 pp = &pci->pp;
1488
1489 pcie->pci = pci;
1490
1491 pcie->ops = of_device_get_match_data(dev);
1492
1493 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_HIGH);
1494 if (IS_ERR(pcie->reset)) {
1495 ret = PTR_ERR(pcie->reset);
1496 goto err_pm_runtime_put;
1497 }
1498
1499 pcie->parf = devm_platform_ioremap_resource_byname(pdev, "parf");
1500 if (IS_ERR(pcie->parf)) {
1501 ret = PTR_ERR(pcie->parf);
1502 goto err_pm_runtime_put;
1503 }
1504
1505 pcie->elbi = devm_platform_ioremap_resource_byname(pdev, "elbi");
1506 if (IS_ERR(pcie->elbi)) {
1507 ret = PTR_ERR(pcie->elbi);
1508 goto err_pm_runtime_put;
1509 }
1510
1511 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1512 if (IS_ERR(pcie->phy)) {
1513 ret = PTR_ERR(pcie->phy);
1514 goto err_pm_runtime_put;
1515 }
1516
1517 ret = pcie->ops->get_resources(pcie);
1518 if (ret)
1519 goto err_pm_runtime_put;
1520
1521 pp->ops = &qcom_pcie_dw_ops;
1522
1523 ret = phy_init(pcie->phy);
1524 if (ret) {
1525 pm_runtime_disable(&pdev->dev);
1526 goto err_pm_runtime_put;
1527 }
1528
1529 platform_set_drvdata(pdev, pcie);
1530
1531 ret = dw_pcie_host_init(pp);
1532 if (ret) {
1533 dev_err(dev, "cannot initialize host\n");
1534 pm_runtime_disable(&pdev->dev);
1535 goto err_pm_runtime_put;
1536 }
1537
1538 return 0;
1539
1540err_pm_runtime_put:
1541 pm_runtime_put(dev);
1542 pm_runtime_disable(dev);
1543
1544 return ret;
1545}
1546
1547static const struct of_device_id qcom_pcie_match[] = {
1548 { .compatible = "qcom,pcie-apq8084", .data = &ops_1_0_0 },
1549 { .compatible = "qcom,pcie-ipq8064", .data = &ops_2_1_0 },
1550 { .compatible = "qcom,pcie-ipq8064-v2", .data = &ops_2_1_0 },
1551 { .compatible = "qcom,pcie-apq8064", .data = &ops_2_1_0 },
1552 { .compatible = "qcom,pcie-msm8996", .data = &ops_2_3_2 },
1553 { .compatible = "qcom,pcie-ipq8074", .data = &ops_2_3_3 },
1554 { .compatible = "qcom,pcie-ipq4019", .data = &ops_2_4_0 },
1555 { .compatible = "qcom,pcie-qcs404", .data = &ops_2_4_0 },
1556 { .compatible = "qcom,pcie-sdm845", .data = &ops_2_7_0 },
1557 { .compatible = "qcom,pcie-sm8250", .data = &ops_1_9_0 },
1558 { }
1559};
1560
1561static void qcom_fixup_class(struct pci_dev *dev)
1562{
1563 dev->class = PCI_CLASS_BRIDGE_PCI << 8;
1564}
1565DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0101, qcom_fixup_class);
1566DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0104, qcom_fixup_class);
1567DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0106, qcom_fixup_class);
1568DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0107, qcom_fixup_class);
1569DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x0302, qcom_fixup_class);
1570DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1000, qcom_fixup_class);
1571DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_QCOM, 0x1001, qcom_fixup_class);
1572
1573static struct platform_driver qcom_pcie_driver = {
1574 .probe = qcom_pcie_probe,
1575 .driver = {
1576 .name = "qcom-pcie",
1577 .suppress_bind_attrs = true,
1578 .of_match_table = qcom_pcie_match,
1579 },
1580};
1581builtin_platform_driver(qcom_pcie_driver);