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 #ifndef CONFIG_LS102XA
15 #include <asm/arch/fdt.h>
16 #include <asm/arch/soc.h>
18 #include <asm/arch/immap_ls102xa.h>
20 #include "pcie_layerscape.h"
23 /* PEX1/2 Misc Ports Status Register */
24 #define LTSSM_STATE_SHIFT 20
26 static int ls_pcie_link_state(struct ls_pcie *pcie)
29 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
31 state = in_be32(&scfg->pexmscportsr[pcie->idx]);
32 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
33 if (state < LTSSM_PCIE_L0) {
34 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
41 static int ls_pcie_link_state(struct ls_pcie *pcie)
45 state = pex_lut_in32(pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_DBG) &
47 if (state < LTSSM_PCIE_L0) {
48 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
56 static int ls_pcie_link_up(struct ls_pcie *pcie)
61 state = ls_pcie_link_state(pcie);
65 /* Try to download speed to gen1 */
66 cap = readl(pcie->dbi + PCIE_LINK_CAP);
67 writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
69 * Notice: the following delay has critical impact on link training
70 * if too short (<30ms) the link doesn't get up.
73 state = ls_pcie_link_state(pcie);
77 writel(cap, pcie->dbi + PCIE_LINK_CAP);
82 static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
84 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
85 pcie->dbi + PCIE_ATU_VIEWPORT);
86 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
89 static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
91 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
92 pcie->dbi + PCIE_ATU_VIEWPORT);
93 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
96 static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
97 u64 phys, u64 bus_addr, pci_size_t size)
99 writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
100 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
101 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
102 writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
103 writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
104 writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
105 writel(type, pcie->dbi + PCIE_ATU_CR1);
106 writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
109 /* Use bar match mode and MEM type as default */
110 static void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
113 writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
114 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
115 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
116 writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
117 writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
118 PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
121 static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
127 /* ATU 0 : OUTBOUND : CFG0 */
128 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
133 /* ATU 1 : OUTBOUND : CFG1 */
134 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
139 /* ATU 2 : OUTBOUND : MEM */
140 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
145 /* ATU 3 : OUTBOUND : IO */
146 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
153 for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
154 writel(PCIE_ATU_REGION_OUTBOUND | i,
155 pcie->dbi + PCIE_ATU_VIEWPORT);
156 debug("iATU%d:\n", i);
157 debug("\tLOWER PHYS 0x%08x\n",
158 readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
159 debug("\tUPPER PHYS 0x%08x\n",
160 readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
161 debug("\tLOWER BUS 0x%08x\n",
162 readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
163 debug("\tUPPER BUS 0x%08x\n",
164 readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
165 debug("\tLIMIT 0x%08x\n",
166 readl(pcie->dbi + PCIE_ATU_LIMIT));
167 debug("\tCR1 0x%08x\n",
168 readl(pcie->dbi + PCIE_ATU_CR1));
169 debug("\tCR2 0x%08x\n",
170 readl(pcie->dbi + PCIE_ATU_CR2));
175 int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
177 /* Do not skip controller */
181 static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
186 /* Controller does not support multi-function in RC mode */
187 if ((PCI_BUS(d) == hose->first_busno) && (PCI_FUNC(d) > 0))
193 static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
196 struct ls_pcie *pcie = hose->priv_data;
199 if (ls_pcie_addr_valid(hose, d)) {
204 if (PCI_BUS(d) == hose->first_busno) {
205 addr = pcie->dbi + (where & ~0x3);
207 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
208 PCIE_ATU_DEV(PCI_DEV(d)) |
209 PCIE_ATU_FUNC(PCI_FUNC(d));
211 if (PCI_BUS(d) == hose->first_busno + 1) {
212 ls_pcie_cfg0_set_busdev(pcie, busdev);
213 addr = pcie->va_cfg0 + (where & ~0x3);
215 ls_pcie_cfg1_set_busdev(pcie, busdev);
216 addr = pcie->va_cfg1 + (where & ~0x3);
225 static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
228 struct ls_pcie *pcie = hose->priv_data;
231 if (ls_pcie_addr_valid(hose, d))
234 if (PCI_BUS(d) == hose->first_busno) {
235 addr = pcie->dbi + (where & ~0x3);
237 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
238 PCIE_ATU_DEV(PCI_DEV(d)) |
239 PCIE_ATU_FUNC(PCI_FUNC(d));
241 if (PCI_BUS(d) == hose->first_busno + 1) {
242 ls_pcie_cfg0_set_busdev(pcie, busdev);
243 addr = pcie->va_cfg0 + (where & ~0x3);
245 ls_pcie_cfg1_set_busdev(pcie, busdev);
246 addr = pcie->va_cfg1 + (where & ~0x3);
255 static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
256 struct ls_pcie_info *info)
258 struct pci_controller *hose = &pcie->hose;
259 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
261 ls_pcie_setup_atu(pcie, info);
263 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
265 /* program correct class for RC */
266 writel(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
267 pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
268 PCI_CLASS_BRIDGE_PCI);
269 #ifndef CONFIG_LS102XA
270 writel(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
274 static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie,
275 struct ls_pcie_info *info)
277 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
279 /* ATU 0 : INBOUND : map BAR0 */
280 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX0, 0, phys);
281 /* ATU 1 : INBOUND : map BAR1 */
282 phys += PCIE_BAR1_SIZE;
283 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX1, 1, phys);
284 /* ATU 2 : INBOUND : map BAR2 */
285 phys += PCIE_BAR2_SIZE;
286 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX2, 2, phys);
287 /* ATU 3 : INBOUND : map BAR4 */
288 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
289 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX3, 4, phys);
291 /* ATU 0 : OUTBOUND : map 4G MEM */
292 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
296 4 * 1024 * 1024 * 1024ULL);
299 /* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
300 static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
307 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
310 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
313 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
314 writel(0, bar_base + PCI_BASE_ADDRESS_3);
317 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
318 writel(0, bar_base + PCI_BASE_ADDRESS_5);
325 static void ls_pcie_ep_setup_bars(void *bar_base)
327 /* BAR0 - 32bit - 4K configuration */
328 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
329 /* BAR1 - 32bit - 8K MSIX*/
330 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
331 /* BAR2 - 64bit - 4K MEM desciptor */
332 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
333 /* BAR4 - 64bit - 1M MEM*/
334 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
337 static void ls_pcie_setup_ep(struct ls_pcie *pcie, struct ls_pcie_info *info)
339 struct pci_controller *hose = &pcie->hose;
340 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
343 sriov = pci_hose_find_ext_capability(hose, dev, PCI_EXT_CAP_ID_SRIOV);
347 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
348 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
349 #ifndef CONFIG_LS102XA
350 writel(PCIE_LCTRL0_VAL(pf, vf),
351 pcie->dbi + PCIE_LUT_BASE +
354 ls_pcie_ep_setup_bars(pcie->dbi);
355 ls_pcie_ep_setup_atu(pcie, info);
360 #ifndef CONFIG_LS102XA
361 writel(0, pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
364 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
365 ls_pcie_ep_setup_atu(pcie, info);
369 int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
371 struct ls_pcie *pcie;
372 struct pci_controller *hose;
373 int num = dev - PCIE1;
374 pci_dev_t pdev = PCI_BDF(busno, 0, 0);
375 int i, linkup, ep_mode;
379 if (!is_serdes_configured(dev)) {
380 printf("PCIe%d: disabled\n", num + 1);
384 pcie = malloc(sizeof(*pcie));
387 memset(pcie, 0, sizeof(*pcie));
390 hose->priv_data = pcie;
391 hose->first_busno = busno;
393 pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
394 pcie->va_cfg0 = map_physmem(info->cfg0_phys,
397 pcie->va_cfg1 = map_physmem(info->cfg1_phys,
400 pcie->next_lut_index = 0;
402 /* outbound memory */
403 pci_set_region(&hose->regions[0],
404 (pci_size_t)info->mem_bus,
405 (phys_size_t)info->mem_phys,
406 (pci_size_t)info->mem_size,
410 pci_set_region(&hose->regions[1],
411 (pci_size_t)info->io_bus,
412 (phys_size_t)info->io_phys,
413 (pci_size_t)info->io_size,
416 /* System memory space */
417 pci_set_region(&hose->regions[2],
418 CONFIG_SYS_PCI_MEMORY_BUS,
419 CONFIG_SYS_PCI_MEMORY_PHYS,
420 CONFIG_SYS_PCI_MEMORY_SIZE,
421 PCI_REGION_SYS_MEMORY);
423 hose->region_count = 3;
425 for (i = 0; i < hose->region_count; i++)
426 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
428 (u64)hose->regions[i].phys_start,
429 (u64)hose->regions[i].bus_start,
430 (u64)hose->regions[i].size,
431 hose->regions[i].flags);
434 pci_hose_read_config_byte_via_dword,
435 pci_hose_read_config_word_via_dword,
437 pci_hose_write_config_byte_via_dword,
438 pci_hose_write_config_word_via_dword,
439 ls_pcie_write_config);
441 pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
442 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
443 printf("PCIe%u: %s ", info->pci_num,
444 ep_mode ? "Endpoint" : "Root Complex");
447 ls_pcie_setup_ep(pcie, info);
449 ls_pcie_setup_ctrl(pcie, info);
451 linkup = ls_pcie_link_up(pcie);
454 /* Let the user know there's no PCIe link */
455 printf("no link, regs @ 0x%lx\n", info->regs);
456 hose->last_busno = hose->first_busno;
460 /* Print the negotiated PCIe link width */
461 pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
462 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
463 (temp16 & 0xf), info->regs);
468 pci_register_hose(hose);
470 hose->last_busno = pci_hose_scan(hose);
472 printf("PCIe%x: Bus %02x - %02x\n",
473 info->pci_num, hose->first_busno, hose->last_busno);
475 return hose->last_busno + 1;
478 int ls_pcie_init_board(int busno)
480 struct ls_pcie_info info;
483 SET_LS_PCIE_INFO(info, 1);
484 busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
488 SET_LS_PCIE_INFO(info, 2);
489 busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
493 SET_LS_PCIE_INFO(info, 3);
494 busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
498 SET_LS_PCIE_INFO(info, 4);
499 busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
505 void pci_init_board(void)
507 ls_pcie_init_board(0);