blob: d7591bcff02fe61d25c6bd04a1c1e287913bc49f [file] [log] [blame]
Hou Zhiqianga7294ab2016-12-13 14:54:16 +08001/*
2 * Copyright 2014-2015 Freescale Semiconductor, Inc.
3 * Layerscape PCIe driver
4 *
5 * SPDX-License-Identifier: GPL-2.0+
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 <libfdt.h>
15#include <fdt_support.h>
16#include "pcie_layerscape.h"
17
Bharat Bhushan47d17362017-03-22 12:06:30 +053018#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Hou Zhiqianga7294ab2016-12-13 14:54:16 +080019/*
20 * Return next available LUT index.
21 */
22static int ls_pcie_next_lut_index(struct ls_pcie *pcie)
23{
24 if (pcie->next_lut_index < PCIE_LUT_ENTRY_COUNT)
25 return pcie->next_lut_index++;
26 else
27 return -ENOSPC; /* LUT is full */
28}
29
30/* returns the next available streamid for pcie, -errno if failed */
31static int ls_pcie_next_streamid(void)
32{
33 static int next_stream_id = FSL_PEX_STREAM_ID_START;
34
35 if (next_stream_id > FSL_PEX_STREAM_ID_END)
36 return -EINVAL;
37
38 return next_stream_id++;
39}
40
Minghuan Lian80afc632016-12-13 14:54:17 +080041static void lut_writel(struct ls_pcie *pcie, unsigned int value,
42 unsigned int offset)
43{
44 if (pcie->big_endian)
45 out_be32(pcie->lut + offset, value);
46 else
47 out_le32(pcie->lut + offset, value);
48}
49
50/*
51 * Program a single LUT entry
52 */
53static void ls_pcie_lut_set_mapping(struct ls_pcie *pcie, int index, u32 devid,
54 u32 streamid)
55{
56 /* leave mask as all zeroes, want to match all bits */
57 lut_writel(pcie, devid << 16, PCIE_LUT_UDR(index));
58 lut_writel(pcie, streamid | PCIE_LUT_ENABLE, PCIE_LUT_LDR(index));
59}
60
61/*
62 * An msi-map is a property to be added to the pci controller
63 * node. It is a table, where each entry consists of 4 fields
64 * e.g.:
65 *
66 * msi-map = <[devid] [phandle-to-msi-ctrl] [stream-id] [count]
67 * [devid] [phandle-to-msi-ctrl] [stream-id] [count]>;
68 */
69static void fdt_pcie_set_msi_map_entry(void *blob, struct ls_pcie *pcie,
70 u32 devid, u32 streamid)
71{
72 u32 *prop;
73 u32 phandle;
74 int nodeoffset;
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +080075 uint svr;
76 char *compat = NULL;
Minghuan Lian80afc632016-12-13 14:54:17 +080077
78 /* find pci controller node */
79 nodeoffset = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
80 pcie->dbi_res.start);
81 if (nodeoffset < 0) {
Hou Zhiqiang19538f32016-12-13 14:54:24 +080082#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +080083 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
84 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
85 svr == SVR_LS2048A || svr == SVR_LS2044A)
86 compat = "fsl,ls2088a-pcie";
87 else
88 compat = CONFIG_FSL_PCIE_COMPAT;
89 if (compat)
90 nodeoffset = fdt_node_offset_by_compat_reg(blob,
91 compat, pcie->dbi_res.start);
92#endif
Minghuan Lian80afc632016-12-13 14:54:17 +080093 if (nodeoffset < 0)
94 return;
Minghuan Lian80afc632016-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
113static void fdt_fixup_pcie(void *blob)
114{
115 struct udevice *dev, *bus;
116 struct ls_pcie *pcie;
117 int streamid;
118 int index;
119 pci_dev_t bdf;
120
121 /* Scan all known buses */
122 for (pci_find_first_device(&dev);
123 dev;
124 pci_find_next_device(&dev)) {
125 for (bus = dev; device_is_on_pci_bus(bus);)
126 bus = bus->parent;
127 pcie = dev_get_priv(bus);
128
129 streamid = ls_pcie_next_streamid();
130 if (streamid < 0) {
131 debug("ERROR: no stream ids free\n");
132 continue;
133 }
134
135 index = ls_pcie_next_lut_index(pcie);
136 if (index < 0) {
137 debug("ERROR: no LUT indexes free\n");
138 continue;
139 }
140
141 /* the DT fixup must be relative to the hose first_busno */
142 bdf = dm_pci_get_bdf(dev) - PCI_BDF(bus->seq, 0, 0);
143 /* map PCI b.d.f to streamID in LUT */
144 ls_pcie_lut_set_mapping(pcie, index, bdf >> 8,
145 streamid);
146 /* update msi-map in device tree */
147 fdt_pcie_set_msi_map_entry(blob, pcie, bdf >> 8,
148 streamid);
149 }
150}
151#endif
152
153static void ft_pcie_ls_setup(void *blob, struct ls_pcie *pcie)
154{
155 int off;
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +0800156 uint svr;
157 char *compat = NULL;
Minghuan Lian80afc632016-12-13 14:54:17 +0800158
159 off = fdt_node_offset_by_compat_reg(blob, "fsl,ls-pcie",
160 pcie->dbi_res.start);
161 if (off < 0) {
Hou Zhiqiang19538f32016-12-13 14:54:24 +0800162#ifdef CONFIG_FSL_PCIE_COMPAT /* Compatible with older version of dts node */
Hou Zhiqiang0aaa1a92017-03-03 12:35:10 +0800163 svr = (get_svr() >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
164 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
165 svr == SVR_LS2048A || svr == SVR_LS2044A)
166 compat = "fsl,ls2088a-pcie";
167 else
168 compat = CONFIG_FSL_PCIE_COMPAT;
169 if (compat)
170 off = fdt_node_offset_by_compat_reg(blob,
171 compat, pcie->dbi_res.start);
172#endif
Minghuan Lian80afc632016-12-13 14:54:17 +0800173 if (off < 0)
174 return;
Minghuan Lian80afc632016-12-13 14:54:17 +0800175 }
176
177 if (pcie->enabled)
178 fdt_set_node_status(blob, off, FDT_STATUS_OKAY, 0);
179 else
180 fdt_set_node_status(blob, off, FDT_STATUS_DISABLED, 0);
181}
182
183/* Fixup Kernel DT for PCIe */
184void ft_pci_setup(void *blob, bd_t *bd)
185{
186 struct ls_pcie *pcie;
187
188 list_for_each_entry(pcie, &ls_pcie_list, list)
189 ft_pcie_ls_setup(blob, pcie);
190
Bharat Bhushan47d17362017-03-22 12:06:30 +0530191#if defined(CONFIG_FSL_LSCH3) || defined(CONFIG_FSL_LSCH2)
Minghuan Lian80afc632016-12-13 14:54:17 +0800192 fdt_fixup_pcie(blob);
193#endif
194}
Minghuan Lian80afc632016-12-13 14:54:17 +0800195
Hou Zhiqianga7294ab2016-12-13 14:54:16 +0800196#else /* !CONFIG_OF_BOARD_SETUP */
197void ft_pci_setup(void *blob, bd_t *bd)
198{
199}
200#endif