]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/pci/host/pci-exynos.c
ff046d49ff6325fba13994fa6aed2e7dbe56ce1b
[karo-tx-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/module.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         void __iomem            *elbi_base;
33         void __iomem            *phy_base;
34         void __iomem            *block_base;
35         int                     reset_gpio;
36         struct clk              *clk;
37         struct clk              *bus_clk;
38         struct pcie_port        pp;
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 inline void exynos_elb_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
106 {
107         writel(val, pcie->elbi_base + reg);
108 }
109
110 static inline u32 exynos_elb_readl(struct exynos_pcie *pcie, u32 reg)
111 {
112         return readl(pcie->elbi_base + reg);
113 }
114
115 static inline void exynos_phy_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
116 {
117         writel(val, pcie->phy_base + reg);
118 }
119
120 static inline u32 exynos_phy_readl(struct exynos_pcie *pcie, u32 reg)
121 {
122         return readl(pcie->phy_base + reg);
123 }
124
125 static inline void exynos_blk_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
126 {
127         writel(val, pcie->block_base + reg);
128 }
129
130 static inline u32 exynos_blk_readl(struct exynos_pcie *pcie, u32 reg)
131 {
132         return readl(pcie->block_base + reg);
133 }
134
135 static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on)
136 {
137         u32 val;
138         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
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 pcie_port *pp, bool on)
152 {
153         u32 val;
154         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
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 pcie_port *pp)
168 {
169         u32 val;
170         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
171
172         val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
173         val &= ~PCIE_CORE_RESET_ENABLE;
174         exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
175         exynos_elb_writel(exynos_pcie, 0, PCIE_PWR_RESET);
176         exynos_elb_writel(exynos_pcie, 0, PCIE_STICKY_RESET);
177         exynos_elb_writel(exynos_pcie, 0, PCIE_NONSTICKY_RESET);
178 }
179
180 static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
181 {
182         u32 val;
183         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
184
185         val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
186         val |= PCIE_CORE_RESET_ENABLE;
187
188         exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
189         exynos_elb_writel(exynos_pcie, 1, PCIE_STICKY_RESET);
190         exynos_elb_writel(exynos_pcie, 1, PCIE_NONSTICKY_RESET);
191         exynos_elb_writel(exynos_pcie, 1, PCIE_APP_INIT_RESET);
192         exynos_elb_writel(exynos_pcie, 0, PCIE_APP_INIT_RESET);
193         exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_MAC_RESET);
194 }
195
196 static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
197 {
198         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
199
200         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
201         exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
202 }
203
204 static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
205 {
206         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
207
208         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_GLOBAL_RESET);
209         exynos_elb_writel(exynos_pcie, 1, PCIE_PWR_RESET);
210         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
211         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_CMN_REG);
212         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSVREG_RESET);
213         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSV_RESET);
214 }
215
216 static void exynos_pcie_power_on_phy(struct pcie_port *pp)
217 {
218         u32 val;
219         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
220
221         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
222         val &= ~PCIE_PHY_COMMON_PD_CMN;
223         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
224
225         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
226         val &= ~PCIE_PHY_TRSV0_PD_TSV;
227         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
228
229         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
230         val &= ~PCIE_PHY_TRSV1_PD_TSV;
231         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
232
233         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
234         val &= ~PCIE_PHY_TRSV2_PD_TSV;
235         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
236
237         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
238         val &= ~PCIE_PHY_TRSV3_PD_TSV;
239         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
240 }
241
242 static void exynos_pcie_power_off_phy(struct pcie_port *pp)
243 {
244         u32 val;
245         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
246
247         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
248         val |= PCIE_PHY_COMMON_PD_CMN;
249         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
250
251         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
252         val |= PCIE_PHY_TRSV0_PD_TSV;
253         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
254
255         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
256         val |= PCIE_PHY_TRSV1_PD_TSV;
257         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
258
259         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
260         val |= PCIE_PHY_TRSV2_PD_TSV;
261         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
262
263         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
264         val |= PCIE_PHY_TRSV3_PD_TSV;
265         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
266 }
267
268 static void exynos_pcie_power_on_phy(struct pcie_port *pp)
269 {
270         u32 val;
271         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
272
273         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
274         val &= ~PCIE_PHY_COMMON_PD_CMN;
275         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
276
277         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
278         val &= ~PCIE_PHY_TRSV0_PD_TSV;
279         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
280
281         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
282         val &= ~PCIE_PHY_TRSV1_PD_TSV;
283         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
284
285         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
286         val &= ~PCIE_PHY_TRSV2_PD_TSV;
287         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
288
289         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
290         val &= ~PCIE_PHY_TRSV3_PD_TSV;
291         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
292 }
293
294 static void exynos_pcie_power_off_phy(struct pcie_port *pp)
295 {
296         u32 val;
297         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
298
299         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
300         val |= PCIE_PHY_COMMON_PD_CMN;
301         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
302
303         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
304         val |= PCIE_PHY_TRSV0_PD_TSV;
305         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
306
307         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
308         val |= PCIE_PHY_TRSV1_PD_TSV;
309         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
310
311         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
312         val |= PCIE_PHY_TRSV2_PD_TSV;
313         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
314
315         val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
316         val |= PCIE_PHY_TRSV3_PD_TSV;
317         exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
318 }
319
320 static void exynos_pcie_init_phy(struct pcie_port *pp)
321 {
322         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
323
324         /* DCC feedback control off */
325         exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
326
327         /* set TX/RX impedance */
328         exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
329
330         /* set 50Mhz PHY clock */
331         exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
332         exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
333
334         /* set TX Differential output for lane 0 */
335         exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
336
337         /* set TX Pre-emphasis Level Control for lane 0 to minimum */
338         exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
339
340         /* set RX clock and data recovery bandwidth */
341         exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
342         exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
343         exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
344         exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
345         exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
346
347         /* change TX Pre-emphasis Level Control for lanes */
348         exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
349         exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
350         exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
351         exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
352
353         /* set LVCC */
354         exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
355         exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
356         exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
357         exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
358 }
359
360 static void exynos_pcie_assert_reset(struct pcie_port *pp)
361 {
362         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
363
364         if (exynos_pcie->reset_gpio >= 0)
365                 devm_gpio_request_one(pp->dev, exynos_pcie->reset_gpio,
366                                 GPIOF_OUT_INIT_HIGH, "RESET");
367         return;
368 }
369
370 static int exynos_pcie_establish_link(struct pcie_port *pp)
371 {
372         u32 val;
373         int count = 0;
374         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
375
376         if (dw_pcie_link_up(pp)) {
377                 dev_err(pp->dev, "Link already up\n");
378                 return 0;
379         }
380
381         /* assert reset signals */
382         exynos_pcie_assert_core_reset(pp);
383         exynos_pcie_assert_phy_reset(pp);
384
385         /* de-assert phy reset */
386         exynos_pcie_deassert_phy_reset(pp);
387
388         /* power on phy */
389         exynos_pcie_power_on_phy(pp);
390
391         /* initialize phy */
392         exynos_pcie_init_phy(pp);
393
394         /* pulse for common reset */
395         exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
396         udelay(500);
397         exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
398
399         /* de-assert core reset */
400         exynos_pcie_deassert_core_reset(pp);
401
402         /* setup root complex */
403         dw_pcie_setup_rc(pp);
404
405         /* assert reset signal */
406         exynos_pcie_assert_reset(pp);
407
408         /* assert LTSSM enable */
409         exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
410                           PCIE_APP_LTSSM_ENABLE);
411
412         /* check if the link is up or not */
413         while (!dw_pcie_link_up(pp)) {
414                 mdelay(100);
415                 count++;
416                 if (count == 10) {
417                         while (exynos_phy_readl(exynos_pcie,
418                                                 PCIE_PHY_PLL_LOCKED) == 0) {
419                                 val = exynos_blk_readl(exynos_pcie,
420                                                        PCIE_PHY_PLL_LOCKED);
421                                 dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
422                         }
423                         /* power off phy */
424                         exynos_pcie_power_off_phy(pp);
425
426                         dev_err(pp->dev, "PCIe Link Fail\n");
427                         return -EINVAL;
428                 }
429         }
430
431         dev_info(pp->dev, "Link up\n");
432
433         return 0;
434 }
435
436 static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
437 {
438         u32 val;
439         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
440
441         val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
442         exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
443         return;
444 }
445
446 static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
447 {
448         u32 val;
449         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
450
451         /* enable INTX interrupt */
452         val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
453                 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT,
454         exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
455         return;
456 }
457
458 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
459 {
460         struct pcie_port *pp = arg;
461
462         exynos_pcie_clear_irq_pulse(pp);
463         return IRQ_HANDLED;
464 }
465
466 static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
467 {
468         struct pcie_port *pp = arg;
469
470         return dw_handle_msi_irq(pp);
471 }
472
473 static void exynos_pcie_msi_init(struct pcie_port *pp)
474 {
475         u32 val;
476         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
477
478         dw_pcie_msi_init(pp);
479
480         /* enable MSI interrupt */
481         val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
482         val |= IRQ_MSI_ENABLE;
483         exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
484         return;
485 }
486
487 static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
488 {
489         exynos_pcie_enable_irq_pulse(pp);
490
491         if (IS_ENABLED(CONFIG_PCI_MSI))
492                 exynos_pcie_msi_init(pp);
493
494         return;
495 }
496
497 static inline void exynos_pcie_readl_rc(struct pcie_port *pp,
498                                         void __iomem *dbi_base, u32 *val)
499 {
500         exynos_pcie_sideband_dbi_r_mode(pp, true);
501         *val = readl(dbi_base);
502         exynos_pcie_sideband_dbi_r_mode(pp, false);
503         return;
504 }
505
506 static inline void exynos_pcie_writel_rc(struct pcie_port *pp,
507                                         u32 val, void __iomem *dbi_base)
508 {
509         exynos_pcie_sideband_dbi_w_mode(pp, true);
510         writel(val, dbi_base);
511         exynos_pcie_sideband_dbi_w_mode(pp, false);
512         return;
513 }
514
515 static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
516                                 u32 *val)
517 {
518         int ret;
519
520         exynos_pcie_sideband_dbi_r_mode(pp, true);
521         ret = dw_pcie_cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
522         exynos_pcie_sideband_dbi_r_mode(pp, false);
523         return ret;
524 }
525
526 static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
527                                 u32 val)
528 {
529         int ret;
530
531         exynos_pcie_sideband_dbi_w_mode(pp, true);
532         ret = dw_pcie_cfg_write(pp->dbi_base + (where & ~0x3),
533                         where, size, val);
534         exynos_pcie_sideband_dbi_w_mode(pp, false);
535         return ret;
536 }
537
538 static int exynos_pcie_link_up(struct pcie_port *pp)
539 {
540         struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
541         u32 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
542
543         if (val == PCIE_ELBI_LTSSM_ENABLE)
544                 return 1;
545
546         return 0;
547 }
548
549 static void exynos_pcie_host_init(struct pcie_port *pp)
550 {
551         exynos_pcie_establish_link(pp);
552         exynos_pcie_enable_interrupts(pp);
553 }
554
555 static struct pcie_host_ops exynos_pcie_host_ops = {
556         .readl_rc = exynos_pcie_readl_rc,
557         .writel_rc = exynos_pcie_writel_rc,
558         .rd_own_conf = exynos_pcie_rd_own_conf,
559         .wr_own_conf = exynos_pcie_wr_own_conf,
560         .link_up = exynos_pcie_link_up,
561         .host_init = exynos_pcie_host_init,
562 };
563
564 static int __init add_pcie_port(struct pcie_port *pp,
565                                 struct platform_device *pdev)
566 {
567         int ret;
568
569         pp->irq = platform_get_irq(pdev, 1);
570         if (!pp->irq) {
571                 dev_err(&pdev->dev, "failed to get irq\n");
572                 return -ENODEV;
573         }
574         ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
575                                 IRQF_SHARED, "exynos-pcie", pp);
576         if (ret) {
577                 dev_err(&pdev->dev, "failed to request irq\n");
578                 return ret;
579         }
580
581         if (IS_ENABLED(CONFIG_PCI_MSI)) {
582                 pp->msi_irq = platform_get_irq(pdev, 0);
583                 if (!pp->msi_irq) {
584                         dev_err(&pdev->dev, "failed to get msi irq\n");
585                         return -ENODEV;
586                 }
587
588                 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
589                                         exynos_pcie_msi_irq_handler,
590                                         IRQF_SHARED, "exynos-pcie", pp);
591                 if (ret) {
592                         dev_err(&pdev->dev, "failed to request msi irq\n");
593                         return ret;
594                 }
595         }
596
597         pp->root_bus_nr = -1;
598         pp->ops = &exynos_pcie_host_ops;
599
600         ret = dw_pcie_host_init(pp);
601         if (ret) {
602                 dev_err(&pdev->dev, "failed to initialize host\n");
603                 return ret;
604         }
605
606         return 0;
607 }
608
609 static int __init exynos_pcie_probe(struct platform_device *pdev)
610 {
611         struct exynos_pcie *exynos_pcie;
612         struct pcie_port *pp;
613         struct device_node *np = pdev->dev.of_node;
614         struct resource *elbi_base;
615         struct resource *phy_base;
616         struct resource *block_base;
617         int ret;
618
619         exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie),
620                                 GFP_KERNEL);
621         if (!exynos_pcie)
622                 return -ENOMEM;
623
624         pp = &exynos_pcie->pp;
625
626         pp->dev = &pdev->dev;
627
628         exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
629
630         exynos_pcie->clk = devm_clk_get(&pdev->dev, "pcie");
631         if (IS_ERR(exynos_pcie->clk)) {
632                 dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
633                 return PTR_ERR(exynos_pcie->clk);
634         }
635         ret = clk_prepare_enable(exynos_pcie->clk);
636         if (ret)
637                 return ret;
638
639         exynos_pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
640         if (IS_ERR(exynos_pcie->bus_clk)) {
641                 dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
642                 ret = PTR_ERR(exynos_pcie->bus_clk);
643                 goto fail_clk;
644         }
645         ret = clk_prepare_enable(exynos_pcie->bus_clk);
646         if (ret)
647                 goto fail_clk;
648
649         elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
650         exynos_pcie->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
651         if (IS_ERR(exynos_pcie->elbi_base)) {
652                 ret = PTR_ERR(exynos_pcie->elbi_base);
653                 goto fail_bus_clk;
654         }
655
656         phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
657         exynos_pcie->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
658         if (IS_ERR(exynos_pcie->phy_base)) {
659                 ret = PTR_ERR(exynos_pcie->phy_base);
660                 goto fail_bus_clk;
661         }
662
663         block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
664         exynos_pcie->block_base = devm_ioremap_resource(&pdev->dev, block_base);
665         if (IS_ERR(exynos_pcie->block_base)) {
666                 ret = PTR_ERR(exynos_pcie->block_base);
667                 goto fail_bus_clk;
668         }
669
670         ret = add_pcie_port(pp, pdev);
671         if (ret < 0)
672                 goto fail_bus_clk;
673
674         platform_set_drvdata(pdev, exynos_pcie);
675         return 0;
676
677 fail_bus_clk:
678         clk_disable_unprepare(exynos_pcie->bus_clk);
679 fail_clk:
680         clk_disable_unprepare(exynos_pcie->clk);
681         return ret;
682 }
683
684 static int __exit exynos_pcie_remove(struct platform_device *pdev)
685 {
686         struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
687
688         clk_disable_unprepare(exynos_pcie->bus_clk);
689         clk_disable_unprepare(exynos_pcie->clk);
690
691         return 0;
692 }
693
694 static const struct of_device_id exynos_pcie_of_match[] = {
695         { .compatible = "samsung,exynos5440-pcie", },
696         {},
697 };
698 MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
699
700 static struct platform_driver exynos_pcie_driver = {
701         .remove         = __exit_p(exynos_pcie_remove),
702         .driver = {
703                 .name   = "exynos-pcie",
704                 .owner  = THIS_MODULE,
705                 .of_match_table = exynos_pcie_of_match,
706         },
707 };
708
709 /* Exynos PCIe driver does not allow module unload */
710
711 static int __init pcie_init(void)
712 {
713         return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
714 }
715 subsys_initcall(pcie_init);
716
717 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
718 MODULE_DESCRIPTION("Samsung PCIe host controller driver");
719 MODULE_LICENSE("GPL v2");