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> |
Pali Rohár | 028ac88 | 2021-12-16 12:04:06 +0100 | [diff] [blame] | 10 | * Pali Rohár <pali@kernel.org> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.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> |
Pali Rohár | 5fc93e2 | 2021-12-21 12:20:19 +0100 | [diff] [blame] | 21 | #include <reset.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 22 | #include <asm/io.h> |
| 23 | #include <asm/arch/cpu.h> |
| 24 | #include <asm/arch/soc.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 25 | #include <linux/bitops.h> |
Pali Rohár | 91d02fb | 2021-12-21 12:20:17 +0100 | [diff] [blame] | 26 | #include <linux/delay.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 27 | #include <linux/errno.h> |
| 28 | #include <linux/ioport.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 29 | #include <linux/mbus.h> |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 30 | #include <linux/sizes.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 31 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 32 | /* PCIe unit register offsets */ |
| 33 | #define SELECT(x, n) ((x >> n) & 1UL) |
| 34 | |
| 35 | #define PCIE_DEV_ID_OFF 0x0000 |
| 36 | #define PCIE_CMD_OFF 0x0004 |
| 37 | #define PCIE_DEV_REV_OFF 0x0008 |
| 38 | #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3)) |
| 39 | #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3)) |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 40 | #define PCIE_EXP_ROM_BAR_OFF 0x0030 |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 41 | #define PCIE_CAPAB_OFF 0x0060 |
| 42 | #define PCIE_CTRL_STAT_OFF 0x0068 |
| 43 | #define PCIE_HEADER_LOG_4_OFF 0x0128 |
| 44 | #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4)) |
| 45 | #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4)) |
| 46 | #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4)) |
| 47 | #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4)) |
| 48 | #define PCIE_WIN5_CTRL_OFF 0x1880 |
| 49 | #define PCIE_WIN5_BASE_OFF 0x1884 |
| 50 | #define PCIE_WIN5_REMAP_OFF 0x188c |
| 51 | #define PCIE_CONF_ADDR_OFF 0x18f8 |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 52 | #define PCIE_CONF_DATA_OFF 0x18fc |
| 53 | #define PCIE_MASK_OFF 0x1910 |
| 54 | #define PCIE_MASK_ENABLE_INTS (0xf << 24) |
| 55 | #define PCIE_CTRL_OFF 0x1a00 |
| 56 | #define PCIE_CTRL_X1_MODE BIT(0) |
Pali Rohár | 6f3e57a | 2021-10-22 16:22:14 +0200 | [diff] [blame] | 57 | #define PCIE_CTRL_RC_MODE BIT(1) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 58 | #define PCIE_STAT_OFF 0x1a04 |
| 59 | #define PCIE_STAT_BUS (0xff << 8) |
| 60 | #define PCIE_STAT_DEV (0x1f << 16) |
| 61 | #define PCIE_STAT_LINK_DOWN BIT(0) |
| 62 | #define PCIE_DEBUG_CTRL 0x1a60 |
| 63 | #define PCIE_DEBUG_SOFT_RESET BIT(20) |
| 64 | |
Pali Rohár | 91d02fb | 2021-12-21 12:20:17 +0100 | [diff] [blame] | 65 | #define LINK_WAIT_RETRIES 100 |
| 66 | #define LINK_WAIT_TIMEOUT 1000 |
| 67 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 68 | struct mvebu_pcie { |
| 69 | struct pci_controller hose; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 70 | void __iomem *base; |
| 71 | void __iomem *membase; |
| 72 | struct resource mem; |
| 73 | void __iomem *iobase; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 74 | struct resource io; |
Pali Rohár | bb26c4a | 2021-12-21 12:20:15 +0100 | [diff] [blame] | 75 | u32 intregs; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 76 | u32 port; |
| 77 | u32 lane; |
Pali Rohár | 5fc93e2 | 2021-12-21 12:20:19 +0100 | [diff] [blame] | 78 | bool is_x4; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 79 | int devfn; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 80 | u32 lane_mask; |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 81 | int first_busno; |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 82 | int sec_busno; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 83 | char name[16]; |
| 84 | unsigned int mem_target; |
| 85 | unsigned int mem_attr; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 86 | unsigned int io_target; |
| 87 | unsigned int io_attr; |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 88 | u32 cfgcache[(0x3c - 0x10) / 4]; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 91 | static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie) |
| 92 | { |
| 93 | u32 val; |
| 94 | val = readl(pcie->base + PCIE_STAT_OFF); |
| 95 | return !(val & PCIE_STAT_LINK_DOWN); |
| 96 | } |
| 97 | |
Pali Rohár | 91d02fb | 2021-12-21 12:20:17 +0100 | [diff] [blame] | 98 | static void mvebu_pcie_wait_for_link(struct mvebu_pcie *pcie) |
| 99 | { |
| 100 | int retries; |
| 101 | |
| 102 | /* check if the link is up or not */ |
| 103 | for (retries = 0; retries < LINK_WAIT_RETRIES; retries++) { |
| 104 | if (mvebu_pcie_link_up(pcie)) { |
| 105 | printf("%s: Link up\n", pcie->name); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | udelay(LINK_WAIT_TIMEOUT); |
| 110 | } |
| 111 | |
| 112 | printf("%s: Link down\n", pcie->name); |
| 113 | } |
| 114 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 115 | static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno) |
| 116 | { |
| 117 | u32 stat; |
| 118 | |
| 119 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 120 | stat &= ~PCIE_STAT_BUS; |
| 121 | stat |= busno << 8; |
| 122 | writel(stat, pcie->base + PCIE_STAT_OFF); |
| 123 | } |
| 124 | |
| 125 | static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno) |
| 126 | { |
| 127 | u32 stat; |
| 128 | |
| 129 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 130 | stat &= ~PCIE_STAT_DEV; |
| 131 | stat |= devno << 16; |
| 132 | writel(stat, pcie->base + PCIE_STAT_OFF); |
| 133 | } |
| 134 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 135 | static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose) |
| 136 | { |
| 137 | return container_of(hose, struct mvebu_pcie, hose); |
| 138 | } |
| 139 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 140 | static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie, |
| 141 | int busno, int dev, int func) |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 142 | { |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 143 | /* On primary bus is only one PCI Bridge */ |
| 144 | if (busno == pcie->first_busno && (dev != 0 || func != 0)) |
| 145 | return false; |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 146 | |
Pali Rohár | 2f9b519 | 2021-10-22 16:22:12 +0200 | [diff] [blame] | 147 | /* Access to other buses is possible when link is up */ |
| 148 | if (busno != pcie->first_busno && !mvebu_pcie_link_up(pcie)) |
| 149 | return false; |
| 150 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 151 | /* On secondary bus can be only one PCIe device */ |
| 152 | if (busno == pcie->sec_busno && dev != 0) |
| 153 | return false; |
| 154 | |
| 155 | return true; |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 158 | 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] | 159 | uint offset, ulong *valuep, |
| 160 | enum pci_size_t size) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 161 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 162 | struct mvebu_pcie *pcie = dev_get_plat(bus); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 163 | int busno = PCI_BUS(bdf) - dev_seq(bus); |
| 164 | u32 addr, data; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 165 | |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 166 | debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ", |
| 167 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 168 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 169 | 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] | 170 | debug("- out of range\n"); |
| 171 | *valuep = pci_get_ff(size); |
| 172 | return 0; |
| 173 | } |
| 174 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 175 | /* |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 176 | * The configuration space of the PCI Bridge on primary (first) bus is |
| 177 | * of Type 0 but the BAR registers (including ROM BAR) don't have the |
| 178 | * same meaning as in the PCIe specification. Therefore do not access |
| 179 | * BAR registers and non-common registers (those which have different |
| 180 | * meaning for Type 0 and Type 1 config space) of the PCI Bridge and |
| 181 | * instead read their content from driver virtual cfgcache[]. |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 182 | */ |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 183 | if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) || |
| 184 | (offset >= 0x38 && offset < 0x3c))) { |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 185 | data = pcie->cfgcache[(offset - 0x10) / 4]; |
| 186 | debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n", |
| 187 | offset, size, data); |
| 188 | *valuep = pci_conv_32_to_size(data, offset, size); |
| 189 | return 0; |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | * PCI bridge is device 0 at primary bus but mvebu has it mapped on |
| 194 | * secondary bus with device number 1. |
| 195 | */ |
| 196 | if (busno == pcie->first_busno) |
Pali Rohár | 43a9fc7 | 2021-11-26 11:42:45 +0100 | [diff] [blame] | 197 | addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 198 | else |
Pali Rohár | 43a9fc7 | 2021-11-26 11:42:45 +0100 | [diff] [blame] | 199 | addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 200 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 201 | /* write address */ |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 202 | writel(addr, pcie->base + PCIE_CONF_ADDR_OFF); |
Marek Behún | 9df558d | 2021-02-08 23:01:38 +0100 | [diff] [blame] | 203 | |
| 204 | /* read data */ |
Pali Rohár | 8922013 | 2021-10-22 16:22:09 +0200 | [diff] [blame] | 205 | switch (size) { |
| 206 | case PCI_SIZE_8: |
| 207 | data = readb(pcie->base + PCIE_CONF_DATA_OFF + (offset & 3)); |
| 208 | break; |
| 209 | case PCI_SIZE_16: |
| 210 | data = readw(pcie->base + PCIE_CONF_DATA_OFF + (offset & 2)); |
| 211 | break; |
| 212 | case PCI_SIZE_32: |
| 213 | data = readl(pcie->base + PCIE_CONF_DATA_OFF); |
| 214 | break; |
| 215 | default: |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 219 | if (busno == pcie->first_busno && |
| 220 | (offset & ~3) == (PCI_HEADER_TYPE & ~3)) { |
| 221 | /* |
| 222 | * Change Header Type of PCI Bridge device to Type 1 |
| 223 | * (0x01, used by PCI Bridges) because mvebu reports |
| 224 | * Type 0 (0x00, used by Upstream and Endpoint devices). |
| 225 | */ |
| 226 | data = pci_conv_size_to_32(data, 0, offset, size); |
| 227 | data &= ~0x007f0000; |
| 228 | data |= PCI_HEADER_TYPE_BRIDGE << 16; |
| 229 | data = pci_conv_32_to_size(data, offset, size); |
| 230 | } |
| 231 | |
Marek Behún | d2ed1e5 | 2021-02-08 23:01:39 +0100 | [diff] [blame] | 232 | 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] | 233 | *valuep = data; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 238 | static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 239 | uint offset, ulong value, |
| 240 | enum pci_size_t size) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 241 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 242 | struct mvebu_pcie *pcie = dev_get_plat(bus); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 243 | int busno = PCI_BUS(bdf) - dev_seq(bus); |
| 244 | u32 addr, data; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 245 | |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 246 | debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ", |
| 247 | PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
Marek Behún | d2ed1e5 | 2021-02-08 23:01:39 +0100 | [diff] [blame] | 248 | 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] | 249 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 250 | 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] | 251 | debug("- out of range\n"); |
| 252 | return 0; |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 256 | * As explained in mvebu_pcie_read_config(), PCI Bridge Type 1 specific |
| 257 | * config registers are not available, so we write their content only |
| 258 | * into driver virtual cfgcache[]. |
| 259 | * And as explained in mvebu_pcie_probe(), mvebu has its own specific |
| 260 | * way for configuring primary and secondary bus numbers. |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 261 | */ |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 262 | if (busno == pcie->first_busno && ((offset >= 0x10 && offset < 0x34) || |
| 263 | (offset >= 0x38 && offset < 0x3c))) { |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 264 | debug("Writing to cfgcache only\n"); |
| 265 | data = pcie->cfgcache[(offset - 0x10) / 4]; |
| 266 | data = pci_conv_size_to_32(data, value, offset, size); |
| 267 | /* mvebu PCI bridge does not have configurable bars */ |
| 268 | if ((offset & ~3) == PCI_BASE_ADDRESS_0 || |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 269 | (offset & ~3) == PCI_BASE_ADDRESS_1 || |
| 270 | (offset & ~3) == PCI_ROM_ADDRESS1) |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 271 | data = 0x0; |
| 272 | pcie->cfgcache[(offset - 0x10) / 4] = data; |
| 273 | /* mvebu has its own way how to set PCI primary bus number */ |
| 274 | if (offset == PCI_PRIMARY_BUS) { |
| 275 | pcie->first_busno = data & 0xff; |
| 276 | debug("Primary bus number was changed to %d\n", |
| 277 | pcie->first_busno); |
| 278 | } |
| 279 | /* mvebu has its own way how to set PCI secondary bus number */ |
| 280 | if (offset == PCI_SECONDARY_BUS || |
| 281 | (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) { |
| 282 | pcie->sec_busno = (data >> 8) & 0xff; |
| 283 | mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno); |
| 284 | debug("Secondary bus number was changed to %d\n", |
| 285 | pcie->sec_busno); |
| 286 | } |
| 287 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 290 | /* |
| 291 | * PCI bridge is device 0 at primary bus but mvebu has it mapped on |
| 292 | * secondary bus with device number 1. |
| 293 | */ |
| 294 | if (busno == pcie->first_busno) |
Pali Rohár | 43a9fc7 | 2021-11-26 11:42:45 +0100 | [diff] [blame] | 295 | addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 296 | else |
Pali Rohár | 43a9fc7 | 2021-11-26 11:42:45 +0100 | [diff] [blame] | 297 | addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset); |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 298 | |
Marek Behún | 9df558d | 2021-02-08 23:01:38 +0100 | [diff] [blame] | 299 | /* write address */ |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 300 | writel(addr, pcie->base + PCIE_CONF_ADDR_OFF); |
Marek Behún | 9df558d | 2021-02-08 23:01:38 +0100 | [diff] [blame] | 301 | |
| 302 | /* write data */ |
Pali Rohár | 35bce49 | 2021-10-22 16:22:08 +0200 | [diff] [blame] | 303 | switch (size) { |
| 304 | case PCI_SIZE_8: |
| 305 | writeb(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 3)); |
| 306 | break; |
| 307 | case PCI_SIZE_16: |
| 308 | writew(value, pcie->base + PCIE_CONF_DATA_OFF + (offset & 2)); |
| 309 | break; |
| 310 | case PCI_SIZE_32: |
| 311 | writel(value, pcie->base + PCIE_CONF_DATA_OFF); |
| 312 | break; |
| 313 | default: |
| 314 | return -EINVAL; |
| 315 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Setup PCIE BARs and Address Decode Wins: |
Pali Rohár | 495f313 | 2021-11-11 16:35:42 +0100 | [diff] [blame] | 322 | * BAR[0] -> internal registers |
| 323 | * BAR[1] -> covers all DRAM banks |
| 324 | * BAR[2] -> disabled |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 325 | * WIN[0-3] -> DRAM bank[0-3] |
| 326 | */ |
| 327 | static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie) |
| 328 | { |
| 329 | const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info(); |
| 330 | u32 size; |
| 331 | int i; |
| 332 | |
| 333 | /* First, disable and clear BARs and windows. */ |
| 334 | for (i = 1; i < 3; i++) { |
| 335 | writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i)); |
| 336 | writel(0, pcie->base + PCIE_BAR_LO_OFF(i)); |
| 337 | writel(0, pcie->base + PCIE_BAR_HI_OFF(i)); |
| 338 | } |
| 339 | |
| 340 | for (i = 0; i < 5; i++) { |
| 341 | writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i)); |
| 342 | writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i)); |
| 343 | writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i)); |
| 344 | } |
| 345 | |
| 346 | writel(0, pcie->base + PCIE_WIN5_CTRL_OFF); |
| 347 | writel(0, pcie->base + PCIE_WIN5_BASE_OFF); |
| 348 | writel(0, pcie->base + PCIE_WIN5_REMAP_OFF); |
| 349 | |
| 350 | /* Setup windows for DDR banks. Count total DDR size on the fly. */ |
| 351 | size = 0; |
| 352 | for (i = 0; i < dram->num_cs; i++) { |
| 353 | const struct mbus_dram_window *cs = dram->cs + i; |
| 354 | |
| 355 | writel(cs->base & 0xffff0000, |
| 356 | pcie->base + PCIE_WIN04_BASE_OFF(i)); |
| 357 | writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i)); |
| 358 | writel(((cs->size - 1) & 0xffff0000) | |
| 359 | (cs->mbus_attr << 8) | |
| 360 | (dram->mbus_dram_target_id << 4) | 1, |
| 361 | pcie->base + PCIE_WIN04_CTRL_OFF(i)); |
| 362 | |
| 363 | size += cs->size; |
| 364 | } |
| 365 | |
| 366 | /* Round up 'size' to the nearest power of two. */ |
| 367 | if ((size & (size - 1)) != 0) |
| 368 | size = 1 << fls(size); |
| 369 | |
| 370 | /* Setup BAR[1] to all DRAM banks. */ |
| 371 | writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1)); |
| 372 | writel(0, pcie->base + PCIE_BAR_HI_OFF(1)); |
| 373 | writel(((size - 1) & 0xffff0000) | 0x1, |
| 374 | pcie->base + PCIE_BAR_CTRL_OFF(1)); |
Pali Rohár | 495f313 | 2021-11-11 16:35:42 +0100 | [diff] [blame] | 375 | |
| 376 | /* Setup BAR[0] to internal registers. */ |
Pali Rohár | bb26c4a | 2021-12-21 12:20:15 +0100 | [diff] [blame] | 377 | writel(pcie->intregs, pcie->base + PCIE_BAR_LO_OFF(0)); |
Pali Rohár | 495f313 | 2021-11-11 16:35:42 +0100 | [diff] [blame] | 378 | writel(0, pcie->base + PCIE_BAR_HI_OFF(0)); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 381 | /* Only enable PCIe link, do not setup it */ |
| 382 | static int mvebu_pcie_enable_link(struct mvebu_pcie *pcie, ofnode node) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 383 | { |
Pali Rohár | 5fc93e2 | 2021-12-21 12:20:19 +0100 | [diff] [blame] | 384 | struct reset_ctl rst; |
| 385 | int ret; |
| 386 | |
| 387 | ret = reset_get_by_index_nodev(node, 0, &rst); |
| 388 | if (ret == -ENOENT) { |
| 389 | return 0; |
| 390 | } else if (ret < 0) { |
| 391 | printf("%s: cannot get reset controller: %d\n", pcie->name, ret); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | ret = reset_request(&rst); |
| 396 | if (ret) { |
| 397 | printf("%s: cannot request reset controller: %d\n", pcie->name, ret); |
| 398 | return ret; |
| 399 | } |
| 400 | |
| 401 | ret = reset_deassert(&rst); |
| 402 | reset_free(&rst); |
| 403 | if (ret) { |
| 404 | printf("%s: cannot enable PCIe port: %d\n", pcie->name, ret); |
| 405 | return ret; |
| 406 | } |
| 407 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | /* Setup PCIe link but do not enable it */ |
| 412 | static void mvebu_pcie_setup_link(struct mvebu_pcie *pcie) |
| 413 | { |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 414 | u32 reg; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 415 | |
Pali Rohár | 6f3e57a | 2021-10-22 16:22:14 +0200 | [diff] [blame] | 416 | /* Setup PCIe controller to Root Complex mode */ |
| 417 | reg = readl(pcie->base + PCIE_CTRL_OFF); |
| 418 | reg |= PCIE_CTRL_RC_MODE; |
| 419 | writel(reg, pcie->base + PCIE_CTRL_OFF); |
Pali Rohár | 5fc93e2 | 2021-12-21 12:20:19 +0100 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * Set Maximum Link Width to X1 or X4 in Root Port's PCIe Link |
| 423 | * Capability register. This register is defined by PCIe specification |
| 424 | * as read-only but this mvebu controller has it as read-write and must |
| 425 | * be set to number of SerDes PCIe lanes (1 or 4). If this register is |
| 426 | * not set correctly then link with endpoint card is not established. |
| 427 | */ |
| 428 | reg = readl(pcie->base + PCIE_CAPAB_OFF + PCI_EXP_LNKCAP); |
| 429 | reg &= ~PCI_EXP_LNKCAP_MLW; |
| 430 | reg |= (pcie->is_x4 ? 4 : 1) << 4; |
| 431 | writel(reg, pcie->base + PCIE_CAPAB_OFF + PCI_EXP_LNKCAP); |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static int mvebu_pcie_probe(struct udevice *dev) |
| 435 | { |
| 436 | struct mvebu_pcie *pcie = dev_get_plat(dev); |
| 437 | struct udevice *ctlr = pci_get_controller(dev); |
| 438 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 439 | u32 reg; |
Pali Rohár | 6f3e57a | 2021-10-22 16:22:14 +0200 | [diff] [blame] | 440 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 441 | /* |
| 442 | * Change Class Code of PCI Bridge device to PCI Bridge (0x600400) |
| 443 | * because default value is Memory controller (0x508000) which |
| 444 | * U-Boot cannot recognize as P2P Bridge. |
| 445 | * |
| 446 | * Note that this mvebu PCI Bridge does not have compliant Type 1 |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 447 | * Configuration Space. Header Type is reported as Type 0 and it |
| 448 | * has format of Type 0 config space. |
| 449 | * |
| 450 | * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34) |
| 451 | * have the same format in Marvell's specification as in PCIe |
| 452 | * specification, but their meaning is totally different and they do |
| 453 | * different things: they are aliased into internal mvebu registers |
| 454 | * (e.g. PCIE_BAR_LO_OFF) and these should not be changed or |
| 455 | * reconfigured by pci device drivers. |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 456 | * |
Pali Rohár | 3166722 | 2021-11-11 16:35:45 +0100 | [diff] [blame] | 457 | * So our driver converts Type 0 config space to Type 1 and reports |
| 458 | * Header Type as Type 1. Access to BAR registers and to non-existent |
| 459 | * Type 1 registers is redirected to the virtual cfgcache[] buffer, |
| 460 | * which avoids changing unrelated registers. |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 461 | */ |
| 462 | reg = readl(pcie->base + PCIE_DEV_REV_OFF); |
| 463 | reg &= ~0xffffff00; |
| 464 | reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8; |
| 465 | writel(reg, pcie->base + PCIE_DEV_REV_OFF); |
Marek Behún | 8903673 | 2021-02-08 23:01:40 +0100 | [diff] [blame] | 466 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 467 | /* |
| 468 | * mvebu uses local bus number and local device number to determinate |
| 469 | * type of config request. Type 0 is used if target bus number equals |
| 470 | * local bus number and target device number differs from local device |
| 471 | * number. Type 1 is used if target bus number differs from local bus |
| 472 | * number. And when target bus number equals local bus number and |
| 473 | * target device equals local device number then request is routed to |
| 474 | * PCI Bridge which represent local PCIe Root Port. |
| 475 | * |
| 476 | * It means that PCI primary and secondary buses shares one bus number |
| 477 | * which is configured via local bus number. Determination if config |
| 478 | * request should go to primary or secondary bus is done based on local |
| 479 | * device number. |
| 480 | * |
| 481 | * PCIe is point-to-point bus, so at secondary bus is always exactly one |
| 482 | * device with number 0. So set local device number to 1, it would not |
| 483 | * conflict with any device on secondary bus number and will ensure that |
| 484 | * accessing secondary bus and all buses behind secondary would work |
| 485 | * automatically and correctly. Therefore this configuration of local |
| 486 | * device number implies that setting of local bus number configures |
| 487 | * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will |
| 488 | * later configure it via config write requests to the correct value. |
| 489 | * mvebu_pcie_write_config() catches config write requests which tries |
| 490 | * to change primary/secondary bus number and correctly updates local |
| 491 | * bus number based on new secondary bus number. |
| 492 | * |
| 493 | * With this configuration is PCI Bridge available at secondary bus as |
| 494 | * device number 1. But it must be available at primary bus as device |
| 495 | * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config() |
| 496 | * functions rewrite address to the real one when accessing primary bus. |
| 497 | */ |
| 498 | mvebu_pcie_set_local_bus_nr(pcie, 0); |
| 499 | mvebu_pcie_set_local_dev_nr(pcie, 1); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 500 | |
Pali Rohár | ed9bcb9 | 2022-01-13 14:28:04 +0100 | [diff] [blame] | 501 | /* |
| 502 | * Kirkwood arch code already maps mbus windows for PCIe IO and MEM. |
| 503 | * So skip calling mvebu_mbus_add_window_by_id() function as it would |
| 504 | * fail on error "conflicts with another window" which means conflict |
| 505 | * with existing PCIe window mappings. |
| 506 | */ |
| 507 | #ifndef CONFIG_ARCH_KIRKWOOD |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 508 | if (resource_size(&pcie->mem) && |
| 509 | mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr, |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 510 | (phys_addr_t)pcie->mem.start, |
Pali Rohár | 2aeb942 | 2021-11-11 16:35:43 +0100 | [diff] [blame] | 511 | resource_size(&pcie->mem))) { |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 512 | printf("%s: unable to add mbus window for mem at %08x+%08x\n", |
| 513 | pcie->name, |
Pali Rohár | 2aeb942 | 2021-11-11 16:35:43 +0100 | [diff] [blame] | 514 | (u32)pcie->mem.start, (unsigned)resource_size(&pcie->mem)); |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 515 | pcie->mem.start = 0; |
| 516 | pcie->mem.end = -1; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 517 | } |
| 518 | |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 519 | if (resource_size(&pcie->io) && |
| 520 | mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr, |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 521 | (phys_addr_t)pcie->io.start, |
Pali Rohár | 2aeb942 | 2021-11-11 16:35:43 +0100 | [diff] [blame] | 522 | resource_size(&pcie->io))) { |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 523 | printf("%s: unable to add mbus window for IO at %08x+%08x\n", |
| 524 | pcie->name, |
Pali Rohár | 2aeb942 | 2021-11-11 16:35:43 +0100 | [diff] [blame] | 525 | (u32)pcie->io.start, (unsigned)resource_size(&pcie->io)); |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 526 | pcie->io.start = 0; |
| 527 | pcie->io.end = -1; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 528 | } |
Pali Rohár | ed9bcb9 | 2022-01-13 14:28:04 +0100 | [diff] [blame] | 529 | #endif |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 530 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 531 | /* Setup windows and configure host bridge */ |
| 532 | mvebu_pcie_setup_wins(pcie); |
| 533 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 534 | /* PCI memory space */ |
| 535 | pci_set_region(hose->regions + 0, pcie->mem.start, |
Pali Rohár | 2aeb942 | 2021-11-11 16:35:43 +0100 | [diff] [blame] | 536 | pcie->mem.start, resource_size(&pcie->mem), PCI_REGION_MEM); |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 537 | hose->region_count = 1; |
| 538 | |
| 539 | if (resource_size(&pcie->mem)) { |
| 540 | pci_set_region(hose->regions + hose->region_count, |
| 541 | pcie->mem.start, pcie->mem.start, |
| 542 | resource_size(&pcie->mem), |
| 543 | PCI_REGION_MEM); |
| 544 | hose->region_count++; |
| 545 | } |
| 546 | |
| 547 | if (resource_size(&pcie->io)) { |
| 548 | pci_set_region(hose->regions + hose->region_count, |
| 549 | pcie->io.start, pcie->io.start, |
| 550 | resource_size(&pcie->io), |
| 551 | PCI_REGION_IO); |
| 552 | hose->region_count++; |
| 553 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 554 | |
Pali Rohár | 9e2274d | 2021-10-22 16:22:10 +0200 | [diff] [blame] | 555 | /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */ |
| 556 | pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] = |
| 557 | PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8); |
| 558 | pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] = |
| 559 | PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16); |
| 560 | |
Pali Rohár | 91d02fb | 2021-12-21 12:20:17 +0100 | [diff] [blame] | 561 | mvebu_pcie_wait_for_link(pcie); |
| 562 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 563 | return 0; |
| 564 | } |
| 565 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 566 | #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03) |
| 567 | #define DT_TYPE_IO 0x1 |
| 568 | #define DT_TYPE_MEM32 0x2 |
| 569 | #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF) |
| 570 | #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF) |
| 571 | |
| 572 | static int mvebu_get_tgt_attr(ofnode node, int devfn, |
| 573 | unsigned long type, |
| 574 | unsigned int *tgt, |
| 575 | unsigned int *attr) |
| 576 | { |
| 577 | const int na = 3, ns = 2; |
| 578 | const __be32 *range; |
| 579 | int rlen, nranges, rangesz, pna, i; |
| 580 | |
| 581 | *tgt = -1; |
| 582 | *attr = -1; |
| 583 | |
| 584 | range = ofnode_get_property(node, "ranges", &rlen); |
| 585 | if (!range) |
| 586 | return -EINVAL; |
| 587 | |
Stefan Roese | 24e23bd | 2019-02-11 07:53:34 +0100 | [diff] [blame] | 588 | /* |
| 589 | * Linux uses of_n_addr_cells() to get the number of address cells |
| 590 | * here. Currently this function is only available in U-Boot when |
| 591 | * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in |
| 592 | * general, lets't hardcode the "pna" value in the U-Boot code. |
| 593 | */ |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 594 | pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */ |
| 595 | rangesz = pna + na + ns; |
| 596 | nranges = rlen / sizeof(__be32) / rangesz; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 597 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 598 | for (i = 0; i < nranges; i++, range += rangesz) { |
| 599 | u32 flags = of_read_number(range, 1); |
| 600 | u32 slot = of_read_number(range + 1, 1); |
| 601 | u64 cpuaddr = of_read_number(range + na, pna); |
| 602 | unsigned long rtype; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 603 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 604 | if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO) |
| 605 | rtype = IORESOURCE_IO; |
| 606 | else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32) |
| 607 | rtype = IORESOURCE_MEM; |
| 608 | else |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 609 | continue; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 610 | |
| 611 | /* |
| 612 | * The Linux code used PCI_SLOT() here, which expects devfn |
| 613 | * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(), |
| 614 | * only expects devfn in 15..8, where its saved in this driver. |
| 615 | */ |
| 616 | if (slot == PCI_DEV(devfn) && type == rtype) { |
| 617 | *tgt = DT_CPUADDR_TO_TARGET(cpuaddr); |
| 618 | *attr = DT_CPUADDR_TO_ATTR(cpuaddr); |
| 619 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 620 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 621 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 622 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 623 | return -ENOENT; |
| 624 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 625 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 626 | static int mvebu_pcie_port_parse_dt(ofnode node, ofnode parent, struct mvebu_pcie *pcie) |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 627 | { |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 628 | struct fdt_pci_addr pci_addr; |
Pali Rohár | 807292f | 2021-12-21 12:20:14 +0100 | [diff] [blame] | 629 | const u32 *addr; |
Pali Rohár | 5fc93e2 | 2021-12-21 12:20:19 +0100 | [diff] [blame] | 630 | u32 num_lanes; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 631 | int ret = 0; |
Pali Rohár | 807292f | 2021-12-21 12:20:14 +0100 | [diff] [blame] | 632 | int len; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 633 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 634 | /* Get port number, lane number and memory target / attr */ |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 635 | if (ofnode_read_u32(node, "marvell,pcie-port", |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 636 | &pcie->port)) { |
| 637 | ret = -ENODEV; |
| 638 | goto err; |
| 639 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 640 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 641 | if (ofnode_read_u32(node, "marvell,pcie-lane", &pcie->lane)) |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 642 | pcie->lane = 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 643 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 644 | sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 645 | |
Pali Rohár | 5fc93e2 | 2021-12-21 12:20:19 +0100 | [diff] [blame] | 646 | if (!ofnode_read_u32(node, "num-lanes", &num_lanes) && num_lanes == 4) |
| 647 | pcie->is_x4 = true; |
| 648 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 649 | /* devfn is in bits [15:8], see PCI_DEV usage */ |
| 650 | ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg", &pci_addr); |
| 651 | if (ret < 0) { |
| 652 | printf("%s: property \"reg\" is invalid\n", pcie->name); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 653 | goto err; |
| 654 | } |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 655 | pcie->devfn = pci_addr.phys_hi & 0xff00; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 656 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 657 | ret = mvebu_get_tgt_attr(parent, pcie->devfn, |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 658 | IORESOURCE_MEM, |
| 659 | &pcie->mem_target, &pcie->mem_attr); |
| 660 | if (ret < 0) { |
| 661 | printf("%s: cannot get tgt/attr for mem window\n", pcie->name); |
| 662 | goto err; |
| 663 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 664 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 665 | ret = mvebu_get_tgt_attr(parent, pcie->devfn, |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 666 | IORESOURCE_IO, |
| 667 | &pcie->io_target, &pcie->io_attr); |
| 668 | if (ret < 0) { |
| 669 | printf("%s: cannot get tgt/attr for IO window\n", pcie->name); |
| 670 | goto err; |
| 671 | } |
| 672 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 673 | /* Parse PCIe controller register base from DT */ |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 674 | addr = ofnode_get_property(node, "assigned-addresses", &len); |
Pali Rohár | 807292f | 2021-12-21 12:20:14 +0100 | [diff] [blame] | 675 | if (!addr) { |
| 676 | printf("%s: property \"assigned-addresses\" not found\n", pcie->name); |
| 677 | ret = -FDT_ERR_NOTFOUND; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 678 | goto err; |
Pali Rohár | 807292f | 2021-12-21 12:20:14 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 681 | pcie->base = (void *)(u32)ofnode_translate_address(node, addr); |
Pali Rohár | bb26c4a | 2021-12-21 12:20:15 +0100 | [diff] [blame] | 682 | pcie->intregs = (u32)pcie->base - fdt32_to_cpu(addr[2]); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 683 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 684 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 685 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 686 | err: |
| 687 | return ret; |
| 688 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 689 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 690 | static const struct dm_pci_ops mvebu_pcie_ops = { |
| 691 | .read_config = mvebu_pcie_read_config, |
| 692 | .write_config = mvebu_pcie_write_config, |
| 693 | }; |
Phil Sutter | 68010aa | 2015-12-25 14:41:20 +0100 | [diff] [blame] | 694 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 695 | static struct driver pcie_mvebu_drv = { |
| 696 | .name = "pcie_mvebu", |
| 697 | .id = UCLASS_PCI, |
| 698 | .ops = &mvebu_pcie_ops, |
| 699 | .probe = mvebu_pcie_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 700 | .plat_auto = sizeof(struct mvebu_pcie), |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 701 | }; |
| 702 | |
| 703 | /* |
| 704 | * Use a MISC device to bind the n instances (child nodes) of the |
| 705 | * PCIe base controller in UCLASS_PCI. |
| 706 | */ |
| 707 | static int mvebu_pcie_bind(struct udevice *parent) |
| 708 | { |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 709 | struct mvebu_pcie **ports_pcie; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 710 | struct mvebu_pcie *pcie; |
| 711 | struct uclass_driver *drv; |
| 712 | struct udevice *dev; |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 713 | struct resource mem; |
| 714 | struct resource io; |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 715 | int ports_count, i; |
| 716 | ofnode *ports_nodes; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 717 | ofnode subnode; |
| 718 | |
Pali Rohár | ac2ee55 | 2021-10-22 16:22:15 +0200 | [diff] [blame] | 719 | /* Lookup pci driver */ |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 720 | drv = lists_uclass_lookup(UCLASS_PCI); |
| 721 | if (!drv) { |
| 722 | puts("Cannot find PCI driver\n"); |
| 723 | return -ENOENT; |
| 724 | } |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 725 | |
| 726 | ports_count = ofnode_get_child_count(dev_ofnode(parent)); |
| 727 | ports_pcie = calloc(ports_count, sizeof(*ports_pcie)); |
| 728 | ports_nodes = calloc(ports_count, sizeof(*ports_nodes)); |
| 729 | if (!ports_pcie || !ports_nodes) { |
| 730 | free(ports_pcie); |
| 731 | free(ports_nodes); |
| 732 | return -ENOMEM; |
| 733 | } |
| 734 | ports_count = 0; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 735 | |
Pali Rohár | ed9bcb9 | 2022-01-13 14:28:04 +0100 | [diff] [blame] | 736 | #ifdef CONFIG_ARCH_KIRKWOOD |
| 737 | mem.start = KW_DEFADR_PCI_MEM; |
| 738 | mem.end = KW_DEFADR_PCI_MEM + KW_DEFADR_PCI_MEM_SIZE - 1; |
| 739 | io.start = KW_DEFADR_PCI_IO; |
| 740 | io.end = KW_DEFADR_PCI_IO + KW_DEFADR_PCI_IO_SIZE - 1; |
| 741 | #else |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 742 | mem.start = MBUS_PCI_MEM_BASE; |
| 743 | mem.end = MBUS_PCI_MEM_BASE + MBUS_PCI_MEM_SIZE - 1; |
| 744 | io.start = MBUS_PCI_IO_BASE; |
| 745 | io.end = MBUS_PCI_IO_BASE + MBUS_PCI_IO_SIZE - 1; |
Pali Rohár | ed9bcb9 | 2022-01-13 14:28:04 +0100 | [diff] [blame] | 746 | #endif |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 747 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 748 | /* First phase: Fill mvebu_pcie struct for each port */ |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 749 | ofnode_for_each_subnode(subnode, dev_ofnode(parent)) { |
| 750 | if (!ofnode_is_available(subnode)) |
| 751 | continue; |
| 752 | |
| 753 | pcie = calloc(1, sizeof(*pcie)); |
| 754 | if (!pcie) |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 755 | continue; |
| 756 | |
| 757 | if (mvebu_pcie_port_parse_dt(subnode, dev_ofnode(parent), pcie) < 0) { |
| 758 | free(pcie); |
| 759 | continue; |
| 760 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 761 | |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 762 | /* |
| 763 | * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped |
| 764 | * into SoCs address space. Each controller will map 128M of MEM |
| 765 | * and 64K of I/O space when registered. |
| 766 | */ |
| 767 | |
| 768 | if (resource_size(&mem) >= SZ_128M) { |
| 769 | pcie->mem.start = mem.start; |
| 770 | pcie->mem.end = mem.start + SZ_128M - 1; |
| 771 | mem.start += SZ_128M; |
| 772 | } else { |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 773 | printf("%s: unable to assign mbus window for mem\n", pcie->name); |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 774 | pcie->mem.start = 0; |
| 775 | pcie->mem.end = -1; |
| 776 | } |
| 777 | |
| 778 | if (resource_size(&io) >= SZ_64K) { |
| 779 | pcie->io.start = io.start; |
| 780 | pcie->io.end = io.start + SZ_64K - 1; |
| 781 | io.start += SZ_64K; |
| 782 | } else { |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 783 | printf("%s: unable to assign mbus window for io\n", pcie->name); |
Pali Rohár | ca69af4 | 2021-12-21 12:20:13 +0100 | [diff] [blame] | 784 | pcie->io.start = 0; |
| 785 | pcie->io.end = -1; |
| 786 | } |
| 787 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 788 | ports_pcie[ports_count] = pcie; |
| 789 | ports_nodes[ports_count] = subnode; |
| 790 | ports_count++; |
| 791 | } |
| 792 | |
| 793 | /* Second phase: Setup all PCIe links (do not enable them yet) */ |
| 794 | for (i = 0; i < ports_count; i++) |
| 795 | mvebu_pcie_setup_link(ports_pcie[i]); |
| 796 | |
| 797 | /* Third phase: Enable all PCIe links and create for each UCLASS_PCI device */ |
| 798 | for (i = 0; i < ports_count; i++) { |
| 799 | pcie = ports_pcie[i]; |
| 800 | subnode = ports_nodes[i]; |
| 801 | |
| 802 | /* |
| 803 | * PCIe link can be enabled only after all PCIe links were |
| 804 | * properly configured. This is because more PCIe links shares |
| 805 | * one enable bit and some PCIe links cannot be enabled |
| 806 | * individually. |
| 807 | */ |
| 808 | if (mvebu_pcie_enable_link(pcie, subnode) < 0) { |
| 809 | free(pcie); |
| 810 | continue; |
| 811 | } |
| 812 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 813 | /* Create child device UCLASS_PCI and bind it */ |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 814 | device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode, |
| 815 | &dev); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 816 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 817 | |
Pali Rohár | 3f6890d | 2021-12-21 12:20:16 +0100 | [diff] [blame] | 818 | free(ports_pcie); |
| 819 | free(ports_nodes); |
| 820 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 821 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 822 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 823 | |
| 824 | static const struct udevice_id mvebu_pcie_ids[] = { |
| 825 | { .compatible = "marvell,armada-xp-pcie" }, |
| 826 | { .compatible = "marvell,armada-370-pcie" }, |
Pali Rohár | ed9bcb9 | 2022-01-13 14:28:04 +0100 | [diff] [blame] | 827 | { .compatible = "marvell,kirkwood-pcie" }, |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 828 | { } |
| 829 | }; |
| 830 | |
| 831 | U_BOOT_DRIVER(pcie_mvebu_base) = { |
| 832 | .name = "pcie_mvebu_base", |
| 833 | .id = UCLASS_MISC, |
| 834 | .of_match = mvebu_pcie_ids, |
| 835 | .bind = mvebu_pcie_bind, |
| 836 | }; |