blob: ec6acbb138d960d0b3ace2018607a429d78659c8 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Hou Zhiqianga7294ab2016-12-13 14:54:16 +08002/*
Wasim Khan1185b222020-01-06 12:05:57 +00003 * Copyright 2017-2020 NXP
Hou Zhiqianga7294ab2016-12-13 14:54:16 +08004 * Copyright 2014-2015 Freescale Semiconductor, Inc.
5 * Layerscape PCIe driver
Hou Zhiqianga7294ab2016-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 Yamadab08c8c42018-03-05 01:20:11 +090014#include <linux/libfdt.h>
Hou Zhiqianga7294ab2016-12-13 14:54:16 +080015#include <fdt_support.h>
Simon Glass6e2941d2017-05-17 08:23:06 -060016#ifdef CONFIG_ARM
17#include <asm/arch/clock.h>
18#endif
Hou Zhiqianga7294ab2016-12-13 14:54:16 +080019#include "pcie_layerscape.h"
Wasim Khan1185b222020-01-06 12:05:57 +000020#include "pcie_layerscape_fixup_common.h"
Hou Zhiqianga7294ab2016-12-13 14:54:16 +080021
Bharat Bhushan47d17362017-03-22 12:06:30 +053022#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Hou Zhiqianga7294ab2016-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 Lian80afc632016-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 Khan485304a2019-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 Lian80afc632016-12-13 14:54:17 +080064{
65 u32 *prop;
66 u32 phandle;
67 int nodeoffset;
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +080068 uint svr;
69 char *compat = NULL;
Minghuan Lian80afc632016-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 Zhiqiang19538f32016-12-13 14:54:24 +080075#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +080076 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
77 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jaine809e742017-04-27 15:08:06 +053078 svr == SVR_LS2048A || svr == SVR_LS2044A ||
79 svr == SVR_LS2081A || svr == SVR_LS2041A)
Hou Zhiqiang0aaa1a92017-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 Lian80afc632016-12-13 14:54:17 +080087 if (nodeoffset < 0)
88 return;
Minghuan Lian80afc632016-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 Bhushan78be6222017-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 Khan485304a2019-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 Bhushan78be6222017-03-22 12:12:33 +0530117{
118 u32 *prop;
119 u32 iommu_map[4];
120 int nodeoffset;
121 int lenp;
Bharat Bhushan4b97a822017-08-31 13:26:46 +0530122 uint svr;
123 char *compat = NULL;
Bharat Bhushan78be6222017-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 Bhushan4b97a822017-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 Bhushan78be6222017-03-22 12:12:33 +0530142 if (nodeoffset < 0)
143 return;
Bharat Bhushan78be6222017-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 Khan485304a2019-11-15 09:23:35 +0000168static void fdt_fixup_pcie_ls(void *blob)
Minghuan Lian80afc632016-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 Khand20eb7a2020-01-06 12:05:59 +0000184 streamid = pcie_next_streamid(pcie->stream_id_cur, pcie->idx);
Minghuan Lian80afc632016-12-13 14:54:17 +0800185 if (streamid < 0) {
186 debug("ERROR: no stream ids free\n");
187 continue;
Wasim Khand20eb7a2020-01-06 12:05:59 +0000188 } else {
189 pcie->stream_id_cur++;
Minghuan Lian80afc632016-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 Khan485304a2019-11-15 09:23:35 +0000204 fdt_pcie_set_msi_map_entry_ls(blob, pcie, bdf >> 8,
205 streamid);
Bharat Bhushan78be6222017-03-22 12:12:33 +0530206 /* update iommu-map in device tree */
Wasim Khan485304a2019-11-15 09:23:35 +0000207 fdt_pcie_set_iommu_map_entry_ls(blob, pcie, bdf >> 8,
208 streamid);
Minghuan Lian80afc632016-12-13 14:54:17 +0800209 }
Wasim Khan9c2969e2020-01-06 12:06:00 +0000210 pcie_board_fix_fdt(blob);
Minghuan Lian80afc632016-12-13 14:54:17 +0800211}
212#endif
213
Xiaowei Bao59a557f2018-10-26 09:56:26 +0800214static void ft_pcie_rc_fix(void *blob, struct ls_pcie *pcie)
Minghuan Lian80afc632016-12-13 14:54:17 +0800215{
216 int off;
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +0800217 uint svr;
218 char *compat = NULL;
Minghuan Lian80afc632016-12-13 14:54:17 +0800219
220 off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
221 pcie->dbi_res.start);
222 if (off < 0) {
Hou Zhiqiang19538f32016-12-13 14:54:24 +0800223#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +0800224 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
225 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jaine809e742017-04-27 15:08:06 +0530226 svr == SVR_LS2048A || svr == SVR_LS2044A ||
227 svr == SVR_LS2081A || svr == SVR_LS2041A)
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +0800228 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
Minghuan Lian80afc632016-12-13 14:54:17 +0800235 if (off < 0)
236 return;
Minghuan Lian80afc632016-12-13 14:54:17 +0800237 }
238
Xiaowei Bao59a557f2018-10-26 09:56:26 +0800239 if (pcie->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
Minghuan Lian80afc632016-12-13 14:54:17 +0800240 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
Xiaowei Bao59a557f2018-10-26 09:56:26 +0800245static void ft_pcie_ep_fix(void *blob, struct ls_pcie *pcie)
246{
247 int off;
248
Pankaj Bansal63618e72019-11-30 13:14:10 +0000249 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
Xiaowei Bao59a557f2018-10-26 09:56:26 +0800250 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
260static 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
Minghuan Lian80afc632016-12-13 14:54:17 +0800266/* Fixup Kernel DT for PCIe */
Wasim Khan1185b222020-01-06 12:05:57 +0000267void ft_pci_setup_ls(void *blob, bd_t *bd)
Minghuan Lian80afc632016-12-13 14:54:17 +0800268{
269 struct ls_pcie *pcie;
270
271 list_for_each_entry(pcie, &ls_pcie_list, list)
272 ft_pcie_ls_setup(blob, pcie);
273
Bharat Bhushan47d17362017-03-22 12:06:30 +0530274#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Wasim Khan485304a2019-11-15 09:23:35 +0000275 fdt_fixup_pcie_ls(blob);
Minghuan Lian80afc632016-12-13 14:54:17 +0800276#endif
277}
Minghuan Lian80afc632016-12-13 14:54:17 +0800278
Hou Zhiqianga7294ab2016-12-13 14:54:16 +0800279#else /* !CONFIG_OF_BOARD_SETUP */
Wasim Khan1185b222020-01-06 12:05:57 +0000280void ft_pci_setup_ls(void *blob, bd_t *bd)
Hou Zhiqianga7294ab2016-12-13 14:54:16 +0800281{
282}
283#endif