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