blob: 4cc1e5df8c79960a279c9ea3f10174c83aa69886 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Jingoo Han4b1ced82013-07-31 17:14:10 +09002/*
3 * PCIe host controller driver for Samsung EXYNOS SoCs
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com
7 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
Jingoo Han4b1ced82013-07-31 17:14:10 +09009 */
10
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/gpio.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
Paul Gortmakercaf55482016-08-22 17:59:47 -040016#include <linux/init.h>
Niyas Ahmed S T32784782017-02-01 10:13:06 +053017#include <linux/of_device.h>
Jingoo Han4b1ced82013-07-31 17:14:10 +090018#include <linux/of_gpio.h>
19#include <linux/pci.h>
20#include <linux/platform_device.h>
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +090021#include <linux/phy/phy.h>
Jingoo Han4b1ced82013-07-31 17:14:10 +090022#include <linux/resource.h>
23#include <linux/signal.h>
24#include <linux/types.h>
25
26#include "pcie-designware.h"
27
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053028#define to_exynos_pcie(x) dev_get_drvdata((x)->dev)
Jingoo Han4b1ced82013-07-31 17:14:10 +090029
Jingoo Han4b1ced82013-07-31 17:14:10 +090030/* PCIe ELBI registers */
31#define PCIE_IRQ_PULSE 0x000
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090032#define IRQ_INTA_ASSERT BIT(0)
33#define IRQ_INTB_ASSERT BIT(2)
34#define IRQ_INTC_ASSERT BIT(4)
35#define IRQ_INTD_ASSERT BIT(6)
Jingoo Han4b1ced82013-07-31 17:14:10 +090036#define PCIE_IRQ_LEVEL 0x004
37#define PCIE_IRQ_SPECIAL 0x008
38#define PCIE_IRQ_EN_PULSE 0x00c
39#define PCIE_IRQ_EN_LEVEL 0x010
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090040#define IRQ_MSI_ENABLE BIT(2)
Jingoo Han4b1ced82013-07-31 17:14:10 +090041#define PCIE_IRQ_EN_SPECIAL 0x014
42#define PCIE_PWR_RESET 0x018
43#define PCIE_CORE_RESET 0x01c
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090044#define PCIE_CORE_RESET_ENABLE BIT(0)
Jingoo Han4b1ced82013-07-31 17:14:10 +090045#define PCIE_STICKY_RESET 0x020
46#define PCIE_NONSTICKY_RESET 0x024
47#define PCIE_APP_INIT_RESET 0x028
48#define PCIE_APP_LTSSM_ENABLE 0x02c
49#define PCIE_ELBI_RDLH_LINKUP 0x064
50#define PCIE_ELBI_LTSSM_ENABLE 0x1
51#define PCIE_ELBI_SLV_AWMISC 0x11c
52#define PCIE_ELBI_SLV_ARMISC 0x120
Jaehoon Chung2681c0e2017-01-16 15:31:37 +090053#define PCIE_ELBI_SLV_DBI_ENABLE BIT(21)
Jingoo Han4b1ced82013-07-31 17:14:10 +090054
Niyas Ahmed S T32784782017-02-01 10:13:06 +053055struct exynos_pcie_mem_res {
56 void __iomem *elbi_base; /* DT 0th resource: PCIe CTRL */
Niyas Ahmed S T32784782017-02-01 10:13:06 +053057};
58
59struct exynos_pcie_clk_res {
60 struct clk *clk;
61 struct clk *bus_clk;
62};
63
64struct exynos_pcie {
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -060065 struct dw_pcie *pci;
Niyas Ahmed S T32784782017-02-01 10:13:06 +053066 struct exynos_pcie_mem_res *mem_res;
67 struct exynos_pcie_clk_res *clk_res;
68 const struct exynos_pcie_ops *ops;
69 int reset_gpio;
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +090070
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +090071 struct phy *phy;
Niyas Ahmed S T32784782017-02-01 10:13:06 +053072};
73
74struct exynos_pcie_ops {
75 int (*get_mem_resources)(struct platform_device *pdev,
76 struct exynos_pcie *ep);
77 int (*get_clk_resources)(struct exynos_pcie *ep);
78 int (*init_clk_resources)(struct exynos_pcie *ep);
79 void (*deinit_clk_resources)(struct exynos_pcie *ep);
80};
81
82static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
83 struct exynos_pcie *ep)
Seungwon Jeon058dd012013-08-29 21:35:56 +090084{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -060085 struct dw_pcie *pci = ep->pci;
86 struct device *dev = pci->dev;
Niyas Ahmed S T32784782017-02-01 10:13:06 +053087 struct resource *res;
Niyas Ahmed S T32784782017-02-01 10:13:06 +053088
89 ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
90 if (!ep->mem_res)
91 return -ENOMEM;
92
93 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 ep->mem_res->elbi_base = devm_ioremap_resource(dev, res);
95 if (IS_ERR(ep->mem_res->elbi_base))
96 return PTR_ERR(ep->mem_res->elbi_base);
97
Niyas Ahmed S T32784782017-02-01 10:13:06 +053098 return 0;
Seungwon Jeon058dd012013-08-29 21:35:56 +090099}
100
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530101static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900102{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600103 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530104 struct device *dev = pci->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900105
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530106 ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
107 if (!ep->clk_res)
108 return -ENOMEM;
109
110 ep->clk_res->clk = devm_clk_get(dev, "pcie");
111 if (IS_ERR(ep->clk_res->clk)) {
112 dev_err(dev, "Failed to get pcie rc clock\n");
113 return PTR_ERR(ep->clk_res->clk);
114 }
115
116 ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
117 if (IS_ERR(ep->clk_res->bus_clk)) {
118 dev_err(dev, "Failed to get pcie bus clock\n");
119 return PTR_ERR(ep->clk_res->bus_clk);
120 }
121
122 return 0;
123}
124
125static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
126{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600127 struct dw_pcie *pci = ep->pci;
128 struct device *dev = pci->dev;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530129 int ret;
130
131 ret = clk_prepare_enable(ep->clk_res->clk);
132 if (ret) {
133 dev_err(dev, "cannot enable pcie rc clock");
134 return ret;
135 }
136
137 ret = clk_prepare_enable(ep->clk_res->bus_clk);
138 if (ret) {
139 dev_err(dev, "cannot enable pcie bus clock");
140 goto err_bus_clk;
141 }
142
143 return 0;
144
145err_bus_clk:
146 clk_disable_unprepare(ep->clk_res->clk);
147
148 return ret;
149}
150
151static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
152{
153 clk_disable_unprepare(ep->clk_res->bus_clk);
154 clk_disable_unprepare(ep->clk_res->clk);
155}
156
157static const struct exynos_pcie_ops exynos5440_pcie_ops = {
158 .get_mem_resources = exynos5440_pcie_get_mem_resources,
159 .get_clk_resources = exynos5440_pcie_get_clk_resources,
160 .init_clk_resources = exynos5440_pcie_init_clk_resources,
161 .deinit_clk_resources = exynos5440_pcie_deinit_clk_resources,
162};
163
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900164static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900165{
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900166 writel(val, base + reg);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900167}
168
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900169static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900170{
Jaehoon Chungd6da7d92017-01-16 15:31:35 +0900171 return readl(base + reg);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900172}
173
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900174static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900175{
176 u32 val;
177
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530178 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
Jaehoon Chung92004a062017-01-16 15:31:38 +0900179 if (on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900180 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Jaehoon Chung92004a062017-01-16 15:31:38 +0900181 else
Jingoo Han4b1ced82013-07-31 17:14:10 +0900182 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530183 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900184}
185
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900186static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900187{
188 u32 val;
189
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530190 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
Jaehoon Chung92004a062017-01-16 15:31:38 +0900191 if (on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900192 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Jaehoon Chung92004a062017-01-16 15:31:38 +0900193 else
Jingoo Han4b1ced82013-07-31 17:14:10 +0900194 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530195 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900196}
197
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900198static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900199{
200 u32 val;
201
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530202 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900203 val &= ~PCIE_CORE_RESET_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530204 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
205 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
206 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
207 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900208}
209
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900210static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900211{
212 u32 val;
213
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530214 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900215 val |= PCIE_CORE_RESET_ENABLE;
216
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530217 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
218 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
219 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
220 exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
221 exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900222}
223
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900224static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900225{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600226 struct dw_pcie *pci = ep->pci;
227 struct device *dev = pci->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900228
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900229 if (ep->reset_gpio >= 0)
230 devm_gpio_request_one(dev, ep->reset_gpio,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900231 GPIOF_OUT_INIT_HIGH, "RESET");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900232}
233
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900234static int exynos_pcie_establish_link(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900235{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600236 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530237 struct pcie_port *pp = &pci->pp;
238 struct device *dev = pci->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900239
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530240 if (dw_pcie_link_up(pci)) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500241 dev_err(dev, "Link already up\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900242 return 0;
243 }
244
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900245 exynos_pcie_assert_core_reset(ep);
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900246
Jaehoon Chung83f4f3f2017-12-27 18:43:27 +0900247 phy_reset(ep->phy);
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900248
Jaehoon Chung83f4f3f2017-12-27 18:43:27 +0900249 exynos_pcie_writel(ep->mem_res->elbi_base, 1,
250 PCIE_PWR_RESET);
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900251
Jaehoon Chung83f4f3f2017-12-27 18:43:27 +0900252 phy_power_on(ep->phy);
253 phy_init(ep->phy);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900254
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900255 exynos_pcie_deassert_core_reset(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900256 dw_pcie_setup_rc(pp);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900257 exynos_pcie_assert_reset(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900258
259 /* assert LTSSM enable */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530260 exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
Seungwon Jeon058dd012013-08-29 21:35:56 +0900261 PCIE_APP_LTSSM_ENABLE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900262
263 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530264 if (!dw_pcie_wait_for_link(pci))
Joao Pinto886bc5c2016-03-10 14:44:35 -0600265 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900266
Jaehoon Chung83f4f3f2017-12-27 18:43:27 +0900267 phy_power_off(ep->phy);
Joao Pinto886bc5c2016-03-10 14:44:35 -0600268 return -ETIMEDOUT;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900269}
270
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900271static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900272{
273 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900274
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530275 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
276 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900277}
278
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900279static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900280{
281 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900282
283 /* enable INTX interrupt */
284 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
Jaehoon Chung01d06a92015-03-25 14:13:12 +0900285 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530286 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900287}
288
289static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
290{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900291 struct exynos_pcie *ep = arg;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900292
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900293 exynos_pcie_clear_irq_pulse(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900294 return IRQ_HANDLED;
295}
296
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900297static void exynos_pcie_msi_init(struct exynos_pcie *ep)
Jingoo Hanf342d942013-09-06 15:54:59 +0900298{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600299 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530300 struct pcie_port *pp = &pci->pp;
Jingoo Hanf342d942013-09-06 15:54:59 +0900301 u32 val;
Jingoo Hanf342d942013-09-06 15:54:59 +0900302
303 dw_pcie_msi_init(pp);
304
305 /* enable MSI interrupt */
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530306 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
Jingoo Hanf342d942013-09-06 15:54:59 +0900307 val |= IRQ_MSI_ENABLE;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530308 exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
Jingoo Hanf342d942013-09-06 15:54:59 +0900309}
310
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900311static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900312{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900313 exynos_pcie_enable_irq_pulse(ep);
Jingoo Hanf342d942013-09-06 15:54:59 +0900314
315 if (IS_ENABLED(CONFIG_PCI_MSI))
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900316 exynos_pcie_msi_init(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900317}
318
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530319static u32 exynos_pcie_read_dbi(struct dw_pcie *pci, void __iomem *base,
320 u32 reg, size_t size)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900321{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600322 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaas446fc232016-08-17 14:17:58 -0500323 u32 val;
324
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900325 exynos_pcie_sideband_dbi_r_mode(ep, true);
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530326 dw_pcie_read(base + reg, size, &val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900327 exynos_pcie_sideband_dbi_r_mode(ep, false);
Bjorn Helgaas446fc232016-08-17 14:17:58 -0500328 return val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900329}
330
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530331static void exynos_pcie_write_dbi(struct dw_pcie *pci, void __iomem *base,
332 u32 reg, size_t size, u32 val)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900333{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600334 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500335
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900336 exynos_pcie_sideband_dbi_w_mode(ep, true);
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530337 dw_pcie_write(base + reg, size, val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900338 exynos_pcie_sideband_dbi_w_mode(ep, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900339}
340
341static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
342 u32 *val)
343{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530344 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600345 struct exynos_pcie *ep = to_exynos_pcie(pci);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900346 int ret;
347
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900348 exynos_pcie_sideband_dbi_r_mode(ep, true);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530349 ret = dw_pcie_read(pci->dbi_base + where, size, val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900350 exynos_pcie_sideband_dbi_r_mode(ep, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900351 return ret;
352}
353
354static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
355 u32 val)
356{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530357 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600358 struct exynos_pcie *ep = to_exynos_pcie(pci);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900359 int ret;
360
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900361 exynos_pcie_sideband_dbi_w_mode(ep, true);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530362 ret = dw_pcie_write(pci->dbi_base + where, size, val);
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900363 exynos_pcie_sideband_dbi_w_mode(ep, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900364 return ret;
365}
366
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530367static int exynos_pcie_link_up(struct dw_pcie *pci)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900368{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600369 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500370 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900371
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530372 val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900373 if (val == PCIE_ELBI_LTSSM_ENABLE)
374 return 1;
375
376 return 0;
377}
378
Bjorn Andersson4a301762017-07-15 23:39:45 -0700379static int exynos_pcie_host_init(struct pcie_port *pp)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900380{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530381 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600382 struct exynos_pcie *ep = to_exynos_pcie(pci);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500383
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900384 exynos_pcie_establish_link(ep);
385 exynos_pcie_enable_interrupts(ep);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700386
387 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900388}
389
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800390static const struct dw_pcie_host_ops exynos_pcie_host_ops = {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900391 .rd_own_conf = exynos_pcie_rd_own_conf,
392 .wr_own_conf = exynos_pcie_wr_own_conf,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900393 .host_init = exynos_pcie_host_init,
394};
395
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900396static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
Jingoo Han70b3e892014-10-22 13:58:49 +0900397 struct platform_device *pdev)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900398{
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600399 struct dw_pcie *pci = ep->pci;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530400 struct pcie_port *pp = &pci->pp;
401 struct device *dev = &pdev->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900402 int ret;
403
404 pp->irq = platform_get_irq(pdev, 1);
Fabio Estevam1df5a482017-08-31 14:52:01 -0300405 if (pp->irq < 0) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500406 dev_err(dev, "failed to get irq\n");
Fabio Estevam1df5a482017-08-31 14:52:01 -0300407 return pp->irq;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900408 }
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500409 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900410 IRQF_SHARED, "exynos-pcie", ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900411 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500412 dev_err(dev, "failed to request irq\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900413 return ret;
414 }
415
Jingoo Hanf342d942013-09-06 15:54:59 +0900416 if (IS_ENABLED(CONFIG_PCI_MSI)) {
417 pp->msi_irq = platform_get_irq(pdev, 0);
Fabio Estevam1df5a482017-08-31 14:52:01 -0300418 if (pp->msi_irq < 0) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500419 dev_err(dev, "failed to get msi irq\n");
Fabio Estevam1df5a482017-08-31 14:52:01 -0300420 return pp->msi_irq;
Jingoo Hanf342d942013-09-06 15:54:59 +0900421 }
Jingoo Hanf342d942013-09-06 15:54:59 +0900422 }
423
Jingoo Han4b1ced82013-07-31 17:14:10 +0900424 pp->root_bus_nr = -1;
425 pp->ops = &exynos_pcie_host_ops;
426
Jingoo Han4b1ced82013-07-31 17:14:10 +0900427 ret = dw_pcie_host_init(pp);
428 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500429 dev_err(dev, "failed to initialize host\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900430 return ret;
431 }
432
433 return 0;
434}
435
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530436static const struct dw_pcie_ops dw_pcie_ops = {
Kishon Vijay Abraham Ia509d7d2017-03-13 19:13:26 +0530437 .read_dbi = exynos_pcie_read_dbi,
438 .write_dbi = exynos_pcie_write_dbi,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530439 .link_up = exynos_pcie_link_up,
440};
441
Jingoo Han4b1ced82013-07-31 17:14:10 +0900442static int __init exynos_pcie_probe(struct platform_device *pdev)
443{
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500444 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530445 struct dw_pcie *pci;
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900446 struct exynos_pcie *ep;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500447 struct device_node *np = dev->of_node;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900448 int ret;
449
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900450 ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
451 if (!ep)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900452 return -ENOMEM;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900453
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530454 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
455 if (!pci)
456 return -ENOMEM;
457
458 pci->dev = dev;
459 pci->ops = &dw_pcie_ops;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900460
Guenter Roeckc0464062017-02-25 02:08:12 -0800461 ep->pci = pci;
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530462 ep->ops = (const struct exynos_pcie_ops *)
463 of_device_get_match_data(dev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900464
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900465 ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900466
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900467 ep->phy = devm_of_phy_get(dev, np, NULL);
468 if (IS_ERR(ep->phy)) {
469 if (PTR_ERR(ep->phy) == -EPROBE_DEFER)
470 return PTR_ERR(ep->phy);
Jaehoon Chung83f4f3f2017-12-27 18:43:27 +0900471
472 ep->phy = NULL;
473 }
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900474
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530475 if (ep->ops && ep->ops->get_mem_resources) {
476 ret = ep->ops->get_mem_resources(pdev, ep);
477 if (ret)
478 return ret;
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800479 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900480
Jaehoon Chungb5d6bc92018-01-22 11:28:54 +0900481 if (ep->ops && ep->ops->get_clk_resources &&
482 ep->ops->init_clk_resources) {
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530483 ret = ep->ops->get_clk_resources(ep);
484 if (ret)
485 return ret;
486 ret = ep->ops->init_clk_resources(ep);
487 if (ret)
488 return ret;
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800489 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900490
Bjorn Helgaasb2e6d302017-02-21 15:13:30 -0600491 platform_set_drvdata(pdev, ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900492
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900493 ret = exynos_add_pcie_port(ep, pdev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900494 if (ret < 0)
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530495 goto fail_probe;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900496
Jingoo Han4b1ced82013-07-31 17:14:10 +0900497 return 0;
498
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530499fail_probe:
Jaehoon Chung83f4f3f2017-12-27 18:43:27 +0900500 phy_exit(ep->phy);
Jaehoon Chunge7cd7ef2017-02-13 17:26:13 +0900501
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530502 if (ep->ops && ep->ops->deinit_clk_resources)
503 ep->ops->deinit_clk_resources(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900504 return ret;
505}
506
507static int __exit exynos_pcie_remove(struct platform_device *pdev)
508{
Jaehoon Chung4e0a90b382017-01-16 15:31:34 +0900509 struct exynos_pcie *ep = platform_get_drvdata(pdev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900510
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530511 if (ep->ops && ep->ops->deinit_clk_resources)
512 ep->ops->deinit_clk_resources(ep);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900513
514 return 0;
515}
516
517static const struct of_device_id exynos_pcie_of_match[] = {
Niyas Ahmed S T32784782017-02-01 10:13:06 +0530518 {
519 .compatible = "samsung,exynos5440-pcie",
520 .data = &exynos5440_pcie_ops
521 },
Jingoo Han4b1ced82013-07-31 17:14:10 +0900522 {},
523};
Jingoo Han4b1ced82013-07-31 17:14:10 +0900524
525static struct platform_driver exynos_pcie_driver = {
526 .remove = __exit_p(exynos_pcie_remove),
527 .driver = {
528 .name = "exynos-pcie",
Sachin Kamateb363092013-10-21 14:36:43 +0530529 .of_match_table = exynos_pcie_of_match,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900530 },
531};
532
533/* Exynos PCIe driver does not allow module unload */
534
Jingoo Han70b3e892014-10-22 13:58:49 +0900535static int __init exynos_pcie_init(void)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900536{
537 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
538}
Jingoo Han70b3e892014-10-22 13:58:49 +0900539subsys_initcall(exynos_pcie_init);