blob: 56de6d97becaaf21d8250a238c02fa42bb9e7e45 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +08002/*
Wasim Khan54e44ef2020-01-06 12:05:57 +00003 * Copyright 2017-2020 NXP
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +08004 * Copyright 2014-2015 Freescale Semiconductor, Inc.
5 * Layerscape PCIe driver
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +08006 */
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
Masahiro Yamada75f82d02018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080015#include <fdt_support.h>
Simon Glass243182c2017-05-17 08:23:06 -060016#ifdef CONFIG_ARM
17#include <asm/arch/clock.h>
18#endif
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080019#include "pcie_layerscape.h"
Wasim Khan54e44ef2020-01-06 12:05:57 +000020#include "pcie_layerscape_fixup_common.h"
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080021
Bharat Bhushan36e36be2017-03-22 12:06:30 +053022#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080023/*
24 * Return next available LUT index.
25 */
26static 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
Minghuan Lianc1067842016-12-13 14:54:17 +080034static 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 */
46static 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 */
Wasim Khan8cb089e2019-11-15 09:23:35 +000062static void fdt_pcie_set_msi_map_entry_ls(void *blob, struct ls_pcie *pcie,
63 u32 devid, u32 streamid)
Minghuan Lianc1067842016-12-13 14:54:17 +080064{
65 u32 *prop;
66 u32 phandle;
67 int nodeoffset;
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080068 uint svr;
69 char *compat = NULL;
Minghuan Lianc1067842016-12-13 14:54:17 +080070
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) {
Hou Zhiqiangd553bf22016-12-13 14:54:24 +080075#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080076 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
77 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jain2b361782017-04-27 15:08:06 +053078 svr == SVR_LS2048A || svr == SVR_LS2044A ||
79 svr == SVR_LS2081A || svr == SVR_LS2041A)
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080080 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
Minghuan Lianc1067842016-12-13 14:54:17 +080087 if (nodeoffset < 0)
88 return;
Minghuan Lianc1067842016-12-13 14:54:17 +080089 }
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
Bharat Bhushan50514b92017-03-22 12:12:33 +0530107/*
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 */
Wasim Khan8cb089e2019-11-15 09:23:35 +0000115static void fdt_pcie_set_iommu_map_entry_ls(void *blob, struct ls_pcie *pcie,
116 u32 devid, u32 streamid)
Bharat Bhushan50514b92017-03-22 12:12:33 +0530117{
118 u32 *prop;
119 u32 iommu_map[4];
120 int nodeoffset;
121 int lenp;
Bharat Bhushan42aea352017-08-31 13:26:46 +0530122 uint svr;
123 char *compat = NULL;
Bharat Bhushan50514b92017-03-22 12:12:33 +0530124
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 */
Bharat Bhushan42aea352017-08-31 13:26:46 +0530130 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
Bharat Bhushan50514b92017-03-22 12:12:33 +0530142 if (nodeoffset < 0)
143 return;
Bharat Bhushan50514b92017-03-22 12:12:33 +0530144 }
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
Wasim Khan8cb089e2019-11-15 09:23:35 +0000168static void fdt_fixup_pcie_ls(void *blob)
Minghuan Lianc1067842016-12-13 14:54:17 +0800169{
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
Wasim Khan9d3d2302020-01-06 12:05:59 +0000184 streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx);
Minghuan Lianc1067842016-12-13 14:54:17 +0800185 if (streamid < 0) {
186 debug("ERROR: no stream ids free\n");
187 continue;
Wasim Khan9d3d2302020-01-06 12:05:59 +0000188 } else {
189 pcie->stream_id_cur++;
Minghuan Lianc1067842016-12-13 14:54:17 +0800190 }
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 */
Wasim Khan8cb089e2019-11-15 09:23:35 +0000204 fdt_pcie_set_msi_map_entry_ls(blob, pcie, bdf >> 8,
205 streamid);
Bharat Bhushan50514b92017-03-22 12:12:33 +0530206 /* update iommu-map in device tree */
Wasim Khan8cb089e2019-11-15 09:23:35 +0000207 fdt_pcie_set_iommu_map_entry_ls(blob, pcie, bdf >> 8,
208 streamid);
Minghuan Lianc1067842016-12-13 14:54:17 +0800209 }
210}
211#endif
212
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800213static void ft_pcie_rc_fix(void *blob, struct ls_pcie *pcie)
Minghuan Lianc1067842016-12-13 14:54:17 +0800214{
215 int off;
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800216 uint svr;
217 char *compat = NULL;
Minghuan Lianc1067842016-12-13 14:54:17 +0800218
219 off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
220 pcie->dbi_res.start);
221 if (off < 0) {
Hou Zhiqiangd553bf22016-12-13 14:54:24 +0800222#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800223 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
224 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530225 svr == SVR_LS2048A || svr == SVR_LS2044A ||
226 svr == SVR_LS2081A || svr == SVR_LS2041A)
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800227 compat = "fsl,ls2088a-pcie";
228 else
229 compat = CONFIG_FSL_PCIE_COMPAT;
230 if (compat)
231 off = fdt_node_offset_by_compat_reg(blob,
232 compat, pcie->dbi_res.start);
233#endif
Minghuan Lianc1067842016-12-13 14:54:17 +0800234 if (off < 0)
235 return;
Minghuan Lianc1067842016-12-13 14:54:17 +0800236 }
237
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800238 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
Minghuan Lianc1067842016-12-13 14:54:17 +0800239 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
240 else
241 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
242}
243
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800244static void ft_pcie_ep_fix(void *blob, struct ls_pcie *pcie)
245{
246 int off;
247
Pankaj Bansal64d85a22019-11-30 13:14:10 +0000248 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800249 pcie->dbi_res.start);
250 if (off < 0)
251 return;
252
253 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
254 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
255 else
256 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
257}
258
259static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie)
260{
261 ft_pcie_ep_fix(blob, pcie);
262 ft_pcie_rc_fix(blob, pcie);
263}
264
Minghuan Lianc1067842016-12-13 14:54:17 +0800265/* Fixup Kernel DT for PCIe */
Wasim Khan54e44ef2020-01-06 12:05:57 +0000266void ft_pci_setup_ls(void *blob, bd_t *bd)
Minghuan Lianc1067842016-12-13 14:54:17 +0800267{
268 struct ls_pcie *pcie;
269
270 list_for_each_entry(pcie, &ls_pcie_list, list)
271 ft_pcie_ls_setup(blob, pcie);
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800272
Bharat Bhushan36e36be2017-03-22 12:06:30 +0530273#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Wasim Khan8cb089e2019-11-15 09:23:35 +0000274 fdt_fixup_pcie_ls(blob);
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800275#endif
276}
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800277
278#else /* !CONFIG_OF_BOARD_SETUP */
Wasim Khan54e44ef2020-01-06 12:05:57 +0000279void ft_pci_setup_ls(void *blob, bd_t *bd)
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800280{
281}
282#endif