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>
15 #ifndef CONFIG_LS102XA
16 #include <asm/arch/fdt.h>
17 #include <asm/arch/soc.h>
19 #include <asm/arch/immap_ls102xa.h>
21 #include "pcie_layerscape.h"
23 DECLARE_GLOBAL_DATA_PTR;
27 /* PEX1/2 Misc Ports Status Register */
28 #define LTSSM_STATE_SHIFT 20
30 static int ls_pcie_link_state(struct ls_pcie *pcie)
33 struct ccsr_scfg *scfg = (struct ccsr_scfg *)CONFIG_SYS_FSL_SCFG_ADDR;
35 state = in_be32(&scfg->pexmscportsr[pcie->idx]);
36 state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
37 if (state < LTSSM_PCIE_L0) {
38 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
45 static int ls_pcie_link_state(struct ls_pcie *pcie)
49 state = pex_lut_in32(pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_DBG) &
51 if (state < LTSSM_PCIE_L0) {
52 debug("....PCIe link error. LTSSM=0x%02x.\n", state);
60 static int ls_pcie_link_up(struct ls_pcie *pcie)
65 state = ls_pcie_link_state(pcie);
69 /* Try to download speed to gen1 */
70 cap = readl(pcie->dbi + PCIE_LINK_CAP);
71 writel((cap & (~PCIE_LINK_SPEED_MASK)) | 1, pcie->dbi + PCIE_LINK_CAP);
73 * Notice: the following delay has critical impact on link training
74 * if too short (<30ms) the link doesn't get up.
77 state = ls_pcie_link_state(pcie);
81 writel(cap, pcie->dbi + PCIE_LINK_CAP);
86 static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
88 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
89 pcie->dbi + PCIE_ATU_VIEWPORT);
90 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
93 static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
95 writel(PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
96 pcie->dbi + PCIE_ATU_VIEWPORT);
97 writel(busdev, pcie->dbi + PCIE_ATU_LOWER_TARGET);
100 static void ls_pcie_iatu_outbound_set(struct ls_pcie *pcie, int idx, int type,
101 u64 phys, u64 bus_addr, pci_size_t size)
103 writel(PCIE_ATU_REGION_OUTBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
104 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_BASE);
105 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_BASE);
106 writel(phys + size - 1, pcie->dbi + PCIE_ATU_LIMIT);
107 writel((u32)bus_addr, pcie->dbi + PCIE_ATU_LOWER_TARGET);
108 writel(bus_addr >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
109 writel(type, pcie->dbi + PCIE_ATU_CR1);
110 writel(PCIE_ATU_ENABLE, pcie->dbi + PCIE_ATU_CR2);
113 /* Use bar match mode and MEM type as default */
114 static void ls_pcie_iatu_inbound_set(struct ls_pcie *pcie, int idx,
117 writel(PCIE_ATU_REGION_INBOUND | idx, pcie->dbi + PCIE_ATU_VIEWPORT);
118 writel((u32)phys, pcie->dbi + PCIE_ATU_LOWER_TARGET);
119 writel(phys >> 32, pcie->dbi + PCIE_ATU_UPPER_TARGET);
120 writel(PCIE_ATU_TYPE_MEM, pcie->dbi + PCIE_ATU_CR1);
121 writel(PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
122 PCIE_ATU_BAR_NUM(bar), pcie->dbi + PCIE_ATU_CR2);
125 static void ls_pcie_setup_atu(struct ls_pcie *pcie, struct ls_pcie_info *info)
131 /* ATU 0 : OUTBOUND : CFG0 */
132 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
137 /* ATU 1 : OUTBOUND : CFG1 */
138 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
143 /* ATU 2 : OUTBOUND : MEM */
144 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX2,
149 /* ATU 3 : OUTBOUND : IO */
150 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX3,
157 for (i = 0; i <= PCIE_ATU_REGION_INDEX3; i++) {
158 writel(PCIE_ATU_REGION_OUTBOUND | i,
159 pcie->dbi + PCIE_ATU_VIEWPORT);
160 debug("iATU%d:\n", i);
161 debug("\tLOWER PHYS 0x%08x\n",
162 readl(pcie->dbi + PCIE_ATU_LOWER_BASE));
163 debug("\tUPPER PHYS 0x%08x\n",
164 readl(pcie->dbi + PCIE_ATU_UPPER_BASE));
165 debug("\tLOWER BUS 0x%08x\n",
166 readl(pcie->dbi + PCIE_ATU_LOWER_TARGET));
167 debug("\tUPPER BUS 0x%08x\n",
168 readl(pcie->dbi + PCIE_ATU_UPPER_TARGET));
169 debug("\tLIMIT 0x%08x\n",
170 readl(pcie->dbi + PCIE_ATU_LIMIT));
171 debug("\tCR1 0x%08x\n",
172 readl(pcie->dbi + PCIE_ATU_CR1));
173 debug("\tCR2 0x%08x\n",
174 readl(pcie->dbi + PCIE_ATU_CR2));
179 int pci_skip_dev(struct pci_controller *hose, pci_dev_t dev)
181 /* Do not skip controller */
185 static int ls_pcie_addr_valid(struct pci_controller *hose, pci_dev_t d)
190 /* Controller does not support multi-function in RC mode */
191 if ((PCI_BUS(d) == hose->first_busno) && (PCI_FUNC(d) > 0))
197 static int ls_pcie_read_config(struct pci_controller *hose, pci_dev_t d,
200 struct ls_pcie *pcie = hose->priv_data;
203 if (ls_pcie_addr_valid(hose, d)) {
208 if (PCI_BUS(d) == hose->first_busno) {
209 addr = pcie->dbi + (where & ~0x3);
211 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
212 PCIE_ATU_DEV(PCI_DEV(d)) |
213 PCIE_ATU_FUNC(PCI_FUNC(d));
215 if (PCI_BUS(d) == hose->first_busno + 1) {
216 ls_pcie_cfg0_set_busdev(pcie, busdev);
217 addr = pcie->va_cfg0 + (where & ~0x3);
219 ls_pcie_cfg1_set_busdev(pcie, busdev);
220 addr = pcie->va_cfg1 + (where & ~0x3);
229 static int ls_pcie_write_config(struct pci_controller *hose, pci_dev_t d,
232 struct ls_pcie *pcie = hose->priv_data;
235 if (ls_pcie_addr_valid(hose, d))
238 if (PCI_BUS(d) == hose->first_busno) {
239 addr = pcie->dbi + (where & ~0x3);
241 busdev = PCIE_ATU_BUS(PCI_BUS(d)) |
242 PCIE_ATU_DEV(PCI_DEV(d)) |
243 PCIE_ATU_FUNC(PCI_FUNC(d));
245 if (PCI_BUS(d) == hose->first_busno + 1) {
246 ls_pcie_cfg0_set_busdev(pcie, busdev);
247 addr = pcie->va_cfg0 + (where & ~0x3);
249 ls_pcie_cfg1_set_busdev(pcie, busdev);
250 addr = pcie->va_cfg1 + (where & ~0x3);
259 static void ls_pcie_setup_ctrl(struct ls_pcie *pcie,
260 struct ls_pcie_info *info)
262 struct pci_controller *hose = &pcie->hose;
263 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
265 ls_pcie_setup_atu(pcie, info);
267 pci_hose_write_config_dword(hose, dev, PCI_BASE_ADDRESS_0, 0);
269 /* program correct class for RC */
270 writel(1, pcie->dbi + PCIE_DBI_RO_WR_EN);
271 pci_hose_write_config_word(hose, dev, PCI_CLASS_DEVICE,
272 PCI_CLASS_BRIDGE_PCI);
273 #ifndef CONFIG_LS102XA
274 writel(0, pcie->dbi + PCIE_DBI_RO_WR_EN);
278 static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie,
279 struct ls_pcie_info *info)
281 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
283 /* ATU 0 : INBOUND : map BAR0 */
284 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX0, 0, phys);
285 /* ATU 1 : INBOUND : map BAR1 */
286 phys += PCIE_BAR1_SIZE;
287 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX1, 1, phys);
288 /* ATU 2 : INBOUND : map BAR2 */
289 phys += PCIE_BAR2_SIZE;
290 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX2, 2, phys);
291 /* ATU 3 : INBOUND : map BAR4 */
292 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
293 ls_pcie_iatu_inbound_set(pcie, PCIE_ATU_REGION_INDEX3, 4, phys);
295 /* ATU 0 : OUTBOUND : map 4G MEM */
296 ls_pcie_iatu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
300 4 * 1024 * 1024 * 1024ULL);
303 /* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
304 static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
311 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
314 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
317 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
318 writel(0, bar_base + PCI_BASE_ADDRESS_3);
321 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
322 writel(0, bar_base + PCI_BASE_ADDRESS_5);
329 static void ls_pcie_ep_setup_bars(void *bar_base)
331 /* BAR0 - 32bit - 4K configuration */
332 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
333 /* BAR1 - 32bit - 8K MSIX*/
334 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
335 /* BAR2 - 64bit - 4K MEM desciptor */
336 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
337 /* BAR4 - 64bit - 1M MEM*/
338 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
341 static void ls_pcie_setup_ep(struct ls_pcie *pcie, struct ls_pcie_info *info)
343 struct pci_controller *hose = &pcie->hose;
344 pci_dev_t dev = PCI_BDF(hose->first_busno, 0, 0);
347 sriov = pci_hose_find_ext_capability(hose, dev, PCI_EXT_CAP_ID_SRIOV);
351 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
352 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
353 #ifndef CONFIG_LS102XA
354 writel(PCIE_LCTRL0_VAL(pf, vf),
355 pcie->dbi + PCIE_LUT_BASE +
358 ls_pcie_ep_setup_bars(pcie->dbi);
359 ls_pcie_ep_setup_atu(pcie, info);
364 #ifndef CONFIG_LS102XA
365 writel(0, pcie->dbi + PCIE_LUT_BASE + PCIE_LUT_LCTRL0);
368 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
369 ls_pcie_ep_setup_atu(pcie, info);
373 int ls_pcie_init_ctrl(int busno, enum srds_prtcl dev, struct ls_pcie_info *info)
375 struct ls_pcie *pcie;
376 struct pci_controller *hose;
377 int num = dev - PCIE1;
378 pci_dev_t pdev = PCI_BDF(busno, 0, 0);
379 int i, linkup, ep_mode;
383 if (!is_serdes_configured(dev)) {
384 printf("PCIe%d: disabled\n", num + 1);
388 pcie = malloc(sizeof(*pcie));
391 memset(pcie, 0, sizeof(*pcie));
394 hose->priv_data = pcie;
395 hose->first_busno = busno;
397 pcie->dbi = map_physmem(info->regs, PCIE_DBI_SIZE, MAP_NOCACHE);
398 pcie->va_cfg0 = map_physmem(info->cfg0_phys,
401 pcie->va_cfg1 = map_physmem(info->cfg1_phys,
404 pcie->next_lut_index = 0;
406 /* outbound memory */
407 pci_set_region(&hose->regions[0],
408 (pci_size_t)info->mem_bus,
409 (phys_size_t)info->mem_phys,
410 (pci_size_t)info->mem_size,
414 pci_set_region(&hose->regions[1],
415 (pci_size_t)info->io_bus,
416 (phys_size_t)info->io_phys,
417 (pci_size_t)info->io_size,
420 /* System memory space */
421 pci_set_region(&hose->regions[2],
422 CONFIG_SYS_PCI_MEMORY_BUS,
423 CONFIG_SYS_PCI_MEMORY_PHYS,
424 CONFIG_SYS_PCI_MEMORY_SIZE,
425 PCI_REGION_SYS_MEMORY);
427 hose->region_count = 3;
429 for (i = 0; i < hose->region_count; i++)
430 debug("PCI reg:%d %016llx:%016llx %016llx %08lx\n",
432 (u64)hose->regions[i].phys_start,
433 (u64)hose->regions[i].bus_start,
434 (u64)hose->regions[i].size,
435 hose->regions[i].flags);
438 pci_hose_read_config_byte_via_dword,
439 pci_hose_read_config_word_via_dword,
441 pci_hose_write_config_byte_via_dword,
442 pci_hose_write_config_word_via_dword,
443 ls_pcie_write_config);
445 pci_hose_read_config_byte(hose, pdev, PCI_HEADER_TYPE, &header_type);
446 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
447 printf("PCIe%u: %s ", info->pci_num,
448 ep_mode ? "Endpoint" : "Root Complex");
451 ls_pcie_setup_ep(pcie, info);
453 ls_pcie_setup_ctrl(pcie, info);
455 linkup = ls_pcie_link_up(pcie);
458 /* Let the user know there's no PCIe link */
459 printf("no link, regs @ 0x%lx\n", info->regs);
460 hose->last_busno = hose->first_busno;
464 /* Print the negotiated PCIe link width */
465 pci_hose_read_config_word(hose, pdev, PCIE_LINK_STA, &temp16);
466 printf("x%d gen%d, regs @ 0x%lx\n", (temp16 & 0x3f0) >> 4,
467 (temp16 & 0xf), info->regs);
472 pci_register_hose(hose);
474 hose->last_busno = pci_hose_scan(hose);
476 printf("PCIe%x: Bus %02x - %02x\n",
477 info->pci_num, hose->first_busno, hose->last_busno);
479 return hose->last_busno + 1;
482 int ls_pcie_init_board(int busno)
484 struct ls_pcie_info info;
487 SET_LS_PCIE_INFO(info, 1);
488 busno = ls_pcie_init_ctrl(busno, PCIE1, &info);
492 SET_LS_PCIE_INFO(info, 2);
493 busno = ls_pcie_init_ctrl(busno, PCIE2, &info);
497 SET_LS_PCIE_INFO(info, 3);
498 busno = ls_pcie_init_ctrl(busno, PCIE3, &info);
502 SET_LS_PCIE_INFO(info, 4);
503 busno = ls_pcie_init_ctrl(busno, PCIE4, &info);
509 void pci_init_board(void)
511 ls_pcie_init_board(0);
515 LIST_HEAD(ls_pcie_list);
517 static unsigned int dbi_readl(struct ls_pcie *pcie, unsigned int offset)
519 return in_le32(pcie->dbi + offset);
522 static void dbi_writel(struct ls_pcie *pcie, unsigned int value,
525 out_le32(pcie->dbi + offset, value);
528 static unsigned int ctrl_readl(struct ls_pcie *pcie, unsigned int offset)
530 if (pcie->big_endian)
531 return in_be32(pcie->ctrl + offset);
533 return in_le32(pcie->ctrl + offset);
536 static void ctrl_writel(struct ls_pcie *pcie, unsigned int value,
539 if (pcie->big_endian)
540 out_be32(pcie->ctrl + offset, value);
542 out_le32(pcie->ctrl + offset, value);
545 static int ls_pcie_ltssm(struct ls_pcie *pcie)
551 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
552 state = ctrl_readl(pcie, LS1021_PEXMSCPORTSR(pcie->idx));
553 state = (state >> LS1021_LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
555 state = ctrl_readl(pcie, PCIE_PF_DBG) & LTSSM_STATE_MASK;
561 static int ls_pcie_link_up(struct ls_pcie *pcie)
565 ltssm = ls_pcie_ltssm(pcie);
566 if (ltssm < LTSSM_PCIE_L0)
572 static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
574 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
576 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
579 static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
581 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
583 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
586 static void ls_pcie_atu_outbound_set(struct ls_pcie *pcie, int idx, int type,
587 u64 phys, u64 bus_addr, pci_size_t size)
589 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | idx, PCIE_ATU_VIEWPORT);
590 dbi_writel(pcie, (u32)phys, PCIE_ATU_LOWER_BASE);
591 dbi_writel(pcie, phys >> 32, PCIE_ATU_UPPER_BASE);
592 dbi_writel(pcie, (u32)phys + size - 1, PCIE_ATU_LIMIT);
593 dbi_writel(pcie, (u32)bus_addr, PCIE_ATU_LOWER_TARGET);
594 dbi_writel(pcie, bus_addr >> 32, PCIE_ATU_UPPER_TARGET);
595 dbi_writel(pcie, type, PCIE_ATU_CR1);
596 dbi_writel(pcie, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
599 /* Use bar match mode and MEM type as default */
600 static void ls_pcie_atu_inbound_set(struct ls_pcie *pcie, int idx,
603 dbi_writel(pcie, PCIE_ATU_REGION_INBOUND | idx, PCIE_ATU_VIEWPORT);
604 dbi_writel(pcie, (u32)phys, PCIE_ATU_LOWER_TARGET);
605 dbi_writel(pcie, phys >> 32, PCIE_ATU_UPPER_TARGET);
606 dbi_writel(pcie, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
607 dbi_writel(pcie, PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
608 PCIE_ATU_BAR_NUM(bar), PCIE_ATU_CR2);
611 static void ls_pcie_dump_atu(struct ls_pcie *pcie)
615 for (i = 0; i < PCIE_ATU_REGION_NUM; i++) {
616 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | i,
618 debug("iATU%d:\n", i);
619 debug("\tLOWER PHYS 0x%08x\n",
620 dbi_readl(pcie, PCIE_ATU_LOWER_BASE));
621 debug("\tUPPER PHYS 0x%08x\n",
622 dbi_readl(pcie, PCIE_ATU_UPPER_BASE));
623 debug("\tLOWER BUS 0x%08x\n",
624 dbi_readl(pcie, PCIE_ATU_LOWER_TARGET));
625 debug("\tUPPER BUS 0x%08x\n",
626 dbi_readl(pcie, PCIE_ATU_UPPER_TARGET));
627 debug("\tLIMIT 0x%08x\n",
628 readl(pcie->dbi + PCIE_ATU_LIMIT));
629 debug("\tCR1 0x%08x\n",
630 dbi_readl(pcie, PCIE_ATU_CR1));
631 debug("\tCR2 0x%08x\n",
632 dbi_readl(pcie, PCIE_ATU_CR2));
636 static void ls_pcie_setup_atu(struct ls_pcie *pcie)
638 struct pci_region *io, *mem, *pref;
639 unsigned long long offset = 0;
644 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
645 offset = LS1021_PCIE_SPACE_OFFSET +
646 LS1021_PCIE_SPACE_SIZE * pcie->idx;
649 /* ATU 0 : OUTBOUND : CFG0 */
650 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
652 pcie->cfg_res.start + offset,
654 fdt_resource_size(&pcie->cfg_res) / 2);
655 /* ATU 1 : OUTBOUND : CFG1 */
656 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
658 pcie->cfg_res.start + offset +
659 fdt_resource_size(&pcie->cfg_res) / 2,
661 fdt_resource_size(&pcie->cfg_res) / 2);
663 pci_get_regions(pcie->bus, &io, &mem, &pref);
664 idx = PCIE_ATU_REGION_INDEX1 + 1;
667 /* ATU : OUTBOUND : IO */
668 ls_pcie_atu_outbound_set(pcie, idx++,
670 io->phys_start + offset,
675 /* ATU : OUTBOUND : MEM */
676 ls_pcie_atu_outbound_set(pcie, idx++,
678 mem->phys_start + offset,
683 /* ATU : OUTBOUND : pref */
684 ls_pcie_atu_outbound_set(pcie, idx++,
686 pref->phys_start + offset,
690 ls_pcie_dump_atu(pcie);
693 /* Return 0 if the address is valid, -errno if not valid */
694 static int ls_pcie_addr_valid(struct ls_pcie *pcie, pci_dev_t bdf)
696 struct udevice *bus = pcie->bus;
701 if (PCI_BUS(bdf) < bus->seq)
704 if ((PCI_BUS(bdf) > bus->seq) && (!ls_pcie_link_up(pcie)))
707 if (PCI_BUS(bdf) <= (bus->seq + 1) && (PCI_DEV(bdf) > 0))
713 void *ls_pcie_conf_address(struct ls_pcie *pcie, pci_dev_t bdf,
716 struct udevice *bus = pcie->bus;
719 if (PCI_BUS(bdf) == bus->seq)
720 return pcie->dbi + offset;
722 busdev = PCIE_ATU_BUS(PCI_BUS(bdf)) |
723 PCIE_ATU_DEV(PCI_DEV(bdf)) |
724 PCIE_ATU_FUNC(PCI_FUNC(bdf));
726 if (PCI_BUS(bdf) == bus->seq + 1) {
727 ls_pcie_cfg0_set_busdev(pcie, busdev);
728 return pcie->cfg0 + offset;
730 ls_pcie_cfg1_set_busdev(pcie, busdev);
731 return pcie->cfg1 + offset;
735 static int ls_pcie_read_config(struct udevice *bus, pci_dev_t bdf,
736 uint offset, ulong *valuep,
737 enum pci_size_t size)
739 struct ls_pcie *pcie = dev_get_priv(bus);
742 if (ls_pcie_addr_valid(pcie, bdf)) {
743 *valuep = pci_get_ff(size);
747 address = ls_pcie_conf_address(pcie, bdf, offset);
751 *valuep = readb(address);
754 *valuep = readw(address);
757 *valuep = readl(address);
764 static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
765 uint offset, ulong value,
766 enum pci_size_t size)
768 struct ls_pcie *pcie = dev_get_priv(bus);
771 if (ls_pcie_addr_valid(pcie, bdf))
774 address = ls_pcie_conf_address(pcie, bdf, offset);
778 writeb(value, address);
781 writew(value, address);
784 writel(value, address);
791 /* Clear multi-function bit */
792 static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
794 writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
797 /* Fix class value */
798 static void ls_pcie_fix_class(struct ls_pcie *pcie)
800 writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
803 /* Drop MSG TLP except for Vendor MSG */
804 static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
808 val = dbi_readl(pcie, PCIE_STRFMR1);
810 dbi_writel(pcie, val, PCIE_STRFMR1);
813 /* Disable all bars in RC mode */
814 static void ls_pcie_disable_bars(struct ls_pcie *pcie)
818 sriov = in_le32(pcie->dbi + PCIE_SRIOV);
821 * TODO: For PCIe controller with SRIOV, the method to disable bars
822 * is different and more complex, so will add later.
824 if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV)
827 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
828 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
829 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
832 static void ls_pcie_setup_ctrl(struct ls_pcie *pcie)
834 ls_pcie_setup_atu(pcie);
836 dbi_writel(pcie, 1, PCIE_DBI_RO_WR_EN);
837 ls_pcie_fix_class(pcie);
838 ls_pcie_clear_multifunction(pcie);
839 ls_pcie_drop_msg_tlp(pcie);
840 dbi_writel(pcie, 0, PCIE_DBI_RO_WR_EN);
842 ls_pcie_disable_bars(pcie);
845 static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie)
847 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
849 /* ATU 0 : INBOUND : map BAR0 */
850 ls_pcie_atu_inbound_set(pcie, 0, 0, phys);
851 /* ATU 1 : INBOUND : map BAR1 */
852 phys += PCIE_BAR1_SIZE;
853 ls_pcie_atu_inbound_set(pcie, 1, 1, phys);
854 /* ATU 2 : INBOUND : map BAR2 */
855 phys += PCIE_BAR2_SIZE;
856 ls_pcie_atu_inbound_set(pcie, 2, 2, phys);
857 /* ATU 3 : INBOUND : map BAR4 */
858 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
859 ls_pcie_atu_inbound_set(pcie, 3, 4, phys);
861 /* ATU 0 : OUTBOUND : map MEM */
862 ls_pcie_atu_outbound_set(pcie, 0,
866 CONFIG_SYS_PCI_MEMORY_SIZE);
869 /* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
870 static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
872 /* The least inbound window is 4KiB */
878 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
881 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
884 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
885 writel(0, bar_base + PCI_BASE_ADDRESS_3);
888 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
889 writel(0, bar_base + PCI_BASE_ADDRESS_5);
896 static void ls_pcie_ep_setup_bars(void *bar_base)
898 /* BAR0 - 32bit - 4K configuration */
899 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
900 /* BAR1 - 32bit - 8K MSIX*/
901 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
902 /* BAR2 - 64bit - 4K MEM desciptor */
903 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
904 /* BAR4 - 64bit - 1M MEM*/
905 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
908 static void ls_pcie_setup_ep(struct ls_pcie *pcie)
912 sriov = readl(pcie->dbi + PCIE_SRIOV);
913 if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV) {
916 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
917 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
918 ctrl_writel(pcie, PCIE_LCTRL0_VAL(pf, vf),
921 ls_pcie_ep_setup_bars(pcie->dbi);
922 ls_pcie_ep_setup_atu(pcie);
926 ctrl_writel(pcie, 0, PCIE_PF_VF_CTRL);
928 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
929 ls_pcie_ep_setup_atu(pcie);
933 static int ls_pcie_probe(struct udevice *dev)
935 struct ls_pcie *pcie = dev_get_priv(dev);
936 const void *fdt = gd->fdt_blob;
937 int node = dev->of_offset;
945 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
946 "dbi", &pcie->dbi_res);
948 printf("ls-pcie: resource \"dbi\" not found\n");
952 pcie->idx = (pcie->dbi_res.start - PCIE_SYS_BASE_ADDR) / PCIE_CCSR_SIZE;
954 list_add(&pcie->list, &ls_pcie_list);
956 pcie->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
957 if (!pcie->enabled) {
958 printf("PCIe%d: %s disabled\n", pcie->idx, dev->name);
962 pcie->dbi = map_physmem(pcie->dbi_res.start,
963 fdt_resource_size(&pcie->dbi_res),
966 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
967 "lut", &pcie->lut_res);
969 pcie->lut = map_physmem(pcie->lut_res.start,
970 fdt_resource_size(&pcie->lut_res),
973 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
974 "ctrl", &pcie->ctrl_res);
976 pcie->ctrl = map_physmem(pcie->ctrl_res.start,
977 fdt_resource_size(&pcie->ctrl_res),
980 pcie->ctrl = pcie->lut;
983 printf("%s: NOT find CTRL\n", dev->name);
987 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
988 "config", &pcie->cfg_res);
990 printf("%s: resource \"config\" not found\n", dev->name);
994 pcie->cfg0 = map_physmem(pcie->cfg_res.start,
995 fdt_resource_size(&pcie->cfg_res),
997 pcie->cfg1 = pcie->cfg0 + fdt_resource_size(&pcie->cfg_res) / 2;
999 pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
1001 debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
1002 dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
1003 (unsigned long)pcie->ctrl, (unsigned long)pcie->cfg0,
1006 header_type = readb(pcie->dbi + PCI_HEADER_TYPE);
1007 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
1008 printf("PCIe%u: %s %s", pcie->idx, dev->name,
1009 ep_mode ? "Endpoint" : "Root Complex");
1012 ls_pcie_setup_ep(pcie);
1014 ls_pcie_setup_ctrl(pcie);
1016 if (!ls_pcie_link_up(pcie)) {
1017 /* Let the user know there's no PCIe link */
1018 printf(": no link\n");
1022 /* Print the negotiated PCIe link width */
1023 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
1024 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
1025 link_sta & PCIE_LINK_SPEED_MASK);
1030 static const struct dm_pci_ops ls_pcie_ops = {
1031 .read_config = ls_pcie_read_config,
1032 .write_config = ls_pcie_write_config,
1035 static const struct udevice_id ls_pcie_ids[] = {
1036 { .compatible = "fsl,ls-pcie" },
1040 U_BOOT_DRIVER(pci_layerscape) = {
1041 .name = "pci_layerscape",
1043 .of_match = ls_pcie_ids,
1044 .ops = &ls_pcie_ops,
1045 .probe = ls_pcie_probe,
1046 .priv_auto_alloc_size = sizeof(struct ls_pcie),
1048 #endif /* CONFIG_DM_PCI */