2 * Copyright 2014-2015 Freescale Semiconductor, Inc.
3 * Layerscape PCIe driver
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/arch/fsl_serdes.h>
14 #include <asm/pcie_layerscape.h>
16 #ifndef CONFIG_SYS_PCI_MEMORY_BUS
17 #define CONFIG_SYS_PCI_MEMORY_BUS CONFIG_SYS_SDRAM_BASE
20 #ifndef CONFIG_SYS_PCI_MEMORY_PHYS
21 #define CONFIG_SYS_PCI_MEMORY_PHYS CONFIG_SYS_SDRAM_BASE
24 #ifndef CONFIG_SYS_PCI_MEMORY_SIZE
25 #define CONFIG_SYS_PCI_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL) /* 2G */
29 #define PCIE_ATU_VIEWPORT 0x900
30 #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
31 #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
32 #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
33 #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
34 #define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
35 #define PCIE_ATU_REGION_INDEX3 (0x3 << 0)
36 #define PCIE_ATU_CR1 0x904
37 #define PCIE_ATU_TYPE_MEM (0x0 << 0)
38 #define PCIE_ATU_TYPE_IO (0x2 << 0)
39 #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
40 #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
41 #define PCIE_ATU_CR2 0x908
42 #define PCIE_ATU_ENABLE (0x1 << 31)
43 #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
44 #define PCIE_ATU_LOWER_BASE 0x90C
45 #define PCIE_ATU_UPPER_BASE 0x910
46 #define PCIE_ATU_LIMIT 0x914
47 #define PCIE_ATU_LOWER_TARGET 0x918
48 #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
49 #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
50 #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
51 #define PCIE_ATU_UPPER_TARGET 0x91C
53 #define PCIE_LINK_CAP 0x7c
54 #define PCIE_LINK_SPEED_MASK 0xf
55 #define PCIE_LINK_STA 0x82
57 #define PCIE_DBI_SIZE (4 * 1024) /* 4K */
62 void __iomem *va_cfg0;
63 void __iomem *va_cfg1;
64 struct pci_controller hose;
82 #define SET_LS_PCIE_INFO(x, num) \
84 x.regs = CONFIG_SYS_PCIE##num##_ADDR; \
85 x.cfg0_phys = CONFIG_SYS_PCIE_CFG0_PHYS_OFF + \
86 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
87 x.cfg0_size = CONFIG_SYS_PCIE_CFG0_SIZE; \
88 x.cfg1_phys = CONFIG_SYS_PCIE_CFG1_PHYS_OFF + \
89 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
90 x.cfg1_size = CONFIG_SYS_PCIE_CFG1_SIZE; \
91 x.mem_bus = CONFIG_SYS_PCIE_MEM_BUS; \
92 x.mem_phys = CONFIG_SYS_PCIE_MEM_PHYS_OFF + \
93 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
94 x.mem_size = CONFIG_SYS_PCIE_MEM_SIZE; \
95 x.io_bus = CONFIG_SYS_PCIE_IO_BUS; \
96 x.io_phys = CONFIG_SYS_PCIE_IO_PHYS_OFF + \
97 CONFIG_SYS_PCIE##num##_PHYS_ADDR; \
98 x.io_size = CONFIG_SYS_PCIE_IO_SIZE; \
102 #ifdef CONFIG_LS102XA
103 #include <asm/arch/immap_ls102xa.h>
105 /* PEX1/2 Misc Ports Status Register */
106 #define LTSSM_STATE_SHIFT 20
107 #define LTSSM_STATE_MASK 0x3f
108 #define LTSSM_PCIE_L0 0x11 /* L0 state */
110 static int ls_pcie_link_state(struct ls_pcie *pcie)
113 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
115 state = in_be32(&scfg->pexmscportsr[pcie->idx]);
116 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
117 if (state < LTSSM_PCIE_L0) {
118 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
125 #define PCIE_LDBG 0x7FC
127 static int ls_pcie_link_state(struct ls_pcie *pcie)
131 state = readl(pcie->dbi + PCIE_LDBG);
135 debug("....PCIe link error.\n");
140 static int ls_pcie_link_up(struct ls_pcie *pcie)
145 state = ls_pcie_link_state(pcie);
149 /* Try to download speed to gen1 */
150 cap = readl(pcie->dbi + PCIE_LINK_CAP);
151 writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
153 state = ls_pcie_link_state(pcie);
157 writel(cap, pcie->dbi + PCIE_LINK_CAP);
162 static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
164 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
165 pcie->dbi + PCIE_ATU_VIEWPORT);
166 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
169 static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
171 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
172 pcie->dbi + PCIE_ATU_VIEWPORT);
173 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
176 static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
177 u64 phys, u64 bus_addr, pci_size_t size)
179 writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
180 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
181 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
182 writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
183 writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
184 writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
185 writel(type, pcie->dbi + PCIE_ATU_CR1);
186 writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
189 static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
195 /* ATU 0 : OUTBOUND : CFG0 */
196 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
201 /* ATU 1 : OUTBOUND : CFG1 */
202 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
207 /* ATU 2 : OUTBOUND : MEM */
208 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
213 /* ATU 3 : OUTBOUND : IO */
214 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
221 for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
222 writel(PCIE_ATU_REGION_OUTBOUND | i,
223 pcie->dbi + PCIE_ATU_VIEWPORT);
224 debug("iATU%d:\n", i);
225 debug("\tLOWER PHYS 0x%08x\n",
226 readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
227 debug("\tUPPER PHYS 0x%08x\n",
228 readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
229 debug("\tLOWER BUS 0x%08x\n",
230 readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
231 debug("\tUPPER BUS 0x%08x\n",
232 readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
233 debug("\tLIMIT 0x%08x\n",
234 readl(pcie->dbi + PCIE_ATU_LIMIT));
235 debug("\tCR1 0x%08x\n",
236 readl(pcie->dbi + PCIE_ATU_CR1));
237 debug("\tCR2 0x%08x\n",
238 readl(pcie->dbi + PCIE_ATU_CR2));
243 int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
245 /* Do not skip controller */
249 static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
257 static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
260 struct ls_pcie *pcie = hose->priv_data;
263 if (ls_pcie_addr_valid(hose, d)) {
268 if (PCI_BUS(d) == hose->first_busno) {
269 addr = pcie->dbi + (where & ~0x3);
271 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
272 PCIE_ATU_DEV(PCI_DEV(d)) |
273 PCIE_ATU_FUNC(PCI_FUNC(d));
275 if (PCI_BUS(d) == hose->first_busno + 1) {
276 ls_pcie_cfg0_set_busdev(pcie, busdev);
277 addr = pcie->va_cfg0 + (where & ~0x3);
279 ls_pcie_cfg1_set_busdev(pcie, busdev);
280 addr = pcie->va_cfg1 + (where & ~0x3);
289 static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
292 struct ls_pcie *pcie = hose->priv_data;
295 if (ls_pcie_addr_valid(hose, d))
298 if (PCI_BUS(d) == hose->first_busno) {
299 addr = pcie->dbi + (where & ~0x3);
301 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
302 PCIE_ATU_DEV(PCI_DEV(d)) |
303 PCIE_ATU_FUNC(PCI_FUNC(d));
305 if (PCI_BUS(d) == hose->first_busno + 1) {
306 ls_pcie_cfg0_set_busdev(pcie, busdev);
307 addr = pcie->va_cfg0 + (where & ~0x3);
309 ls_pcie_cfg1_set_busdev(pcie, busdev);
310 addr = pcie->va_cfg1 + (where & ~0x3);
319 static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
320 struct ls_pcie_info *info)
322 struct pci_controller *hose = &pcie->hose;
323 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
325 ls_pcie_setup_atu(pcie, info);
327 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
329 /* program correct class for RC */
330 pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
331 PCI_CLASS_BRIDGE_PCI);
334 int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
336 struct ls_pcie *pcie;
337 struct pci_controller *hose;
338 int num = dev - PCIE1;
339 pci_dev_t pdev = PCI_BDF(busno, 0, 0);
340 int i, linkup, ep_mode;
344 if (!is_serdes_configured(dev)) {
345 printf("PCIe%d: disabled\n", num + 1);
349 pcie = malloc(sizeof(*pcie));
352 memset(pcie, 0, sizeof(*pcie));
355 hose->priv_data = pcie;
356 hose->first_busno = busno;
358 pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
359 pcie->va_cfg0 = map_physmem(info->cfg0_phys,
362 pcie->va_cfg1 = map_physmem(info->cfg1_phys,
366 /* outbound memory */
367 pci_set_region(&hose->regions[0],
368 (pci_size_t)info->mem_bus,
369 (phys_size_t)info->mem_phys,
370 (pci_size_t)info->mem_size,
374 pci_set_region(&hose->regions[1],
375 (pci_size_t)info->io_bus,
376 (phys_size_t)info->io_phys,
377 (pci_size_t)info->io_size,
380 /* System memory space */
381 pci_set_region(&hose->regions[2],
382 CONFIG_SYS_PCI_MEMORY_BUS,
383 CONFIG_SYS_PCI_MEMORY_PHYS,
384 CONFIG_SYS_PCI_MEMORY_SIZE,
385 PCI_REGION_SYS_MEMORY);
387 hose->region_count = 3;
389 for (i = 0; i < hose->region_count; i++)
390 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
392 (u64)hose->regions[i].phys_start,
393 (u64)hose->regions[i].bus_start,
394 (u64)hose->regions[i].size,
395 hose->regions[i].flags);
398 pci_hose_read_config_byte_via_dword,
399 pci_hose_read_config_word_via_dword,
401 pci_hose_write_config_byte_via_dword,
402 pci_hose_write_config_word_via_dword,
403 ls_pcie_write_config);
405 pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
406 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
407 printf("PCIe%u: %s ", info->pci_num,
408 ep_mode ? "Endpoint" : "Root Complex");
410 linkup = ls_pcie_link_up(pcie);
413 /* Let the user know there's no PCIe link */
414 printf("no link, regs @ 0x%lx\n", info->regs);
415 hose->last_busno = hose->first_busno;
419 /* Print the negotiated PCIe link width */
420 pci_hose_read_config_word(hose, dev, PCIE_LINK_STA, &temp16);
421 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
422 (temp16 & 0xf), info->regs);
427 ls_pcie_setup_ctrl(pcie, info);
429 pci_register_hose(hose);
431 hose->last_busno = pci_hose_scan(hose);
433 printf("PCIe%x: Bus %02x - %02x\n",
434 info->pci_num, hose->first_busno, hose->last_busno);
436 return hose->last_busno + 1;
439 int ls_pcie_init_board(int busno)
441 struct ls_pcie_info info;
444 SET_LS_PCIE_INFO(info, 1);
445 busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
449 SET_LS_PCIE_INFO(info, 2);
450 busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
454 SET_LS_PCIE_INFO(info, 3);
455 busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
459 SET_LS_PCIE_INFO(info, 4);
460 busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
466 void pci_init_board(void)
468 ls_pcie_init_board(0);
471 #ifdef CONFIG_OF_BOARD_SETUP
473 #include <fdt_support.h>
475 static void ft_pcie_ls_setup(void *blob, const char *pci_compat,
476 unsigned long ctrl_addr, enum srds_prtcl dev)
480 off = fdt_node_offset_by_compat_reg(blob, pci_compat,
481 (phys_addr_t)ctrl_addr);
485 if (!is_serdes_configured(dev))
486 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
489 void ft_pcie_setup(void *blob, bd_t *bd)
492 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE1_ADDR, PCIE1);
496 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE2_ADDR, PCIE2);
500 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE3_ADDR, PCIE3);
504 ft_pcie_ls_setup(blob, FSL_PCIE_COMPAT, CONFIG_SYS_PCIE4_ADDR, PCIE4);
509 void ft_pcie_setup(void *blob, bd_t *bd)