blob: 39b6d40802500a4b95ec1d3cbd6bf2a75a9ac2df [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Minghuan Liana4d6b612014-10-31 13:43:44 +08002/*
Wasim Khan4a0e9be2019-11-15 09:23:34 +00003 * Copyright 2017-2019 NXP
Minghuan Lianfdab5452015-01-21 17:29:20 +08004 * Copyright 2014-2015 Freescale Semiconductor, Inc.
Minghuan Liana4d6b612014-10-31 13:43:44 +08005 * Layerscape PCIe driver
Minghuan Liana4d6b612014-10-31 13:43:44 +08006 */
7
8#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Minghuan Liana4d6b612014-10-31 13:43:44 +080010#include <asm/arch/fsl_serdes.h>
11#include <pci.h>
12#include <asm/io.h>
Minghuan Lianfdab5452015-01-21 17:29:20 +080013#include <errno.h>
14#include <malloc.h>
Minghuan Lianc1067842016-12-13 14:54:17 +080015#include <dm.h>
Simon Glass243182c2017-05-17 08:23:06 -060016#if defined(CONFIG_FSL_LSCH2) || defined(CONFIG_FSL_LSCH3) || \
17 defined(CONFIG_ARM)
18#include <asm/arch/clock.h>
19#endif
Hou Zhiqiang09716a7b2016-12-13 14:54:16 +080020#include "pcie_layerscape.h"
Minghuan Lianfdab5452015-01-21 17:29:20 +080021
Minghuan Lianc1067842016-12-13 14:54:17 +080022DECLARE_GLOBAL_DATA_PTR;
23
Minghuan Lianc1067842016-12-13 14:54:17 +080024LIST_HEAD(ls_pcie_list);
25
26static unsigned int dbi_readl(struct ls_pcie *pcie, unsigned int offset)
27{
28 return in_le32(pcie->dbi + offset);
29}
30
31static void dbi_writel(struct ls_pcie *pcie, unsigned int value,
32 unsigned int offset)
33{
34 out_le32(pcie->dbi + offset, value);
35}
36
37static unsigned int ctrl_readl(struct ls_pcie *pcie, unsigned int offset)
38{
39 if (pcie->big_endian)
40 return in_be32(pcie->ctrl + offset);
41 else
42 return in_le32(pcie->ctrl + offset);
43}
44
45static void ctrl_writel(struct ls_pcie *pcie, unsigned int value,
46 unsigned int offset)
47{
48 if (pcie->big_endian)
49 out_be32(pcie->ctrl + offset, value);
50 else
51 out_le32(pcie->ctrl + offset, value);
52}
53
54static int ls_pcie_ltssm(struct ls_pcie *pcie)
55{
56 u32 state;
57 uint svr;
58
59 svr = get_svr();
60 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
61 state = ctrl_readl(pcie, LS1021_PEXMSCPORTSR(pcie->idx));
62 state = (state >> LS1021_LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
63 } else {
64 state = ctrl_readl(pcie, PCIE_PF_DBG) & LTSSM_STATE_MASK;
65 }
66
67 return state;
68}
69
70static int ls_pcie_link_up(struct ls_pcie *pcie)
71{
72 int ltssm;
73
74 ltssm = ls_pcie_ltssm(pcie);
75 if (ltssm < LTSSM_PCIE_L0)
76 return 0;
77
78 return 1;
79}
80
81static void ls_pcie_cfg0_set_busdev(struct ls_pcie *pcie, u32 busdev)
82{
83 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
84 PCIE_ATU_VIEWPORT);
85 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
86}
87
88static void ls_pcie_cfg1_set_busdev(struct ls_pcie *pcie, u32 busdev)
89{
90 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
91 PCIE_ATU_VIEWPORT);
92 dbi_writel(pcie, busdev, PCIE_ATU_LOWER_TARGET);
93}
94
95static void ls_pcie_atu_outbound_set(struct ls_pcie *pcie, int idx, int type,
96 u64 phys, u64 bus_addr, pci_size_t size)
97{
98 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | idx, PCIE_ATU_VIEWPORT);
99 dbi_writel(pcie, (u32)phys, PCIE_ATU_LOWER_BASE);
100 dbi_writel(pcie, phys >> 32, PCIE_ATU_UPPER_BASE);
101 dbi_writel(pcie, (u32)phys + size - 1, PCIE_ATU_LIMIT);
102 dbi_writel(pcie, (u32)bus_addr, PCIE_ATU_LOWER_TARGET);
103 dbi_writel(pcie, bus_addr >> 32, PCIE_ATU_UPPER_TARGET);
104 dbi_writel(pcie, type, PCIE_ATU_CR1);
105 dbi_writel(pcie, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
106}
107
108/* Use bar match mode and MEM type as default */
109static void ls_pcie_atu_inbound_set(struct ls_pcie *pcie, int idx,
110 int bar, u64 phys)
111{
112 dbi_writel(pcie, PCIE_ATU_REGION_INBOUND | idx, PCIE_ATU_VIEWPORT);
113 dbi_writel(pcie, (u32)phys, PCIE_ATU_LOWER_TARGET);
114 dbi_writel(pcie, phys >> 32, PCIE_ATU_UPPER_TARGET);
115 dbi_writel(pcie, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
116 dbi_writel(pcie, PCIE_ATU_ENABLE | PCIE_ATU_BAR_MODE_ENABLE |
117 PCIE_ATU_BAR_NUM(bar), PCIE_ATU_CR2);
118}
119
120static void ls_pcie_dump_atu(struct ls_pcie *pcie)
121{
122 int i;
123
124 for (i = 0; i < PCIE_ATU_REGION_NUM; i++) {
125 dbi_writel(pcie, PCIE_ATU_REGION_OUTBOUND | i,
126 PCIE_ATU_VIEWPORT);
127 debug("iATU%d:\n", i);
128 debug("\tLOWER PHYS 0x%08x\n",
129 dbi_readl(pcie, PCIE_ATU_LOWER_BASE));
130 debug("\tUPPER PHYS 0x%08x\n",
131 dbi_readl(pcie, PCIE_ATU_UPPER_BASE));
132 debug("\tLOWER BUS 0x%08x\n",
133 dbi_readl(pcie, PCIE_ATU_LOWER_TARGET));
134 debug("\tUPPER BUS 0x%08x\n",
135 dbi_readl(pcie, PCIE_ATU_UPPER_TARGET));
136 debug("\tLIMIT 0x%08x\n",
137 readl(pcie->dbi + PCIE_ATU_LIMIT));
138 debug("\tCR1 0x%08x\n",
139 dbi_readl(pcie, PCIE_ATU_CR1));
140 debug("\tCR2 0x%08x\n",
141 dbi_readl(pcie, PCIE_ATU_CR2));
142 }
143}
144
145static void ls_pcie_setup_atu(struct ls_pcie *pcie)
146{
147 struct pci_region *io, *mem, *pref;
148 unsigned long long offset = 0;
149 int idx = 0;
150 uint svr;
151
152 svr = get_svr();
153 if (((svr >> SVR_VAR_PER_SHIFT) & SVR_LS102XA_MASK) == SVR_LS102XA) {
154 offset = LS1021_PCIE_SPACE_OFFSET +
155 LS1021_PCIE_SPACE_SIZE * pcie->idx;
156 }
157
158 /* ATU 0 : OUTBOUND : CFG0 */
159 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX0,
160 PCIE_ATU_TYPE_CFG0,
161 pcie->cfg_res.start + offset,
162 0,
163 fdt_resource_size(&pcie->cfg_res) / 2);
164 /* ATU 1 : OUTBOUND : CFG1 */
165 ls_pcie_atu_outbound_set(pcie, PCIE_ATU_REGION_INDEX1,
166 PCIE_ATU_TYPE_CFG1,
167 pcie->cfg_res.start + offset +
168 fdt_resource_size(&pcie->cfg_res) / 2,
169 0,
170 fdt_resource_size(&pcie->cfg_res) / 2);
171
172 pci_get_regions(pcie->bus, &io, &mem, &pref);
173 idx = PCIE_ATU_REGION_INDEX1 + 1;
174
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800175 /* Fix the pcie memory map for LS2088A series SoCs */
176 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
177 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530178 svr == SVR_LS2048A || svr == SVR_LS2044A ||
179 svr == SVR_LS2081A || svr == SVR_LS2041A) {
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800180 if (io)
181 io->phys_start = (io->phys_start &
182 (PCIE_PHYS_SIZE - 1)) +
183 LS2088A_PCIE1_PHYS_ADDR +
184 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
185 if (mem)
186 mem->phys_start = (mem->phys_start &
187 (PCIE_PHYS_SIZE - 1)) +
188 LS2088A_PCIE1_PHYS_ADDR +
189 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
190 if (pref)
191 pref->phys_start = (pref->phys_start &
192 (PCIE_PHYS_SIZE - 1)) +
193 LS2088A_PCIE1_PHYS_ADDR +
194 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
195 }
196
Minghuan Lianc1067842016-12-13 14:54:17 +0800197 if (io)
198 /* ATU : OUTBOUND : IO */
199 ls_pcie_atu_outbound_set(pcie, idx++,
200 PCIE_ATU_TYPE_IO,
201 io->phys_start + offset,
202 io->bus_start,
203 io->size);
204
205 if (mem)
206 /* ATU : OUTBOUND : MEM */
207 ls_pcie_atu_outbound_set(pcie, idx++,
208 PCIE_ATU_TYPE_MEM,
209 mem->phys_start + offset,
210 mem->bus_start,
211 mem->size);
212
213 if (pref)
214 /* ATU : OUTBOUND : pref */
215 ls_pcie_atu_outbound_set(pcie, idx++,
216 PCIE_ATU_TYPE_MEM,
217 pref->phys_start + offset,
218 pref->bus_start,
219 pref->size);
220
221 ls_pcie_dump_atu(pcie);
222}
223
224/* Return 0 if the address is valid, -errno if not valid */
225static int ls_pcie_addr_valid(struct ls_pcie *pcie, pci_dev_t bdf)
226{
227 struct udevice *bus = pcie->bus;
228
Xiaowei Bao4da0ae22018-10-26 09:56:25 +0800229 if (pcie->mode == PCI_HEADER_TYPE_NORMAL)
230 return -ENODEV;
231
Minghuan Lianc1067842016-12-13 14:54:17 +0800232 if (!pcie->enabled)
233 return -ENXIO;
234
235 if (PCI_BUS(bdf) < bus->seq)
236 return -EINVAL;
237
238 if ((PCI_BUS(bdf) > bus->seq) && (!ls_pcie_link_up(pcie)))
239 return -EINVAL;
240
241 if (PCI_BUS(bdf) <= (bus->seq + 1) && (PCI_DEV(bdf) > 0))
242 return -EINVAL;
243
244 return 0;
245}
246
Simon Glass2a311e82020-01-27 08:49:37 -0700247int ls_pcie_conf_address(const struct udevice *bus, pci_dev_t bdf,
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300248 uint offset, void **paddress)
Minghuan Lianc1067842016-12-13 14:54:17 +0800249{
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300250 struct ls_pcie *pcie = dev_get_priv(bus);
Minghuan Lianc1067842016-12-13 14:54:17 +0800251 u32 busdev;
252
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300253 if (ls_pcie_addr_valid(pcie, bdf))
254 return -EINVAL;
255
256 if (PCI_BUS(bdf) == bus->seq) {
257 *paddress = pcie->dbi + offset;
258 return 0;
259 }
Minghuan Lianc1067842016-12-13 14:54:17 +0800260
Minghuan Liana1c94382017-10-20 10:45:50 +0800261 busdev = PCIE_ATU_BUS(PCI_BUS(bdf) - bus->seq) |
Minghuan Lianc1067842016-12-13 14:54:17 +0800262 PCIE_ATU_DEV(PCI_DEV(bdf)) |
263 PCIE_ATU_FUNC(PCI_FUNC(bdf));
264
265 if (PCI_BUS(bdf) == bus->seq + 1) {
266 ls_pcie_cfg0_set_busdev(pcie, busdev);
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300267 *paddress = pcie->cfg0 + offset;
Minghuan Lianc1067842016-12-13 14:54:17 +0800268 } else {
269 ls_pcie_cfg1_set_busdev(pcie, busdev);
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300270 *paddress = pcie->cfg1 + offset;
Minghuan Lianc1067842016-12-13 14:54:17 +0800271 }
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300272 return 0;
Minghuan Lianc1067842016-12-13 14:54:17 +0800273}
274
Simon Glass2a311e82020-01-27 08:49:37 -0700275static int ls_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Minghuan Lianc1067842016-12-13 14:54:17 +0800276 uint offset, ulong *valuep,
277 enum pci_size_t size)
278{
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300279 return pci_generic_mmap_read_config(bus, ls_pcie_conf_address,
280 bdf, offset, valuep, size);
Minghuan Lianc1067842016-12-13 14:54:17 +0800281}
282
283static int ls_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
284 uint offset, ulong value,
285 enum pci_size_t size)
286{
Tuomas Tynkkynen544a2e72017-09-19 23:18:05 +0300287 return pci_generic_mmap_write_config(bus, ls_pcie_conf_address,
288 bdf, offset, value, size);
Minghuan Lianc1067842016-12-13 14:54:17 +0800289}
290
291/* Clear multi-function bit */
292static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
293{
294 writeb(PCI_HEADER_TYPE_BRIDGE, pcie->dbi + PCI_HEADER_TYPE);
295}
296
297/* Fix class value */
298static void ls_pcie_fix_class(struct ls_pcie *pcie)
299{
300 writew(PCI_CLASS_BRIDGE_PCI, pcie->dbi + PCI_CLASS_DEVICE);
301}
302
303/* Drop MSG TLP except for Vendor MSG */
304static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
305{
306 u32 val;
307
308 val = dbi_readl(pcie, PCIE_STRFMR1);
309 val &= 0xDFFFFFFF;
310 dbi_writel(pcie, val, PCIE_STRFMR1);
311}
312
313/* Disable all bars in RC mode */
314static void ls_pcie_disable_bars(struct ls_pcie *pcie)
315{
Minghuan Lianc1067842016-12-13 14:54:17 +0800316 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_0);
317 dbi_writel(pcie, 0, PCIE_CS2_OFFSET + PCI_BASE_ADDRESS_1);
Hou Zhiqiangca3815d2019-12-17 10:10:40 +0000318 dbi_writel(pcie, 0xfffffffe, PCIE_CS2_OFFSET + PCI_ROM_ADDRESS1);
Minghuan Lianc1067842016-12-13 14:54:17 +0800319}
320
321static void ls_pcie_setup_ctrl(struct ls_pcie *pcie)
322{
323 ls_pcie_setup_atu(pcie);
324
325 dbi_writel(pcie, 1, PCIE_DBI_RO_WR_EN);
326 ls_pcie_fix_class(pcie);
327 ls_pcie_clear_multifunction(pcie);
328 ls_pcie_drop_msg_tlp(pcie);
329 dbi_writel(pcie, 0, PCIE_DBI_RO_WR_EN);
330
331 ls_pcie_disable_bars(pcie);
Wasim Khan4a0e9be2019-11-15 09:23:34 +0000332 pcie->stream_id_cur = 0;
Minghuan Lianc1067842016-12-13 14:54:17 +0800333}
334
335static void ls_pcie_ep_setup_atu(struct ls_pcie *pcie)
336{
337 u64 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE;
338
339 /* ATU 0 : INBOUND : map BAR0 */
340 ls_pcie_atu_inbound_set(pcie, 0, 0, phys);
341 /* ATU 1 : INBOUND : map BAR1 */
342 phys += PCIE_BAR1_SIZE;
343 ls_pcie_atu_inbound_set(pcie, 1, 1, phys);
344 /* ATU 2 : INBOUND : map BAR2 */
345 phys += PCIE_BAR2_SIZE;
346 ls_pcie_atu_inbound_set(pcie, 2, 2, phys);
347 /* ATU 3 : INBOUND : map BAR4 */
348 phys = CONFIG_SYS_PCI_EP_MEMORY_BASE + PCIE_BAR4_SIZE;
349 ls_pcie_atu_inbound_set(pcie, 3, 4, phys);
350
351 /* ATU 0 : OUTBOUND : map MEM */
352 ls_pcie_atu_outbound_set(pcie, 0,
353 PCIE_ATU_TYPE_MEM,
354 pcie->cfg_res.start,
355 0,
356 CONFIG_SYS_PCI_MEMORY_SIZE);
357}
358
359/* BAR0 and BAR1 are 32bit BAR2 and BAR4 are 64bit */
360static void ls_pcie_ep_setup_bar(void *bar_base, int bar, u32 size)
361{
362 /* The least inbound window is 4KiB */
363 if (size < 4 * 1024)
364 return;
365
366 switch (bar) {
367 case 0:
368 writel(size - 1, bar_base + PCI_BASE_ADDRESS_0);
369 break;
370 case 1:
371 writel(size - 1, bar_base + PCI_BASE_ADDRESS_1);
372 break;
373 case 2:
374 writel(size - 1, bar_base + PCI_BASE_ADDRESS_2);
375 writel(0, bar_base + PCI_BASE_ADDRESS_3);
376 break;
377 case 4:
378 writel(size - 1, bar_base + PCI_BASE_ADDRESS_4);
379 writel(0, bar_base + PCI_BASE_ADDRESS_5);
380 break;
381 default:
382 break;
383 }
384}
385
386static void ls_pcie_ep_setup_bars(void *bar_base)
387{
388 /* BAR0 - 32bit - 4K configuration */
389 ls_pcie_ep_setup_bar(bar_base, 0, PCIE_BAR0_SIZE);
390 /* BAR1 - 32bit - 8K MSIX*/
391 ls_pcie_ep_setup_bar(bar_base, 1, PCIE_BAR1_SIZE);
392 /* BAR2 - 64bit - 4K MEM desciptor */
393 ls_pcie_ep_setup_bar(bar_base, 2, PCIE_BAR2_SIZE);
394 /* BAR4 - 64bit - 1M MEM*/
395 ls_pcie_ep_setup_bar(bar_base, 4, PCIE_BAR4_SIZE);
396}
397
Hou Zhiqiang5faf5612017-02-10 15:42:11 +0800398static void ls_pcie_ep_enable_cfg(struct ls_pcie *pcie)
399{
Pankaj Bansalc63fac82019-10-14 11:43:19 +0000400 u32 config;
401
402 config = ctrl_readl(pcie, PCIE_PF_CONFIG);
403 config |= PCIE_CONFIG_READY;
404 ctrl_writel(pcie, config, PCIE_PF_CONFIG);
Hou Zhiqiang5faf5612017-02-10 15:42:11 +0800405}
406
Minghuan Lianc1067842016-12-13 14:54:17 +0800407static void ls_pcie_setup_ep(struct ls_pcie *pcie)
408{
409 u32 sriov;
410
411 sriov = readl(pcie->dbi + PCIE_SRIOV);
412 if (PCI_EXT_CAP_ID(sriov) == PCI_EXT_CAP_ID_SRIOV) {
413 int pf, vf;
414
415 for (pf = 0; pf < PCIE_PF_NUM; pf++) {
416 for (vf = 0; vf <= PCIE_VF_NUM; vf++) {
417 ctrl_writel(pcie, PCIE_LCTRL0_VAL(pf, vf),
418 PCIE_PF_VF_CTRL);
419
420 ls_pcie_ep_setup_bars(pcie->dbi);
421 ls_pcie_ep_setup_atu(pcie);
422 }
423 }
424 /* Disable CFG2 */
425 ctrl_writel(pcie, 0, PCIE_PF_VF_CTRL);
426 } else {
427 ls_pcie_ep_setup_bars(pcie->dbi + PCIE_NO_SRIOV_BAR_BASE);
428 ls_pcie_ep_setup_atu(pcie);
429 }
Hou Zhiqiang5faf5612017-02-10 15:42:11 +0800430
431 ls_pcie_ep_enable_cfg(pcie);
Minghuan Lianc1067842016-12-13 14:54:17 +0800432}
433
434static int ls_pcie_probe(struct udevice *dev)
435{
436 struct ls_pcie *pcie = dev_get_priv(dev);
437 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700438 int node = dev_of_offset(dev);
Minghuan Lianc1067842016-12-13 14:54:17 +0800439 u16 link_sta;
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800440 uint svr;
Minghuan Lianc1067842016-12-13 14:54:17 +0800441 int ret;
Hou Zhiqiangb9dd2e62017-07-18 11:29:12 +0800442 fdt_size_t cfg_size;
Minghuan Lianc1067842016-12-13 14:54:17 +0800443
444 pcie->bus = dev;
445
446 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
447 "dbi", &pcie->dbi_res);
448 if (ret) {
449 printf("ls-pcie: resource \"dbi\" not found\n");
450 return ret;
451 }
452
453 pcie->idx = (pcie->dbi_res.start - PCIE_SYS_BASE_ADDR) / PCIE_CCSR_SIZE;
454
455 list_add(&pcie->list, &ls_pcie_list);
456
457 pcie->enabled = is_serdes_configured(PCIE_SRDS_PRTCL(pcie->idx));
458 if (!pcie->enabled) {
459 printf("PCIe%d: %s disabled\n", pcie->idx, dev->name);
460 return 0;
461 }
462
463 pcie->dbi = map_physmem(pcie->dbi_res.start,
464 fdt_resource_size(&pcie->dbi_res),
465 MAP_NOCACHE);
466
467 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
468 "lut", &pcie->lut_res);
469 if (!ret)
470 pcie->lut = map_physmem(pcie->lut_res.start,
471 fdt_resource_size(&pcie->lut_res),
472 MAP_NOCACHE);
473
474 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
475 "ctrl", &pcie->ctrl_res);
476 if (!ret)
477 pcie->ctrl = map_physmem(pcie->ctrl_res.start,
478 fdt_resource_size(&pcie->ctrl_res),
479 MAP_NOCACHE);
480 if (!pcie->ctrl)
481 pcie->ctrl = pcie->lut;
482
483 if (!pcie->ctrl) {
484 printf("%s: NOT find CTRL\n", dev->name);
485 return -1;
486 }
487
488 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
489 "config", &pcie->cfg_res);
490 if (ret) {
491 printf("%s: resource \"config\" not found\n", dev->name);
492 return ret;
493 }
494
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800495 /*
496 * Fix the pcie memory map address and PF control registers address
497 * for LS2088A series SoCs
498 */
499 svr = get_svr();
500 svr = (svr >> SVR_VAR_PER_SHIFT) & 0xFFFFFE;
501 if (svr == SVR_LS2088A || svr == SVR_LS2084A ||
Priyanka Jain2b361782017-04-27 15:08:06 +0530502 svr == SVR_LS2048A || svr == SVR_LS2044A ||
503 svr == SVR_LS2081A || svr == SVR_LS2041A) {
Hou Zhiqiangb9dd2e62017-07-18 11:29:12 +0800504 cfg_size = fdt_resource_size(&pcie->cfg_res);
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800505 pcie->cfg_res.start = LS2088A_PCIE1_PHYS_ADDR +
506 LS2088A_PCIE_PHYS_SIZE * pcie->idx;
Hou Zhiqiangb9dd2e62017-07-18 11:29:12 +0800507 pcie->cfg_res.end = pcie->cfg_res.start + cfg_size;
Hou Zhiqiang92fecb52017-03-03 12:35:09 +0800508 pcie->ctrl = pcie->lut + 0x40000;
509 }
510
Minghuan Lianc1067842016-12-13 14:54:17 +0800511 pcie->cfg0 = map_physmem(pcie->cfg_res.start,
512 fdt_resource_size(&pcie->cfg_res),
513 MAP_NOCACHE);
514 pcie->cfg1 = pcie->cfg0 + fdt_resource_size(&pcie->cfg_res) / 2;
515
516 pcie->big_endian = fdtdec_get_bool(fdt, node, "big-endian");
517
518 debug("%s dbi:%lx lut:%lx ctrl:0x%lx cfg0:0x%lx, big-endian:%d\n",
519 dev->name, (unsigned long)pcie->dbi, (unsigned long)pcie->lut,
520 (unsigned long)pcie->ctrl, (unsigned long)pcie->cfg0,
521 pcie->big_endian);
522
Xiaowei Bao77d56f92018-10-26 09:56:24 +0800523 pcie->mode = readb(pcie->dbi + PCI_HEADER_TYPE) & 0x7f;
Minghuan Lianc1067842016-12-13 14:54:17 +0800524
Xiaowei Bao77d56f92018-10-26 09:56:24 +0800525 if (pcie->mode == PCI_HEADER_TYPE_NORMAL) {
526 printf("PCIe%u: %s %s", pcie->idx, dev->name, "Endpoint");
527 ls_pcie_setup_ep(pcie);
528 } else {
529 printf("PCIe%u: %s %s", pcie->idx, dev->name, "Root Complex");
530 ls_pcie_setup_ctrl(pcie);
531 }
Minghuan Lianc1067842016-12-13 14:54:17 +0800532
533 if (!ls_pcie_link_up(pcie)) {
534 /* Let the user know there's no PCIe link */
535 printf(": no link\n");
536 return 0;
537 }
538
539 /* Print the negotiated PCIe link width */
540 link_sta = readw(pcie->dbi + PCIE_LINK_STA);
541 printf(": x%d gen%d\n", (link_sta & PCIE_LINK_WIDTH_MASK) >> 4,
542 link_sta & PCIE_LINK_SPEED_MASK);
543
544 return 0;
545}
546
547static const struct dm_pci_ops ls_pcie_ops = {
548 .read_config = ls_pcie_read_config,
549 .write_config = ls_pcie_write_config,
550};
551
552static const struct udevice_id ls_pcie_ids[] = {
553 { .compatible = "fsl,ls-pcie" },
554 { }
555};
556
557U_BOOT_DRIVER(pci_layerscape) = {
558 .name = "pci_layerscape",
559 .id = UCLASS_PCI,
560 .of_match = ls_pcie_ids,
561 .ops = &ls_pcie_ops,
562 .probe = ls_pcie_probe,
563 .priv_auto_alloc_size = sizeof(struct ls_pcie),
564};