1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright 2014-2015 Freescale Semiconductor, Inc.
5 * Layerscape PCIe driver
9 #include <asm/arch/fsl_serdes.h>
15 #if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
17 #include <asm/arch/clock.h>
19 #include "pcie_layerscape.h"
21 DECLARE_GLOBAL_DATA_PTR;
23 LIST_HEAD(ls_pcie_list);
25 static unsigned int dbi_readl(struct ls_pcie *pcie, unsigned int offset)
27 return in_le32(pcie->dbi + offset);
30 static void dbi_writel(struct ls_pcie *pcie, unsigned int value,
33 out_le32(pcie->dbi + offset, value);
36 static unsigned int ctrl_readl(struct ls_pcie *pcie, unsigned int offset)
39 return in_be32(pcie->ctrl + offset);
41 return in_le32(pcie->ctrl + offset);
44 static void ctrl_writel(struct ls_pcie *pcie, unsigned int value,
48 out_be32(pcie->ctrl + offset, value);
50 out_le32(pcie->ctrl + offset, value);
53 static int ls_pcie_ltssm(struct ls_pcie *pcie)
59 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
60 state = ctrl_readl(pcie, LS1021_PEXMSCPORTSR(pcie->idx));
61 state = (state >> LS1021_LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
63 state = ctrl_readl(pcie, PCIE_PF_DBG) & LTSSM_STATE_MASK;
69 static int ls_pcie_link_up(struct ls_pcie *pcie)
73 ltssm = ls_pcie_ltssm(pcie);
74 if (ltssm < LTSSM_PCIE_L0)
80 static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
82 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
84 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
87 static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
89 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
91 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
94 static void ls_pcie_atu_outbound_set(struct ls_pcie *pcie, int idx, int type,
95 u64 phys, u64 bus_addr, pci_size_t size)
97 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | idx, PCIE_ATU_VIEWPORT);
98 dbi_writel(pcie, (u32)phys, PCIE_ATU_LOWER_BASE);
99 dbi_writel(pcie, phys >> 32, PCIE_ATU_UPPER_BASE);
100 dbi_writel(pcie, (u32)phys + size - 1, PCIE_ATU_LIMIT);
101 dbi_writel(pcie, (u32)bus_addr, PCIE_ATU_LOWER_TARGET);
102 dbi_writel(pcie, bus_addr >> 32, PCIE_ATU_UPPER_TARGET);
103 dbi_writel(pcie, type, PCIE_ATU_CR1);
104 dbi_writel(pcie, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
107 /* Use bar match mode and MEM type as default */
108 static void ls_pcie_atu_inbound_set(struct ls_pcie *pcie, int idx,
111 dbi_writel(pcie, PCIE_ATU_REGION_INBOUND | idx, PCIE_ATU_VIEWPORT);
112 dbi_writel(pcie, (u32)phys, PCIE_ATU_LOWER_TARGET);
113 dbi_writel(pcie, phys >> 32, PCIE_ATU_UPPER_TARGET);
114 dbi_writel(pcie, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
115 dbi_writel(pcie, PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
116 PCIE_ATU_BAR_NUM(bar), PCIE_ATU_CR2);
119 static void ls_pcie_dump_atu(struct ls_pcie *pcie)
123 for (i = 0; i < PCIE_ATU_REGION_NUM; i++) {
124 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | i,
126 debug("iATU%d:\n", i);
127 debug("\tLOWER PHYS 0x%08x\n",
128 dbi_readl(pcie, PCIE_ATU_LOWER_BASE));
129 debug("\tUPPER PHYS 0x%08x\n",
130 dbi_readl(pcie, PCIE_ATU_UPPER_BASE));
131 debug("\tLOWER BUS 0x%08x\n",
132 dbi_readl(pcie, PCIE_ATU_LOWER_TARGET));
133 debug("\tUPPER BUS 0x%08x\n",
134 dbi_readl(pcie, PCIE_ATU_UPPER_TARGET));
135 debug("\tLIMIT 0x%08x\n",
136 readl(pcie->dbi + PCIE_ATU_LIMIT));
137 debug("\tCR1 0x%08x\n",
138 dbi_readl(pcie, PCIE_ATU_CR1));
139 debug("\tCR2 0x%08x\n",
140 dbi_readl(pcie, PCIE_ATU_CR2));
144 static void ls_pcie_setup_atu(struct ls_pcie *pcie)
146 struct pci_region *io, *mem, *pref;
147 unsigned long long offset = 0;
152 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
153 offset = LS1021_PCIE_SPACE_OFFSET +
154 LS1021_PCIE_SPACE_SIZE * pcie->idx;
157 /* ATU 0 : OUTBOUND : CFG0 */
158 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
160 pcie->cfg_res.start + offset,
162 fdt_resource_size(&pcie->cfg_res) / 2);
163 /* ATU 1 : OUTBOUND : CFG1 */
164 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
166 pcie->cfg_res.start + offset +
167 fdt_resource_size(&pcie->cfg_res) / 2,
169 fdt_resource_size(&pcie->cfg_res) / 2);
171 pci_get_regions(pcie->bus, &io, &mem, &pref);
172 idx = PCIE_ATU_REGION_INDEX1 + 1;
174 /* Fix the pcie memory map for LS2088A series SoCs */
175 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
176 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
177 svr == SVR_LS2048A || svr == SVR_LS2044A ||
178 svr == SVR_LS2081A || svr == SVR_LS2041A) {
180 io->phys_start = (io->phys_start &
181 (PCIE_PHYS_SIZE - 1)) +
182 LS2088A_PCIE1_PHYS_ADDR +
183 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
185 mem->phys_start = (mem->phys_start &
186 (PCIE_PHYS_SIZE - 1)) +
187 LS2088A_PCIE1_PHYS_ADDR +
188 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
190 pref->phys_start = (pref->phys_start &
191 (PCIE_PHYS_SIZE - 1)) +
192 LS2088A_PCIE1_PHYS_ADDR +
193 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
197 /* ATU : OUTBOUND : IO */
198 ls_pcie_atu_outbound_set(pcie, idx++,
200 io->phys_start + offset,
205 /* ATU : OUTBOUND : MEM */
206 ls_pcie_atu_outbound_set(pcie, idx++,
208 mem->phys_start + offset,
213 /* ATU : OUTBOUND : pref */
214 ls_pcie_atu_outbound_set(pcie, idx++,
216 pref->phys_start + offset,
220 ls_pcie_dump_atu(pcie);
223 /* Return 0 if the address is valid, -errno if not valid */
224 static int ls_pcie_addr_valid(struct ls_pcie *pcie, pci_dev_t bdf)
226 struct udevice *bus = pcie->bus;
231 if (PCI_BUS(bdf) < bus->seq)
234 if ((PCI_BUS(bdf) > bus->seq) && (!ls_pcie_link_up(pcie)))
237 if (PCI_BUS(bdf) <= (bus->seq + 1) && (PCI_DEV(bdf) > 0))
243 int ls_pcie_conf_address(struct udevice *bus, pci_dev_t bdf,
244 uint offset, void **paddress)
246 struct ls_pcie *pcie = dev_get_priv(bus);
249 if (ls_pcie_addr_valid(pcie, bdf))
252 if (PCI_BUS(bdf) == bus->seq) {
253 *paddress = pcie->dbi + offset;
257 busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - bus->seq) |
258 PCIE_ATU_DEV(PCI_DEV(bdf)) |
259 PCIE_ATU_FUNC(PCI_FUNC(bdf));
261 if (PCI_BUS(bdf) == bus->seq + 1) {
262 ls_pcie_cfg0_set_busdev(pcie, busdev);
263 *paddress = pcie->cfg0 + offset;
265 ls_pcie_cfg1_set_busdev(pcie, busdev);
266 *paddress = pcie->cfg1 + offset;
271 static int ls_pcie_read_config(struct udevice *bus, pci_dev_t bdf,
272 uint offset, ulong *valuep,
273 enum pci_size_t size)
275 return pci_generic_mmap_read_config(bus, ls_pcie_conf_address,
276 bdf, offset, valuep, size);
279 static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
280 uint offset, ulong value,
281 enum pci_size_t size)
283 return pci_generic_mmap_write_config(bus, ls_pcie_conf_address,
284 bdf, offset, value, size);
287 /* Clear multi-function bit */
288 static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
290 writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
293 /* Fix class value */
294 static void ls_pcie_fix_class(struct ls_pcie *pcie)
296 writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
299 /* Drop MSG TLP except for Vendor MSG */
300 static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
304 val = dbi_readl(pcie, PCIE_STRFMR1);
306 dbi_writel(pcie, val, PCIE_STRFMR1);
309 /* Disable all bars in RC mode */
310 static void ls_pcie_disable_bars(struct ls_pcie *pcie)
314 sriov = in_le32(pcie->dbi + PCIE_SRIOV);
317 * TODO: For PCIe controller with SRIOV, the method to disable bars
318 * is different and more complex, so will add later.
320 if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV)
323 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
324 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
325 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
328 static void ls_pcie_setup_ctrl(struct ls_pcie *pcie)
330 ls_pcie_setup_atu(pcie);
332 dbi_writel(pcie, 1, PCIE_DBI_RO_WR_EN);
333 ls_pcie_fix_class(pcie);
334 ls_pcie_clear_multifunction(pcie);
335 ls_pcie_drop_msg_tlp(pcie);
336 dbi_writel(pcie, 0, PCIE_DBI_RO_WR_EN);
338 ls_pcie_disable_bars(pcie);
341 static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie)
343 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
345 /* ATU 0 : INBOUND : map BAR0 */
346 ls_pcie_atu_inbound_set(pcie, 0, 0, phys);
347 /* ATU 1 : INBOUND : map BAR1 */
348 phys += PCIE_BAR1_SIZE;
349 ls_pcie_atu_inbound_set(pcie, 1, 1, phys);
350 /* ATU 2 : INBOUND : map BAR2 */
351 phys += PCIE_BAR2_SIZE;
352 ls_pcie_atu_inbound_set(pcie, 2, 2, phys);
353 /* ATU 3 : INBOUND : map BAR4 */
354 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
355 ls_pcie_atu_inbound_set(pcie, 3, 4, phys);
357 /* ATU 0 : OUTBOUND : map MEM */
358 ls_pcie_atu_outbound_set(pcie, 0,
362 CONFIG_SYS_PCI_MEMORY_SIZE);
365 /* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
366 static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
368 /* The least inbound window is 4KiB */
374 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
377 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
380 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
381 writel(0, bar_base + PCI_BASE_ADDRESS_3);
384 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
385 writel(0, bar_base + PCI_BASE_ADDRESS_5);
392 static void ls_pcie_ep_setup_bars(void *bar_base)
394 /* BAR0 - 32bit - 4K configuration */
395 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
396 /* BAR1 - 32bit - 8K MSIX*/
397 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
398 /* BAR2 - 64bit - 4K MEM desciptor */
399 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
400 /* BAR4 - 64bit - 1M MEM*/
401 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
404 static void ls_pcie_ep_enable_cfg(struct ls_pcie *pcie)
406 ctrl_writel(pcie, PCIE_CONFIG_READY, PCIE_PF_CONFIG);
409 static void ls_pcie_setup_ep(struct ls_pcie *pcie)
413 sriov = readl(pcie->dbi + PCIE_SRIOV);
414 if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV) {
417 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
418 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
419 ctrl_writel(pcie, PCIE_LCTRL0_VAL(pf, vf),
422 ls_pcie_ep_setup_bars(pcie->dbi);
423 ls_pcie_ep_setup_atu(pcie);
427 ctrl_writel(pcie, 0, PCIE_PF_VF_CTRL);
429 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
430 ls_pcie_ep_setup_atu(pcie);
433 ls_pcie_ep_enable_cfg(pcie);
436 static int ls_pcie_probe(struct udevice *dev)
438 struct ls_pcie *pcie = dev_get_priv(dev);
439 const void *fdt = gd->fdt_blob;
440 int node = dev_of_offset(dev);
450 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
451 "dbi", &pcie->dbi_res);
453 printf("ls-pcie: resource \"dbi\" not found\n");
457 pcie->idx = (pcie->dbi_res.start - PCIE_SYS_BASE_ADDR) / PCIE_CCSR_SIZE;
459 list_add(&pcie->list, &ls_pcie_list);
461 pcie->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
462 if (!pcie->enabled) {
463 printf("PCIe%d: %s disabled\n", pcie->idx, dev->name);
467 pcie->dbi = map_physmem(pcie->dbi_res.start,
468 fdt_resource_size(&pcie->dbi_res),
471 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
472 "lut", &pcie->lut_res);
474 pcie->lut = map_physmem(pcie->lut_res.start,
475 fdt_resource_size(&pcie->lut_res),
478 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
479 "ctrl", &pcie->ctrl_res);
481 pcie->ctrl = map_physmem(pcie->ctrl_res.start,
482 fdt_resource_size(&pcie->ctrl_res),
485 pcie->ctrl = pcie->lut;
488 printf("%s: NOT find CTRL\n", dev->name);
492 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
493 "config", &pcie->cfg_res);
495 printf("%s: resource \"config\" not found\n", dev->name);
500 * Fix the pcie memory map address and PF control registers address
501 * for LS2088A series SoCs
504 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
505 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
506 svr == SVR_LS2048A || svr == SVR_LS2044A ||
507 svr == SVR_LS2081A || svr == SVR_LS2041A) {
508 cfg_size = fdt_resource_size(&pcie->cfg_res);
509 pcie->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR +
510 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
511 pcie->cfg_res.end = pcie->cfg_res.start + cfg_size;
512 pcie->ctrl = pcie->lut + 0x40000;
515 pcie->cfg0 = map_physmem(pcie->cfg_res.start,
516 fdt_resource_size(&pcie->cfg_res),
518 pcie->cfg1 = pcie->cfg0 + fdt_resource_size(&pcie->cfg_res) / 2;
520 pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
522 debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
523 dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
524 (unsigned long)pcie->ctrl, (unsigned long)pcie->cfg0,
527 header_type = readb(pcie->dbi + PCI_HEADER_TYPE);
528 ep_mode = (header_type & 0x7f) == PCI_HEADER_TYPE_NORMAL;
529 printf("PCIe%u: %s %s", pcie->idx, dev->name,
530 ep_mode ? "Endpoint" : "Root Complex");
533 ls_pcie_setup_ep(pcie);
535 ls_pcie_setup_ctrl(pcie);
537 if (!ls_pcie_link_up(pcie)) {
538 /* Let the user know there's no PCIe link */
539 printf(": no link\n");
543 /* Print the negotiated PCIe link width */
544 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
545 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
546 link_sta & PCIE_LINK_SPEED_MASK);
551 static const struct dm_pci_ops ls_pcie_ops = {
552 .read_config = ls_pcie_read_config,
553 .write_config = ls_pcie_write_config,
556 static const struct udevice_id ls_pcie_ids[] = {
557 { .compatible = "fsl,ls-pcie" },
561 U_BOOT_DRIVER(pci_layerscape) = {
562 .name = "pci_layerscape",
564 .of_match = ls_pcie_ids,
566 .probe = ls_pcie_probe,
567 .priv_auto_alloc_size = sizeof(struct ls_pcie),