blob: a7c12f737690782cee20e635992a4e1a1957b3a4 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Stanimir Varbanov82a82382015-12-18 14:38:57 +02002/*
Paul Gortmakerf9a66602016-08-24 16:57:48 -04003 * Qualcomm PCIe root complex driver
4 *
Stanimir Varbanov82a82382015-12-18 14:38:57 +02005 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
6 * Copyright 2015 Linaro Limited.
7 *
Paul Gortmakerf9a66602016-08-24 16:57:48 -04008 * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
Stanimir Varbanov82a82382015-12-18 14:38:57 +02009 */
10
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/gpio.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/iopoll.h>
17#include <linux/kernel.h>
Paul Gortmakerf9a66602016-08-24 16:57:48 -040018#include <linux/init.h>
Stanimir Varbanov82a82382015-12-18 14:38:57 +020019#include <linux/of_device.h>
20#include <linux/of_gpio.h>
21#include <linux/pci.h>
22#include <linux/platform_device.h>
23#include <linux/phy/phy.h>
24#include <linux/regulator/consumer.h>
25#include <linux/reset.h>
26#include <linux/slab.h>
27#include <linux/types.h>
28
29#include "pcie-designware.h"
30
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +000031#define PCIE20_PARF_SYS_CTRL 0x00
Varadarajan Narayanan5d761172017-08-18 12:59:53 +053032#define MST_WAKEUP_EN BIT(13)
33#define SLV_WAKEUP_EN BIT(12)
34#define MSTR_ACLK_CGC_DIS BIT(10)
35#define SLV_ACLK_CGC_DIS BIT(9)
36#define CORE_CLK_CGC_DIS BIT(6)
37#define AUX_PWR_DET BIT(4)
38#define L23_CLK_RMV_DIS BIT(2)
39#define L1_CLK_RMV_DIS BIT(1)
40
41#define PCIE20_COMMAND_STATUS 0x04
42#define CMD_BME_VAL 0x4
43#define PCIE20_DEVICE_CONTROL2_STATUS2 0x98
44#define PCIE_CAP_CPL_TIMEOUT_DISABLE 0x10
45
Stanimir Varbanov82a82382015-12-18 14:38:57 +020046#define PCIE20_PARF_PHY_CTRL 0x40
47#define PCIE20_PARF_PHY_REFCLK 0x4C
48#define PCIE20_PARF_DBI_BASE_ADDR 0x168
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +000049#define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
50#define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
Stanimir Varbanov82a82382015-12-18 14:38:57 +020051#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +000052#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
53#define PCIE20_PARF_LTSSM 0x1B0
54#define PCIE20_PARF_SID_OFFSET 0x234
55#define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
Stanimir Varbanov82a82382015-12-18 14:38:57 +020056
57#define PCIE20_ELBI_SYS_CTRL 0x04
58#define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
59
Srinivas Kandagatlab8f2a852017-06-29 17:34:55 +020060#define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
61#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
62#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
63#define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
64#define CFG_BRIDGE_SB_INIT BIT(0)
65
Stanimir Varbanov82a82382015-12-18 14:38:57 +020066#define PCIE20_CAP 0x70
Varadarajan Narayanan5d761172017-08-18 12:59:53 +053067#define PCIE20_CAP_LINK_CAPABILITIES (PCIE20_CAP + 0xC)
68#define PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT (BIT(10) | BIT(11))
69#define PCIE20_CAP_LINK_1 (PCIE20_CAP + 0x14)
70#define PCIE_CAP_LINK1_VAL 0x2FD7F
71
72#define PCIE20_PARF_Q2A_FLUSH 0x1AC
73
74#define PCIE20_MISC_CONTROL_1_REG 0x8BC
75#define DBI_RO_WR_EN 1
Stanimir Varbanov82a82382015-12-18 14:38:57 +020076
77#define PERST_DELAY_US 1000
78
Varadarajan Narayanan5d761172017-08-18 12:59:53 +053079#define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
80#define SLV_ADDR_SPACE_SZ 0x10000000
81
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +053082struct qcom_pcie_resources_2_1_0 {
Stanimir Varbanov82a82382015-12-18 14:38:57 +020083 struct clk *iface_clk;
84 struct clk *core_clk;
85 struct clk *phy_clk;
86 struct reset_control *pci_reset;
87 struct reset_control *axi_reset;
88 struct reset_control *ahb_reset;
89 struct reset_control *por_reset;
90 struct reset_control *phy_reset;
91 struct regulator *vdda;
92 struct regulator *vdda_phy;
93 struct regulator *vdda_refclk;
94};
95
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +053096struct qcom_pcie_resources_1_0_0 {
Stanimir Varbanov82a82382015-12-18 14:38:57 +020097 struct clk *iface;
98 struct clk *aux;
99 struct clk *master_bus;
100 struct clk *slave_bus;
101 struct reset_control *core;
102 struct regulator *vdda;
103};
104
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000105#define QCOM_PCIE_2_3_2_MAX_SUPPLY 2
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530106struct qcom_pcie_resources_2_3_2 {
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000107 struct clk *aux_clk;
108 struct clk *master_clk;
109 struct clk *slave_clk;
110 struct clk *cfg_clk;
111 struct clk *pipe_clk;
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000112 struct regulator_bulk_data supplies[QCOM_PCIE_2_3_2_MAX_SUPPLY];
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000113};
114
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530115struct qcom_pcie_resources_2_4_0 {
John Crispin90d52d52017-05-23 15:02:28 -0500116 struct clk *aux_clk;
117 struct clk *master_clk;
118 struct clk *slave_clk;
119 struct reset_control *axi_m_reset;
120 struct reset_control *axi_s_reset;
121 struct reset_control *pipe_reset;
122 struct reset_control *axi_m_vmid_reset;
123 struct reset_control *axi_s_xpu_reset;
124 struct reset_control *parf_reset;
125 struct reset_control *phy_reset;
126 struct reset_control *axi_m_sticky_reset;
127 struct reset_control *pipe_sticky_reset;
128 struct reset_control *pwr_reset;
129 struct reset_control *ahb_reset;
130 struct reset_control *phy_ahb_reset;
131};
132
Varadarajan Narayanan5d761172017-08-18 12:59:53 +0530133struct qcom_pcie_resources_2_3_3 {
134 struct clk *iface;
135 struct clk *axi_m_clk;
136 struct clk *axi_s_clk;
137 struct clk *ahb_clk;
138 struct clk *aux_clk;
139 struct reset_control *rst[7];
140};
141
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200142union qcom_pcie_resources {
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530143 struct qcom_pcie_resources_1_0_0 v1_0_0;
144 struct qcom_pcie_resources_2_1_0 v2_1_0;
145 struct qcom_pcie_resources_2_3_2 v2_3_2;
Varadarajan Narayanan5d761172017-08-18 12:59:53 +0530146 struct qcom_pcie_resources_2_3_3 v2_3_3;
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530147 struct qcom_pcie_resources_2_4_0 v2_4_0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200148};
149
150struct qcom_pcie;
151
152struct qcom_pcie_ops {
153 int (*get_resources)(struct qcom_pcie *pcie);
154 int (*init)(struct qcom_pcie *pcie);
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000155 int (*post_init)(struct qcom_pcie *pcie);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200156 void (*deinit)(struct qcom_pcie *pcie);
Bjorn Andersson71cee8e2017-07-15 23:42:03 -0700157 void (*post_deinit)(struct qcom_pcie *pcie);
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000158 void (*ltssm_enable)(struct qcom_pcie *pcie);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200159};
160
161struct qcom_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530162 struct dw_pcie *pci;
Bjorn Helgaasee053692016-10-06 13:39:37 -0500163 void __iomem *parf; /* DT parf */
164 void __iomem *elbi; /* DT elbi */
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200165 union qcom_pcie_resources res;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200166 struct phy *phy;
167 struct gpio_desc *reset;
Julia Lawall8e64a7c2018-01-02 14:28:00 +0100168 const struct qcom_pcie_ops *ops;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200169};
170
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530171#define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200172
173static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
174{
Fabio Estevama8c20382017-07-16 19:56:38 -0300175 gpiod_set_value_cansleep(pcie->reset, 1);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200176 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
177}
178
179static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
180{
Fabio Estevama8c20382017-07-16 19:56:38 -0300181 gpiod_set_value_cansleep(pcie->reset, 0);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200182 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
183}
184
185static irqreturn_t qcom_pcie_msi_irq_handler(int irq, void *arg)
186{
187 struct pcie_port *pp = arg;
188
189 return dw_handle_msi_irq(pp);
190}
191
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000192static int qcom_pcie_establish_link(struct qcom_pcie *pcie)
193{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530194 struct dw_pcie *pci = pcie->pci;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000195
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530196 if (dw_pcie_link_up(pci))
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000197 return 0;
198
199 /* Enable Link Training state machine */
200 if (pcie->ops->ltssm_enable)
201 pcie->ops->ltssm_enable(pcie);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200202
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530203 return dw_pcie_wait_for_link(pci);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200204}
205
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530206static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500207{
208 u32 val;
209
210 /* enable link training */
211 val = readl(pcie->elbi + PCIE20_ELBI_SYS_CTRL);
212 val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
213 writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
214}
215
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530216static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200217{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530218 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530219 struct dw_pcie *pci = pcie->pci;
220 struct device *dev = pci->dev;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200221
222 res->vdda = devm_regulator_get(dev, "vdda");
223 if (IS_ERR(res->vdda))
224 return PTR_ERR(res->vdda);
225
226 res->vdda_phy = devm_regulator_get(dev, "vdda_phy");
227 if (IS_ERR(res->vdda_phy))
228 return PTR_ERR(res->vdda_phy);
229
230 res->vdda_refclk = devm_regulator_get(dev, "vdda_refclk");
231 if (IS_ERR(res->vdda_refclk))
232 return PTR_ERR(res->vdda_refclk);
233
234 res->iface_clk = devm_clk_get(dev, "iface");
235 if (IS_ERR(res->iface_clk))
236 return PTR_ERR(res->iface_clk);
237
238 res->core_clk = devm_clk_get(dev, "core");
239 if (IS_ERR(res->core_clk))
240 return PTR_ERR(res->core_clk);
241
242 res->phy_clk = devm_clk_get(dev, "phy");
243 if (IS_ERR(res->phy_clk))
244 return PTR_ERR(res->phy_clk);
245
Philipp Zabel244e0002017-07-19 17:25:55 +0200246 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200247 if (IS_ERR(res->pci_reset))
248 return PTR_ERR(res->pci_reset);
249
Philipp Zabel244e0002017-07-19 17:25:55 +0200250 res->axi_reset = devm_reset_control_get_exclusive(dev, "axi");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200251 if (IS_ERR(res->axi_reset))
252 return PTR_ERR(res->axi_reset);
253
Philipp Zabel244e0002017-07-19 17:25:55 +0200254 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200255 if (IS_ERR(res->ahb_reset))
256 return PTR_ERR(res->ahb_reset);
257
Philipp Zabel244e0002017-07-19 17:25:55 +0200258 res->por_reset = devm_reset_control_get_exclusive(dev, "por");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200259 if (IS_ERR(res->por_reset))
260 return PTR_ERR(res->por_reset);
261
Philipp Zabel244e0002017-07-19 17:25:55 +0200262 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
Fengguang Wu11a61a82017-02-04 09:35:32 +0800263 return PTR_ERR_OR_ZERO(res->phy_reset);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200264}
265
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530266static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200267{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530268 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200269
270 reset_control_assert(res->pci_reset);
271 reset_control_assert(res->axi_reset);
272 reset_control_assert(res->ahb_reset);
273 reset_control_assert(res->por_reset);
274 reset_control_assert(res->pci_reset);
275 clk_disable_unprepare(res->iface_clk);
276 clk_disable_unprepare(res->core_clk);
277 clk_disable_unprepare(res->phy_clk);
278 regulator_disable(res->vdda);
279 regulator_disable(res->vdda_phy);
280 regulator_disable(res->vdda_refclk);
281}
282
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530283static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200284{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530285 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530286 struct dw_pcie *pci = pcie->pci;
287 struct device *dev = pci->dev;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200288 u32 val;
289 int ret;
290
291 ret = regulator_enable(res->vdda);
292 if (ret) {
293 dev_err(dev, "cannot enable vdda regulator\n");
294 return ret;
295 }
296
297 ret = regulator_enable(res->vdda_refclk);
298 if (ret) {
299 dev_err(dev, "cannot enable vdda_refclk regulator\n");
300 goto err_refclk;
301 }
302
303 ret = regulator_enable(res->vdda_phy);
304 if (ret) {
305 dev_err(dev, "cannot enable vdda_phy regulator\n");
306 goto err_vdda_phy;
307 }
308
309 ret = reset_control_assert(res->ahb_reset);
310 if (ret) {
311 dev_err(dev, "cannot assert ahb reset\n");
312 goto err_assert_ahb;
313 }
314
315 ret = clk_prepare_enable(res->iface_clk);
316 if (ret) {
317 dev_err(dev, "cannot prepare/enable iface clock\n");
318 goto err_assert_ahb;
319 }
320
321 ret = clk_prepare_enable(res->phy_clk);
322 if (ret) {
323 dev_err(dev, "cannot prepare/enable phy clock\n");
324 goto err_clk_phy;
325 }
326
327 ret = clk_prepare_enable(res->core_clk);
328 if (ret) {
329 dev_err(dev, "cannot prepare/enable core clock\n");
330 goto err_clk_core;
331 }
332
333 ret = reset_control_deassert(res->ahb_reset);
334 if (ret) {
335 dev_err(dev, "cannot deassert ahb reset\n");
336 goto err_deassert_ahb;
337 }
338
339 /* enable PCIe clocks and resets */
340 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
341 val &= ~BIT(0);
342 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
343
344 /* enable external reference clock */
345 val = readl(pcie->parf + PCIE20_PARF_PHY_REFCLK);
346 val |= BIT(16);
347 writel(val, pcie->parf + PCIE20_PARF_PHY_REFCLK);
348
349 ret = reset_control_deassert(res->phy_reset);
350 if (ret) {
351 dev_err(dev, "cannot deassert phy reset\n");
352 return ret;
353 }
354
355 ret = reset_control_deassert(res->pci_reset);
356 if (ret) {
357 dev_err(dev, "cannot deassert pci reset\n");
358 return ret;
359 }
360
361 ret = reset_control_deassert(res->por_reset);
362 if (ret) {
363 dev_err(dev, "cannot deassert por reset\n");
364 return ret;
365 }
366
367 ret = reset_control_deassert(res->axi_reset);
368 if (ret) {
369 dev_err(dev, "cannot deassert axi reset\n");
370 return ret;
371 }
372
373 /* wait for clock acquisition */
374 usleep_range(1000, 1500);
375
Srinivas Kandagatlab8f2a852017-06-29 17:34:55 +0200376
377 /* Set the Max TLP size to 2K, instead of using default of 4K */
378 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
379 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL0);
380 writel(CFG_BRIDGE_SB_INIT,
381 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL1);
382
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200383 return 0;
384
385err_deassert_ahb:
386 clk_disable_unprepare(res->core_clk);
387err_clk_core:
388 clk_disable_unprepare(res->phy_clk);
389err_clk_phy:
390 clk_disable_unprepare(res->iface_clk);
391err_assert_ahb:
392 regulator_disable(res->vdda_phy);
393err_vdda_phy:
394 regulator_disable(res->vdda_refclk);
395err_refclk:
396 regulator_disable(res->vdda);
397
398 return ret;
399}
400
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530401static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500402{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530403 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500404 struct dw_pcie *pci = pcie->pci;
405 struct device *dev = pci->dev;
406
407 res->vdda = devm_regulator_get(dev, "vdda");
408 if (IS_ERR(res->vdda))
409 return PTR_ERR(res->vdda);
410
411 res->iface = devm_clk_get(dev, "iface");
412 if (IS_ERR(res->iface))
413 return PTR_ERR(res->iface);
414
415 res->aux = devm_clk_get(dev, "aux");
416 if (IS_ERR(res->aux))
417 return PTR_ERR(res->aux);
418
419 res->master_bus = devm_clk_get(dev, "master_bus");
420 if (IS_ERR(res->master_bus))
421 return PTR_ERR(res->master_bus);
422
423 res->slave_bus = devm_clk_get(dev, "slave_bus");
424 if (IS_ERR(res->slave_bus))
425 return PTR_ERR(res->slave_bus);
426
Philipp Zabel244e0002017-07-19 17:25:55 +0200427 res->core = devm_reset_control_get_exclusive(dev, "core");
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500428 return PTR_ERR_OR_ZERO(res->core);
429}
430
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530431static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200432{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530433 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200434
435 reset_control_assert(res->core);
436 clk_disable_unprepare(res->slave_bus);
437 clk_disable_unprepare(res->master_bus);
438 clk_disable_unprepare(res->iface);
439 clk_disable_unprepare(res->aux);
440 regulator_disable(res->vdda);
441}
442
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530443static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200444{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530445 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530446 struct dw_pcie *pci = pcie->pci;
447 struct device *dev = pci->dev;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200448 int ret;
449
450 ret = reset_control_deassert(res->core);
451 if (ret) {
452 dev_err(dev, "cannot deassert core reset\n");
453 return ret;
454 }
455
456 ret = clk_prepare_enable(res->aux);
457 if (ret) {
458 dev_err(dev, "cannot prepare/enable aux clock\n");
459 goto err_res;
460 }
461
462 ret = clk_prepare_enable(res->iface);
463 if (ret) {
464 dev_err(dev, "cannot prepare/enable iface clock\n");
465 goto err_aux;
466 }
467
468 ret = clk_prepare_enable(res->master_bus);
469 if (ret) {
470 dev_err(dev, "cannot prepare/enable master_bus clock\n");
471 goto err_iface;
472 }
473
474 ret = clk_prepare_enable(res->slave_bus);
475 if (ret) {
476 dev_err(dev, "cannot prepare/enable slave_bus clock\n");
477 goto err_master;
478 }
479
480 ret = regulator_enable(res->vdda);
481 if (ret) {
482 dev_err(dev, "cannot enable vdda regulator\n");
483 goto err_slave;
484 }
485
486 /* change DBI base address */
487 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
488
489 if (IS_ENABLED(CONFIG_PCI_MSI)) {
490 u32 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
491
492 val |= BIT(31);
493 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
494 }
495
496 return 0;
497err_slave:
498 clk_disable_unprepare(res->slave_bus);
499err_master:
500 clk_disable_unprepare(res->master_bus);
501err_iface:
502 clk_disable_unprepare(res->iface);
503err_aux:
504 clk_disable_unprepare(res->aux);
505err_res:
506 reset_control_assert(res->core);
507
508 return ret;
509}
510
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530511static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500512{
513 u32 val;
514
515 /* enable link training */
516 val = readl(pcie->parf + PCIE20_PARF_LTSSM);
517 val |= BIT(8);
518 writel(val, pcie->parf + PCIE20_PARF_LTSSM);
519}
520
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530521static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000522{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530523 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530524 struct dw_pcie *pci = pcie->pci;
525 struct device *dev = pci->dev;
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000526 int ret;
527
528 res->supplies[0].supply = "vdda";
529 res->supplies[1].supply = "vddpe-3v3";
530 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(res->supplies),
531 res->supplies);
532 if (ret)
533 return ret;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000534
535 res->aux_clk = devm_clk_get(dev, "aux");
536 if (IS_ERR(res->aux_clk))
537 return PTR_ERR(res->aux_clk);
538
539 res->cfg_clk = devm_clk_get(dev, "cfg");
540 if (IS_ERR(res->cfg_clk))
541 return PTR_ERR(res->cfg_clk);
542
543 res->master_clk = devm_clk_get(dev, "bus_master");
544 if (IS_ERR(res->master_clk))
545 return PTR_ERR(res->master_clk);
546
547 res->slave_clk = devm_clk_get(dev, "bus_slave");
548 if (IS_ERR(res->slave_clk))
549 return PTR_ERR(res->slave_clk);
550
551 res->pipe_clk = devm_clk_get(dev, "pipe");
Fengguang Wu11a61a82017-02-04 09:35:32 +0800552 return PTR_ERR_OR_ZERO(res->pipe_clk);
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000553}
554
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530555static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500556{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530557 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500558
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500559 clk_disable_unprepare(res->slave_clk);
560 clk_disable_unprepare(res->master_clk);
561 clk_disable_unprepare(res->cfg_clk);
562 clk_disable_unprepare(res->aux_clk);
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000563
564 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500565}
566
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530567static void qcom_pcie_post_deinit_2_3_2(struct qcom_pcie *pcie)
Bjorn Andersson71cee8e2017-07-15 23:42:03 -0700568{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530569 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Bjorn Andersson71cee8e2017-07-15 23:42:03 -0700570
571 clk_disable_unprepare(res->pipe_clk);
572}
573
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530574static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000575{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530576 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530577 struct dw_pcie *pci = pcie->pci;
578 struct device *dev = pci->dev;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000579 u32 val;
580 int ret;
581
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000582 ret = regulator_bulk_enable(ARRAY_SIZE(res->supplies), res->supplies);
583 if (ret < 0) {
584 dev_err(dev, "cannot enable regulators\n");
585 return ret;
586 }
587
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000588 ret = clk_prepare_enable(res->aux_clk);
589 if (ret) {
590 dev_err(dev, "cannot prepare/enable aux clock\n");
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000591 goto err_aux_clk;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000592 }
593
594 ret = clk_prepare_enable(res->cfg_clk);
595 if (ret) {
596 dev_err(dev, "cannot prepare/enable cfg clock\n");
597 goto err_cfg_clk;
598 }
599
600 ret = clk_prepare_enable(res->master_clk);
601 if (ret) {
602 dev_err(dev, "cannot prepare/enable master clock\n");
603 goto err_master_clk;
604 }
605
606 ret = clk_prepare_enable(res->slave_clk);
607 if (ret) {
608 dev_err(dev, "cannot prepare/enable slave clock\n");
609 goto err_slave_clk;
610 }
611
612 /* enable PCIe clocks and resets */
613 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
614 val &= ~BIT(0);
615 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
616
617 /* change DBI base address */
618 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
619
620 /* MAC PHY_POWERDOWN MUX DISABLE */
621 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
622 val &= ~BIT(29);
623 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
624
625 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
626 val |= BIT(4);
627 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
628
629 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
630 val |= BIT(31);
631 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
632
633 return 0;
634
635err_slave_clk:
636 clk_disable_unprepare(res->master_clk);
637err_master_clk:
638 clk_disable_unprepare(res->cfg_clk);
639err_cfg_clk:
640 clk_disable_unprepare(res->aux_clk);
641
Srinivas Kandagatlaf625b1a2018-02-15 13:22:48 +0000642err_aux_clk:
643 regulator_bulk_disable(ARRAY_SIZE(res->supplies), res->supplies);
644
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000645 return ret;
646}
647
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530648static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000649{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530650 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530651 struct dw_pcie *pci = pcie->pci;
652 struct device *dev = pci->dev;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000653 int ret;
654
655 ret = clk_prepare_enable(res->pipe_clk);
656 if (ret) {
657 dev_err(dev, "cannot prepare/enable pipe clock\n");
658 return ret;
659 }
660
661 return 0;
662}
663
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530664static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
John Crispin90d52d52017-05-23 15:02:28 -0500665{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530666 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
John Crispin90d52d52017-05-23 15:02:28 -0500667 struct dw_pcie *pci = pcie->pci;
668 struct device *dev = pci->dev;
669
670 res->aux_clk = devm_clk_get(dev, "aux");
671 if (IS_ERR(res->aux_clk))
672 return PTR_ERR(res->aux_clk);
673
674 res->master_clk = devm_clk_get(dev, "master_bus");
675 if (IS_ERR(res->master_clk))
676 return PTR_ERR(res->master_clk);
677
678 res->slave_clk = devm_clk_get(dev, "slave_bus");
679 if (IS_ERR(res->slave_clk))
680 return PTR_ERR(res->slave_clk);
681
Philipp Zabel244e0002017-07-19 17:25:55 +0200682 res->axi_m_reset = devm_reset_control_get_exclusive(dev, "axi_m");
John Crispin90d52d52017-05-23 15:02:28 -0500683 if (IS_ERR(res->axi_m_reset))
684 return PTR_ERR(res->axi_m_reset);
685
Philipp Zabel244e0002017-07-19 17:25:55 +0200686 res->axi_s_reset = devm_reset_control_get_exclusive(dev, "axi_s");
John Crispin90d52d52017-05-23 15:02:28 -0500687 if (IS_ERR(res->axi_s_reset))
688 return PTR_ERR(res->axi_s_reset);
689
Philipp Zabel244e0002017-07-19 17:25:55 +0200690 res->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
John Crispin90d52d52017-05-23 15:02:28 -0500691 if (IS_ERR(res->pipe_reset))
692 return PTR_ERR(res->pipe_reset);
693
Philipp Zabel244e0002017-07-19 17:25:55 +0200694 res->axi_m_vmid_reset = devm_reset_control_get_exclusive(dev,
695 "axi_m_vmid");
John Crispin90d52d52017-05-23 15:02:28 -0500696 if (IS_ERR(res->axi_m_vmid_reset))
697 return PTR_ERR(res->axi_m_vmid_reset);
698
Philipp Zabel244e0002017-07-19 17:25:55 +0200699 res->axi_s_xpu_reset = devm_reset_control_get_exclusive(dev,
700 "axi_s_xpu");
John Crispin90d52d52017-05-23 15:02:28 -0500701 if (IS_ERR(res->axi_s_xpu_reset))
702 return PTR_ERR(res->axi_s_xpu_reset);
703
Philipp Zabel244e0002017-07-19 17:25:55 +0200704 res->parf_reset = devm_reset_control_get_exclusive(dev, "parf");
John Crispin90d52d52017-05-23 15:02:28 -0500705 if (IS_ERR(res->parf_reset))
706 return PTR_ERR(res->parf_reset);
707
Philipp Zabel244e0002017-07-19 17:25:55 +0200708 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
John Crispin90d52d52017-05-23 15:02:28 -0500709 if (IS_ERR(res->phy_reset))
710 return PTR_ERR(res->phy_reset);
711
Philipp Zabel244e0002017-07-19 17:25:55 +0200712 res->axi_m_sticky_reset = devm_reset_control_get_exclusive(dev,
713 "axi_m_sticky");
John Crispin90d52d52017-05-23 15:02:28 -0500714 if (IS_ERR(res->axi_m_sticky_reset))
715 return PTR_ERR(res->axi_m_sticky_reset);
716
Philipp Zabel244e0002017-07-19 17:25:55 +0200717 res->pipe_sticky_reset = devm_reset_control_get_exclusive(dev,
718 "pipe_sticky");
John Crispin90d52d52017-05-23 15:02:28 -0500719 if (IS_ERR(res->pipe_sticky_reset))
720 return PTR_ERR(res->pipe_sticky_reset);
721
Philipp Zabel244e0002017-07-19 17:25:55 +0200722 res->pwr_reset = devm_reset_control_get_exclusive(dev, "pwr");
John Crispin90d52d52017-05-23 15:02:28 -0500723 if (IS_ERR(res->pwr_reset))
724 return PTR_ERR(res->pwr_reset);
725
Philipp Zabel244e0002017-07-19 17:25:55 +0200726 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
John Crispin90d52d52017-05-23 15:02:28 -0500727 if (IS_ERR(res->ahb_reset))
728 return PTR_ERR(res->ahb_reset);
729
Philipp Zabel244e0002017-07-19 17:25:55 +0200730 res->phy_ahb_reset = devm_reset_control_get_exclusive(dev, "phy_ahb");
John Crispin90d52d52017-05-23 15:02:28 -0500731 if (IS_ERR(res->phy_ahb_reset))
732 return PTR_ERR(res->phy_ahb_reset);
733
734 return 0;
735}
736
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530737static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
John Crispin90d52d52017-05-23 15:02:28 -0500738{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530739 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
John Crispin90d52d52017-05-23 15:02:28 -0500740
741 reset_control_assert(res->axi_m_reset);
742 reset_control_assert(res->axi_s_reset);
743 reset_control_assert(res->pipe_reset);
744 reset_control_assert(res->pipe_sticky_reset);
745 reset_control_assert(res->phy_reset);
746 reset_control_assert(res->phy_ahb_reset);
747 reset_control_assert(res->axi_m_sticky_reset);
748 reset_control_assert(res->pwr_reset);
749 reset_control_assert(res->ahb_reset);
750 clk_disable_unprepare(res->aux_clk);
751 clk_disable_unprepare(res->master_clk);
752 clk_disable_unprepare(res->slave_clk);
753}
754
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530755static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
John Crispin90d52d52017-05-23 15:02:28 -0500756{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530757 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
John Crispin90d52d52017-05-23 15:02:28 -0500758 struct dw_pcie *pci = pcie->pci;
759 struct device *dev = pci->dev;
760 u32 val;
761 int ret;
762
763 ret = reset_control_assert(res->axi_m_reset);
764 if (ret) {
765 dev_err(dev, "cannot assert axi master reset\n");
766 return ret;
767 }
768
769 ret = reset_control_assert(res->axi_s_reset);
770 if (ret) {
Colin Ian King7a5966e2017-05-31 06:34:14 +0100771 dev_err(dev, "cannot assert axi slave reset\n");
John Crispin90d52d52017-05-23 15:02:28 -0500772 return ret;
773 }
774
775 usleep_range(10000, 12000);
776
777 ret = reset_control_assert(res->pipe_reset);
778 if (ret) {
779 dev_err(dev, "cannot assert pipe reset\n");
780 return ret;
781 }
782
783 ret = reset_control_assert(res->pipe_sticky_reset);
784 if (ret) {
785 dev_err(dev, "cannot assert pipe sticky reset\n");
786 return ret;
787 }
788
789 ret = reset_control_assert(res->phy_reset);
790 if (ret) {
791 dev_err(dev, "cannot assert phy reset\n");
792 return ret;
793 }
794
795 ret = reset_control_assert(res->phy_ahb_reset);
796 if (ret) {
797 dev_err(dev, "cannot assert phy ahb reset\n");
798 return ret;
799 }
800
801 usleep_range(10000, 12000);
802
803 ret = reset_control_assert(res->axi_m_sticky_reset);
804 if (ret) {
805 dev_err(dev, "cannot assert axi master sticky reset\n");
806 return ret;
807 }
808
809 ret = reset_control_assert(res->pwr_reset);
810 if (ret) {
811 dev_err(dev, "cannot assert power reset\n");
812 return ret;
813 }
814
815 ret = reset_control_assert(res->ahb_reset);
816 if (ret) {
817 dev_err(dev, "cannot assert ahb reset\n");
818 return ret;
819 }
820
821 usleep_range(10000, 12000);
822
823 ret = reset_control_deassert(res->phy_ahb_reset);
824 if (ret) {
825 dev_err(dev, "cannot deassert phy ahb reset\n");
826 return ret;
827 }
828
829 ret = reset_control_deassert(res->phy_reset);
830 if (ret) {
831 dev_err(dev, "cannot deassert phy reset\n");
832 goto err_rst_phy;
833 }
834
835 ret = reset_control_deassert(res->pipe_reset);
836 if (ret) {
837 dev_err(dev, "cannot deassert pipe reset\n");
838 goto err_rst_pipe;
839 }
840
841 ret = reset_control_deassert(res->pipe_sticky_reset);
842 if (ret) {
843 dev_err(dev, "cannot deassert pipe sticky reset\n");
844 goto err_rst_pipe_sticky;
845 }
846
847 usleep_range(10000, 12000);
848
849 ret = reset_control_deassert(res->axi_m_reset);
850 if (ret) {
851 dev_err(dev, "cannot deassert axi master reset\n");
852 goto err_rst_axi_m;
853 }
854
855 ret = reset_control_deassert(res->axi_m_sticky_reset);
856 if (ret) {
857 dev_err(dev, "cannot deassert axi master sticky reset\n");
858 goto err_rst_axi_m_sticky;
859 }
860
861 ret = reset_control_deassert(res->axi_s_reset);
862 if (ret) {
863 dev_err(dev, "cannot deassert axi slave reset\n");
864 goto err_rst_axi_s;
865 }
866
867 ret = reset_control_deassert(res->pwr_reset);
868 if (ret) {
869 dev_err(dev, "cannot deassert power reset\n");
870 goto err_rst_pwr;
871 }
872
873 ret = reset_control_deassert(res->ahb_reset);
874 if (ret) {
875 dev_err(dev, "cannot deassert ahb reset\n");
876 goto err_rst_ahb;
877 }
878
879 usleep_range(10000, 12000);
880
881 ret = clk_prepare_enable(res->aux_clk);
882 if (ret) {
883 dev_err(dev, "cannot prepare/enable iface clock\n");
884 goto err_clk_aux;
885 }
886
887 ret = clk_prepare_enable(res->master_clk);
888 if (ret) {
889 dev_err(dev, "cannot prepare/enable core clock\n");
890 goto err_clk_axi_m;
891 }
892
893 ret = clk_prepare_enable(res->slave_clk);
894 if (ret) {
895 dev_err(dev, "cannot prepare/enable phy clock\n");
896 goto err_clk_axi_s;
897 }
898
899 /* enable PCIe clocks and resets */
900 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
901 val &= !BIT(0);
902 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
903
904 /* change DBI base address */
905 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
906
907 /* MAC PHY_POWERDOWN MUX DISABLE */
908 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
909 val &= ~BIT(29);
910 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
911
912 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
913 val |= BIT(4);
914 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
915
916 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
917 val |= BIT(31);
918 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
919
920 return 0;
921
922err_clk_axi_s:
923 clk_disable_unprepare(res->master_clk);
924err_clk_axi_m:
925 clk_disable_unprepare(res->aux_clk);
926err_clk_aux:
927 reset_control_assert(res->ahb_reset);
928err_rst_ahb:
929 reset_control_assert(res->pwr_reset);
930err_rst_pwr:
931 reset_control_assert(res->axi_s_reset);
932err_rst_axi_s:
933 reset_control_assert(res->axi_m_sticky_reset);
934err_rst_axi_m_sticky:
935 reset_control_assert(res->axi_m_reset);
936err_rst_axi_m:
937 reset_control_assert(res->pipe_sticky_reset);
938err_rst_pipe_sticky:
939 reset_control_assert(res->pipe_reset);
940err_rst_pipe:
941 reset_control_assert(res->phy_reset);
942err_rst_phy:
943 reset_control_assert(res->phy_ahb_reset);
944 return ret;
945}
946
Varadarajan Narayanan5d761172017-08-18 12:59:53 +0530947static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
948{
949 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
950 struct dw_pcie *pci = pcie->pci;
951 struct device *dev = pci->dev;
952 int i;
953 const char *rst_names[] = { "axi_m", "axi_s", "pipe",
954 "axi_m_sticky", "sticky",
955 "ahb", "sleep", };
956
957 res->iface = devm_clk_get(dev, "iface");
958 if (IS_ERR(res->iface))
959 return PTR_ERR(res->iface);
960
961 res->axi_m_clk = devm_clk_get(dev, "axi_m");
962 if (IS_ERR(res->axi_m_clk))
963 return PTR_ERR(res->axi_m_clk);
964
965 res->axi_s_clk = devm_clk_get(dev, "axi_s");
966 if (IS_ERR(res->axi_s_clk))
967 return PTR_ERR(res->axi_s_clk);
968
969 res->ahb_clk = devm_clk_get(dev, "ahb");
970 if (IS_ERR(res->ahb_clk))
971 return PTR_ERR(res->ahb_clk);
972
973 res->aux_clk = devm_clk_get(dev, "aux");
974 if (IS_ERR(res->aux_clk))
975 return PTR_ERR(res->aux_clk);
976
977 for (i = 0; i < ARRAY_SIZE(rst_names); i++) {
978 res->rst[i] = devm_reset_control_get(dev, rst_names[i]);
979 if (IS_ERR(res->rst[i]))
980 return PTR_ERR(res->rst[i]);
981 }
982
983 return 0;
984}
985
986static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
987{
988 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
989
990 clk_disable_unprepare(res->iface);
991 clk_disable_unprepare(res->axi_m_clk);
992 clk_disable_unprepare(res->axi_s_clk);
993 clk_disable_unprepare(res->ahb_clk);
994 clk_disable_unprepare(res->aux_clk);
995}
996
997static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
998{
999 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
1000 struct dw_pcie *pci = pcie->pci;
1001 struct device *dev = pci->dev;
1002 int i, ret;
1003 u32 val;
1004
1005 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1006 ret = reset_control_assert(res->rst[i]);
1007 if (ret) {
1008 dev_err(dev, "reset #%d assert failed (%d)\n", i, ret);
1009 return ret;
1010 }
1011 }
1012
1013 usleep_range(2000, 2500);
1014
1015 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1016 ret = reset_control_deassert(res->rst[i]);
1017 if (ret) {
1018 dev_err(dev, "reset #%d deassert failed (%d)\n", i,
1019 ret);
1020 return ret;
1021 }
1022 }
1023
1024 /*
1025 * Don't have a way to see if the reset has completed.
1026 * Wait for some time.
1027 */
1028 usleep_range(2000, 2500);
1029
1030 ret = clk_prepare_enable(res->iface);
1031 if (ret) {
1032 dev_err(dev, "cannot prepare/enable core clock\n");
1033 goto err_clk_iface;
1034 }
1035
1036 ret = clk_prepare_enable(res->axi_m_clk);
1037 if (ret) {
1038 dev_err(dev, "cannot prepare/enable core clock\n");
1039 goto err_clk_axi_m;
1040 }
1041
1042 ret = clk_prepare_enable(res->axi_s_clk);
1043 if (ret) {
1044 dev_err(dev, "cannot prepare/enable axi slave clock\n");
1045 goto err_clk_axi_s;
1046 }
1047
1048 ret = clk_prepare_enable(res->ahb_clk);
1049 if (ret) {
1050 dev_err(dev, "cannot prepare/enable ahb clock\n");
1051 goto err_clk_ahb;
1052 }
1053
1054 ret = clk_prepare_enable(res->aux_clk);
1055 if (ret) {
1056 dev_err(dev, "cannot prepare/enable aux clock\n");
1057 goto err_clk_aux;
1058 }
1059
1060 writel(SLV_ADDR_SPACE_SZ,
1061 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1062
1063 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1064 val &= ~BIT(0);
1065 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1066
1067 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1068
1069 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
1070 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1071 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1072 pcie->parf + PCIE20_PARF_SYS_CTRL);
1073 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1074
1075 writel(CMD_BME_VAL, pci->dbi_base + PCIE20_COMMAND_STATUS);
1076 writel(DBI_RO_WR_EN, pci->dbi_base + PCIE20_MISC_CONTROL_1_REG);
1077 writel(PCIE_CAP_LINK1_VAL, pci->dbi_base + PCIE20_CAP_LINK_1);
1078
1079 val = readl(pci->dbi_base + PCIE20_CAP_LINK_CAPABILITIES);
1080 val &= ~PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT;
1081 writel(val, pci->dbi_base + PCIE20_CAP_LINK_CAPABILITIES);
1082
1083 writel(PCIE_CAP_CPL_TIMEOUT_DISABLE, pci->dbi_base +
1084 PCIE20_DEVICE_CONTROL2_STATUS2);
1085
1086 return 0;
1087
1088err_clk_aux:
1089 clk_disable_unprepare(res->ahb_clk);
1090err_clk_ahb:
1091 clk_disable_unprepare(res->axi_s_clk);
1092err_clk_axi_s:
1093 clk_disable_unprepare(res->axi_m_clk);
1094err_clk_axi_m:
1095 clk_disable_unprepare(res->iface);
1096err_clk_iface:
1097 /*
1098 * Not checking for failure, will anyway return
1099 * the original failure in 'ret'.
1100 */
1101 for (i = 0; i < ARRAY_SIZE(res->rst); i++)
1102 reset_control_assert(res->rst[i]);
1103
1104 return ret;
1105}
1106
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301107static int qcom_pcie_link_up(struct dw_pcie *pci)
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001108{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301109 u16 val = readw(pci->dbi_base + PCIE20_CAP + PCI_EXP_LNKSTA);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001110
1111 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1112}
1113
Bjorn Andersson4a301762017-07-15 23:39:45 -07001114static int qcom_pcie_host_init(struct pcie_port *pp)
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001115{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301116 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1117 struct qcom_pcie *pcie = to_qcom_pcie(pci);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001118 int ret;
1119
1120 qcom_ep_reset_assert(pcie);
1121
1122 ret = pcie->ops->init(pcie);
1123 if (ret)
Bjorn Andersson89539f02017-07-15 23:41:53 -07001124 return ret;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001125
1126 ret = phy_power_on(pcie->phy);
1127 if (ret)
1128 goto err_deinit;
1129
Bjorn Andersson71cee8e2017-07-15 23:42:03 -07001130 if (pcie->ops->post_init) {
1131 ret = pcie->ops->post_init(pcie);
1132 if (ret)
1133 goto err_disable_phy;
1134 }
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +00001135
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001136 dw_pcie_setup_rc(pp);
1137
1138 if (IS_ENABLED(CONFIG_PCI_MSI))
1139 dw_pcie_msi_init(pp);
1140
1141 qcom_ep_reset_deassert(pcie);
1142
1143 ret = qcom_pcie_establish_link(pcie);
1144 if (ret)
1145 goto err;
1146
Bjorn Andersson4a301762017-07-15 23:39:45 -07001147 return 0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001148err:
1149 qcom_ep_reset_assert(pcie);
Bjorn Andersson71cee8e2017-07-15 23:42:03 -07001150 if (pcie->ops->post_deinit)
1151 pcie->ops->post_deinit(pcie);
1152err_disable_phy:
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001153 phy_power_off(pcie->phy);
1154err_deinit:
1155 pcie->ops->deinit(pcie);
Bjorn Andersson4a301762017-07-15 23:39:45 -07001156
1157 return ret;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001158}
1159
1160static int qcom_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
1161 u32 *val)
1162{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301163 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1164
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001165 /* the device class is not reported correctly from the register */
1166 if (where == PCI_CLASS_REVISION && size == 4) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301167 *val = readl(pci->dbi_base + PCI_CLASS_REVISION);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001168 *val &= 0xff; /* keep revision id */
1169 *val |= PCI_CLASS_BRIDGE_PCI << 16;
1170 return PCIBIOS_SUCCESSFUL;
1171 }
1172
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301173 return dw_pcie_read(pci->dbi_base + where, size, val);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001174}
1175
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +08001176static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001177 .host_init = qcom_pcie_host_init,
1178 .rd_own_conf = qcom_pcie_rd_own_conf,
1179};
1180
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301181/* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1182static const struct qcom_pcie_ops ops_2_1_0 = {
1183 .get_resources = qcom_pcie_get_resources_2_1_0,
1184 .init = qcom_pcie_init_2_1_0,
1185 .deinit = qcom_pcie_deinit_2_1_0,
1186 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001187};
1188
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301189/* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1190static const struct qcom_pcie_ops ops_1_0_0 = {
1191 .get_resources = qcom_pcie_get_resources_1_0_0,
1192 .init = qcom_pcie_init_1_0_0,
1193 .deinit = qcom_pcie_deinit_1_0_0,
1194 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +00001195};
1196
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301197/* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1198static const struct qcom_pcie_ops ops_2_3_2 = {
1199 .get_resources = qcom_pcie_get_resources_2_3_2,
1200 .init = qcom_pcie_init_2_3_2,
1201 .post_init = qcom_pcie_post_init_2_3_2,
1202 .deinit = qcom_pcie_deinit_2_3_2,
1203 .post_deinit = qcom_pcie_post_deinit_2_3_2,
1204 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1205};
1206
1207/* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1208static const struct qcom_pcie_ops ops_2_4_0 = {
1209 .get_resources = qcom_pcie_get_resources_2_4_0,
1210 .init = qcom_pcie_init_2_4_0,
1211 .deinit = qcom_pcie_deinit_2_4_0,
1212 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001213};
1214
Varadarajan Narayanan5d761172017-08-18 12:59:53 +05301215/* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1216static const struct qcom_pcie_ops ops_2_3_3 = {
1217 .get_resources = qcom_pcie_get_resources_2_3_3,
1218 .init = qcom_pcie_init_2_3_3,
1219 .deinit = qcom_pcie_deinit_2_3_3,
1220 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1221};
1222
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301223static const struct dw_pcie_ops dw_pcie_ops = {
1224 .link_up = qcom_pcie_link_up,
1225};
1226
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001227static int qcom_pcie_probe(struct platform_device *pdev)
1228{
1229 struct device *dev = &pdev->dev;
1230 struct resource *res;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001231 struct pcie_port *pp;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301232 struct dw_pcie *pci;
1233 struct qcom_pcie *pcie;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001234 int ret;
1235
1236 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1237 if (!pcie)
1238 return -ENOMEM;
1239
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301240 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1241 if (!pci)
1242 return -ENOMEM;
1243
1244 pci->dev = dev;
1245 pci->ops = &dw_pcie_ops;
1246 pp = &pci->pp;
1247
Guenter Roeckc0464062017-02-25 02:08:12 -08001248 pcie->pci = pci;
1249
Julia Lawall8e64a7c2018-01-02 14:28:00 +01001250 pcie->ops = of_device_get_match_data(dev);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001251
1252 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_LOW);
1253 if (IS_ERR(pcie->reset))
1254 return PTR_ERR(pcie->reset);
1255
1256 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "parf");
1257 pcie->parf = devm_ioremap_resource(dev, res);
1258 if (IS_ERR(pcie->parf))
1259 return PTR_ERR(pcie->parf);
1260
1261 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
Lorenzo Pieralisi10c736f2017-04-19 17:49:01 +01001262 pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301263 if (IS_ERR(pci->dbi_base))
1264 return PTR_ERR(pci->dbi_base);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001265
1266 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "elbi");
1267 pcie->elbi = devm_ioremap_resource(dev, res);
1268 if (IS_ERR(pcie->elbi))
1269 return PTR_ERR(pcie->elbi);
1270
1271 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1272 if (IS_ERR(pcie->phy))
1273 return PTR_ERR(pcie->phy);
1274
1275 ret = pcie->ops->get_resources(pcie);
1276 if (ret)
1277 return ret;
1278
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001279 pp->root_bus_nr = -1;
1280 pp->ops = &qcom_pcie_dw_ops;
1281
1282 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1283 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
1284 if (pp->msi_irq < 0)
1285 return pp->msi_irq;
1286
1287 ret = devm_request_irq(dev, pp->msi_irq,
1288 qcom_pcie_msi_irq_handler,
Jisheng Zhang3eefa792017-04-20 18:27:18 +08001289 IRQF_SHARED | IRQF_NO_THREAD,
1290 "qcom-pcie-msi", pp);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001291 if (ret) {
1292 dev_err(dev, "cannot request msi irq\n");
1293 return ret;
1294 }
1295 }
1296
1297 ret = phy_init(pcie->phy);
1298 if (ret)
1299 return ret;
1300
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +05301301 platform_set_drvdata(pdev, pcie);
1302
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001303 ret = dw_pcie_host_init(pp);
1304 if (ret) {
1305 dev_err(dev, "cannot initialize host\n");
1306 return ret;
1307 }
1308
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001309 return 0;
1310}
1311
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001312static const struct of_device_id qcom_pcie_match[] = {
Varadarajan Narayanan5d761172017-08-18 12:59:53 +05301313 { .compatible = "qcom,pcie-apq8084", .data = &ops_1_0_0 },
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301314 { .compatible = "qcom,pcie-ipq8064", .data = &ops_2_1_0 },
1315 { .compatible = "qcom,pcie-apq8064", .data = &ops_2_1_0 },
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301316 { .compatible = "qcom,pcie-msm8996", .data = &ops_2_3_2 },
Varadarajan Narayanan5d761172017-08-18 12:59:53 +05301317 { .compatible = "qcom,pcie-ipq8074", .data = &ops_2_3_3 },
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301318 { .compatible = "qcom,pcie-ipq4019", .data = &ops_2_4_0 },
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001319 { }
1320};
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001321
1322static struct platform_driver qcom_pcie_driver = {
1323 .probe = qcom_pcie_probe,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001324 .driver = {
1325 .name = "qcom-pcie",
Paul Gortmakerf9a66602016-08-24 16:57:48 -04001326 .suppress_bind_attrs = true,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001327 .of_match_table = qcom_pcie_match,
1328 },
1329};
Paul Gortmakerf9a66602016-08-24 16:57:48 -04001330builtin_platform_driver(qcom_pcie_driver);