1 // SPDX-License-Identifier: GPL-2.0+ OR X11
3 * Copyright 2018-2021 NXP
5 * PCIe Gen4 driver for NXP Layerscape SoCs
14 #include <asm/arch/fsl_serdes.h>
17 #ifdef CONFIG_OF_BOARD_SETUP
18 #include <linux/libfdt.h>
19 #include <fdt_support.h>
21 #include <asm/arch/clock.h>
23 #include "pcie_layerscape_gen4.h"
24 #include "pcie_layerscape_fixup_common.h"
26 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
28 * Return next available LUT index.
30 static int ls_pcie_g4_next_lut_index(struct ls_pcie_g4 *pcie)
32 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
33 return pcie->next_lut_index++;
35 return -ENOSPC; /* LUT is full */
39 * Program a single LUT entry
41 static void ls_pcie_g4_lut_set_mapping(struct ls_pcie_g4 *pcie, int index,
42 u32 devid, u32 streamid)
44 /* leave mask as all zeroes, want to match all bits */
45 lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
46 lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
50 * An msi-map is a property to be added to the pci controller
51 * node. It is a table, where each entry consists of 4 fields
54 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
55 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
57 static void fdt_pcie_set_msi_map_entry_ls_gen4(void *blob,
58 struct ls_pcie_g4 *pcie,
59 u32 devid, u32 streamid)
65 #ifdef CONFIG_FSL_PCIE_COMPAT
66 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
67 pcie->ccsr_res.start);
69 #error "No CONFIG_FSL_PCIE_COMPAT defined"
72 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
76 /* get phandle to MSI controller */
77 prop = (u32 *)fdt_getprop(blob, nodeoff, "msi-parent", 0);
79 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
83 phandle = fdt32_to_cpu(*prop);
85 /* set one msi-map row */
86 fdt_appendprop_u32(blob, nodeoff, "msi-map", devid);
87 fdt_appendprop_u32(blob, nodeoff, "msi-map", phandle);
88 fdt_appendprop_u32(blob, nodeoff, "msi-map", streamid);
89 fdt_appendprop_u32(blob, nodeoff, "msi-map", 1);
93 * An iommu-map is a property to be added to the pci controller
94 * node. It is a table, where each entry consists of 4 fields
97 * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
98 * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
100 static void fdt_pcie_set_iommu_map_entry_ls_gen4(void *blob,
101 struct ls_pcie_g4 *pcie,
102 u32 devid, u32 streamid)
109 #ifdef CONFIG_FSL_PCIE_COMPAT
110 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
111 pcie->ccsr_res.start);
113 #error "No CONFIG_FSL_PCIE_COMPAT defined"
116 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
120 /* get phandle to iommu controller */
121 prop = fdt_getprop_w(blob, nodeoff, "iommu-map", &lenp);
123 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
124 __func__, pcie->idx);
128 /* set iommu-map row */
129 iommu_map[0] = cpu_to_fdt32(devid);
130 iommu_map[1] = *++prop;
131 iommu_map[2] = cpu_to_fdt32(streamid);
132 iommu_map[3] = cpu_to_fdt32(1);
135 fdt_setprop_inplace(blob, nodeoff, "iommu-map", iommu_map, 16);
137 fdt_appendprop(blob, nodeoff, "iommu-map", iommu_map, 16);
140 static void fdt_fixup_pcie_ls_gen4(void *blob)
142 struct udevice *dev, *bus;
143 struct ls_pcie_g4 *pcie;
148 /* Scan all known buses */
149 for (pci_find_first_device(&dev); dev; pci_find_next_device(&dev)) {
150 for (bus = dev; device_is_on_pci_bus(bus);)
152 pcie = dev_get_priv(bus);
154 streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx);
156 debug("ERROR: no stream ids free\n");
159 pcie->stream_id_cur++;
162 index = ls_pcie_g4_next_lut_index(pcie);
164 debug("ERROR: no LUT indexes free\n");
168 /* the DT fixup must be relative to the hose first_busno */
169 bdf = dm_pci_get_bdf(dev) - PCI_BDF(dev_seq(bus), 0, 0);
170 /* map PCI b.d.f to streamID in LUT */
171 ls_pcie_g4_lut_set_mapping(pcie, index, bdf >> 8, streamid);
172 /* update msi-map in device tree */
173 fdt_pcie_set_msi_map_entry_ls_gen4(blob, pcie, bdf >> 8,
175 /* update iommu-map in device tree */
176 fdt_pcie_set_iommu_map_entry_ls_gen4(blob, pcie, bdf >> 8,
182 static void ft_pcie_ep_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
186 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
187 pcie->ccsr_res.start);
190 debug("%s: ERROR: failed to find pcie compatiable\n",
195 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
196 fdt_set_node_status(blob, off, FDT_STATUS_OKAY);
198 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED);
201 static void ft_pcie_rc_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
205 #ifdef CONFIG_FSL_PCIE_COMPAT
206 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
207 pcie->ccsr_res.start);
209 #error "No CONFIG_FSL_PCIE_COMPAT defined"
212 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
216 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
217 fdt_set_node_status(blob, off, FDT_STATUS_OKAY);
219 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED);
222 static void ft_pcie_layerscape_gen4_setup(void *blob, struct ls_pcie_g4 *pcie)
224 ft_pcie_rc_layerscape_gen4_fix(blob, pcie);
225 ft_pcie_ep_layerscape_gen4_fix(blob, pcie);
227 pcie->stream_id_cur = 0;
228 pcie->next_lut_index = 0;
231 /* Fixup Kernel DT for PCIe */
232 void ft_pci_setup_ls_gen4(void *blob, struct bd_info *bd)
234 struct ls_pcie_g4 *pcie;
236 list_for_each_entry(pcie, &ls_pcie_g4_list, list)
237 ft_pcie_layerscape_gen4_setup(blob, pcie);
239 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
240 fdt_fixup_pcie_ls_gen4(blob);
244 #else /* !CONFIG_OF_BOARD_SETUP */
245 void ft_pci_setup_ls_gen4(void *blob, struct bd_info *bd)