blob: f1c544bb8b684b7c969bfb574801a76810a6dca0 [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>
Paul Gortmakercaf55482016-08-22 17:59:47 -040019#include <linux/init.h>
Jingoo Han4b1ced82013-07-31 17:14:10 +090020#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 {
Bjorn Helgaas6b1f1852016-10-06 13:33:40 -050032 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 */
Jingoo Han4b1ced82013-07-31 17:14:10 +090036 int reset_gpio;
37 struct clk *clk;
38 struct clk *bus_clk;
Jingoo Han4b1ced82013-07-31 17:14:10 +090039};
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
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500105static void exynos_elb_writel(struct exynos_pcie *exynos_pcie, u32 val, u32 reg)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900106{
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500107 writel(val, exynos_pcie->elbi_base + reg);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900108}
109
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500110static u32 exynos_elb_readl(struct exynos_pcie *exynos_pcie, u32 reg)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900111{
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500112 return readl(exynos_pcie->elbi_base + reg);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900113}
114
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500115static void exynos_phy_writel(struct exynos_pcie *exynos_pcie, u32 val, u32 reg)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900116{
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500117 writel(val, exynos_pcie->phy_base + reg);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900118}
119
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500120static u32 exynos_phy_readl(struct exynos_pcie *exynos_pcie, u32 reg)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900121{
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500122 return readl(exynos_pcie->phy_base + reg);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900123}
124
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500125static void exynos_blk_writel(struct exynos_pcie *exynos_pcie, u32 val, u32 reg)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900126{
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500127 writel(val, exynos_pcie->block_base + reg);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900128}
129
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500130static u32 exynos_blk_readl(struct exynos_pcie *exynos_pcie, u32 reg)
Seungwon Jeon058dd012013-08-29 21:35:56 +0900131{
Bjorn Helgaas10284bf2016-10-06 13:33:39 -0500132 return readl(exynos_pcie->block_base + reg);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900133}
134
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500135static void exynos_pcie_sideband_dbi_w_mode(struct exynos_pcie *exynos_pcie,
136 bool on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900137{
138 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900139
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
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500151static void exynos_pcie_sideband_dbi_r_mode(struct exynos_pcie *exynos_pcie,
152 bool on)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900153{
154 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900155
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
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500167static void exynos_pcie_assert_core_reset(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900168{
169 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900170
Seungwon Jeon058dd012013-08-29 21:35:56 +0900171 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900172 val &= ~PCIE_CORE_RESET_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900173 exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
174 exynos_elb_writel(exynos_pcie, 0, PCIE_PWR_RESET);
175 exynos_elb_writel(exynos_pcie, 0, PCIE_STICKY_RESET);
176 exynos_elb_writel(exynos_pcie, 0, PCIE_NONSTICKY_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900177}
178
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500179static void exynos_pcie_deassert_core_reset(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900180{
181 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900182
Seungwon Jeon058dd012013-08-29 21:35:56 +0900183 val = exynos_elb_readl(exynos_pcie, PCIE_CORE_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900184 val |= PCIE_CORE_RESET_ENABLE;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900185
186 exynos_elb_writel(exynos_pcie, val, PCIE_CORE_RESET);
187 exynos_elb_writel(exynos_pcie, 1, PCIE_STICKY_RESET);
188 exynos_elb_writel(exynos_pcie, 1, PCIE_NONSTICKY_RESET);
189 exynos_elb_writel(exynos_pcie, 1, PCIE_APP_INIT_RESET);
190 exynos_elb_writel(exynos_pcie, 0, PCIE_APP_INIT_RESET);
191 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_MAC_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900192}
193
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500194static void exynos_pcie_assert_phy_reset(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900195{
Seungwon Jeon058dd012013-08-29 21:35:56 +0900196 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_MAC_RESET);
197 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_GLOBAL_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900198}
199
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500200static void exynos_pcie_deassert_phy_reset(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900201{
Seungwon Jeon058dd012013-08-29 21:35:56 +0900202 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_GLOBAL_RESET);
203 exynos_elb_writel(exynos_pcie, 1, PCIE_PWR_RESET);
204 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
205 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_CMN_REG);
206 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSVREG_RESET);
207 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_TRSV_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900208}
209
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500210static void exynos_pcie_power_on_phy(struct exynos_pcie *exynos_pcie)
Jingoo Hanf62b8782013-09-06 17:21:45 +0900211{
212 u32 val;
Jingoo Hanf62b8782013-09-06 17:21:45 +0900213
214 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
215 val &= ~PCIE_PHY_COMMON_PD_CMN;
216 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
217
218 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
219 val &= ~PCIE_PHY_TRSV0_PD_TSV;
220 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
221
222 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
223 val &= ~PCIE_PHY_TRSV1_PD_TSV;
224 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
225
226 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
227 val &= ~PCIE_PHY_TRSV2_PD_TSV;
228 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
229
230 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
231 val &= ~PCIE_PHY_TRSV3_PD_TSV;
232 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
233}
234
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500235static void exynos_pcie_power_off_phy(struct exynos_pcie *exynos_pcie)
Jingoo Hanf62b8782013-09-06 17:21:45 +0900236{
237 u32 val;
Jingoo Hanf62b8782013-09-06 17:21:45 +0900238
239 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_COMMON_POWER);
240 val |= PCIE_PHY_COMMON_PD_CMN;
241 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_COMMON_POWER);
242
243 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV0_POWER);
244 val |= PCIE_PHY_TRSV0_PD_TSV;
245 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV0_POWER);
246
247 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV1_POWER);
248 val |= PCIE_PHY_TRSV1_PD_TSV;
249 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV1_POWER);
250
251 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV2_POWER);
252 val |= PCIE_PHY_TRSV2_PD_TSV;
253 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV2_POWER);
254
255 val = exynos_phy_readl(exynos_pcie, PCIE_PHY_TRSV3_POWER);
256 val |= PCIE_PHY_TRSV3_PD_TSV;
257 exynos_phy_writel(exynos_pcie, val, PCIE_PHY_TRSV3_POWER);
258}
259
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500260static void exynos_pcie_init_phy(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900261{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900262 /* DCC feedback control off */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900263 exynos_phy_writel(exynos_pcie, 0x29, PCIE_PHY_DCC_FEEDBACK);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900264
265 /* set TX/RX impedance */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900266 exynos_phy_writel(exynos_pcie, 0xd5, PCIE_PHY_IMPEDANCE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900267
268 /* set 50Mhz PHY clock */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900269 exynos_phy_writel(exynos_pcie, 0x14, PCIE_PHY_PLL_DIV_0);
270 exynos_phy_writel(exynos_pcie, 0x12, PCIE_PHY_PLL_DIV_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900271
272 /* set TX Differential output for lane 0 */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900273 exynos_phy_writel(exynos_pcie, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900274
275 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900276 exynos_phy_writel(exynos_pcie, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900277
278 /* set RX clock and data recovery bandwidth */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900279 exynos_phy_writel(exynos_pcie, 0xe7, PCIE_PHY_PLL_BIAS);
280 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV0_RXCDR);
281 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV1_RXCDR);
282 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV2_RXCDR);
283 exynos_phy_writel(exynos_pcie, 0x82, PCIE_PHY_TRSV3_RXCDR);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900284
285 /* change TX Pre-emphasis Level Control for lanes */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900286 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
287 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
288 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
289 exynos_phy_writel(exynos_pcie, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900290
291 /* set LVCC */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900292 exynos_phy_writel(exynos_pcie, 0x20, PCIE_PHY_TRSV0_LVCC);
293 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV1_LVCC);
294 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV2_LVCC);
295 exynos_phy_writel(exynos_pcie, 0xa0, PCIE_PHY_TRSV3_LVCC);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900296}
297
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500298static void exynos_pcie_assert_reset(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900299{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500300 struct pcie_port *pp = &exynos_pcie->pp;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500301 struct device *dev = pp->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900302
303 if (exynos_pcie->reset_gpio >= 0)
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500304 devm_gpio_request_one(dev, exynos_pcie->reset_gpio,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900305 GPIOF_OUT_INIT_HIGH, "RESET");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900306}
307
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500308static int exynos_pcie_establish_link(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900309{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500310 struct pcie_port *pp = &exynos_pcie->pp;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500311 struct device *dev = pp->dev;
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500312 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900313
314 if (dw_pcie_link_up(pp)) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500315 dev_err(dev, "Link already up\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900316 return 0;
317 }
318
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500319 exynos_pcie_assert_core_reset(exynos_pcie);
320 exynos_pcie_assert_phy_reset(exynos_pcie);
321 exynos_pcie_deassert_phy_reset(exynos_pcie);
322 exynos_pcie_power_on_phy(exynos_pcie);
323 exynos_pcie_init_phy(exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900324
325 /* pulse for common reset */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900326 exynos_blk_writel(exynos_pcie, 1, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900327 udelay(500);
Seungwon Jeon058dd012013-08-29 21:35:56 +0900328 exynos_blk_writel(exynos_pcie, 0, PCIE_PHY_COMMON_RESET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900329
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500330 exynos_pcie_deassert_core_reset(exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900331 dw_pcie_setup_rc(pp);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500332 exynos_pcie_assert_reset(exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900333
334 /* assert LTSSM enable */
Seungwon Jeon058dd012013-08-29 21:35:56 +0900335 exynos_elb_writel(exynos_pcie, PCIE_ELBI_LTSSM_ENABLE,
336 PCIE_APP_LTSSM_ENABLE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900337
338 /* check if the link is up or not */
Joao Pinto886bc5c2016-03-10 14:44:35 -0600339 if (!dw_pcie_wait_for_link(pp))
340 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900341
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500342 while (exynos_phy_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED) == 0) {
343 val = exynos_blk_readl(exynos_pcie, PCIE_PHY_PLL_LOCKED);
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500344 dev_info(dev, "PLL Locked: 0x%x\n", val);
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500345 }
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500346 exynos_pcie_power_off_phy(exynos_pcie);
Joao Pinto886bc5c2016-03-10 14:44:35 -0600347 return -ETIMEDOUT;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900348}
349
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500350static void exynos_pcie_clear_irq_pulse(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900351{
352 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900353
Seungwon Jeon058dd012013-08-29 21:35:56 +0900354 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_PULSE);
355 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900356}
357
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500358static void exynos_pcie_enable_irq_pulse(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900359{
360 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900361
362 /* enable INTX interrupt */
363 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
Jaehoon Chung01d06a92015-03-25 14:13:12 +0900364 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT;
Seungwon Jeon058dd012013-08-29 21:35:56 +0900365 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_PULSE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900366}
367
368static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
369{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500370 struct exynos_pcie *exynos_pcie = arg;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900371
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500372 exynos_pcie_clear_irq_pulse(exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900373 return IRQ_HANDLED;
374}
375
Jingoo Hanf342d942013-09-06 15:54:59 +0900376static irqreturn_t exynos_pcie_msi_irq_handler(int irq, void *arg)
377{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500378 struct exynos_pcie *exynos_pcie = arg;
379 struct pcie_port *pp = &exynos_pcie->pp;
Jingoo Hanf342d942013-09-06 15:54:59 +0900380
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100381 return dw_handle_msi_irq(pp);
Jingoo Hanf342d942013-09-06 15:54:59 +0900382}
383
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500384static void exynos_pcie_msi_init(struct exynos_pcie *exynos_pcie)
Jingoo Hanf342d942013-09-06 15:54:59 +0900385{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500386 struct pcie_port *pp = &exynos_pcie->pp;
Jingoo Hanf342d942013-09-06 15:54:59 +0900387 u32 val;
Jingoo Hanf342d942013-09-06 15:54:59 +0900388
389 dw_pcie_msi_init(pp);
390
391 /* enable MSI interrupt */
392 val = exynos_elb_readl(exynos_pcie, PCIE_IRQ_EN_LEVEL);
393 val |= IRQ_MSI_ENABLE;
394 exynos_elb_writel(exynos_pcie, val, PCIE_IRQ_EN_LEVEL);
Jingoo Hanf342d942013-09-06 15:54:59 +0900395}
396
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500397static void exynos_pcie_enable_interrupts(struct exynos_pcie *exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900398{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500399 exynos_pcie_enable_irq_pulse(exynos_pcie);
Jingoo Hanf342d942013-09-06 15:54:59 +0900400
401 if (IS_ENABLED(CONFIG_PCI_MSI))
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500402 exynos_pcie_msi_init(exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900403}
404
Bjorn Helgaas53e5bff12016-10-10 07:50:07 -0500405static u32 exynos_pcie_readl_rc(struct pcie_port *pp, u32 reg)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900406{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500407 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Bjorn Helgaas446fc232016-08-17 14:17:58 -0500408 u32 val;
409
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500410 exynos_pcie_sideband_dbi_r_mode(exynos_pcie, true);
Bjorn Helgaas7e00dfd2016-10-06 13:25:46 -0500411 val = readl(pp->dbi_base + reg);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500412 exynos_pcie_sideband_dbi_r_mode(exynos_pcie, false);
Bjorn Helgaas446fc232016-08-17 14:17:58 -0500413 return val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900414}
415
Bjorn Helgaas53e5bff12016-10-10 07:50:07 -0500416static void exynos_pcie_writel_rc(struct pcie_port *pp, u32 reg, u32 val)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900417{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500418 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
419
420 exynos_pcie_sideband_dbi_w_mode(exynos_pcie, true);
Bjorn Helgaas7e00dfd2016-10-06 13:25:46 -0500421 writel(val, pp->dbi_base + reg);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500422 exynos_pcie_sideband_dbi_w_mode(exynos_pcie, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900423}
424
425static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
426 u32 *val)
427{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500428 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900429 int ret;
430
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500431 exynos_pcie_sideband_dbi_r_mode(exynos_pcie, true);
Gabriele Paoloni4c458522015-10-08 14:27:48 -0500432 ret = dw_pcie_cfg_read(pp->dbi_base + where, size, val);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500433 exynos_pcie_sideband_dbi_r_mode(exynos_pcie, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900434 return ret;
435}
436
437static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
438 u32 val)
439{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500440 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900441 int ret;
442
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500443 exynos_pcie_sideband_dbi_w_mode(exynos_pcie, true);
Gabriele Paoloni4c458522015-10-08 14:27:48 -0500444 ret = dw_pcie_cfg_write(pp->dbi_base + where, size, val);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500445 exynos_pcie_sideband_dbi_w_mode(exynos_pcie, false);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900446 return ret;
447}
448
449static int exynos_pcie_link_up(struct pcie_port *pp)
450{
451 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500452 u32 val;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900453
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500454 val = exynos_elb_readl(exynos_pcie, PCIE_ELBI_RDLH_LINKUP);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900455 if (val == PCIE_ELBI_LTSSM_ENABLE)
456 return 1;
457
458 return 0;
459}
460
461static void exynos_pcie_host_init(struct pcie_port *pp)
462{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500463 struct exynos_pcie *exynos_pcie = to_exynos_pcie(pp);
464
465 exynos_pcie_establish_link(exynos_pcie);
466 exynos_pcie_enable_interrupts(exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900467}
468
469static struct pcie_host_ops exynos_pcie_host_ops = {
470 .readl_rc = exynos_pcie_readl_rc,
471 .writel_rc = exynos_pcie_writel_rc,
472 .rd_own_conf = exynos_pcie_rd_own_conf,
473 .wr_own_conf = exynos_pcie_wr_own_conf,
474 .link_up = exynos_pcie_link_up,
475 .host_init = exynos_pcie_host_init,
476};
477
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500478static int __init exynos_add_pcie_port(struct exynos_pcie *exynos_pcie,
Jingoo Han70b3e892014-10-22 13:58:49 +0900479 struct platform_device *pdev)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900480{
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500481 struct pcie_port *pp = &exynos_pcie->pp;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500482 struct device *dev = pp->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900483 int ret;
484
485 pp->irq = platform_get_irq(pdev, 1);
486 if (!pp->irq) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500487 dev_err(dev, "failed to get irq\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900488 return -ENODEV;
489 }
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500490 ret = devm_request_irq(dev, pp->irq, exynos_pcie_irq_handler,
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500491 IRQF_SHARED, "exynos-pcie", exynos_pcie);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900492 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500493 dev_err(dev, "failed to request irq\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900494 return ret;
495 }
496
Jingoo Hanf342d942013-09-06 15:54:59 +0900497 if (IS_ENABLED(CONFIG_PCI_MSI)) {
498 pp->msi_irq = platform_get_irq(pdev, 0);
499 if (!pp->msi_irq) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500500 dev_err(dev, "failed to get msi irq\n");
Jingoo Hanf342d942013-09-06 15:54:59 +0900501 return -ENODEV;
502 }
503
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500504 ret = devm_request_irq(dev, pp->msi_irq,
Jingoo Hanf342d942013-09-06 15:54:59 +0900505 exynos_pcie_msi_irq_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200506 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500507 "exynos-pcie", exynos_pcie);
Jingoo Hanf342d942013-09-06 15:54:59 +0900508 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500509 dev_err(dev, "failed to request msi irq\n");
Jingoo Hanf342d942013-09-06 15:54:59 +0900510 return ret;
511 }
512 }
513
Jingoo Han4b1ced82013-07-31 17:14:10 +0900514 pp->root_bus_nr = -1;
515 pp->ops = &exynos_pcie_host_ops;
516
Jingoo Han4b1ced82013-07-31 17:14:10 +0900517 ret = dw_pcie_host_init(pp);
518 if (ret) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500519 dev_err(dev, "failed to initialize host\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900520 return ret;
521 }
522
523 return 0;
524}
525
526static int __init exynos_pcie_probe(struct platform_device *pdev)
527{
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500528 struct device *dev = &pdev->dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900529 struct exynos_pcie *exynos_pcie;
530 struct pcie_port *pp;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500531 struct device_node *np = dev->of_node;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900532 struct resource *elbi_base;
533 struct resource *phy_base;
534 struct resource *block_base;
535 int ret;
536
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500537 exynos_pcie = devm_kzalloc(dev, sizeof(*exynos_pcie), GFP_KERNEL);
Jingoo Han755ba5e2014-05-09 14:31:25 +0900538 if (!exynos_pcie)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900539 return -ENOMEM;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900540
541 pp = &exynos_pcie->pp;
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500542 pp->dev = dev;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900543
544 exynos_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
545
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500546 exynos_pcie->clk = devm_clk_get(dev, "pcie");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900547 if (IS_ERR(exynos_pcie->clk)) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500548 dev_err(dev, "Failed to get pcie rc clock\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900549 return PTR_ERR(exynos_pcie->clk);
550 }
551 ret = clk_prepare_enable(exynos_pcie->clk);
552 if (ret)
553 return ret;
554
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500555 exynos_pcie->bus_clk = devm_clk_get(dev, "pcie_bus");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900556 if (IS_ERR(exynos_pcie->bus_clk)) {
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500557 dev_err(dev, "Failed to get pcie bus clock\n");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900558 ret = PTR_ERR(exynos_pcie->bus_clk);
559 goto fail_clk;
560 }
561 ret = clk_prepare_enable(exynos_pcie->bus_clk);
562 if (ret)
563 goto fail_clk;
564
565 elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500566 exynos_pcie->elbi_base = devm_ioremap_resource(dev, elbi_base);
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800567 if (IS_ERR(exynos_pcie->elbi_base)) {
568 ret = PTR_ERR(exynos_pcie->elbi_base);
569 goto fail_bus_clk;
570 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900571
572 phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500573 exynos_pcie->phy_base = devm_ioremap_resource(dev, phy_base);
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800574 if (IS_ERR(exynos_pcie->phy_base)) {
575 ret = PTR_ERR(exynos_pcie->phy_base);
576 goto fail_bus_clk;
577 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900578
579 block_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
Bjorn Helgaasfae68d62016-10-06 13:33:41 -0500580 exynos_pcie->block_base = devm_ioremap_resource(dev, block_base);
Wei Yongjunf8db3c92013-09-29 10:29:11 +0800581 if (IS_ERR(exynos_pcie->block_base)) {
582 ret = PTR_ERR(exynos_pcie->block_base);
583 goto fail_bus_clk;
584 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900585
Bjorn Helgaascc08e822016-10-06 13:33:39 -0500586 ret = exynos_add_pcie_port(exynos_pcie, pdev);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900587 if (ret < 0)
588 goto fail_bus_clk;
589
590 platform_set_drvdata(pdev, exynos_pcie);
591 return 0;
592
593fail_bus_clk:
594 clk_disable_unprepare(exynos_pcie->bus_clk);
595fail_clk:
596 clk_disable_unprepare(exynos_pcie->clk);
597 return ret;
598}
599
600static int __exit exynos_pcie_remove(struct platform_device *pdev)
601{
602 struct exynos_pcie *exynos_pcie = platform_get_drvdata(pdev);
603
604 clk_disable_unprepare(exynos_pcie->bus_clk);
605 clk_disable_unprepare(exynos_pcie->clk);
606
607 return 0;
608}
609
610static const struct of_device_id exynos_pcie_of_match[] = {
611 { .compatible = "samsung,exynos5440-pcie", },
612 {},
613};
Jingoo Han4b1ced82013-07-31 17:14:10 +0900614
615static struct platform_driver exynos_pcie_driver = {
616 .remove = __exit_p(exynos_pcie_remove),
617 .driver = {
618 .name = "exynos-pcie",
Sachin Kamateb363092013-10-21 14:36:43 +0530619 .of_match_table = exynos_pcie_of_match,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900620 },
621};
622
623/* Exynos PCIe driver does not allow module unload */
624
Jingoo Han70b3e892014-10-22 13:58:49 +0900625static int __init exynos_pcie_init(void)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900626{
627 return platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
628}
Jingoo Han70b3e892014-10-22 13:58:49 +0900629subsys_initcall(exynos_pcie_init);