blob: f9f468d9a819d8402244e31805d51a034fb6392f [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
Jingoo Hanf62b8782013-09-06 17:21:45 +090081#define PCIE_PHY_COMMON_POWER 0x064
82#define PCIE_PHY_COMMON_PD_CMN (0x1 << 3)
Jingoo Han4b1ced82013-07-31 17:14:10 +090083#define PCIE_PHY_TRSV0_EMP_LVL 0x084
84#define PCIE_PHY_TRSV0_DRV_LVL 0x088
85#define PCIE_PHY_TRSV0_RXCDR 0x0ac
Jingoo Hanf62b8782013-09-06 17:21:45 +090086#define PCIE_PHY_TRSV0_POWER 0x0c4
87#define PCIE_PHY_TRSV0_PD_TSV (0x1 << 7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090088#define PCIE_PHY_TRSV0_LVCC 0x0dc
89#define PCIE_PHY_TRSV1_EMP_LVL 0x144
90#define PCIE_PHY_TRSV1_RXCDR 0x16c
Jingoo Hanf62b8782013-09-06 17:21:45 +090091#define PCIE_PHY_TRSV1_POWER 0x184
92#define PCIE_PHY_TRSV1_PD_TSV (0x1 << 7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090093#define PCIE_PHY_TRSV1_LVCC 0x19c
94#define PCIE_PHY_TRSV2_EMP_LVL 0x204
95#define PCIE_PHY_TRSV2_RXCDR 0x22c
Jingoo Hanf62b8782013-09-06 17:21:45 +090096#define PCIE_PHY_TRSV2_POWER 0x244
97#define PCIE_PHY_TRSV2_PD_TSV (0x1 << 7)
Jingoo Han4b1ced82013-07-31 17:14:10 +090098#define PCIE_PHY_TRSV2_LVCC 0x25c
99#define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
100#define PCIE_PHY_TRSV3_RXCDR 0x2ec
Jingoo Hanf62b8782013-09-06 17:21:45 +0900101#define PCIE_PHY_TRSV3_POWER 0x304
102#define PCIE_PHY_TRSV3_PD_TSV (0x1 << 7)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900103#define PCIE_PHY_TRSV3_LVCC 0x31c
104
Seungwon Jeon058dd012013-08-29 21:35:56 +0900105static inline void exynos_elb_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
106{
107 writel(val, pcie->elbi_base + reg);
108}
109
110static inline u32 exynos_elb_readl(struct exynos_pcie *pcie, u32 reg)
111{
112 return readl(pcie->elbi_base + reg);
113}
114
115static inline void exynos_phy_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
116{
117 writel(val, pcie->phy_base + reg);
118}
119
120static inline u32 exynos_phy_readl(struct exynos_pcie *pcie, u32 reg)
121{
122 return readl(pcie->phy_base + reg);
123}
124
125static inline void exynos_blk_writel(struct exynos_pcie *pcie, u32 val, u32 reg)
126{
127 writel(val, pcie->block_base + reg);
128}
129
130static inline u32 exynos_blk_readl(struct exynos_pcie *pcie, u32 reg)
131{
132 return readl(pcie->block_base + reg);
133}
134
Jingoo Han4b1ced82013-07-31 17:14:10 +0900135static 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) {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900141 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900142 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900143 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900144 } else {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900145 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900146 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900147 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_AWMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900148 }
149}
150
151static 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) {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900157 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900158 val |= PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900159 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900160 } else {
Seungwon Jeon058dd012013-08-29 21:35:56 +0900161 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900162 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900163 exynos_elb_writel(exynos_pcie, val, PCIE_ELBI_SLV_ARMISC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900164 }
165}
166
167static void exynos_pcie_assert_core_reset(struct pcie_port *pp)
168{
169 u32 val;
170 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900171
Seungwon Jeon058dd012013-08-29 21:35:56 +0900172 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900173 val &= ~PCIE_CORE_RESET_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900174 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);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900178}
179
180static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
181{
182 u32 val;
183 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900184
Seungwon Jeon058dd012013-08-29 21:35:56 +0900185 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900186 val |= PCIE_CORE_RESET_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900187
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);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900194}
195
196static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
197{
198 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900199
Seungwon Jeon058dd012013-08-29 21:35:56 +0900200 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
201 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900202}
203
204static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
205{
206 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900207
Seungwon Jeon058dd012013-08-29 21:35:56 +0900208 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);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900214}
215
Jingoo Hanf62b8782013-09-06 17:21:45 +0900216static 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
242static 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
Jingoo Han4b1ced82013-07-31 17:14:10 +0900268static void exynos_pcie_init_phy(struct pcie_port *pp)
269{
270 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900271
272 /* DCC feedback control off */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900273 exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900274
275 /* set TX/RX impedance */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900276 exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900277
278 /* set 50Mhz PHY clock */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900279 exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
280 exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900281
282 /* set TX Differential output for lane 0 */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900283 exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900284
285 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900286 exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900287
288 /* set RX clock and data recovery bandwidth */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900289 exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
290 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
291 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
292 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
293 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900294
295 /* change TX Pre-emphasis Level Control for lanes */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900296 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
297 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
298 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
299 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900300
301 /* set LVCC */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900302 exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
303 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
304 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
305 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900306}
307
308static void exynos_pcie_assert_reset(struct pcie_port *pp)
309{
310 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
311
312 if (exynos_pcie->reset_gpio >= 0)
313 devm_gpio_request_one(pp->dev, exynos_pcie->reset_gpio,
314 GPIOF_OUT_INIT_HIGH, "RESET");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900315}
316
317static int exynos_pcie_establish_link(struct pcie_port *pp)
318{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900319 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500320 u32 val;
321 unsigned int retries;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900322
323 if (dw_pcie_link_up(pp)) {
324 dev_err(pp->dev, "Link already up\n");
325 return 0;
326 }
327
328 /* assert reset signals */
329 exynos_pcie_assert_core_reset(pp);
330 exynos_pcie_assert_phy_reset(pp);
331
332 /* de-assert phy reset */
333 exynos_pcie_deassert_phy_reset(pp);
334
Jingoo Hanf62b8782013-09-06 17:21:45 +0900335 /* power on phy */
336 exynos_pcie_power_on_phy(pp);
337
Jingoo Han4b1ced82013-07-31 17:14:10 +0900338 /* initialize phy */
339 exynos_pcie_init_phy(pp);
340
341 /* pulse for common reset */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900342 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900343 udelay(500);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900344 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900345
346 /* de-assert core reset */
347 exynos_pcie_deassert_core_reset(pp);
348
349 /* setup root complex */
350 dw_pcie_setup_rc(pp);
351
352 /* assert reset signal */
353 exynos_pcie_assert_reset(pp);
354
355 /* assert LTSSM enable */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900356 exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
357 PCIE_APP_LTSSM_ENABLE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900358
359 /* check if the link is up or not */
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500360 for (retries = 0; retries < 10; retries++) {
361 if (dw_pcie_link_up(pp)) {
362 dev_info(pp->dev, "Link up\n");
363 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900364 }
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500365 mdelay(100);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900366 }
367
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500368 while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
369 val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
370 dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
371 }
372 /* power off phy */
373 exynos_pcie_power_off_phy(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900374
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500375 dev_err(pp->dev, "PCIe Link Fail\n");
376 return -EINVAL;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900377}
378
379static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
380{
381 u32 val;
382 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900383
Seungwon Jeon058dd012013-08-29 21:35:56 +0900384 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
385 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900386}
387
388static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
389{
390 u32 val;
391 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900392
393 /* enable INTX interrupt */
394 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
Jaehoon Chung01d06a92015-03-25 14:13:12 +0900395 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900396 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900397}
398
399static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
400{
401 struct pcie_port *pp = arg;
402
403 exynos_pcie_clear_irq_pulse(pp);
404 return IRQ_HANDLED;
405}
406
Jingoo Hanf342d942013-09-06 15:54:59 +0900407static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
408{
409 struct pcie_port *pp = arg;
410
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100411 return dw_handle_msi_irq(pp);
Jingoo Hanf342d942013-09-06 15:54:59 +0900412}
413
414static void exynos_pcie_msi_init(struct pcie_port *pp)
415{
416 u32 val;
417 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
418
419 dw_pcie_msi_init(pp);
420
421 /* enable MSI interrupt */
422 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
423 val |= IRQ_MSI_ENABLE;
424 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
Jingoo Hanf342d942013-09-06 15:54:59 +0900425}
426
Jingoo Han4b1ced82013-07-31 17:14:10 +0900427static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
428{
429 exynos_pcie_enable_irq_pulse(pp);
Jingoo Hanf342d942013-09-06 15:54:59 +0900430
431 if (IS_ENABLED(CONFIG_PCI_MSI))
432 exynos_pcie_msi_init(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900433}
434
435static inline void exynos_pcie_readl_rc(struct pcie_port *pp,
436 void __iomem *dbi_base, u32 *val)
437{
438 exynos_pcie_sideband_dbi_r_mode(pp, true);
439 *val = readl(dbi_base);
440 exynos_pcie_sideband_dbi_r_mode(pp, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900441}
442
443static inline void exynos_pcie_writel_rc(struct pcie_port *pp,
444 u32 val, void __iomem *dbi_base)
445{
446 exynos_pcie_sideband_dbi_w_mode(pp, true);
447 writel(val, dbi_base);
448 exynos_pcie_sideband_dbi_w_mode(pp, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900449}
450
451static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
452 u32 *val)
453{
454 int ret;
455
456 exynos_pcie_sideband_dbi_r_mode(pp, true);
Pratyush Ananda01ef592013-12-11 15:08:32 +0530457 ret = dw_pcie_cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900458 exynos_pcie_sideband_dbi_r_mode(pp, false);
459 return ret;
460}
461
462static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
463 u32 val)
464{
465 int ret;
466
467 exynos_pcie_sideband_dbi_w_mode(pp, true);
Pratyush Ananda01ef592013-12-11 15:08:32 +0530468 ret = dw_pcie_cfg_write(pp->dbi_base + (where & ~0x3),
469 where, size, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900470 exynos_pcie_sideband_dbi_w_mode(pp, false);
471 return ret;
472}
473
474static int exynos_pcie_link_up(struct pcie_port *pp)
475{
476 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900477 u32 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900478
479 if (val == PCIE_ELBI_LTSSM_ENABLE)
480 return 1;
481
482 return 0;
483}
484
485static void exynos_pcie_host_init(struct pcie_port *pp)
486{
487 exynos_pcie_establish_link(pp);
488 exynos_pcie_enable_interrupts(pp);
489}
490
491static struct pcie_host_ops exynos_pcie_host_ops = {
492 .readl_rc = exynos_pcie_readl_rc,
493 .writel_rc = exynos_pcie_writel_rc,
494 .rd_own_conf = exynos_pcie_rd_own_conf,
495 .wr_own_conf = exynos_pcie_wr_own_conf,
496 .link_up = exynos_pcie_link_up,
497 .host_init = exynos_pcie_host_init,
498};
499
Jingoo Han70b3e892014-10-22 13:58:49 +0900500static int __init exynos_add_pcie_port(struct pcie_port *pp,
501 struct platform_device *pdev)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900502{
503 int ret;
504
505 pp->irq = platform_get_irq(pdev, 1);
506 if (!pp->irq) {
507 dev_err(&pdev->dev, "failed to get irq\n");
508 return -ENODEV;
509 }
510 ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
511 IRQF_SHARED, "exynos-pcie", pp);
512 if (ret) {
513 dev_err(&pdev->dev, "failed to request irq\n");
514 return ret;
515 }
516
Jingoo Hanf342d942013-09-06 15:54:59 +0900517 if (IS_ENABLED(CONFIG_PCI_MSI)) {
518 pp->msi_irq = platform_get_irq(pdev, 0);
519 if (!pp->msi_irq) {
520 dev_err(&pdev->dev, "failed to get msi irq\n");
521 return -ENODEV;
522 }
523
524 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
525 exynos_pcie_msi_irq_handler,
526 IRQF_SHARED, "exynos-pcie", pp);
527 if (ret) {
528 dev_err(&pdev->dev, "failed to request msi irq\n");
529 return ret;
530 }
531 }
532
Jingoo Han4b1ced82013-07-31 17:14:10 +0900533 pp->root_bus_nr = -1;
534 pp->ops = &exynos_pcie_host_ops;
535
Jingoo Han4b1ced82013-07-31 17:14:10 +0900536 ret = dw_pcie_host_init(pp);
537 if (ret) {
538 dev_err(&pdev->dev, "failed to initialize host\n");
539 return ret;
540 }
541
542 return 0;
543}
544
545static int __init exynos_pcie_probe(struct platform_device *pdev)
546{
547 struct exynos_pcie *exynos_pcie;
548 struct pcie_port *pp;
549 struct device_node *np = pdev->dev.of_node;
550 struct resource *elbi_base;
551 struct resource *phy_base;
552 struct resource *block_base;
553 int ret;
554
555 exynos_pcie = devm_kzalloc(&pdev->dev, sizeof(*exynos_pcie),
556 GFP_KERNEL);
Jingoo Han755ba5e2014-05-09 14:31:25 +0900557 if (!exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900558 return -ENOMEM;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900559
560 pp = &exynos_pcie->pp;
561
562 pp->dev = &pdev->dev;
563
564 exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
565
566 exynos_pcie->clk = devm_clk_get(&pdev->dev, "pcie");
567 if (IS_ERR(exynos_pcie->clk)) {
568 dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
569 return PTR_ERR(exynos_pcie->clk);
570 }
571 ret = clk_prepare_enable(exynos_pcie->clk);
572 if (ret)
573 return ret;
574
575 exynos_pcie->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
576 if (IS_ERR(exynos_pcie->bus_clk)) {
577 dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
578 ret = PTR_ERR(exynos_pcie->bus_clk);
579 goto fail_clk;
580 }
581 ret = clk_prepare_enable(exynos_pcie->bus_clk);
582 if (ret)
583 goto fail_clk;
584
585 elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
586 exynos_pcie->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800587 if (IS_ERR(exynos_pcie->elbi_base)) {
588 ret = PTR_ERR(exynos_pcie->elbi_base);
589 goto fail_bus_clk;
590 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900591
592 phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
593 exynos_pcie->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800594 if (IS_ERR(exynos_pcie->phy_base)) {
595 ret = PTR_ERR(exynos_pcie->phy_base);
596 goto fail_bus_clk;
597 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900598
599 block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
600 exynos_pcie->block_base = devm_ioremap_resource(&pdev->dev, block_base);
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800601 if (IS_ERR(exynos_pcie->block_base)) {
602 ret = PTR_ERR(exynos_pcie->block_base);
603 goto fail_bus_clk;
604 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900605
Jingoo Han70b3e892014-10-22 13:58:49 +0900606 ret = exynos_add_pcie_port(pp, pdev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900607 if (ret < 0)
608 goto fail_bus_clk;
609
610 platform_set_drvdata(pdev, exynos_pcie);
611 return 0;
612
613fail_bus_clk:
614 clk_disable_unprepare(exynos_pcie->bus_clk);
615fail_clk:
616 clk_disable_unprepare(exynos_pcie->clk);
617 return ret;
618}
619
620static int __exit exynos_pcie_remove(struct platform_device *pdev)
621{
622 struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
623
624 clk_disable_unprepare(exynos_pcie->bus_clk);
625 clk_disable_unprepare(exynos_pcie->clk);
626
627 return 0;
628}
629
630static const struct of_device_id exynos_pcie_of_match[] = {
631 { .compatible = "samsung,exynos5440-pcie", },
632 {},
633};
634MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
635
636static struct platform_driver exynos_pcie_driver = {
637 .remove = __exit_p(exynos_pcie_remove),
638 .driver = {
639 .name = "exynos-pcie",
Sachin Kamateb363092013-10-21 14:36:43 +0530640 .of_match_table = exynos_pcie_of_match,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900641 },
642};
643
644/* Exynos PCIe driver does not allow module unload */
645
Jingoo Han70b3e892014-10-22 13:58:49 +0900646static int __init exynos_pcie_init(void)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900647{
648 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
649}
Jingoo Han70b3e892014-10-22 13:58:49 +0900650subsys_initcall(exynos_pcie_init);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900651
652MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
653MODULE_DESCRIPTION("Samsung PCIe host controller driver");
654MODULE_LICENSE("GPL v2");