blob: 2e6b986dbce169a997984c37601b7ebce5a731e6 [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>
Mingkai Huaf523a02015-11-11 17:58:34 +080014#ifndef CONFIG_LS102XA
Mingkai Hu9f3183d2015-10-26 19:47:50 +080015#include <asm/arch/fdt.h>
Mingkai Huaf523a02015-11-11 17:58:34 +080016#include <asm/arch/soc.h>
Mingkai Hu9f3183d2015-10-26 19:47:50 +080017#endif
Minghuan Lianda419022014-10-31 13:43:44 +080018
Minghuan Liane4e8cb72015-01-21 17:29:20 +080019#ifndef CONFIG_SYS_PCI_MEMORY_BUS
20#define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
21#endif
22
23#ifndef CONFIG_SYS_PCI_MEMORY_PHYS
24#define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
25#endif
26
27#ifndef CONFIG_SYS_PCI_MEMORY_SIZE
28#define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
29#endif
30
Minghuan Lian01cafce2015-07-10 11:35:09 +080031#ifndef CONFIG_SYS_PCI_EP_MEMORY_BASE
32#define CONFIG_SYS_PCI_EP_MEMORY_BASE CONFIG_SYS_LOAD_ADDR
33#endif
34
Minghuan Liane4e8cb72015-01-21 17:29:20 +080035/* iATU registers */
36#define PCIE_ATU_VIEWPORT 0x900
37#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
38#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
39#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
40#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
41#define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
42#define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
43#define PCIE_ATU_CR1 0x904
44#define PCIE_ATU_TYPE_MEM (0x0 << 0)
45#define PCIE_ATU_TYPE_IO (0x2 << 0)
46#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
47#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
48#define PCIE_ATU_CR2 0x908
49#define PCIE_ATU_ENABLE (0x1 << 31)
50#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
Minghuan Lian01cafce2015-07-10 11:35:09 +080051#define PCIE_ATU_BAR_NUM(bar) ((bar) << 8)
Minghuan Liane4e8cb72015-01-21 17:29:20 +080052#define PCIE_ATU_LOWER_BASE 0x90C
53#define PCIE_ATU_UPPER_BASE 0x910
54#define PCIE_ATU_LIMIT 0x914
55#define PCIE_ATU_LOWER_TARGET 0x918
56#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
57#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
58#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
59#define PCIE_ATU_UPPER_TARGET 0x91C
60
Minghuan Lian00704592015-03-12 10:58:49 +080061#define PCIE_DBI_RO_WR_EN 0x8bc
62
Minghuan Liane4e8cb72015-01-21 17:29:20 +080063#define PCIE_LINK_CAP 0x7c
64#define PCIE_LINK_SPEED_MASK 0xf
65#define PCIE_LINK_STA 0x82
66
Minghuan Lian00704592015-03-12 10:58:49 +080067#define LTSSM_STATE_MASK 0x3f
68#define LTSSM_PCIE_L0 0x11 /* L0 state */
69
70#define PCIE_DBI_SIZE 0x100000 /* 1M */
Minghuan Liane4e8cb72015-01-21 17:29:20 +080071
Minghuan Lian01cafce2015-07-10 11:35:09 +080072#define PCIE_LCTRL0_CFG2_ENABLE (1 << 31)
73#define PCIE_LCTRL0_VF(vf) ((vf) << 22)
74#define PCIE_LCTRL0_PF(pf) ((pf) << 16)
75#define PCIE_LCTRL0_VF_ACTIVE (1 << 21)
76#define PCIE_LCTRL0_VAL(pf, vf) (PCIE_LCTRL0_PF(pf) | \
77 PCIE_LCTRL0_VF(vf) | \
78 ((vf) == 0 ? 0 : PCIE_LCTRL0_VF_ACTIVE) | \
79 PCIE_LCTRL0_CFG2_ENABLE)
80
81#define PCIE_NO_SRIOV_BAR_BASE 0x1000
82
83#define PCIE_PF_NUM 2
84#define PCIE_VF_NUM 64
85
86#define PCIE_BAR0_SIZE (4 * 1024) /* 4K */
87#define PCIE_BAR1_SIZE (8 * 1024) /* 8K for MSIX */
88#define PCIE_BAR2_SIZE (4 * 1024) /* 4K */
89#define PCIE_BAR4_SIZE (1 * 1024 * 1024) /* 1M */
90
Minghuan Liane4e8cb72015-01-21 17:29:20 +080091struct ls_pcie {
92 int idx;
93 void __iomem *dbi;
94 void __iomem *va_cfg0;
95 void __iomem *va_cfg1;
Stuart Yoder5e8e27b2016-03-10 10:52:30 -060096 int next_lut_index;
Minghuan Liane4e8cb72015-01-21 17:29:20 +080097 struct pci_controller hose;
98};
99
100struct ls_pcie_info {
101 unsigned long regs;
102 int pci_num;
Minghuan Lian01cafce2015-07-10 11:35:09 +0800103 u64 phys_base;
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800104 u64 cfg0_phys;
105 u64 cfg0_size;
106 u64 cfg1_phys;
107 u64 cfg1_size;
108 u64 mem_bus;
109 u64 mem_phys;
110 u64 mem_size;
111 u64 io_bus;
112 u64 io_phys;
113 u64 io_size;
114};
115
116#define SET_LS_PCIE_INFO(x, num) \
117{ \
118 x.regs = CONFIG_SYS_PCIE##num##_ADDR; \
Minghuan Lian01cafce2015-07-10 11:35:09 +0800119 x.phys_base = CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800120 x.cfg0_phys = CONFIG_SYS_PCIE_CFG0_PHYS_OFF + \
121 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
122 x.cfg0_size = CONFIG_SYS_PCIE_CFG0_SIZE; \
123 x.cfg1_phys = CONFIG_SYS_PCIE_CFG1_PHYS_OFF + \
124 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
125 x.cfg1_size = CONFIG_SYS_PCIE_CFG1_SIZE; \
126 x.mem_bus = CONFIG_SYS_PCIE_MEM_BUS; \
127 x.mem_phys = CONFIG_SYS_PCIE_MEM_PHYS_OFF + \
128 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
129 x.mem_size = CONFIG_SYS_PCIE_MEM_SIZE; \
130 x.io_bus = CONFIG_SYS_PCIE_IO_BUS; \
131 x.io_phys = CONFIG_SYS_PCIE_IO_PHYS_OFF + \
132 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
133 x.io_size = CONFIG_SYS_PCIE_IO_SIZE; \
134 x.pci_num = num; \
135}
136
137#ifdef CONFIG_LS102XA
138#include <asm/arch/immap_ls102xa.h>
139
140/* PEX1/2 Misc Ports Status Register */
141#define LTSSM_STATE_SHIFT 20
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800142
143static int ls_pcie_link_state(struct ls_pcie *pcie)
144{
145 u32 state;
146 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
147
148 state = in_be32(&scfg->pexmscportsr[pcie->idx]);
149 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
150 if (state < LTSSM_PCIE_L0) {
151 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
152 return 0;
153 }
154
155 return 1;
156}
157#else
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800158static int ls_pcie_link_state(struct ls_pcie *pcie)
159{
160 u32 state;
161
Mingkai Huaf523a02015-11-11 17:58:34 +0800162 state = pex_lut_in32(pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_DBG) &
Minghuan Lian00704592015-03-12 10:58:49 +0800163 LTSSM_STATE_MASK;
164 if (state < LTSSM_PCIE_L0) {
165 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
166 return 0;
167 }
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800168
Minghuan Lian00704592015-03-12 10:58:49 +0800169 return 1;
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800170}
171#endif
172
173static int ls_pcie_link_up(struct ls_pcie *pcie)
174{
175 int state;
176 u32 cap;
177
178 state = ls_pcie_link_state(pcie);
179 if (state)
180 return state;
181
182 /* Try to download speed to gen1 */
183 cap = readl(pcie->dbi + PCIE_LINK_CAP);
184 writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
Minghuan Lian00704592015-03-12 10:58:49 +0800185 /*
186 * Notice: the following delay has critical impact on link training
187 * if too short (<30ms) the link doesn't get up.
188 */
189 mdelay(100);
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800190 state = ls_pcie_link_state(pcie);
191 if (state)
192 return state;
193
194 writel(cap, pcie->dbi + PCIE_LINK_CAP);
195
196 return 0;
197}
198
199static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
200{
201 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
202 pcie->dbi + PCIE_ATU_VIEWPORT);
203 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
204}
205
206static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
207{
208 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
209 pcie->dbi + PCIE_ATU_VIEWPORT);
210 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
211}
212
213static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
214 u64 phys, u64 bus_addr, pci_size_t size)
215{
216 writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
217 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
218 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
219 writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
220 writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
221 writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
222 writel(type, pcie->dbi + PCIE_ATU_CR1);
223 writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
224}
225
Minghuan Lian01cafce2015-07-10 11:35:09 +0800226/* Use bar match mode and MEM type as default */
227static void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
228 int bar, u64 phys)
229{
230 writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
231 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
232 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
233 writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
234 writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
235 PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
236}
237
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800238static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
239{
240#ifdef DEBUG
241 int i;
242#endif
243
244 /* ATU 0 : OUTBOUND : CFG0 */
245 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
246 PCIE_ATU_TYPE_CFG0,
247 info->cfg0_phys,
248 0,
249 info->cfg0_size);
250 /* ATU 1 : OUTBOUND : CFG1 */
251 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
252 PCIE_ATU_TYPE_CFG1,
253 info->cfg1_phys,
254 0,
255 info->cfg1_size);
256 /* ATU 2 : OUTBOUND : MEM */
257 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
258 PCIE_ATU_TYPE_MEM,
259 info->mem_phys,
260 info->mem_bus,
261 info->mem_size);
262 /* ATU 3 : OUTBOUND : IO */
263 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
264 PCIE_ATU_TYPE_IO,
265 info->io_phys,
266 info->io_bus,
267 info->io_size);
268
269#ifdef DEBUG
270 for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
271 writel(PCIE_ATU_REGION_OUTBOUND | i,
272 pcie->dbi + PCIE_ATU_VIEWPORT);
273 debug("iATU%d:\n", i);
274 debug("\tLOWER PHYS 0x%08x\n",
275 readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
276 debug("\tUPPER PHYS 0x%08x\n",
277 readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
278 debug("\tLOWER BUS 0x%08x\n",
279 readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
280 debug("\tUPPER BUS 0x%08x\n",
281 readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
282 debug("\tLIMIT 0x%08x\n",
283 readl(pcie->dbi + PCIE_ATU_LIMIT));
284 debug("\tCR1 0x%08x\n",
285 readl(pcie->dbi + PCIE_ATU_CR1));
286 debug("\tCR2 0x%08x\n",
287 readl(pcie->dbi + PCIE_ATU_CR2));
288 }
289#endif
290}
291
292int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
293{
294 /* Do not skip controller */
295 return 0;
296}
297
298static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
299{
300 if (PCI_DEV(d) > 0)
301 return -EINVAL;
302
Minghuan Lian00704592015-03-12 10:58:49 +0800303 /* Controller does not support multi-function in RC mode */
304 if ((PCI_BUS(d) == hose->first_busno) && (PCI_FUNC(d) > 0))
305 return -EINVAL;
306
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800307 return 0;
308}
309
310static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
311 int where, u32 *val)
312{
313 struct ls_pcie *pcie = hose->priv_data;
314 u32 busdev, *addr;
315
316 if (ls_pcie_addr_valid(hose, d)) {
317 *val = 0xffffffff;
Bin Meng7ba34ff2016-01-08 01:03:21 -0800318 return 0;
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800319 }
320
321 if (PCI_BUS(d) == hose->first_busno) {
322 addr = pcie->dbi + (where & ~0x3);
323 } else {
324 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
325 PCIE_ATU_DEV(PCI_DEV(d)) |
326 PCIE_ATU_FUNC(PCI_FUNC(d));
327
328 if (PCI_BUS(d) == hose->first_busno + 1) {
329 ls_pcie_cfg0_set_busdev(pcie, busdev);
330 addr = pcie->va_cfg0 + (where & ~0x3);
331 } else {
332 ls_pcie_cfg1_set_busdev(pcie, busdev);
333 addr = pcie->va_cfg1 + (where & ~0x3);
334 }
335 }
336
337 *val = readl(addr);
338
339 return 0;
340}
341
342static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
343 int where, u32 val)
344{
345 struct ls_pcie *pcie = hose->priv_data;
346 u32 busdev, *addr;
347
348 if (ls_pcie_addr_valid(hose, d))
349 return -EINVAL;
350
351 if (PCI_BUS(d) == hose->first_busno) {
352 addr = pcie->dbi + (where & ~0x3);
353 } else {
354 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
355 PCIE_ATU_DEV(PCI_DEV(d)) |
356 PCIE_ATU_FUNC(PCI_FUNC(d));
357
358 if (PCI_BUS(d) == hose->first_busno + 1) {
359 ls_pcie_cfg0_set_busdev(pcie, busdev);
360 addr = pcie->va_cfg0 + (where & ~0x3);
361 } else {
362 ls_pcie_cfg1_set_busdev(pcie, busdev);
363 addr = pcie->va_cfg1 + (where & ~0x3);
364 }
365 }
366
367 writel(val, addr);
368
369 return 0;
370}
371
372static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
373 struct ls_pcie_info *info)
374{
375 struct pci_controller *hose = &pcie->hose;
376 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
377
378 ls_pcie_setup_atu(pcie, info);
379
380 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
381
382 /* program correct class for RC */
Minghuan Lian00704592015-03-12 10:58:49 +0800383 writel(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800384 pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
385 PCI_CLASS_BRIDGE_PCI);
Minghuan Lian00704592015-03-12 10:58:49 +0800386#ifndef CONFIG_LS102XA
387 writel(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
388#endif
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800389}
390
Minghuan Lian01cafce2015-07-10 11:35:09 +0800391static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie,
392 struct ls_pcie_info *info)
393{
394 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
395
396 /* ATU 0 : INBOUND : map BAR0 */
397 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX0, 0, phys);
398 /* ATU 1 : INBOUND : map BAR1 */
399 phys += PCIE_BAR1_SIZE;
400 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX1, 1, phys);
401 /* ATU 2 : INBOUND : map BAR2 */
402 phys += PCIE_BAR2_SIZE;
403 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX2, 2, phys);
404 /* ATU 3 : INBOUND : map BAR4 */
405 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
406 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX3, 4, phys);
407
408 /* ATU 0 : OUTBOUND : map 4G MEM */
409 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
410 PCIE_ATU_TYPE_MEM,
411 info->phys_base,
412 0,
413 4 * 1024 * 1024 * 1024ULL);
414}
415
416/* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
417static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
418{
419 if (size < 4 * 1024)
420 return;
421
422 switch (bar) {
423 case 0:
424 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
425 break;
426 case 1:
427 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
428 break;
429 case 2:
430 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
431 writel(0, bar_base + PCI_BASE_ADDRESS_3);
432 break;
433 case 4:
434 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
435 writel(0, bar_base + PCI_BASE_ADDRESS_5);
436 break;
437 default:
438 break;
439 }
440}
441
442static void ls_pcie_ep_setup_bars(void *bar_base)
443{
444 /* BAR0 - 32bit - 4K configuration */
445 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
446 /* BAR1 - 32bit - 8K MSIX*/
447 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
448 /* BAR2 - 64bit - 4K MEM desciptor */
449 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
450 /* BAR4 - 64bit - 1M MEM*/
451 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
452}
453
454static void ls_pcie_setup_ep(struct ls_pcie *pcie, struct ls_pcie_info *info)
455{
456 struct pci_controller *hose = &pcie->hose;
457 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
458 int sriov;
459
460 sriov = pci_hose_find_ext_capability(hose, dev, PCI_EXT_CAP_ID_SRIOV);
461 if (sriov) {
462 int pf, vf;
463
464 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
465 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
Mingkai Huaf523a02015-11-11 17:58:34 +0800466#ifndef CONFIG_LS102XA
Minghuan Lian01cafce2015-07-10 11:35:09 +0800467 writel(PCIE_LCTRL0_VAL(pf, vf),
468 pcie->dbi + PCIE_LUT_BASE +
469 PCIE_LUT_LCTRL0);
Mingkai Huaf523a02015-11-11 17:58:34 +0800470#endif
Minghuan Lian01cafce2015-07-10 11:35:09 +0800471 ls_pcie_ep_setup_bars(pcie->dbi);
472 ls_pcie_ep_setup_atu(pcie, info);
473 }
474 }
475
476 /* Disable CFG2 */
Mingkai Huaf523a02015-11-11 17:58:34 +0800477#ifndef CONFIG_LS102XA
Minghuan Lian01cafce2015-07-10 11:35:09 +0800478 writel(0, pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
Mingkai Huaf523a02015-11-11 17:58:34 +0800479#endif
Minghuan Lian01cafce2015-07-10 11:35:09 +0800480 } else {
481 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
482 ls_pcie_ep_setup_atu(pcie, info);
483 }
484}
485
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600486#ifdef CONFIG_FSL_LSCH3
487/*
488 * Return next available LUT index.
489 */
490static int ls_pcie_next_lut_index(struct ls_pcie *pcie)
491{
492 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
493 return pcie->next_lut_index++;
494 else
495 return -1; /* LUT is full */
496}
497
498/*
499 * Program a single LUT entry
500 */
501static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid,
502 u32 streamid)
503{
504 void __iomem *lut;
505
506 lut = pcie->dbi + PCIE_LUT_BASE;
507
508 /* leave mask as all zeroes, want to match all bits */
509 writel((devid << 16), lut + PCIE_LUT_UDR(index));
510 writel(streamid | PCIE_LUT_ENABLE, lut + PCIE_LUT_LDR(index));
511}
512
513/* returns the next available streamid */
514static u32 ls_pcie_next_streamid(void)
515{
516 static int next_stream_id = FSL_PEX_STREAM_ID_START;
517
518 if (next_stream_id > FSL_PEX_STREAM_ID_END)
519 return 0xffffffff;
520
521 return next_stream_id++;
522}
523
524/*
525 * An msi-map is a property to be added to the pci controller
526 * node. It is a table, where each entry consists of 4 fields
527 * e.g.:
528 *
529 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
530 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
531 */
532static void fdt_pcie_set_msi_map_entry(void *blob, struct ls_pcie *pcie,
533 u32 devid, u32 streamid)
534{
535 char pcie_path[19];
536 u32 *prop;
537 u32 phandle;
538 int nodeoffset;
539
540 /* find pci controller node */
541 snprintf(pcie_path, sizeof(pcie_path), "/soc/pcie@%llx",
542 (u64)pcie->dbi);
543 nodeoffset = fdt_path_offset(blob, pcie_path);
544 if (nodeoffset < 0) {
545 printf("\n%s: ERROR: unable to update PCIe node: %s\n",
546 __func__, pcie_path);
547 return;
548 }
549
550 /* get phandle to MSI controller */
551 prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0);
552 if (prop == NULL) {
553 printf("\n%s: ERROR: missing msi-parent: %s\n", __func__,
554 pcie_path);
555 return;
556 }
557 phandle = be32_to_cpu(*prop);
558
559 /* set one msi-map row */
560 fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid);
561 fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle);
562 fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid);
563 fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1);
564}
565
566static void fdt_fixup_pcie(void *blob)
567{
568 unsigned int found_multi = 0;
569 unsigned char header_type;
570 int index;
571 u32 streamid;
Bogdan Purcareata33787272016-05-17 07:18:40 +0000572 pci_dev_t dev, bdf;
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600573 int bus;
574 unsigned short id;
575 struct pci_controller *hose;
576 struct ls_pcie *pcie;
577 int i;
578
579 for (i = 0, hose = pci_get_hose_head(); hose; hose = hose->next, i++) {
580 pcie = hose->priv_data;
581 for (bus = hose->first_busno; bus <= hose->last_busno; bus++) {
582
583 for (dev = PCI_BDF(bus, 0, 0);
584 dev < PCI_BDF(bus, PCI_MAX_PCI_DEVICES - 1,
585 PCI_MAX_PCI_FUNCTIONS - 1);
586 dev += PCI_BDF(0, 0, 1)) {
587
588 if (PCI_FUNC(dev) && !found_multi)
589 continue;
590
591 pci_read_config_word(dev, PCI_VENDOR_ID, &id);
592
593 pci_read_config_byte(dev, PCI_HEADER_TYPE,
594 &header_type);
595
596 if ((id == 0xFFFF) || (id == 0x0000))
597 continue;
598
599 if (!PCI_FUNC(dev))
600 found_multi = header_type & 0x80;
601
602 streamid = ls_pcie_next_streamid();
603 if (streamid == 0xffffffff) {
604 printf("ERROR: no stream ids free\n");
605 continue;
606 }
607
608 index = ls_pcie_next_lut_index(pcie);
609 if (index < 0) {
610 printf("ERROR: no LUT indexes free\n");
611 continue;
612 }
613
Bogdan Purcareata33787272016-05-17 07:18:40 +0000614 /* the DT fixup must be relative to the hose first_busno */
615 bdf = dev - PCI_BDF(hose->first_busno, 0, 0);
616
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600617 /* map PCI b.d.f to streamID in LUT */
Bogdan Purcareata33787272016-05-17 07:18:40 +0000618 ls_pcie_lut_set_mapping(pcie, index, bdf >> 8,
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600619 streamid);
620
621 /* update msi-map in device tree */
Bogdan Purcareata33787272016-05-17 07:18:40 +0000622 fdt_pcie_set_msi_map_entry(blob, pcie, bdf >> 8,
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600623 streamid);
624 }
625 }
626 }
627}
628#endif
629
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800630int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
631{
632 struct ls_pcie *pcie;
633 struct pci_controller *hose;
634 int num = dev - PCIE1;
635 pci_dev_t pdev = PCI_BDF(busno, 0, 0);
636 int i, linkup, ep_mode;
637 u8 header_type;
638 u16 temp16;
639
640 if (!is_serdes_configured(dev)) {
641 printf("PCIe%d: disabled\n", num + 1);
642 return busno;
643 }
644
645 pcie = malloc(sizeof(*pcie));
646 if (!pcie)
647 return busno;
648 memset(pcie, 0, sizeof(*pcie));
649
650 hose = &pcie->hose;
651 hose->priv_data = pcie;
652 hose->first_busno = busno;
653 pcie->idx = num;
654 pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
655 pcie->va_cfg0 = map_physmem(info->cfg0_phys,
656 info->cfg0_size,
657 MAP_NOCACHE);
658 pcie->va_cfg1 = map_physmem(info->cfg1_phys,
659 info->cfg1_size,
660 MAP_NOCACHE);
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600661 pcie->next_lut_index = 0;
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800662
663 /* outbound memory */
664 pci_set_region(&hose->regions[0],
665 (pci_size_t)info->mem_bus,
666 (phys_size_t)info->mem_phys,
667 (pci_size_t)info->mem_size,
668 PCI_REGION_MEM);
669
670 /* outbound io */
671 pci_set_region(&hose->regions[1],
672 (pci_size_t)info->io_bus,
673 (phys_size_t)info->io_phys,
674 (pci_size_t)info->io_size,
675 PCI_REGION_IO);
676
677 /* System memory space */
678 pci_set_region(&hose->regions[2],
679 CONFIG_SYS_PCI_MEMORY_BUS,
680 CONFIG_SYS_PCI_MEMORY_PHYS,
681 CONFIG_SYS_PCI_MEMORY_SIZE,
682 PCI_REGION_SYS_MEMORY);
683
684 hose->region_count = 3;
685
686 for (i = 0; i < hose->region_count; i++)
687 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
688 i,
689 (u64)hose->regions[i].phys_start,
690 (u64)hose->regions[i].bus_start,
691 (u64)hose->regions[i].size,
692 hose->regions[i].flags);
693
694 pci_set_ops(hose,
695 pci_hose_read_config_byte_via_dword,
696 pci_hose_read_config_word_via_dword,
697 ls_pcie_read_config,
698 pci_hose_write_config_byte_via_dword,
699 pci_hose_write_config_word_via_dword,
700 ls_pcie_write_config);
701
702 pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
703 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
704 printf("PCIe%u: %s ", info->pci_num,
705 ep_mode ? "Endpoint" : "Root Complex");
706
Minghuan Lian01cafce2015-07-10 11:35:09 +0800707 if (ep_mode)
708 ls_pcie_setup_ep(pcie, info);
709 else
710 ls_pcie_setup_ctrl(pcie, info);
711
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800712 linkup = ls_pcie_link_up(pcie);
713
714 if (!linkup) {
715 /* Let the user know there's no PCIe link */
716 printf("no link, regs @ 0x%lx\n", info->regs);
717 hose->last_busno = hose->first_busno;
718 return busno;
719 }
720
721 /* Print the negotiated PCIe link width */
Minghuan Lian00704592015-03-12 10:58:49 +0800722 pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
723 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
724 (temp16 & 0xf), info->regs);
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800725
726 if (ep_mode)
727 return busno;
728
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800729 pci_register_hose(hose);
730
731 hose->last_busno = pci_hose_scan(hose);
732
733 printf("PCIe%x: Bus %02x - %02x\n",
734 info->pci_num, hose->first_busno, hose->last_busno);
735
736 return hose->last_busno + 1;
737}
738
739int ls_pcie_init_board(int busno)
740{
741 struct ls_pcie_info info;
742
743#ifdef CONFIG_PCIE1
744 SET_LS_PCIE_INFO(info, 1);
745 busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
746#endif
747
748#ifdef CONFIG_PCIE2
749 SET_LS_PCIE_INFO(info, 2);
750 busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
751#endif
752
753#ifdef CONFIG_PCIE3
754 SET_LS_PCIE_INFO(info, 3);
755 busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
756#endif
757
758#ifdef CONFIG_PCIE4
759 SET_LS_PCIE_INFO(info, 4);
760 busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
761#endif
762
763 return busno;
764}
765
766void pci_init_board(void)
767{
768 ls_pcie_init_board(0);
769}
770
Minghuan Lianda419022014-10-31 13:43:44 +0800771#ifdef CONFIG_OF_BOARD_SETUP
772#include <libfdt.h>
773#include <fdt_support.h>
774
775static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
776 unsigned long ctrl_addr, enum srds_prtcl dev)
777{
778 int off;
779
780 off = fdt_node_offset_by_compat_reg(blob, pci_compat,
781 (phys_addr_t)ctrl_addr);
782 if (off < 0)
783 return;
784
785 if (!is_serdes_configured(dev))
786 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
787}
788
Minghuan Liand42bd342015-03-12 10:58:48 +0800789void ft_pci_setup(void *blob, bd_t *bd)
Minghuan Lianda419022014-10-31 13:43:44 +0800790{
791 #ifdef CONFIG_PCIE1
792 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
793 #endif
794
795 #ifdef CONFIG_PCIE2
796 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
797 #endif
Minghuan Liane4e8cb72015-01-21 17:29:20 +0800798
799 #ifdef CONFIG_PCIE3
800 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
801 #endif
802
803 #ifdef CONFIG_PCIE4
804 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
805 #endif
Stuart Yoder5e8e27b2016-03-10 10:52:30 -0600806
807 #ifdef CONFIG_FSL_LSCH3
808 fdt_fixup_pcie(blob);
809 #endif
Minghuan Lianda419022014-10-31 13:43:44 +0800810}
811
812#else
Minghuan Liand42bd342015-03-12 10:58:48 +0800813void ft_pci_setup(void *blob, bd_t *bd)
Minghuan Lianda419022014-10-31 13:43:44 +0800814{
815}
816#endif