blob: bfe197e7d83f4de4cdd72507dcb1e6fac2b4e6ac [file] [log] [blame]
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +00001// SPDX-License-Identifier: GPL-2.0+ OR X11
2/*
Wasim Khan54e44ef2020-01-06 12:05:57 +00003 * Copyright 2018-2020 NXP
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +00004 *
5 * PCIe Gen4 driver for NXP Layerscape SoCs
6 * Author: Hou Zhiqiang <Minder.Hou@gmail.com>
7 *
8 */
9
10#include <common.h>
11#include <pci.h>
12#include <asm/arch/fsl_serdes.h>
13#include <asm/io.h>
14#include <errno.h>
15#ifdef CONFIG_OF_BOARD_SETUP
16#include <linux/libfdt.h>
17#include <fdt_support.h>
18#ifdef CONFIG_ARM
19#include <asm/arch/clock.h>
20#endif
21#include "pcie_layerscape_gen4.h"
Wasim Khan54e44ef2020-01-06 12:05:57 +000022#include "pcie_layerscape_fixup_common.h"
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +000023
24#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
25/*
26 * Return next available LUT index.
27 */
28static int ls_pcie_g4_next_lut_index(struct ls_pcie_g4 *pcie)
29{
30 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
31 return pcie->next_lut_index++;
32
33 return -ENOSPC; /* LUT is full */
34}
35
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +000036/*
37 * Program a single LUT entry
38 */
39static void ls_pcie_g4_lut_set_mapping(struct ls_pcie_g4 *pcie, int index,
40 u32 devid, u32 streamid)
41{
42 /* leave mask as all zeroes, want to match all bits */
43 lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
44 lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
45}
46
47/*
48 * An msi-map is a property to be added to the pci controller
49 * node. It is a table, where each entry consists of 4 fields
50 * e.g.:
51 *
52 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
53 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
54 */
Wasim Khanb1ec2812019-11-15 09:23:37 +000055static void fdt_pcie_set_msi_map_entry_ls_gen4(void *blob,
56 struct ls_pcie_g4 *pcie,
57 u32 devid, u32 streamid)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +000058{
59 u32 *prop;
60 u32 phandle;
61 int nodeoff;
62
63#ifdef CONFIG_FSL_PCIE_COMPAT
64 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
65 pcie->ccsr_res.start);
66#else
67#error "No CONFIG_FSL_PCIE_COMPAT defined"
68#endif
69 if (nodeoff < 0) {
70 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
71 return;
72 }
73
74 /* get phandle to MSI controller */
75 prop = (u32 *)fdt_getprop(blob, nodeoff, "msi-parent", 0);
76 if (!prop) {
77 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
78 __func__, pcie->idx);
79 return;
80 }
81 phandle = fdt32_to_cpu(*prop);
82
83 /* set one msi-map row */
84 fdt_appendprop_u32(blob, nodeoff, "msi-map", devid);
85 fdt_appendprop_u32(blob, nodeoff, "msi-map", phandle);
86 fdt_appendprop_u32(blob, nodeoff, "msi-map", streamid);
87 fdt_appendprop_u32(blob, nodeoff, "msi-map", 1);
88}
89
90/*
91 * An iommu-map is a property to be added to the pci controller
92 * node. It is a table, where each entry consists of 4 fields
93 * e.g.:
94 *
95 * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
96 * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
97 */
Wasim Khanb1ec2812019-11-15 09:23:37 +000098static void fdt_pcie_set_iommu_map_entry_ls_gen4(void *blob,
99 struct ls_pcie_g4 *pcie,
100 u32 devid, u32 streamid)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000101{
102 u32 *prop;
103 u32 iommu_map[4];
104 int nodeoff;
105 int lenp;
106
107#ifdef CONFIG_FSL_PCIE_COMPAT
108 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
109 pcie->ccsr_res.start);
110#else
111#error "No CONFIG_FSL_PCIE_COMPAT defined"
112#endif
113 if (nodeoff < 0) {
114 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
115 return;
116 }
117
118 /* get phandle to iommu controller */
119 prop = fdt_getprop_w(blob, nodeoff, "iommu-map", &lenp);
120 if (!prop) {
121 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
122 __func__, pcie->idx);
123 return;
124 }
125
126 /* set iommu-map row */
127 iommu_map[0] = cpu_to_fdt32(devid);
128 iommu_map[1] = *++prop;
129 iommu_map[2] = cpu_to_fdt32(streamid);
130 iommu_map[3] = cpu_to_fdt32(1);
131
132 if (devid == 0)
133 fdt_setprop_inplace(blob, nodeoff, "iommu-map", iommu_map, 16);
134 else
135 fdt_appendprop(blob, nodeoff, "iommu-map", iommu_map, 16);
136}
137
Wasim Khanb1ec2812019-11-15 09:23:37 +0000138static void fdt_fixup_pcie_ls_gen4(void *blob)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000139{
140 struct udevice *dev, *bus;
141 struct ls_pcie_g4 *pcie;
142 int streamid;
143 int index;
144 pci_dev_t bdf;
145
146 /* Scan all known buses */
147 for (pci_find_first_device(&dev); dev; pci_find_next_device(&dev)) {
148 for (bus = dev; device_is_on_pci_bus(bus);)
149 bus = bus->parent;
150 pcie = dev_get_priv(bus);
151
Wasim Khan9d3d2302020-01-06 12:05:59 +0000152 streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000153 if (streamid < 0) {
154 debug("ERROR: no stream ids free\n");
155 continue;
Wasim Khan9d3d2302020-01-06 12:05:59 +0000156 } else {
157 pcie->stream_id_cur++;
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000158 }
159
160 index = ls_pcie_g4_next_lut_index(pcie);
161 if (index < 0) {
162 debug("ERROR: no LUT indexes free\n");
163 continue;
164 }
165
166 /* the DT fixup must be relative to the hose first_busno */
167 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
168 /* map PCI b.d.f to streamID in LUT */
169 ls_pcie_g4_lut_set_mapping(pcie, index, bdf >> 8, streamid);
170 /* update msi-map in device tree */
Wasim Khanb1ec2812019-11-15 09:23:37 +0000171 fdt_pcie_set_msi_map_entry_ls_gen4(blob, pcie, bdf >> 8,
172 streamid);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000173 /* update iommu-map in device tree */
Wasim Khanb1ec2812019-11-15 09:23:37 +0000174 fdt_pcie_set_iommu_map_entry_ls_gen4(blob, pcie, bdf >> 8,
175 streamid);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000176 }
177}
178#endif
179
180static void ft_pcie_ep_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
181{
182 int off;
183
Pankaj Bansal64d85a22019-11-30 13:14:10 +0000184 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000185 pcie->ccsr_res.start);
186
187 if (off < 0) {
188 debug("%s: ERROR: failed to find pcie compatiable\n",
189 __func__);
190 return;
191 }
192
193 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
194 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
195 else
196 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
197}
198
199static void ft_pcie_rc_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
200{
201 int off;
202
203#ifdef CONFIG_FSL_PCIE_COMPAT
204 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
205 pcie->ccsr_res.start);
206#else
207#error "No CONFIG_FSL_PCIE_COMPAT defined"
208#endif
209 if (off < 0) {
210 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
211 return;
212 }
213
214 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
215 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
216 else
217 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
218}
219
220static void ft_pcie_layerscape_gen4_setup(void *blob, struct ls_pcie_g4 *pcie)
221{
222 ft_pcie_rc_layerscape_gen4_fix(blob, pcie);
223 ft_pcie_ep_layerscape_gen4_fix(blob, pcie);
224}
225
226/* Fixup Kernel DT for PCIe */
Wasim Khan54e44ef2020-01-06 12:05:57 +0000227void ft_pci_setup_ls_gen4(void *blob, bd_t *bd)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000228{
229 struct ls_pcie_g4 *pcie;
230
231 list_for_each_entry(pcie, &ls_pcie_g4_list, list)
232 ft_pcie_layerscape_gen4_setup(blob, pcie);
233
234#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Wasim Khanb1ec2812019-11-15 09:23:37 +0000235 fdt_fixup_pcie_ls_gen4(blob);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000236#endif
237}
238
239#else /* !CONFIG_OF_BOARD_SETUP */
Wasim Khan54e44ef2020-01-06 12:05:57 +0000240void ft_pci_setup_ls_gen4(void *blob, bd_t *bd)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000241{
242}
243#endif