blob: f062acaf052aa898bfe8cb946f8506fc38b3c033 [file] [log] [blame]
Jingoo Han4b1ced82013-07-31 17:14:10 +09001/*
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
31struct 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
Jingoo Hanf342d942013-09-06 15:54:59 +090051#define IRQ_MSI_ENABLE (0x1 << 2)
Jingoo Han4b1ced82013-07-31 17:14:10 +090052#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_TRSV0_EMP_LVL 0x084
82#define PCIE_PHY_TRSV0_DRV_LVL 0x088
83#define PCIE_PHY_TRSV0_RXCDR 0x0ac
84#define PCIE_PHY_TRSV0_LVCC 0x0dc
85#define PCIE_PHY_TRSV1_EMP_LVL 0x144
86#define PCIE_PHY_TRSV1_RXCDR 0x16c
87#define PCIE_PHY_TRSV1_LVCC 0x19c
88#define PCIE_PHY_TRSV2_EMP_LVL 0x204
89#define PCIE_PHY_TRSV2_RXCDR 0x22c
90#define PCIE_PHY_TRSV2_LVCC 0x25c
91#define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
92#define PCIE_PHY_TRSV3_RXCDR 0x2ec
93#define PCIE_PHY_TRSV3_LVCC 0x31c
94
Seungwon Jeon058dd012013-08-29 21:35:56 +090095static inline void exynos_elb_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
96{
97 writel(val, pcie->elbi_base + reg);
98}
99
100static inline u32 exynos_elb_readl(struct exynos_pcie *pcie, u32 reg)
101{
102 return readl(pcie->elbi_base + reg);
103}
104
105static inline void exynos_phy_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
106{
107 writel(val, pcie->phy_base + reg);
108}
109
110static inline u32 exynos_phy_readl(struct exynos_pcie *pcie, u32 reg)
111{
112 return readl(pcie->phy_base + reg);
113}
114
115static inline void exynos_blk_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
116{
117 writel(val, pcie->block_base + reg);
118}
119
120static inline u32 exynos_blk_readl(struct exynos_pcie *pcie, u32 reg)
121{
122 return readl(pcie->block_base + reg);
123}
124
Jingoo Han4b1ced82013-07-31 17:14:10 +0900125static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on)
126{
127 u32 val;
128 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
129
130 if (on) {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900131 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900132 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900133 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900134 } else {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900135 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900136 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900137 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900138 }
139}
140
141static void exynos_pcie_sideband_dbi_r_mode(struct pcie_port *pp, bool on)
142{
143 u32 val;
144 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
145
146 if (on) {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900147 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900148 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900149 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900150 } else {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900151 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900152 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900153 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900154 }
155}
156
157static void exynos_pcie_assert_core_reset(struct pcie_port *pp)
158{
159 u32 val;
160 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900161
Seungwon Jeon058dd012013-08-29 21:35:56 +0900162 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900163 val &= ~PCIE_CORE_RESET_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900164 exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
165 exynos_elb_writel(exynos_pcie, 0, PCIE_PWR_RESET);
166 exynos_elb_writel(exynos_pcie, 0, PCIE_STICKY_RESET);
167 exynos_elb_writel(exynos_pcie, 0, PCIE_NONSTICKY_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900168}
169
170static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
171{
172 u32 val;
173 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900174
Seungwon Jeon058dd012013-08-29 21:35:56 +0900175 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900176 val |= PCIE_CORE_RESET_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900177
178 exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
179 exynos_elb_writel(exynos_pcie, 1, PCIE_STICKY_RESET);
180 exynos_elb_writel(exynos_pcie, 1, PCIE_NONSTICKY_RESET);
181 exynos_elb_writel(exynos_pcie, 1, PCIE_APP_INIT_RESET);
182 exynos_elb_writel(exynos_pcie, 0, PCIE_APP_INIT_RESET);
183 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_MAC_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900184}
185
186static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
187{
188 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900189
Seungwon Jeon058dd012013-08-29 21:35:56 +0900190 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
191 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900192}
193
194static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
195{
196 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900197
Seungwon Jeon058dd012013-08-29 21:35:56 +0900198 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_GLOBAL_RESET);
199 exynos_elb_writel(exynos_pcie, 1, PCIE_PWR_RESET);
200 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
201 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_CMN_REG);
202 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSVREG_RESET);
203 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSV_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900204}
205
206static void exynos_pcie_init_phy(struct pcie_port *pp)
207{
208 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900209
210 /* DCC feedback control off */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900211 exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900212
213 /* set TX/RX impedance */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900214 exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900215
216 /* set 50Mhz PHY clock */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900217 exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
218 exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900219
220 /* set TX Differential output for lane 0 */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900221 exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900222
223 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900224 exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900225
226 /* set RX clock and data recovery bandwidth */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900227 exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
228 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
229 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
230 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
231 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900232
233 /* change TX Pre-emphasis Level Control for lanes */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900234 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
235 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
236 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
237 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900238
239 /* set LVCC */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900240 exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
241 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
242 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
243 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900244}
245
246static void exynos_pcie_assert_reset(struct pcie_port *pp)
247{
248 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
249
250 if (exynos_pcie->reset_gpio >= 0)
251 devm_gpio_request_one(pp->dev, exynos_pcie->reset_gpio,
252 GPIOF_OUT_INIT_HIGH, "RESET");
253 return;
254}
255
256static int exynos_pcie_establish_link(struct pcie_port *pp)
257{
258 u32 val;
259 int count = 0;
260 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900261
262 if (dw_pcie_link_up(pp)) {
263 dev_err(pp->dev, "Link already up\n");
264 return 0;
265 }
266
267 /* assert reset signals */
268 exynos_pcie_assert_core_reset(pp);
269 exynos_pcie_assert_phy_reset(pp);
270
271 /* de-assert phy reset */
272 exynos_pcie_deassert_phy_reset(pp);
273
274 /* initialize phy */
275 exynos_pcie_init_phy(pp);
276
277 /* pulse for common reset */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900278 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900279 udelay(500);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900280 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900281
282 /* de-assert core reset */
283 exynos_pcie_deassert_core_reset(pp);
284
285 /* setup root complex */
286 dw_pcie_setup_rc(pp);
287
288 /* assert reset signal */
289 exynos_pcie_assert_reset(pp);
290
291 /* assert LTSSM enable */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900292 exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
293 PCIE_APP_LTSSM_ENABLE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900294
295 /* check if the link is up or not */
296 while (!dw_pcie_link_up(pp)) {
297 mdelay(100);
298 count++;
299 if (count == 10) {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900300 while (exynos_phy_readl(exynos_pcie,
301 PCIE_PHY_PLL_LOCKED) == 0) {
302 val = exynos_blk_readl(exynos_pcie,
303 PCIE_PHY_PLL_LOCKED);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900304 dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
305 }
306 dev_err(pp->dev, "PCIe Link Fail\n");
307 return -EINVAL;
308 }
309 }
310
311 dev_info(pp->dev, "Link up\n");
312
313 return 0;
314}
315
316static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
317{
318 u32 val;
319 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900320
Seungwon Jeon058dd012013-08-29 21:35:56 +0900321 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
322 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900323 return;
324}
325
326static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
327{
328 u32 val;
329 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900330
331 /* enable INTX interrupt */
332 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
333 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT,
Seungwon Jeon058dd012013-08-29 21:35:56 +0900334 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900335 return;
336}
337
338static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
339{
340 struct pcie_port *pp = arg;
341
342 exynos_pcie_clear_irq_pulse(pp);
343 return IRQ_HANDLED;
344}
345
Jingoo Hanf342d942013-09-06 15:54:59 +0900346static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
347{
348 struct pcie_port *pp = arg;
349
350 dw_handle_msi_irq(pp);
351
352 return IRQ_HANDLED;
353}
354
355static void exynos_pcie_msi_init(struct pcie_port *pp)
356{
357 u32 val;
358 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
359
360 dw_pcie_msi_init(pp);
361
362 /* enable MSI interrupt */
363 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
364 val |= IRQ_MSI_ENABLE;
365 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
366 return;
367}
368
Jingoo Han4b1ced82013-07-31 17:14:10 +0900369static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
370{
371 exynos_pcie_enable_irq_pulse(pp);
Jingoo Hanf342d942013-09-06 15:54:59 +0900372
373 if (IS_ENABLED(CONFIG_PCI_MSI))
374 exynos_pcie_msi_init(pp);
375
Jingoo Han4b1ced82013-07-31 17:14:10 +0900376 return;
377}
378
379static inline void exynos_pcie_readl_rc(struct pcie_port *pp,
380 void __iomem *dbi_base, u32 *val)
381{
382 exynos_pcie_sideband_dbi_r_mode(pp, true);
383 *val = readl(dbi_base);
384 exynos_pcie_sideband_dbi_r_mode(pp, false);
385 return;
386}
387
388static inline void exynos_pcie_writel_rc(struct pcie_port *pp,
389 u32 val, void __iomem *dbi_base)
390{
391 exynos_pcie_sideband_dbi_w_mode(pp, true);
392 writel(val, dbi_base);
393 exynos_pcie_sideband_dbi_w_mode(pp, false);
394 return;
395}
396
397static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
398 u32 *val)
399{
400 int ret;
401
402 exynos_pcie_sideband_dbi_r_mode(pp, true);
403 ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
404 exynos_pcie_sideband_dbi_r_mode(pp, false);
405 return ret;
406}
407
408static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
409 u32 val)
410{
411 int ret;
412
413 exynos_pcie_sideband_dbi_w_mode(pp, true);
414 ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size, val);
415 exynos_pcie_sideband_dbi_w_mode(pp, false);
416 return ret;
417}
418
419static int exynos_pcie_link_up(struct pcie_port *pp)
420{
421 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900422 u32 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900423
424 if (val == PCIE_ELBI_LTSSM_ENABLE)
425 return 1;
426
427 return 0;
428}
429
430static void exynos_pcie_host_init(struct pcie_port *pp)
431{
432 exynos_pcie_establish_link(pp);
433 exynos_pcie_enable_interrupts(pp);
434}
435
436static struct pcie_host_ops exynos_pcie_host_ops = {
437 .readl_rc = exynos_pcie_readl_rc,
438 .writel_rc = exynos_pcie_writel_rc,
439 .rd_own_conf = exynos_pcie_rd_own_conf,
440 .wr_own_conf = exynos_pcie_wr_own_conf,
441 .link_up = exynos_pcie_link_up,
442 .host_init = exynos_pcie_host_init,
443};
444
445static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev)
446{
447 int ret;
448
449 pp->irq = platform_get_irq(pdev, 1);
450 if (!pp->irq) {
451 dev_err(&pdev->dev, "failed to get irq\n");
452 return -ENODEV;
453 }
454 ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
455 IRQF_SHARED, "exynos-pcie", pp);
456 if (ret) {
457 dev_err(&pdev->dev, "failed to request irq\n");
458 return ret;
459 }
460
Jingoo Hanf342d942013-09-06 15:54:59 +0900461 if (IS_ENABLED(CONFIG_PCI_MSI)) {
462 pp->msi_irq = platform_get_irq(pdev, 0);
463 if (!pp->msi_irq) {
464 dev_err(&pdev->dev, "failed to get msi irq\n");
465 return -ENODEV;
466 }
467
468 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
469 exynos_pcie_msi_irq_handler,
470 IRQF_SHARED, "exynos-pcie", pp);
471 if (ret) {
472 dev_err(&pdev->dev, "failed to request msi irq\n");
473 return ret;
474 }
475 }
476
Jingoo Han4b1ced82013-07-31 17:14:10 +0900477 pp->root_bus_nr = -1;
478 pp->ops = &exynos_pcie_host_ops;
479
480 spin_lock_init(&pp->conf_lock);
481 ret = dw_pcie_host_init(pp);
482 if (ret) {
483 dev_err(&pdev->dev, "failed to initialize host\n");
484 return ret;
485 }
486
487 return 0;
488}
489
490static int __init exynos_pcie_probe(struct platform_device *pdev)
491{
492 struct exynos_pcie *exynos_pcie;
493 struct pcie_port *pp;
494 struct device_node *np = pdev->dev.of_node;
495 struct resource *elbi_base;
496 struct resource *phy_base;
497 struct resource *block_base;
498 int ret;
499
500 exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie),
501 GFP_KERNEL);
502 if (!exynos_pcie) {
503 dev_err(&pdev->dev, "no memory for exynos pcie\n");
504 return -ENOMEM;
505 }
506
507 pp = &exynos_pcie->pp;
508
509 pp->dev = &pdev->dev;
510
511 exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
512
513 exynos_pcie->clk = devm_clk_get(&pdev->dev, "pcie");
514 if (IS_ERR(exynos_pcie->clk)) {
515 dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
516 return PTR_ERR(exynos_pcie->clk);
517 }
518 ret = clk_prepare_enable(exynos_pcie->clk);
519 if (ret)
520 return ret;
521
522 exynos_pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
523 if (IS_ERR(exynos_pcie->bus_clk)) {
524 dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
525 ret = PTR_ERR(exynos_pcie->bus_clk);
526 goto fail_clk;
527 }
528 ret = clk_prepare_enable(exynos_pcie->bus_clk);
529 if (ret)
530 goto fail_clk;
531
532 elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
533 exynos_pcie->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
534 if (IS_ERR(exynos_pcie->elbi_base))
535 return PTR_ERR(exynos_pcie->elbi_base);
536
537 phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
538 exynos_pcie->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
539 if (IS_ERR(exynos_pcie->phy_base))
540 return PTR_ERR(exynos_pcie->phy_base);
541
542 block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
543 exynos_pcie->block_base = devm_ioremap_resource(&pdev->dev, block_base);
544 if (IS_ERR(exynos_pcie->block_base))
545 return PTR_ERR(exynos_pcie->block_base);
546
547 ret = add_pcie_port(pp, pdev);
548 if (ret < 0)
549 goto fail_bus_clk;
550
551 platform_set_drvdata(pdev, exynos_pcie);
552 return 0;
553
554fail_bus_clk:
555 clk_disable_unprepare(exynos_pcie->bus_clk);
556fail_clk:
557 clk_disable_unprepare(exynos_pcie->clk);
558 return ret;
559}
560
561static int __exit exynos_pcie_remove(struct platform_device *pdev)
562{
563 struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
564
565 clk_disable_unprepare(exynos_pcie->bus_clk);
566 clk_disable_unprepare(exynos_pcie->clk);
567
568 return 0;
569}
570
571static const struct of_device_id exynos_pcie_of_match[] = {
572 { .compatible = "samsung,exynos5440-pcie", },
573 {},
574};
575MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
576
577static struct platform_driver exynos_pcie_driver = {
578 .remove = __exit_p(exynos_pcie_remove),
579 .driver = {
580 .name = "exynos-pcie",
581 .owner = THIS_MODULE,
582 .of_match_table = of_match_ptr(exynos_pcie_of_match),
583 },
584};
585
586/* Exynos PCIe driver does not allow module unload */
587
588static int __init pcie_init(void)
589{
590 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
591}
592subsys_initcall(pcie_init);
593
594MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
595MODULE_DESCRIPTION("Samsung PCIe host controller driver");
596MODULE_LICENSE("GPL v2");