blob: ce7ba5b7552acf301dc967e65e85569e87854fc2 [file] [log] [blame]
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001/*
Paul Gortmakerf9a66602016-08-24 16:57:48 -04002 * Qualcomm PCIe root complex driver
3 *
Stanimir Varbanov82a82382015-12-18 14:38:57 +02004 * Copyright (c) 2014-2015, The Linux Foundation. All rights reserved.
5 * Copyright 2015 Linaro Limited.
6 *
Paul Gortmakerf9a66602016-08-24 16:57:48 -04007 * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
8 *
Stanimir Varbanov82a82382015-12-18 14:38:57 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 and
11 * only version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19#include <linux/clk.h>
20#include <linux/delay.h>
21#include <linux/gpio.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/iopoll.h>
25#include <linux/kernel.h>
Paul Gortmakerf9a66602016-08-24 16:57:48 -040026#include <linux/init.h>
Stanimir Varbanov82a82382015-12-18 14:38:57 +020027#include <linux/of_device.h>
28#include <linux/of_gpio.h>
29#include <linux/pci.h>
30#include <linux/platform_device.h>
31#include <linux/phy/phy.h>
32#include <linux/regulator/consumer.h>
33#include <linux/reset.h>
34#include <linux/slab.h>
35#include <linux/types.h>
36
37#include "pcie-designware.h"
38
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +000039#define PCIE20_PARF_SYS_CTRL 0x00
Varadarajan Narayanan5d761172017-08-18 12:59:53 +053040#define MST_WAKEUP_EN BIT(13)
41#define SLV_WAKEUP_EN BIT(12)
42#define MSTR_ACLK_CGC_DIS BIT(10)
43#define SLV_ACLK_CGC_DIS BIT(9)
44#define CORE_CLK_CGC_DIS BIT(6)
45#define AUX_PWR_DET BIT(4)
46#define L23_CLK_RMV_DIS BIT(2)
47#define L1_CLK_RMV_DIS BIT(1)
48
49#define PCIE20_COMMAND_STATUS 0x04
50#define CMD_BME_VAL 0x4
51#define PCIE20_DEVICE_CONTROL2_STATUS2 0x98
52#define PCIE_CAP_CPL_TIMEOUT_DISABLE 0x10
53
Stanimir Varbanov82a82382015-12-18 14:38:57 +020054#define PCIE20_PARF_PHY_CTRL 0x40
55#define PCIE20_PARF_PHY_REFCLK 0x4C
56#define PCIE20_PARF_DBI_BASE_ADDR 0x168
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +000057#define PCIE20_PARF_SLV_ADDR_SPACE_SIZE 0x16C
58#define PCIE20_PARF_MHI_CLOCK_RESET_CTRL 0x174
Stanimir Varbanov82a82382015-12-18 14:38:57 +020059#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT 0x178
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +000060#define PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2 0x1A8
61#define PCIE20_PARF_LTSSM 0x1B0
62#define PCIE20_PARF_SID_OFFSET 0x234
63#define PCIE20_PARF_BDF_TRANSLATE_CFG 0x24C
Stanimir Varbanov82a82382015-12-18 14:38:57 +020064
65#define PCIE20_ELBI_SYS_CTRL 0x04
66#define PCIE20_ELBI_SYS_CTRL_LT_ENABLE BIT(0)
67
Srinivas Kandagatlab8f2a852017-06-29 17:34:55 +020068#define PCIE20_AXI_MSTR_RESP_COMP_CTRL0 0x818
69#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K 0x4
70#define CFG_REMOTE_RD_REQ_BRIDGE_SIZE_4K 0x5
71#define PCIE20_AXI_MSTR_RESP_COMP_CTRL1 0x81c
72#define CFG_BRIDGE_SB_INIT BIT(0)
73
Stanimir Varbanov82a82382015-12-18 14:38:57 +020074#define PCIE20_CAP 0x70
Varadarajan Narayanan5d761172017-08-18 12:59:53 +053075#define PCIE20_CAP_LINK_CAPABILITIES (PCIE20_CAP + 0xC)
76#define PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT (BIT(10) | BIT(11))
77#define PCIE20_CAP_LINK_1 (PCIE20_CAP + 0x14)
78#define PCIE_CAP_LINK1_VAL 0x2FD7F
79
80#define PCIE20_PARF_Q2A_FLUSH 0x1AC
81
82#define PCIE20_MISC_CONTROL_1_REG 0x8BC
83#define DBI_RO_WR_EN 1
Stanimir Varbanov82a82382015-12-18 14:38:57 +020084
85#define PERST_DELAY_US 1000
86
Varadarajan Narayanan5d761172017-08-18 12:59:53 +053087#define PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE 0x358
88#define SLV_ADDR_SPACE_SZ 0x10000000
89
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +053090struct qcom_pcie_resources_2_1_0 {
Stanimir Varbanov82a82382015-12-18 14:38:57 +020091 struct clk *iface_clk;
92 struct clk *core_clk;
93 struct clk *phy_clk;
94 struct reset_control *pci_reset;
95 struct reset_control *axi_reset;
96 struct reset_control *ahb_reset;
97 struct reset_control *por_reset;
98 struct reset_control *phy_reset;
99 struct regulator *vdda;
100 struct regulator *vdda_phy;
101 struct regulator *vdda_refclk;
102};
103
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530104struct qcom_pcie_resources_1_0_0 {
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200105 struct clk *iface;
106 struct clk *aux;
107 struct clk *master_bus;
108 struct clk *slave_bus;
109 struct reset_control *core;
110 struct regulator *vdda;
111};
112
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530113struct qcom_pcie_resources_2_3_2 {
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000114 struct clk *aux_clk;
115 struct clk *master_clk;
116 struct clk *slave_clk;
117 struct clk *cfg_clk;
118 struct clk *pipe_clk;
119};
120
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530121struct qcom_pcie_resources_2_4_0 {
John Crispin90d52d52017-05-23 15:02:28 -0500122 struct clk *aux_clk;
123 struct clk *master_clk;
124 struct clk *slave_clk;
125 struct reset_control *axi_m_reset;
126 struct reset_control *axi_s_reset;
127 struct reset_control *pipe_reset;
128 struct reset_control *axi_m_vmid_reset;
129 struct reset_control *axi_s_xpu_reset;
130 struct reset_control *parf_reset;
131 struct reset_control *phy_reset;
132 struct reset_control *axi_m_sticky_reset;
133 struct reset_control *pipe_sticky_reset;
134 struct reset_control *pwr_reset;
135 struct reset_control *ahb_reset;
136 struct reset_control *phy_ahb_reset;
137};
138
Varadarajan Narayanan5d761172017-08-18 12:59:53 +0530139struct qcom_pcie_resources_2_3_3 {
140 struct clk *iface;
141 struct clk *axi_m_clk;
142 struct clk *axi_s_clk;
143 struct clk *ahb_clk;
144 struct clk *aux_clk;
145 struct reset_control *rst[7];
146};
147
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200148union qcom_pcie_resources {
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530149 struct qcom_pcie_resources_1_0_0 v1_0_0;
150 struct qcom_pcie_resources_2_1_0 v2_1_0;
151 struct qcom_pcie_resources_2_3_2 v2_3_2;
Varadarajan Narayanan5d761172017-08-18 12:59:53 +0530152 struct qcom_pcie_resources_2_3_3 v2_3_3;
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530153 struct qcom_pcie_resources_2_4_0 v2_4_0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200154};
155
156struct qcom_pcie;
157
158struct qcom_pcie_ops {
159 int (*get_resources)(struct qcom_pcie *pcie);
160 int (*init)(struct qcom_pcie *pcie);
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000161 int (*post_init)(struct qcom_pcie *pcie);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200162 void (*deinit)(struct qcom_pcie *pcie);
Bjorn Andersson71cee8e2017-07-15 23:42:03 -0700163 void (*post_deinit)(struct qcom_pcie *pcie);
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000164 void (*ltssm_enable)(struct qcom_pcie *pcie);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200165};
166
167struct qcom_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530168 struct dw_pcie *pci;
Bjorn Helgaasee053692016-10-06 13:39:37 -0500169 void __iomem *parf; /* DT parf */
170 void __iomem *elbi; /* DT elbi */
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200171 union qcom_pcie_resources res;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200172 struct phy *phy;
173 struct gpio_desc *reset;
174 struct qcom_pcie_ops *ops;
175};
176
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530177#define to_qcom_pcie(x) dev_get_drvdata((x)->dev)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200178
179static void qcom_ep_reset_assert(struct qcom_pcie *pcie)
180{
Fabio Estevama8c20382017-07-16 19:56:38 -0300181 gpiod_set_value_cansleep(pcie->reset, 1);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200182 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
183}
184
185static void qcom_ep_reset_deassert(struct qcom_pcie *pcie)
186{
Fabio Estevama8c20382017-07-16 19:56:38 -0300187 gpiod_set_value_cansleep(pcie->reset, 0);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200188 usleep_range(PERST_DELAY_US, PERST_DELAY_US + 500);
189}
190
191static irqreturn_t qcom_pcie_msi_irq_handler(int irq, void *arg)
192{
193 struct pcie_port *pp = arg;
194
195 return dw_handle_msi_irq(pp);
196}
197
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000198static int qcom_pcie_establish_link(struct qcom_pcie *pcie)
199{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530200 struct dw_pcie *pci = pcie->pci;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000201
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530202 if (dw_pcie_link_up(pci))
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000203 return 0;
204
205 /* Enable Link Training state machine */
206 if (pcie->ops->ltssm_enable)
207 pcie->ops->ltssm_enable(pcie);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200208
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530209 return dw_pcie_wait_for_link(pci);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200210}
211
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530212static void qcom_pcie_2_1_0_ltssm_enable(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500213{
214 u32 val;
215
216 /* enable link training */
217 val = readl(pcie->elbi + PCIE20_ELBI_SYS_CTRL);
218 val |= PCIE20_ELBI_SYS_CTRL_LT_ENABLE;
219 writel(val, pcie->elbi + PCIE20_ELBI_SYS_CTRL);
220}
221
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530222static int qcom_pcie_get_resources_2_1_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200223{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530224 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530225 struct dw_pcie *pci = pcie->pci;
226 struct device *dev = pci->dev;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200227
228 res->vdda = devm_regulator_get(dev, "vdda");
229 if (IS_ERR(res->vdda))
230 return PTR_ERR(res->vdda);
231
232 res->vdda_phy = devm_regulator_get(dev, "vdda_phy");
233 if (IS_ERR(res->vdda_phy))
234 return PTR_ERR(res->vdda_phy);
235
236 res->vdda_refclk = devm_regulator_get(dev, "vdda_refclk");
237 if (IS_ERR(res->vdda_refclk))
238 return PTR_ERR(res->vdda_refclk);
239
240 res->iface_clk = devm_clk_get(dev, "iface");
241 if (IS_ERR(res->iface_clk))
242 return PTR_ERR(res->iface_clk);
243
244 res->core_clk = devm_clk_get(dev, "core");
245 if (IS_ERR(res->core_clk))
246 return PTR_ERR(res->core_clk);
247
248 res->phy_clk = devm_clk_get(dev, "phy");
249 if (IS_ERR(res->phy_clk))
250 return PTR_ERR(res->phy_clk);
251
Philipp Zabel244e0002017-07-19 17:25:55 +0200252 res->pci_reset = devm_reset_control_get_exclusive(dev, "pci");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200253 if (IS_ERR(res->pci_reset))
254 return PTR_ERR(res->pci_reset);
255
Philipp Zabel244e0002017-07-19 17:25:55 +0200256 res->axi_reset = devm_reset_control_get_exclusive(dev, "axi");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200257 if (IS_ERR(res->axi_reset))
258 return PTR_ERR(res->axi_reset);
259
Philipp Zabel244e0002017-07-19 17:25:55 +0200260 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200261 if (IS_ERR(res->ahb_reset))
262 return PTR_ERR(res->ahb_reset);
263
Philipp Zabel244e0002017-07-19 17:25:55 +0200264 res->por_reset = devm_reset_control_get_exclusive(dev, "por");
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200265 if (IS_ERR(res->por_reset))
266 return PTR_ERR(res->por_reset);
267
Philipp Zabel244e0002017-07-19 17:25:55 +0200268 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
Fengguang Wu11a61a82017-02-04 09:35:32 +0800269 return PTR_ERR_OR_ZERO(res->phy_reset);
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200270}
271
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530272static void qcom_pcie_deinit_2_1_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200273{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530274 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200275
276 reset_control_assert(res->pci_reset);
277 reset_control_assert(res->axi_reset);
278 reset_control_assert(res->ahb_reset);
279 reset_control_assert(res->por_reset);
280 reset_control_assert(res->pci_reset);
281 clk_disable_unprepare(res->iface_clk);
282 clk_disable_unprepare(res->core_clk);
283 clk_disable_unprepare(res->phy_clk);
284 regulator_disable(res->vdda);
285 regulator_disable(res->vdda_phy);
286 regulator_disable(res->vdda_refclk);
287}
288
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530289static int qcom_pcie_init_2_1_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200290{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530291 struct qcom_pcie_resources_2_1_0 *res = &pcie->res.v2_1_0;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530292 struct dw_pcie *pci = pcie->pci;
293 struct device *dev = pci->dev;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200294 u32 val;
295 int ret;
296
297 ret = regulator_enable(res->vdda);
298 if (ret) {
299 dev_err(dev, "cannot enable vdda regulator\n");
300 return ret;
301 }
302
303 ret = regulator_enable(res->vdda_refclk);
304 if (ret) {
305 dev_err(dev, "cannot enable vdda_refclk regulator\n");
306 goto err_refclk;
307 }
308
309 ret = regulator_enable(res->vdda_phy);
310 if (ret) {
311 dev_err(dev, "cannot enable vdda_phy regulator\n");
312 goto err_vdda_phy;
313 }
314
315 ret = reset_control_assert(res->ahb_reset);
316 if (ret) {
317 dev_err(dev, "cannot assert ahb reset\n");
318 goto err_assert_ahb;
319 }
320
321 ret = clk_prepare_enable(res->iface_clk);
322 if (ret) {
323 dev_err(dev, "cannot prepare/enable iface clock\n");
324 goto err_assert_ahb;
325 }
326
327 ret = clk_prepare_enable(res->phy_clk);
328 if (ret) {
329 dev_err(dev, "cannot prepare/enable phy clock\n");
330 goto err_clk_phy;
331 }
332
333 ret = clk_prepare_enable(res->core_clk);
334 if (ret) {
335 dev_err(dev, "cannot prepare/enable core clock\n");
336 goto err_clk_core;
337 }
338
339 ret = reset_control_deassert(res->ahb_reset);
340 if (ret) {
341 dev_err(dev, "cannot deassert ahb reset\n");
342 goto err_deassert_ahb;
343 }
344
345 /* enable PCIe clocks and resets */
346 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
347 val &= ~BIT(0);
348 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
349
350 /* enable external reference clock */
351 val = readl(pcie->parf + PCIE20_PARF_PHY_REFCLK);
352 val |= BIT(16);
353 writel(val, pcie->parf + PCIE20_PARF_PHY_REFCLK);
354
355 ret = reset_control_deassert(res->phy_reset);
356 if (ret) {
357 dev_err(dev, "cannot deassert phy reset\n");
358 return ret;
359 }
360
361 ret = reset_control_deassert(res->pci_reset);
362 if (ret) {
363 dev_err(dev, "cannot deassert pci reset\n");
364 return ret;
365 }
366
367 ret = reset_control_deassert(res->por_reset);
368 if (ret) {
369 dev_err(dev, "cannot deassert por reset\n");
370 return ret;
371 }
372
373 ret = reset_control_deassert(res->axi_reset);
374 if (ret) {
375 dev_err(dev, "cannot deassert axi reset\n");
376 return ret;
377 }
378
379 /* wait for clock acquisition */
380 usleep_range(1000, 1500);
381
Srinivas Kandagatlab8f2a852017-06-29 17:34:55 +0200382
383 /* Set the Max TLP size to 2K, instead of using default of 4K */
384 writel(CFG_REMOTE_RD_REQ_BRIDGE_SIZE_2K,
385 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL0);
386 writel(CFG_BRIDGE_SB_INIT,
387 pci->dbi_base + PCIE20_AXI_MSTR_RESP_COMP_CTRL1);
388
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200389 return 0;
390
391err_deassert_ahb:
392 clk_disable_unprepare(res->core_clk);
393err_clk_core:
394 clk_disable_unprepare(res->phy_clk);
395err_clk_phy:
396 clk_disable_unprepare(res->iface_clk);
397err_assert_ahb:
398 regulator_disable(res->vdda_phy);
399err_vdda_phy:
400 regulator_disable(res->vdda_refclk);
401err_refclk:
402 regulator_disable(res->vdda);
403
404 return ret;
405}
406
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530407static int qcom_pcie_get_resources_1_0_0(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500408{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530409 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500410 struct dw_pcie *pci = pcie->pci;
411 struct device *dev = pci->dev;
412
413 res->vdda = devm_regulator_get(dev, "vdda");
414 if (IS_ERR(res->vdda))
415 return PTR_ERR(res->vdda);
416
417 res->iface = devm_clk_get(dev, "iface");
418 if (IS_ERR(res->iface))
419 return PTR_ERR(res->iface);
420
421 res->aux = devm_clk_get(dev, "aux");
422 if (IS_ERR(res->aux))
423 return PTR_ERR(res->aux);
424
425 res->master_bus = devm_clk_get(dev, "master_bus");
426 if (IS_ERR(res->master_bus))
427 return PTR_ERR(res->master_bus);
428
429 res->slave_bus = devm_clk_get(dev, "slave_bus");
430 if (IS_ERR(res->slave_bus))
431 return PTR_ERR(res->slave_bus);
432
Philipp Zabel244e0002017-07-19 17:25:55 +0200433 res->core = devm_reset_control_get_exclusive(dev, "core");
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500434 return PTR_ERR_OR_ZERO(res->core);
435}
436
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530437static void qcom_pcie_deinit_1_0_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200438{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530439 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200440
441 reset_control_assert(res->core);
442 clk_disable_unprepare(res->slave_bus);
443 clk_disable_unprepare(res->master_bus);
444 clk_disable_unprepare(res->iface);
445 clk_disable_unprepare(res->aux);
446 regulator_disable(res->vdda);
447}
448
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530449static int qcom_pcie_init_1_0_0(struct qcom_pcie *pcie)
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200450{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530451 struct qcom_pcie_resources_1_0_0 *res = &pcie->res.v1_0_0;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530452 struct dw_pcie *pci = pcie->pci;
453 struct device *dev = pci->dev;
Stanimir Varbanov82a82382015-12-18 14:38:57 +0200454 int ret;
455
456 ret = reset_control_deassert(res->core);
457 if (ret) {
458 dev_err(dev, "cannot deassert core reset\n");
459 return ret;
460 }
461
462 ret = clk_prepare_enable(res->aux);
463 if (ret) {
464 dev_err(dev, "cannot prepare/enable aux clock\n");
465 goto err_res;
466 }
467
468 ret = clk_prepare_enable(res->iface);
469 if (ret) {
470 dev_err(dev, "cannot prepare/enable iface clock\n");
471 goto err_aux;
472 }
473
474 ret = clk_prepare_enable(res->master_bus);
475 if (ret) {
476 dev_err(dev, "cannot prepare/enable master_bus clock\n");
477 goto err_iface;
478 }
479
480 ret = clk_prepare_enable(res->slave_bus);
481 if (ret) {
482 dev_err(dev, "cannot prepare/enable slave_bus clock\n");
483 goto err_master;
484 }
485
486 ret = regulator_enable(res->vdda);
487 if (ret) {
488 dev_err(dev, "cannot enable vdda regulator\n");
489 goto err_slave;
490 }
491
492 /* change DBI base address */
493 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
494
495 if (IS_ENABLED(CONFIG_PCI_MSI)) {
496 u32 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
497
498 val |= BIT(31);
499 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT);
500 }
501
502 return 0;
503err_slave:
504 clk_disable_unprepare(res->slave_bus);
505err_master:
506 clk_disable_unprepare(res->master_bus);
507err_iface:
508 clk_disable_unprepare(res->iface);
509err_aux:
510 clk_disable_unprepare(res->aux);
511err_res:
512 reset_control_assert(res->core);
513
514 return ret;
515}
516
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530517static void qcom_pcie_2_3_2_ltssm_enable(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500518{
519 u32 val;
520
521 /* enable link training */
522 val = readl(pcie->parf + PCIE20_PARF_LTSSM);
523 val |= BIT(8);
524 writel(val, pcie->parf + PCIE20_PARF_LTSSM);
525}
526
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530527static int qcom_pcie_get_resources_2_3_2(struct qcom_pcie *pcie)
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000528{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530529 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530530 struct dw_pcie *pci = pcie->pci;
531 struct device *dev = pci->dev;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000532
533 res->aux_clk = devm_clk_get(dev, "aux");
534 if (IS_ERR(res->aux_clk))
535 return PTR_ERR(res->aux_clk);
536
537 res->cfg_clk = devm_clk_get(dev, "cfg");
538 if (IS_ERR(res->cfg_clk))
539 return PTR_ERR(res->cfg_clk);
540
541 res->master_clk = devm_clk_get(dev, "bus_master");
542 if (IS_ERR(res->master_clk))
543 return PTR_ERR(res->master_clk);
544
545 res->slave_clk = devm_clk_get(dev, "bus_slave");
546 if (IS_ERR(res->slave_clk))
547 return PTR_ERR(res->slave_clk);
548
549 res->pipe_clk = devm_clk_get(dev, "pipe");
Fengguang Wu11a61a82017-02-04 09:35:32 +0800550 return PTR_ERR_OR_ZERO(res->pipe_clk);
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000551}
552
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530553static void qcom_pcie_deinit_2_3_2(struct qcom_pcie *pcie)
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500554{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530555 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500556
Bjorn Helgaas5d0f1b82017-05-24 15:19:36 -0500557 clk_disable_unprepare(res->slave_clk);
558 clk_disable_unprepare(res->master_clk);
559 clk_disable_unprepare(res->cfg_clk);
560 clk_disable_unprepare(res->aux_clk);
561}
562
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530563static void qcom_pcie_post_deinit_2_3_2(struct qcom_pcie *pcie)
Bjorn Andersson71cee8e2017-07-15 23:42:03 -0700564{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530565 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Bjorn Andersson71cee8e2017-07-15 23:42:03 -0700566
567 clk_disable_unprepare(res->pipe_clk);
568}
569
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530570static int qcom_pcie_init_2_3_2(struct qcom_pcie *pcie)
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000571{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530572 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530573 struct dw_pcie *pci = pcie->pci;
574 struct device *dev = pci->dev;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000575 u32 val;
576 int ret;
577
578 ret = clk_prepare_enable(res->aux_clk);
579 if (ret) {
580 dev_err(dev, "cannot prepare/enable aux clock\n");
581 return ret;
582 }
583
584 ret = clk_prepare_enable(res->cfg_clk);
585 if (ret) {
586 dev_err(dev, "cannot prepare/enable cfg clock\n");
587 goto err_cfg_clk;
588 }
589
590 ret = clk_prepare_enable(res->master_clk);
591 if (ret) {
592 dev_err(dev, "cannot prepare/enable master clock\n");
593 goto err_master_clk;
594 }
595
596 ret = clk_prepare_enable(res->slave_clk);
597 if (ret) {
598 dev_err(dev, "cannot prepare/enable slave clock\n");
599 goto err_slave_clk;
600 }
601
602 /* enable PCIe clocks and resets */
603 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
604 val &= ~BIT(0);
605 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
606
607 /* change DBI base address */
608 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
609
610 /* MAC PHY_POWERDOWN MUX DISABLE */
611 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
612 val &= ~BIT(29);
613 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
614
615 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
616 val |= BIT(4);
617 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
618
619 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
620 val |= BIT(31);
621 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
622
623 return 0;
624
625err_slave_clk:
626 clk_disable_unprepare(res->master_clk);
627err_master_clk:
628 clk_disable_unprepare(res->cfg_clk);
629err_cfg_clk:
630 clk_disable_unprepare(res->aux_clk);
631
632 return ret;
633}
634
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530635static int qcom_pcie_post_init_2_3_2(struct qcom_pcie *pcie)
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000636{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530637 struct qcom_pcie_resources_2_3_2 *res = &pcie->res.v2_3_2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530638 struct dw_pcie *pci = pcie->pci;
639 struct device *dev = pci->dev;
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +0000640 int ret;
641
642 ret = clk_prepare_enable(res->pipe_clk);
643 if (ret) {
644 dev_err(dev, "cannot prepare/enable pipe clock\n");
645 return ret;
646 }
647
648 return 0;
649}
650
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530651static int qcom_pcie_get_resources_2_4_0(struct qcom_pcie *pcie)
John Crispin90d52d52017-05-23 15:02:28 -0500652{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530653 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
John Crispin90d52d52017-05-23 15:02:28 -0500654 struct dw_pcie *pci = pcie->pci;
655 struct device *dev = pci->dev;
656
657 res->aux_clk = devm_clk_get(dev, "aux");
658 if (IS_ERR(res->aux_clk))
659 return PTR_ERR(res->aux_clk);
660
661 res->master_clk = devm_clk_get(dev, "master_bus");
662 if (IS_ERR(res->master_clk))
663 return PTR_ERR(res->master_clk);
664
665 res->slave_clk = devm_clk_get(dev, "slave_bus");
666 if (IS_ERR(res->slave_clk))
667 return PTR_ERR(res->slave_clk);
668
Philipp Zabel244e0002017-07-19 17:25:55 +0200669 res->axi_m_reset = devm_reset_control_get_exclusive(dev, "axi_m");
John Crispin90d52d52017-05-23 15:02:28 -0500670 if (IS_ERR(res->axi_m_reset))
671 return PTR_ERR(res->axi_m_reset);
672
Philipp Zabel244e0002017-07-19 17:25:55 +0200673 res->axi_s_reset = devm_reset_control_get_exclusive(dev, "axi_s");
John Crispin90d52d52017-05-23 15:02:28 -0500674 if (IS_ERR(res->axi_s_reset))
675 return PTR_ERR(res->axi_s_reset);
676
Philipp Zabel244e0002017-07-19 17:25:55 +0200677 res->pipe_reset = devm_reset_control_get_exclusive(dev, "pipe");
John Crispin90d52d52017-05-23 15:02:28 -0500678 if (IS_ERR(res->pipe_reset))
679 return PTR_ERR(res->pipe_reset);
680
Philipp Zabel244e0002017-07-19 17:25:55 +0200681 res->axi_m_vmid_reset = devm_reset_control_get_exclusive(dev,
682 "axi_m_vmid");
John Crispin90d52d52017-05-23 15:02:28 -0500683 if (IS_ERR(res->axi_m_vmid_reset))
684 return PTR_ERR(res->axi_m_vmid_reset);
685
Philipp Zabel244e0002017-07-19 17:25:55 +0200686 res->axi_s_xpu_reset = devm_reset_control_get_exclusive(dev,
687 "axi_s_xpu");
John Crispin90d52d52017-05-23 15:02:28 -0500688 if (IS_ERR(res->axi_s_xpu_reset))
689 return PTR_ERR(res->axi_s_xpu_reset);
690
Philipp Zabel244e0002017-07-19 17:25:55 +0200691 res->parf_reset = devm_reset_control_get_exclusive(dev, "parf");
John Crispin90d52d52017-05-23 15:02:28 -0500692 if (IS_ERR(res->parf_reset))
693 return PTR_ERR(res->parf_reset);
694
Philipp Zabel244e0002017-07-19 17:25:55 +0200695 res->phy_reset = devm_reset_control_get_exclusive(dev, "phy");
John Crispin90d52d52017-05-23 15:02:28 -0500696 if (IS_ERR(res->phy_reset))
697 return PTR_ERR(res->phy_reset);
698
Philipp Zabel244e0002017-07-19 17:25:55 +0200699 res->axi_m_sticky_reset = devm_reset_control_get_exclusive(dev,
700 "axi_m_sticky");
John Crispin90d52d52017-05-23 15:02:28 -0500701 if (IS_ERR(res->axi_m_sticky_reset))
702 return PTR_ERR(res->axi_m_sticky_reset);
703
Philipp Zabel244e0002017-07-19 17:25:55 +0200704 res->pipe_sticky_reset = devm_reset_control_get_exclusive(dev,
705 "pipe_sticky");
John Crispin90d52d52017-05-23 15:02:28 -0500706 if (IS_ERR(res->pipe_sticky_reset))
707 return PTR_ERR(res->pipe_sticky_reset);
708
Philipp Zabel244e0002017-07-19 17:25:55 +0200709 res->pwr_reset = devm_reset_control_get_exclusive(dev, "pwr");
John Crispin90d52d52017-05-23 15:02:28 -0500710 if (IS_ERR(res->pwr_reset))
711 return PTR_ERR(res->pwr_reset);
712
Philipp Zabel244e0002017-07-19 17:25:55 +0200713 res->ahb_reset = devm_reset_control_get_exclusive(dev, "ahb");
John Crispin90d52d52017-05-23 15:02:28 -0500714 if (IS_ERR(res->ahb_reset))
715 return PTR_ERR(res->ahb_reset);
716
Philipp Zabel244e0002017-07-19 17:25:55 +0200717 res->phy_ahb_reset = devm_reset_control_get_exclusive(dev, "phy_ahb");
John Crispin90d52d52017-05-23 15:02:28 -0500718 if (IS_ERR(res->phy_ahb_reset))
719 return PTR_ERR(res->phy_ahb_reset);
720
721 return 0;
722}
723
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530724static void qcom_pcie_deinit_2_4_0(struct qcom_pcie *pcie)
John Crispin90d52d52017-05-23 15:02:28 -0500725{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530726 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
John Crispin90d52d52017-05-23 15:02:28 -0500727
728 reset_control_assert(res->axi_m_reset);
729 reset_control_assert(res->axi_s_reset);
730 reset_control_assert(res->pipe_reset);
731 reset_control_assert(res->pipe_sticky_reset);
732 reset_control_assert(res->phy_reset);
733 reset_control_assert(res->phy_ahb_reset);
734 reset_control_assert(res->axi_m_sticky_reset);
735 reset_control_assert(res->pwr_reset);
736 reset_control_assert(res->ahb_reset);
737 clk_disable_unprepare(res->aux_clk);
738 clk_disable_unprepare(res->master_clk);
739 clk_disable_unprepare(res->slave_clk);
740}
741
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530742static int qcom_pcie_init_2_4_0(struct qcom_pcie *pcie)
John Crispin90d52d52017-05-23 15:02:28 -0500743{
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +0530744 struct qcom_pcie_resources_2_4_0 *res = &pcie->res.v2_4_0;
John Crispin90d52d52017-05-23 15:02:28 -0500745 struct dw_pcie *pci = pcie->pci;
746 struct device *dev = pci->dev;
747 u32 val;
748 int ret;
749
750 ret = reset_control_assert(res->axi_m_reset);
751 if (ret) {
752 dev_err(dev, "cannot assert axi master reset\n");
753 return ret;
754 }
755
756 ret = reset_control_assert(res->axi_s_reset);
757 if (ret) {
Colin Ian King7a5966e2017-05-31 06:34:14 +0100758 dev_err(dev, "cannot assert axi slave reset\n");
John Crispin90d52d52017-05-23 15:02:28 -0500759 return ret;
760 }
761
762 usleep_range(10000, 12000);
763
764 ret = reset_control_assert(res->pipe_reset);
765 if (ret) {
766 dev_err(dev, "cannot assert pipe reset\n");
767 return ret;
768 }
769
770 ret = reset_control_assert(res->pipe_sticky_reset);
771 if (ret) {
772 dev_err(dev, "cannot assert pipe sticky reset\n");
773 return ret;
774 }
775
776 ret = reset_control_assert(res->phy_reset);
777 if (ret) {
778 dev_err(dev, "cannot assert phy reset\n");
779 return ret;
780 }
781
782 ret = reset_control_assert(res->phy_ahb_reset);
783 if (ret) {
784 dev_err(dev, "cannot assert phy ahb reset\n");
785 return ret;
786 }
787
788 usleep_range(10000, 12000);
789
790 ret = reset_control_assert(res->axi_m_sticky_reset);
791 if (ret) {
792 dev_err(dev, "cannot assert axi master sticky reset\n");
793 return ret;
794 }
795
796 ret = reset_control_assert(res->pwr_reset);
797 if (ret) {
798 dev_err(dev, "cannot assert power reset\n");
799 return ret;
800 }
801
802 ret = reset_control_assert(res->ahb_reset);
803 if (ret) {
804 dev_err(dev, "cannot assert ahb reset\n");
805 return ret;
806 }
807
808 usleep_range(10000, 12000);
809
810 ret = reset_control_deassert(res->phy_ahb_reset);
811 if (ret) {
812 dev_err(dev, "cannot deassert phy ahb reset\n");
813 return ret;
814 }
815
816 ret = reset_control_deassert(res->phy_reset);
817 if (ret) {
818 dev_err(dev, "cannot deassert phy reset\n");
819 goto err_rst_phy;
820 }
821
822 ret = reset_control_deassert(res->pipe_reset);
823 if (ret) {
824 dev_err(dev, "cannot deassert pipe reset\n");
825 goto err_rst_pipe;
826 }
827
828 ret = reset_control_deassert(res->pipe_sticky_reset);
829 if (ret) {
830 dev_err(dev, "cannot deassert pipe sticky reset\n");
831 goto err_rst_pipe_sticky;
832 }
833
834 usleep_range(10000, 12000);
835
836 ret = reset_control_deassert(res->axi_m_reset);
837 if (ret) {
838 dev_err(dev, "cannot deassert axi master reset\n");
839 goto err_rst_axi_m;
840 }
841
842 ret = reset_control_deassert(res->axi_m_sticky_reset);
843 if (ret) {
844 dev_err(dev, "cannot deassert axi master sticky reset\n");
845 goto err_rst_axi_m_sticky;
846 }
847
848 ret = reset_control_deassert(res->axi_s_reset);
849 if (ret) {
850 dev_err(dev, "cannot deassert axi slave reset\n");
851 goto err_rst_axi_s;
852 }
853
854 ret = reset_control_deassert(res->pwr_reset);
855 if (ret) {
856 dev_err(dev, "cannot deassert power reset\n");
857 goto err_rst_pwr;
858 }
859
860 ret = reset_control_deassert(res->ahb_reset);
861 if (ret) {
862 dev_err(dev, "cannot deassert ahb reset\n");
863 goto err_rst_ahb;
864 }
865
866 usleep_range(10000, 12000);
867
868 ret = clk_prepare_enable(res->aux_clk);
869 if (ret) {
870 dev_err(dev, "cannot prepare/enable iface clock\n");
871 goto err_clk_aux;
872 }
873
874 ret = clk_prepare_enable(res->master_clk);
875 if (ret) {
876 dev_err(dev, "cannot prepare/enable core clock\n");
877 goto err_clk_axi_m;
878 }
879
880 ret = clk_prepare_enable(res->slave_clk);
881 if (ret) {
882 dev_err(dev, "cannot prepare/enable phy clock\n");
883 goto err_clk_axi_s;
884 }
885
886 /* enable PCIe clocks and resets */
887 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
888 val &= !BIT(0);
889 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
890
891 /* change DBI base address */
892 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
893
894 /* MAC PHY_POWERDOWN MUX DISABLE */
895 val = readl(pcie->parf + PCIE20_PARF_SYS_CTRL);
896 val &= ~BIT(29);
897 writel(val, pcie->parf + PCIE20_PARF_SYS_CTRL);
898
899 val = readl(pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
900 val |= BIT(4);
901 writel(val, pcie->parf + PCIE20_PARF_MHI_CLOCK_RESET_CTRL);
902
903 val = readl(pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
904 val |= BIT(31);
905 writel(val, pcie->parf + PCIE20_PARF_AXI_MSTR_WR_ADDR_HALT_V2);
906
907 return 0;
908
909err_clk_axi_s:
910 clk_disable_unprepare(res->master_clk);
911err_clk_axi_m:
912 clk_disable_unprepare(res->aux_clk);
913err_clk_aux:
914 reset_control_assert(res->ahb_reset);
915err_rst_ahb:
916 reset_control_assert(res->pwr_reset);
917err_rst_pwr:
918 reset_control_assert(res->axi_s_reset);
919err_rst_axi_s:
920 reset_control_assert(res->axi_m_sticky_reset);
921err_rst_axi_m_sticky:
922 reset_control_assert(res->axi_m_reset);
923err_rst_axi_m:
924 reset_control_assert(res->pipe_sticky_reset);
925err_rst_pipe_sticky:
926 reset_control_assert(res->pipe_reset);
927err_rst_pipe:
928 reset_control_assert(res->phy_reset);
929err_rst_phy:
930 reset_control_assert(res->phy_ahb_reset);
931 return ret;
932}
933
Varadarajan Narayanan5d761172017-08-18 12:59:53 +0530934static int qcom_pcie_get_resources_2_3_3(struct qcom_pcie *pcie)
935{
936 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
937 struct dw_pcie *pci = pcie->pci;
938 struct device *dev = pci->dev;
939 int i;
940 const char *rst_names[] = { "axi_m", "axi_s", "pipe",
941 "axi_m_sticky", "sticky",
942 "ahb", "sleep", };
943
944 res->iface = devm_clk_get(dev, "iface");
945 if (IS_ERR(res->iface))
946 return PTR_ERR(res->iface);
947
948 res->axi_m_clk = devm_clk_get(dev, "axi_m");
949 if (IS_ERR(res->axi_m_clk))
950 return PTR_ERR(res->axi_m_clk);
951
952 res->axi_s_clk = devm_clk_get(dev, "axi_s");
953 if (IS_ERR(res->axi_s_clk))
954 return PTR_ERR(res->axi_s_clk);
955
956 res->ahb_clk = devm_clk_get(dev, "ahb");
957 if (IS_ERR(res->ahb_clk))
958 return PTR_ERR(res->ahb_clk);
959
960 res->aux_clk = devm_clk_get(dev, "aux");
961 if (IS_ERR(res->aux_clk))
962 return PTR_ERR(res->aux_clk);
963
964 for (i = 0; i < ARRAY_SIZE(rst_names); i++) {
965 res->rst[i] = devm_reset_control_get(dev, rst_names[i]);
966 if (IS_ERR(res->rst[i]))
967 return PTR_ERR(res->rst[i]);
968 }
969
970 return 0;
971}
972
973static void qcom_pcie_deinit_2_3_3(struct qcom_pcie *pcie)
974{
975 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
976
977 clk_disable_unprepare(res->iface);
978 clk_disable_unprepare(res->axi_m_clk);
979 clk_disable_unprepare(res->axi_s_clk);
980 clk_disable_unprepare(res->ahb_clk);
981 clk_disable_unprepare(res->aux_clk);
982}
983
984static int qcom_pcie_init_2_3_3(struct qcom_pcie *pcie)
985{
986 struct qcom_pcie_resources_2_3_3 *res = &pcie->res.v2_3_3;
987 struct dw_pcie *pci = pcie->pci;
988 struct device *dev = pci->dev;
989 int i, ret;
990 u32 val;
991
992 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
993 ret = reset_control_assert(res->rst[i]);
994 if (ret) {
995 dev_err(dev, "reset #%d assert failed (%d)\n", i, ret);
996 return ret;
997 }
998 }
999
1000 usleep_range(2000, 2500);
1001
1002 for (i = 0; i < ARRAY_SIZE(res->rst); i++) {
1003 ret = reset_control_deassert(res->rst[i]);
1004 if (ret) {
1005 dev_err(dev, "reset #%d deassert failed (%d)\n", i,
1006 ret);
1007 return ret;
1008 }
1009 }
1010
1011 /*
1012 * Don't have a way to see if the reset has completed.
1013 * Wait for some time.
1014 */
1015 usleep_range(2000, 2500);
1016
1017 ret = clk_prepare_enable(res->iface);
1018 if (ret) {
1019 dev_err(dev, "cannot prepare/enable core clock\n");
1020 goto err_clk_iface;
1021 }
1022
1023 ret = clk_prepare_enable(res->axi_m_clk);
1024 if (ret) {
1025 dev_err(dev, "cannot prepare/enable core clock\n");
1026 goto err_clk_axi_m;
1027 }
1028
1029 ret = clk_prepare_enable(res->axi_s_clk);
1030 if (ret) {
1031 dev_err(dev, "cannot prepare/enable axi slave clock\n");
1032 goto err_clk_axi_s;
1033 }
1034
1035 ret = clk_prepare_enable(res->ahb_clk);
1036 if (ret) {
1037 dev_err(dev, "cannot prepare/enable ahb clock\n");
1038 goto err_clk_ahb;
1039 }
1040
1041 ret = clk_prepare_enable(res->aux_clk);
1042 if (ret) {
1043 dev_err(dev, "cannot prepare/enable aux clock\n");
1044 goto err_clk_aux;
1045 }
1046
1047 writel(SLV_ADDR_SPACE_SZ,
1048 pcie->parf + PCIE20_v3_PARF_SLV_ADDR_SPACE_SIZE);
1049
1050 val = readl(pcie->parf + PCIE20_PARF_PHY_CTRL);
1051 val &= ~BIT(0);
1052 writel(val, pcie->parf + PCIE20_PARF_PHY_CTRL);
1053
1054 writel(0, pcie->parf + PCIE20_PARF_DBI_BASE_ADDR);
1055
1056 writel(MST_WAKEUP_EN | SLV_WAKEUP_EN | MSTR_ACLK_CGC_DIS
1057 | SLV_ACLK_CGC_DIS | CORE_CLK_CGC_DIS |
1058 AUX_PWR_DET | L23_CLK_RMV_DIS | L1_CLK_RMV_DIS,
1059 pcie->parf + PCIE20_PARF_SYS_CTRL);
1060 writel(0, pcie->parf + PCIE20_PARF_Q2A_FLUSH);
1061
1062 writel(CMD_BME_VAL, pci->dbi_base + PCIE20_COMMAND_STATUS);
1063 writel(DBI_RO_WR_EN, pci->dbi_base + PCIE20_MISC_CONTROL_1_REG);
1064 writel(PCIE_CAP_LINK1_VAL, pci->dbi_base + PCIE20_CAP_LINK_1);
1065
1066 val = readl(pci->dbi_base + PCIE20_CAP_LINK_CAPABILITIES);
1067 val &= ~PCIE20_CAP_ACTIVE_STATE_LINK_PM_SUPPORT;
1068 writel(val, pci->dbi_base + PCIE20_CAP_LINK_CAPABILITIES);
1069
1070 writel(PCIE_CAP_CPL_TIMEOUT_DISABLE, pci->dbi_base +
1071 PCIE20_DEVICE_CONTROL2_STATUS2);
1072
1073 return 0;
1074
1075err_clk_aux:
1076 clk_disable_unprepare(res->ahb_clk);
1077err_clk_ahb:
1078 clk_disable_unprepare(res->axi_s_clk);
1079err_clk_axi_s:
1080 clk_disable_unprepare(res->axi_m_clk);
1081err_clk_axi_m:
1082 clk_disable_unprepare(res->iface);
1083err_clk_iface:
1084 /*
1085 * Not checking for failure, will anyway return
1086 * the original failure in 'ret'.
1087 */
1088 for (i = 0; i < ARRAY_SIZE(res->rst); i++)
1089 reset_control_assert(res->rst[i]);
1090
1091 return ret;
1092}
1093
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301094static int qcom_pcie_link_up(struct dw_pcie *pci)
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001095{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301096 u16 val = readw(pci->dbi_base + PCIE20_CAP + PCI_EXP_LNKSTA);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001097
1098 return !!(val & PCI_EXP_LNKSTA_DLLLA);
1099}
1100
Bjorn Andersson4a301762017-07-15 23:39:45 -07001101static int qcom_pcie_host_init(struct pcie_port *pp)
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001102{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301103 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1104 struct qcom_pcie *pcie = to_qcom_pcie(pci);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001105 int ret;
1106
1107 qcom_ep_reset_assert(pcie);
1108
1109 ret = pcie->ops->init(pcie);
1110 if (ret)
Bjorn Andersson89539f02017-07-15 23:41:53 -07001111 return ret;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001112
1113 ret = phy_power_on(pcie->phy);
1114 if (ret)
1115 goto err_deinit;
1116
Bjorn Andersson71cee8e2017-07-15 23:42:03 -07001117 if (pcie->ops->post_init) {
1118 ret = pcie->ops->post_init(pcie);
1119 if (ret)
1120 goto err_disable_phy;
1121 }
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +00001122
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001123 dw_pcie_setup_rc(pp);
1124
1125 if (IS_ENABLED(CONFIG_PCI_MSI))
1126 dw_pcie_msi_init(pp);
1127
1128 qcom_ep_reset_deassert(pcie);
1129
1130 ret = qcom_pcie_establish_link(pcie);
1131 if (ret)
1132 goto err;
1133
Bjorn Andersson4a301762017-07-15 23:39:45 -07001134 return 0;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001135err:
1136 qcom_ep_reset_assert(pcie);
Bjorn Andersson71cee8e2017-07-15 23:42:03 -07001137 if (pcie->ops->post_deinit)
1138 pcie->ops->post_deinit(pcie);
1139err_disable_phy:
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001140 phy_power_off(pcie->phy);
1141err_deinit:
1142 pcie->ops->deinit(pcie);
Bjorn Andersson4a301762017-07-15 23:39:45 -07001143
1144 return ret;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001145}
1146
1147static int qcom_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
1148 u32 *val)
1149{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301150 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1151
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001152 /* the device class is not reported correctly from the register */
1153 if (where == PCI_CLASS_REVISION && size == 4) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301154 *val = readl(pci->dbi_base + PCI_CLASS_REVISION);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001155 *val &= 0xff; /* keep revision id */
1156 *val |= PCI_CLASS_BRIDGE_PCI << 16;
1157 return PCIBIOS_SUCCESSFUL;
1158 }
1159
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301160 return dw_pcie_read(pci->dbi_base + where, size, val);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001161}
1162
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +08001163static const struct dw_pcie_host_ops qcom_pcie_dw_ops = {
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001164 .host_init = qcom_pcie_host_init,
1165 .rd_own_conf = qcom_pcie_rd_own_conf,
1166};
1167
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301168/* Qcom IP rev.: 2.1.0 Synopsys IP rev.: 4.01a */
1169static const struct qcom_pcie_ops ops_2_1_0 = {
1170 .get_resources = qcom_pcie_get_resources_2_1_0,
1171 .init = qcom_pcie_init_2_1_0,
1172 .deinit = qcom_pcie_deinit_2_1_0,
1173 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001174};
1175
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301176/* Qcom IP rev.: 1.0.0 Synopsys IP rev.: 4.11a */
1177static const struct qcom_pcie_ops ops_1_0_0 = {
1178 .get_resources = qcom_pcie_get_resources_1_0_0,
1179 .init = qcom_pcie_init_1_0_0,
1180 .deinit = qcom_pcie_deinit_1_0_0,
1181 .ltssm_enable = qcom_pcie_2_1_0_ltssm_enable,
Srinivas Kandagatlad0491fc2016-11-22 10:43:29 +00001182};
1183
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301184/* Qcom IP rev.: 2.3.2 Synopsys IP rev.: 4.21a */
1185static const struct qcom_pcie_ops ops_2_3_2 = {
1186 .get_resources = qcom_pcie_get_resources_2_3_2,
1187 .init = qcom_pcie_init_2_3_2,
1188 .post_init = qcom_pcie_post_init_2_3_2,
1189 .deinit = qcom_pcie_deinit_2_3_2,
1190 .post_deinit = qcom_pcie_post_deinit_2_3_2,
1191 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1192};
1193
1194/* Qcom IP rev.: 2.4.0 Synopsys IP rev.: 4.20a */
1195static const struct qcom_pcie_ops ops_2_4_0 = {
1196 .get_resources = qcom_pcie_get_resources_2_4_0,
1197 .init = qcom_pcie_init_2_4_0,
1198 .deinit = qcom_pcie_deinit_2_4_0,
1199 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001200};
1201
Varadarajan Narayanan5d761172017-08-18 12:59:53 +05301202/* Qcom IP rev.: 2.3.3 Synopsys IP rev.: 4.30a */
1203static const struct qcom_pcie_ops ops_2_3_3 = {
1204 .get_resources = qcom_pcie_get_resources_2_3_3,
1205 .init = qcom_pcie_init_2_3_3,
1206 .deinit = qcom_pcie_deinit_2_3_3,
1207 .ltssm_enable = qcom_pcie_2_3_2_ltssm_enable,
1208};
1209
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301210static const struct dw_pcie_ops dw_pcie_ops = {
1211 .link_up = qcom_pcie_link_up,
1212};
1213
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001214static int qcom_pcie_probe(struct platform_device *pdev)
1215{
1216 struct device *dev = &pdev->dev;
1217 struct resource *res;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001218 struct pcie_port *pp;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301219 struct dw_pcie *pci;
1220 struct qcom_pcie *pcie;
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001221 int ret;
1222
1223 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
1224 if (!pcie)
1225 return -ENOMEM;
1226
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301227 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1228 if (!pci)
1229 return -ENOMEM;
1230
1231 pci->dev = dev;
1232 pci->ops = &dw_pcie_ops;
1233 pp = &pci->pp;
1234
Guenter Roeckc0464062017-02-25 02:08:12 -08001235 pcie->pci = pci;
1236
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001237 pcie->ops = (struct qcom_pcie_ops *)of_device_get_match_data(dev);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001238
1239 pcie->reset = devm_gpiod_get_optional(dev, "perst", GPIOD_OUT_LOW);
1240 if (IS_ERR(pcie->reset))
1241 return PTR_ERR(pcie->reset);
1242
1243 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "parf");
1244 pcie->parf = devm_ioremap_resource(dev, res);
1245 if (IS_ERR(pcie->parf))
1246 return PTR_ERR(pcie->parf);
1247
1248 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
Lorenzo Pieralisi10c736f2017-04-19 17:49:01 +01001249 pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301250 if (IS_ERR(pci->dbi_base))
1251 return PTR_ERR(pci->dbi_base);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001252
1253 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "elbi");
1254 pcie->elbi = devm_ioremap_resource(dev, res);
1255 if (IS_ERR(pcie->elbi))
1256 return PTR_ERR(pcie->elbi);
1257
1258 pcie->phy = devm_phy_optional_get(dev, "pciephy");
1259 if (IS_ERR(pcie->phy))
1260 return PTR_ERR(pcie->phy);
1261
1262 ret = pcie->ops->get_resources(pcie);
1263 if (ret)
1264 return ret;
1265
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001266 pp->root_bus_nr = -1;
1267 pp->ops = &qcom_pcie_dw_ops;
1268
1269 if (IS_ENABLED(CONFIG_PCI_MSI)) {
1270 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
1271 if (pp->msi_irq < 0)
1272 return pp->msi_irq;
1273
1274 ret = devm_request_irq(dev, pp->msi_irq,
1275 qcom_pcie_msi_irq_handler,
Jisheng Zhang3eefa792017-04-20 18:27:18 +08001276 IRQF_SHARED | IRQF_NO_THREAD,
1277 "qcom-pcie-msi", pp);
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001278 if (ret) {
1279 dev_err(dev, "cannot request msi irq\n");
1280 return ret;
1281 }
1282 }
1283
1284 ret = phy_init(pcie->phy);
1285 if (ret)
1286 return ret;
1287
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +05301288 platform_set_drvdata(pdev, pcie);
1289
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001290 ret = dw_pcie_host_init(pp);
1291 if (ret) {
1292 dev_err(dev, "cannot initialize host\n");
1293 return ret;
1294 }
1295
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001296 return 0;
1297}
1298
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001299static const struct of_device_id qcom_pcie_match[] = {
Varadarajan Narayanan5d761172017-08-18 12:59:53 +05301300 { .compatible = "qcom,pcie-apq8084", .data = &ops_1_0_0 },
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301301 { .compatible = "qcom,pcie-ipq8064", .data = &ops_2_1_0 },
1302 { .compatible = "qcom,pcie-apq8064", .data = &ops_2_1_0 },
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301303 { .compatible = "qcom,pcie-msm8996", .data = &ops_2_3_2 },
Varadarajan Narayanan5d761172017-08-18 12:59:53 +05301304 { .compatible = "qcom,pcie-ipq8074", .data = &ops_2_3_3 },
Varadarajan Narayanandeff11f2017-08-18 12:59:51 +05301305 { .compatible = "qcom,pcie-ipq4019", .data = &ops_2_4_0 },
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001306 { }
1307};
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001308
1309static struct platform_driver qcom_pcie_driver = {
1310 .probe = qcom_pcie_probe,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001311 .driver = {
1312 .name = "qcom-pcie",
Paul Gortmakerf9a66602016-08-24 16:57:48 -04001313 .suppress_bind_attrs = true,
Stanimir Varbanov82a82382015-12-18 14:38:57 +02001314 .of_match_table = qcom_pcie_match,
1315 },
1316};
Paul Gortmakerf9a66602016-08-24 16:57:48 -04001317builtin_platform_driver(qcom_pcie_driver);