blob: 3991086e0d29fb4c756e924e5d0a15d5fb0c81e2 [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;
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +010082 int first_busno;
83 int local_dev;
Stefan Roese94f453e2019-01-25 11:52:43 +010084 char name[16];
85 unsigned int mem_target;
86 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010087 unsigned int io_target;
88 unsigned int io_attr;
Anton Schubert9c28d612015-08-11 11:54:01 +020089};
90
Anton Schubert9c28d612015-08-11 11:54:01 +020091/*
92 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
VlaoMao49b23e02017-09-22 18:49:02 +030093 * into SoCs address space. Each controller will map 128M of MEM
Anton Schubert9c28d612015-08-11 11:54:01 +020094 * and 64K of I/O space when registered.
95 */
96static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE;
VlaoMao49b23e02017-09-22 18:49:02 +030097#define PCIE_MEM_SIZE (128 << 20)
Phil Sutterba8ae032021-01-03 23:06:46 +010098static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE;
Anton Schubert9c28d612015-08-11 11:54:01 +020099
Anton Schubert9c28d612015-08-11 11:54:01 +0200100static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
101{
102 u32 val;
103 val = readl(pcie->base + PCIE_STAT_OFF);
104 return !(val & PCIE_STAT_LINK_DOWN);
105}
106
107static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
108{
109 u32 stat;
110
111 stat = readl(pcie->base + PCIE_STAT_OFF);
112 stat &= ~PCIE_STAT_BUS;
113 stat |= busno << 8;
114 writel(stat, pcie->base + PCIE_STAT_OFF);
115}
116
117static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
118{
119 u32 stat;
120
121 stat = readl(pcie->base + PCIE_STAT_OFF);
122 stat &= ~PCIE_STAT_DEV;
123 stat |= devno << 16;
124 writel(stat, pcie->base + PCIE_STAT_OFF);
125}
126
127static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie)
128{
129 u32 stat;
130
131 stat = readl(pcie->base + PCIE_STAT_OFF);
132 return (stat & PCIE_STAT_BUS) >> 8;
133}
134
135static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie)
136{
137 u32 stat;
138
139 stat = readl(pcie->base + PCIE_STAT_OFF);
140 return (stat & PCIE_STAT_DEV) >> 16;
141}
142
143static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
144{
145 return container_of(hose, struct mvebu_pcie, hose);
146}
147
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100148static int mvebu_pcie_valid_addr(struct mvebu_pcie *pcie, pci_dev_t bdf)
149{
150 /*
151 * There are two devices visible on local bus:
152 * * on slot configured by function mvebu_pcie_set_local_dev_nr()
153 * (by default this register is set to 0) there is a
154 * "Marvell Memory controller", which isn't useful in root complex
155 * mode,
156 * * on all other slots the real PCIe card connected to the PCIe slot.
157 *
158 * We therefore allow access only to the real PCIe card.
159 */
160 if (PCI_BUS(bdf) == pcie->first_busno &&
161 PCI_DEV(bdf) != !pcie->local_dev)
162 return 0;
163
164 return 1;
165}
166
Simon Glassc4e72c42020-01-27 08:49:37 -0700167static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100168 uint offset, ulong *valuep,
169 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200170{
Simon Glassc69cda22020-12-03 16:55:20 -0700171 struct mvebu_pcie *pcie = dev_get_plat(bus);
Stefan Roese94f453e2019-01-25 11:52:43 +0100172 u32 data;
173
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100174 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
175 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200176
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100177 if (!mvebu_pcie_valid_addr(pcie, bdf)) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100178 debug("- out of range\n");
179 *valuep = pci_get_ff(size);
180 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200181 }
182
183 /* write address */
Marek BehĂșn241d7632021-02-08 23:01:38 +0100184 writel(PCIE_CONF_ADDR(bdf, offset), pcie->base + PCIE_CONF_ADDR_OFF);
185
186 /* read data */
Pali RohĂĄr657177a2021-10-22 16:22:09 +0200187 switch (size) {
188 case PCI_SIZE_8:
189 data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
190 break;
191 case PCI_SIZE_16:
192 data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
193 break;
194 case PCI_SIZE_32:
195 data = readl(pcie->base + PCIE_CONF_DATA_OFF);
196 break;
197 default:
198 return -EINVAL;
199 }
200
Marek BehĂșn26f7a762021-02-08 23:01:39 +0100201 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali RohĂĄr657177a2021-10-22 16:22:09 +0200202 *valuep = data;
Anton Schubert9c28d612015-08-11 11:54:01 +0200203
204 return 0;
205}
206
Stefan Roese94f453e2019-01-25 11:52:43 +0100207static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
208 uint offset, ulong value,
209 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200210{
Simon Glassc69cda22020-12-03 16:55:20 -0700211 struct mvebu_pcie *pcie = dev_get_plat(bus);
Stefan Roese94f453e2019-01-25 11:52:43 +0100212 u32 data;
213
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100214 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
215 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek BehĂșn26f7a762021-02-08 23:01:39 +0100216 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200217
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100218 if (!mvebu_pcie_valid_addr(pcie, bdf)) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100219 debug("- out of range\n");
220 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200221 }
222
Marek BehĂșn241d7632021-02-08 23:01:38 +0100223 /* write address */
Stefan Roese94f453e2019-01-25 11:52:43 +0100224 writel(PCIE_CONF_ADDR(bdf, offset), pcie->base + PCIE_CONF_ADDR_OFF);
Marek BehĂșn241d7632021-02-08 23:01:38 +0100225
226 /* write data */
Pali RohĂĄrdaa9bfd2021-10-22 16:22:08 +0200227 switch (size) {
228 case PCI_SIZE_8:
229 writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3));
230 break;
231 case PCI_SIZE_16:
232 writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2));
233 break;
234 case PCI_SIZE_32:
235 writel(value, pcie->base + PCIE_CONF_DATA_OFF);
236 break;
237 default:
238 return -EINVAL;
239 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200240
241 return 0;
242}
243
244/*
245 * Setup PCIE BARs and Address Decode Wins:
246 * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
247 * WIN[0-3] -> DRAM bank[0-3]
248 */
249static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
250{
251 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
252 u32 size;
253 int i;
254
255 /* First, disable and clear BARs and windows. */
256 for (i = 1; i < 3; i++) {
257 writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i));
258 writel(0, pcie->base + PCIE_BAR_LO_OFF(i));
259 writel(0, pcie->base + PCIE_BAR_HI_OFF(i));
260 }
261
262 for (i = 0; i < 5; i++) {
263 writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i));
264 writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i));
265 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
266 }
267
268 writel(0, pcie->base + PCIE_WIN5_CTRL_OFF);
269 writel(0, pcie->base + PCIE_WIN5_BASE_OFF);
270 writel(0, pcie->base + PCIE_WIN5_REMAP_OFF);
271
272 /* Setup windows for DDR banks. Count total DDR size on the fly. */
273 size = 0;
274 for (i = 0; i < dram->num_cs; i++) {
275 const struct mbus_dram_window *cs = dram->cs + i;
276
277 writel(cs->base & 0xffff0000,
278 pcie->base + PCIE_WIN04_BASE_OFF(i));
279 writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i));
280 writel(((cs->size - 1) & 0xffff0000) |
281 (cs->mbus_attr << 8) |
282 (dram->mbus_dram_target_id << 4) | 1,
283 pcie->base + PCIE_WIN04_CTRL_OFF(i));
284
285 size += cs->size;
286 }
287
288 /* Round up 'size' to the nearest power of two. */
289 if ((size & (size - 1)) != 0)
290 size = 1 << fls(size);
291
292 /* Setup BAR[1] to all DRAM banks. */
293 writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1));
294 writel(0, pcie->base + PCIE_BAR_HI_OFF(1));
295 writel(((size - 1) & 0xffff0000) | 0x1,
296 pcie->base + PCIE_BAR_CTRL_OFF(1));
297}
298
Stefan Roese94f453e2019-01-25 11:52:43 +0100299static int mvebu_pcie_probe(struct udevice *dev)
Anton Schubert9c28d612015-08-11 11:54:01 +0200300{
Simon Glassc69cda22020-12-03 16:55:20 -0700301 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100302 struct udevice *ctlr = pci_get_controller(dev);
303 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Marek BehĂșn201958a2021-02-08 23:01:37 +0100304 int bus = dev_seq(dev);
Anton Schubert9c28d612015-08-11 11:54:01 +0200305 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200306
Stefan Roese94f453e2019-01-25 11:52:43 +0100307 debug("%s: PCIe %d.%d - up, base %08x\n", __func__,
308 pcie->port, pcie->lane, (u32)pcie->base);
Anton Schubert9c28d612015-08-11 11:54:01 +0200309
Stefan Roese94f453e2019-01-25 11:52:43 +0100310 /* Read Id info and local bus/dev */
311 debug("direct conf read %08x, local bus %d, local dev %d\n",
312 readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie),
313 mvebu_pcie_get_local_dev_nr(pcie));
Anton Schubert9c28d612015-08-11 11:54:01 +0200314
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100315 pcie->first_busno = bus;
Marek BehĂșn5ddfd3f2021-02-08 23:01:41 +0100316 pcie->local_dev = 1;
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100317
Stefan Roese94f453e2019-01-25 11:52:43 +0100318 mvebu_pcie_set_local_bus_nr(pcie, bus);
Marek BehĂșn10eb2cc2021-02-08 23:01:40 +0100319 mvebu_pcie_set_local_dev_nr(pcie, pcie->local_dev);
Anton Schubert9c28d612015-08-11 11:54:01 +0200320
Stefan Roese94f453e2019-01-25 11:52:43 +0100321 pcie->mem.start = (u32)mvebu_pcie_membase;
322 pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1;
323 mvebu_pcie_membase += PCIE_MEM_SIZE;
324
325 if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
326 (phys_addr_t)pcie->mem.start,
327 PCIE_MEM_SIZE)) {
328 printf("PCIe unable to add mbus window for mem at %08x+%08x\n",
329 (u32)pcie->mem.start, PCIE_MEM_SIZE);
330 }
331
Phil Sutterba8ae032021-01-03 23:06:46 +0100332 pcie->io.start = (u32)mvebu_pcie_iobase;
333 pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1;
334 mvebu_pcie_iobase += MBUS_PCI_IO_SIZE;
335
336 if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
337 (phys_addr_t)pcie->io.start,
338 MBUS_PCI_IO_SIZE)) {
339 printf("PCIe unable to add mbus window for IO at %08x+%08x\n",
340 (u32)pcie->io.start, MBUS_PCI_IO_SIZE);
341 }
342
Stefan Roese94f453e2019-01-25 11:52:43 +0100343 /* Setup windows and configure host bridge */
344 mvebu_pcie_setup_wins(pcie);
345
346 /* Master + slave enable. */
347 reg = readl(pcie->base + PCIE_CMD_OFF);
348 reg |= PCI_COMMAND_MEMORY;
Phil Sutterba8ae032021-01-03 23:06:46 +0100349 reg |= PCI_COMMAND_IO;
Stefan Roese94f453e2019-01-25 11:52:43 +0100350 reg |= PCI_COMMAND_MASTER;
351 reg |= BIT(10); /* disable interrupts */
352 writel(reg, pcie->base + PCIE_CMD_OFF);
353
Stefan Roese94f453e2019-01-25 11:52:43 +0100354 /* PCI memory space */
355 pci_set_region(hose->regions + 0, pcie->mem.start,
356 pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM);
357 pci_set_region(hose->regions + 1,
358 0, 0,
359 gd->ram_size,
360 PCI_REGION_MEM | PCI_REGION_SYS_MEMORY);
Phil Sutterba8ae032021-01-03 23:06:46 +0100361 pci_set_region(hose->regions + 2, pcie->io.start,
362 pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO);
363 hose->region_count = 3;
Stefan Roese94f453e2019-01-25 11:52:43 +0100364
Marek BehĂșn193a1e92019-08-07 15:01:56 +0200365 /* Set BAR0 to internal registers */
366 writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0));
367 writel(0, pcie->base + PCIE_BAR_HI_OFF(0));
368
Stefan Roese94f453e2019-01-25 11:52:43 +0100369 return 0;
370}
371
372static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie)
373{
374 const u32 *addr;
375 int len;
376
377 addr = ofnode_get_property(node, "assigned-addresses", &len);
378 if (!addr) {
379 pr_err("property \"assigned-addresses\" not found");
380 return -FDT_ERR_NOTFOUND;
381 }
382
383 pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE);
384
385 return 0;
386}
387
388#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
389#define DT_TYPE_IO 0x1
390#define DT_TYPE_MEM32 0x2
391#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
392#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
393
394static int mvebu_get_tgt_attr(ofnode node, int devfn,
395 unsigned long type,
396 unsigned int *tgt,
397 unsigned int *attr)
398{
399 const int na = 3, ns = 2;
400 const __be32 *range;
401 int rlen, nranges, rangesz, pna, i;
402
403 *tgt = -1;
404 *attr = -1;
405
406 range = ofnode_get_property(node, "ranges", &rlen);
407 if (!range)
408 return -EINVAL;
409
Stefan Roese0df62e82019-02-11 07:53:34 +0100410 /*
411 * Linux uses of_n_addr_cells() to get the number of address cells
412 * here. Currently this function is only available in U-Boot when
413 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
414 * general, lets't hardcode the "pna" value in the U-Boot code.
415 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100416 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
417 rangesz = pna + na + ns;
418 nranges = rlen / sizeof(__be32) / rangesz;
419
420 for (i = 0; i < nranges; i++, range += rangesz) {
421 u32 flags = of_read_number(range, 1);
422 u32 slot = of_read_number(range + 1, 1);
423 u64 cpuaddr = of_read_number(range + na, pna);
424 unsigned long rtype;
425
426 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
427 rtype = IORESOURCE_IO;
428 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
429 rtype = IORESOURCE_MEM;
430 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200431 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200432
Stefan Roese94f453e2019-01-25 11:52:43 +0100433 /*
434 * The Linux code used PCI_SLOT() here, which expects devfn
435 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
436 * only expects devfn in 15..8, where its saved in this driver.
437 */
438 if (slot == PCI_DEV(devfn) && type == rtype) {
439 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
440 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
441 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100442 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200443 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100444
445 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200446}
Stefan Roese94f453e2019-01-25 11:52:43 +0100447
Simon Glassd1998a92020-12-03 16:55:21 -0700448static int mvebu_pcie_of_to_plat(struct udevice *dev)
Stefan Roese94f453e2019-01-25 11:52:43 +0100449{
Simon Glassc69cda22020-12-03 16:55:20 -0700450 struct mvebu_pcie *pcie = dev_get_plat(dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100451 int ret = 0;
452
453 /* Get port number, lane number and memory target / attr */
454 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port",
455 &pcie->port)) {
456 ret = -ENODEV;
457 goto err;
458 }
459
460 if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane))
461 pcie->lane = 0;
462
463 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
464
465 /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */
466 pcie->devfn = pci_get_devfn(dev);
467 if (pcie->devfn < 0) {
468 ret = -ENODEV;
469 goto err;
470 }
471
472 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
473 IORESOURCE_MEM,
474 &pcie->mem_target, &pcie->mem_attr);
475 if (ret < 0) {
476 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
477 goto err;
478 }
479
Phil Sutterba8ae032021-01-03 23:06:46 +0100480 ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn,
481 IORESOURCE_IO,
482 &pcie->io_target, &pcie->io_attr);
483 if (ret < 0) {
484 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
485 goto err;
486 }
487
Stefan Roese94f453e2019-01-25 11:52:43 +0100488 /* Parse PCIe controller register base from DT */
489 ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie);
490 if (ret < 0)
491 goto err;
492
493 /* Check link and skip ports that have no link */
494 if (!mvebu_pcie_link_up(pcie)) {
495 debug("%s: %s - down\n", __func__, pcie->name);
496 ret = -ENODEV;
497 goto err;
498 }
499
500 return 0;
501
502err:
503 return ret;
504}
505
506static const struct dm_pci_ops mvebu_pcie_ops = {
507 .read_config = mvebu_pcie_read_config,
508 .write_config = mvebu_pcie_write_config,
509};
510
511static struct driver pcie_mvebu_drv = {
512 .name = "pcie_mvebu",
513 .id = UCLASS_PCI,
514 .ops = &mvebu_pcie_ops,
515 .probe = mvebu_pcie_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700516 .of_to_plat = mvebu_pcie_of_to_plat,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700517 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100518};
519
520/*
521 * Use a MISC device to bind the n instances (child nodes) of the
522 * PCIe base controller in UCLASS_PCI.
523 */
524static int mvebu_pcie_bind(struct udevice *parent)
525{
526 struct mvebu_pcie *pcie;
527 struct uclass_driver *drv;
528 struct udevice *dev;
529 ofnode subnode;
530
531 /* Lookup eth driver */
532 drv = lists_uclass_lookup(UCLASS_PCI);
533 if (!drv) {
534 puts("Cannot find PCI driver\n");
535 return -ENOENT;
536 }
537
538 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
539 if (!ofnode_is_available(subnode))
540 continue;
541
542 pcie = calloc(1, sizeof(*pcie));
543 if (!pcie)
544 return -ENOMEM;
545
546 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700547 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
548 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100549 }
550
551 return 0;
552}
553
554static const struct udevice_id mvebu_pcie_ids[] = {
555 { .compatible = "marvell,armada-xp-pcie" },
556 { .compatible = "marvell,armada-370-pcie" },
557 { }
558};
559
560U_BOOT_DRIVER(pcie_mvebu_base) = {
561 .name = "pcie_mvebu_base",
562 .id = UCLASS_MISC,
563 .of_match = mvebu_pcie_ids,
564 .bind = mvebu_pcie_bind,
565};