1 // SPDX-License-Identifier: GPL-2.0+ OR X11
3 * Copyright 2018-2019 NXP
5 * PCIe Gen4 driver for NXP Layerscape SoCs
12 #include <asm/arch/fsl_serdes.h>
15 #ifdef CONFIG_OF_BOARD_SETUP
16 #include <linux/libfdt.h>
17 #include <fdt_support.h>
19 #include <asm/arch/clock.h>
21 #include "pcie_layerscape_gen4.h"
23 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
25 * Return next available LUT index.
27 static int ls_pcie_g4_next_lut_index(struct ls_pcie_g4 *pcie)
29 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
30 return pcie->next_lut_index++;
32 return -ENOSPC; /* LUT is full */
35 /* returns the next available streamid for pcie, -errno if failed */
36 static int ls_pcie_g4_next_streamid(struct ls_pcie_g4 *pcie)
38 int stream_id = pcie->stream_id_cur;
40 if (stream_id > FSL_PEX_STREAM_ID_END)
43 pcie->stream_id_cur++;
45 return stream_id | ((pcie->idx + 1) << 11);
49 * Program a single LUT entry
51 static void ls_pcie_g4_lut_set_mapping(struct ls_pcie_g4 *pcie, int index,
52 u32 devid, u32 streamid)
54 /* leave mask as all zeroes, want to match all bits */
55 lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
56 lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
60 * An msi-map is a property to be added to the pci controller
61 * node. It is a table, where each entry consists of 4 fields
64 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
65 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
67 static void fdt_pcie_set_msi_map_entry_ls_gen4(void *blob,
68 struct ls_pcie_g4 *pcie,
69 u32 devid, u32 streamid)
75 #ifdef CONFIG_FSL_PCIE_COMPAT
76 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
77 pcie->ccsr_res.start);
79 #error "No CONFIG_FSL_PCIE_COMPAT defined"
82 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
86 /* get phandle to MSI controller */
87 prop = (u32 *)fdt_getprop(blob, nodeoff, "msi-parent", 0);
89 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
93 phandle = fdt32_to_cpu(*prop);
95 /* set one msi-map row */
96 fdt_appendprop_u32(blob, nodeoff, "msi-map", devid);
97 fdt_appendprop_u32(blob, nodeoff, "msi-map", phandle);
98 fdt_appendprop_u32(blob, nodeoff, "msi-map", streamid);
99 fdt_appendprop_u32(blob, nodeoff, "msi-map", 1);
103 * An iommu-map is a property to be added to the pci controller
104 * node. It is a table, where each entry consists of 4 fields
107 * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
108 * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
110 static void fdt_pcie_set_iommu_map_entry_ls_gen4(void *blob,
111 struct ls_pcie_g4 *pcie,
112 u32 devid, u32 streamid)
119 #ifdef CONFIG_FSL_PCIE_COMPAT
120 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
121 pcie->ccsr_res.start);
123 #error "No CONFIG_FSL_PCIE_COMPAT defined"
126 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
130 /* get phandle to iommu controller */
131 prop = fdt_getprop_w(blob, nodeoff, "iommu-map", &lenp);
133 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
134 __func__, pcie->idx);
138 /* set iommu-map row */
139 iommu_map[0] = cpu_to_fdt32(devid);
140 iommu_map[1] = *++prop;
141 iommu_map[2] = cpu_to_fdt32(streamid);
142 iommu_map[3] = cpu_to_fdt32(1);
145 fdt_setprop_inplace(blob, nodeoff, "iommu-map", iommu_map, 16);
147 fdt_appendprop(blob, nodeoff, "iommu-map", iommu_map, 16);
150 static void fdt_fixup_pcie_ls_gen4(void *blob)
152 struct udevice *dev, *bus;
153 struct ls_pcie_g4 *pcie;
158 /* Scan all known buses */
159 for (pci_find_first_device(&dev); dev; pci_find_next_device(&dev)) {
160 for (bus = dev; device_is_on_pci_bus(bus);)
162 pcie = dev_get_priv(bus);
164 streamid = ls_pcie_g4_next_streamid(pcie);
166 debug("ERROR: no stream ids free\n");
170 index = ls_pcie_g4_next_lut_index(pcie);
172 debug("ERROR: no LUT indexes free\n");
176 /* the DT fixup must be relative to the hose first_busno */
177 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
178 /* map PCI b.d.f to streamID in LUT */
179 ls_pcie_g4_lut_set_mapping(pcie, index, bdf >> 8, streamid);
180 /* update msi-map in device tree */
181 fdt_pcie_set_msi_map_entry_ls_gen4(blob, pcie, bdf >> 8,
183 /* update iommu-map in device tree */
184 fdt_pcie_set_iommu_map_entry_ls_gen4(blob, pcie, bdf >> 8,
190 static void ft_pcie_ep_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
194 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
195 pcie->ccsr_res.start);
198 debug("%s: ERROR: failed to find pcie compatiable\n",
203 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
204 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
206 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
209 static void ft_pcie_rc_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
213 #ifdef CONFIG_FSL_PCIE_COMPAT
214 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
215 pcie->ccsr_res.start);
217 #error "No CONFIG_FSL_PCIE_COMPAT defined"
220 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
224 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
225 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
227 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
230 static void ft_pcie_layerscape_gen4_setup(void *blob, struct ls_pcie_g4 *pcie)
232 ft_pcie_rc_layerscape_gen4_fix(blob, pcie);
233 ft_pcie_ep_layerscape_gen4_fix(blob, pcie);
236 /* Fixup Kernel DT for PCIe */
237 void ft_pci_setup(void *blob, bd_t *bd)
239 struct ls_pcie_g4 *pcie;
241 list_for_each_entry(pcie, &ls_pcie_g4_list, list)
242 ft_pcie_layerscape_gen4_setup(blob, pcie);
244 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
245 fdt_fixup_pcie_ls_gen4(blob);
249 #else /* !CONFIG_OF_BOARD_SETUP */
250 void ft_pci_setup(void *blob, bd_t *bd)