blob: 93a7508d8a2a149b4ca967168e773eadc50d0c0b [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>
Pali Rohár22f69fc2021-12-16 12:04:06 +010010 * Pali Rohár <pali@kernel.org>
Anton Schubert9c28d612015-08-11 11:54:01 +020011 */
12
13#include <common.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010014#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070016#include <malloc.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>
Pali Rohár94c30f92021-12-21 12:20:19 +010021#include <reset.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020022#include <asm/io.h>
23#include <asm/arch/cpu.h>
24#include <asm/arch/soc.h>
Pali Rohárca3756c2022-08-05 16:03:41 +020025#include <asm/gpio.h>
Simon Glasscd93d622020-05-10 11:40:13 -060026#include <linux/bitops.h>
Pali Roháre7ff4272021-12-21 12:20:17 +010027#include <linux/delay.h>
Stefan Roese94f453e2019-01-25 11:52:43 +010028#include <linux/errno.h>
29#include <linux/ioport.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020030#include <linux/mbus.h>
Pali Rohár537b0142021-12-21 12:20:13 +010031#include <linux/sizes.h>
Anton Schubert9c28d612015-08-11 11:54:01 +020032
Anton Schubert9c28d612015-08-11 11:54:01 +020033/* PCIe unit register offsets */
Pali Rohárfc27e5d2022-02-18 12:25:23 +010034#define MVPCIE_ROOT_PORT_PCI_CFG_OFF 0x0000
35#define MVPCIE_ROOT_PORT_PCI_EXP_OFF 0x0060
36#define MVPCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
37#define MVPCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
38#define MVPCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
39#define MVPCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
40#define MVPCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
41#define MVPCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
42#define MVPCIE_WIN5_CTRL_OFF 0x1880
43#define MVPCIE_WIN5_BASE_OFF 0x1884
44#define MVPCIE_WIN5_REMAP_OFF 0x188c
45#define MVPCIE_CONF_ADDR_OFF 0x18f8
46#define MVPCIE_CONF_DATA_OFF 0x18fc
47#define MVPCIE_CTRL_OFF 0x1a00
48#define MVPCIE_CTRL_RC_MODE BIT(1)
49#define MVPCIE_STAT_OFF 0x1a04
50#define MVPCIE_STAT_BUS (0xff << 8)
51#define MVPCIE_STAT_DEV (0x1f << 16)
52#define MVPCIE_STAT_LINK_DOWN BIT(0)
Anton Schubert9c28d612015-08-11 11:54:01 +020053
Pali Roháre7ff4272021-12-21 12:20:17 +010054#define LINK_WAIT_RETRIES 100
55#define LINK_WAIT_TIMEOUT 1000
56
Anton Schubert9c28d612015-08-11 11:54:01 +020057struct mvebu_pcie {
58 struct pci_controller hose;
Anton Schubert9c28d612015-08-11 11:54:01 +020059 void __iomem *base;
60 void __iomem *membase;
61 struct resource mem;
62 void __iomem *iobase;
Phil Sutterba8ae032021-01-03 23:06:46 +010063 struct resource io;
Pali Rohárca3756c2022-08-05 16:03:41 +020064 struct gpio_desc reset_gpio;
Pali Rohár137db2a2021-12-21 12:20:15 +010065 u32 intregs;
Anton Schubert9c28d612015-08-11 11:54:01 +020066 u32 port;
67 u32 lane;
Pali Rohár94c30f92021-12-21 12:20:19 +010068 bool is_x4;
Stefan Roese94f453e2019-01-25 11:52:43 +010069 int devfn;
Pali Rohára7b61ab2021-10-22 16:22:10 +020070 int sec_busno;
Stefan Roese94f453e2019-01-25 11:52:43 +010071 char name[16];
72 unsigned int mem_target;
73 unsigned int mem_attr;
Phil Sutterba8ae032021-01-03 23:06:46 +010074 unsigned int io_target;
75 unsigned int io_attr;
Pali Rohára48e4282021-11-11 16:35:45 +010076 u32 cfgcache[(0x3c - 0x10) / 4];
Anton Schubert9c28d612015-08-11 11:54:01 +020077};
78
Anton Schubert9c28d612015-08-11 11:54:01 +020079static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
80{
81 u32 val;
Pali Rohárfc27e5d2022-02-18 12:25:23 +010082 val = readl(pcie->base + MVPCIE_STAT_OFF);
83 return !(val & MVPCIE_STAT_LINK_DOWN);
Anton Schubert9c28d612015-08-11 11:54:01 +020084}
85
Pali Roháre7ff4272021-12-21 12:20:17 +010086static void mvebu_pcie_wait_for_link(struct mvebu_pcie *pcie)
87{
88 int retries;
89
90 /* check if the link is up or not */
91 for (retries = 0; retries < LINK_WAIT_RETRIES; retries++) {
92 if (mvebu_pcie_link_up(pcie)) {
93 printf("%s: Link up\n", pcie->name);
94 return;
95 }
96
97 udelay(LINK_WAIT_TIMEOUT);
98 }
99
100 printf("%s: Link down\n", pcie->name);
101}
102
Anton Schubert9c28d612015-08-11 11:54:01 +0200103static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
104{
105 u32 stat;
106
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100107 stat = readl(pcie->base + MVPCIE_STAT_OFF);
108 stat &= ~MVPCIE_STAT_BUS;
Anton Schubert9c28d612015-08-11 11:54:01 +0200109 stat |= busno << 8;
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100110 writel(stat, pcie->base + MVPCIE_STAT_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200111}
112
113static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
114{
115 u32 stat;
116
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100117 stat = readl(pcie->base + MVPCIE_STAT_OFF);
118 stat &= ~MVPCIE_STAT_DEV;
Anton Schubert9c28d612015-08-11 11:54:01 +0200119 stat |= devno << 16;
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100120 writel(stat, pcie->base + MVPCIE_STAT_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200121}
122
Anton Schubert9c28d612015-08-11 11:54:01 +0200123static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
124{
125 return container_of(hose, struct mvebu_pcie, hose);
126}
127
Pali Rohára7b61ab2021-10-22 16:22:10 +0200128static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
129 int busno, int dev, int func)
Marek Behún10eb2cc2021-02-08 23:01:40 +0100130{
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100131 /* On the root bus is only one PCI Bridge */
132 if (busno == 0 && (dev != 0 || func != 0))
Pali Rohára7b61ab2021-10-22 16:22:10 +0200133 return false;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100134
Pali Rohár79b4eb22021-10-22 16:22:12 +0200135 /* Access to other buses is possible when link is up */
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100136 if (busno != 0 && !mvebu_pcie_link_up(pcie))
Pali Rohár79b4eb22021-10-22 16:22:12 +0200137 return false;
138
Pali Rohára7b61ab2021-10-22 16:22:10 +0200139 /* On secondary bus can be only one PCIe device */
140 if (busno == pcie->sec_busno && dev != 0)
141 return false;
142
143 return true;
Marek Behún10eb2cc2021-02-08 23:01:40 +0100144}
145
Simon Glassc4e72c42020-01-27 08:49:37 -0700146static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
Stefan Roese94f453e2019-01-25 11:52:43 +0100147 uint offset, ulong *valuep,
148 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200149{
Simon Glassc69cda22020-12-03 16:55:20 -0700150 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200151 int busno = PCI_BUS(bdf) - dev_seq(bus);
152 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100153
Marek Behún10eb2cc2021-02-08 23:01:40 +0100154 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
155 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Anton Schubert9c28d612015-08-11 11:54:01 +0200156
Pali Rohára7b61ab2021-10-22 16:22:10 +0200157 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100158 debug("- out of range\n");
159 *valuep = pci_get_ff(size);
160 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200161 }
162
Pali Rohára7b61ab2021-10-22 16:22:10 +0200163 /*
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100164 * The configuration space of the PCI Bridge on the root bus (zero) is
Pali Rohára48e4282021-11-11 16:35:45 +0100165 * of Type 0 but the BAR registers (including ROM BAR) don't have the
166 * same meaning as in the PCIe specification. Therefore do not access
167 * BAR registers and non-common registers (those which have different
168 * meaning for Type 0 and Type 1 config space) of the PCI Bridge and
169 * instead read their content from driver virtual cfgcache[].
Pali Rohára7b61ab2021-10-22 16:22:10 +0200170 */
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100171 if (busno == 0 && ((offset >= 0x10 && offset < 0x34) ||
172 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200173 data = pcie->cfgcache[(offset - 0x10) / 4];
174 debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
175 offset, size, data);
176 *valuep = pci_conv_32_to_size(data, offset, size);
177 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200178 }
179
180 /*
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100181 * PCI bridge is device 0 at the root bus (zero) but mvebu has it
182 * mapped on secondary bus with device number 1.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200183 */
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100184 if (busno == 0)
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100185 addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200186 else
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100187 addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200188
Anton Schubert9c28d612015-08-11 11:54:01 +0200189 /* write address */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100190 writel(addr, pcie->base + MVPCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100191
192 /* read data */
Pali Rohár657177a2021-10-22 16:22:09 +0200193 switch (size) {
194 case PCI_SIZE_8:
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100195 data = readb(pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 3));
Pali Rohár657177a2021-10-22 16:22:09 +0200196 break;
197 case PCI_SIZE_16:
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100198 data = readw(pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 2));
Pali Rohár657177a2021-10-22 16:22:09 +0200199 break;
200 case PCI_SIZE_32:
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100201 data = readl(pcie->base + MVPCIE_CONF_DATA_OFF);
Pali Rohár657177a2021-10-22 16:22:09 +0200202 break;
203 default:
204 return -EINVAL;
205 }
206
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100207 if (busno == 0 && (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200208 /*
209 * Change Header Type of PCI Bridge device to Type 1
210 * (0x01, used by PCI Bridges) because mvebu reports
211 * Type 0 (0x00, used by Upstream and Endpoint devices).
212 */
213 data = pci_conv_size_to_32(data, 0, offset, size);
214 data &= ~0x007f0000;
215 data |= PCI_HEADER_TYPE_BRIDGE << 16;
216 data = pci_conv_32_to_size(data, offset, size);
217 }
218
Marek Behún26f7a762021-02-08 23:01:39 +0100219 debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
Pali Rohár657177a2021-10-22 16:22:09 +0200220 *valuep = data;
Anton Schubert9c28d612015-08-11 11:54:01 +0200221
222 return 0;
223}
224
Stefan Roese94f453e2019-01-25 11:52:43 +0100225static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
226 uint offset, ulong value,
227 enum pci_size_t size)
Anton Schubert9c28d612015-08-11 11:54:01 +0200228{
Simon Glassc69cda22020-12-03 16:55:20 -0700229 struct mvebu_pcie *pcie = dev_get_plat(bus);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200230 int busno = PCI_BUS(bdf) - dev_seq(bus);
231 u32 addr, data;
Stefan Roese94f453e2019-01-25 11:52:43 +0100232
Marek Behún10eb2cc2021-02-08 23:01:40 +0100233 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
234 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
Marek Behún26f7a762021-02-08 23:01:39 +0100235 debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
Anton Schubert9c28d612015-08-11 11:54:01 +0200236
Pali Rohára7b61ab2021-10-22 16:22:10 +0200237 if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
Stefan Roese6a2fa282021-01-25 15:25:31 +0100238 debug("- out of range\n");
239 return 0;
Anton Schubert9c28d612015-08-11 11:54:01 +0200240 }
241
Pali Rohára7b61ab2021-10-22 16:22:10 +0200242 /*
Pali Rohára48e4282021-11-11 16:35:45 +0100243 * As explained in mvebu_pcie_read_config(), PCI Bridge Type 1 specific
244 * config registers are not available, so we write their content only
245 * into driver virtual cfgcache[].
246 * And as explained in mvebu_pcie_probe(), mvebu has its own specific
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100247 * way for configuring secondary bus number.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200248 */
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100249 if (busno == 0 && ((offset >= 0x10 && offset < 0x34) ||
250 (offset >= 0x38 && offset < 0x3c))) {
Pali Rohára7b61ab2021-10-22 16:22:10 +0200251 debug("Writing to cfgcache only\n");
252 data = pcie->cfgcache[(offset - 0x10) / 4];
253 data = pci_conv_size_to_32(data, value, offset, size);
254 /* mvebu PCI bridge does not have configurable bars */
255 if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
Pali Rohára48e4282021-11-11 16:35:45 +0100256 (offset & ~3) == PCI_BASE_ADDRESS_1 ||
257 (offset & ~3) == PCI_ROM_ADDRESS1)
Pali Rohára7b61ab2021-10-22 16:22:10 +0200258 data = 0x0;
259 pcie->cfgcache[(offset - 0x10) / 4] = data;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200260 /* mvebu has its own way how to set PCI secondary bus number */
261 if (offset == PCI_SECONDARY_BUS ||
262 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
263 pcie->sec_busno = (data >> 8) & 0xff;
264 mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
265 debug("Secondary bus number was changed to %d\n",
266 pcie->sec_busno);
267 }
268 return 0;
Pali Rohára7b61ab2021-10-22 16:22:10 +0200269 }
270
271 /*
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100272 * PCI bridge is device 0 at the root bus (zero) but mvebu has it
273 * mapped on secondary bus with device number 1.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200274 */
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100275 if (busno == 0)
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100276 addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200277 else
Pali Rohárd0dd49f2021-11-26 11:42:45 +0100278 addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200279
Marek Behún241d7632021-02-08 23:01:38 +0100280 /* write address */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100281 writel(addr, pcie->base + MVPCIE_CONF_ADDR_OFF);
Marek Behún241d7632021-02-08 23:01:38 +0100282
283 /* write data */
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200284 switch (size) {
285 case PCI_SIZE_8:
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100286 writeb(value, pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 3));
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200287 break;
288 case PCI_SIZE_16:
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100289 writew(value, pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 2));
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200290 break;
291 case PCI_SIZE_32:
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100292 writel(value, pcie->base + MVPCIE_CONF_DATA_OFF);
Pali Rohárdaa9bfd2021-10-22 16:22:08 +0200293 break;
294 default:
295 return -EINVAL;
296 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200297
298 return 0;
299}
300
301/*
302 * Setup PCIE BARs and Address Decode Wins:
Pali Rohár4a1a5932021-11-11 16:35:42 +0100303 * BAR[0] -> internal registers
304 * BAR[1] -> covers all DRAM banks
305 * BAR[2] -> disabled
Anton Schubert9c28d612015-08-11 11:54:01 +0200306 * WIN[0-3] -> DRAM bank[0-3]
307 */
308static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
309{
310 const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
311 u32 size;
312 int i;
313
314 /* First, disable and clear BARs and windows. */
315 for (i = 1; i < 3; i++) {
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100316 writel(0, pcie->base + MVPCIE_BAR_CTRL_OFF(i));
317 writel(0, pcie->base + MVPCIE_BAR_LO_OFF(i));
318 writel(0, pcie->base + MVPCIE_BAR_HI_OFF(i));
Anton Schubert9c28d612015-08-11 11:54:01 +0200319 }
320
321 for (i = 0; i < 5; i++) {
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100322 writel(0, pcie->base + MVPCIE_WIN04_CTRL_OFF(i));
323 writel(0, pcie->base + MVPCIE_WIN04_BASE_OFF(i));
324 writel(0, pcie->base + MVPCIE_WIN04_REMAP_OFF(i));
Anton Schubert9c28d612015-08-11 11:54:01 +0200325 }
326
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100327 writel(0, pcie->base + MVPCIE_WIN5_CTRL_OFF);
328 writel(0, pcie->base + MVPCIE_WIN5_BASE_OFF);
329 writel(0, pcie->base + MVPCIE_WIN5_REMAP_OFF);
Anton Schubert9c28d612015-08-11 11:54:01 +0200330
331 /* Setup windows for DDR banks. Count total DDR size on the fly. */
332 size = 0;
333 for (i = 0; i < dram->num_cs; i++) {
334 const struct mbus_dram_window *cs = dram->cs + i;
335
336 writel(cs->base & 0xffff0000,
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100337 pcie->base + MVPCIE_WIN04_BASE_OFF(i));
338 writel(0, pcie->base + MVPCIE_WIN04_REMAP_OFF(i));
Anton Schubert9c28d612015-08-11 11:54:01 +0200339 writel(((cs->size - 1) & 0xffff0000) |
340 (cs->mbus_attr << 8) |
341 (dram->mbus_dram_target_id << 4) | 1,
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100342 pcie->base + MVPCIE_WIN04_CTRL_OFF(i));
Anton Schubert9c28d612015-08-11 11:54:01 +0200343
344 size += cs->size;
345 }
346
347 /* Round up 'size' to the nearest power of two. */
348 if ((size & (size - 1)) != 0)
349 size = 1 << fls(size);
350
351 /* Setup BAR[1] to all DRAM banks. */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100352 writel(dram->cs[0].base | 0xc, pcie->base + MVPCIE_BAR_LO_OFF(1));
353 writel(0, pcie->base + MVPCIE_BAR_HI_OFF(1));
Anton Schubert9c28d612015-08-11 11:54:01 +0200354 writel(((size - 1) & 0xffff0000) | 0x1,
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100355 pcie->base + MVPCIE_BAR_CTRL_OFF(1));
Pali Rohár4a1a5932021-11-11 16:35:42 +0100356
357 /* Setup BAR[0] to internal registers. */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100358 writel(pcie->intregs, pcie->base + MVPCIE_BAR_LO_OFF(0));
359 writel(0, pcie->base + MVPCIE_BAR_HI_OFF(0));
Anton Schubert9c28d612015-08-11 11:54:01 +0200360}
361
Pali Rohárafef9f42021-12-21 12:20:16 +0100362/* Only enable PCIe link, do not setup it */
363static int mvebu_pcie_enable_link(struct mvebu_pcie *pcie, ofnode node)
Anton Schubert9c28d612015-08-11 11:54:01 +0200364{
Pali Rohár94c30f92021-12-21 12:20:19 +0100365 struct reset_ctl rst;
366 int ret;
367
368 ret = reset_get_by_index_nodev(node, 0, &rst);
369 if (ret == -ENOENT) {
370 return 0;
371 } else if (ret < 0) {
372 printf("%s: cannot get reset controller: %d\n", pcie->name, ret);
373 return ret;
374 }
375
376 ret = reset_request(&rst);
377 if (ret) {
378 printf("%s: cannot request reset controller: %d\n", pcie->name, ret);
379 return ret;
380 }
381
382 ret = reset_deassert(&rst);
383 reset_free(&rst);
384 if (ret) {
385 printf("%s: cannot enable PCIe port: %d\n", pcie->name, ret);
386 return ret;
387 }
388
Pali Rohárafef9f42021-12-21 12:20:16 +0100389 return 0;
390}
391
392/* Setup PCIe link but do not enable it */
393static void mvebu_pcie_setup_link(struct mvebu_pcie *pcie)
394{
Anton Schubert9c28d612015-08-11 11:54:01 +0200395 u32 reg;
Anton Schubert9c28d612015-08-11 11:54:01 +0200396
Pali Rohár2344a762021-10-22 16:22:14 +0200397 /* Setup PCIe controller to Root Complex mode */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100398 reg = readl(pcie->base + MVPCIE_CTRL_OFF);
399 reg |= MVPCIE_CTRL_RC_MODE;
400 writel(reg, pcie->base + MVPCIE_CTRL_OFF);
Pali Rohár94c30f92021-12-21 12:20:19 +0100401
402 /*
403 * Set Maximum Link Width to X1 or X4 in Root Port's PCIe Link
404 * Capability register. This register is defined by PCIe specification
405 * as read-only but this mvebu controller has it as read-write and must
406 * be set to number of SerDes PCIe lanes (1 or 4). If this register is
407 * not set correctly then link with endpoint card is not established.
408 */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100409 reg = readl(pcie->base + MVPCIE_ROOT_PORT_PCI_EXP_OFF + PCI_EXP_LNKCAP);
Pali Rohár94c30f92021-12-21 12:20:19 +0100410 reg &= ~PCI_EXP_LNKCAP_MLW;
411 reg |= (pcie->is_x4 ? 4 : 1) << 4;
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100412 writel(reg, pcie->base + MVPCIE_ROOT_PORT_PCI_EXP_OFF + PCI_EXP_LNKCAP);
Pali Rohárafef9f42021-12-21 12:20:16 +0100413}
414
415static int mvebu_pcie_probe(struct udevice *dev)
416{
417 struct mvebu_pcie *pcie = dev_get_plat(dev);
418 struct udevice *ctlr = pci_get_controller(dev);
419 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
420 u32 reg;
Pali Rohárca3756c2022-08-05 16:03:41 +0200421 int ret;
422
423 /* Request for optional PERST# GPIO */
424 ret = gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio, GPIOD_IS_OUT);
425 if (ret && ret != -ENOENT) {
426 printf("%s: unable to request reset-gpios: %d\n", pcie->name, ret);
427 return ret;
428 }
Pali Rohár2344a762021-10-22 16:22:14 +0200429
Pali Rohára7b61ab2021-10-22 16:22:10 +0200430 /*
431 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
432 * because default value is Memory controller (0x508000) which
433 * U-Boot cannot recognize as P2P Bridge.
434 *
435 * Note that this mvebu PCI Bridge does not have compliant Type 1
Pali Rohára48e4282021-11-11 16:35:45 +0100436 * Configuration Space. Header Type is reported as Type 0 and it
437 * has format of Type 0 config space.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200438 *
Pali Rohára48e4282021-11-11 16:35:45 +0100439 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
440 * have the same format in Marvell's specification as in PCIe
441 * specification, but their meaning is totally different and they do
442 * different things: they are aliased into internal mvebu registers
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100443 * (e.g. MVPCIE_BAR_LO_OFF) and these should not be changed or
Pali Rohára48e4282021-11-11 16:35:45 +0100444 * reconfigured by pci device drivers.
445 *
446 * So our driver converts Type 0 config space to Type 1 and reports
447 * Header Type as Type 1. Access to BAR registers and to non-existent
448 * Type 1 registers is redirected to the virtual cfgcache[] buffer,
449 * which avoids changing unrelated registers.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200450 */
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100451 reg = readl(pcie->base + MVPCIE_ROOT_PORT_PCI_CFG_OFF + PCI_CLASS_REVISION);
Pali Rohára7b61ab2021-10-22 16:22:10 +0200452 reg &= ~0xffffff00;
Pali Rohárd7b90402022-02-18 13:18:40 +0100453 reg |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
Pali Rohárfc27e5d2022-02-18 12:25:23 +0100454 writel(reg, pcie->base + MVPCIE_ROOT_PORT_PCI_CFG_OFF + PCI_CLASS_REVISION);
Anton Schubert9c28d612015-08-11 11:54:01 +0200455
Pali Rohára7b61ab2021-10-22 16:22:10 +0200456 /*
457 * mvebu uses local bus number and local device number to determinate
458 * type of config request. Type 0 is used if target bus number equals
459 * local bus number and target device number differs from local device
460 * number. Type 1 is used if target bus number differs from local bus
461 * number. And when target bus number equals local bus number and
462 * target device equals local device number then request is routed to
463 * PCI Bridge which represent local PCIe Root Port.
464 *
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100465 * It means that PCI root and secondary buses shares one bus number
Pali Rohára7b61ab2021-10-22 16:22:10 +0200466 * which is configured via local bus number. Determination if config
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100467 * request should go to root or secondary bus is done based on local
Pali Rohára7b61ab2021-10-22 16:22:10 +0200468 * device number.
469 *
470 * PCIe is point-to-point bus, so at secondary bus is always exactly one
471 * device with number 0. So set local device number to 1, it would not
472 * conflict with any device on secondary bus number and will ensure that
473 * accessing secondary bus and all buses behind secondary would work
474 * automatically and correctly. Therefore this configuration of local
475 * device number implies that setting of local bus number configures
476 * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
477 * later configure it via config write requests to the correct value.
478 * mvebu_pcie_write_config() catches config write requests which tries
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100479 * to change secondary bus number and correctly updates local bus number
480 * based on new secondary bus number.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200481 *
482 * With this configuration is PCI Bridge available at secondary bus as
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100483 * device number 1. But it must be available at root bus (zero) as device
Pali Rohára7b61ab2021-10-22 16:22:10 +0200484 * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
Pali Rohár0eebc3d2022-02-15 11:34:01 +0100485 * functions rewrite address to the real one when accessing the root bus.
Pali Rohára7b61ab2021-10-22 16:22:10 +0200486 */
487 mvebu_pcie_set_local_bus_nr(pcie, 0);
488 mvebu_pcie_set_local_dev_nr(pcie, 1);
Anton Schubert9c28d612015-08-11 11:54:01 +0200489
Pali Rohár43640712022-01-13 14:28:04 +0100490 /*
491 * Kirkwood arch code already maps mbus windows for PCIe IO and MEM.
492 * So skip calling mvebu_mbus_add_window_by_id() function as it would
493 * fail on error "conflicts with another window" which means conflict
494 * with existing PCIe window mappings.
495 */
496#ifndef CONFIG_ARCH_KIRKWOOD
Pali Rohár537b0142021-12-21 12:20:13 +0100497 if (resource_size(&pcie->mem) &&
498 mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
Stefan Roese94f453e2019-01-25 11:52:43 +0100499 (phys_addr_t)pcie->mem.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100500 resource_size(&pcie->mem))) {
Pali Rohárafef9f42021-12-21 12:20:16 +0100501 printf("%s: unable to add mbus window for mem at %08x+%08x\n",
502 pcie->name,
Pali Roháre1cee892021-11-11 16:35:43 +0100503 (u32)pcie->mem.start, (unsigned)resource_size(&pcie->mem));
Pali Rohár537b0142021-12-21 12:20:13 +0100504 pcie->mem.start = 0;
505 pcie->mem.end = -1;
Stefan Roese94f453e2019-01-25 11:52:43 +0100506 }
507
Pali Rohár537b0142021-12-21 12:20:13 +0100508 if (resource_size(&pcie->io) &&
509 mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
Phil Sutterba8ae032021-01-03 23:06:46 +0100510 (phys_addr_t)pcie->io.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100511 resource_size(&pcie->io))) {
Pali Rohárafef9f42021-12-21 12:20:16 +0100512 printf("%s: unable to add mbus window for IO at %08x+%08x\n",
513 pcie->name,
Pali Roháre1cee892021-11-11 16:35:43 +0100514 (u32)pcie->io.start, (unsigned)resource_size(&pcie->io));
Pali Rohár537b0142021-12-21 12:20:13 +0100515 pcie->io.start = 0;
516 pcie->io.end = -1;
Phil Sutterba8ae032021-01-03 23:06:46 +0100517 }
Pali Rohár43640712022-01-13 14:28:04 +0100518#endif
Phil Sutterba8ae032021-01-03 23:06:46 +0100519
Stefan Roese94f453e2019-01-25 11:52:43 +0100520 /* Setup windows and configure host bridge */
521 mvebu_pcie_setup_wins(pcie);
522
Stefan Roese94f453e2019-01-25 11:52:43 +0100523 /* PCI memory space */
524 pci_set_region(hose->regions + 0, pcie->mem.start,
Pali Roháre1cee892021-11-11 16:35:43 +0100525 pcie->mem.start, resource_size(&pcie->mem), PCI_REGION_MEM);
Pali Rohár537b0142021-12-21 12:20:13 +0100526 hose->region_count = 1;
527
528 if (resource_size(&pcie->mem)) {
529 pci_set_region(hose->regions + hose->region_count,
530 pcie->mem.start, pcie->mem.start,
531 resource_size(&pcie->mem),
532 PCI_REGION_MEM);
533 hose->region_count++;
534 }
535
536 if (resource_size(&pcie->io)) {
537 pci_set_region(hose->regions + hose->region_count,
538 pcie->io.start, pcie->io.start,
539 resource_size(&pcie->io),
540 PCI_REGION_IO);
541 hose->region_count++;
542 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100543
Pali Rohára7b61ab2021-10-22 16:22:10 +0200544 /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
545 pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
546 PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
547 pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
548 PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
549
Pali Rohárca3756c2022-08-05 16:03:41 +0200550 /* Release PERST# via GPIO when it was defined */
551 if (dm_gpio_is_valid(&pcie->reset_gpio))
552 dm_gpio_set_value(&pcie->reset_gpio, 0);
553
Pali Roháre7ff4272021-12-21 12:20:17 +0100554 mvebu_pcie_wait_for_link(pcie);
555
Stefan Roese94f453e2019-01-25 11:52:43 +0100556 return 0;
557}
558
Stefan Roese94f453e2019-01-25 11:52:43 +0100559#define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
560#define DT_TYPE_IO 0x1
561#define DT_TYPE_MEM32 0x2
562#define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
563#define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
564
565static int mvebu_get_tgt_attr(ofnode node, int devfn,
566 unsigned long type,
567 unsigned int *tgt,
568 unsigned int *attr)
569{
570 const int na = 3, ns = 2;
571 const __be32 *range;
572 int rlen, nranges, rangesz, pna, i;
573
574 *tgt = -1;
575 *attr = -1;
576
577 range = ofnode_get_property(node, "ranges", &rlen);
578 if (!range)
579 return -EINVAL;
580
Stefan Roese0df62e82019-02-11 07:53:34 +0100581 /*
582 * Linux uses of_n_addr_cells() to get the number of address cells
583 * here. Currently this function is only available in U-Boot when
584 * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
585 * general, lets't hardcode the "pna" value in the U-Boot code.
586 */
Stefan Roese94f453e2019-01-25 11:52:43 +0100587 pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
588 rangesz = pna + na + ns;
589 nranges = rlen / sizeof(__be32) / rangesz;
590
591 for (i = 0; i < nranges; i++, range += rangesz) {
592 u32 flags = of_read_number(range, 1);
593 u32 slot = of_read_number(range + 1, 1);
594 u64 cpuaddr = of_read_number(range + na, pna);
595 unsigned long rtype;
596
597 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
598 rtype = IORESOURCE_IO;
599 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
600 rtype = IORESOURCE_MEM;
601 else
Anton Schubert9c28d612015-08-11 11:54:01 +0200602 continue;
Anton Schubert9c28d612015-08-11 11:54:01 +0200603
Stefan Roese94f453e2019-01-25 11:52:43 +0100604 /*
605 * The Linux code used PCI_SLOT() here, which expects devfn
606 * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
607 * only expects devfn in 15..8, where its saved in this driver.
608 */
609 if (slot == PCI_DEV(devfn) && type == rtype) {
610 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
611 *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
612 return 0;
Phil Sutter9a045272015-12-25 14:41:20 +0100613 }
Anton Schubert9c28d612015-08-11 11:54:01 +0200614 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100615
616 return -ENOENT;
Anton Schubert9c28d612015-08-11 11:54:01 +0200617}
Stefan Roese94f453e2019-01-25 11:52:43 +0100618
Pali Rohárafef9f42021-12-21 12:20:16 +0100619static int mvebu_pcie_port_parse_dt(ofnode node, ofnode parent, struct mvebu_pcie *pcie)
Stefan Roese94f453e2019-01-25 11:52:43 +0100620{
Pali Rohárafef9f42021-12-21 12:20:16 +0100621 struct fdt_pci_addr pci_addr;
Pali Rohár6f4988f2021-12-21 12:20:14 +0100622 const u32 *addr;
Pali Rohár94c30f92021-12-21 12:20:19 +0100623 u32 num_lanes;
Stefan Roese94f453e2019-01-25 11:52:43 +0100624 int ret = 0;
Pali Rohár6f4988f2021-12-21 12:20:14 +0100625 int len;
Stefan Roese94f453e2019-01-25 11:52:43 +0100626
627 /* Get port number, lane number and memory target / attr */
Pali Rohárafef9f42021-12-21 12:20:16 +0100628 if (ofnode_read_u32(node, "marvell,pcie-port",
Stefan Roese94f453e2019-01-25 11:52:43 +0100629 &pcie->port)) {
630 ret = -ENODEV;
631 goto err;
632 }
633
Pali Rohárafef9f42021-12-21 12:20:16 +0100634 if (ofnode_read_u32(node, "marvell,pcie-lane", &pcie->lane))
Stefan Roese94f453e2019-01-25 11:52:43 +0100635 pcie->lane = 0;
636
637 sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
638
Pali Rohár94c30f92021-12-21 12:20:19 +0100639 if (!ofnode_read_u32(node, "num-lanes", &num_lanes) && num_lanes == 4)
640 pcie->is_x4 = true;
641
Pali Rohárafef9f42021-12-21 12:20:16 +0100642 /* devfn is in bits [15:8], see PCI_DEV usage */
643 ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg", &pci_addr);
644 if (ret < 0) {
645 printf("%s: property \"reg\" is invalid\n", pcie->name);
Stefan Roese94f453e2019-01-25 11:52:43 +0100646 goto err;
647 }
Pali Rohárafef9f42021-12-21 12:20:16 +0100648 pcie->devfn = pci_addr.phys_hi & 0xff00;
Stefan Roese94f453e2019-01-25 11:52:43 +0100649
Pali Rohárafef9f42021-12-21 12:20:16 +0100650 ret = mvebu_get_tgt_attr(parent, pcie->devfn,
Stefan Roese94f453e2019-01-25 11:52:43 +0100651 IORESOURCE_MEM,
652 &pcie->mem_target, &pcie->mem_attr);
653 if (ret < 0) {
654 printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
655 goto err;
656 }
657
Pali Rohárafef9f42021-12-21 12:20:16 +0100658 ret = mvebu_get_tgt_attr(parent, pcie->devfn,
Phil Sutterba8ae032021-01-03 23:06:46 +0100659 IORESOURCE_IO,
660 &pcie->io_target, &pcie->io_attr);
661 if (ret < 0) {
662 printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
663 goto err;
664 }
665
Stefan Roese94f453e2019-01-25 11:52:43 +0100666 /* Parse PCIe controller register base from DT */
Pali Rohárafef9f42021-12-21 12:20:16 +0100667 addr = ofnode_get_property(node, "assigned-addresses", &len);
Pali Rohár6f4988f2021-12-21 12:20:14 +0100668 if (!addr) {
669 printf("%s: property \"assigned-addresses\" not found\n", pcie->name);
670 ret = -FDT_ERR_NOTFOUND;
Stefan Roese94f453e2019-01-25 11:52:43 +0100671 goto err;
Pali Rohár6f4988f2021-12-21 12:20:14 +0100672 }
673
Pali Rohárafef9f42021-12-21 12:20:16 +0100674 pcie->base = (void *)(u32)ofnode_translate_address(node, addr);
Pali Rohár137db2a2021-12-21 12:20:15 +0100675 pcie->intregs = (u32)pcie->base - fdt32_to_cpu(addr[2]);
Stefan Roese94f453e2019-01-25 11:52:43 +0100676
Stefan Roese94f453e2019-01-25 11:52:43 +0100677 return 0;
678
679err:
680 return ret;
681}
682
683static const struct dm_pci_ops mvebu_pcie_ops = {
684 .read_config = mvebu_pcie_read_config,
685 .write_config = mvebu_pcie_write_config,
686};
687
688static struct driver pcie_mvebu_drv = {
689 .name = "pcie_mvebu",
690 .id = UCLASS_PCI,
691 .ops = &mvebu_pcie_ops,
692 .probe = mvebu_pcie_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700693 .plat_auto = sizeof(struct mvebu_pcie),
Stefan Roese94f453e2019-01-25 11:52:43 +0100694};
695
696/*
697 * Use a MISC device to bind the n instances (child nodes) of the
698 * PCIe base controller in UCLASS_PCI.
699 */
700static int mvebu_pcie_bind(struct udevice *parent)
701{
Pali Rohárafef9f42021-12-21 12:20:16 +0100702 struct mvebu_pcie **ports_pcie;
Stefan Roese94f453e2019-01-25 11:52:43 +0100703 struct mvebu_pcie *pcie;
704 struct uclass_driver *drv;
705 struct udevice *dev;
Pali Rohár537b0142021-12-21 12:20:13 +0100706 struct resource mem;
707 struct resource io;
Pali Rohárafef9f42021-12-21 12:20:16 +0100708 int ports_count, i;
709 ofnode *ports_nodes;
Stefan Roese94f453e2019-01-25 11:52:43 +0100710 ofnode subnode;
711
Pali Rohár03a8a5e2021-10-22 16:22:15 +0200712 /* Lookup pci driver */
Stefan Roese94f453e2019-01-25 11:52:43 +0100713 drv = lists_uclass_lookup(UCLASS_PCI);
714 if (!drv) {
715 puts("Cannot find PCI driver\n");
716 return -ENOENT;
717 }
718
Pali Rohárafef9f42021-12-21 12:20:16 +0100719 ports_count = ofnode_get_child_count(dev_ofnode(parent));
720 ports_pcie = calloc(ports_count, sizeof(*ports_pcie));
721 ports_nodes = calloc(ports_count, sizeof(*ports_nodes));
722 if (!ports_pcie || !ports_nodes) {
723 free(ports_pcie);
724 free(ports_nodes);
725 return -ENOMEM;
726 }
727 ports_count = 0;
728
Pali Rohár43640712022-01-13 14:28:04 +0100729#ifdef CONFIG_ARCH_KIRKWOOD
730 mem.start = KW_DEFADR_PCI_MEM;
731 mem.end = KW_DEFADR_PCI_MEM + KW_DEFADR_PCI_MEM_SIZE - 1;
732 io.start = KW_DEFADR_PCI_IO;
733 io.end = KW_DEFADR_PCI_IO + KW_DEFADR_PCI_IO_SIZE - 1;
734#else
Pali Rohár537b0142021-12-21 12:20:13 +0100735 mem.start = MBUS_PCI_MEM_BASE;
736 mem.end = MBUS_PCI_MEM_BASE + MBUS_PCI_MEM_SIZE - 1;
737 io.start = MBUS_PCI_IO_BASE;
738 io.end = MBUS_PCI_IO_BASE + MBUS_PCI_IO_SIZE - 1;
Pali Rohár43640712022-01-13 14:28:04 +0100739#endif
Pali Rohár537b0142021-12-21 12:20:13 +0100740
Pali Rohárafef9f42021-12-21 12:20:16 +0100741 /* First phase: Fill mvebu_pcie struct for each port */
Stefan Roese94f453e2019-01-25 11:52:43 +0100742 ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
Simon Glass89090662022-09-06 20:27:17 -0600743 if (!ofnode_is_enabled(subnode))
Stefan Roese94f453e2019-01-25 11:52:43 +0100744 continue;
745
746 pcie = calloc(1, sizeof(*pcie));
747 if (!pcie)
Pali Rohárafef9f42021-12-21 12:20:16 +0100748 continue;
749
750 if (mvebu_pcie_port_parse_dt(subnode, dev_ofnode(parent), pcie) < 0) {
751 free(pcie);
752 continue;
753 }
Stefan Roese94f453e2019-01-25 11:52:43 +0100754
Pali Rohár537b0142021-12-21 12:20:13 +0100755 /*
756 * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
757 * into SoCs address space. Each controller will map 128M of MEM
758 * and 64K of I/O space when registered.
759 */
760
761 if (resource_size(&mem) >= SZ_128M) {
762 pcie->mem.start = mem.start;
763 pcie->mem.end = mem.start + SZ_128M - 1;
764 mem.start += SZ_128M;
765 } else {
Pali Rohárafef9f42021-12-21 12:20:16 +0100766 printf("%s: unable to assign mbus window for mem\n", pcie->name);
Pali Rohár537b0142021-12-21 12:20:13 +0100767 pcie->mem.start = 0;
768 pcie->mem.end = -1;
769 }
770
771 if (resource_size(&io) >= SZ_64K) {
772 pcie->io.start = io.start;
773 pcie->io.end = io.start + SZ_64K - 1;
774 io.start += SZ_64K;
775 } else {
Pali Rohárafef9f42021-12-21 12:20:16 +0100776 printf("%s: unable to assign mbus window for io\n", pcie->name);
Pali Rohár537b0142021-12-21 12:20:13 +0100777 pcie->io.start = 0;
778 pcie->io.end = -1;
779 }
780
Pali Rohárafef9f42021-12-21 12:20:16 +0100781 ports_pcie[ports_count] = pcie;
782 ports_nodes[ports_count] = subnode;
783 ports_count++;
784 }
785
786 /* Second phase: Setup all PCIe links (do not enable them yet) */
787 for (i = 0; i < ports_count; i++)
788 mvebu_pcie_setup_link(ports_pcie[i]);
789
790 /* Third phase: Enable all PCIe links and create for each UCLASS_PCI device */
791 for (i = 0; i < ports_count; i++) {
792 pcie = ports_pcie[i];
793 subnode = ports_nodes[i];
794
795 /*
796 * PCIe link can be enabled only after all PCIe links were
797 * properly configured. This is because more PCIe links shares
798 * one enable bit and some PCIe links cannot be enabled
799 * individually.
800 */
801 if (mvebu_pcie_enable_link(pcie, subnode) < 0) {
802 free(pcie);
803 continue;
804 }
805
Stefan Roese94f453e2019-01-25 11:52:43 +0100806 /* Create child device UCLASS_PCI and bind it */
Simon Glass734206d2020-11-28 17:50:01 -0700807 device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
808 &dev);
Stefan Roese94f453e2019-01-25 11:52:43 +0100809 }
810
Pali Rohárafef9f42021-12-21 12:20:16 +0100811 free(ports_pcie);
812 free(ports_nodes);
813
Stefan Roese94f453e2019-01-25 11:52:43 +0100814 return 0;
815}
816
817static const struct udevice_id mvebu_pcie_ids[] = {
818 { .compatible = "marvell,armada-xp-pcie" },
819 { .compatible = "marvell,armada-370-pcie" },
Pali Rohár43640712022-01-13 14:28:04 +0100820 { .compatible = "marvell,kirkwood-pcie" },
Stefan Roese94f453e2019-01-25 11:52:43 +0100821 { }
822};
823
824U_BOOT_DRIVER(pcie_mvebu_base) = {
825 .name = "pcie_mvebu_base",
826 .id = UCLASS_MISC,
827 .of_match = mvebu_pcie_ids,
828 .bind = mvebu_pcie_bind,
829};