2 * Copyright 2014-2015 Freescale Semiconductor, Inc.
3 * Layerscape PCIe driver
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/arch/fsl_serdes.h>
13 #ifdef CONFIG_OF_BOARD_SETUP
15 #include <fdt_support.h>
16 #include "pcie_layerscape.h"
18 #ifdef CONFIG_FSL_LSCH3
20 * Return next available LUT index.
22 static int ls_pcie_next_lut_index(struct ls_pcie *pcie)
24 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
25 return pcie->next_lut_index++;
27 return -ENOSPC; /* LUT is full */
30 /* returns the next available streamid for pcie, -errno if failed */
31 static int ls_pcie_next_streamid(void)
33 static int next_stream_id = FSL_PEX_STREAM_ID_START;
35 if (next_stream_id > FSL_PEX_STREAM_ID_END)
38 return next_stream_id++;
41 static void lut_writel(struct ls_pcie *pcie, unsigned int value,
45 out_be32(pcie->lut + offset, value);
47 out_le32(pcie->lut + offset, value);
51 * Program a single LUT entry
53 static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid,
56 /* leave mask as all zeroes, want to match all bits */
57 lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
58 lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
62 * An msi-map is a property to be added to the pci controller
63 * node. It is a table, where each entry consists of 4 fields
66 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
67 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
69 static void fdt_pcie_set_msi_map_entry(void *blob, struct ls_pcie *pcie,
70 u32 devid, u32 streamid)
76 /* find pci controller node */
77 nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
80 #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
81 nodeoffset = fdt_node_offset_by_compat_reg(blob,
82 CONFIG_FSL_PCIE_COMPAT, pcie->dbi_res.start);
90 /* get phandle to MSI controller */
91 prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0);
93 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
97 phandle = fdt32_to_cpu(*prop);
99 /* set one msi-map row */
100 fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid);
101 fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle);
102 fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid);
103 fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1);
106 static void fdt_fixup_pcie(void *blob)
108 struct udevice *dev, *bus;
109 struct ls_pcie *pcie;
114 /* Scan all known buses */
115 for (pci_find_first_device(&dev);
117 pci_find_next_device(&dev)) {
118 for (bus = dev; device_is_on_pci_bus(bus);)
120 pcie = dev_get_priv(bus);
122 streamid = ls_pcie_next_streamid();
124 debug("ERROR: no stream ids free\n");
128 index = ls_pcie_next_lut_index(pcie);
130 debug("ERROR: no LUT indexes free\n");
134 /* the DT fixup must be relative to the hose first_busno */
135 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
136 /* map PCI b.d.f to streamID in LUT */
137 ls_pcie_lut_set_mapping(pcie, index, bdf >> 8,
139 /* update msi-map in device tree */
140 fdt_pcie_set_msi_map_entry(blob, pcie, bdf >> 8,
146 static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie)
150 off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
151 pcie->dbi_res.start);
153 #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
154 off = fdt_node_offset_by_compat_reg(blob,
155 CONFIG_FSL_PCIE_COMPAT,
156 pcie->dbi_res.start);
165 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
167 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
170 /* Fixup Kernel DT for PCIe */
171 void ft_pci_setup(void *blob, bd_t *bd)
173 struct ls_pcie *pcie;
175 list_for_each_entry(pcie, &ls_pcie_list, list)
176 ft_pcie_ls_setup(blob, pcie);
178 #ifdef CONFIG_FSL_LSCH3
179 fdt_fixup_pcie(blob);
183 #else /* !CONFIG_OF_BOARD_SETUP */
184 void ft_pci_setup(void *blob, bd_t *bd)