blob: 9a440f57d6e71b15ea9e220d3c760c63cce676d1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Anton Schubert9c28d612015-08-11 11:54:01 +02002/*
3 * PCIe driver for Marvell MVEBU SoCs
4 *
5 * Based on Barebox drivers/pci/pci-mvebu.c
6 *
7 * Ported to U-Boot by:
8 * Anton Schubert <anton.schubert@gmx.de>
9 * Stefan Roese <sr@denx.de>
Anton Schubert9c28d612015-08-11 11:54:01 +020010 */
11
12#include <common.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010013#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070015#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010017#include <dm/device-internal.h>
18#include <dm/lists.h>
19#include <dm/of_access.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020020#include <pci.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020021#include <asm/io.h>
22#include <asm/arch/cpu.h>
23#include <asm/arch/soc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010025#include <linux/errno.h>
26#include <linux/ioport.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020027#include <linux/mbus.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31/* PCIe unit register offsets */
32#define SELECT(x, n) ((x >> n) & 1UL)
33
34#define PCIE_DEV_ID_OFF 0x0000
35#define PCIE_CMD_OFF 0x0004
36#define PCIE_DEV_REV_OFF 0x0008
37#define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
38#define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
39#define PCIE_CAPAB_OFF 0x0060
40#define PCIE_CTRL_STAT_OFF 0x0068
41#define PCIE_HEADER_LOG_4_OFF 0x0128
42#define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
43#define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
44#define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
45#define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
46#define PCIE_WIN5_CTRL_OFF 0x1880
47#define PCIE_WIN5_BASE_OFF 0x1884
48#define PCIE_WIN5_REMAP_OFF 0x188c
49#define PCIE_CONF_ADDR_OFF 0x18f8
50#define PCIE_CONF_ADDR_EN BIT(31)
51#define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
52#define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
53#define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
54#define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
55#define PCIE_CONF_ADDR(dev, reg) \
56 (PCIE_CONF_BUS(PCI_BUS(dev)) | PCIE_CONF_DEV(PCI_DEV(dev)) | \
57 PCIE_CONF_FUNC(PCI_FUNC(dev)) | PCIE_CONF_REG(reg) | \
58 PCIE_CONF_ADDR_EN)
59#define PCIE_CONF_DATA_OFF 0x18fc
60#define PCIE_MASK_OFF 0x1910
61#define PCIE_MASK_ENABLE_INTS (0xf << 24)
62#define PCIE_CTRL_OFF 0x1a00
63#define PCIE_CTRL_X1_MODE BIT(0)
64#define PCIE_STAT_OFF 0x1a04
65#define PCIE_STAT_BUS (0xff << 8)
66#define PCIE_STAT_DEV (0x1f << 16)
67#define PCIE_STAT_LINK_DOWN BIT(0)
68#define PCIE_DEBUG_CTRL 0x1a60
69#define PCIE_DEBUG_SOFT_RESET BIT(20)
70
Anton Schubert9c28d612015-08-11 11:54:01 +020071struct mvebu_pcie {
72 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020073 void __iomem *base;
74 void __iomem *membase;
75 struct resource mem;
76 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010077 struct resource io;
Anton Schubert9c28d612015-08-11 11:54:01 +020078 u32 port;
79 u32 lane;
Stefan Roese94f453e2019-01-25 11:52:43 +010080 int devfn;
Anton Schubert9c28d612015-08-11 11:54:01 +020081 u32 lane_mask;
82 pci_dev_t dev;
Stefan Roese94f453e2019-01-25 11:52:43 +010083 char name[16];
84 unsigned int mem_target;
85 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010086 unsigned int io_target;
87 unsigned int io_attr;
Anton Schubert9c28d612015-08-11 11:54:01 +020088};
89
Anton Schubert9c28d612015-08-11 11:54:01 +020090/*
91 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030092 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-08-11 11:54:01 +020093 * and 64K of I/O space when registered.
94 */
95static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
VlaoMao49b23e02017-09-22 18:49:02 +030096#define PCIE_MEM_SIZE (128 << 20)
Phil Sutterba8ae032021-01-03 23:06:46 +010097static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert9c28d612015-08-11 11:54:01 +020098
Anton Schubert9c28d612015-08-11 11:54:01 +020099static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
100{
101 u32 val;
102 val = readl(pcie->base + PCIE_STAT_OFF);
103 return !(val & PCIE_STAT_LINK_DOWN);
104}
105
106static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
107{
108 u32 stat;
109
110 stat = readl(pcie->base + PCIE_STAT_OFF);
111 stat &= ~PCIE_STAT_BUS;
112 stat |= busno << 8;
113 writel(stat, pcie->base + PCIE_STAT_OFF);
114}
115
116static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
117{
118 u32 stat;
119
120 stat = readl(pcie->base + PCIE_STAT_OFF);
121 stat &= ~PCIE_STAT_DEV;
122 stat |= devno << 16;
123 writel(stat, pcie->base + PCIE_STAT_OFF);
124}
125
126static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
127{
128 u32 stat;
129
130 stat = readl(pcie->base + PCIE_STAT_OFF);
131 return (stat & PCIE_STAT_BUS) >> 8;
132}
133
134static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
135{
136 u32 stat;
137
138 stat = readl(pcie->base + PCIE_STAT_OFF);
139 return (stat & PCIE_STAT_DEV) >> 16;
140}
141
142static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
143{
144 return container_of(hose, struct mvebu_pcie, hose);
145}
146
Simon Glassc4e72c42020-01-27 08:49:37 -0700147static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100148 uint offset, ulong *valuep,
149 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200150{
Simon Glassc69cda22020-12-03 16:55:20 -0700151 struct mvebu_pcie *pcie = dev_get_plat(bus);
Anton Schubert9c28d612015-08-11 11:54:01 +0200152 int local_bus = PCI_BUS(pcie->dev);
153 int local_dev = PCI_DEV(pcie->dev);
154 u32 reg;
Stefan Roese94f453e2019-01-25 11:52:43 +0100155 u32 data;
156
157 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
158 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200159
160 /* Only allow one other device besides the local one on the local bus */
Stefan Roese94f453e2019-01-25 11:52:43 +0100161 if (PCI_BUS(bdf) == local_bus && PCI_DEV(bdf) != local_dev) {
162 if (local_dev == 0 && PCI_DEV(bdf) != 1) {
163 debug("- out of range\n");
Anton Schubert9c28d612015-08-11 11:54:01 +0200164 /*
165 * If local dev is 0, the first other dev can
166 * only be 1
167 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100168 *valuep = pci_get_ff(size);
169 return 0;
170 } else if (local_dev != 0 && PCI_DEV(bdf) != 0) {
171 debug("- out of range\n");
Anton Schubert9c28d612015-08-11 11:54:01 +0200172 /*
173 * If local dev is not 0, the first other dev can
174 * only be 0
175 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100176 *valuep = pci_get_ff(size);
177 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200178 }
179 }
180
181 /* write address */
Stefan Roese94f453e2019-01-25 11:52:43 +0100182 reg = PCIE_CONF_ADDR(bdf, offset);
Anton Schubert9c28d612015-08-11 11:54:01 +0200183 writel(reg, pcie->base + PCIE_CONF_ADDR_OFF);
Stefan Roese94f453e2019-01-25 11:52:43 +0100184 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
185 debug("(addr,val)=(0x%04x, 0x%08x)\n", offset, data);
186 *valuep = pci_conv_32_to_size(data, offset, size);
Anton Schubert9c28d612015-08-11 11:54:01 +0200187
188 return 0;
189}
190
Stefan Roese94f453e2019-01-25 11:52:43 +0100191static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
192 uint offset, ulong value,
193 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200194{
Simon Glassc69cda22020-12-03 16:55:20 -0700195 struct mvebu_pcie *pcie = dev_get_plat(bus);
Anton Schubert9c28d612015-08-11 11:54:01 +0200196 int local_bus = PCI_BUS(pcie->dev);
197 int local_dev = PCI_DEV(pcie->dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100198 u32 data;
199
200 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
201 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
202 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200203
204 /* Only allow one other device besides the local one on the local bus */
Stefan Roese94f453e2019-01-25 11:52:43 +0100205 if (PCI_BUS(bdf) == local_bus && PCI_DEV(bdf) != local_dev) {
206 if (local_dev == 0 && PCI_DEV(bdf) != 1) {
Anton Schubert9c28d612015-08-11 11:54:01 +0200207 /*
208 * If local dev is 0, the first other dev can
209 * only be 1
210 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100211 return 0;
212 } else if (local_dev != 0 && PCI_DEV(bdf) != 0) {
Anton Schubert9c28d612015-08-11 11:54:01 +0200213 /*
214 * If local dev is not 0, the first other dev can
215 * only be 0
216 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100217 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200218 }
219 }
220
Stefan Roese94f453e2019-01-25 11:52:43 +0100221 writel(PCIE_CONF_ADDR(bdf, offset), pcie->base + PCIE_CONF_ADDR_OFF);
222 data = pci_conv_size_to_32(0, value, offset, size);
223 writel(data, pcie->base + PCIE_CONF_DATA_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200224
225 return 0;
226}
227
228/*
229 * Setup PCIE BARs and Address Decode Wins:
230 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
231 * WIN[0-3] -> DRAM bank[0-3]
232 */
233static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
234{
235 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
236 u32 size;
237 int i;
238
239 /* First, disable and clear BARs and windows. */
240 for (i = 1; i < 3; i++) {
241 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
242 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
243 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
244 }
245
246 for (i = 0; i < 5; i++) {
247 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
248 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
249 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
250 }
251
252 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
253 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
254 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
255
256 /* Setup windows for DDR banks. Count total DDR size on the fly. */
257 size = 0;
258 for (i = 0; i < dram->num_cs; i++) {
259 const struct mbus_dram_window *cs = dram->cs + i;
260
261 writel(cs->base & 0xffff0000,
262 pcie->base + PCIE_WIN04_BASE_OFF(i));
263 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
264 writel(((cs->size - 1) & 0xffff0000) |
265 (cs->mbus_attr << 8) |
266 (dram->mbus_dram_target_id << 4) | 1,
267 pcie->base + PCIE_WIN04_CTRL_OFF(i));
268
269 size += cs->size;
270 }
271
272 /* Round up 'size' to the nearest power of two. */
273 if ((size & (size - 1)) != 0)
274 size = 1 << fls(size);
275
276 /* Setup BAR[1] to all DRAM banks. */
277 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
278 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
279 writel(((size - 1) & 0xffff0000) | 0x1,
280 pcie->base + PCIE_BAR_CTRL_OFF(1));
281}
282
Stefan Roese94f453e2019-01-25 11:52:43 +0100283static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200284{
Simon Glassc69cda22020-12-03 16:55:20 -0700285 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100286 struct udevice *ctlr = pci_get_controller(dev);
287 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
288 static int bus;
Anton Schubert9c28d612015-08-11 11:54:01 +0200289 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200290
Stefan Roese94f453e2019-01-25 11:52:43 +0100291 debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
292 pcie->port, pcie->lane, (u32)pcie->base);
Anton Schubert9c28d612015-08-11 11:54:01 +0200293
Stefan Roese94f453e2019-01-25 11:52:43 +0100294 /* Read Id info and local bus/dev */
295 debug("direct conf read %08x, local bus %d, local dev %d\n",
296 readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
297 mvebu_pcie_get_local_dev_nr(pcie));
Anton Schubert9c28d612015-08-11 11:54:01 +0200298
Stefan Roese94f453e2019-01-25 11:52:43 +0100299 mvebu_pcie_set_local_bus_nr(pcie, bus);
300 mvebu_pcie_set_local_dev_nr(pcie, 0);
301 pcie->dev = PCI_BDF(bus, 0, 0);
Anton Schubert9c28d612015-08-11 11:54:01 +0200302
Stefan Roese94f453e2019-01-25 11:52:43 +0100303 pcie->mem.start = (u32)mvebu_pcie_membase;
304 pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
305 mvebu_pcie_membase += PCIE_MEM_SIZE;
306
307 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
308 (phys_addr_t)pcie->mem.start,
309 PCIE_MEM_SIZE)) {
310 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
311 (u32)pcie->mem.start, PCIE_MEM_SIZE);
312 }
313
Phil Sutterba8ae032021-01-03 23:06:46 +0100314 pcie->io.start = (u32)mvebu_pcie_iobase;
315 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
316 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
317
318 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
319 (phys_addr_t)pcie->io.start,
320 MBUS_PCI_IO_SIZE)) {
321 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
322 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
323 }
324
Stefan Roese94f453e2019-01-25 11:52:43 +0100325 /* Setup windows and configure host bridge */
326 mvebu_pcie_setup_wins(pcie);
327
328 /* Master + slave enable. */
329 reg = readl(pcie->base + PCIE_CMD_OFF);
330 reg |= PCI_COMMAND_MEMORY;
Phil Sutterba8ae032021-01-03 23:06:46 +0100331 reg |= PCI_COMMAND_IO;
Stefan Roese94f453e2019-01-25 11:52:43 +0100332 reg |= PCI_COMMAND_MASTER;
333 reg |= BIT(10); /* disable interrupts */
334 writel(reg, pcie->base + PCIE_CMD_OFF);
335
Stefan Roese94f453e2019-01-25 11:52:43 +0100336 /* PCI memory space */
337 pci_set_region(hose->regions + 0, pcie->mem.start,
338 pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
339 pci_set_region(hose->regions + 1,
340 0, 0,
341 gd->ram_size,
342 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100343 pci_set_region(hose->regions + 2, pcie->io.start,
344 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
345 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100346
Marek Behún193a1e92019-08-07 15:01:56 +0200347 /* Set BAR0 to internal registers */
348 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
349 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
350
Stefan Roese94f453e2019-01-25 11:52:43 +0100351 bus++;
352
353 return 0;
354}
355
356static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
357{
358 const u32 *addr;
359 int len;
360
361 addr = ofnode_get_property(node, "assigned-addresses", &len);
362 if (!addr) {
363 pr_err("property \"assigned-addresses\" not found");
364 return -FDT_ERR_NOTFOUND;
365 }
366
367 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
368
369 return 0;
370}
371
372#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
373#define DT_TYPE_IO 0x1
374#define DT_TYPE_MEM32 0x2
375#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
376#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
377
378static int mvebu_get_tgt_attr(ofnode node, int devfn,
379 unsigned long type,
380 unsigned int *tgt,
381 unsigned int *attr)
382{
383 const int na = 3, ns = 2;
384 const __be32 *range;
385 int rlen, nranges, rangesz, pna, i;
386
387 *tgt = -1;
388 *attr = -1;
389
390 range = ofnode_get_property(node, "ranges", &rlen);
391 if (!range)
392 return -EINVAL;
393
Stefan Roese0df62e82019-02-11 07:53:34 +0100394 /*
395 * Linux uses of_n_addr_cells() to get the number of address cells
396 * here. Currently this function is only available in U-Boot when
397 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
398 * general, lets't hardcode the "pna" value in the U-Boot code.
399 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100400 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
401 rangesz = pna + na + ns;
402 nranges = rlen / sizeof(__be32) / rangesz;
403
404 for (i = 0; i < nranges; i++, range += rangesz) {
405 u32 flags = of_read_number(range, 1);
406 u32 slot = of_read_number(range + 1, 1);
407 u64 cpuaddr = of_read_number(range + na, pna);
408 unsigned long rtype;
409
410 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
411 rtype = IORESOURCE_IO;
412 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
413 rtype = IORESOURCE_MEM;
414 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200415 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200416
Stefan Roese94f453e2019-01-25 11:52:43 +0100417 /*
418 * The Linux code used PCI_SLOT() here, which expects devfn
419 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
420 * only expects devfn in 15..8, where its saved in this driver.
421 */
422 if (slot == PCI_DEV(devfn) && type == rtype) {
423 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
424 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
425 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100426 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200427 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100428
429 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200430}
Stefan Roese94f453e2019-01-25 11:52:43 +0100431
Simon Glassd1998a92020-12-03 16:55:21 -0700432static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100433{
Simon Glassc69cda22020-12-03 16:55:20 -0700434 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100435 int ret = 0;
436
437 /* Get port number, lane number and memory target / attr */
438 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
439 &pcie->port)) {
440 ret = -ENODEV;
441 goto err;
442 }
443
444 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
445 pcie->lane = 0;
446
447 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
448
449 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
450 pcie->devfn = pci_get_devfn(dev);
451 if (pcie->devfn < 0) {
452 ret = -ENODEV;
453 goto err;
454 }
455
456 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
457 IORESOURCE_MEM,
458 &pcie->mem_target, &pcie->mem_attr);
459 if (ret < 0) {
460 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
461 goto err;
462 }
463
Phil Sutterba8ae032021-01-03 23:06:46 +0100464 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
465 IORESOURCE_IO,
466 &pcie->io_target, &pcie->io_attr);
467 if (ret < 0) {
468 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
469 goto err;
470 }
471
Stefan Roese94f453e2019-01-25 11:52:43 +0100472 /* Parse PCIe controller register base from DT */
473 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
474 if (ret < 0)
475 goto err;
476
477 /* Check link and skip ports that have no link */
478 if (!mvebu_pcie_link_up(pcie)) {
479 debug("%s: %s - down\n", __func__, pcie->name);
480 ret = -ENODEV;
481 goto err;
482 }
483
484 return 0;
485
486err:
487 return ret;
488}
489
490static const struct dm_pci_ops mvebu_pcie_ops = {
491 .read_config = mvebu_pcie_read_config,
492 .write_config = mvebu_pcie_write_config,
493};
494
495static struct driver pcie_mvebu_drv = {
496 .name = "pcie_mvebu",
497 .id = UCLASS_PCI,
498 .ops = &mvebu_pcie_ops,
499 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700500 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700501 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100502};
503
504/*
505 * Use a MISC device to bind the n instances (child nodes) of the
506 * PCIe base controller in UCLASS_PCI.
507 */
508static int mvebu_pcie_bind(struct udevice *parent)
509{
510 struct mvebu_pcie *pcie;
511 struct uclass_driver *drv;
512 struct udevice *dev;
513 ofnode subnode;
514
515 /* Lookup eth driver */
516 drv = lists_uclass_lookup(UCLASS_PCI);
517 if (!drv) {
518 puts("Cannot find PCI driver\n");
519 return -ENOENT;
520 }
521
522 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
523 if (!ofnode_is_available(subnode))
524 continue;
525
526 pcie = calloc(1, sizeof(*pcie));
527 if (!pcie)
528 return -ENOMEM;
529
530 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700531 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
532 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100533 }
534
535 return 0;
536}
537
538static const struct udevice_id mvebu_pcie_ids[] = {
539 { .compatible = "marvell,armada-xp-pcie" },
540 { .compatible = "marvell,armada-370-pcie" },
541 { }
542};
543
544U_BOOT_DRIVER(pcie_mvebu_base) = {
545 .name = "pcie_mvebu_base",
546 .id = UCLASS_MISC,
547 .of_match = mvebu_pcie_ids,
548 .bind = mvebu_pcie_bind,
549};