blob: e7913d43a8be398f870ec949601ccc2eb493d672 [file] [log] [blame]
Xiaowei Bao118e58e2020-07-09 23:31:33 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
Hou Zhiqiangbe384162021-11-09 16:56:24 +05303 * Copyright 2020,2021 NXP
Xiaowei Bao118e58e2020-07-09 23:31:33 +08004 * Layerscape PCIe driver
5 */
6
Xiaowei Bao118e58e2020-07-09 23:31:33 +08007#include <asm/arch/fsl_serdes.h>
8#include <pci.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Xiaowei Bao118e58e2020-07-09 23:31:33 +080010#include <asm/io.h>
11#include <errno.h>
12#include <malloc.h>
13#include <dm.h>
14#include <dm/devres.h>
15#if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
16 defined(CONFIG_ARM)
17#include <asm/arch/clock.h>
18#endif
19#include "pcie_layerscape.h"
20
21DECLARE_GLOBAL_DATA_PTR;
22
Michael Wallee10da1f2021-10-13 18:14:22 +020023struct ls_pcie_drvdata {
24 u32 lut_offset;
25 u32 ctrl_offset;
26 bool big_endian;
27};
28
Xiaowei Bao118e58e2020-07-09 23:31:33 +080029static void ls_pcie_cfg0_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
30{
31 struct ls_pcie *pcie = pcie_rc->pcie;
32
33 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
34 PCIE_ATU_VIEWPORT);
35 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
36}
37
38static void ls_pcie_cfg1_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
39{
40 struct ls_pcie *pcie = pcie_rc->pcie;
41
42 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
43 PCIE_ATU_VIEWPORT);
44 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
45}
46
47static void ls_pcie_setup_atu(struct ls_pcie_rc *pcie_rc)
48{
49 struct pci_region *io, *mem, *pref;
50 unsigned long long offset = 0;
51 struct ls_pcie *pcie = pcie_rc->pcie;
52 int idx = 0;
53 uint svr;
54
55 svr = get_svr();
56 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
57 offset = LS1021_PCIE_SPACE_OFFSET +
58 LS1021_PCIE_SPACE_SIZE * pcie->idx;
59 }
60
61 /* ATU 0 : OUTBOUND : CFG0 */
62 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
63 PCIE_ATU_TYPE_CFG0,
64 pcie_rc->cfg_res.start + offset,
65 0,
66 fdt_resource_size(&pcie_rc->cfg_res) / 2);
67 /* ATU 1 : OUTBOUND : CFG1 */
68 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
69 PCIE_ATU_TYPE_CFG1,
70 pcie_rc->cfg_res.start + offset +
71 fdt_resource_size(&pcie_rc->cfg_res) / 2,
72 0,
73 fdt_resource_size(&pcie_rc->cfg_res) / 2);
74
75 pci_get_regions(pcie_rc->bus, &io, &mem, &pref);
76 idx = PCIE_ATU_REGION_INDEX1 + 1;
77
78 /* Fix the pcie memory map for LS2088A series SoCs */
79 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
80 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
81 svr == SVR_LS2048A || svr == SVR_LS2044A ||
82 svr == SVR_LS2081A || svr == SVR_LS2041A) {
83 if (io)
84 io->phys_start = (io->phys_start &
85 (PCIE_PHYS_SIZE - 1)) +
86 LS2088A_PCIE1_PHYS_ADDR +
87 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
88 if (mem)
89 mem->phys_start = (mem->phys_start &
90 (PCIE_PHYS_SIZE - 1)) +
91 LS2088A_PCIE1_PHYS_ADDR +
92 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
93 if (pref)
94 pref->phys_start = (pref->phys_start &
95 (PCIE_PHYS_SIZE - 1)) +
96 LS2088A_PCIE1_PHYS_ADDR +
97 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
98 }
99
100 if (io)
101 /* ATU : OUTBOUND : IO */
102 ls_pcie_atu_outbound_set(pcie, idx++,
103 PCIE_ATU_TYPE_IO,
104 io->phys_start + offset,
105 io->bus_start,
106 io->size);
107
108 if (mem)
109 /* ATU : OUTBOUND : MEM */
110 ls_pcie_atu_outbound_set(pcie, idx++,
111 PCIE_ATU_TYPE_MEM,
112 mem->phys_start + offset,
113 mem->bus_start,
114 mem->size);
115
116 if (pref)
117 /* ATU : OUTBOUND : pref */
118 ls_pcie_atu_outbound_set(pcie, idx++,
119 PCIE_ATU_TYPE_MEM,
120 pref->phys_start + offset,
121 pref->bus_start,
122 pref->size);
123
Xiaowei Bao80b5a662020-07-09 23:31:40 +0800124 ls_pcie_dump_atu(pcie, PCIE_ATU_REGION_NUM, PCIE_ATU_REGION_OUTBOUND);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800125}
126
127/* Return 0 if the address is valid, -errno if not valid */
128static int ls_pcie_addr_valid(struct ls_pcie_rc *pcie_rc, pci_dev_t bdf)
129{
130 struct udevice *bus = pcie_rc->bus;
131 struct ls_pcie *pcie = pcie_rc->pcie;
132
133 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
134 return -ENODEV;
135
136 if (!pcie_rc->enabled)
137 return -ENXIO;
138
Simon Glass8b85dfc2020-12-16 21:20:07 -0700139 if (PCI_BUS(bdf) < dev_seq(bus))
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800140 return -EINVAL;
141
Simon Glass8b85dfc2020-12-16 21:20:07 -0700142 if ((PCI_BUS(bdf) > dev_seq(bus)) && (!ls_pcie_link_up(pcie)))
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800143 return -EINVAL;
144
Simon Glass8b85dfc2020-12-16 21:20:07 -0700145 if (PCI_BUS(bdf) <= (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0))
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800146 return -EINVAL;
147
148 return 0;
149}
150
Vladimir Olteanc67930e2021-09-17 15:11:28 +0300151static int ls_pcie_conf_address(const struct udevice *bus, pci_dev_t bdf,
152 uint offset, void **paddress)
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800153{
154 struct ls_pcie_rc *pcie_rc = dev_get_priv(bus);
155 struct ls_pcie *pcie = pcie_rc->pcie;
156 u32 busdev;
157
158 if (ls_pcie_addr_valid(pcie_rc, bdf))
159 return -EINVAL;
160
Simon Glass8b85dfc2020-12-16 21:20:07 -0700161 if (PCI_BUS(bdf) == dev_seq(bus)) {
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800162 *paddress = pcie->dbi + offset;
163 return 0;
164 }
165
Simon Glass8b85dfc2020-12-16 21:20:07 -0700166 busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - dev_seq(bus)) |
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800167 PCIE_ATU_DEV(PCI_DEV(bdf)) |
168 PCIE_ATU_FUNC(PCI_FUNC(bdf));
169
Simon Glass8b85dfc2020-12-16 21:20:07 -0700170 if (PCI_BUS(bdf) == dev_seq(bus) + 1) {
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800171 ls_pcie_cfg0_set_busdev(pcie_rc, busdev);
172 *paddress = pcie_rc->cfg0 + offset;
173 } else {
174 ls_pcie_cfg1_set_busdev(pcie_rc, busdev);
175 *paddress = pcie_rc->cfg1 + offset;
176 }
177 return 0;
178}
179
180static int ls_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
181 uint offset, ulong *valuep,
182 enum pci_size_t size)
183{
184 return pci_generic_mmap_read_config(bus, ls_pcie_conf_address,
185 bdf, offset, valuep, size);
186}
187
188static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
189 uint offset, ulong value,
190 enum pci_size_t size)
191{
192 return pci_generic_mmap_write_config(bus, ls_pcie_conf_address,
193 bdf, offset, value, size);
194}
195
196/* Clear multi-function bit */
197static void ls_pcie_clear_multifunction(struct ls_pcie_rc *pcie_rc)
198{
199 struct ls_pcie *pcie = pcie_rc->pcie;
200
201 writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
202}
203
204/* Fix class value */
205static void ls_pcie_fix_class(struct ls_pcie_rc *pcie_rc)
206{
207 struct ls_pcie *pcie = pcie_rc->pcie;
208
209 writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
210}
211
212/* Drop MSG TLP except for Vendor MSG */
213static void ls_pcie_drop_msg_tlp(struct ls_pcie_rc *pcie_rc)
214{
215 struct ls_pcie *pcie = pcie_rc->pcie;
216 u32 val;
217
218 val = dbi_readl(pcie, PCIE_STRFMR1);
219 val &= 0xDFFFFFFF;
220 dbi_writel(pcie, val, PCIE_STRFMR1);
221}
222
223/* Disable all bars in RC mode */
224static void ls_pcie_disable_bars(struct ls_pcie_rc *pcie_rc)
225{
226 struct ls_pcie *pcie = pcie_rc->pcie;
227
228 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
229 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
230 dbi_writel(pcie, 0xfffffffe, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
231}
232
233static void ls_pcie_setup_ctrl(struct ls_pcie_rc *pcie_rc)
234{
235 struct ls_pcie *pcie = pcie_rc->pcie;
236
237 ls_pcie_setup_atu(pcie_rc);
238
239 ls_pcie_dbi_ro_wr_en(pcie);
240 ls_pcie_fix_class(pcie_rc);
241 ls_pcie_clear_multifunction(pcie_rc);
242 ls_pcie_drop_msg_tlp(pcie_rc);
243 ls_pcie_dbi_ro_wr_dis(pcie);
244
245 ls_pcie_disable_bars(pcie_rc);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800246}
247
248static int ls_pcie_probe(struct udevice *dev)
249{
Michael Wallee10da1f2021-10-13 18:14:22 +0200250 const struct ls_pcie_drvdata *drvdata = (void *)dev_get_driver_data(dev);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800251 struct ls_pcie_rc *pcie_rc = dev_get_priv(dev);
252 const void *fdt = gd->fdt_blob;
253 int node = dev_of_offset(dev);
254 struct ls_pcie *pcie;
255 u16 link_sta;
256 uint svr;
257 int ret;
258 fdt_size_t cfg_size;
259
260 pcie_rc->bus = dev;
261
Hou Zhiqiangae5cbc42021-03-11 15:30:51 +0800262 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800263 if (!pcie)
264 return -ENOMEM;
265
266 pcie_rc->pcie = pcie;
267
Michael Wallee10da1f2021-10-13 18:14:22 +0200268 /* try resource name of the official binding first */
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800269 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
Michael Wallee10da1f2021-10-13 18:14:22 +0200270 "regs", &pcie_rc->dbi_res);
271 if (ret)
272 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
273 "dbi", &pcie_rc->dbi_res);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800274 if (ret) {
275 printf("ls-pcie: resource \"dbi\" not found\n");
276 return ret;
277 }
278
279 pcie->idx = (pcie_rc->dbi_res.start - PCIE_SYS_BASE_ADDR) /
280 PCIE_CCSR_SIZE;
281
282 list_add(&pcie_rc->list, &ls_pcie_list);
283
284 pcie_rc->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
285 if (!pcie_rc->enabled) {
Wasim Khanb6c6a242020-09-28 16:26:04 +0530286 printf("PCIe%d: %s disabled\n", PCIE_SRDS_PRTCL(pcie->idx),
287 dev->name);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800288 return 0;
289 }
290
291 pcie->dbi = map_physmem(pcie_rc->dbi_res.start,
292 fdt_resource_size(&pcie_rc->dbi_res),
293 MAP_NOCACHE);
294
295 pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
296 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
297 return 0;
298
Michael Wallee10da1f2021-10-13 18:14:22 +0200299 if (drvdata) {
300 pcie->lut = pcie->dbi + drvdata->lut_offset;
301 } else {
302 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
303 "lut", &pcie_rc->lut_res);
304 if (!ret)
305 pcie->lut = map_physmem(pcie_rc->lut_res.start,
306 fdt_resource_size(&pcie_rc->lut_res),
307 MAP_NOCACHE);
308 }
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800309
Michael Wallee10da1f2021-10-13 18:14:22 +0200310 if (drvdata) {
311 pcie->ctrl = pcie->lut + drvdata->ctrl_offset;
312 } else {
313 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
314 "ctrl", &pcie_rc->ctrl_res);
315 if (!ret)
316 pcie->ctrl = map_physmem(pcie_rc->ctrl_res.start,
317 fdt_resource_size(&pcie_rc->ctrl_res),
318 MAP_NOCACHE);
319 if (!pcie->ctrl)
320 pcie->ctrl = pcie->lut;
321 }
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800322
323 if (!pcie->ctrl) {
324 printf("%s: NOT find CTRL\n", dev->name);
325 return -1;
326 }
327
328 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
329 "config", &pcie_rc->cfg_res);
330 if (ret) {
331 printf("%s: resource \"config\" not found\n", dev->name);
332 return ret;
333 }
334
Wasim Khan49df7c92020-09-28 16:26:13 +0530335 cfg_size = fdt_resource_size(&pcie_rc->cfg_res);
336 if (cfg_size < SZ_8K) {
337 printf("PCIe%d: %s Invalid size(0x%llx) for resource \"config\",expected minimum 0x%x\n",
338 PCIE_SRDS_PRTCL(pcie->idx), dev->name, (u64)cfg_size, SZ_8K);
339 return 0;
340 }
341
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800342 /*
343 * Fix the pcie memory map address and PF control registers address
344 * for LS2088A series SoCs
345 */
346 svr = get_svr();
347 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
348 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
349 svr == SVR_LS2048A || svr == SVR_LS2044A ||
350 svr == SVR_LS2081A || svr == SVR_LS2041A) {
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800351 pcie_rc->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR +
352 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
353 pcie_rc->cfg_res.end = pcie_rc->cfg_res.start + cfg_size;
354 pcie->ctrl = pcie->lut + 0x40000;
355 }
356
357 pcie_rc->cfg0 = map_physmem(pcie_rc->cfg_res.start,
358 fdt_resource_size(&pcie_rc->cfg_res),
359 MAP_NOCACHE);
360 pcie_rc->cfg1 = pcie_rc->cfg0 +
361 fdt_resource_size(&pcie_rc->cfg_res) / 2;
362
Michael Wallee10da1f2021-10-13 18:14:22 +0200363 if (drvdata)
364 pcie->big_endian = drvdata->big_endian;
365 else
366 pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800367
368 debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
369 dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
370 (unsigned long)pcie->ctrl, (unsigned long)pcie_rc->cfg0,
371 pcie->big_endian);
372
Wasim Khanb6c6a242020-09-28 16:26:04 +0530373 printf("PCIe%u: %s %s", PCIE_SRDS_PRTCL(pcie->idx), dev->name,
374 "Root Complex");
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800375 ls_pcie_setup_ctrl(pcie_rc);
376
377 if (!ls_pcie_link_up(pcie)) {
378 /* Let the user know there's no PCIe link */
379 printf(": no link\n");
380 return 0;
381 }
382
383 /* Print the negotiated PCIe link width */
384 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
385 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
386 link_sta & PCIE_LINK_SPEED_MASK);
387
388 return 0;
389}
390
391static const struct dm_pci_ops ls_pcie_ops = {
392 .read_config = ls_pcie_read_config,
393 .write_config = ls_pcie_write_config,
394};
395
Michael Wallee10da1f2021-10-13 18:14:22 +0200396static const struct ls_pcie_drvdata ls1028a_drvdata = {
397 .lut_offset = 0x80000,
398 .ctrl_offset = 0x40000,
399 .big_endian = false,
400};
401
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800402static const struct udevice_id ls_pcie_ids[] = {
403 { .compatible = "fsl,ls-pcie" },
Michael Wallee10da1f2021-10-13 18:14:22 +0200404 { .compatible = "fsl,ls1028a-pcie", .data = (ulong)&ls1028a_drvdata },
Mathew McBridee0f74092023-04-12 07:38:17 +0000405 { .compatible = "fsl,ls1088a-pcie", .data = (ulong)&ls1028a_drvdata },
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800406 { }
407};
408
409U_BOOT_DRIVER(pci_layerscape) = {
410 .name = "pci_layerscape",
411 .id = UCLASS_PCI,
412 .of_match = ls_pcie_ids,
413 .ops = &ls_pcie_ops,
414 .probe = ls_pcie_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700415 .priv_auto = sizeof(struct ls_pcie_rc),
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800416};