kconfig / kbuild: Re-sync with Linux 4.19
[oweals/u-boot.git] / drivers / pci / pcie_layerscape_fixup.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2017-2020 NXP
4  * Copyright 2014-2015 Freescale Semiconductor, Inc.
5  * Layerscape PCIe driver
6  */
7
8 #include <common.h>
9 #include <pci.h>
10 #include <asm/arch/fsl_serdes.h>
11 #include <asm/io.h>
12 #include <errno.h>
13 #ifdef CONFIG_OF_BOARD_SETUP
14 #include <linux/libfdt.h>
15 #include <fdt_support.h>
16 #ifdef CONFIG_ARM
17 #include <asm/arch/clock.h>
18 #endif
19 #include "pcie_layerscape.h"
20 #include "pcie_layerscape_fixup_common.h"
21
22 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
23 /*
24  * Return next available LUT index.
25  */
26 static int ls_pcie_next_lut_index(struct ls_pcie *pcie)
27 {
28         if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
29                 return pcie->next_lut_index++;
30         else
31                 return -ENOSPC;  /* LUT is full */
32 }
33
34 static void lut_writel(struct ls_pcie *pcie, unsigned int value,
35                        unsigned int offset)
36 {
37         if (pcie->big_endian)
38                 out_be32(pcie->lut + offset, value);
39         else
40                 out_le32(pcie->lut + offset, value);
41 }
42
43 /*
44  * Program a single LUT entry
45  */
46 static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid,
47                                     u32 streamid)
48 {
49         /* leave mask as all zeroes, want to match all bits */
50         lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
51         lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
52 }
53
54 /*
55  * An msi-map is a property to be added to the pci controller
56  * node.  It is a table, where each entry consists of 4 fields
57  * e.g.:
58  *
59  *      msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
60  *                 [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
61  */
62 static void fdt_pcie_set_msi_map_entry_ls(void *blob, struct ls_pcie *pcie,
63                                           u32 devid, u32 streamid)
64 {
65         u32 *prop;
66         u32 phandle;
67         int nodeoffset;
68         uint svr;
69         char *compat = NULL;
70
71         /* find pci controller node */
72         nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
73                                                    pcie->dbi_res.start);
74         if (nodeoffset < 0) {
75 #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
76                 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
77                 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
78                     svr == SVR_LS2048A || svr == SVR_LS2044A ||
79                     svr == SVR_LS2081A || svr == SVR_LS2041A)
80                         compat = "fsl,ls2088a-pcie";
81                 else
82                         compat = CONFIG_FSL_PCIE_COMPAT;
83                 if (compat)
84                         nodeoffset = fdt_node_offset_by_compat_reg(blob,
85                                         compat, pcie->dbi_res.start);
86 #endif
87                 if (nodeoffset < 0)
88                         return;
89         }
90
91         /* get phandle to MSI controller */
92         prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0);
93         if (prop == NULL) {
94                 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
95                       __func__, pcie->idx);
96                 return;
97         }
98         phandle = fdt32_to_cpu(*prop);
99
100         /* set one msi-map row */
101         fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid);
102         fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle);
103         fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid);
104         fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1);
105 }
106
107 /*
108  * An iommu-map is a property to be added to the pci controller
109  * node.  It is a table, where each entry consists of 4 fields
110  * e.g.:
111  *
112  *      iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
113  *                 [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
114  */
115 static void fdt_pcie_set_iommu_map_entry_ls(void *blob, struct ls_pcie *pcie,
116                                             u32 devid, u32 streamid)
117 {
118         u32 *prop;
119         u32 iommu_map[4];
120         int nodeoffset;
121         int lenp;
122         uint svr;
123         char *compat = NULL;
124
125         /* find pci controller node */
126         nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
127                                                    pcie->dbi_res.start);
128         if (nodeoffset < 0) {
129 #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
130                 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
131                 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
132                     svr == SVR_LS2048A || svr == SVR_LS2044A ||
133                     svr == SVR_LS2081A || svr == SVR_LS2041A)
134                         compat = "fsl,ls2088a-pcie";
135                 else
136                         compat = CONFIG_FSL_PCIE_COMPAT;
137
138                 if (compat)
139                         nodeoffset = fdt_node_offset_by_compat_reg(blob,
140                                                 compat, pcie->dbi_res.start);
141 #endif
142                 if (nodeoffset < 0)
143                         return;
144         }
145
146         /* get phandle to iommu controller */
147         prop = fdt_getprop_w(blob, nodeoffset, "iommu-map", &lenp);
148         if (prop == NULL) {
149                 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
150                       __func__, pcie->idx);
151                 return;
152         }
153
154         /* set iommu-map row */
155         iommu_map[0] = cpu_to_fdt32(devid);
156         iommu_map[1] = *++prop;
157         iommu_map[2] = cpu_to_fdt32(streamid);
158         iommu_map[3] = cpu_to_fdt32(1);
159
160         if (devid == 0) {
161                 fdt_setprop_inplace(blob, nodeoffset, "iommu-map",
162                                     iommu_map, 16);
163         } else {
164                 fdt_appendprop(blob, nodeoffset, "iommu-map", iommu_map, 16);
165         }
166 }
167
168 static void fdt_fixup_pcie_ls(void *blob)
169 {
170         struct udevice *dev, *bus;
171         struct ls_pcie *pcie;
172         int streamid;
173         int index;
174         pci_dev_t bdf;
175
176         /* Scan all known buses */
177         for (pci_find_first_device(&dev);
178              dev;
179              pci_find_next_device(&dev)) {
180                 for (bus = dev; device_is_on_pci_bus(bus);)
181                         bus = bus->parent;
182                 pcie = dev_get_priv(bus);
183
184                 streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx);
185                 if (streamid < 0) {
186                         debug("ERROR: no stream ids free\n");
187                         continue;
188                 } else {
189                         pcie->stream_id_cur++;
190                 }
191
192                 index = ls_pcie_next_lut_index(pcie);
193                 if (index < 0) {
194                         debug("ERROR: no LUT indexes free\n");
195                         continue;
196                 }
197
198                 /* the DT fixup must be relative to the hose first_busno */
199                 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
200                 /* map PCI b.d.f to streamID in LUT */
201                 ls_pcie_lut_set_mapping(pcie, index, bdf >> 8,
202                                         streamid);
203                 /* update msi-map in device tree */
204                 fdt_pcie_set_msi_map_entry_ls(blob, pcie, bdf >> 8,
205                                               streamid);
206                 /* update iommu-map in device tree */
207                 fdt_pcie_set_iommu_map_entry_ls(blob, pcie, bdf >> 8,
208                                                 streamid);
209         }
210         pcie_board_fix_fdt(blob);
211 }
212 #endif
213
214 static void ft_pcie_rc_fix(void *blob, struct ls_pcie *pcie)
215 {
216         int off;
217         uint svr;
218         char *compat = NULL;
219
220         off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
221                                             pcie->dbi_res.start);
222         if (off < 0) {
223 #ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
224                 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
225                 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
226                     svr == SVR_LS2048A || svr == SVR_LS2044A ||
227                     svr == SVR_LS2081A || svr == SVR_LS2041A)
228                         compat = "fsl,ls2088a-pcie";
229                 else
230                         compat = CONFIG_FSL_PCIE_COMPAT;
231                 if (compat)
232                         off = fdt_node_offset_by_compat_reg(blob,
233                                         compat, pcie->dbi_res.start);
234 #endif
235                 if (off < 0)
236                         return;
237         }
238
239         if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
240                 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
241         else
242                 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
243 }
244
245 static void ft_pcie_ep_fix(void *blob, struct ls_pcie *pcie)
246 {
247         int off;
248
249         off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
250                                             pcie->dbi_res.start);
251         if (off < 0)
252                 return;
253
254         if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
255                 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
256         else
257                 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
258 }
259
260 static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie)
261 {
262         ft_pcie_ep_fix(blob, pcie);
263         ft_pcie_rc_fix(blob, pcie);
264 }
265
266 /* Fixup Kernel DT for PCIe */
267 void ft_pci_setup_ls(void *blob, bd_t *bd)
268 {
269         struct ls_pcie *pcie;
270
271         list_for_each_entry(pcie, &ls_pcie_list, list)
272                 ft_pcie_ls_setup(blob, pcie);
273
274 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
275         fdt_fixup_pcie_ls(blob);
276 #endif
277 }
278
279 #else /* !CONFIG_OF_BOARD_SETUP */
280 void ft_pci_setup_ls(void *blob, bd_t *bd)
281 {
282 }
283 #endif