blob: 26c04a9fc27cd084d4824ead4841e97aa1f5d0a4 [file] [log] [blame]
Xiaowei Bao118e58e2020-07-09 23:31:33 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2020 NXP
4 * Layerscape PCIe EP driver
5 */
6
7#include <common.h>
Wasim Khanb6c6a242020-09-28 16:26:04 +05308#include <asm/arch/fsl_serdes.h>
Xiaowei Bao118e58e2020-07-09 23:31:33 +08009#include <dm.h>
10#include <dm/devres.h>
11#include <errno.h>
12#include <pci_ep.h>
13#include <asm/io.h>
14#include <linux/sizes.h>
15#include <linux/log2.h>
16#include "pcie_layerscape.h"
17
18DECLARE_GLOBAL_DATA_PTR;
19
20static void ls_pcie_ep_enable_cfg(struct ls_pcie_ep *pcie_ep)
21{
22 struct ls_pcie *pcie = pcie_ep->pcie;
23 u32 config;
24
25 config = ctrl_readl(pcie, PCIE_PF_CONFIG);
26 config |= PCIE_CONFIG_READY;
27 ctrl_writel(pcie, config, PCIE_PF_CONFIG);
28}
29
30static int ls_ep_set_bar(struct udevice *dev, uint fn, struct pci_bar *ep_bar)
31{
32 struct ls_pcie_ep *pcie_ep = dev_get_priv(dev);
33 struct ls_pcie *pcie = pcie_ep->pcie;
34 dma_addr_t bar_phys = ep_bar->phys_addr;
35 enum pci_barno bar = ep_bar->barno;
36 u32 reg = PCI_BASE_ADDRESS_0 + (4 * bar);
37 int flags = ep_bar->flags;
38 int type, idx;
39 u64 size;
40
41 idx = bar;
42 /* BAR size is 2^(aperture + 11) */
43 size = max_t(size_t, ep_bar->size, FSL_PCIE_EP_MIN_APERTURE);
44
45 if (!(flags & PCI_BASE_ADDRESS_SPACE))
46 type = PCIE_ATU_TYPE_MEM;
47 else
48 type = PCIE_ATU_TYPE_IO;
49
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080050 ls_pcie_atu_inbound_set(pcie, fn, 0, type, idx, bar, bar_phys);
Xiaowei Bao118e58e2020-07-09 23:31:33 +080051
52 dbi_writel(pcie, lower_32_bits(size - 1), reg + PCIE_NO_SRIOV_BAR_BASE);
53 dbi_writel(pcie, flags, reg);
54
55 if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
56 dbi_writel(pcie, upper_32_bits(size - 1),
57 reg + 4 + PCIE_NO_SRIOV_BAR_BASE);
58 dbi_writel(pcie, 0, reg + 4);
59 }
60
61 return 0;
62}
63
64static struct pci_ep_ops ls_pcie_ep_ops = {
65 .set_bar = ls_ep_set_bar,
66};
67
Xiaowei Baoc5174a52020-07-09 23:31:36 +080068static void ls_pcie_ep_setup_atu(struct ls_pcie_ep *pcie_ep, u32 pf)
Xiaowei Bao118e58e2020-07-09 23:31:33 +080069{
70 struct ls_pcie *pcie = pcie_ep->pcie;
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080071 u32 vf_flag = 0;
Xiaowei Baoc5174a52020-07-09 23:31:36 +080072 u64 phys = 0;
Xiaowei Bao118e58e2020-07-09 23:31:33 +080073
Xiaowei Baoc5174a52020-07-09 23:31:36 +080074 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + pf * SZ_64M;
75
76 phys = ALIGN(phys, PCIE_BAR0_SIZE);
Xiaowei Bao118e58e2020-07-09 23:31:33 +080077 /* ATU 0 : INBOUND : map BAR0 */
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080078 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
Xiaowei Baoc5174a52020-07-09 23:31:36 +080079 0 + pf * BAR_NUM, 0, phys);
Xiaowei Bao118e58e2020-07-09 23:31:33 +080080 /* ATU 1 : INBOUND : map BAR1 */
Xiaowei Baoc5174a52020-07-09 23:31:36 +080081 phys = ALIGN(phys + PCIE_BAR0_SIZE, PCIE_BAR1_SIZE);
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080082 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
Xiaowei Baoc5174a52020-07-09 23:31:36 +080083 1 + pf * BAR_NUM, 1, phys);
Xiaowei Bao118e58e2020-07-09 23:31:33 +080084 /* ATU 2 : INBOUND : map BAR2 */
Xiaowei Baoc5174a52020-07-09 23:31:36 +080085 phys = ALIGN(phys + PCIE_BAR1_SIZE, PCIE_BAR2_SIZE);
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080086 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
Xiaowei Baoc5174a52020-07-09 23:31:36 +080087 2 + pf * BAR_NUM, 2, phys);
88 /* ATU 3 : INBOUND : map BAR2 */
89 phys = ALIGN(phys + PCIE_BAR2_SIZE, PCIE_BAR4_SIZE);
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080090 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
Xiaowei Baoc5174a52020-07-09 23:31:36 +080091 3 + pf * BAR_NUM, 4, phys);
Xiaowei Bao118e58e2020-07-09 23:31:33 +080092
Xiaowei Bao83bf32e2020-07-09 23:31:39 +080093 if (pcie_ep->sriov_flag) {
94 vf_flag = 1;
95 /* ATU 4 : INBOUND : map BAR0 */
96 phys = ALIGN(phys + PCIE_BAR4_SIZE, PCIE_BAR0_SIZE);
97 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
98 4 + pf * BAR_NUM, 0, phys);
99 /* ATU 5 : INBOUND : map BAR1 */
100 phys = ALIGN(phys + PCIE_BAR0_SIZE * PCIE_VF_NUM,
101 PCIE_BAR1_SIZE);
102 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
103 5 + pf * BAR_NUM, 1, phys);
104 /* ATU 6 : INBOUND : map BAR2 */
105 phys = ALIGN(phys + PCIE_BAR1_SIZE * PCIE_VF_NUM,
106 PCIE_BAR2_SIZE);
107 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
108 6 + pf * BAR_NUM, 2, phys);
109 /* ATU 7 : INBOUND : map BAR4 */
110 phys = ALIGN(phys + PCIE_BAR2_SIZE * PCIE_VF_NUM,
111 PCIE_BAR4_SIZE);
112 ls_pcie_atu_inbound_set(pcie, pf, vf_flag, PCIE_ATU_TYPE_MEM,
113 7 + pf * BAR_NUM, 4, phys);
114 }
115
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800116 /* ATU: OUTBOUND : map MEM */
117 ls_pcie_atu_outbound_set(pcie, pf, PCIE_ATU_TYPE_MEM,
118 (u64)pcie_ep->addr_res.start +
119 pf * CONFIG_SYS_PCI_MEMORY_SIZE,
120 0, CONFIG_SYS_PCI_MEMORY_SIZE);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800121}
122
123/* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
124static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
125{
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800126 u32 mask;
127
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800128 /* The least inbound window is 4KiB */
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800129 if (size < SZ_4K)
130 mask = 0;
131 else
132 mask = size - 1;
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800133
134 switch (bar) {
135 case 0:
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800136 writel(mask, bar_base + PCI_BASE_ADDRESS_0);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800137 break;
138 case 1:
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800139 writel(mask, bar_base + PCI_BASE_ADDRESS_1);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800140 break;
141 case 2:
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800142 writel(mask, bar_base + PCI_BASE_ADDRESS_2);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800143 writel(0, bar_base + PCI_BASE_ADDRESS_3);
144 break;
145 case 4:
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800146 writel(mask, bar_base + PCI_BASE_ADDRESS_4);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800147 writel(0, bar_base + PCI_BASE_ADDRESS_5);
148 break;
149 default:
150 break;
151 }
152}
153
154static void ls_pcie_ep_setup_bars(void *bar_base)
155{
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800156 /* BAR0 - 32bit - MEM */
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800157 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800158 /* BAR1 - 32bit - MEM*/
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800159 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800160 /* BAR2 - 64bit - MEM */
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800161 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800162 /* BAR4 - 64bit - MEM */
163 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
164}
165
166static void ls_pcie_ep_setup_vf_bars(void *bar_base)
167{
168 /* VF BAR0 MASK register at offset 0x19c*/
169 bar_base += PCIE_SRIOV_VFBAR0 - PCI_BASE_ADDRESS_0;
170
171 /* VF-BAR0 - 32bit - MEM */
172 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
173 /* VF-BAR1 - 32bit - MEM*/
174 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
175 /* VF-BAR2 - 64bit - MEM */
176 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
177 /* VF-BAR4 - 64bit - MEM */
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800178 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
179}
180
181static void ls_pcie_setup_ep(struct ls_pcie_ep *pcie_ep)
182{
183 u32 sriov;
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800184 u32 pf, vf;
185 void *bar_base = NULL;
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800186 struct ls_pcie *pcie = pcie_ep->pcie;
187
188 sriov = readl(pcie->dbi + PCIE_SRIOV);
189 if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV) {
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800190 pcie_ep->sriov_flag = 1;
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800191 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
Xiaowei Bao15ce1fa2020-07-09 23:31:37 +0800192 /*
193 * The VF_BARn_REG register's Prefetchable and Type bit
194 * fields are overwritten by a write to VF's BAR Mask
195 * register. Before writing to the VF_BARn_MASK_REG
196 * register, write 0b to the PCIE_MISC_CONTROL_1_OFF
197 * register.
198 */
199 writel(0, pcie->dbi + PCIE_MISC_CONTROL_1_OFF);
200
Xiaowei Bao78c56b22020-07-09 23:31:38 +0800201 bar_base = pcie->dbi +
Xiaowei Bao4085e3a2020-07-09 23:31:41 +0800202 PCIE_MASK_OFFSET(pcie_ep->cfg2_flag, pf,
203 pcie_ep->pf1_offset);
Xiaowei Bao78c56b22020-07-09 23:31:38 +0800204
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800205 if (pcie_ep->cfg2_flag) {
Xiaowei Bao78c56b22020-07-09 23:31:38 +0800206 ctrl_writel(pcie,
207 PCIE_LCTRL0_VAL(pf, 0),
208 PCIE_PF_VF_CTRL);
209 ls_pcie_ep_setup_bars(bar_base);
210
211 for (vf = 1; vf <= PCIE_VF_NUM; vf++) {
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800212 ctrl_writel(pcie,
213 PCIE_LCTRL0_VAL(pf, vf),
214 PCIE_PF_VF_CTRL);
Xiaowei Bao78c56b22020-07-09 23:31:38 +0800215 ls_pcie_ep_setup_vf_bars(bar_base);
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800216 }
Xiaowei Bao78c56b22020-07-09 23:31:38 +0800217 } else {
218 ls_pcie_ep_setup_bars(bar_base);
219 ls_pcie_ep_setup_vf_bars(bar_base);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800220 }
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800221
222 ls_pcie_ep_setup_atu(pcie_ep, pf);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800223 }
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800224
225 if (pcie_ep->cfg2_flag) /* Disable CFG2 */
226 ctrl_writel(pcie, 0, PCIE_PF_VF_CTRL);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800227 } else {
228 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800229 ls_pcie_ep_setup_atu(pcie_ep, 0);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800230 }
231
Xiaowei Bao80b5a662020-07-09 23:31:40 +0800232 ls_pcie_dump_atu(pcie, PCIE_ATU_REGION_NUM_SRIOV,
233 PCIE_ATU_REGION_INBOUND);
234
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800235 ls_pcie_ep_enable_cfg(pcie_ep);
236}
237
238static int ls_pcie_ep_probe(struct udevice *dev)
239{
240 struct ls_pcie_ep *pcie_ep = dev_get_priv(dev);
241 struct ls_pcie *pcie;
242 u16 link_sta;
243 int ret;
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800244 u32 svr;
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800245
246 pcie = devm_kmalloc(dev, sizeof(*pcie), GFP_KERNEL);
247 if (!pcie)
248 return -ENOMEM;
249
250 pcie_ep->pcie = pcie;
251
252 pcie->dbi = (void __iomem *)devfdt_get_addr_index(dev, 0);
253 if (!pcie->dbi)
254 return -ENOMEM;
255
256 pcie->ctrl = (void __iomem *)devfdt_get_addr_index(dev, 1);
257 if (!pcie->ctrl)
258 return -ENOMEM;
259
260 ret = fdt_get_named_resource(gd->fdt_blob, dev_of_offset(dev),
261 "reg", "reg-names",
262 "addr_space", &pcie_ep->addr_res);
263 if (ret) {
264 printf("%s: resource \"addr_space\" not found\n", dev->name);
265 return ret;
266 }
267
268 pcie->idx = ((unsigned long)pcie->dbi - PCIE_SYS_BASE_ADDR) /
269 PCIE_CCSR_SIZE;
270
271 pcie->big_endian = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
272 "big-endian");
273
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800274 svr = SVR_SOC_VER(get_svr());
275
Xiaowei Bao4085e3a2020-07-09 23:31:41 +0800276 if (svr == SVR_LX2160A)
277 pcie_ep->pf1_offset = LX2160_PCIE_PF1_OFFSET;
278 else
279 pcie_ep->pf1_offset = LS_PCIE_PF1_OFFSET;
280
Xiaowei Baoc5174a52020-07-09 23:31:36 +0800281 if (svr == SVR_LS2080A || svr == SVR_LS2085A)
282 pcie_ep->cfg2_flag = 1;
283 else
284 pcie_ep->cfg2_flag = 0;
285
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800286 pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
287 if (pcie->mode != PCI_HEADER_TYPE_NORMAL)
288 return 0;
289
290 pcie_ep->max_functions = fdtdec_get_int(gd->fdt_blob,
291 dev_of_offset(dev),
292 "max-functions", 1);
293 pcie_ep->num_ib_wins = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
294 "num-ib-windows", 8);
295 pcie_ep->num_ob_wins = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
296 "num-ob-windows", 8);
297
Wasim Khanb6c6a242020-09-28 16:26:04 +0530298 printf("PCIe%u: %s %s", PCIE_SRDS_PRTCL(pcie->idx), dev->name,
299 "Endpoint");
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800300 ls_pcie_setup_ep(pcie_ep);
301
302 if (!ls_pcie_link_up(pcie)) {
303 /* Let the user know there's no PCIe link */
304 printf(": no link\n");
305 return 0;
306 }
307
308 /* Print the negotiated PCIe link width */
309 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
310 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
311 link_sta & PCIE_LINK_SPEED_MASK);
312
313 return 0;
314}
315
316static int ls_pcie_ep_remove(struct udevice *dev)
317{
318 return 0;
319}
320
321const struct udevice_id ls_pcie_ep_ids[] = {
322 { .compatible = "fsl,ls-pcie-ep" },
323 { }
324};
325
326U_BOOT_DRIVER(pci_layerscape_ep) = {
327 .name = "pci_layerscape_ep",
328 .id = UCLASS_PCI_EP,
329 .of_match = ls_pcie_ep_ids,
330 .ops = &ls_pcie_ep_ops,
331 .probe = ls_pcie_ep_probe,
332 .remove = ls_pcie_ep_remove,
333 .priv_auto_alloc_size = sizeof(struct ls_pcie_ep),
334};