pci: layerscape: Common device tree fixup for NXP SoCs
[oweals/u-boot.git] / drivers / pci / pcie_layerscape_gen4_fixup.c
1 // SPDX-License-Identifier: GPL-2.0+ OR X11
2 /*
3  * Copyright 2018-2020 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 #include "pcie_layerscape_fixup_common.h"
23
24 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
25 /*
26  * Return next available LUT index.
27  */
28 static 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
36 /* returns the next available streamid for pcie, -errno if failed */
37 static int ls_pcie_g4_next_streamid(struct ls_pcie_g4 *pcie)
38 {
39         int stream_id = pcie->stream_id_cur;
40
41         if (stream_id > FSL_PEX_STREAM_ID_END)
42                 return -EINVAL;
43
44         pcie->stream_id_cur++;
45
46         return stream_id | ((pcie->idx + 1) << 11);
47 }
48
49 /*
50  * Program a single LUT entry
51  */
52 static void ls_pcie_g4_lut_set_mapping(struct ls_pcie_g4 *pcie, int index,
53                                        u32 devid, u32 streamid)
54 {
55         /* leave mask as all zeroes, want to match all bits */
56         lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
57         lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
58 }
59
60 /*
61  * An msi-map is a property to be added to the pci controller
62  * node.  It is a table, where each entry consists of 4 fields
63  * e.g.:
64  *
65  *      msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
66  *                 [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
67  */
68 static void fdt_pcie_set_msi_map_entry_ls_gen4(void *blob,
69                                                struct ls_pcie_g4 *pcie,
70                                                u32 devid, u32 streamid)
71 {
72         u32 *prop;
73         u32 phandle;
74         int nodeoff;
75
76 #ifdef CONFIG_FSL_PCIE_COMPAT
77         nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
78                                                 pcie->ccsr_res.start);
79 #else
80 #error "No CONFIG_FSL_PCIE_COMPAT defined"
81 #endif
82         if (nodeoff < 0) {
83                 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
84                 return;
85         }
86
87         /* get phandle to MSI controller */
88         prop = (u32 *)fdt_getprop(blob, nodeoff, "msi-parent", 0);
89         if (!prop) {
90                 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
91                       __func__, pcie->idx);
92                 return;
93         }
94         phandle = fdt32_to_cpu(*prop);
95
96         /* set one msi-map row */
97         fdt_appendprop_u32(blob, nodeoff, "msi-map", devid);
98         fdt_appendprop_u32(blob, nodeoff, "msi-map", phandle);
99         fdt_appendprop_u32(blob, nodeoff, "msi-map", streamid);
100         fdt_appendprop_u32(blob, nodeoff, "msi-map", 1);
101 }
102
103 /*
104  * An iommu-map is a property to be added to the pci controller
105  * node.  It is a table, where each entry consists of 4 fields
106  * e.g.:
107  *
108  *      iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
109  *                 [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
110  */
111 static void fdt_pcie_set_iommu_map_entry_ls_gen4(void *blob,
112                                                  struct ls_pcie_g4 *pcie,
113                                                  u32 devid, u32 streamid)
114 {
115         u32 *prop;
116         u32 iommu_map[4];
117         int nodeoff;
118         int lenp;
119
120 #ifdef CONFIG_FSL_PCIE_COMPAT
121         nodeoff = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
122                                                 pcie->ccsr_res.start);
123 #else
124 #error "No CONFIG_FSL_PCIE_COMPAT defined"
125 #endif
126         if (nodeoff < 0) {
127                 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
128                 return;
129         }
130
131         /* get phandle to iommu controller */
132         prop = fdt_getprop_w(blob, nodeoff, "iommu-map", &lenp);
133         if (!prop) {
134                 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
135                       __func__, pcie->idx);
136                 return;
137         }
138
139         /* set iommu-map row */
140         iommu_map[0] = cpu_to_fdt32(devid);
141         iommu_map[1] = *++prop;
142         iommu_map[2] = cpu_to_fdt32(streamid);
143         iommu_map[3] = cpu_to_fdt32(1);
144
145         if (devid == 0)
146                 fdt_setprop_inplace(blob, nodeoff, "iommu-map", iommu_map, 16);
147         else
148                 fdt_appendprop(blob, nodeoff, "iommu-map", iommu_map, 16);
149 }
150
151 static void fdt_fixup_pcie_ls_gen4(void *blob)
152 {
153         struct udevice *dev, *bus;
154         struct ls_pcie_g4 *pcie;
155         int streamid;
156         int index;
157         pci_dev_t bdf;
158
159         /* Scan all known buses */
160         for (pci_find_first_device(&dev); dev; pci_find_next_device(&dev)) {
161                 for (bus = dev; device_is_on_pci_bus(bus);)
162                         bus = bus->parent;
163                 pcie = dev_get_priv(bus);
164
165                 streamid = ls_pcie_g4_next_streamid(pcie);
166                 if (streamid < 0) {
167                         debug("ERROR: no stream ids free\n");
168                         continue;
169                 }
170
171                 index = ls_pcie_g4_next_lut_index(pcie);
172                 if (index < 0) {
173                         debug("ERROR: no LUT indexes free\n");
174                         continue;
175                 }
176
177                 /* the DT fixup must be relative to the hose first_busno */
178                 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
179                 /* map PCI b.d.f to streamID in LUT */
180                 ls_pcie_g4_lut_set_mapping(pcie, index, bdf >> 8, streamid);
181                 /* update msi-map in device tree */
182                 fdt_pcie_set_msi_map_entry_ls_gen4(blob, pcie, bdf >> 8,
183                                                    streamid);
184                 /* update iommu-map in device tree */
185                 fdt_pcie_set_iommu_map_entry_ls_gen4(blob, pcie, bdf >> 8,
186                                                      streamid);
187         }
188 }
189 #endif
190
191 static void ft_pcie_ep_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
192 {
193         int off;
194
195         off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
196                                             pcie->ccsr_res.start);
197
198         if (off < 0) {
199                 debug("%s: ERROR: failed to find pcie compatiable\n",
200                       __func__);
201                 return;
202         }
203
204         if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
205                 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
206         else
207                 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
208 }
209
210 static void ft_pcie_rc_layerscape_gen4_fix(void *blob, struct ls_pcie_g4 *pcie)
211 {
212         int off;
213
214 #ifdef CONFIG_FSL_PCIE_COMPAT
215         off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_COMPAT,
216                                             pcie->ccsr_res.start);
217 #else
218 #error "No CONFIG_FSL_PCIE_COMPAT defined"
219 #endif
220         if (off < 0) {
221                 debug("%s: ERROR: failed to find pcie compatiable\n", __func__);
222                 return;
223         }
224
225         if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
226                 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
227         else
228                 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
229 }
230
231 static void ft_pcie_layerscape_gen4_setup(void *blob, struct ls_pcie_g4 *pcie)
232 {
233         ft_pcie_rc_layerscape_gen4_fix(blob, pcie);
234         ft_pcie_ep_layerscape_gen4_fix(blob, pcie);
235 }
236
237 /* Fixup Kernel DT for PCIe */
238 void ft_pci_setup_ls_gen4(void *blob, bd_t *bd)
239 {
240         struct ls_pcie_g4 *pcie;
241
242         list_for_each_entry(pcie, &ls_pcie_g4_list, list)
243                 ft_pcie_layerscape_gen4_setup(blob, pcie);
244
245 #if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
246         fdt_fixup_pcie_ls_gen4(blob);
247 #endif
248 }
249
250 #else /* !CONFIG_OF_BOARD_SETUP */
251 void ft_pci_setup_ls_gen4(void *blob, bd_t *bd)
252 {
253 }
254 #endif