aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pci/host/pcie-designware.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/pci/host/pcie-designware.c')
-rw-r--r--drivers/pci/host/pcie-designware.c1044
1 files changed, 276 insertions, 768 deletions
diff --git a/drivers/pci/host/pcie-designware.c b/drivers/pci/host/pcie-designware.c
index 26bdbda8ff90..c10e9ac9bbbc 100644
--- a/drivers/pci/host/pcie-designware.c
+++ b/drivers/pci/host/pcie-designware.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * PCIe host controller driver for Samsung EXYNOS SoCs 2 * Synopsys Designware PCIe host controller driver
3 * 3 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd. 4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com 5 * http://www.samsung.com
@@ -11,74 +11,28 @@
11 * published by the Free Software Foundation. 11 * published by the Free Software Foundation.
12 */ 12 */
13 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> 14#include <linux/kernel.h>
19#include <linux/list.h>
20#include <linux/module.h> 15#include <linux/module.h>
21#include <linux/of.h>
22#include <linux/of_address.h> 16#include <linux/of_address.h>
23#include <linux/of_gpio.h>
24#include <linux/of_pci.h>
25#include <linux/pci.h> 17#include <linux/pci.h>
26#include <linux/pci_regs.h> 18#include <linux/pci_regs.h>
27#include <linux/platform_device.h>
28#include <linux/resource.h>
29#include <linux/signal.h>
30#include <linux/slab.h>
31#include <linux/types.h> 19#include <linux/types.h>
32 20
33struct pcie_port_info { 21#include "pcie-designware.h"
34 u32 cfg0_size;
35 u32 cfg1_size;
36 u32 io_size;
37 u32 mem_size;
38 phys_addr_t io_bus_addr;
39 phys_addr_t mem_bus_addr;
40};
41
42struct pcie_port {
43 struct device *dev;
44 u8 controller;
45 u8 root_bus_nr;
46 void __iomem *dbi_base;
47 void __iomem *elbi_base;
48 void __iomem *phy_base;
49 void __iomem *purple_base;
50 u64 cfg0_base;
51 void __iomem *va_cfg0_base;
52 u64 cfg1_base;
53 void __iomem *va_cfg1_base;
54 u64 io_base;
55 u64 mem_base;
56 spinlock_t conf_lock;
57 struct resource cfg;
58 struct resource io;
59 struct resource mem;
60 struct pcie_port_info config;
61 struct clk *clk;
62 struct clk *bus_clk;
63 int irq;
64 int reset_gpio;
65};
66
67/*
68 * Exynos PCIe IP consists of Synopsys specific part and Exynos
69 * specific part. Only core block is a Synopsys designware part;
70 * other parts are Exynos specific.
71 */
72 22
73/* Synopsis specific PCIE configuration registers */ 23/* Synopsis specific PCIE configuration registers */
74#define PCIE_PORT_LINK_CONTROL 0x710 24#define PCIE_PORT_LINK_CONTROL 0x710
75#define PORT_LINK_MODE_MASK (0x3f << 16) 25#define PORT_LINK_MODE_MASK (0x3f << 16)
26#define PORT_LINK_MODE_1_LANES (0x1 << 16)
27#define PORT_LINK_MODE_2_LANES (0x3 << 16)
76#define PORT_LINK_MODE_4_LANES (0x7 << 16) 28#define PORT_LINK_MODE_4_LANES (0x7 << 16)
77 29
78#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C 30#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
79#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17) 31#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
80#define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8) 32#define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
81#define PORT_LOGIC_LINK_WIDTH_4_LANES (0x7 << 8) 33#define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
34#define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
35#define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
82 36
83#define PCIE_MSI_ADDR_LO 0x820 37#define PCIE_MSI_ADDR_LO 0x820
84#define PCIE_MSI_ADDR_HI 0x824 38#define PCIE_MSI_ADDR_HI 0x824
@@ -108,69 +62,16 @@ struct pcie_port {
108#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16) 62#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
109#define PCIE_ATU_UPPER_TARGET 0x91C 63#define PCIE_ATU_UPPER_TARGET 0x91C
110 64
111/* Exynos specific PCIE configuration registers */ 65static struct hw_pci dw_pci;
112 66
113/* PCIe ELBI registers */ 67unsigned long global_io_offset;
114#define PCIE_IRQ_PULSE 0x000
115#define IRQ_INTA_ASSERT (0x1 << 0)
116#define IRQ_INTB_ASSERT (0x1 << 2)
117#define IRQ_INTC_ASSERT (0x1 << 4)
118#define IRQ_INTD_ASSERT (0x1 << 6)
119#define PCIE_IRQ_LEVEL 0x004
120#define PCIE_IRQ_SPECIAL 0x008
121#define PCIE_IRQ_EN_PULSE 0x00c
122#define PCIE_IRQ_EN_LEVEL 0x010
123#define PCIE_IRQ_EN_SPECIAL 0x014
124#define PCIE_PWR_RESET 0x018
125#define PCIE_CORE_RESET 0x01c
126#define PCIE_CORE_RESET_ENABLE (0x1 << 0)
127#define PCIE_STICKY_RESET 0x020
128#define PCIE_NONSTICKY_RESET 0x024
129#define PCIE_APP_INIT_RESET 0x028
130#define PCIE_APP_LTSSM_ENABLE 0x02c
131#define PCIE_ELBI_RDLH_LINKUP 0x064
132#define PCIE_ELBI_LTSSM_ENABLE 0x1
133#define PCIE_ELBI_SLV_AWMISC 0x11c
134#define PCIE_ELBI_SLV_ARMISC 0x120
135#define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
136
137/* PCIe Purple registers */
138#define PCIE_PHY_GLOBAL_RESET 0x000
139#define PCIE_PHY_COMMON_RESET 0x004
140#define PCIE_PHY_CMN_REG 0x008
141#define PCIE_PHY_MAC_RESET 0x00c
142#define PCIE_PHY_PLL_LOCKED 0x010
143#define PCIE_PHY_TRSVREG_RESET 0x020
144#define PCIE_PHY_TRSV_RESET 0x024
145
146/* PCIe PHY registers */
147#define PCIE_PHY_IMPEDANCE 0x004
148#define PCIE_PHY_PLL_DIV_0 0x008
149#define PCIE_PHY_PLL_BIAS 0x00c
150#define PCIE_PHY_DCC_FEEDBACK 0x014
151#define PCIE_PHY_PLL_DIV_1 0x05c
152#define PCIE_PHY_TRSV0_EMP_LVL 0x084
153#define PCIE_PHY_TRSV0_DRV_LVL 0x088
154#define PCIE_PHY_TRSV0_RXCDR 0x0ac
155#define PCIE_PHY_TRSV0_LVCC 0x0dc
156#define PCIE_PHY_TRSV1_EMP_LVL 0x144
157#define PCIE_PHY_TRSV1_RXCDR 0x16c
158#define PCIE_PHY_TRSV1_LVCC 0x19c
159#define PCIE_PHY_TRSV2_EMP_LVL 0x204
160#define PCIE_PHY_TRSV2_RXCDR 0x22c
161#define PCIE_PHY_TRSV2_LVCC 0x25c
162#define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
163#define PCIE_PHY_TRSV3_RXCDR 0x2ec
164#define PCIE_PHY_TRSV3_LVCC 0x31c
165
166static struct hw_pci exynos_pci;
167 68
168static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys) 69static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
169{ 70{
170 return sys->private_data; 71 return sys->private_data;
171} 72}
172 73
173static inline int cfg_read(void *addr, int where, int size, u32 *val) 74int cfg_read(void __iomem *addr, int where, int size, u32 *val)
174{ 75{
175 *val = readl(addr); 76 *val = readl(addr);
176 77
@@ -184,7 +85,7 @@ static inline int cfg_read(void *addr, int where, int size, u32 *val)
184 return PCIBIOS_SUCCESSFUL; 85 return PCIBIOS_SUCCESSFUL;
185} 86}
186 87
187static inline int cfg_write(void *addr, int where, int size, u32 val) 88int cfg_write(void __iomem *addr, int where, int size, u32 val)
188{ 89{
189 if (size == 4) 90 if (size == 4)
190 writel(val, addr); 91 writel(val, addr);
@@ -198,155 +99,217 @@ static inline int cfg_write(void *addr, int where, int size, u32 val)
198 return PCIBIOS_SUCCESSFUL; 99 return PCIBIOS_SUCCESSFUL;
199} 100}
200 101
201static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on) 102static inline void dw_pcie_readl_rc(struct pcie_port *pp, u32 reg, u32 *val)
202{ 103{
203 u32 val; 104 if (pp->ops->readl_rc)
204 105 pp->ops->readl_rc(pp, pp->dbi_base + reg, val);
205 if (on) { 106 else
206 val = readl(pp->elbi_base + PCIE_ELBI_SLV_AWMISC); 107 *val = readl(pp->dbi_base + reg);
207 val |= PCIE_ELBI_SLV_DBI_ENABLE;
208 writel(val, pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
209 } else {
210 val = readl(pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
211 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
212 writel(val, pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
213 }
214}
215
216static void exynos_pcie_sideband_dbi_r_mode(struct pcie_port *pp, bool on)
217{
218 u32 val;
219
220 if (on) {
221 val = readl(pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
222 val |= PCIE_ELBI_SLV_DBI_ENABLE;
223 writel(val, pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
224 } else {
225 val = readl(pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
226 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
227 writel(val, pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
228 }
229}
230
231static inline void readl_rc(struct pcie_port *pp, void *dbi_base, u32 *val)
232{
233 exynos_pcie_sideband_dbi_r_mode(pp, true);
234 *val = readl(dbi_base);
235 exynos_pcie_sideband_dbi_r_mode(pp, false);
236 return;
237} 108}
238 109
239static inline void writel_rc(struct pcie_port *pp, u32 val, void *dbi_base) 110static inline void dw_pcie_writel_rc(struct pcie_port *pp, u32 val, u32 reg)
240{ 111{
241 exynos_pcie_sideband_dbi_w_mode(pp, true); 112 if (pp->ops->writel_rc)
242 writel(val, dbi_base); 113 pp->ops->writel_rc(pp, val, pp->dbi_base + reg);
243 exynos_pcie_sideband_dbi_w_mode(pp, false); 114 else
244 return; 115 writel(val, pp->dbi_base + reg);
245} 116}
246 117
247static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size, 118int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
248 u32 *val) 119 u32 *val)
249{ 120{
250 int ret; 121 int ret;
251 122
252 exynos_pcie_sideband_dbi_r_mode(pp, true); 123 if (pp->ops->rd_own_conf)
253 ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val); 124 ret = pp->ops->rd_own_conf(pp, where, size, val);
254 exynos_pcie_sideband_dbi_r_mode(pp, false); 125 else
126 ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
127
255 return ret; 128 return ret;
256} 129}
257 130
258static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size, 131int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
259 u32 val) 132 u32 val)
260{ 133{
261 int ret; 134 int ret;
262 135
263 exynos_pcie_sideband_dbi_w_mode(pp, true); 136 if (pp->ops->wr_own_conf)
264 ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size, val); 137 ret = pp->ops->wr_own_conf(pp, where, size, val);
265 exynos_pcie_sideband_dbi_w_mode(pp, false); 138 else
139 ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size,
140 val);
141
266 return ret; 142 return ret;
267} 143}
268 144
269static void exynos_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev) 145int dw_pcie_link_up(struct pcie_port *pp)
146{
147 if (pp->ops->link_up)
148 return pp->ops->link_up(pp);
149 else
150 return 0;
151}
152
153int __init dw_pcie_host_init(struct pcie_port *pp)
270{ 154{
155 struct device_node *np = pp->dev->of_node;
156 struct of_pci_range range;
157 struct of_pci_range_parser parser;
271 u32 val; 158 u32 val;
272 void __iomem *dbi_base = pp->dbi_base;
273 159
274 /* Program viewport 0 : OUTBOUND : CFG0 */ 160 if (of_pci_range_parser_init(&parser, np)) {
275 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0; 161 dev_err(pp->dev, "missing ranges property\n");
276 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT); 162 return -EINVAL;
277 writel_rc(pp, pp->cfg0_base, dbi_base + PCIE_ATU_LOWER_BASE); 163 }
278 writel_rc(pp, (pp->cfg0_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE); 164
279 writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1, 165 /* Get the I/O and memory ranges from DT */
280 dbi_base + PCIE_ATU_LIMIT); 166 for_each_of_pci_range(&parser, &range) {
281 writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET); 167 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
282 writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET); 168 if (restype == IORESOURCE_IO) {
283 writel_rc(pp, PCIE_ATU_TYPE_CFG0, dbi_base + PCIE_ATU_CR1); 169 of_pci_range_to_resource(&range, np, &pp->io);
284 val = PCIE_ATU_ENABLE; 170 pp->io.name = "I/O";
285 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2); 171 pp->io.start = max_t(resource_size_t,
172 PCIBIOS_MIN_IO,
173 range.pci_addr + global_io_offset);
174 pp->io.end = min_t(resource_size_t,
175 IO_SPACE_LIMIT,
176 range.pci_addr + range.size
177 + global_io_offset);
178 pp->config.io_size = resource_size(&pp->io);
179 pp->config.io_bus_addr = range.pci_addr;
180 }
181 if (restype == IORESOURCE_MEM) {
182 of_pci_range_to_resource(&range, np, &pp->mem);
183 pp->mem.name = "MEM";
184 pp->config.mem_size = resource_size(&pp->mem);
185 pp->config.mem_bus_addr = range.pci_addr;
186 }
187 if (restype == 0) {
188 of_pci_range_to_resource(&range, np, &pp->cfg);
189 pp->config.cfg0_size = resource_size(&pp->cfg)/2;
190 pp->config.cfg1_size = resource_size(&pp->cfg)/2;
191 }
192 }
193
194 if (!pp->dbi_base) {
195 pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
196 resource_size(&pp->cfg));
197 if (!pp->dbi_base) {
198 dev_err(pp->dev, "error with ioremap\n");
199 return -ENOMEM;
200 }
201 }
202
203 pp->cfg0_base = pp->cfg.start;
204 pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size;
205 pp->io_base = pp->io.start;
206 pp->mem_base = pp->mem.start;
207
208 pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
209 pp->config.cfg0_size);
210 if (!pp->va_cfg0_base) {
211 dev_err(pp->dev, "error with ioremap in function\n");
212 return -ENOMEM;
213 }
214 pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
215 pp->config.cfg1_size);
216 if (!pp->va_cfg1_base) {
217 dev_err(pp->dev, "error with ioremap\n");
218 return -ENOMEM;
219 }
220
221 if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
222 dev_err(pp->dev, "Failed to parse the number of lanes\n");
223 return -EINVAL;
224 }
225
226 if (pp->ops->host_init)
227 pp->ops->host_init(pp);
228
229 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
230
231 /* program correct class for RC */
232 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
233
234 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
235 val |= PORT_LOGIC_SPEED_CHANGE;
236 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
237
238 dw_pci.nr_controllers = 1;
239 dw_pci.private_data = (void **)&pp;
240
241 pci_common_init(&dw_pci);
242 pci_assign_unassigned_resources();
243#ifdef CONFIG_PCI_DOMAINS
244 dw_pci.domain++;
245#endif
246
247 return 0;
286} 248}
287 249
288static void exynos_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev) 250static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
289{ 251{
290 u32 val; 252 /* Program viewport 0 : OUTBOUND : CFG0 */
291 void __iomem *dbi_base = pp->dbi_base; 253 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
254 PCIE_ATU_VIEWPORT);
255 dw_pcie_writel_rc(pp, pp->cfg0_base, PCIE_ATU_LOWER_BASE);
256 dw_pcie_writel_rc(pp, (pp->cfg0_base >> 32), PCIE_ATU_UPPER_BASE);
257 dw_pcie_writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
258 PCIE_ATU_LIMIT);
259 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
260 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
261 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1);
262 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
263}
292 264
265static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
266{
293 /* Program viewport 1 : OUTBOUND : CFG1 */ 267 /* Program viewport 1 : OUTBOUND : CFG1 */
294 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1; 268 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
295 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT); 269 PCIE_ATU_VIEWPORT);
296 writel_rc(pp, PCIE_ATU_TYPE_CFG1, dbi_base + PCIE_ATU_CR1); 270 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1);
297 val = PCIE_ATU_ENABLE; 271 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
298 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2); 272 dw_pcie_writel_rc(pp, pp->cfg1_base, PCIE_ATU_LOWER_BASE);
299 writel_rc(pp, pp->cfg1_base, dbi_base + PCIE_ATU_LOWER_BASE); 273 dw_pcie_writel_rc(pp, (pp->cfg1_base >> 32), PCIE_ATU_UPPER_BASE);
300 writel_rc(pp, (pp->cfg1_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE); 274 dw_pcie_writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
301 writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1, 275 PCIE_ATU_LIMIT);
302 dbi_base + PCIE_ATU_LIMIT); 276 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
303 writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET); 277 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
304 writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
305} 278}
306 279
307static void exynos_pcie_prog_viewport_mem_outbound(struct pcie_port *pp) 280static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
308{ 281{
309 u32 val;
310 void __iomem *dbi_base = pp->dbi_base;
311
312 /* Program viewport 0 : OUTBOUND : MEM */ 282 /* Program viewport 0 : OUTBOUND : MEM */
313 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0; 283 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
314 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT); 284 PCIE_ATU_VIEWPORT);
315 writel_rc(pp, PCIE_ATU_TYPE_MEM, dbi_base + PCIE_ATU_CR1); 285 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
316 val = PCIE_ATU_ENABLE; 286 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
317 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2); 287 dw_pcie_writel_rc(pp, pp->mem_base, PCIE_ATU_LOWER_BASE);
318 writel_rc(pp, pp->mem_base, dbi_base + PCIE_ATU_LOWER_BASE); 288 dw_pcie_writel_rc(pp, (pp->mem_base >> 32), PCIE_ATU_UPPER_BASE);
319 writel_rc(pp, (pp->mem_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE); 289 dw_pcie_writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
320 writel_rc(pp, pp->mem_base + pp->config.mem_size - 1, 290 PCIE_ATU_LIMIT);
321 dbi_base + PCIE_ATU_LIMIT); 291 dw_pcie_writel_rc(pp, pp->config.mem_bus_addr, PCIE_ATU_LOWER_TARGET);
322 writel_rc(pp, pp->config.mem_bus_addr, 292 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
323 dbi_base + PCIE_ATU_LOWER_TARGET); 293 PCIE_ATU_UPPER_TARGET);
324 writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
325 dbi_base + PCIE_ATU_UPPER_TARGET);
326} 294}
327 295
328static void exynos_pcie_prog_viewport_io_outbound(struct pcie_port *pp) 296static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
329{ 297{
330 u32 val;
331 void __iomem *dbi_base = pp->dbi_base;
332
333 /* Program viewport 1 : OUTBOUND : IO */ 298 /* Program viewport 1 : OUTBOUND : IO */
334 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1; 299 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
335 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT); 300 PCIE_ATU_VIEWPORT);
336 writel_rc(pp, PCIE_ATU_TYPE_IO, dbi_base + PCIE_ATU_CR1); 301 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1);
337 val = PCIE_ATU_ENABLE; 302 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
338 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2); 303 dw_pcie_writel_rc(pp, pp->io_base, PCIE_ATU_LOWER_BASE);
339 writel_rc(pp, pp->io_base, dbi_base + PCIE_ATU_LOWER_BASE); 304 dw_pcie_writel_rc(pp, (pp->io_base >> 32), PCIE_ATU_UPPER_BASE);
340 writel_rc(pp, (pp->io_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE); 305 dw_pcie_writel_rc(pp, pp->io_base + pp->config.io_size - 1,
341 writel_rc(pp, pp->io_base + pp->config.io_size - 1, 306 PCIE_ATU_LIMIT);
342 dbi_base + PCIE_ATU_LIMIT); 307 dw_pcie_writel_rc(pp, pp->config.io_bus_addr, PCIE_ATU_LOWER_TARGET);
343 writel_rc(pp, pp->config.io_bus_addr, 308 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
344 dbi_base + PCIE_ATU_LOWER_TARGET); 309 PCIE_ATU_UPPER_TARGET);
345 writel_rc(pp, upper_32_bits(pp->config.io_bus_addr), 310}
346 dbi_base + PCIE_ATU_UPPER_TARGET); 311
347} 312static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
348
349static int exynos_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
350 u32 devfn, int where, int size, u32 *val) 313 u32 devfn, int where, int size, u32 *val)
351{ 314{
352 int ret = PCIBIOS_SUCCESSFUL; 315 int ret = PCIBIOS_SUCCESSFUL;
@@ -357,19 +320,19 @@ static int exynos_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
357 address = where & ~0x3; 320 address = where & ~0x3;
358 321
359 if (bus->parent->number == pp->root_bus_nr) { 322 if (bus->parent->number == pp->root_bus_nr) {
360 exynos_pcie_prog_viewport_cfg0(pp, busdev); 323 dw_pcie_prog_viewport_cfg0(pp, busdev);
361 ret = cfg_read(pp->va_cfg0_base + address, where, size, val); 324 ret = cfg_read(pp->va_cfg0_base + address, where, size, val);
362 exynos_pcie_prog_viewport_mem_outbound(pp); 325 dw_pcie_prog_viewport_mem_outbound(pp);
363 } else { 326 } else {
364 exynos_pcie_prog_viewport_cfg1(pp, busdev); 327 dw_pcie_prog_viewport_cfg1(pp, busdev);
365 ret = cfg_read(pp->va_cfg1_base + address, where, size, val); 328 ret = cfg_read(pp->va_cfg1_base + address, where, size, val);
366 exynos_pcie_prog_viewport_io_outbound(pp); 329 dw_pcie_prog_viewport_io_outbound(pp);
367 } 330 }
368 331
369 return ret; 332 return ret;
370} 333}
371 334
372static int exynos_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus, 335static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
373 u32 devfn, int where, int size, u32 val) 336 u32 devfn, int where, int size, u32 val)
374{ 337{
375 int ret = PCIBIOS_SUCCESSFUL; 338 int ret = PCIBIOS_SUCCESSFUL;
@@ -380,59 +343,25 @@ static int exynos_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
380 address = where & ~0x3; 343 address = where & ~0x3;
381 344
382 if (bus->parent->number == pp->root_bus_nr) { 345 if (bus->parent->number == pp->root_bus_nr) {
383 exynos_pcie_prog_viewport_cfg0(pp, busdev); 346 dw_pcie_prog_viewport_cfg0(pp, busdev);
384 ret = cfg_write(pp->va_cfg0_base + address, where, size, val); 347 ret = cfg_write(pp->va_cfg0_base + address, where, size, val);
385 exynos_pcie_prog_viewport_mem_outbound(pp); 348 dw_pcie_prog_viewport_mem_outbound(pp);
386 } else { 349 } else {
387 exynos_pcie_prog_viewport_cfg1(pp, busdev); 350 dw_pcie_prog_viewport_cfg1(pp, busdev);
388 ret = cfg_write(pp->va_cfg1_base + address, where, size, val); 351 ret = cfg_write(pp->va_cfg1_base + address, where, size, val);
389 exynos_pcie_prog_viewport_io_outbound(pp); 352 dw_pcie_prog_viewport_io_outbound(pp);
390 } 353 }
391 354
392 return ret; 355 return ret;
393} 356}
394 357
395static unsigned long global_io_offset;
396
397static int exynos_pcie_setup(int nr, struct pci_sys_data *sys)
398{
399 struct pcie_port *pp;
400
401 pp = sys_to_pcie(sys);
402
403 if (!pp)
404 return 0;
405
406 if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
407 sys->io_offset = global_io_offset - pp->config.io_bus_addr;
408 pci_ioremap_io(sys->io_offset, pp->io.start);
409 global_io_offset += SZ_64K;
410 pci_add_resource_offset(&sys->resources, &pp->io,
411 sys->io_offset);
412 }
413
414 sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
415 pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
416
417 return 1;
418}
419
420static int exynos_pcie_link_up(struct pcie_port *pp)
421{
422 u32 val = readl(pp->elbi_base + PCIE_ELBI_RDLH_LINKUP);
423
424 if (val == PCIE_ELBI_LTSSM_ENABLE)
425 return 1;
426 358
427 return 0; 359static int dw_pcie_valid_config(struct pcie_port *pp,
428}
429
430static int exynos_pcie_valid_config(struct pcie_port *pp,
431 struct pci_bus *bus, int dev) 360 struct pci_bus *bus, int dev)
432{ 361{
433 /* If there is no link, then there is no device */ 362 /* If there is no link, then there is no device */
434 if (bus->number != pp->root_bus_nr) { 363 if (bus->number != pp->root_bus_nr) {
435 if (!exynos_pcie_link_up(pp)) 364 if (!dw_pcie_link_up(pp))
436 return 0; 365 return 0;
437 } 366 }
438 367
@@ -450,7 +379,7 @@ static int exynos_pcie_valid_config(struct pcie_port *pp,
450 return 1; 379 return 1;
451} 380}
452 381
453static int exynos_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, 382static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
454 int size, u32 *val) 383 int size, u32 *val)
455{ 384{
456 struct pcie_port *pp = sys_to_pcie(bus->sysdata); 385 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
@@ -462,23 +391,23 @@ static int exynos_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
462 return -EINVAL; 391 return -EINVAL;
463 } 392 }
464 393
465 if (exynos_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) { 394 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
466 *val = 0xffffffff; 395 *val = 0xffffffff;
467 return PCIBIOS_DEVICE_NOT_FOUND; 396 return PCIBIOS_DEVICE_NOT_FOUND;
468 } 397 }
469 398
470 spin_lock_irqsave(&pp->conf_lock, flags); 399 spin_lock_irqsave(&pp->conf_lock, flags);
471 if (bus->number != pp->root_bus_nr) 400 if (bus->number != pp->root_bus_nr)
472 ret = exynos_pcie_rd_other_conf(pp, bus, devfn, 401 ret = dw_pcie_rd_other_conf(pp, bus, devfn,
473 where, size, val); 402 where, size, val);
474 else 403 else
475 ret = exynos_pcie_rd_own_conf(pp, where, size, val); 404 ret = dw_pcie_rd_own_conf(pp, where, size, val);
476 spin_unlock_irqrestore(&pp->conf_lock, flags); 405 spin_unlock_irqrestore(&pp->conf_lock, flags);
477 406
478 return ret; 407 return ret;
479} 408}
480 409
481static int exynos_pcie_wr_conf(struct pci_bus *bus, u32 devfn, 410static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
482 int where, int size, u32 val) 411 int where, int size, u32 val)
483{ 412{
484 struct pcie_port *pp = sys_to_pcie(bus->sysdata); 413 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
@@ -490,34 +419,56 @@ static int exynos_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
490 return -EINVAL; 419 return -EINVAL;
491 } 420 }
492 421
493 if (exynos_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) 422 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
494 return PCIBIOS_DEVICE_NOT_FOUND; 423 return PCIBIOS_DEVICE_NOT_FOUND;
495 424
496 spin_lock_irqsave(&pp->conf_lock, flags); 425 spin_lock_irqsave(&pp->conf_lock, flags);
497 if (bus->number != pp->root_bus_nr) 426 if (bus->number != pp->root_bus_nr)
498 ret = exynos_pcie_wr_other_conf(pp, bus, devfn, 427 ret = dw_pcie_wr_other_conf(pp, bus, devfn,
499 where, size, val); 428 where, size, val);
500 else 429 else
501 ret = exynos_pcie_wr_own_conf(pp, where, size, val); 430 ret = dw_pcie_wr_own_conf(pp, where, size, val);
502 spin_unlock_irqrestore(&pp->conf_lock, flags); 431 spin_unlock_irqrestore(&pp->conf_lock, flags);
503 432
504 return ret; 433 return ret;
505} 434}
506 435
507static struct pci_ops exynos_pcie_ops = { 436static struct pci_ops dw_pcie_ops = {
508 .read = exynos_pcie_rd_conf, 437 .read = dw_pcie_rd_conf,
509 .write = exynos_pcie_wr_conf, 438 .write = dw_pcie_wr_conf,
510}; 439};
511 440
512static struct pci_bus *exynos_pcie_scan_bus(int nr, 441int dw_pcie_setup(int nr, struct pci_sys_data *sys)
513 struct pci_sys_data *sys) 442{
443 struct pcie_port *pp;
444
445 pp = sys_to_pcie(sys);
446
447 if (!pp)
448 return 0;
449
450 if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
451 sys->io_offset = global_io_offset - pp->config.io_bus_addr;
452 pci_ioremap_io(sys->io_offset, pp->io.start);
453 global_io_offset += SZ_64K;
454 pci_add_resource_offset(&sys->resources, &pp->io,
455 sys->io_offset);
456 }
457
458 sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
459 pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
460
461 return 1;
462}
463
464struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
514{ 465{
515 struct pci_bus *bus; 466 struct pci_bus *bus;
516 struct pcie_port *pp = sys_to_pcie(sys); 467 struct pcie_port *pp = sys_to_pcie(sys);
517 468
518 if (pp) { 469 if (pp) {
519 pp->root_bus_nr = sys->busnr; 470 pp->root_bus_nr = sys->busnr;
520 bus = pci_scan_root_bus(NULL, sys->busnr, &exynos_pcie_ops, 471 bus = pci_scan_root_bus(NULL, sys->busnr, &dw_pcie_ops,
521 sys, &sys->resources); 472 sys, &sys->resources);
522 } else { 473 } else {
523 bus = NULL; 474 bus = NULL;
@@ -527,531 +478,88 @@ static struct pci_bus *exynos_pcie_scan_bus(int nr,
527 return bus; 478 return bus;
528} 479}
529 480
530static int exynos_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 481int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
531{ 482{
532 struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata); 483 struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
533 484
534 return pp->irq; 485 return pp->irq;
535} 486}
536 487
537static struct hw_pci exynos_pci = { 488static struct hw_pci dw_pci = {
538 .setup = exynos_pcie_setup, 489 .setup = dw_pcie_setup,
539 .scan = exynos_pcie_scan_bus, 490 .scan = dw_pcie_scan_bus,
540 .map_irq = exynos_pcie_map_irq, 491 .map_irq = dw_pcie_map_irq,
541}; 492};
542 493
543static void exynos_pcie_setup_rc(struct pcie_port *pp) 494void dw_pcie_setup_rc(struct pcie_port *pp)
544{ 495{
545 struct pcie_port_info *config = &pp->config; 496 struct pcie_port_info *config = &pp->config;
546 void __iomem *dbi_base = pp->dbi_base;
547 u32 val; 497 u32 val;
548 u32 membase; 498 u32 membase;
549 u32 memlimit; 499 u32 memlimit;
550 500
551 /* set the number of lines as 4 */ 501 /* set the number of lines as 4 */
552 readl_rc(pp, dbi_base + PCIE_PORT_LINK_CONTROL, &val); 502 dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
553 val &= ~PORT_LINK_MODE_MASK; 503 val &= ~PORT_LINK_MODE_MASK;
554 val |= PORT_LINK_MODE_4_LANES; 504 switch (pp->lanes) {
555 writel_rc(pp, val, dbi_base + PCIE_PORT_LINK_CONTROL); 505 case 1:
506 val |= PORT_LINK_MODE_1_LANES;
507 break;
508 case 2:
509 val |= PORT_LINK_MODE_2_LANES;
510 break;
511 case 4:
512 val |= PORT_LINK_MODE_4_LANES;
513 break;
514 }
515 dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
556 516
557 /* set link width speed control register */ 517 /* set link width speed control register */
558 readl_rc(pp, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL, &val); 518 dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
559 val &= ~PORT_LOGIC_LINK_WIDTH_MASK; 519 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
560 val |= PORT_LOGIC_LINK_WIDTH_4_LANES; 520 switch (pp->lanes) {
561 writel_rc(pp, val, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL); 521 case 1:
522 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
523 break;
524 case 2:
525 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
526 break;
527 case 4:
528 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
529 break;
530 }
531 dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
562 532
563 /* setup RC BARs */ 533 /* setup RC BARs */
564 writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_0); 534 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
565 writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_1); 535 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_1);
566 536
567 /* setup interrupt pins */ 537 /* setup interrupt pins */
568 readl_rc(pp, dbi_base + PCI_INTERRUPT_LINE, &val); 538 dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
569 val &= 0xffff00ff; 539 val &= 0xffff00ff;
570 val |= 0x00000100; 540 val |= 0x00000100;
571 writel_rc(pp, val, dbi_base + PCI_INTERRUPT_LINE); 541 dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
572 542
573 /* setup bus numbers */ 543 /* setup bus numbers */
574 readl_rc(pp, dbi_base + PCI_PRIMARY_BUS, &val); 544 dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
575 val &= 0xff000000; 545 val &= 0xff000000;
576 val |= 0x00010100; 546 val |= 0x00010100;
577 writel_rc(pp, val, dbi_base + PCI_PRIMARY_BUS); 547 dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
578 548
579 /* setup memory base, memory limit */ 549 /* setup memory base, memory limit */
580 membase = ((u32)pp->mem_base & 0xfff00000) >> 16; 550 membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
581 memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000; 551 memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
582 val = memlimit | membase; 552 val = memlimit | membase;
583 writel_rc(pp, val, dbi_base + PCI_MEMORY_BASE); 553 dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE);
584 554
585 /* setup command register */ 555 /* setup command register */
586 readl_rc(pp, dbi_base + PCI_COMMAND, &val); 556 dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
587 val &= 0xffff0000; 557 val &= 0xffff0000;
588 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | 558 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
589 PCI_COMMAND_MASTER | PCI_COMMAND_SERR; 559 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
590 writel_rc(pp, val, dbi_base + PCI_COMMAND); 560 dw_pcie_writel_rc(pp, val, PCI_COMMAND);
591}
592
593static void exynos_pcie_assert_core_reset(struct pcie_port *pp)
594{
595 u32 val;
596 void __iomem *elbi_base = pp->elbi_base;
597
598 val = readl(elbi_base + PCIE_CORE_RESET);
599 val &= ~PCIE_CORE_RESET_ENABLE;
600 writel(val, elbi_base + PCIE_CORE_RESET);
601 writel(0, elbi_base + PCIE_PWR_RESET);
602 writel(0, elbi_base + PCIE_STICKY_RESET);
603 writel(0, elbi_base + PCIE_NONSTICKY_RESET);
604}
605
606static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
607{
608 u32 val;
609 void __iomem *elbi_base = pp->elbi_base;
610 void __iomem *purple_base = pp->purple_base;
611
612 val = readl(elbi_base + PCIE_CORE_RESET);
613 val |= PCIE_CORE_RESET_ENABLE;
614 writel(val, elbi_base + PCIE_CORE_RESET);
615 writel(1, elbi_base + PCIE_STICKY_RESET);
616 writel(1, elbi_base + PCIE_NONSTICKY_RESET);
617 writel(1, elbi_base + PCIE_APP_INIT_RESET);
618 writel(0, elbi_base + PCIE_APP_INIT_RESET);
619 writel(1, purple_base + PCIE_PHY_MAC_RESET);
620}
621
622static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
623{
624 void __iomem *purple_base = pp->purple_base;
625
626 writel(0, purple_base + PCIE_PHY_MAC_RESET);
627 writel(1, purple_base + PCIE_PHY_GLOBAL_RESET);
628}
629
630static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
631{
632 void __iomem *elbi_base = pp->elbi_base;
633 void __iomem *purple_base = pp->purple_base;
634
635 writel(0, purple_base + PCIE_PHY_GLOBAL_RESET);
636 writel(1, elbi_base + PCIE_PWR_RESET);
637 writel(0, purple_base + PCIE_PHY_COMMON_RESET);
638 writel(0, purple_base + PCIE_PHY_CMN_REG);
639 writel(0, purple_base + PCIE_PHY_TRSVREG_RESET);
640 writel(0, purple_base + PCIE_PHY_TRSV_RESET);
641}
642
643static void exynos_pcie_init_phy(struct pcie_port *pp)
644{
645 void __iomem *phy_base = pp->phy_base;
646
647 /* DCC feedback control off */
648 writel(0x29, phy_base + PCIE_PHY_DCC_FEEDBACK);
649
650 /* set TX/RX impedance */
651 writel(0xd5, phy_base + PCIE_PHY_IMPEDANCE);
652
653 /* set 50Mhz PHY clock */
654 writel(0x14, phy_base + PCIE_PHY_PLL_DIV_0);
655 writel(0x12, phy_base + PCIE_PHY_PLL_DIV_1);
656
657 /* set TX Differential output for lane 0 */
658 writel(0x7f, phy_base + PCIE_PHY_TRSV0_DRV_LVL);
659
660 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
661 writel(0x0, phy_base + PCIE_PHY_TRSV0_EMP_LVL);
662
663 /* set RX clock and data recovery bandwidth */
664 writel(0xe7, phy_base + PCIE_PHY_PLL_BIAS);
665 writel(0x82, phy_base + PCIE_PHY_TRSV0_RXCDR);
666 writel(0x82, phy_base + PCIE_PHY_TRSV1_RXCDR);
667 writel(0x82, phy_base + PCIE_PHY_TRSV2_RXCDR);
668 writel(0x82, phy_base + PCIE_PHY_TRSV3_RXCDR);
669
670 /* change TX Pre-emphasis Level Control for lanes */
671 writel(0x39, phy_base + PCIE_PHY_TRSV0_EMP_LVL);
672 writel(0x39, phy_base + PCIE_PHY_TRSV1_EMP_LVL);
673 writel(0x39, phy_base + PCIE_PHY_TRSV2_EMP_LVL);
674 writel(0x39, phy_base + PCIE_PHY_TRSV3_EMP_LVL);
675
676 /* set LVCC */
677 writel(0x20, phy_base + PCIE_PHY_TRSV0_LVCC);
678 writel(0xa0, phy_base + PCIE_PHY_TRSV1_LVCC);
679 writel(0xa0, phy_base + PCIE_PHY_TRSV2_LVCC);
680 writel(0xa0, phy_base + PCIE_PHY_TRSV3_LVCC);
681}
682
683static void exynos_pcie_assert_reset(struct pcie_port *pp)
684{
685 if (pp->reset_gpio >= 0)
686 devm_gpio_request_one(pp->dev, pp->reset_gpio,
687 GPIOF_OUT_INIT_HIGH, "RESET");
688 return;
689}
690
691static int exynos_pcie_establish_link(struct pcie_port *pp)
692{
693 u32 val;
694 int count = 0;
695 void __iomem *elbi_base = pp->elbi_base;
696 void __iomem *purple_base = pp->purple_base;
697 void __iomem *phy_base = pp->phy_base;
698
699 if (exynos_pcie_link_up(pp)) {
700 dev_err(pp->dev, "Link already up\n");
701 return 0;
702 }
703
704 /* assert reset signals */
705 exynos_pcie_assert_core_reset(pp);
706 exynos_pcie_assert_phy_reset(pp);
707
708 /* de-assert phy reset */
709 exynos_pcie_deassert_phy_reset(pp);
710
711 /* initialize phy */
712 exynos_pcie_init_phy(pp);
713
714 /* pulse for common reset */
715 writel(1, purple_base + PCIE_PHY_COMMON_RESET);
716 udelay(500);
717 writel(0, purple_base + PCIE_PHY_COMMON_RESET);
718
719 /* de-assert core reset */
720 exynos_pcie_deassert_core_reset(pp);
721
722 /* setup root complex */
723 exynos_pcie_setup_rc(pp);
724
725 /* assert reset signal */
726 exynos_pcie_assert_reset(pp);
727
728 /* assert LTSSM enable */
729 writel(PCIE_ELBI_LTSSM_ENABLE, elbi_base + PCIE_APP_LTSSM_ENABLE);
730
731 /* check if the link is up or not */
732 while (!exynos_pcie_link_up(pp)) {
733 mdelay(100);
734 count++;
735 if (count == 10) {
736 while (readl(phy_base + PCIE_PHY_PLL_LOCKED) == 0) {
737 val = readl(purple_base + PCIE_PHY_PLL_LOCKED);
738 dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
739 }
740 dev_err(pp->dev, "PCIe Link Fail\n");
741 return -EINVAL;
742 }
743 }
744
745 dev_info(pp->dev, "Link up\n");
746
747 return 0;
748}
749
750static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
751{
752 u32 val;
753 void __iomem *elbi_base = pp->elbi_base;
754
755 val = readl(elbi_base + PCIE_IRQ_PULSE);
756 writel(val, elbi_base + PCIE_IRQ_PULSE);
757 return;
758}
759
760static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
761{
762 u32 val;
763 void __iomem *elbi_base = pp->elbi_base;
764
765 /* enable INTX interrupt */
766 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
767 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT,
768 writel(val, elbi_base + PCIE_IRQ_EN_PULSE);
769 return;
770}
771
772static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
773{
774 struct pcie_port *pp = arg;
775
776 exynos_pcie_clear_irq_pulse(pp);
777 return IRQ_HANDLED;
778}
779
780static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
781{
782 exynos_pcie_enable_irq_pulse(pp);
783 return;
784}
785
786static void exynos_pcie_host_init(struct pcie_port *pp)
787{
788 struct pcie_port_info *config = &pp->config;
789 u32 val;
790
791 /* Keep first 64K for IO */
792 pp->cfg0_base = pp->cfg.start;
793 pp->cfg1_base = pp->cfg.start + config->cfg0_size;
794 pp->io_base = pp->io.start;
795 pp->mem_base = pp->mem.start;
796
797 /* enable link */
798 exynos_pcie_establish_link(pp);
799
800 exynos_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
801
802 /* program correct class for RC */
803 exynos_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
804
805 exynos_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
806 val |= PORT_LOGIC_SPEED_CHANGE;
807 exynos_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
808
809 exynos_pcie_enable_interrupts(pp);
810}
811
812static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev)
813{
814 struct resource *elbi_base;
815 struct resource *phy_base;
816 struct resource *purple_base;
817 int ret;
818
819 elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
820 if (!elbi_base) {
821 dev_err(&pdev->dev, "couldn't get elbi base resource\n");
822 return -EINVAL;
823 }
824 pp->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
825 if (IS_ERR(pp->elbi_base))
826 return PTR_ERR(pp->elbi_base);
827
828 phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
829 if (!phy_base) {
830 dev_err(&pdev->dev, "couldn't get phy base resource\n");
831 return -EINVAL;
832 }
833 pp->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
834 if (IS_ERR(pp->phy_base))
835 return PTR_ERR(pp->phy_base);
836
837 purple_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
838 if (!purple_base) {
839 dev_err(&pdev->dev, "couldn't get purple base resource\n");
840 return -EINVAL;
841 }
842 pp->purple_base = devm_ioremap_resource(&pdev->dev, purple_base);
843 if (IS_ERR(pp->purple_base))
844 return PTR_ERR(pp->purple_base);
845
846 pp->irq = platform_get_irq(pdev, 1);
847 if (!pp->irq) {
848 dev_err(&pdev->dev, "failed to get irq\n");
849 return -ENODEV;
850 }
851 ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
852 IRQF_SHARED, "exynos-pcie", pp);
853 if (ret) {
854 dev_err(&pdev->dev, "failed to request irq\n");
855 return ret;
856 }
857
858 pp->dbi_base = devm_ioremap(&pdev->dev, pp->cfg.start,
859 resource_size(&pp->cfg));
860 if (!pp->dbi_base) {
861 dev_err(&pdev->dev, "error with ioremap\n");
862 return -ENOMEM;
863 }
864
865 pp->root_bus_nr = -1;
866
867 spin_lock_init(&pp->conf_lock);
868 exynos_pcie_host_init(pp);
869 pp->va_cfg0_base = devm_ioremap(&pdev->dev, pp->cfg0_base,
870 pp->config.cfg0_size);
871 if (!pp->va_cfg0_base) {
872 dev_err(pp->dev, "error with ioremap in function\n");
873 return -ENOMEM;
874 }
875 pp->va_cfg1_base = devm_ioremap(&pdev->dev, pp->cfg1_base,
876 pp->config.cfg1_size);
877 if (!pp->va_cfg1_base) {
878 dev_err(pp->dev, "error with ioremap\n");
879 return -ENOMEM;
880 }
881
882 return 0;
883}
884
885static int __init exynos_pcie_probe(struct platform_device *pdev)
886{
887 struct pcie_port *pp;
888 struct device_node *np = pdev->dev.of_node;
889 struct of_pci_range range;
890 struct of_pci_range_parser parser;
891 int ret;
892
893 pp = devm_kzalloc(&pdev->dev, sizeof(*pp), GFP_KERNEL);
894 if (!pp) {
895 dev_err(&pdev->dev, "no memory for pcie port\n");
896 return -ENOMEM;
897 }
898
899 pp->dev = &pdev->dev;
900
901 if (of_pci_range_parser_init(&parser, np)) {
902 dev_err(&pdev->dev, "missing ranges property\n");
903 return -EINVAL;
904 }
905
906 /* Get the I/O and memory ranges from DT */
907 for_each_of_pci_range(&parser, &range) {
908 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
909 if (restype == IORESOURCE_IO) {
910 of_pci_range_to_resource(&range, np, &pp->io);
911 pp->io.name = "I/O";
912 pp->io.start = max_t(resource_size_t,
913 PCIBIOS_MIN_IO,
914 range.pci_addr + global_io_offset);
915 pp->io.end = min_t(resource_size_t,
916 IO_SPACE_LIMIT,
917 range.pci_addr + range.size
918 + global_io_offset);
919 pp->config.io_size = resource_size(&pp->io);
920 pp->config.io_bus_addr = range.pci_addr;
921 }
922 if (restype == IORESOURCE_MEM) {
923 of_pci_range_to_resource(&range, np, &pp->mem);
924 pp->mem.name = "MEM";
925 pp->config.mem_size = resource_size(&pp->mem);
926 pp->config.mem_bus_addr = range.pci_addr;
927 }
928 if (restype == 0) {
929 of_pci_range_to_resource(&range, np, &pp->cfg);
930 pp->config.cfg0_size = resource_size(&pp->cfg)/2;
931 pp->config.cfg1_size = resource_size(&pp->cfg)/2;
932 }
933 }
934
935 pp->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
936
937 pp->clk = devm_clk_get(&pdev->dev, "pcie");
938 if (IS_ERR(pp->clk)) {
939 dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
940 return PTR_ERR(pp->clk);
941 }
942 ret = clk_prepare_enable(pp->clk);
943 if (ret)
944 return ret;
945
946 pp->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
947 if (IS_ERR(pp->bus_clk)) {
948 dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
949 ret = PTR_ERR(pp->bus_clk);
950 goto fail_clk;
951 }
952 ret = clk_prepare_enable(pp->bus_clk);
953 if (ret)
954 goto fail_clk;
955
956 ret = add_pcie_port(pp, pdev);
957 if (ret < 0)
958 goto fail_bus_clk;
959
960 pp->controller = exynos_pci.nr_controllers;
961 exynos_pci.nr_controllers = 1;
962 exynos_pci.private_data = (void **)&pp;
963
964 pci_common_init(&exynos_pci);
965 pci_assign_unassigned_resources();
966#ifdef CONFIG_PCI_DOMAINS
967 exynos_pci.domain++;
968#endif
969
970 platform_set_drvdata(pdev, pp);
971 return 0;
972
973fail_bus_clk:
974 clk_disable_unprepare(pp->bus_clk);
975fail_clk:
976 clk_disable_unprepare(pp->clk);
977 return ret;
978}
979
980static int __exit exynos_pcie_remove(struct platform_device *pdev)
981{
982 struct pcie_port *pp = platform_get_drvdata(pdev);
983
984 clk_disable_unprepare(pp->bus_clk);
985 clk_disable_unprepare(pp->clk);
986
987 return 0;
988}
989
990static const struct of_device_id exynos_pcie_of_match[] = {
991 { .compatible = "samsung,exynos5440-pcie", },
992 {},
993};
994MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
995
996static struct platform_driver exynos_pcie_driver = {
997 .remove = __exit_p(exynos_pcie_remove),
998 .driver = {
999 .name = "exynos-pcie",
1000 .owner = THIS_MODULE,
1001 .of_match_table = of_match_ptr(exynos_pcie_of_match),
1002 },
1003};
1004
1005static int exynos_pcie_abort(unsigned long addr, unsigned int fsr,
1006 struct pt_regs *regs)
1007{
1008 unsigned long pc = instruction_pointer(regs);
1009 unsigned long instr = *(unsigned long *)pc;
1010
1011 WARN_ONCE(1, "pcie abort\n");
1012
1013 /*
1014 * If the instruction being executed was a read,
1015 * make it look like it read all-ones.
1016 */
1017 if ((instr & 0x0c100000) == 0x04100000) {
1018 int reg = (instr >> 12) & 15;
1019 unsigned long val;
1020
1021 if (instr & 0x00400000)
1022 val = 255;
1023 else
1024 val = -1;
1025
1026 regs->uregs[reg] = val;
1027 regs->ARM_pc += 4;
1028 return 0;
1029 }
1030
1031 if ((instr & 0x0e100090) == 0x00100090) {
1032 int reg = (instr >> 12) & 15;
1033
1034 regs->uregs[reg] = -1;
1035 regs->ARM_pc += 4;
1036 return 0;
1037 }
1038
1039 return 1;
1040}
1041
1042/* Exynos PCIe driver does not allow module unload */
1043
1044static int __init pcie_init(void)
1045{
1046 hook_fault_code(16 + 6, exynos_pcie_abort, SIGBUS, 0,
1047 "imprecise external abort");
1048
1049 platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
1050
1051 return 0;
1052} 561}
1053subsys_initcall(pcie_init);
1054 562
1055MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>"); 563MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
1056MODULE_DESCRIPTION("Samsung PCIe host controller driver"); 564MODULE_DESCRIPTION("Designware PCIe host controller driver");
1057MODULE_LICENSE("GPL v2"); 565MODULE_LICENSE("GPL v2");