Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 2 | /* |
| 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 Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
| 18 | #include <dm/lists.h> |
| 19 | #include <dm/of_access.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 20 | #include <pci.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <asm/arch/cpu.h> |
| 23 | #include <asm/arch/soc.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 25 | #include <linux/errno.h> |
| 26 | #include <linux/ioport.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 27 | #include <linux/mbus.h> |
| 28 | |
| 29 | DECLARE_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)) |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 39 | #define PCIE_EXP_ROM_BAR_OFF 0x0030 |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 40 | #define PCIE_CAPAB_OFF 0x0060 |
| 41 | #define PCIE_CTRL_STAT_OFF 0x0068 |
| 42 | #define PCIE_HEADER_LOG_4_OFF 0x0128 |
| 43 | #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4)) |
| 44 | #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4)) |
| 45 | #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4)) |
| 46 | #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4)) |
| 47 | #define PCIE_WIN5_CTRL_OFF 0x1880 |
| 48 | #define PCIE_WIN5_BASE_OFF 0x1884 |
| 49 | #define PCIE_WIN5_REMAP_OFF 0x188c |
| 50 | #define PCIE_CONF_ADDR_OFF 0x18f8 |
| 51 | #define PCIE_CONF_ADDR_EN BIT(31) |
| 52 | #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc)) |
| 53 | #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16) |
| 54 | #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11) |
| 55 | #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8) |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 56 | #define PCIE_CONF_ADDR(b, d, f, reg) \ |
| 57 | (PCIE_CONF_BUS(b) | PCIE_CONF_DEV(d) | \ |
| 58 | PCIE_CONF_FUNC(f) | PCIE_CONF_REG(reg) | \ |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 59 | PCIE_CONF_ADDR_EN) |
| 60 | #define PCIE_CONF_DATA_OFF 0x18fc |
| 61 | #define PCIE_MASK_OFF 0x1910 |
| 62 | #define PCIE_MASK_ENABLE_INTS (0xf << 24) |
| 63 | #define PCIE_CTRL_OFF 0x1a00 |
| 64 | #define PCIE_CTRL_X1_MODE BIT(0) |
| 65 | #define PCIE_STAT_OFF 0x1a04 |
| 66 | #define PCIE_STAT_BUS (0xff << 8) |
| 67 | #define PCIE_STAT_DEV (0x1f << 16) |
| 68 | #define PCIE_STAT_LINK_DOWN BIT(0) |
| 69 | #define PCIE_DEBUG_CTRL 0x1a60 |
| 70 | #define PCIE_DEBUG_SOFT_RESET BIT(20) |
| 71 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 72 | struct mvebu_pcie { |
| 73 | struct pci_controller hose; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 74 | void __iomem *base; |
| 75 | void __iomem *membase; |
| 76 | struct resource mem; |
| 77 | void __iomem *iobase; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 78 | struct resource io; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 79 | u32 port; |
| 80 | u32 lane; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 81 | int devfn; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 82 | u32 lane_mask; |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 83 | int first_busno; |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 84 | int sec_busno; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 85 | char name[16]; |
| 86 | unsigned int mem_target; |
| 87 | unsigned int mem_attr; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 88 | unsigned int io_target; |
| 89 | unsigned int io_attr; |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 90 | u32 cfgcache[0x34 - 0x10]; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 93 | /* |
| 94 | * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped |
VlaoMao | 9b9606d | 2017-09-22 18:49:02 +0300 | [diff] [blame] | 95 | * into SoCs address space. Each controller will map 128M of MEM |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 96 | * and 64K of I/O space when registered. |
| 97 | */ |
| 98 | static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE; |
VlaoMao | 9b9606d | 2017-09-22 18:49:02 +0300 | [diff] [blame] | 99 | #define PCIE_MEM_SIZE (128 << 20) |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 100 | static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 101 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 102 | static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie) |
| 103 | { |
| 104 | u32 val; |
| 105 | val = readl(pcie->base + PCIE_STAT_OFF); |
| 106 | return !(val & PCIE_STAT_LINK_DOWN); |
| 107 | } |
| 108 | |
| 109 | static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno) |
| 110 | { |
| 111 | u32 stat; |
| 112 | |
| 113 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 114 | stat &= ~PCIE_STAT_BUS; |
| 115 | stat |= busno << 8; |
| 116 | writel(stat, pcie->base + PCIE_STAT_OFF); |
| 117 | } |
| 118 | |
| 119 | static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno) |
| 120 | { |
| 121 | u32 stat; |
| 122 | |
| 123 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 124 | stat &= ~PCIE_STAT_DEV; |
| 125 | stat |= devno << 16; |
| 126 | writel(stat, pcie->base + PCIE_STAT_OFF); |
| 127 | } |
| 128 | |
| 129 | static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie) |
| 130 | { |
| 131 | u32 stat; |
| 132 | |
| 133 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 134 | return (stat & PCIE_STAT_BUS) >> 8; |
| 135 | } |
| 136 | |
| 137 | static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie) |
| 138 | { |
| 139 | u32 stat; |
| 140 | |
| 141 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 142 | return (stat & PCIE_STAT_DEV) >> 16; |
| 143 | } |
| 144 | |
| 145 | static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose) |
| 146 | { |
| 147 | return container_of(hose, struct mvebu_pcie, hose); |
| 148 | } |
| 149 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 150 | static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie, |
| 151 | int busno, int dev, int func) |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 152 | { |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 153 | /* On primary bus is only one PCI Bridge */ |
| 154 | if (busno == pcie->first_busno && (dev != 0 || func != 0)) |
| 155 | return false; |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 156 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 157 | /* On secondary bus can be only one PCIe device */ |
| 158 | if (busno == pcie->sec_busno && dev != 0) |
| 159 | return false; |
| 160 | |
| 161 | return true; |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 164 | static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf, |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 165 | uint offset, ulong *valuep, |
| 166 | enum pci_size_t size) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 167 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 168 | struct mvebu_pcie *pcie = dev_get_plat(bus); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 169 | int busno = PCI_BUS(bdf) - dev_seq(bus); |
| 170 | u32 addr, data; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 171 | |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 172 | debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ", |
| 173 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 174 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 175 | if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) { |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame] | 176 | debug("- out of range\n"); |
| 177 | *valuep = pci_get_ff(size); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 181 | /* |
| 182 | * mvebu has different internal registers mapped into PCI config space |
| 183 | * in range 0x10-0x34 for PCI bridge, so do not access PCI config space |
| 184 | * for this range and instead read content from driver virtual cfgcache |
| 185 | */ |
| 186 | if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) { |
| 187 | data = pcie->cfgcache[(offset - 0x10) / 4]; |
| 188 | debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n", |
| 189 | offset, size, data); |
| 190 | *valuep = pci_conv_32_to_size(data, offset, size); |
| 191 | return 0; |
| 192 | } else if (busno == pcie->first_busno && |
| 193 | (offset & ~3) == PCI_ROM_ADDRESS1) { |
| 194 | /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */ |
| 195 | offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * PCI bridge is device 0 at primary bus but mvebu has it mapped on |
| 200 | * secondary bus with device number 1. |
| 201 | */ |
| 202 | if (busno == pcie->first_busno) |
| 203 | addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset); |
| 204 | else |
| 205 | addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset); |
| 206 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 207 | /* write address */ |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 208 | writel(addr, pcie->base + PCIE_CONF_ADDR_OFF); |
Marek Behún | 9df558d | 2021-02-08 23:01:38 +0100 | [diff] [blame] | 209 | |
| 210 | /* read data */ |
Pali Rohár | 8922013 | 2021-10-22 16:22:09 +0200 | [diff] [blame] | 211 | switch (size) { |
| 212 | case PCI_SIZE_8: |
| 213 | data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3)); |
| 214 | break; |
| 215 | case PCI_SIZE_16: |
| 216 | data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2)); |
| 217 | break; |
| 218 | case PCI_SIZE_32: |
| 219 | data = readl(pcie->base + PCIE_CONF_DATA_OFF); |
| 220 | break; |
| 221 | default: |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 225 | if (busno == pcie->first_busno && |
| 226 | (offset & ~3) == (PCI_HEADER_TYPE & ~3)) { |
| 227 | /* |
| 228 | * Change Header Type of PCI Bridge device to Type 1 |
| 229 | * (0x01, used by PCI Bridges) because mvebu reports |
| 230 | * Type 0 (0x00, used by Upstream and Endpoint devices). |
| 231 | */ |
| 232 | data = pci_conv_size_to_32(data, 0, offset, size); |
| 233 | data &= ~0x007f0000; |
| 234 | data |= PCI_HEADER_TYPE_BRIDGE << 16; |
| 235 | data = pci_conv_32_to_size(data, offset, size); |
| 236 | } |
| 237 | |
Marek Behún | d2ed1e5 | 2021-02-08 23:01:39 +0100 | [diff] [blame] | 238 | debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data); |
Pali Rohár | 8922013 | 2021-10-22 16:22:09 +0200 | [diff] [blame] | 239 | *valuep = data; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 244 | static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 245 | uint offset, ulong value, |
| 246 | enum pci_size_t size) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 247 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 248 | struct mvebu_pcie *pcie = dev_get_plat(bus); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 249 | int busno = PCI_BUS(bdf) - dev_seq(bus); |
| 250 | u32 addr, data; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 251 | |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 252 | debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ", |
| 253 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
Marek Behún | d2ed1e5 | 2021-02-08 23:01:39 +0100 | [diff] [blame] | 254 | debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 255 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 256 | if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) { |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame] | 257 | debug("- out of range\n"); |
| 258 | return 0; |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * mvebu has different internal registers mapped into PCI config space |
| 263 | * in range 0x10-0x34 for PCI bridge, so do not access PCI config space |
| 264 | * for this range and instead write content to driver virtual cfgcache |
| 265 | */ |
| 266 | if (busno == pcie->first_busno && offset >= 0x10 && offset < 0x34) { |
| 267 | debug("Writing to cfgcache only\n"); |
| 268 | data = pcie->cfgcache[(offset - 0x10) / 4]; |
| 269 | data = pci_conv_size_to_32(data, value, offset, size); |
| 270 | /* mvebu PCI bridge does not have configurable bars */ |
| 271 | if ((offset & ~3) == PCI_BASE_ADDRESS_0 || |
| 272 | (offset & ~3) == PCI_BASE_ADDRESS_1) |
| 273 | data = 0x0; |
| 274 | pcie->cfgcache[(offset - 0x10) / 4] = data; |
| 275 | /* mvebu has its own way how to set PCI primary bus number */ |
| 276 | if (offset == PCI_PRIMARY_BUS) { |
| 277 | pcie->first_busno = data & 0xff; |
| 278 | debug("Primary bus number was changed to %d\n", |
| 279 | pcie->first_busno); |
| 280 | } |
| 281 | /* mvebu has its own way how to set PCI secondary bus number */ |
| 282 | if (offset == PCI_SECONDARY_BUS || |
| 283 | (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) { |
| 284 | pcie->sec_busno = (data >> 8) & 0xff; |
| 285 | mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno); |
| 286 | debug("Secondary bus number was changed to %d\n", |
| 287 | pcie->sec_busno); |
| 288 | } |
| 289 | return 0; |
| 290 | } else if (busno == pcie->first_busno && |
| 291 | (offset & ~3) == PCI_ROM_ADDRESS1) { |
| 292 | /* mvebu has Expansion ROM Base Address (0x38) at offset 0x30 */ |
| 293 | offset -= PCI_ROM_ADDRESS1 - PCIE_EXP_ROM_BAR_OFF; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 296 | /* |
| 297 | * PCI bridge is device 0 at primary bus but mvebu has it mapped on |
| 298 | * secondary bus with device number 1. |
| 299 | */ |
| 300 | if (busno == pcie->first_busno) |
| 301 | addr = PCIE_CONF_ADDR(pcie->sec_busno, 1, 0, offset); |
| 302 | else |
| 303 | addr = PCIE_CONF_ADDR(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset); |
| 304 | |
Marek Behún | 9df558d | 2021-02-08 23:01:38 +0100 | [diff] [blame] | 305 | /* write address */ |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 306 | writel(addr, pcie->base + PCIE_CONF_ADDR_OFF); |
Marek Behún | 9df558d | 2021-02-08 23:01:38 +0100 | [diff] [blame] | 307 | |
| 308 | /* write data */ |
Pali Rohár | 35bce49 | 2021-10-22 16:22:08 +0200 | [diff] [blame] | 309 | switch (size) { |
| 310 | case PCI_SIZE_8: |
| 311 | writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3)); |
| 312 | break; |
| 313 | case PCI_SIZE_16: |
| 314 | writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2)); |
| 315 | break; |
| 316 | case PCI_SIZE_32: |
| 317 | writel(value, pcie->base + PCIE_CONF_DATA_OFF); |
| 318 | break; |
| 319 | default: |
| 320 | return -EINVAL; |
| 321 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Setup PCIE BARs and Address Decode Wins: |
| 328 | * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks |
| 329 | * WIN[0-3] -> DRAM bank[0-3] |
| 330 | */ |
| 331 | static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie) |
| 332 | { |
| 333 | const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info(); |
| 334 | u32 size; |
| 335 | int i; |
| 336 | |
| 337 | /* First, disable and clear BARs and windows. */ |
| 338 | for (i = 1; i < 3; i++) { |
| 339 | writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i)); |
| 340 | writel(0, pcie->base + PCIE_BAR_LO_OFF(i)); |
| 341 | writel(0, pcie->base + PCIE_BAR_HI_OFF(i)); |
| 342 | } |
| 343 | |
| 344 | for (i = 0; i < 5; i++) { |
| 345 | writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i)); |
| 346 | writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i)); |
| 347 | writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i)); |
| 348 | } |
| 349 | |
| 350 | writel(0, pcie->base + PCIE_WIN5_CTRL_OFF); |
| 351 | writel(0, pcie->base + PCIE_WIN5_BASE_OFF); |
| 352 | writel(0, pcie->base + PCIE_WIN5_REMAP_OFF); |
| 353 | |
| 354 | /* Setup windows for DDR banks. Count total DDR size on the fly. */ |
| 355 | size = 0; |
| 356 | for (i = 0; i < dram->num_cs; i++) { |
| 357 | const struct mbus_dram_window *cs = dram->cs + i; |
| 358 | |
| 359 | writel(cs->base & 0xffff0000, |
| 360 | pcie->base + PCIE_WIN04_BASE_OFF(i)); |
| 361 | writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i)); |
| 362 | writel(((cs->size - 1) & 0xffff0000) | |
| 363 | (cs->mbus_attr << 8) | |
| 364 | (dram->mbus_dram_target_id << 4) | 1, |
| 365 | pcie->base + PCIE_WIN04_CTRL_OFF(i)); |
| 366 | |
| 367 | size += cs->size; |
| 368 | } |
| 369 | |
| 370 | /* Round up 'size' to the nearest power of two. */ |
| 371 | if ((size & (size - 1)) != 0) |
| 372 | size = 1 << fls(size); |
| 373 | |
| 374 | /* Setup BAR[1] to all DRAM banks. */ |
| 375 | writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1)); |
| 376 | writel(0, pcie->base + PCIE_BAR_HI_OFF(1)); |
| 377 | writel(((size - 1) & 0xffff0000) | 0x1, |
| 378 | pcie->base + PCIE_BAR_CTRL_OFF(1)); |
| 379 | } |
| 380 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 381 | static int mvebu_pcie_probe(struct udevice *dev) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 382 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 383 | struct mvebu_pcie *pcie = dev_get_plat(dev); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 384 | struct udevice *ctlr = pci_get_controller(dev); |
| 385 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 386 | u32 reg; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 387 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 388 | debug("%s: PCIe %d.%d - up, base %08x\n", __func__, |
| 389 | pcie->port, pcie->lane, (u32)pcie->base); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 390 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 391 | /* |
| 392 | * Change Class Code of PCI Bridge device to PCI Bridge (0x600400) |
| 393 | * because default value is Memory controller (0x508000) which |
| 394 | * U-Boot cannot recognize as P2P Bridge. |
| 395 | * |
| 396 | * Note that this mvebu PCI Bridge does not have compliant Type 1 |
| 397 | * Configuration Space. Header Type is reported as Type 0 and in |
| 398 | * range 0x10-0x34 it has aliased internal mvebu registers 0x10-0x34 |
| 399 | * (e.g. PCIE_BAR_LO_OFF) and register 0x38 is reserved. |
| 400 | * |
| 401 | * Driver for this range redirects access to virtual cfgcache[] buffer |
| 402 | * which avoids changing internal mvebu registers. And changes Header |
| 403 | * Type response value to Type 1. |
| 404 | */ |
| 405 | reg = readl(pcie->base + PCIE_DEV_REV_OFF); |
| 406 | reg &= ~0xffffff00; |
| 407 | reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8; |
| 408 | writel(reg, pcie->base + PCIE_DEV_REV_OFF); |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 409 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 410 | /* |
| 411 | * mvebu uses local bus number and local device number to determinate |
| 412 | * type of config request. Type 0 is used if target bus number equals |
| 413 | * local bus number and target device number differs from local device |
| 414 | * number. Type 1 is used if target bus number differs from local bus |
| 415 | * number. And when target bus number equals local bus number and |
| 416 | * target device equals local device number then request is routed to |
| 417 | * PCI Bridge which represent local PCIe Root Port. |
| 418 | * |
| 419 | * It means that PCI primary and secondary buses shares one bus number |
| 420 | * which is configured via local bus number. Determination if config |
| 421 | * request should go to primary or secondary bus is done based on local |
| 422 | * device number. |
| 423 | * |
| 424 | * PCIe is point-to-point bus, so at secondary bus is always exactly one |
| 425 | * device with number 0. So set local device number to 1, it would not |
| 426 | * conflict with any device on secondary bus number and will ensure that |
| 427 | * accessing secondary bus and all buses behind secondary would work |
| 428 | * automatically and correctly. Therefore this configuration of local |
| 429 | * device number implies that setting of local bus number configures |
| 430 | * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will |
| 431 | * later configure it via config write requests to the correct value. |
| 432 | * mvebu_pcie_write_config() catches config write requests which tries |
| 433 | * to change primary/secondary bus number and correctly updates local |
| 434 | * bus number based on new secondary bus number. |
| 435 | * |
| 436 | * With this configuration is PCI Bridge available at secondary bus as |
| 437 | * device number 1. But it must be available at primary bus as device |
| 438 | * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config() |
| 439 | * functions rewrite address to the real one when accessing primary bus. |
| 440 | */ |
| 441 | mvebu_pcie_set_local_bus_nr(pcie, 0); |
| 442 | mvebu_pcie_set_local_dev_nr(pcie, 1); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 443 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 444 | pcie->mem.start = (u32)mvebu_pcie_membase; |
| 445 | pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1; |
| 446 | mvebu_pcie_membase += PCIE_MEM_SIZE; |
| 447 | |
| 448 | if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr, |
| 449 | (phys_addr_t)pcie->mem.start, |
| 450 | PCIE_MEM_SIZE)) { |
| 451 | printf("PCIe unable to add mbus window for mem at %08x+%08x\n", |
| 452 | (u32)pcie->mem.start, PCIE_MEM_SIZE); |
| 453 | } |
| 454 | |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 455 | pcie->io.start = (u32)mvebu_pcie_iobase; |
| 456 | pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1; |
| 457 | mvebu_pcie_iobase += MBUS_PCI_IO_SIZE; |
| 458 | |
| 459 | if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr, |
| 460 | (phys_addr_t)pcie->io.start, |
| 461 | MBUS_PCI_IO_SIZE)) { |
| 462 | printf("PCIe unable to add mbus window for IO at %08x+%08x\n", |
| 463 | (u32)pcie->io.start, MBUS_PCI_IO_SIZE); |
| 464 | } |
| 465 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 466 | /* Setup windows and configure host bridge */ |
| 467 | mvebu_pcie_setup_wins(pcie); |
| 468 | |
| 469 | /* Master + slave enable. */ |
| 470 | reg = readl(pcie->base + PCIE_CMD_OFF); |
| 471 | reg |= PCI_COMMAND_MEMORY; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 472 | reg |= PCI_COMMAND_IO; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 473 | reg |= PCI_COMMAND_MASTER; |
| 474 | reg |= BIT(10); /* disable interrupts */ |
| 475 | writel(reg, pcie->base + PCIE_CMD_OFF); |
| 476 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 477 | /* PCI memory space */ |
| 478 | pci_set_region(hose->regions + 0, pcie->mem.start, |
| 479 | pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM); |
| 480 | pci_set_region(hose->regions + 1, |
| 481 | 0, 0, |
| 482 | gd->ram_size, |
| 483 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 484 | pci_set_region(hose->regions + 2, pcie->io.start, |
| 485 | pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO); |
| 486 | hose->region_count = 3; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 487 | |
Marek Behún | 587816d | 2019-08-07 15:01:56 +0200 | [diff] [blame] | 488 | /* Set BAR0 to internal registers */ |
| 489 | writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0)); |
| 490 | writel(0, pcie->base + PCIE_BAR_HI_OFF(0)); |
| 491 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame^] | 492 | /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */ |
| 493 | pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] = |
| 494 | PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8); |
| 495 | pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] = |
| 496 | PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16); |
| 497 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie) |
| 502 | { |
| 503 | const u32 *addr; |
| 504 | int len; |
| 505 | |
| 506 | addr = ofnode_get_property(node, "assigned-addresses", &len); |
| 507 | if (!addr) { |
| 508 | pr_err("property \"assigned-addresses\" not found"); |
| 509 | return -FDT_ERR_NOTFOUND; |
| 510 | } |
| 511 | |
| 512 | pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03) |
| 518 | #define DT_TYPE_IO 0x1 |
| 519 | #define DT_TYPE_MEM32 0x2 |
| 520 | #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF) |
| 521 | #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF) |
| 522 | |
| 523 | static int mvebu_get_tgt_attr(ofnode node, int devfn, |
| 524 | unsigned long type, |
| 525 | unsigned int *tgt, |
| 526 | unsigned int *attr) |
| 527 | { |
| 528 | const int na = 3, ns = 2; |
| 529 | const __be32 *range; |
| 530 | int rlen, nranges, rangesz, pna, i; |
| 531 | |
| 532 | *tgt = -1; |
| 533 | *attr = -1; |
| 534 | |
| 535 | range = ofnode_get_property(node, "ranges", &rlen); |
| 536 | if (!range) |
| 537 | return -EINVAL; |
| 538 | |
Stefan Roese | 24e23bd | 2019-02-11 07:53:34 +0100 | [diff] [blame] | 539 | /* |
| 540 | * Linux uses of_n_addr_cells() to get the number of address cells |
| 541 | * here. Currently this function is only available in U-Boot when |
| 542 | * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in |
| 543 | * general, lets't hardcode the "pna" value in the U-Boot code. |
| 544 | */ |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 545 | pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */ |
| 546 | rangesz = pna + na + ns; |
| 547 | nranges = rlen / sizeof(__be32) / rangesz; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 548 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 549 | for (i = 0; i < nranges; i++, range += rangesz) { |
| 550 | u32 flags = of_read_number(range, 1); |
| 551 | u32 slot = of_read_number(range + 1, 1); |
| 552 | u64 cpuaddr = of_read_number(range + na, pna); |
| 553 | unsigned long rtype; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 554 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 555 | if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO) |
| 556 | rtype = IORESOURCE_IO; |
| 557 | else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32) |
| 558 | rtype = IORESOURCE_MEM; |
| 559 | else |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 560 | continue; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 561 | |
| 562 | /* |
| 563 | * The Linux code used PCI_SLOT() here, which expects devfn |
| 564 | * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(), |
| 565 | * only expects devfn in 15..8, where its saved in this driver. |
| 566 | */ |
| 567 | if (slot == PCI_DEV(devfn) && type == rtype) { |
| 568 | *tgt = DT_CPUADDR_TO_TARGET(cpuaddr); |
| 569 | *attr = DT_CPUADDR_TO_ATTR(cpuaddr); |
| 570 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 571 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 572 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 573 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 574 | return -ENOENT; |
| 575 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 576 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 577 | static int mvebu_pcie_of_to_plat(struct udevice *dev) |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 578 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 579 | struct mvebu_pcie *pcie = dev_get_plat(dev); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 580 | int ret = 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 581 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 582 | /* Get port number, lane number and memory target / attr */ |
| 583 | if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port", |
| 584 | &pcie->port)) { |
| 585 | ret = -ENODEV; |
| 586 | goto err; |
| 587 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 588 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 589 | if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane)) |
| 590 | pcie->lane = 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 591 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 592 | sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 593 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 594 | /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */ |
| 595 | pcie->devfn = pci_get_devfn(dev); |
| 596 | if (pcie->devfn < 0) { |
| 597 | ret = -ENODEV; |
| 598 | goto err; |
| 599 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 600 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 601 | ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn, |
| 602 | IORESOURCE_MEM, |
| 603 | &pcie->mem_target, &pcie->mem_attr); |
| 604 | if (ret < 0) { |
| 605 | printf("%s: cannot get tgt/attr for mem window\n", pcie->name); |
| 606 | goto err; |
| 607 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 608 | |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 609 | ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn, |
| 610 | IORESOURCE_IO, |
| 611 | &pcie->io_target, &pcie->io_attr); |
| 612 | if (ret < 0) { |
| 613 | printf("%s: cannot get tgt/attr for IO window\n", pcie->name); |
| 614 | goto err; |
| 615 | } |
| 616 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 617 | /* Parse PCIe controller register base from DT */ |
| 618 | ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie); |
| 619 | if (ret < 0) |
| 620 | goto err; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 621 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 622 | /* Check link and skip ports that have no link */ |
| 623 | if (!mvebu_pcie_link_up(pcie)) { |
| 624 | debug("%s: %s - down\n", __func__, pcie->name); |
| 625 | ret = -ENODEV; |
| 626 | goto err; |
| 627 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 628 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 629 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 630 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 631 | err: |
| 632 | return ret; |
| 633 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 634 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 635 | static const struct dm_pci_ops mvebu_pcie_ops = { |
| 636 | .read_config = mvebu_pcie_read_config, |
| 637 | .write_config = mvebu_pcie_write_config, |
| 638 | }; |
Phil Sutter | 68010aa | 2015-12-25 14:41:20 +0100 | [diff] [blame] | 639 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 640 | static struct driver pcie_mvebu_drv = { |
| 641 | .name = "pcie_mvebu", |
| 642 | .id = UCLASS_PCI, |
| 643 | .ops = &mvebu_pcie_ops, |
| 644 | .probe = mvebu_pcie_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 645 | .of_to_plat = mvebu_pcie_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 646 | .plat_auto = sizeof(struct mvebu_pcie), |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 647 | }; |
| 648 | |
| 649 | /* |
| 650 | * Use a MISC device to bind the n instances (child nodes) of the |
| 651 | * PCIe base controller in UCLASS_PCI. |
| 652 | */ |
| 653 | static int mvebu_pcie_bind(struct udevice *parent) |
| 654 | { |
| 655 | struct mvebu_pcie *pcie; |
| 656 | struct uclass_driver *drv; |
| 657 | struct udevice *dev; |
| 658 | ofnode subnode; |
| 659 | |
| 660 | /* Lookup eth driver */ |
| 661 | drv = lists_uclass_lookup(UCLASS_PCI); |
| 662 | if (!drv) { |
| 663 | puts("Cannot find PCI driver\n"); |
| 664 | return -ENOENT; |
| 665 | } |
| 666 | |
| 667 | ofnode_for_each_subnode(subnode, dev_ofnode(parent)) { |
| 668 | if (!ofnode_is_available(subnode)) |
| 669 | continue; |
| 670 | |
| 671 | pcie = calloc(1, sizeof(*pcie)); |
| 672 | if (!pcie) |
| 673 | return -ENOMEM; |
| 674 | |
| 675 | /* Create child device UCLASS_PCI and bind it */ |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 676 | device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode, |
| 677 | &dev); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 678 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 679 | |
| 680 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 681 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 682 | |
| 683 | static const struct udevice_id mvebu_pcie_ids[] = { |
| 684 | { .compatible = "marvell,armada-xp-pcie" }, |
| 685 | { .compatible = "marvell,armada-370-pcie" }, |
| 686 | { } |
| 687 | }; |
| 688 | |
| 689 | U_BOOT_DRIVER(pcie_mvebu_base) = { |
| 690 | .name = "pcie_mvebu_base", |
| 691 | .id = UCLASS_MISC, |
| 692 | .of_match = mvebu_pcie_ids, |
| 693 | .bind = mvebu_pcie_bind, |
| 694 | }; |