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