blob: da9817159fdca8b67927c6bb424df95129aaf853 [file] [log] [blame]
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +00001// SPDX-License-Identifier: GPL-2.0+ OR X11
2/*
3 * Copyright 2018-2019 NXP
4 *
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"
22
23#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
24/*
25 * Return next available LUT index.
26 */
27static int ls_pcie_g4_next_lut_index(struct ls_pcie_g4 *pcie)
28{
29 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
30 return pcie->next_lut_index++;
31
32 return -ENOSPC; /* LUT is full */
33}
34
35/* returns the next available streamid for pcie, -errno if failed */
36static int ls_pcie_g4_next_streamid(struct ls_pcie_g4 *pcie)
37{
38 int stream_id = pcie->stream_id_cur;
39
Wasim Khanf6cbc732019-11-15 09:23:39 +000040 if (stream_id > FSL_PEX_STREAM_ID_END)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +000041 return -EINVAL;
42
43 pcie->stream_id_cur++;
44
45 return stream_id | ((pcie->idx + 1) << 11);
46}
47
48/*
49 * Program a single LUT entry
50 */
51static void ls_pcie_g4_lut_set_mapping(struct ls_pcie_g4 *pcie, int index,
52 u32 devid, u32 streamid)
53{
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));
57}
58
59/*
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
62 * e.g.:
63 *
64 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
65 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
66 */
Wasim Khanb1ec2812019-11-15 09:23:37 +000067static void fdt_pcie_set_msi_map_entry_ls_gen4(void *blob,
68 struct ls_pcie_g4 *pcie,
69 u32 devid, u32 streamid)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +000070{
71 u32 *prop;
72 u32 phandle;
73 int nodeoff;
74
75#ifdef CONFIG_FSL_PCIE_COMPAT
76 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
77 pcie->ccsr_res.start);
78#else
79#error "No CONFIG_FSL_PCIE_COMPAT defined"
80#endif
81 if (nodeoff < 0) {
82 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
83 return;
84 }
85
86 /* get phandle to MSI controller */
87 prop = (u32 *)fdt_getprop(blob, nodeoff, "msi-parent", 0);
88 if (!prop) {
89 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
90 __func__, pcie->idx);
91 return;
92 }
93 phandle = fdt32_to_cpu(*prop);
94
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);
100}
101
102/*
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
105 * e.g.:
106 *
107 * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
108 * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
109 */
Wasim Khanb1ec2812019-11-15 09:23:37 +0000110static void fdt_pcie_set_iommu_map_entry_ls_gen4(void *blob,
111 struct ls_pcie_g4 *pcie,
112 u32 devid, u32 streamid)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000113{
114 u32 *prop;
115 u32 iommu_map[4];
116 int nodeoff;
117 int lenp;
118
119#ifdef CONFIG_FSL_PCIE_COMPAT
120 nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
121 pcie->ccsr_res.start);
122#else
123#error "No CONFIG_FSL_PCIE_COMPAT defined"
124#endif
125 if (nodeoff < 0) {
126 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
127 return;
128 }
129
130 /* get phandle to iommu controller */
131 prop = fdt_getprop_w(blob, nodeoff, "iommu-map", &lenp);
132 if (!prop) {
133 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
134 __func__, pcie->idx);
135 return;
136 }
137
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);
143
144 if (devid == 0)
145 fdt_setprop_inplace(blob, nodeoff, "iommu-map", iommu_map, 16);
146 else
147 fdt_appendprop(blob, nodeoff, "iommu-map", iommu_map, 16);
148}
149
Wasim Khanb1ec2812019-11-15 09:23:37 +0000150static void fdt_fixup_pcie_ls_gen4(void *blob)
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000151{
152 struct udevice *dev, *bus;
153 struct ls_pcie_g4 *pcie;
154 int streamid;
155 int index;
156 pci_dev_t bdf;
157
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);)
161 bus = bus->parent;
162 pcie = dev_get_priv(bus);
163
164 streamid = ls_pcie_g4_next_streamid(pcie);
165 if (streamid < 0) {
166 debug("ERROR: no stream ids free\n");
167 continue;
168 }
169
170 index = ls_pcie_g4_next_lut_index(pcie);
171 if (index < 0) {
172 debug("ERROR: no LUT indexes free\n");
173 continue;
174 }
175
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 */
Wasim Khanb1ec2812019-11-15 09:23:37 +0000181 fdt_pcie_set_msi_map_entry_ls_gen4(blob, pcie, bdf >> 8,
182 streamid);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000183 /* update iommu-map in device tree */
Wasim Khanb1ec2812019-11-15 09:23:37 +0000184 fdt_pcie_set_iommu_map_entry_ls_gen4(blob, pcie, bdf >> 8,
185 streamid);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000186 }
187}
188#endif
189
190static void ft_pcie_ep_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
191{
192 int off;
193
Pankaj Bansal64d85a22019-11-30 13:14:10 +0000194 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000195 pcie->ccsr_res.start);
196
197 if (off < 0) {
198 debug("%s: ERROR: failed to find pcie compatiable\n",
199 __func__);
200 return;
201 }
202
203 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
204 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
205 else
206 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
207}
208
209static void ft_pcie_rc_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
210{
211 int off;
212
213#ifdef CONFIG_FSL_PCIE_COMPAT
214 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
215 pcie->ccsr_res.start);
216#else
217#error "No CONFIG_FSL_PCIE_COMPAT defined"
218#endif
219 if (off < 0) {
220 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
221 return;
222 }
223
224 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
225 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
226 else
227 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
228}
229
230static void ft_pcie_layerscape_gen4_setup(void *blob, struct ls_pcie_g4 *pcie)
231{
232 ft_pcie_rc_layerscape_gen4_fix(blob, pcie);
233 ft_pcie_ep_layerscape_gen4_fix(blob, pcie);
234}
235
236/* Fixup Kernel DT for PCIe */
237void ft_pci_setup(void *blob, bd_t *bd)
238{
239 struct ls_pcie_g4 *pcie;
240
241 list_for_each_entry(pcie, &ls_pcie_g4_list, list)
242 ft_pcie_layerscape_gen4_setup(blob, pcie);
243
244#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Wasim Khanb1ec2812019-11-15 09:23:37 +0000245 fdt_fixup_pcie_ls_gen4(blob);
Hou Zhiqiang5a9a5ed2019-04-08 10:15:54 +0000246#endif
247}
248
249#else /* !CONFIG_OF_BOARD_SETUP */
250void ft_pci_setup(void *blob, bd_t *bd)
251{
252}
253#endif