blob: 94de4edaf4f4d9a959aadbb00eb6f79e38ed3ba2 [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>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080011#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
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080017#include <fdt_support.h>
Simon Glass243182c2017-05-17 08:23:06 -060018#ifdef CONFIG_ARM
19#include <asm/arch/clock.h>
20#endif
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080021#include "pcie_layerscape.h"
Wasim Khan54e44ef2020-01-06 12:05:57 +000022#include "pcie_layerscape_fixup_common.h"
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080023
Bharat Bhushan36e36be2017-03-22 12:06:30 +053024#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080025/*
26 * Return next available LUT index.
27 */
Xiaowei Bao13b277f2020-07-09 23:31:33 +080028static int ls_pcie_next_lut_index(struct ls_pcie_rc *pcie_rc)
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080029{
Xiaowei Bao13b277f2020-07-09 23:31:33 +080030 if (pcie_rc->next_lut_index < PCIE_LUT_ENTRY_COUNT)
31 return pcie_rc->next_lut_index++;
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080032 else
33 return -ENOSPC; /* LUT is full */
34}
35
Xiaowei Bao13b277f2020-07-09 23:31:33 +080036static void lut_writel(struct ls_pcie_rc *pcie_rc, unsigned int value,
Minghuan Lianc1067842016-12-13 14:54:17 +080037 unsigned int offset)
38{
Xiaowei Bao13b277f2020-07-09 23:31:33 +080039 struct ls_pcie *pcie = pcie_rc->pcie;
40
Minghuan Lianc1067842016-12-13 14:54:17 +080041 if (pcie->big_endian)
42 out_be32(pcie->lut + offset, value);
43 else
44 out_le32(pcie->lut + offset, value);
45}
46
47/*
48 * Program a single LUT entry
49 */
Xiaowei Bao13b277f2020-07-09 23:31:33 +080050static void ls_pcie_lut_set_mapping(struct ls_pcie_rc *pcie_rc, int index,
51 u32 devid, u32 streamid)
Minghuan Lianc1067842016-12-13 14:54:17 +080052{
53 /* leave mask as all zeroes, want to match all bits */
Xiaowei Bao13b277f2020-07-09 23:31:33 +080054 lut_writel(pcie_rc, devid << 16, PCIE_LUT_UDR(index));
55 lut_writel(pcie_rc, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
Minghuan Lianc1067842016-12-13 14:54:17 +080056}
57
58/*
59 * An msi-map is a property to be added to the pci controller
60 * node. It is a table, where each entry consists of 4 fields
61 * e.g.:
62 *
63 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
64 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
65 */
Xiaowei Bao13b277f2020-07-09 23:31:33 +080066static void fdt_pcie_set_msi_map_entry_ls(void *blob,
67 struct ls_pcie_rc *pcie_rc,
Wasim Khan8cb089e2019-11-15 09:23:35 +000068 u32 devid, u32 streamid)
Minghuan Lianc1067842016-12-13 14:54:17 +080069{
70 u32 *prop;
71 u32 phandle;
72 int nodeoffset;
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080073 uint svr;
74 char *compat = NULL;
Xiaowei Bao13b277f2020-07-09 23:31:33 +080075 struct ls_pcie *pcie = pcie_rc->pcie;
Minghuan Lianc1067842016-12-13 14:54:17 +080076
77 /* find pci controller node */
78 nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
Xiaowei Bao13b277f2020-07-09 23:31:33 +080079 pcie_rc->dbi_res.start);
Minghuan Lianc1067842016-12-13 14:54:17 +080080 if (nodeoffset < 0) {
Hou Zhiqiangd553bf22016-12-13 14:54:24 +080081#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080082 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
83 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jain2b361782017-04-27 15:08:06 +053084 svr == SVR_LS2048A || svr == SVR_LS2044A ||
85 svr == SVR_LS2081A || svr == SVR_LS2041A)
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080086 compat = "fsl,ls2088a-pcie";
87 else
88 compat = CONFIG_FSL_PCIE_COMPAT;
89 if (compat)
90 nodeoffset = fdt_node_offset_by_compat_reg(blob,
Xiaowei Bao13b277f2020-07-09 23:31:33 +080091 compat, pcie_rc->dbi_res.start);
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +080092#endif
Minghuan Lianc1067842016-12-13 14:54:17 +080093 if (nodeoffset < 0)
94 return;
Minghuan Lianc1067842016-12-13 14:54:17 +080095 }
96
97 /* get phandle to MSI controller */
98 prop = (u32 *)fdt_getprop(blob, nodeoffset, "msi-parent", 0);
99 if (prop == NULL) {
100 debug("\n%s: ERROR: missing msi-parent: PCIe%d\n",
101 __func__, pcie->idx);
102 return;
103 }
104 phandle = fdt32_to_cpu(*prop);
105
106 /* set one msi-map row */
107 fdt_appendprop_u32(blob, nodeoffset, "msi-map", devid);
108 fdt_appendprop_u32(blob, nodeoffset, "msi-map", phandle);
109 fdt_appendprop_u32(blob, nodeoffset, "msi-map", streamid);
110 fdt_appendprop_u32(blob, nodeoffset, "msi-map", 1);
111}
112
Bharat Bhushan50514b92017-03-22 12:12:33 +0530113/*
114 * An iommu-map is a property to be added to the pci controller
115 * node. It is a table, where each entry consists of 4 fields
116 * e.g.:
117 *
118 * iommu-map = <[devid] [phandle-to-iommu-ctrl] [stream-id] [count]
119 * [devid] [phandle-to-iommu-ctrl] [stream-id] [count]>;
120 */
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800121static void fdt_pcie_set_iommu_map_entry_ls(void *blob,
122 struct ls_pcie_rc *pcie_rc,
Wasim Khan8cb089e2019-11-15 09:23:35 +0000123 u32 devid, u32 streamid)
Bharat Bhushan50514b92017-03-22 12:12:33 +0530124{
125 u32 *prop;
126 u32 iommu_map[4];
127 int nodeoffset;
128 int lenp;
Bharat Bhushan42aea352017-08-31 13:26:46 +0530129 uint svr;
130 char *compat = NULL;
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800131 struct ls_pcie *pcie = pcie_rc->pcie;
Bharat Bhushan50514b92017-03-22 12:12:33 +0530132
133 /* find pci controller node */
134 nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800135 pcie_rc->dbi_res.start);
Bharat Bhushan50514b92017-03-22 12:12:33 +0530136 if (nodeoffset < 0) {
137#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Bharat Bhushan42aea352017-08-31 13:26:46 +0530138 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
139 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
140 svr == SVR_LS2048A || svr == SVR_LS2044A ||
141 svr == SVR_LS2081A || svr == SVR_LS2041A)
142 compat = "fsl,ls2088a-pcie";
143 else
144 compat = CONFIG_FSL_PCIE_COMPAT;
145
146 if (compat)
147 nodeoffset = fdt_node_offset_by_compat_reg(blob,
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800148 compat, pcie_rc->dbi_res.start);
Bharat Bhushan42aea352017-08-31 13:26:46 +0530149#endif
Bharat Bhushan50514b92017-03-22 12:12:33 +0530150 if (nodeoffset < 0)
151 return;
Bharat Bhushan50514b92017-03-22 12:12:33 +0530152 }
153
154 /* get phandle to iommu controller */
155 prop = fdt_getprop_w(blob, nodeoffset, "iommu-map", &lenp);
156 if (prop == NULL) {
157 debug("\n%s: ERROR: missing iommu-map: PCIe%d\n",
158 __func__, pcie->idx);
159 return;
160 }
161
162 /* set iommu-map row */
163 iommu_map[0] = cpu_to_fdt32(devid);
164 iommu_map[1] = *++prop;
165 iommu_map[2] = cpu_to_fdt32(streamid);
166 iommu_map[3] = cpu_to_fdt32(1);
167
168 if (devid == 0) {
169 fdt_setprop_inplace(blob, nodeoffset, "iommu-map",
170 iommu_map, 16);
171 } else {
172 fdt_appendprop(blob, nodeoffset, "iommu-map", iommu_map, 16);
173 }
174}
175
Wasim Khan8cb089e2019-11-15 09:23:35 +0000176static void fdt_fixup_pcie_ls(void *blob)
Minghuan Lianc1067842016-12-13 14:54:17 +0800177{
178 struct udevice *dev, *bus;
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800179 struct ls_pcie_rc *pcie_rc;
Minghuan Lianc1067842016-12-13 14:54:17 +0800180 int streamid;
181 int index;
182 pci_dev_t bdf;
183
184 /* Scan all known buses */
185 for (pci_find_first_device(&dev);
186 dev;
187 pci_find_next_device(&dev)) {
188 for (bus = dev; device_is_on_pci_bus(bus);)
189 bus = bus->parent;
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800190 pcie_rc = dev_get_priv(bus);
Minghuan Lianc1067842016-12-13 14:54:17 +0800191
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800192 streamid = pcie_next_streamid(pcie_rc->stream_id_cur,
193 pcie_rc->pcie->idx);
Minghuan Lianc1067842016-12-13 14:54:17 +0800194 if (streamid < 0) {
195 debug("ERROR: no stream ids free\n");
196 continue;
Wasim Khan9d3d2302020-01-06 12:05:59 +0000197 } else {
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800198 pcie_rc->stream_id_cur++;
Minghuan Lianc1067842016-12-13 14:54:17 +0800199 }
200
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800201 index = ls_pcie_next_lut_index(pcie_rc);
Minghuan Lianc1067842016-12-13 14:54:17 +0800202 if (index < 0) {
203 debug("ERROR: no LUT indexes free\n");
204 continue;
205 }
206
207 /* the DT fixup must be relative to the hose first_busno */
208 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
209 /* map PCI b.d.f to streamID in LUT */
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800210 ls_pcie_lut_set_mapping(pcie_rc, index, bdf >> 8,
Minghuan Lianc1067842016-12-13 14:54:17 +0800211 streamid);
212 /* update msi-map in device tree */
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800213 fdt_pcie_set_msi_map_entry_ls(blob, pcie_rc, bdf >> 8,
Wasim Khan8cb089e2019-11-15 09:23:35 +0000214 streamid);
Bharat Bhushan50514b92017-03-22 12:12:33 +0530215 /* update iommu-map in device tree */
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800216 fdt_pcie_set_iommu_map_entry_ls(blob, pcie_rc, bdf >> 8,
Wasim Khan8cb089e2019-11-15 09:23:35 +0000217 streamid);
Minghuan Lianc1067842016-12-13 14:54:17 +0800218 }
Wasim Khan70bec5c2020-01-06 12:06:00 +0000219 pcie_board_fix_fdt(blob);
Minghuan Lianc1067842016-12-13 14:54:17 +0800220}
221#endif
222
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800223static void ft_pcie_rc_fix(void *blob, struct ls_pcie_rc *pcie_rc)
Minghuan Lianc1067842016-12-13 14:54:17 +0800224{
225 int off;
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800226 uint svr;
227 char *compat = NULL;
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800228 struct ls_pcie *pcie = pcie_rc->pcie;
Minghuan Lianc1067842016-12-13 14:54:17 +0800229
230 off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800231 pcie_rc->dbi_res.start);
Minghuan Lianc1067842016-12-13 14:54:17 +0800232 if (off < 0) {
Hou Zhiqiangd553bf22016-12-13 14:54:24 +0800233#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800234 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
235 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530236 svr == SVR_LS2048A || svr == SVR_LS2044A ||
237 svr == SVR_LS2081A || svr == SVR_LS2041A)
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800238 compat = "fsl,ls2088a-pcie";
239 else
240 compat = CONFIG_FSL_PCIE_COMPAT;
241 if (compat)
242 off = fdt_node_offset_by_compat_reg(blob,
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800243 compat, pcie_rc->dbi_res.start);
Hou Zhiqiang8cd3f482017-03-03 12:35:10 +0800244#endif
Minghuan Lianc1067842016-12-13 14:54:17 +0800245 if (off < 0)
246 return;
Minghuan Lianc1067842016-12-13 14:54:17 +0800247 }
248
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800249 if (pcie_rc->enabled && pcie->mode == PCI_HEADER_TYPE_BRIDGE)
Minghuan Lianc1067842016-12-13 14:54:17 +0800250 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
251 else
252 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
253}
254
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800255static void ft_pcie_ep_fix(void *blob, struct ls_pcie_rc *pcie_rc)
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800256{
257 int off;
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800258 struct ls_pcie *pcie = pcie_rc->pcie;
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800259
Pankaj Bansal64d85a22019-11-30 13:14:10 +0000260 off = fdt_node_offset_by_compat_reg(blob, CONFIG_FSL_PCIE_EP_COMPAT,
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800261 pcie_rc->dbi_res.start);
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800262 if (off < 0)
263 return;
264
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800265 if (pcie_rc->enabled && pcie->mode == PCI_HEADER_TYPE_NORMAL)
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800266 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
267 else
268 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
269}
270
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800271static void ft_pcie_ls_setup(void *blob, struct ls_pcie_rc *pcie_rc)
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800272{
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800273 ft_pcie_ep_fix(blob, pcie_rc);
274 ft_pcie_rc_fix(blob, pcie_rc);
Xiaowei Bao8d7e2e82018-10-26 09:56:26 +0800275}
276
Minghuan Lianc1067842016-12-13 14:54:17 +0800277/* Fixup Kernel DT for PCIe */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900278void ft_pci_setup_ls(void *blob, struct bd_info *bd)
Minghuan Lianc1067842016-12-13 14:54:17 +0800279{
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800280 struct ls_pcie_rc *pcie_rc;
Minghuan Lianc1067842016-12-13 14:54:17 +0800281
Xiaowei Bao13b277f2020-07-09 23:31:33 +0800282 list_for_each_entry(pcie_rc, &ls_pcie_list, list)
283 ft_pcie_ls_setup(blob, pcie_rc);
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800284
Bharat Bhushan36e36be2017-03-22 12:06:30 +0530285#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Wasim Khan8cb089e2019-11-15 09:23:35 +0000286 fdt_fixup_pcie_ls(blob);
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800287#endif
288}
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800289
290#else /* !CONFIG_OF_BOARD_SETUP */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900291void ft_pci_setup_ls(void *blob, struct bd_info *bd)
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +0800292{
293}
294#endif