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