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