Documentation: binding: Modify the exynos5440 PCIe binding
[platform/kernel/linux-starfive.git] / drivers / pci / host / pci-exynos.c
1 /*
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>
19 #include <linux/init.h>
20 #include <linux/of_device.h>
21 #include <linux/of_gpio.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/resource.h>
25 #include <linux/signal.h>
26 #include <linux/types.h>
27
28 #include "pcie-designware.h"
29
30 #define to_exynos_pcie(x)       container_of(x, struct exynos_pcie, pp)
31
32 /* PCIe ELBI registers */
33 #define PCIE_IRQ_PULSE                  0x000
34 #define IRQ_INTA_ASSERT                 BIT(0)
35 #define IRQ_INTB_ASSERT                 BIT(2)
36 #define IRQ_INTC_ASSERT                 BIT(4)
37 #define IRQ_INTD_ASSERT                 BIT(6)
38 #define PCIE_IRQ_LEVEL                  0x004
39 #define PCIE_IRQ_SPECIAL                0x008
40 #define PCIE_IRQ_EN_PULSE               0x00c
41 #define PCIE_IRQ_EN_LEVEL               0x010
42 #define IRQ_MSI_ENABLE                  BIT(2)
43 #define PCIE_IRQ_EN_SPECIAL             0x014
44 #define PCIE_PWR_RESET                  0x018
45 #define PCIE_CORE_RESET                 0x01c
46 #define PCIE_CORE_RESET_ENABLE          BIT(0)
47 #define PCIE_STICKY_RESET               0x020
48 #define PCIE_NONSTICKY_RESET            0x024
49 #define PCIE_APP_INIT_RESET             0x028
50 #define PCIE_APP_LTSSM_ENABLE           0x02c
51 #define PCIE_ELBI_RDLH_LINKUP           0x064
52 #define PCIE_ELBI_LTSSM_ENABLE          0x1
53 #define PCIE_ELBI_SLV_AWMISC            0x11c
54 #define PCIE_ELBI_SLV_ARMISC            0x120
55 #define PCIE_ELBI_SLV_DBI_ENABLE        BIT(21)
56
57 /* PCIe Purple registers */
58 #define PCIE_PHY_GLOBAL_RESET           0x000
59 #define PCIE_PHY_COMMON_RESET           0x004
60 #define PCIE_PHY_CMN_REG                0x008
61 #define PCIE_PHY_MAC_RESET              0x00c
62 #define PCIE_PHY_PLL_LOCKED             0x010
63 #define PCIE_PHY_TRSVREG_RESET          0x020
64 #define PCIE_PHY_TRSV_RESET             0x024
65
66 /* PCIe PHY registers */
67 #define PCIE_PHY_IMPEDANCE              0x004
68 #define PCIE_PHY_PLL_DIV_0              0x008
69 #define PCIE_PHY_PLL_BIAS               0x00c
70 #define PCIE_PHY_DCC_FEEDBACK           0x014
71 #define PCIE_PHY_PLL_DIV_1              0x05c
72 #define PCIE_PHY_COMMON_POWER           0x064
73 #define PCIE_PHY_COMMON_PD_CMN          BIT(3)
74 #define PCIE_PHY_TRSV0_EMP_LVL          0x084
75 #define PCIE_PHY_TRSV0_DRV_LVL          0x088
76 #define PCIE_PHY_TRSV0_RXCDR            0x0ac
77 #define PCIE_PHY_TRSV0_POWER            0x0c4
78 #define PCIE_PHY_TRSV0_PD_TSV           BIT(7)
79 #define PCIE_PHY_TRSV0_LVCC             0x0dc
80 #define PCIE_PHY_TRSV1_EMP_LVL          0x144
81 #define PCIE_PHY_TRSV1_RXCDR            0x16c
82 #define PCIE_PHY_TRSV1_POWER            0x184
83 #define PCIE_PHY_TRSV1_PD_TSV           BIT(7)
84 #define PCIE_PHY_TRSV1_LVCC             0x19c
85 #define PCIE_PHY_TRSV2_EMP_LVL          0x204
86 #define PCIE_PHY_TRSV2_RXCDR            0x22c
87 #define PCIE_PHY_TRSV2_POWER            0x244
88 #define PCIE_PHY_TRSV2_PD_TSV           BIT(7)
89 #define PCIE_PHY_TRSV2_LVCC             0x25c
90 #define PCIE_PHY_TRSV3_EMP_LVL          0x2c4
91 #define PCIE_PHY_TRSV3_RXCDR            0x2ec
92 #define PCIE_PHY_TRSV3_POWER            0x304
93 #define PCIE_PHY_TRSV3_PD_TSV           BIT(7)
94 #define PCIE_PHY_TRSV3_LVCC             0x31c
95
96 struct exynos_pcie_mem_res {
97         void __iomem *elbi_base;   /* DT 0th resource: PCIe CTRL */
98         void __iomem *phy_base;    /* DT 1st resource: PHY CTRL */
99         void __iomem *block_base;  /* DT 2nd resource: PHY ADDITIONAL CTRL */
100 };
101
102 struct exynos_pcie_clk_res {
103         struct clk *clk;
104         struct clk *bus_clk;
105 };
106
107 struct exynos_pcie {
108         struct pcie_port                pp;
109         struct exynos_pcie_mem_res      *mem_res;
110         struct exynos_pcie_clk_res      *clk_res;
111         const struct exynos_pcie_ops    *ops;
112         int                             reset_gpio;
113 };
114
115 struct exynos_pcie_ops {
116         int (*get_mem_resources)(struct platform_device *pdev,
117                         struct exynos_pcie *ep);
118         int (*get_clk_resources)(struct exynos_pcie *ep);
119         int (*init_clk_resources)(struct exynos_pcie *ep);
120         void (*deinit_clk_resources)(struct exynos_pcie *ep);
121 };
122
123 static int exynos5440_pcie_get_mem_resources(struct platform_device *pdev,
124                                              struct exynos_pcie *ep)
125 {
126         struct resource *res;
127         struct device *dev = ep->pp.dev;
128
129         ep->mem_res = devm_kzalloc(dev, sizeof(*ep->mem_res), GFP_KERNEL);
130         if (!ep->mem_res)
131                 return -ENOMEM;
132
133         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134         ep->mem_res->elbi_base = devm_ioremap_resource(dev, res);
135         if (IS_ERR(ep->mem_res->elbi_base))
136                 return PTR_ERR(ep->mem_res->elbi_base);
137
138         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
139         ep->mem_res->phy_base = devm_ioremap_resource(dev, res);
140         if (IS_ERR(ep->mem_res->phy_base))
141                 return PTR_ERR(ep->mem_res->phy_base);
142
143         res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
144         ep->mem_res->block_base = devm_ioremap_resource(dev, res);
145         if (IS_ERR(ep->mem_res->block_base))
146                 return PTR_ERR(ep->mem_res->block_base);
147
148         return 0;
149 }
150
151 static int exynos5440_pcie_get_clk_resources(struct exynos_pcie *ep)
152 {
153         struct device *dev = ep->pp.dev;
154
155         ep->clk_res = devm_kzalloc(dev, sizeof(*ep->clk_res), GFP_KERNEL);
156         if (!ep->clk_res)
157                 return -ENOMEM;
158
159         ep->clk_res->clk = devm_clk_get(dev, "pcie");
160         if (IS_ERR(ep->clk_res->clk)) {
161                 dev_err(dev, "Failed to get pcie rc clock\n");
162                 return PTR_ERR(ep->clk_res->clk);
163         }
164
165         ep->clk_res->bus_clk = devm_clk_get(dev, "pcie_bus");
166         if (IS_ERR(ep->clk_res->bus_clk)) {
167                 dev_err(dev, "Failed to get pcie bus clock\n");
168                 return PTR_ERR(ep->clk_res->bus_clk);
169         }
170
171         return 0;
172 }
173
174 static int exynos5440_pcie_init_clk_resources(struct exynos_pcie *ep)
175 {
176         struct device *dev = ep->pp.dev;
177         int ret;
178
179         ret = clk_prepare_enable(ep->clk_res->clk);
180         if (ret) {
181                 dev_err(dev, "cannot enable pcie rc clock");
182                 return ret;
183         }
184
185         ret = clk_prepare_enable(ep->clk_res->bus_clk);
186         if (ret) {
187                 dev_err(dev, "cannot enable pcie bus clock");
188                 goto err_bus_clk;
189         }
190
191         return 0;
192
193 err_bus_clk:
194         clk_disable_unprepare(ep->clk_res->clk);
195
196         return ret;
197 }
198
199 static void exynos5440_pcie_deinit_clk_resources(struct exynos_pcie *ep)
200 {
201         clk_disable_unprepare(ep->clk_res->bus_clk);
202         clk_disable_unprepare(ep->clk_res->clk);
203 }
204
205 static const struct exynos_pcie_ops exynos5440_pcie_ops = {
206         .get_mem_resources      = exynos5440_pcie_get_mem_resources,
207         .get_clk_resources      = exynos5440_pcie_get_clk_resources,
208         .init_clk_resources     = exynos5440_pcie_init_clk_resources,
209         .deinit_clk_resources   = exynos5440_pcie_deinit_clk_resources,
210 };
211
212 static void exynos_pcie_writel(void __iomem *base, u32 val, u32 reg)
213 {
214         writel(val, base + reg);
215 }
216
217 static u32 exynos_pcie_readl(void __iomem *base, u32 reg)
218 {
219         return readl(base + reg);
220 }
221
222 static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *ep, bool on)
223 {
224         u32 val;
225
226         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_AWMISC);
227         if (on)
228                 val |= PCIE_ELBI_SLV_DBI_ENABLE;
229         else
230                 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
231         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_AWMISC);
232 }
233
234 static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *ep, bool on)
235 {
236         u32 val;
237
238         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_SLV_ARMISC);
239         if (on)
240                 val |= PCIE_ELBI_SLV_DBI_ENABLE;
241         else
242                 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
243         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_ELBI_SLV_ARMISC);
244 }
245
246 static void exynos_pcie_assert_core_reset(struct exynos_pcie *ep)
247 {
248         u32 val;
249
250         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
251         val &= ~PCIE_CORE_RESET_ENABLE;
252         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
253         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_PWR_RESET);
254         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_STICKY_RESET);
255         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_NONSTICKY_RESET);
256 }
257
258 static void exynos_pcie_deassert_core_reset(struct exynos_pcie *ep)
259 {
260         u32 val;
261
262         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_CORE_RESET);
263         val |= PCIE_CORE_RESET_ENABLE;
264
265         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_CORE_RESET);
266         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_STICKY_RESET);
267         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_NONSTICKY_RESET);
268         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_APP_INIT_RESET);
269         exynos_pcie_writel(ep->mem_res->elbi_base, 0, PCIE_APP_INIT_RESET);
270         exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_MAC_RESET);
271 }
272
273 static void exynos_pcie_assert_phy_reset(struct exynos_pcie *ep)
274 {
275         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_MAC_RESET);
276         exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_GLOBAL_RESET);
277 }
278
279 static void exynos_pcie_deassert_phy_reset(struct exynos_pcie *ep)
280 {
281         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_GLOBAL_RESET);
282         exynos_pcie_writel(ep->mem_res->elbi_base, 1, PCIE_PWR_RESET);
283         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET);
284         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_CMN_REG);
285         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSVREG_RESET);
286         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_TRSV_RESET);
287 }
288
289 static void exynos_pcie_power_on_phy(struct exynos_pcie *ep)
290 {
291         u32 val;
292
293         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER);
294         val &= ~PCIE_PHY_COMMON_PD_CMN;
295         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER);
296
297         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER);
298         val &= ~PCIE_PHY_TRSV0_PD_TSV;
299         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER);
300
301         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER);
302         val &= ~PCIE_PHY_TRSV1_PD_TSV;
303         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER);
304
305         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER);
306         val &= ~PCIE_PHY_TRSV2_PD_TSV;
307         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER);
308
309         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER);
310         val &= ~PCIE_PHY_TRSV3_PD_TSV;
311         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER);
312 }
313
314 static void exynos_pcie_power_off_phy(struct exynos_pcie *ep)
315 {
316         u32 val;
317
318         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_COMMON_POWER);
319         val |= PCIE_PHY_COMMON_PD_CMN;
320         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_COMMON_POWER);
321
322         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV0_POWER);
323         val |= PCIE_PHY_TRSV0_PD_TSV;
324         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV0_POWER);
325
326         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV1_POWER);
327         val |= PCIE_PHY_TRSV1_PD_TSV;
328         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV1_POWER);
329
330         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV2_POWER);
331         val |= PCIE_PHY_TRSV2_PD_TSV;
332         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV2_POWER);
333
334         val = exynos_pcie_readl(ep->mem_res->phy_base, PCIE_PHY_TRSV3_POWER);
335         val |= PCIE_PHY_TRSV3_PD_TSV;
336         exynos_pcie_writel(ep->mem_res->phy_base, val, PCIE_PHY_TRSV3_POWER);
337 }
338
339 static void exynos_pcie_init_phy(struct exynos_pcie *ep)
340 {
341         /* DCC feedback control off */
342         exynos_pcie_writel(ep->mem_res->phy_base, 0x29, PCIE_PHY_DCC_FEEDBACK);
343
344         /* set TX/RX impedance */
345         exynos_pcie_writel(ep->mem_res->phy_base, 0xd5, PCIE_PHY_IMPEDANCE);
346
347         /* set 50Mhz PHY clock */
348         exynos_pcie_writel(ep->mem_res->phy_base, 0x14, PCIE_PHY_PLL_DIV_0);
349         exynos_pcie_writel(ep->mem_res->phy_base, 0x12, PCIE_PHY_PLL_DIV_1);
350
351         /* set TX Differential output for lane 0 */
352         exynos_pcie_writel(ep->mem_res->phy_base, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
353
354         /* set TX Pre-emphasis Level Control for lane 0 to minimum */
355         exynos_pcie_writel(ep->mem_res->phy_base, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
356
357         /* set RX clock and data recovery bandwidth */
358         exynos_pcie_writel(ep->mem_res->phy_base, 0xe7, PCIE_PHY_PLL_BIAS);
359         exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV0_RXCDR);
360         exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV1_RXCDR);
361         exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV2_RXCDR);
362         exynos_pcie_writel(ep->mem_res->phy_base, 0x82, PCIE_PHY_TRSV3_RXCDR);
363
364         /* change TX Pre-emphasis Level Control for lanes */
365         exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
366         exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
367         exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
368         exynos_pcie_writel(ep->mem_res->phy_base, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
369
370         /* set LVCC */
371         exynos_pcie_writel(ep->mem_res->phy_base, 0x20, PCIE_PHY_TRSV0_LVCC);
372         exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV1_LVCC);
373         exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV2_LVCC);
374         exynos_pcie_writel(ep->mem_res->phy_base, 0xa0, PCIE_PHY_TRSV3_LVCC);
375 }
376
377 static void exynos_pcie_assert_reset(struct exynos_pcie *ep)
378 {
379         struct pcie_port *pp = &ep->pp;
380         struct device *dev = pp->dev;
381
382         if (ep->reset_gpio >= 0)
383                 devm_gpio_request_one(dev, ep->reset_gpio,
384                                 GPIOF_OUT_INIT_HIGH, "RESET");
385 }
386
387 static int exynos_pcie_establish_link(struct exynos_pcie *ep)
388 {
389         struct pcie_port *pp = &ep->pp;
390         struct device *dev = pp->dev;
391         u32 val;
392
393         if (dw_pcie_link_up(pp)) {
394                 dev_err(dev, "Link already up\n");
395                 return 0;
396         }
397
398         exynos_pcie_assert_core_reset(ep);
399         exynos_pcie_assert_phy_reset(ep);
400         exynos_pcie_deassert_phy_reset(ep);
401         exynos_pcie_power_on_phy(ep);
402         exynos_pcie_init_phy(ep);
403
404         /* pulse for common reset */
405         exynos_pcie_writel(ep->mem_res->block_base, 1, PCIE_PHY_COMMON_RESET);
406         udelay(500);
407         exynos_pcie_writel(ep->mem_res->block_base, 0, PCIE_PHY_COMMON_RESET);
408
409         exynos_pcie_deassert_core_reset(ep);
410         dw_pcie_setup_rc(pp);
411         exynos_pcie_assert_reset(ep);
412
413         /* assert LTSSM enable */
414         exynos_pcie_writel(ep->mem_res->elbi_base, PCIE_ELBI_LTSSM_ENABLE,
415                           PCIE_APP_LTSSM_ENABLE);
416
417         /* check if the link is up or not */
418         if (!dw_pcie_wait_for_link(pp))
419                 return 0;
420
421         while (exynos_pcie_readl(ep->mem_res->phy_base,
422                                 PCIE_PHY_PLL_LOCKED) == 0) {
423                 val = exynos_pcie_readl(ep->mem_res->block_base,
424                                 PCIE_PHY_PLL_LOCKED);
425                 dev_info(dev, "PLL Locked: 0x%x\n", val);
426         }
427         exynos_pcie_power_off_phy(ep);
428         return -ETIMEDOUT;
429 }
430
431 static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *ep)
432 {
433         u32 val;
434
435         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_PULSE);
436         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_PULSE);
437 }
438
439 static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *ep)
440 {
441         u32 val;
442
443         /* enable INTX interrupt */
444         val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
445                 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
446         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_PULSE);
447 }
448
449 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
450 {
451         struct exynos_pcie *ep = arg;
452
453         exynos_pcie_clear_irq_pulse(ep);
454         return IRQ_HANDLED;
455 }
456
457 static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
458 {
459         struct exynos_pcie *ep = arg;
460         struct pcie_port *pp = &ep->pp;
461
462         return dw_handle_msi_irq(pp);
463 }
464
465 static void exynos_pcie_msi_init(struct exynos_pcie *ep)
466 {
467         struct pcie_port *pp = &ep->pp;
468         u32 val;
469
470         dw_pcie_msi_init(pp);
471
472         /* enable MSI interrupt */
473         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_IRQ_EN_LEVEL);
474         val |= IRQ_MSI_ENABLE;
475         exynos_pcie_writel(ep->mem_res->elbi_base, val, PCIE_IRQ_EN_LEVEL);
476 }
477
478 static void exynos_pcie_enable_interrupts(struct exynos_pcie *ep)
479 {
480         exynos_pcie_enable_irq_pulse(ep);
481
482         if (IS_ENABLED(CONFIG_PCI_MSI))
483                 exynos_pcie_msi_init(ep);
484 }
485
486 static u32 exynos_pcie_readl_rc(struct pcie_port *pp, u32 reg)
487 {
488         struct exynos_pcie *ep = to_exynos_pcie(pp);
489         u32 val;
490
491         exynos_pcie_sideband_dbi_r_mode(ep, true);
492         val = readl(pp->dbi_base + reg);
493         exynos_pcie_sideband_dbi_r_mode(ep, false);
494         return val;
495 }
496
497 static void exynos_pcie_writel_rc(struct pcie_port *pp, u32 reg, u32 val)
498 {
499         struct exynos_pcie *ep = to_exynos_pcie(pp);
500
501         exynos_pcie_sideband_dbi_w_mode(ep, true);
502         writel(val, pp->dbi_base + reg);
503         exynos_pcie_sideband_dbi_w_mode(ep, false);
504 }
505
506 static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
507                                 u32 *val)
508 {
509         struct exynos_pcie *ep = to_exynos_pcie(pp);
510         int ret;
511
512         exynos_pcie_sideband_dbi_r_mode(ep, true);
513         ret = dw_pcie_cfg_read(pp->dbi_base + where, size, val);
514         exynos_pcie_sideband_dbi_r_mode(ep, false);
515         return ret;
516 }
517
518 static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
519                                 u32 val)
520 {
521         struct exynos_pcie *ep = to_exynos_pcie(pp);
522         int ret;
523
524         exynos_pcie_sideband_dbi_w_mode(ep, true);
525         ret = dw_pcie_cfg_write(pp->dbi_base + where, size, val);
526         exynos_pcie_sideband_dbi_w_mode(ep, false);
527         return ret;
528 }
529
530 static int exynos_pcie_link_up(struct pcie_port *pp)
531 {
532         struct exynos_pcie *ep = to_exynos_pcie(pp);
533         u32 val;
534
535         val = exynos_pcie_readl(ep->mem_res->elbi_base, PCIE_ELBI_RDLH_LINKUP);
536         if (val == PCIE_ELBI_LTSSM_ENABLE)
537                 return 1;
538
539         return 0;
540 }
541
542 static void exynos_pcie_host_init(struct pcie_port *pp)
543 {
544         struct exynos_pcie *ep = to_exynos_pcie(pp);
545
546         exynos_pcie_establish_link(ep);
547         exynos_pcie_enable_interrupts(ep);
548 }
549
550 static struct pcie_host_ops exynos_pcie_host_ops = {
551         .readl_rc = exynos_pcie_readl_rc,
552         .writel_rc = exynos_pcie_writel_rc,
553         .rd_own_conf = exynos_pcie_rd_own_conf,
554         .wr_own_conf = exynos_pcie_wr_own_conf,
555         .link_up = exynos_pcie_link_up,
556         .host_init = exynos_pcie_host_init,
557 };
558
559 static int __init exynos_add_pcie_port(struct exynos_pcie *ep,
560                                        struct platform_device *pdev)
561 {
562         struct pcie_port *pp = &ep->pp;
563         struct device *dev = pp->dev;
564         int ret;
565
566         pp->irq = platform_get_irq(pdev, 1);
567         if (!pp->irq) {
568                 dev_err(dev, "failed to get irq\n");
569                 return -ENODEV;
570         }
571         ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
572                                 IRQF_SHARED, "exynos-pcie", ep);
573         if (ret) {
574                 dev_err(dev, "failed to request irq\n");
575                 return ret;
576         }
577
578         if (IS_ENABLED(CONFIG_PCI_MSI)) {
579                 pp->msi_irq = platform_get_irq(pdev, 0);
580                 if (!pp->msi_irq) {
581                         dev_err(dev, "failed to get msi irq\n");
582                         return -ENODEV;
583                 }
584
585                 ret = devm_request_irq(dev, pp->msi_irq,
586                                         exynos_pcie_msi_irq_handler,
587                                         IRQF_SHARED | IRQF_NO_THREAD,
588                                         "exynos-pcie", ep);
589                 if (ret) {
590                         dev_err(dev, "failed to request msi irq\n");
591                         return ret;
592                 }
593         }
594
595         pp->root_bus_nr = -1;
596         pp->ops = &exynos_pcie_host_ops;
597
598         ret = dw_pcie_host_init(pp);
599         if (ret) {
600                 dev_err(dev, "failed to initialize host\n");
601                 return ret;
602         }
603
604         return 0;
605 }
606
607 static int __init exynos_pcie_probe(struct platform_device *pdev)
608 {
609         struct device *dev = &pdev->dev;
610         struct exynos_pcie *ep;
611         struct pcie_port *pp;
612         struct device_node *np = dev->of_node;
613         int ret;
614
615         ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
616         if (!ep)
617                 return -ENOMEM;
618
619         pp = &ep->pp;
620         pp->dev = dev;
621
622         ep->ops = (const struct exynos_pcie_ops *)
623                 of_device_get_match_data(dev);
624
625         ep->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
626
627         if (ep->ops && ep->ops->get_mem_resources) {
628                 ret = ep->ops->get_mem_resources(pdev, ep);
629                 if (ret)
630                         return ret;
631         }
632
633         if (ep->ops && ep->ops->get_clk_resources) {
634                 ret = ep->ops->get_clk_resources(ep);
635                 if (ret)
636                         return ret;
637                 ret = ep->ops->init_clk_resources(ep);
638                 if (ret)
639                         return ret;
640         }
641
642         ret = exynos_add_pcie_port(ep, pdev);
643         if (ret < 0)
644                 goto fail_probe;
645
646         platform_set_drvdata(pdev, ep);
647         return 0;
648
649 fail_probe:
650         if (ep->ops && ep->ops->deinit_clk_resources)
651                 ep->ops->deinit_clk_resources(ep);
652         return ret;
653 }
654
655 static int __exit exynos_pcie_remove(struct platform_device *pdev)
656 {
657         struct exynos_pcie *ep = platform_get_drvdata(pdev);
658
659         if (ep->ops && ep->ops->deinit_clk_resources)
660                 ep->ops->deinit_clk_resources(ep);
661
662         return 0;
663 }
664
665 static const struct of_device_id exynos_pcie_of_match[] = {
666         {
667                 .compatible = "samsung,exynos5440-pcie",
668                 .data = &exynos5440_pcie_ops
669         },
670         {},
671 };
672
673 static struct platform_driver exynos_pcie_driver = {
674         .remove         = __exit_p(exynos_pcie_remove),
675         .driver = {
676                 .name   = "exynos-pcie",
677                 .of_match_table = exynos_pcie_of_match,
678         },
679 };
680
681 /* Exynos PCIe driver does not allow module unload */
682
683 static int __init exynos_pcie_init(void)
684 {
685         return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
686 }
687 subsys_initcall(exynos_pcie_init);