blob: 5596fdedbb9471c49b4bb73fbc57b5b03170521f [file] [log] [blame]
Jingoo Han4b1ced82013-07-31 17:14:10 +09001/*
2 * PCIe host controller driver for Samsung EXYNOS SoCs
3 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
7 * Author: Jingoo Han <jg1.han@samsung.com>
8 *
9 * 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 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/gpio.h>
17#include <linux/interrupt.h>
18#include <linux/kernel.h>
Paul Gortmakercaf55482016-08-22 17:59:47 -040019#include <linux/init.h>
Niyas Ahmed S T32784782017-02-01 10:13:06 +053020#include <linux/of_device.h>
Jingoo Han4b1ced82013-07-31 17:14:10 +090021#include <linux/of_gpio.h>
22#include <linux/pci.h>
23#include <linux/platform_device.h>
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +090024#include <linux/phy/phy.h>
Jingoo Han4b1ced82013-07-31 17:14:10 +090025#include <linux/resource.h>
26#include <linux/signal.h>
27#include <linux/types.h>
28
29#include "pcie-designware.h"
30
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053031#define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
Jingoo Han4b1ced82013-07-31 17:14:10 +090032
Jingoo Han4b1ced82013-07-31 17:14:10 +090033/* PCIe ELBI registers */
34#define PCIE_IRQ_PULSE 0x000
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090035#define IRQ_INTA_ASSERT BIT(0)
36#define IRQ_INTB_ASSERT BIT(2)
37#define IRQ_INTC_ASSERT BIT(4)
38#define IRQ_INTD_ASSERT BIT(6)
Jingoo Han4b1ced82013-07-31 17:14:10 +090039#define PCIE_IRQ_LEVEL 0x004
40#define PCIE_IRQ_SPECIAL 0x008
41#define PCIE_IRQ_EN_PULSE 0x00c
42#define PCIE_IRQ_EN_LEVEL 0x010
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090043#define IRQ_MSI_ENABLE BIT(2)
Jingoo Han4b1ced82013-07-31 17:14:10 +090044#define PCIE_IRQ_EN_SPECIAL 0x014
45#define PCIE_PWR_RESET 0x018
46#define PCIE_CORE_RESET 0x01c
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090047#define PCIE_CORE_RESET_ENABLE BIT(0)
Jingoo Han4b1ced82013-07-31 17:14:10 +090048#define PCIE_STICKY_RESET 0x020
49#define PCIE_NONSTICKY_RESET 0x024
50#define PCIE_APP_INIT_RESET 0x028
51#define PCIE_APP_LTSSM_ENABLE 0x02c
52#define PCIE_ELBI_RDLH_LINKUP 0x064
53#define PCIE_ELBI_LTSSM_ENABLE 0x1
54#define PCIE_ELBI_SLV_AWMISC 0x11c
55#define PCIE_ELBI_SLV_ARMISC 0x120
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090056#define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
Jingoo Han4b1ced82013-07-31 17:14:10 +090057
58/* PCIe Purple registers */
59#define PCIE_PHY_GLOBAL_RESET 0x000
60#define PCIE_PHY_COMMON_RESET 0x004
61#define PCIE_PHY_CMN_REG 0x008
62#define PCIE_PHY_MAC_RESET 0x00c
63#define PCIE_PHY_PLL_LOCKED 0x010
64#define PCIE_PHY_TRSVREG_RESET 0x020
65#define PCIE_PHY_TRSV_RESET 0x024
66
67/* PCIe PHY registers */
68#define PCIE_PHY_IMPEDANCE 0x004
69#define PCIE_PHY_PLL_DIV_0 0x008
70#define PCIE_PHY_PLL_BIAS 0x00c
71#define PCIE_PHY_DCC_FEEDBACK 0x014
72#define PCIE_PHY_PLL_DIV_1 0x05c
Jingoo Hanf62b8782013-09-06 17:21:45 +090073#define PCIE_PHY_COMMON_POWER 0x064
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090074#define PCIE_PHY_COMMON_PD_CMN BIT(3)
Jingoo Han4b1ced82013-07-31 17:14:10 +090075#define PCIE_PHY_TRSV0_EMP_LVL 0x084
76#define PCIE_PHY_TRSV0_DRV_LVL 0x088
77#define PCIE_PHY_TRSV0_RXCDR 0x0ac
Jingoo Hanf62b8782013-09-06 17:21:45 +090078#define PCIE_PHY_TRSV0_POWER 0x0c4
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090079#define PCIE_PHY_TRSV0_PD_TSV BIT(7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090080#define PCIE_PHY_TRSV0_LVCC 0x0dc
81#define PCIE_PHY_TRSV1_EMP_LVL 0x144
82#define PCIE_PHY_TRSV1_RXCDR 0x16c
Jingoo Hanf62b8782013-09-06 17:21:45 +090083#define PCIE_PHY_TRSV1_POWER 0x184
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090084#define PCIE_PHY_TRSV1_PD_TSV BIT(7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090085#define PCIE_PHY_TRSV1_LVCC 0x19c
86#define PCIE_PHY_TRSV2_EMP_LVL 0x204
87#define PCIE_PHY_TRSV2_RXCDR 0x22c
Jingoo Hanf62b8782013-09-06 17:21:45 +090088#define PCIE_PHY_TRSV2_POWER 0x244
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090089#define PCIE_PHY_TRSV2_PD_TSV BIT(7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090090#define PCIE_PHY_TRSV2_LVCC 0x25c
91#define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
92#define PCIE_PHY_TRSV3_RXCDR 0x2ec
Jingoo Hanf62b8782013-09-06 17:21:45 +090093#define PCIE_PHY_TRSV3_POWER 0x304
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090094#define PCIE_PHY_TRSV3_PD_TSV BIT(7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090095#define PCIE_PHY_TRSV3_LVCC 0x31c
96
Niyas Ahmed S T32784782017-02-01 10:13:06 +053097struct exynos_pcie_mem_res {
98 void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */
99 void __iomem *phy_base; /* DT 1st resource: PHY CTRL */
100 void __iomem *block_base; /* DT 2nd resource: PHY ADDITIONAL CTRL */
101};
102
103struct exynos_pcie_clk_res {
104 struct clk *clk;
105 struct clk *bus_clk;
106};
107
108struct exynos_pcie {
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600109 struct dw_pcie *pci;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530110 struct exynos_pcie_mem_res *mem_res;
111 struct exynos_pcie_clk_res *clk_res;
112 const struct exynos_pcie_ops *ops;
113 int reset_gpio;
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900114
115 /* For Generic PHY Framework */
116 bool using_phy;
117 struct phy *phy;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530118};
119
120struct exynos_pcie_ops {
121 int (*get_mem_resources)(struct platform_device *pdev,
122 struct exynos_pcie *ep);
123 int (*get_clk_resources)(struct exynos_pcie *ep);
124 int (*init_clk_resources)(struct exynos_pcie *ep);
125 void (*deinit_clk_resources)(struct exynos_pcie *ep);
126};
127
128static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
129 struct exynos_pcie *ep)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900130{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600131 struct dw_pcie *pci = ep->pci;
132 struct device *dev = pci->dev;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530133 struct resource *res;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530134
135 ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
136 if (!ep->mem_res)
137 return -ENOMEM;
138
139 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
140 ep->mem_res->elbi_base = devm_ioremap_resource(dev, res);
141 if (IS_ERR(ep->mem_res->elbi_base))
142 return PTR_ERR(ep->mem_res->elbi_base);
143
Jaehoon Chung544714d2017-03-07 19:54:05 +0900144 /* If using the PHY framework, doesn't need to get other resource */
145 if (ep->using_phy)
146 return 0;
147
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530148 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
149 ep->mem_res->phy_base = devm_ioremap_resource(dev, res);
150 if (IS_ERR(ep->mem_res->phy_base))
151 return PTR_ERR(ep->mem_res->phy_base);
152
153 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
154 ep->mem_res->block_base = devm_ioremap_resource(dev, res);
155 if (IS_ERR(ep->mem_res->block_base))
156 return PTR_ERR(ep->mem_res->block_base);
157
158 return 0;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900159}
160
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530161static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900162{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600163 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530164 struct device *dev = pci->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900165
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530166 ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
167 if (!ep->clk_res)
168 return -ENOMEM;
169
170 ep->clk_res->clk = devm_clk_get(dev, "pcie");
171 if (IS_ERR(ep->clk_res->clk)) {
172 dev_err(dev, "Failed to get pcie rc clock\n");
173 return PTR_ERR(ep->clk_res->clk);
174 }
175
176 ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
177 if (IS_ERR(ep->clk_res->bus_clk)) {
178 dev_err(dev, "Failed to get pcie bus clock\n");
179 return PTR_ERR(ep->clk_res->bus_clk);
180 }
181
182 return 0;
183}
184
185static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
186{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600187 struct dw_pcie *pci = ep->pci;
188 struct device *dev = pci->dev;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530189 int ret;
190
191 ret = clk_prepare_enable(ep->clk_res->clk);
192 if (ret) {
193 dev_err(dev, "cannot enable pcie rc clock");
194 return ret;
195 }
196
197 ret = clk_prepare_enable(ep->clk_res->bus_clk);
198 if (ret) {
199 dev_err(dev, "cannot enable pcie bus clock");
200 goto err_bus_clk;
201 }
202
203 return 0;
204
205err_bus_clk:
206 clk_disable_unprepare(ep->clk_res->clk);
207
208 return ret;
209}
210
211static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
212{
213 clk_disable_unprepare(ep->clk_res->bus_clk);
214 clk_disable_unprepare(ep->clk_res->clk);
215}
216
217static const struct exynos_pcie_ops exynos5440_pcie_ops = {
218 .get_mem_resources = exynos5440_pcie_get_mem_resources,
219 .get_clk_resources = exynos5440_pcie_get_clk_resources,
220 .init_clk_resources = exynos5440_pcie_init_clk_resources,
221 .deinit_clk_resources = exynos5440_pcie_deinit_clk_resources,
222};
223
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900224static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900225{
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900226 writel(val, base + reg);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900227}
228
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900229static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900230{
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900231 return readl(base + reg);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900232}
233
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900234static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900235{
236 u32 val;
237
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530238 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
Jaehoon Chung92004a062017-01-16 15:31:38 +0900239 if (on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900240 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Jaehoon Chung92004a062017-01-16 15:31:38 +0900241 else
Jingoo Han4b1ced82013-07-31 17:14:10 +0900242 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530243 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900244}
245
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900246static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900247{
248 u32 val;
249
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530250 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
Jaehoon Chung92004a062017-01-16 15:31:38 +0900251 if (on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900252 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Jaehoon Chung92004a062017-01-16 15:31:38 +0900253 else
Jingoo Han4b1ced82013-07-31 17:14:10 +0900254 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530255 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900256}
257
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900258static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900259{
260 u32 val;
261
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530262 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900263 val &= ~PCIE_CORE_RESET_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530264 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
265 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
266 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
267 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900268}
269
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900270static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900271{
272 u32 val;
273
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530274 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900275 val |= PCIE_CORE_RESET_ENABLE;
276
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530277 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
278 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
279 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
280 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
281 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
282 exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_MAC_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900283}
284
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900285static void exynos_pcie_assert_phy_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900286{
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530287 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_MAC_RESET);
288 exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_GLOBAL_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900289}
290
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900291static void exynos_pcie_deassert_phy_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900292{
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530293 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_GLOBAL_RESET);
294 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_PWR_RESET);
295 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET);
296 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_CMN_REG);
297 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSVREG_RESET);
298 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSV_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900299}
300
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900301static void exynos_pcie_power_on_phy(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900302{
303 u32 val;
304
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530305 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900306 val &= ~PCIE_PHY_COMMON_PD_CMN;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530307 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900308
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530309 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900310 val &= ~PCIE_PHY_TRSV0_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530311 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900312
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530313 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900314 val &= ~PCIE_PHY_TRSV1_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530315 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900316
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530317 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900318 val &= ~PCIE_PHY_TRSV2_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530319 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900320
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530321 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900322 val &= ~PCIE_PHY_TRSV3_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530323 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900324}
325
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900326static void exynos_pcie_power_off_phy(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900327{
328 u32 val;
329
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530330 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900331 val |= PCIE_PHY_COMMON_PD_CMN;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530332 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900333
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530334 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900335 val |= PCIE_PHY_TRSV0_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530336 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900337
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530338 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900339 val |= PCIE_PHY_TRSV1_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530340 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900341
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530342 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900343 val |= PCIE_PHY_TRSV2_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530344 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900345
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530346 val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900347 val |= PCIE_PHY_TRSV3_PD_TSV;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530348 exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900349}
350
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900351static void exynos_pcie_init_phy(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900352{
353 /* DCC feedback control off */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530354 exynos_pcie_writel(ep->mem_res->phy_base, 0x29, PCIE_PHY_DCC_FEEDBACK);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900355
356 /* set TX/RX impedance */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530357 exynos_pcie_writel(ep->mem_res->phy_base, 0xd5, PCIE_PHY_IMPEDANCE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900358
359 /* set 50Mhz PHY clock */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530360 exynos_pcie_writel(ep->mem_res->phy_base, 0x14, PCIE_PHY_PLL_DIV_0);
361 exynos_pcie_writel(ep->mem_res->phy_base, 0x12, PCIE_PHY_PLL_DIV_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900362
363 /* set TX Differential output for lane 0 */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530364 exynos_pcie_writel(ep->mem_res->phy_base, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900365
366 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530367 exynos_pcie_writel(ep->mem_res->phy_base, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900368
369 /* set RX clock and data recovery bandwidth */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530370 exynos_pcie_writel(ep->mem_res->phy_base, 0xe7, PCIE_PHY_PLL_BIAS);
371 exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV0_RXCDR);
372 exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV1_RXCDR);
373 exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV2_RXCDR);
374 exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV3_RXCDR);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900375
376 /* change TX Pre-emphasis Level Control for lanes */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530377 exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
378 exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
379 exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
380 exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900381
382 /* set LVCC */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530383 exynos_pcie_writel(ep->mem_res->phy_base, 0x20, PCIE_PHY_TRSV0_LVCC);
384 exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV1_LVCC);
385 exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV2_LVCC);
386 exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV3_LVCC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900387}
388
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900389static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900390{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600391 struct dw_pcie *pci = ep->pci;
392 struct device *dev = pci->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900393
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900394 if (ep->reset_gpio >= 0)
395 devm_gpio_request_one(dev, ep->reset_gpio,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900396 GPIOF_OUT_INIT_HIGH, "RESET");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900397}
398
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900399static int exynos_pcie_establish_link(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900400{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600401 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530402 struct pcie_port *pp = &pci->pp;
403 struct device *dev = pci->dev;
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500404 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900405
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530406 if (dw_pcie_link_up(pci)) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500407 dev_err(dev, "Link already up\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900408 return 0;
409 }
410
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900411 exynos_pcie_assert_core_reset(ep);
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900412
413 if (ep->using_phy) {
414 phy_reset(ep->phy);
415
416 exynos_pcie_writel(ep->mem_res->elbi_base, 1,
417 PCIE_PWR_RESET);
418
419 phy_power_on(ep->phy);
420 phy_init(ep->phy);
421 } else {
422 exynos_pcie_assert_phy_reset(ep);
423 exynos_pcie_deassert_phy_reset(ep);
424 exynos_pcie_power_on_phy(ep);
425 exynos_pcie_init_phy(ep);
426
427 /* pulse for common reset */
428 exynos_pcie_writel(ep->mem_res->block_base, 1,
429 PCIE_PHY_COMMON_RESET);
430 udelay(500);
431 exynos_pcie_writel(ep->mem_res->block_base, 0,
432 PCIE_PHY_COMMON_RESET);
433 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900434
435 /* pulse for common reset */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530436 exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900437 udelay(500);
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530438 exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900439
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900440 exynos_pcie_deassert_core_reset(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900441 dw_pcie_setup_rc(pp);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900442 exynos_pcie_assert_reset(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900443
444 /* assert LTSSM enable */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530445 exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
Seungwon Jeon058dd012013-08-29 21:35:56 +0900446 PCIE_APP_LTSSM_ENABLE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900447
448 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530449 if (!dw_pcie_wait_for_link(pci))
Joao Pinto886bc5c2016-03-10 14:44:35 -0600450 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900451
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900452 if (ep->using_phy) {
453 phy_power_off(ep->phy);
454 return -ETIMEDOUT;
455 }
456
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530457 while (exynos_pcie_readl(ep->mem_res->phy_base,
458 PCIE_PHY_PLL_LOCKED) == 0) {
459 val = exynos_pcie_readl(ep->mem_res->block_base,
460 PCIE_PHY_PLL_LOCKED);
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500461 dev_info(dev, "PLL Locked: 0x%x\n", val);
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500462 }
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900463 exynos_pcie_power_off_phy(ep);
Joao Pinto886bc5c2016-03-10 14:44:35 -0600464 return -ETIMEDOUT;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900465}
466
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900467static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900468{
469 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900470
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530471 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
472 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900473}
474
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900475static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900476{
477 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900478
479 /* enable INTX interrupt */
480 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
Jaehoon Chung01d06a92015-03-25 14:13:12 +0900481 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530482 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900483}
484
485static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
486{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900487 struct exynos_pcie *ep = arg;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900488
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900489 exynos_pcie_clear_irq_pulse(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900490 return IRQ_HANDLED;
491}
492
Jingoo Hanf342d942013-09-06 15:54:59 +0900493static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
494{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900495 struct exynos_pcie *ep = arg;
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600496 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530497 struct pcie_port *pp = &pci->pp;
Jingoo Hanf342d942013-09-06 15:54:59 +0900498
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100499 return dw_handle_msi_irq(pp);
Jingoo Hanf342d942013-09-06 15:54:59 +0900500}
501
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900502static void exynos_pcie_msi_init(struct exynos_pcie *ep)
Jingoo Hanf342d942013-09-06 15:54:59 +0900503{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600504 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530505 struct pcie_port *pp = &pci->pp;
Jingoo Hanf342d942013-09-06 15:54:59 +0900506 u32 val;
Jingoo Hanf342d942013-09-06 15:54:59 +0900507
508 dw_pcie_msi_init(pp);
509
510 /* enable MSI interrupt */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530511 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
Jingoo Hanf342d942013-09-06 15:54:59 +0900512 val |= IRQ_MSI_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530513 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
Jingoo Hanf342d942013-09-06 15:54:59 +0900514}
515
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900516static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900517{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900518 exynos_pcie_enable_irq_pulse(ep);
Jingoo Hanf342d942013-09-06 15:54:59 +0900519
520 if (IS_ENABLED(CONFIG_PCI_MSI))
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900521 exynos_pcie_msi_init(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900522}
523
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530524static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
525 u32 reg, size_t size)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900526{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600527 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaas446fc232016-08-17 14:17:58 -0500528 u32 val;
529
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900530 exynos_pcie_sideband_dbi_r_mode(ep, true);
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530531 dw_pcie_read(base + reg, size, &val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900532 exynos_pcie_sideband_dbi_r_mode(ep, false);
Bjorn Helgaas446fc232016-08-17 14:17:58 -0500533 return val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900534}
535
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530536static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
537 u32 reg, size_t size, u32 val)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900538{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600539 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500540
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900541 exynos_pcie_sideband_dbi_w_mode(ep, true);
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530542 dw_pcie_write(base + reg, size, val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900543 exynos_pcie_sideband_dbi_w_mode(ep, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900544}
545
546static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
547 u32 *val)
548{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530549 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600550 struct exynos_pcie *ep = to_exynos_pcie(pci);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900551 int ret;
552
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900553 exynos_pcie_sideband_dbi_r_mode(ep, true);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530554 ret = dw_pcie_read(pci->dbi_base + where, size, val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900555 exynos_pcie_sideband_dbi_r_mode(ep, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900556 return ret;
557}
558
559static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
560 u32 val)
561{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530562 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600563 struct exynos_pcie *ep = to_exynos_pcie(pci);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900564 int ret;
565
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900566 exynos_pcie_sideband_dbi_w_mode(ep, true);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530567 ret = dw_pcie_write(pci->dbi_base + where, size, val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900568 exynos_pcie_sideband_dbi_w_mode(ep, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900569 return ret;
570}
571
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530572static int exynos_pcie_link_up(struct dw_pcie *pci)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900573{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600574 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500575 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900576
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530577 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900578 if (val == PCIE_ELBI_LTSSM_ENABLE)
579 return 1;
580
581 return 0;
582}
583
Bjorn Andersson4a301762017-07-15 23:39:45 -0700584static int exynos_pcie_host_init(struct pcie_port *pp)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900585{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530586 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600587 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500588
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900589 exynos_pcie_establish_link(ep);
590 exynos_pcie_enable_interrupts(ep);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700591
592 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900593}
594
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800595static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900596 .rd_own_conf = exynos_pcie_rd_own_conf,
597 .wr_own_conf = exynos_pcie_wr_own_conf,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900598 .host_init = exynos_pcie_host_init,
599};
600
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900601static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
Jingoo Han70b3e892014-10-22 13:58:49 +0900602 struct platform_device *pdev)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900603{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600604 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530605 struct pcie_port *pp = &pci->pp;
606 struct device *dev = &pdev->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900607 int ret;
608
609 pp->irq = platform_get_irq(pdev, 1);
Fabio Estevam1df5a482017-08-31 14:52:01 -0300610 if (pp->irq < 0) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500611 dev_err(dev, "failed to get irq\n");
Fabio Estevam1df5a482017-08-31 14:52:01 -0300612 return pp->irq;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900613 }
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500614 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900615 IRQF_SHARED, "exynos-pcie", ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900616 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500617 dev_err(dev, "failed to request irq\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900618 return ret;
619 }
620
Jingoo Hanf342d942013-09-06 15:54:59 +0900621 if (IS_ENABLED(CONFIG_PCI_MSI)) {
622 pp->msi_irq = platform_get_irq(pdev, 0);
Fabio Estevam1df5a482017-08-31 14:52:01 -0300623 if (pp->msi_irq < 0) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500624 dev_err(dev, "failed to get msi irq\n");
Fabio Estevam1df5a482017-08-31 14:52:01 -0300625 return pp->msi_irq;
Jingoo Hanf342d942013-09-06 15:54:59 +0900626 }
627
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500628 ret = devm_request_irq(dev, pp->msi_irq,
Jingoo Hanf342d942013-09-06 15:54:59 +0900629 exynos_pcie_msi_irq_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200630 IRQF_SHARED | IRQF_NO_THREAD,
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900631 "exynos-pcie", ep);
Jingoo Hanf342d942013-09-06 15:54:59 +0900632 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500633 dev_err(dev, "failed to request msi irq\n");
Jingoo Hanf342d942013-09-06 15:54:59 +0900634 return ret;
635 }
636 }
637
Jingoo Han4b1ced82013-07-31 17:14:10 +0900638 pp->root_bus_nr = -1;
639 pp->ops = &exynos_pcie_host_ops;
640
Jingoo Han4b1ced82013-07-31 17:14:10 +0900641 ret = dw_pcie_host_init(pp);
642 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500643 dev_err(dev, "failed to initialize host\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900644 return ret;
645 }
646
647 return 0;
648}
649
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530650static const struct dw_pcie_ops dw_pcie_ops = {
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530651 .read_dbi = exynos_pcie_read_dbi,
652 .write_dbi = exynos_pcie_write_dbi,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530653 .link_up = exynos_pcie_link_up,
654};
655
Jingoo Han4b1ced82013-07-31 17:14:10 +0900656static int __init exynos_pcie_probe(struct platform_device *pdev)
657{
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500658 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530659 struct dw_pcie *pci;
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900660 struct exynos_pcie *ep;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500661 struct device_node *np = dev->of_node;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900662 int ret;
663
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900664 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
665 if (!ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900666 return -ENOMEM;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900667
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530668 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
669 if (!pci)
670 return -ENOMEM;
671
672 pci->dev = dev;
673 pci->ops = &dw_pcie_ops;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900674
Guenter Roeckc0464062017-02-25 02:08:12 -0800675 ep->pci = pci;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530676 ep->ops = (const struct exynos_pcie_ops *)
677 of_device_get_match_data(dev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900678
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900679 ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900680
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900681 /* Assume that controller doesn't use the PHY framework */
682 ep->using_phy = false;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900683
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900684 ep->phy = devm_of_phy_get(dev, np, NULL);
685 if (IS_ERR(ep->phy)) {
686 if (PTR_ERR(ep->phy) == -EPROBE_DEFER)
687 return PTR_ERR(ep->phy);
688 dev_warn(dev, "Use the 'phy' property. Current DT of pci-exynos was deprecated!!\n");
689 } else
690 ep->using_phy = true;
691
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530692 if (ep->ops && ep->ops->get_mem_resources) {
693 ret = ep->ops->get_mem_resources(pdev, ep);
694 if (ret)
695 return ret;
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800696 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900697
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530698 if (ep->ops && ep->ops->get_clk_resources) {
699 ret = ep->ops->get_clk_resources(ep);
700 if (ret)
701 return ret;
702 ret = ep->ops->init_clk_resources(ep);
703 if (ret)
704 return ret;
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800705 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900706
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600707 platform_set_drvdata(pdev, ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900708
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900709 ret = exynos_add_pcie_port(ep, pdev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900710 if (ret < 0)
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530711 goto fail_probe;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900712
Jingoo Han4b1ced82013-07-31 17:14:10 +0900713 return 0;
714
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530715fail_probe:
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900716 if (ep->using_phy)
717 phy_exit(ep->phy);
718
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530719 if (ep->ops && ep->ops->deinit_clk_resources)
720 ep->ops->deinit_clk_resources(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900721 return ret;
722}
723
724static int __exit exynos_pcie_remove(struct platform_device *pdev)
725{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900726 struct exynos_pcie *ep = platform_get_drvdata(pdev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900727
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530728 if (ep->ops && ep->ops->deinit_clk_resources)
729 ep->ops->deinit_clk_resources(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900730
731 return 0;
732}
733
734static const struct of_device_id exynos_pcie_of_match[] = {
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530735 {
736 .compatible = "samsung,exynos5440-pcie",
737 .data = &exynos5440_pcie_ops
738 },
Jingoo Han4b1ced82013-07-31 17:14:10 +0900739 {},
740};
Jingoo Han4b1ced82013-07-31 17:14:10 +0900741
742static struct platform_driver exynos_pcie_driver = {
743 .remove = __exit_p(exynos_pcie_remove),
744 .driver = {
745 .name = "exynos-pcie",
Sachin Kamateb363092013-10-21 14:36:43 +0530746 .of_match_table = exynos_pcie_of_match,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900747 },
748};
749
750/* Exynos PCIe driver does not allow module unload */
751
Jingoo Han70b3e892014-10-22 13:58:49 +0900752static int __init exynos_pcie_init(void)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900753{
754 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
755}
Jingoo Han70b3e892014-10-22 13:58:49 +0900756subsys_initcall(exynos_pcie_init);