blob: 8e674bbef22d3082b4327831b15e46704ad6f9e3 [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);
Stefan Roese94f453e2019-01-25 11:52:43 +0100154 u32 data;
155
Stefan Roese6a2fa282021-01-25 15:25:31 +0100156 debug("PCIE CFG read: loc_bus=%d loc_dev=%d (b,d,f)=(%2d,%2d,%2d) ",
157 local_bus, local_dev, PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200158
Stefan Roese6a2fa282021-01-25 15:25:31 +0100159 /* Don't access the local host controller via this API */
160 if (PCI_BUS(bdf) == local_bus && PCI_DEV(bdf) == local_dev) {
161 debug("- skipping host controller\n");
162 *valuep = pci_get_ff(size);
163 return 0;
164 }
165
166 /* If local dev is 0, the first other dev can only be 1 */
167 if (PCI_BUS(bdf) == local_bus && local_dev == 0 && PCI_DEV(bdf) != 1) {
168 debug("- out of range\n");
169 *valuep = pci_get_ff(size);
170 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200171 }
172
173 /* write address */
Marek Behún241d7632021-02-08 23:01:38 +0100174 writel(PCIE_CONF_ADDR(bdf, offset), pcie->base + PCIE_CONF_ADDR_OFF);
175
176 /* read data */
Stefan Roese94f453e2019-01-25 11:52:43 +0100177 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
Marek Behún26f7a762021-02-08 23:01:39 +0100178 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Stefan Roese94f453e2019-01-25 11:52:43 +0100179 *valuep = pci_conv_32_to_size(data, offset, size);
Anton Schubert9c28d612015-08-11 11:54:01 +0200180
181 return 0;
182}
183
Stefan Roese94f453e2019-01-25 11:52:43 +0100184static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
185 uint offset, ulong value,
186 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200187{
Simon Glassc69cda22020-12-03 16:55:20 -0700188 struct mvebu_pcie *pcie = dev_get_plat(bus);
Anton Schubert9c28d612015-08-11 11:54:01 +0200189 int local_bus = PCI_BUS(pcie->dev);
190 int local_dev = PCI_DEV(pcie->dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100191 u32 data;
192
Stefan Roese6a2fa282021-01-25 15:25:31 +0100193 debug("PCIE CFG write: loc_bus=%d loc_dev=%d (b,d,f)=(%2d,%2d,%2d) ",
194 local_bus, local_dev, PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behún26f7a762021-02-08 23:01:39 +0100195 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200196
Stefan Roese6a2fa282021-01-25 15:25:31 +0100197 /* Don't access the local host controller via this API */
198 if (PCI_BUS(bdf) == local_bus && PCI_DEV(bdf) == local_dev) {
199 debug("- skipping host controller\n");
200 return 0;
201 }
202
203 /* If local dev is 0, the first other dev can only be 1 */
204 if (PCI_BUS(bdf) == local_bus && local_dev == 0 && PCI_DEV(bdf) != 1) {
205 debug("- out of range\n");
206 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200207 }
208
Marek Behún241d7632021-02-08 23:01:38 +0100209 /* write address */
Stefan Roese94f453e2019-01-25 11:52:43 +0100210 writel(PCIE_CONF_ADDR(bdf, offset), pcie->base + PCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100211
212 /* write data */
Stefan Roese94f453e2019-01-25 11:52:43 +0100213 data = pci_conv_size_to_32(0, value, offset, size);
214 writel(data, pcie->base + PCIE_CONF_DATA_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200215
216 return 0;
217}
218
219/*
220 * Setup PCIE BARs and Address Decode Wins:
221 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
222 * WIN[0-3] -> DRAM bank[0-3]
223 */
224static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
225{
226 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
227 u32 size;
228 int i;
229
230 /* First, disable and clear BARs and windows. */
231 for (i = 1; i < 3; i++) {
232 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
233 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
234 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
235 }
236
237 for (i = 0; i < 5; i++) {
238 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
239 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
240 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
241 }
242
243 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
244 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
245 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
246
247 /* Setup windows for DDR banks. Count total DDR size on the fly. */
248 size = 0;
249 for (i = 0; i < dram->num_cs; i++) {
250 const struct mbus_dram_window *cs = dram->cs + i;
251
252 writel(cs->base & 0xffff0000,
253 pcie->base + PCIE_WIN04_BASE_OFF(i));
254 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
255 writel(((cs->size - 1) & 0xffff0000) |
256 (cs->mbus_attr << 8) |
257 (dram->mbus_dram_target_id << 4) | 1,
258 pcie->base + PCIE_WIN04_CTRL_OFF(i));
259
260 size += cs->size;
261 }
262
263 /* Round up 'size' to the nearest power of two. */
264 if ((size & (size - 1)) != 0)
265 size = 1 << fls(size);
266
267 /* Setup BAR[1] to all DRAM banks. */
268 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
269 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
270 writel(((size - 1) & 0xffff0000) | 0x1,
271 pcie->base + PCIE_BAR_CTRL_OFF(1));
272}
273
Stefan Roese94f453e2019-01-25 11:52:43 +0100274static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200275{
Simon Glassc69cda22020-12-03 16:55:20 -0700276 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100277 struct udevice *ctlr = pci_get_controller(dev);
278 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Marek Behún201958a2021-02-08 23:01:37 +0100279 int bus = dev_seq(dev);
Anton Schubert9c28d612015-08-11 11:54:01 +0200280 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200281
Stefan Roese94f453e2019-01-25 11:52:43 +0100282 debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
283 pcie->port, pcie->lane, (u32)pcie->base);
Anton Schubert9c28d612015-08-11 11:54:01 +0200284
Stefan Roese94f453e2019-01-25 11:52:43 +0100285 /* Read Id info and local bus/dev */
286 debug("direct conf read %08x, local bus %d, local dev %d\n",
287 readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
288 mvebu_pcie_get_local_dev_nr(pcie));
Anton Schubert9c28d612015-08-11 11:54:01 +0200289
Stefan Roese94f453e2019-01-25 11:52:43 +0100290 mvebu_pcie_set_local_bus_nr(pcie, bus);
291 mvebu_pcie_set_local_dev_nr(pcie, 0);
292 pcie->dev = PCI_BDF(bus, 0, 0);
Anton Schubert9c28d612015-08-11 11:54:01 +0200293
Stefan Roese94f453e2019-01-25 11:52:43 +0100294 pcie->mem.start = (u32)mvebu_pcie_membase;
295 pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
296 mvebu_pcie_membase += PCIE_MEM_SIZE;
297
298 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
299 (phys_addr_t)pcie->mem.start,
300 PCIE_MEM_SIZE)) {
301 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
302 (u32)pcie->mem.start, PCIE_MEM_SIZE);
303 }
304
Phil Sutterba8ae032021-01-03 23:06:46 +0100305 pcie->io.start = (u32)mvebu_pcie_iobase;
306 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
307 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
308
309 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
310 (phys_addr_t)pcie->io.start,
311 MBUS_PCI_IO_SIZE)) {
312 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
313 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
314 }
315
Stefan Roese94f453e2019-01-25 11:52:43 +0100316 /* Setup windows and configure host bridge */
317 mvebu_pcie_setup_wins(pcie);
318
319 /* Master + slave enable. */
320 reg = readl(pcie->base + PCIE_CMD_OFF);
321 reg |= PCI_COMMAND_MEMORY;
Phil Sutterba8ae032021-01-03 23:06:46 +0100322 reg |= PCI_COMMAND_IO;
Stefan Roese94f453e2019-01-25 11:52:43 +0100323 reg |= PCI_COMMAND_MASTER;
324 reg |= BIT(10); /* disable interrupts */
325 writel(reg, pcie->base + PCIE_CMD_OFF);
326
Stefan Roese94f453e2019-01-25 11:52:43 +0100327 /* PCI memory space */
328 pci_set_region(hose->regions + 0, pcie->mem.start,
329 pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
330 pci_set_region(hose->regions + 1,
331 0, 0,
332 gd->ram_size,
333 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100334 pci_set_region(hose->regions + 2, pcie->io.start,
335 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
336 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100337
Marek Behún193a1e92019-08-07 15:01:56 +0200338 /* Set BAR0 to internal registers */
339 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
340 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
341
Stefan Roese94f453e2019-01-25 11:52:43 +0100342 return 0;
343}
344
345static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
346{
347 const u32 *addr;
348 int len;
349
350 addr = ofnode_get_property(node, "assigned-addresses", &len);
351 if (!addr) {
352 pr_err("property \"assigned-addresses\" not found");
353 return -FDT_ERR_NOTFOUND;
354 }
355
356 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
357
358 return 0;
359}
360
361#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
362#define DT_TYPE_IO 0x1
363#define DT_TYPE_MEM32 0x2
364#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
365#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
366
367static int mvebu_get_tgt_attr(ofnode node, int devfn,
368 unsigned long type,
369 unsigned int *tgt,
370 unsigned int *attr)
371{
372 const int na = 3, ns = 2;
373 const __be32 *range;
374 int rlen, nranges, rangesz, pna, i;
375
376 *tgt = -1;
377 *attr = -1;
378
379 range = ofnode_get_property(node, "ranges", &rlen);
380 if (!range)
381 return -EINVAL;
382
Stefan Roese0df62e82019-02-11 07:53:34 +0100383 /*
384 * Linux uses of_n_addr_cells() to get the number of address cells
385 * here. Currently this function is only available in U-Boot when
386 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
387 * general, lets't hardcode the "pna" value in the U-Boot code.
388 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100389 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
390 rangesz = pna + na + ns;
391 nranges = rlen / sizeof(__be32) / rangesz;
392
393 for (i = 0; i < nranges; i++, range += rangesz) {
394 u32 flags = of_read_number(range, 1);
395 u32 slot = of_read_number(range + 1, 1);
396 u64 cpuaddr = of_read_number(range + na, pna);
397 unsigned long rtype;
398
399 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
400 rtype = IORESOURCE_IO;
401 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
402 rtype = IORESOURCE_MEM;
403 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200404 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200405
Stefan Roese94f453e2019-01-25 11:52:43 +0100406 /*
407 * The Linux code used PCI_SLOT() here, which expects devfn
408 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
409 * only expects devfn in 15..8, where its saved in this driver.
410 */
411 if (slot == PCI_DEV(devfn) && type == rtype) {
412 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
413 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
414 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100415 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200416 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100417
418 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200419}
Stefan Roese94f453e2019-01-25 11:52:43 +0100420
Simon Glassd1998a92020-12-03 16:55:21 -0700421static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100422{
Simon Glassc69cda22020-12-03 16:55:20 -0700423 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100424 int ret = 0;
425
426 /* Get port number, lane number and memory target / attr */
427 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
428 &pcie->port)) {
429 ret = -ENODEV;
430 goto err;
431 }
432
433 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
434 pcie->lane = 0;
435
436 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
437
438 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
439 pcie->devfn = pci_get_devfn(dev);
440 if (pcie->devfn < 0) {
441 ret = -ENODEV;
442 goto err;
443 }
444
445 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
446 IORESOURCE_MEM,
447 &pcie->mem_target, &pcie->mem_attr);
448 if (ret < 0) {
449 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
450 goto err;
451 }
452
Phil Sutterba8ae032021-01-03 23:06:46 +0100453 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
454 IORESOURCE_IO,
455 &pcie->io_target, &pcie->io_attr);
456 if (ret < 0) {
457 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
458 goto err;
459 }
460
Stefan Roese94f453e2019-01-25 11:52:43 +0100461 /* Parse PCIe controller register base from DT */
462 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
463 if (ret < 0)
464 goto err;
465
466 /* Check link and skip ports that have no link */
467 if (!mvebu_pcie_link_up(pcie)) {
468 debug("%s: %s - down\n", __func__, pcie->name);
469 ret = -ENODEV;
470 goto err;
471 }
472
473 return 0;
474
475err:
476 return ret;
477}
478
479static const struct dm_pci_ops mvebu_pcie_ops = {
480 .read_config = mvebu_pcie_read_config,
481 .write_config = mvebu_pcie_write_config,
482};
483
484static struct driver pcie_mvebu_drv = {
485 .name = "pcie_mvebu",
486 .id = UCLASS_PCI,
487 .ops = &mvebu_pcie_ops,
488 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700489 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700490 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100491};
492
493/*
494 * Use a MISC device to bind the n instances (child nodes) of the
495 * PCIe base controller in UCLASS_PCI.
496 */
497static int mvebu_pcie_bind(struct udevice *parent)
498{
499 struct mvebu_pcie *pcie;
500 struct uclass_driver *drv;
501 struct udevice *dev;
502 ofnode subnode;
503
504 /* Lookup eth driver */
505 drv = lists_uclass_lookup(UCLASS_PCI);
506 if (!drv) {
507 puts("Cannot find PCI driver\n");
508 return -ENOENT;
509 }
510
511 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
512 if (!ofnode_is_available(subnode))
513 continue;
514
515 pcie = calloc(1, sizeof(*pcie));
516 if (!pcie)
517 return -ENOMEM;
518
519 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700520 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
521 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100522 }
523
524 return 0;
525}
526
527static const struct udevice_id mvebu_pcie_ids[] = {
528 { .compatible = "marvell,armada-xp-pcie" },
529 { .compatible = "marvell,armada-370-pcie" },
530 { }
531};
532
533U_BOOT_DRIVER(pcie_mvebu_base) = {
534 .name = "pcie_mvebu_base",
535 .id = UCLASS_MISC,
536 .of_match = mvebu_pcie_ids,
537 .bind = mvebu_pcie_bind,
538};