blob: 95cfe8c16fb73383e3755e7071e3892c58488c6f [file] [log] [blame]
Minghuan Lianda419022014-10-31 13:43:44 +08001/*
Minghuan Liane4e8cb72015-01-21 17:29:20 +08002 * Copyright 2014-2015 Freescale Semiconductor, Inc.
Minghuan Lianda419022014-10-31 13:43:44 +08003 * Layerscape PCIe driver
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/arch/fsl_serdes.h>
10#include <pci.h>
11#include <asm/io.h>
Minghuan Liane4e8cb72015-01-21 17:29:20 +080012#include <errno.h>
13#include <malloc.h>
Stuart Yoder70e52d22015-07-02 11:29:04 +053014#include <asm/arch-fsl-lsch3/fdt.h>
Minghuan Lianda419022014-10-31 13:43:44 +080015
Minghuan Liane4e8cb72015-01-21 17:29:20 +080016#ifndef CONFIG_SYS_PCI_MEMORY_BUS
17#define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
18#endif
19
20#ifndef CONFIG_SYS_PCI_MEMORY_PHYS
21#define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
22#endif
23
24#ifndef CONFIG_SYS_PCI_MEMORY_SIZE
25#define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
26#endif
27
Minghuan Lian01cafce2015-07-10 11:35:09 +080028#ifndef CONFIG_SYS_PCI_EP_MEMORY_BASE
29#define CONFIG_SYS_PCI_EP_MEMORY_BASE CONFIG_SYS_LOAD_ADDR
30#endif
31
Minghuan Liane4e8cb72015-01-21 17:29:20 +080032/* iATU registers */
33#define PCIE_ATU_VIEWPORT 0x900
34#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
35#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
36#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
37#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
38#define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
39#define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
40#define PCIE_ATU_CR1 0x904
41#define PCIE_ATU_TYPE_MEM (0x0 << 0)
42#define PCIE_ATU_TYPE_IO (0x2 << 0)
43#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
44#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
45#define PCIE_ATU_CR2 0x908
46#define PCIE_ATU_ENABLE (0x1 << 31)
47#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
Minghuan Lian01cafce2015-07-10 11:35:09 +080048#define PCIE_ATU_BAR_NUM(bar) ((bar) << 8)
Minghuan Liane4e8cb72015-01-21 17:29:20 +080049#define PCIE_ATU_LOWER_BASE 0x90C
50#define PCIE_ATU_UPPER_BASE 0x910
51#define PCIE_ATU_LIMIT 0x914
52#define PCIE_ATU_LOWER_TARGET 0x918
53#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
54#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
55#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
56#define PCIE_ATU_UPPER_TARGET 0x91C
57
Minghuan Lian00704592015-03-12 10:58:49 +080058/* LUT registers */
59#define PCIE_LUT_BASE 0x80000
Minghuan Lian01cafce2015-07-10 11:35:09 +080060#define PCIE_LUT_LCTRL0 0x7F8
Minghuan Lian00704592015-03-12 10:58:49 +080061#define PCIE_LUT_DBG 0x7FC
62
63#define PCIE_DBI_RO_WR_EN 0x8bc
64
Minghuan Liane4e8cb72015-01-21 17:29:20 +080065#define PCIE_LINK_CAP 0x7c
66#define PCIE_LINK_SPEED_MASK 0xf
67#define PCIE_LINK_STA 0x82
68
Minghuan Lian00704592015-03-12 10:58:49 +080069#define LTSSM_STATE_MASK 0x3f
70#define LTSSM_PCIE_L0 0x11 /* L0 state */
71
72#define PCIE_DBI_SIZE 0x100000 /* 1M */
Minghuan Liane4e8cb72015-01-21 17:29:20 +080073
Minghuan Lian01cafce2015-07-10 11:35:09 +080074#define PCIE_LCTRL0_CFG2_ENABLE (1 << 31)
75#define PCIE_LCTRL0_VF(vf) ((vf) << 22)
76#define PCIE_LCTRL0_PF(pf) ((pf) << 16)
77#define PCIE_LCTRL0_VF_ACTIVE (1 << 21)
78#define PCIE_LCTRL0_VAL(pf, vf) (PCIE_LCTRL0_PF(pf) | \
79 PCIE_LCTRL0_VF(vf) | \
80 ((vf) == 0 ? 0 : PCIE_LCTRL0_VF_ACTIVE) | \
81 PCIE_LCTRL0_CFG2_ENABLE)
82
83#define PCIE_NO_SRIOV_BAR_BASE 0x1000
84
85#define PCIE_PF_NUM 2
86#define PCIE_VF_NUM 64
87
88#define PCIE_BAR0_SIZE (4 * 1024) /* 4K */
89#define PCIE_BAR1_SIZE (8 * 1024) /* 8K for MSIX */
90#define PCIE_BAR2_SIZE (4 * 1024) /* 4K */
91#define PCIE_BAR4_SIZE (1 * 1024 * 1024) /* 1M */
92
Minghuan Liane4e8cb72015-01-21 17:29:20 +080093struct ls_pcie {
94 int idx;
95 void __iomem *dbi;
96 void __iomem *va_cfg0;
97 void __iomem *va_cfg1;
98 struct pci_controller hose;
99};
100
101struct ls_pcie_info {
102 unsigned long regs;
103 int pci_num;
Minghuan Lian01cafce2015-07-10 11:35:09 +0800104 u64 phys_base;
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800105 u64 cfg0_phys;
106 u64 cfg0_size;
107 u64 cfg1_phys;
108 u64 cfg1_size;
109 u64 mem_bus;
110 u64 mem_phys;
111 u64 mem_size;
112 u64 io_bus;
113 u64 io_phys;
114 u64 io_size;
115};
116
117#define SET_LS_PCIE_INFO(x, num) \
118{ \
119 x.regs = CONFIG_SYS_PCIE##num##_ADDR; \
Minghuan Lian01cafce2015-07-10 11:35:09 +0800120 x.phys_base = CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800121 x.cfg0_phys = CONFIG_SYS_PCIE_CFG0_PHYS_OFF + \
122 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
123 x.cfg0_size = CONFIG_SYS_PCIE_CFG0_SIZE; \
124 x.cfg1_phys = CONFIG_SYS_PCIE_CFG1_PHYS_OFF + \
125 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
126 x.cfg1_size = CONFIG_SYS_PCIE_CFG1_SIZE; \
127 x.mem_bus = CONFIG_SYS_PCIE_MEM_BUS; \
128 x.mem_phys = CONFIG_SYS_PCIE_MEM_PHYS_OFF + \
129 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
130 x.mem_size = CONFIG_SYS_PCIE_MEM_SIZE; \
131 x.io_bus = CONFIG_SYS_PCIE_IO_BUS; \
132 x.io_phys = CONFIG_SYS_PCIE_IO_PHYS_OFF + \
133 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
134 x.io_size = CONFIG_SYS_PCIE_IO_SIZE; \
135 x.pci_num = num; \
136}
137
138#ifdef CONFIG_LS102XA
139#include <asm/arch/immap_ls102xa.h>
140
141/* PEX1/2 Misc Ports Status Register */
142#define LTSSM_STATE_SHIFT 20
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800143
144static int ls_pcie_link_state(struct ls_pcie *pcie)
145{
146 u32 state;
147 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
148
149 state = in_be32(&scfg->pexmscportsr[pcie->idx]);
150 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
151 if (state < LTSSM_PCIE_L0) {
152 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
153 return 0;
154 }
155
156 return 1;
157}
158#else
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800159static int ls_pcie_link_state(struct ls_pcie *pcie)
160{
161 u32 state;
162
Minghuan Lian00704592015-03-12 10:58:49 +0800163 state = readl(pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_DBG) &
164 LTSSM_STATE_MASK;
165 if (state < LTSSM_PCIE_L0) {
166 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
167 return 0;
168 }
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800169
Minghuan Lian00704592015-03-12 10:58:49 +0800170 return 1;
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800171}
172#endif
173
174static int ls_pcie_link_up(struct ls_pcie *pcie)
175{
176 int state;
177 u32 cap;
178
179 state = ls_pcie_link_state(pcie);
180 if (state)
181 return state;
182
183 /* Try to download speed to gen1 */
184 cap = readl(pcie->dbi + PCIE_LINK_CAP);
185 writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
Minghuan Lian00704592015-03-12 10:58:49 +0800186 /*
187 * Notice: the following delay has critical impact on link training
188 * if too short (<30ms) the link doesn't get up.
189 */
190 mdelay(100);
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800191 state = ls_pcie_link_state(pcie);
192 if (state)
193 return state;
194
195 writel(cap, pcie->dbi + PCIE_LINK_CAP);
196
197 return 0;
198}
199
200static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
201{
202 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
203 pcie->dbi + PCIE_ATU_VIEWPORT);
204 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
205}
206
207static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
208{
209 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
210 pcie->dbi + PCIE_ATU_VIEWPORT);
211 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
212}
213
214static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
215 u64 phys, u64 bus_addr, pci_size_t size)
216{
217 writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
218 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
219 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
220 writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
221 writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
222 writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
223 writel(type, pcie->dbi + PCIE_ATU_CR1);
224 writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
225}
226
Minghuan Lian01cafce2015-07-10 11:35:09 +0800227/* Use bar match mode and MEM type as default */
228static void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
229 int bar, u64 phys)
230{
231 writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
232 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
233 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
234 writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
235 writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
236 PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
237}
238
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800239static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
240{
241#ifdef DEBUG
242 int i;
243#endif
244
245 /* ATU 0 : OUTBOUND : CFG0 */
246 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
247 PCIE_ATU_TYPE_CFG0,
248 info->cfg0_phys,
249 0,
250 info->cfg0_size);
251 /* ATU 1 : OUTBOUND : CFG1 */
252 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
253 PCIE_ATU_TYPE_CFG1,
254 info->cfg1_phys,
255 0,
256 info->cfg1_size);
257 /* ATU 2 : OUTBOUND : MEM */
258 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
259 PCIE_ATU_TYPE_MEM,
260 info->mem_phys,
261 info->mem_bus,
262 info->mem_size);
263 /* ATU 3 : OUTBOUND : IO */
264 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
265 PCIE_ATU_TYPE_IO,
266 info->io_phys,
267 info->io_bus,
268 info->io_size);
269
270#ifdef DEBUG
271 for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
272 writel(PCIE_ATU_REGION_OUTBOUND | i,
273 pcie->dbi + PCIE_ATU_VIEWPORT);
274 debug("iATU%d:\n", i);
275 debug("\tLOWER PHYS 0x%08x\n",
276 readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
277 debug("\tUPPER PHYS 0x%08x\n",
278 readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
279 debug("\tLOWER BUS 0x%08x\n",
280 readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
281 debug("\tUPPER BUS 0x%08x\n",
282 readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
283 debug("\tLIMIT 0x%08x\n",
284 readl(pcie->dbi + PCIE_ATU_LIMIT));
285 debug("\tCR1 0x%08x\n",
286 readl(pcie->dbi + PCIE_ATU_CR1));
287 debug("\tCR2 0x%08x\n",
288 readl(pcie->dbi + PCIE_ATU_CR2));
289 }
290#endif
291}
292
293int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
294{
295 /* Do not skip controller */
296 return 0;
297}
298
299static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
300{
301 if (PCI_DEV(d) > 0)
302 return -EINVAL;
303
Minghuan Lian00704592015-03-12 10:58:49 +0800304 /* Controller does not support multi-function in RC mode */
305 if ((PCI_BUS(d) == hose->first_busno) && (PCI_FUNC(d) > 0))
306 return -EINVAL;
307
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800308 return 0;
309}
310
311static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
312 int where, u32 *val)
313{
314 struct ls_pcie *pcie = hose->priv_data;
315 u32 busdev, *addr;
316
317 if (ls_pcie_addr_valid(hose, d)) {
318 *val = 0xffffffff;
319 return -EINVAL;
320 }
321
322 if (PCI_BUS(d) == hose->first_busno) {
323 addr = pcie->dbi + (where & ~0x3);
324 } else {
325 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
326 PCIE_ATU_DEV(PCI_DEV(d)) |
327 PCIE_ATU_FUNC(PCI_FUNC(d));
328
329 if (PCI_BUS(d) == hose->first_busno + 1) {
330 ls_pcie_cfg0_set_busdev(pcie, busdev);
331 addr = pcie->va_cfg0 + (where & ~0x3);
332 } else {
333 ls_pcie_cfg1_set_busdev(pcie, busdev);
334 addr = pcie->va_cfg1 + (where & ~0x3);
335 }
336 }
337
338 *val = readl(addr);
339
340 return 0;
341}
342
343static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
344 int where, u32 val)
345{
346 struct ls_pcie *pcie = hose->priv_data;
347 u32 busdev, *addr;
348
349 if (ls_pcie_addr_valid(hose, d))
350 return -EINVAL;
351
352 if (PCI_BUS(d) == hose->first_busno) {
353 addr = pcie->dbi + (where & ~0x3);
354 } else {
355 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
356 PCIE_ATU_DEV(PCI_DEV(d)) |
357 PCIE_ATU_FUNC(PCI_FUNC(d));
358
359 if (PCI_BUS(d) == hose->first_busno + 1) {
360 ls_pcie_cfg0_set_busdev(pcie, busdev);
361 addr = pcie->va_cfg0 + (where & ~0x3);
362 } else {
363 ls_pcie_cfg1_set_busdev(pcie, busdev);
364 addr = pcie->va_cfg1 + (where & ~0x3);
365 }
366 }
367
368 writel(val, addr);
369
370 return 0;
371}
372
373static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
374 struct ls_pcie_info *info)
375{
376 struct pci_controller *hose = &pcie->hose;
377 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
378
379 ls_pcie_setup_atu(pcie, info);
380
381 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
382
383 /* program correct class for RC */
Minghuan Lian00704592015-03-12 10:58:49 +0800384 writel(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800385 pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
386 PCI_CLASS_BRIDGE_PCI);
Minghuan Lian00704592015-03-12 10:58:49 +0800387#ifndef CONFIG_LS102XA
388 writel(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
389#endif
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800390}
391
Minghuan Lian01cafce2015-07-10 11:35:09 +0800392static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie,
393 struct ls_pcie_info *info)
394{
395 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
396
397 /* ATU 0 : INBOUND : map BAR0 */
398 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX0, 0, phys);
399 /* ATU 1 : INBOUND : map BAR1 */
400 phys += PCIE_BAR1_SIZE;
401 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX1, 1, phys);
402 /* ATU 2 : INBOUND : map BAR2 */
403 phys += PCIE_BAR2_SIZE;
404 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX2, 2, phys);
405 /* ATU 3 : INBOUND : map BAR4 */
406 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
407 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX3, 4, phys);
408
409 /* ATU 0 : OUTBOUND : map 4G MEM */
410 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
411 PCIE_ATU_TYPE_MEM,
412 info->phys_base,
413 0,
414 4 * 1024 * 1024 * 1024ULL);
415}
416
417/* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
418static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
419{
420 if (size < 4 * 1024)
421 return;
422
423 switch (bar) {
424 case 0:
425 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
426 break;
427 case 1:
428 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
429 break;
430 case 2:
431 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
432 writel(0, bar_base + PCI_BASE_ADDRESS_3);
433 break;
434 case 4:
435 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
436 writel(0, bar_base + PCI_BASE_ADDRESS_5);
437 break;
438 default:
439 break;
440 }
441}
442
443static void ls_pcie_ep_setup_bars(void *bar_base)
444{
445 /* BAR0 - 32bit - 4K configuration */
446 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
447 /* BAR1 - 32bit - 8K MSIX*/
448 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
449 /* BAR2 - 64bit - 4K MEM desciptor */
450 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
451 /* BAR4 - 64bit - 1M MEM*/
452 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
453}
454
455static void ls_pcie_setup_ep(struct ls_pcie *pcie, struct ls_pcie_info *info)
456{
457 struct pci_controller *hose = &pcie->hose;
458 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
459 int sriov;
460
461 sriov = pci_hose_find_ext_capability(hose, dev, PCI_EXT_CAP_ID_SRIOV);
462 if (sriov) {
463 int pf, vf;
464
465 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
466 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
467 writel(PCIE_LCTRL0_VAL(pf, vf),
468 pcie->dbi + PCIE_LUT_BASE +
469 PCIE_LUT_LCTRL0);
470 ls_pcie_ep_setup_bars(pcie->dbi);
471 ls_pcie_ep_setup_atu(pcie, info);
472 }
473 }
474
475 /* Disable CFG2 */
476 writel(0, pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
477 } else {
478 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
479 ls_pcie_ep_setup_atu(pcie, info);
480 }
481}
482
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800483int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
484{
485 struct ls_pcie *pcie;
486 struct pci_controller *hose;
487 int num = dev - PCIE1;
488 pci_dev_t pdev = PCI_BDF(busno, 0, 0);
489 int i, linkup, ep_mode;
490 u8 header_type;
491 u16 temp16;
492
493 if (!is_serdes_configured(dev)) {
494 printf("PCIe%d: disabled\n", num + 1);
495 return busno;
496 }
497
498 pcie = malloc(sizeof(*pcie));
499 if (!pcie)
500 return busno;
501 memset(pcie, 0, sizeof(*pcie));
502
503 hose = &pcie->hose;
504 hose->priv_data = pcie;
505 hose->first_busno = busno;
506 pcie->idx = num;
507 pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
508 pcie->va_cfg0 = map_physmem(info->cfg0_phys,
509 info->cfg0_size,
510 MAP_NOCACHE);
511 pcie->va_cfg1 = map_physmem(info->cfg1_phys,
512 info->cfg1_size,
513 MAP_NOCACHE);
514
515 /* outbound memory */
516 pci_set_region(&hose->regions[0],
517 (pci_size_t)info->mem_bus,
518 (phys_size_t)info->mem_phys,
519 (pci_size_t)info->mem_size,
520 PCI_REGION_MEM);
521
522 /* outbound io */
523 pci_set_region(&hose->regions[1],
524 (pci_size_t)info->io_bus,
525 (phys_size_t)info->io_phys,
526 (pci_size_t)info->io_size,
527 PCI_REGION_IO);
528
529 /* System memory space */
530 pci_set_region(&hose->regions[2],
531 CONFIG_SYS_PCI_MEMORY_BUS,
532 CONFIG_SYS_PCI_MEMORY_PHYS,
533 CONFIG_SYS_PCI_MEMORY_SIZE,
534 PCI_REGION_SYS_MEMORY);
535
536 hose->region_count = 3;
537
538 for (i = 0; i < hose->region_count; i++)
539 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
540 i,
541 (u64)hose->regions[i].phys_start,
542 (u64)hose->regions[i].bus_start,
543 (u64)hose->regions[i].size,
544 hose->regions[i].flags);
545
546 pci_set_ops(hose,
547 pci_hose_read_config_byte_via_dword,
548 pci_hose_read_config_word_via_dword,
549 ls_pcie_read_config,
550 pci_hose_write_config_byte_via_dword,
551 pci_hose_write_config_word_via_dword,
552 ls_pcie_write_config);
553
554 pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
555 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
556 printf("PCIe%u: %s ", info->pci_num,
557 ep_mode ? "Endpoint" : "Root Complex");
558
Minghuan Lian01cafce2015-07-10 11:35:09 +0800559 if (ep_mode)
560 ls_pcie_setup_ep(pcie, info);
561 else
562 ls_pcie_setup_ctrl(pcie, info);
563
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800564 linkup = ls_pcie_link_up(pcie);
565
566 if (!linkup) {
567 /* Let the user know there's no PCIe link */
568 printf("no link, regs @ 0x%lx\n", info->regs);
569 hose->last_busno = hose->first_busno;
570 return busno;
571 }
572
573 /* Print the negotiated PCIe link width */
Minghuan Lian00704592015-03-12 10:58:49 +0800574 pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
575 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
576 (temp16 & 0xf), info->regs);
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800577
578 if (ep_mode)
579 return busno;
580
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800581 pci_register_hose(hose);
582
583 hose->last_busno = pci_hose_scan(hose);
584
585 printf("PCIe%x: Bus %02x - %02x\n",
586 info->pci_num, hose->first_busno, hose->last_busno);
587
588 return hose->last_busno + 1;
589}
590
591int ls_pcie_init_board(int busno)
592{
593 struct ls_pcie_info info;
594
595#ifdef CONFIG_PCIE1
596 SET_LS_PCIE_INFO(info, 1);
597 busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
598#endif
599
600#ifdef CONFIG_PCIE2
601 SET_LS_PCIE_INFO(info, 2);
602 busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
603#endif
604
605#ifdef CONFIG_PCIE3
606 SET_LS_PCIE_INFO(info, 3);
607 busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
608#endif
609
610#ifdef CONFIG_PCIE4
611 SET_LS_PCIE_INFO(info, 4);
612 busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
613#endif
614
615 return busno;
616}
617
618void pci_init_board(void)
619{
620 ls_pcie_init_board(0);
621}
622
Minghuan Lianda419022014-10-31 13:43:44 +0800623#ifdef CONFIG_OF_BOARD_SETUP
624#include <libfdt.h>
625#include <fdt_support.h>
626
627static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
628 unsigned long ctrl_addr, enum srds_prtcl dev)
629{
630 int off;
631
632 off = fdt_node_offset_by_compat_reg(blob, pci_compat,
633 (phys_addr_t)ctrl_addr);
634 if (off < 0)
635 return;
636
637 if (!is_serdes_configured(dev))
638 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
639}
640
Minghuan Liand42bd342015-03-12 10:58:48 +0800641void ft_pci_setup(void *blob, bd_t *bd)
Minghuan Lianda419022014-10-31 13:43:44 +0800642{
643 #ifdef CONFIG_PCIE1
644 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
645 #endif
646
647 #ifdef CONFIG_PCIE2
648 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
649 #endif
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800650
651 #ifdef CONFIG_PCIE3
652 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
653 #endif
654
655 #ifdef CONFIG_PCIE4
656 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
657 #endif
Minghuan Lianda419022014-10-31 13:43:44 +0800658}
659
660#else
Minghuan Liand42bd342015-03-12 10:58:48 +0800661void ft_pci_setup(void *blob, bd_t *bd)
Minghuan Lianda419022014-10-31 13:43:44 +0800662{
663}
664#endif
Stuart Yoder70e52d22015-07-02 11:29:04 +0530665
666#ifdef CONFIG_LS2085A
667
668void pcie_set_available_streamids(void *blob, const char *pcie_path,
669 u32 *stream_ids, int count)
670{
671 int nodeoffset;
672 int i;
673
674 nodeoffset = fdt_path_offset(blob, pcie_path);
675 if (nodeoffset < 0) {
676 printf("\n%s: ERROR: unable to update PCIe node\n", __func__);
677 return;
678 }
679
680 /* for each stream ID, append to mmu-masters */
681 for (i = 0; i < count; i++) {
682 fdt_appendprop_u32(blob, nodeoffset, "available-stream-ids",
683 stream_ids[i]);
684 }
685}
686
687#define MAX_STREAM_IDS 4
688void fdt_fixup_smmu_pcie(void *blob)
689{
690 int count;
691 u32 stream_ids[MAX_STREAM_IDS];
692
693 #ifdef CONFIG_PCIE1
694 /* PEX1 stream ID fixup */
695 count = FSL_PEX1_STREAM_ID_END - FSL_PEX1_STREAM_ID_START + 1;
696 alloc_stream_ids(FSL_PEX1_STREAM_ID_START, count, stream_ids,
697 MAX_STREAM_IDS);
698 pcie_set_available_streamids(blob, "/pcie@3400000", stream_ids, count);
699 #endif
700
701 #ifdef CONFIG_PCIE2
702 /* PEX2 stream ID fixup */
703 count = FSL_PEX2_STREAM_ID_END - FSL_PEX2_STREAM_ID_START + 1;
704 alloc_stream_ids(FSL_PEX2_STREAM_ID_START, count, stream_ids,
705 MAX_STREAM_IDS);
706 pcie_set_available_streamids(blob, "/pcie@3500000", stream_ids, count);
707 #endif
708
709 #ifdef CONFIG_PCIE3
710 /* PEX3 stream ID fixup */
711 count = FSL_PEX3_STREAM_ID_END - FSL_PEX3_STREAM_ID_START + 1;
712 alloc_stream_ids(FSL_PEX3_STREAM_ID_START, count, stream_ids,
713 MAX_STREAM_IDS);
714 pcie_set_available_streamids(blob, "/pcie@3600000", stream_ids, count);
715 #endif
716
717 #ifdef CONFIG_PCIE4
718 /* PEX4 stream ID fixup */
719 count = FSL_PEX4_STREAM_ID_END - FSL_PEX4_STREAM_ID_START + 1;
720 alloc_stream_ids(FSL_PEX4_STREAM_ID_START, count, stream_ids,
721 MAX_STREAM_IDS);
722 pcie_set_available_streamids(blob, "/pcie@3700000", stream_ids, count);
723 #endif
724}
725#endif