blob: 217b420076ae6ae15c5908876fee9e4a37b41a4d [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 driver
5 */
6
7#include <common.h>
8#include <asm/arch/fsl_serdes.h>
9#include <pci.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Xiaowei Bao118e58e2020-07-09 23:31:33 +080011#include <asm/io.h>
12#include <errno.h>
13#include <malloc.h>
14#include <dm.h>
15#include <dm/devres.h>
16#if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
17 defined(CONFIG_ARM)
18#include <asm/arch/clock.h>
19#endif
20#include "pcie_layerscape.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
Michael Wallee10da1f2021-10-13 18:14:22 +020024struct ls_pcie_drvdata {
25 u32 lut_offset;
26 u32 ctrl_offset;
27 bool big_endian;
28};
29
Xiaowei Bao118e58e2020-07-09 23:31:33 +080030static void ls_pcie_cfg0_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
31{
32 struct ls_pcie *pcie = pcie_rc->pcie;
33
34 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
35 PCIE_ATU_VIEWPORT);
36 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
37}
38
39static void ls_pcie_cfg1_set_busdev(struct ls_pcie_rc *pcie_rc, u32 busdev)
40{
41 struct ls_pcie *pcie = pcie_rc->pcie;
42
43 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
44 PCIE_ATU_VIEWPORT);
45 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
46}
47
48static void ls_pcie_setup_atu(struct ls_pcie_rc *pcie_rc)
49{
50 struct pci_region *io, *mem, *pref;
51 unsigned long long offset = 0;
52 struct ls_pcie *pcie = pcie_rc->pcie;
53 int idx = 0;
54 uint svr;
55
56 svr = get_svr();
57 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
58 offset = LS1021_PCIE_SPACE_OFFSET +
59 LS1021_PCIE_SPACE_SIZE * pcie->idx;
60 }
61
62 /* ATU 0 : OUTBOUND : CFG0 */
63 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
64 PCIE_ATU_TYPE_CFG0,
65 pcie_rc->cfg_res.start + offset,
66 0,
67 fdt_resource_size(&pcie_rc->cfg_res) / 2);
68 /* ATU 1 : OUTBOUND : CFG1 */
69 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
70 PCIE_ATU_TYPE_CFG1,
71 pcie_rc->cfg_res.start + offset +
72 fdt_resource_size(&pcie_rc->cfg_res) / 2,
73 0,
74 fdt_resource_size(&pcie_rc->cfg_res) / 2);
75
76 pci_get_regions(pcie_rc->bus, &io, &mem, &pref);
77 idx = PCIE_ATU_REGION_INDEX1 + 1;
78
79 /* Fix the pcie memory map for LS2088A series SoCs */
80 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
81 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
82 svr == SVR_LS2048A || svr == SVR_LS2044A ||
83 svr == SVR_LS2081A || svr == SVR_LS2041A) {
84 if (io)
85 io->phys_start = (io->phys_start &
86 (PCIE_PHYS_SIZE - 1)) +
87 LS2088A_PCIE1_PHYS_ADDR +
88 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
89 if (mem)
90 mem->phys_start = (mem->phys_start &
91 (PCIE_PHYS_SIZE - 1)) +
92 LS2088A_PCIE1_PHYS_ADDR +
93 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
94 if (pref)
95 pref->phys_start = (pref->phys_start &
96 (PCIE_PHYS_SIZE - 1)) +
97 LS2088A_PCIE1_PHYS_ADDR +
98 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
99 }
100
101 if (io)
102 /* ATU : OUTBOUND : IO */
103 ls_pcie_atu_outbound_set(pcie, idx++,
104 PCIE_ATU_TYPE_IO,
105 io->phys_start + offset,
106 io->bus_start,
107 io->size);
108
109 if (mem)
110 /* ATU : OUTBOUND : MEM */
111 ls_pcie_atu_outbound_set(pcie, idx++,
112 PCIE_ATU_TYPE_MEM,
113 mem->phys_start + offset,
114 mem->bus_start,
115 mem->size);
116
117 if (pref)
118 /* ATU : OUTBOUND : pref */
119 ls_pcie_atu_outbound_set(pcie, idx++,
120 PCIE_ATU_TYPE_MEM,
121 pref->phys_start + offset,
122 pref->bus_start,
123 pref->size);
124
Xiaowei Bao80b5a662020-07-09 23:31:40 +0800125 ls_pcie_dump_atu(pcie, PCIE_ATU_REGION_NUM, PCIE_ATU_REGION_OUTBOUND);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800126}
127
128/* Return 0 if the address is valid, -errno if not valid */
129static int ls_pcie_addr_valid(struct ls_pcie_rc *pcie_rc, pci_dev_t bdf)
130{
131 struct udevice *bus = pcie_rc->bus;
132 struct ls_pcie *pcie = pcie_rc->pcie;
133
134 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
135 return -ENODEV;
136
137 if (!pcie_rc->enabled)
138 return -ENXIO;
139
Simon Glass8b85dfc2020-12-16 21:20:07 -0700140 if (PCI_BUS(bdf) < dev_seq(bus))
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800141 return -EINVAL;
142
Simon Glass8b85dfc2020-12-16 21:20:07 -0700143 if ((PCI_BUS(bdf) > dev_seq(bus)) && (!ls_pcie_link_up(pcie)))
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800144 return -EINVAL;
145
Simon Glass8b85dfc2020-12-16 21:20:07 -0700146 if (PCI_BUS(bdf) <= (dev_seq(bus) + 1) && (PCI_DEV(bdf) > 0))
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800147 return -EINVAL;
148
149 return 0;
150}
151
Vladimir Olteanc67930e2021-09-17 15:11:28 +0300152static int ls_pcie_conf_address(const struct udevice *bus, pci_dev_t bdf,
153 uint offset, void **paddress)
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800154{
155 struct ls_pcie_rc *pcie_rc = dev_get_priv(bus);
156 struct ls_pcie *pcie = pcie_rc->pcie;
157 u32 busdev;
158
159 if (ls_pcie_addr_valid(pcie_rc, bdf))
160 return -EINVAL;
161
Simon Glass8b85dfc2020-12-16 21:20:07 -0700162 if (PCI_BUS(bdf) == dev_seq(bus)) {
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800163 *paddress = pcie->dbi + offset;
164 return 0;
165 }
166
Simon Glass8b85dfc2020-12-16 21:20:07 -0700167 busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - dev_seq(bus)) |
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800168 PCIE_ATU_DEV(PCI_DEV(bdf)) |
169 PCIE_ATU_FUNC(PCI_FUNC(bdf));
170
Simon Glass8b85dfc2020-12-16 21:20:07 -0700171 if (PCI_BUS(bdf) == dev_seq(bus) + 1) {
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800172 ls_pcie_cfg0_set_busdev(pcie_rc, busdev);
173 *paddress = pcie_rc->cfg0 + offset;
174 } else {
175 ls_pcie_cfg1_set_busdev(pcie_rc, busdev);
176 *paddress = pcie_rc->cfg1 + offset;
177 }
178 return 0;
179}
180
181static int ls_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
182 uint offset, ulong *valuep,
183 enum pci_size_t size)
184{
185 return pci_generic_mmap_read_config(bus, ls_pcie_conf_address,
186 bdf, offset, valuep, size);
187}
188
189static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
190 uint offset, ulong value,
191 enum pci_size_t size)
192{
193 return pci_generic_mmap_write_config(bus, ls_pcie_conf_address,
194 bdf, offset, value, size);
195}
196
197/* Clear multi-function bit */
198static void ls_pcie_clear_multifunction(struct ls_pcie_rc *pcie_rc)
199{
200 struct ls_pcie *pcie = pcie_rc->pcie;
201
202 writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
203}
204
205/* Fix class value */
206static void ls_pcie_fix_class(struct ls_pcie_rc *pcie_rc)
207{
208 struct ls_pcie *pcie = pcie_rc->pcie;
209
210 writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
211}
212
213/* Drop MSG TLP except for Vendor MSG */
214static void ls_pcie_drop_msg_tlp(struct ls_pcie_rc *pcie_rc)
215{
216 struct ls_pcie *pcie = pcie_rc->pcie;
217 u32 val;
218
219 val = dbi_readl(pcie, PCIE_STRFMR1);
220 val &= 0xDFFFFFFF;
221 dbi_writel(pcie, val, PCIE_STRFMR1);
222}
223
224/* Disable all bars in RC mode */
225static void ls_pcie_disable_bars(struct ls_pcie_rc *pcie_rc)
226{
227 struct ls_pcie *pcie = pcie_rc->pcie;
228
229 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
230 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
231 dbi_writel(pcie, 0xfffffffe, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
232}
233
234static void ls_pcie_setup_ctrl(struct ls_pcie_rc *pcie_rc)
235{
236 struct ls_pcie *pcie = pcie_rc->pcie;
237
238 ls_pcie_setup_atu(pcie_rc);
239
240 ls_pcie_dbi_ro_wr_en(pcie);
241 ls_pcie_fix_class(pcie_rc);
242 ls_pcie_clear_multifunction(pcie_rc);
243 ls_pcie_drop_msg_tlp(pcie_rc);
244 ls_pcie_dbi_ro_wr_dis(pcie);
245
246 ls_pcie_disable_bars(pcie_rc);
247 pcie_rc->stream_id_cur = 0;
248}
249
250static int ls_pcie_probe(struct udevice *dev)
251{
Michael Wallee10da1f2021-10-13 18:14:22 +0200252 const struct ls_pcie_drvdata *drvdata = (void *)dev_get_driver_data(dev);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800253 struct ls_pcie_rc *pcie_rc = dev_get_priv(dev);
254 const void *fdt = gd->fdt_blob;
255 int node = dev_of_offset(dev);
256 struct ls_pcie *pcie;
257 u16 link_sta;
258 uint svr;
259 int ret;
260 fdt_size_t cfg_size;
261
262 pcie_rc->bus = dev;
263
Hou Zhiqiangae5cbc42021-03-11 15:30:51 +0800264 pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800265 if (!pcie)
266 return -ENOMEM;
267
268 pcie_rc->pcie = pcie;
269
Michael Wallee10da1f2021-10-13 18:14:22 +0200270 /* try resource name of the official binding first */
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800271 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
Michael Wallee10da1f2021-10-13 18:14:22 +0200272 "regs", &pcie_rc->dbi_res);
273 if (ret)
274 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
275 "dbi", &pcie_rc->dbi_res);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800276 if (ret) {
277 printf("ls-pcie: resource \"dbi\" not found\n");
278 return ret;
279 }
280
281 pcie->idx = (pcie_rc->dbi_res.start - PCIE_SYS_BASE_ADDR) /
282 PCIE_CCSR_SIZE;
283
284 list_add(&pcie_rc->list, &ls_pcie_list);
285
286 pcie_rc->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
287 if (!pcie_rc->enabled) {
Wasim Khanb6c6a242020-09-28 16:26:04 +0530288 printf("PCIe%d: %s disabled\n", PCIE_SRDS_PRTCL(pcie->idx),
289 dev->name);
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800290 return 0;
291 }
292
293 pcie->dbi = map_physmem(pcie_rc->dbi_res.start,
294 fdt_resource_size(&pcie_rc->dbi_res),
295 MAP_NOCACHE);
296
297 pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
298 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
299 return 0;
300
Michael Wallee10da1f2021-10-13 18:14:22 +0200301 if (drvdata) {
302 pcie->lut = pcie->dbi + drvdata->lut_offset;
303 } else {
304 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
305 "lut", &pcie_rc->lut_res);
306 if (!ret)
307 pcie->lut = map_physmem(pcie_rc->lut_res.start,
308 fdt_resource_size(&pcie_rc->lut_res),
309 MAP_NOCACHE);
310 }
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800311
Michael Wallee10da1f2021-10-13 18:14:22 +0200312 if (drvdata) {
313 pcie->ctrl = pcie->lut + drvdata->ctrl_offset;
314 } else {
315 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
316 "ctrl", &pcie_rc->ctrl_res);
317 if (!ret)
318 pcie->ctrl = map_physmem(pcie_rc->ctrl_res.start,
319 fdt_resource_size(&pcie_rc->ctrl_res),
320 MAP_NOCACHE);
321 if (!pcie->ctrl)
322 pcie->ctrl = pcie->lut;
323 }
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800324
325 if (!pcie->ctrl) {
326 printf("%s: NOT find CTRL\n", dev->name);
327 return -1;
328 }
329
330 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
331 "config", &pcie_rc->cfg_res);
332 if (ret) {
333 printf("%s: resource \"config\" not found\n", dev->name);
334 return ret;
335 }
336
Wasim Khan49df7c92020-09-28 16:26:13 +0530337 cfg_size = fdt_resource_size(&pcie_rc->cfg_res);
338 if (cfg_size < SZ_8K) {
339 printf("PCIe%d: %s Invalid size(0x%llx) for resource \"config\",expected minimum 0x%x\n",
340 PCIE_SRDS_PRTCL(pcie->idx), dev->name, (u64)cfg_size, SZ_8K);
341 return 0;
342 }
343
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800344 /*
345 * Fix the pcie memory map address and PF control registers address
346 * for LS2088A series SoCs
347 */
348 svr = get_svr();
349 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
350 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
351 svr == SVR_LS2048A || svr == SVR_LS2044A ||
352 svr == SVR_LS2081A || svr == SVR_LS2041A) {
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800353 pcie_rc->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR +
354 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
355 pcie_rc->cfg_res.end = pcie_rc->cfg_res.start + cfg_size;
356 pcie->ctrl = pcie->lut + 0x40000;
357 }
358
359 pcie_rc->cfg0 = map_physmem(pcie_rc->cfg_res.start,
360 fdt_resource_size(&pcie_rc->cfg_res),
361 MAP_NOCACHE);
362 pcie_rc->cfg1 = pcie_rc->cfg0 +
363 fdt_resource_size(&pcie_rc->cfg_res) / 2;
364
Michael Wallee10da1f2021-10-13 18:14:22 +0200365 if (drvdata)
366 pcie->big_endian = drvdata->big_endian;
367 else
368 pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800369
370 debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
371 dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
372 (unsigned long)pcie->ctrl, (unsigned long)pcie_rc->cfg0,
373 pcie->big_endian);
374
Wasim Khanb6c6a242020-09-28 16:26:04 +0530375 printf("PCIe%u: %s %s", PCIE_SRDS_PRTCL(pcie->idx), dev->name,
376 "Root Complex");
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800377 ls_pcie_setup_ctrl(pcie_rc);
378
379 if (!ls_pcie_link_up(pcie)) {
380 /* Let the user know there's no PCIe link */
381 printf(": no link\n");
382 return 0;
383 }
384
385 /* Print the negotiated PCIe link width */
386 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
387 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
388 link_sta & PCIE_LINK_SPEED_MASK);
389
390 return 0;
391}
392
393static const struct dm_pci_ops ls_pcie_ops = {
394 .read_config = ls_pcie_read_config,
395 .write_config = ls_pcie_write_config,
396};
397
Michael Wallee10da1f2021-10-13 18:14:22 +0200398static const struct ls_pcie_drvdata ls1028a_drvdata = {
399 .lut_offset = 0x80000,
400 .ctrl_offset = 0x40000,
401 .big_endian = false,
402};
403
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800404static const struct udevice_id ls_pcie_ids[] = {
405 { .compatible = "fsl,ls-pcie" },
Michael Wallee10da1f2021-10-13 18:14:22 +0200406 { .compatible = "fsl,ls1028a-pcie", .data = (ulong)&ls1028a_drvdata },
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800407 { }
408};
409
410U_BOOT_DRIVER(pci_layerscape) = {
411 .name = "pci_layerscape",
412 .id = UCLASS_PCI,
413 .of_match = ls_pcie_ids,
414 .ops = &ls_pcie_ops,
415 .probe = ls_pcie_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700416 .priv_auto = sizeof(struct ls_pcie_rc),
Xiaowei Bao118e58e2020-07-09 23:31:33 +0800417};