Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * PCIe driver for Marvell MVEBU SoCs |
| 4 | * |
| 5 | * Based on Barebox drivers/pci/pci-mvebu.c |
| 6 | * |
| 7 | * Ported to U-Boot by: |
| 8 | * Anton Schubert <anton.schubert@gmx.de> |
| 9 | * Stefan Roese <sr@denx.de> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 13 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/lists.h> |
| 18 | #include <dm/of_access.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 19 | #include <pci.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <asm/arch/cpu.h> |
| 22 | #include <asm/arch/soc.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 24 | #include <linux/errno.h> |
| 25 | #include <linux/ioport.h> |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 26 | #include <linux/mbus.h> |
| 27 | |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | /* PCIe unit register offsets */ |
| 31 | #define SELECT(x, n) ((x >> n) & 1UL) |
| 32 | |
| 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)) |
| 38 | #define PCIE_CAPAB_OFF 0x0060 |
| 39 | #define PCIE_CTRL_STAT_OFF 0x0068 |
| 40 | #define PCIE_HEADER_LOG_4_OFF 0x0128 |
| 41 | #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4)) |
| 42 | #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4)) |
| 43 | #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4)) |
| 44 | #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4)) |
| 45 | #define PCIE_WIN5_CTRL_OFF 0x1880 |
| 46 | #define PCIE_WIN5_BASE_OFF 0x1884 |
| 47 | #define PCIE_WIN5_REMAP_OFF 0x188c |
| 48 | #define PCIE_CONF_ADDR_OFF 0x18f8 |
| 49 | #define PCIE_CONF_ADDR_EN BIT(31) |
| 50 | #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc)) |
| 51 | #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16) |
| 52 | #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11) |
| 53 | #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8) |
| 54 | #define PCIE_CONF_ADDR(dev, reg) \ |
| 55 | (PCIE_CONF_BUS(PCI_BUS(dev)) | PCIE_CONF_DEV(PCI_DEV(dev)) | \ |
| 56 | PCIE_CONF_FUNC(PCI_FUNC(dev)) | PCIE_CONF_REG(reg) | \ |
| 57 | PCIE_CONF_ADDR_EN) |
| 58 | #define PCIE_CONF_DATA_OFF 0x18fc |
| 59 | #define PCIE_MASK_OFF 0x1910 |
| 60 | #define PCIE_MASK_ENABLE_INTS (0xf << 24) |
| 61 | #define PCIE_CTRL_OFF 0x1a00 |
| 62 | #define PCIE_CTRL_X1_MODE BIT(0) |
| 63 | #define PCIE_STAT_OFF 0x1a04 |
| 64 | #define PCIE_STAT_BUS (0xff << 8) |
| 65 | #define PCIE_STAT_DEV (0x1f << 16) |
| 66 | #define PCIE_STAT_LINK_DOWN BIT(0) |
| 67 | #define PCIE_DEBUG_CTRL 0x1a60 |
| 68 | #define PCIE_DEBUG_SOFT_RESET BIT(20) |
| 69 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 70 | struct mvebu_pcie { |
| 71 | struct pci_controller hose; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 72 | void __iomem *base; |
| 73 | void __iomem *membase; |
| 74 | struct resource mem; |
| 75 | void __iomem *iobase; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 76 | struct resource io; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 77 | u32 port; |
| 78 | u32 lane; |
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; |
| 81 | pci_dev_t dev; |
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; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 87 | }; |
| 88 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 89 | /* |
| 90 | * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped |
VlaoMao | 9b9606d | 2017-09-22 18:49:02 +0300 | [diff] [blame] | 91 | * into SoCs address space. Each controller will map 128M of MEM |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 92 | * and 64K of I/O space when registered. |
| 93 | */ |
| 94 | static void __iomem *mvebu_pcie_membase = (void __iomem *)MBUS_PCI_MEM_BASE; |
VlaoMao | 9b9606d | 2017-09-22 18:49:02 +0300 | [diff] [blame] | 95 | #define PCIE_MEM_SIZE (128 << 20) |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 96 | static void __iomem *mvebu_pcie_iobase = (void __iomem *)MBUS_PCI_IO_BASE; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 97 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 98 | static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie) |
| 99 | { |
| 100 | u32 val; |
| 101 | val = readl(pcie->base + PCIE_STAT_OFF); |
| 102 | return !(val & PCIE_STAT_LINK_DOWN); |
| 103 | } |
| 104 | |
| 105 | static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno) |
| 106 | { |
| 107 | u32 stat; |
| 108 | |
| 109 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 110 | stat &= ~PCIE_STAT_BUS; |
| 111 | stat |= busno << 8; |
| 112 | writel(stat, pcie->base + PCIE_STAT_OFF); |
| 113 | } |
| 114 | |
| 115 | static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno) |
| 116 | { |
| 117 | u32 stat; |
| 118 | |
| 119 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 120 | stat &= ~PCIE_STAT_DEV; |
| 121 | stat |= devno << 16; |
| 122 | writel(stat, pcie->base + PCIE_STAT_OFF); |
| 123 | } |
| 124 | |
| 125 | static int mvebu_pcie_get_local_bus_nr(struct mvebu_pcie *pcie) |
| 126 | { |
| 127 | u32 stat; |
| 128 | |
| 129 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 130 | return (stat & PCIE_STAT_BUS) >> 8; |
| 131 | } |
| 132 | |
| 133 | static int mvebu_pcie_get_local_dev_nr(struct mvebu_pcie *pcie) |
| 134 | { |
| 135 | u32 stat; |
| 136 | |
| 137 | stat = readl(pcie->base + PCIE_STAT_OFF); |
| 138 | return (stat & PCIE_STAT_DEV) >> 16; |
| 139 | } |
| 140 | |
| 141 | static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose) |
| 142 | { |
| 143 | return container_of(hose, struct mvebu_pcie, hose); |
| 144 | } |
| 145 | |
Simon Glass | 2a311e8 | 2020-01-27 08:49:37 -0700 | [diff] [blame] | 146 | 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] | 147 | uint offset, ulong *valuep, |
| 148 | enum pci_size_t size) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 149 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 150 | struct mvebu_pcie *pcie = dev_get_plat(bus); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 151 | int local_bus = PCI_BUS(pcie->dev); |
| 152 | int local_dev = PCI_DEV(pcie->dev); |
| 153 | u32 reg; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 154 | u32 data; |
| 155 | |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame^] | 156 | debug("PCIE CFG read: loc_bus=%d loc_dev=%d (b,d,f)=(%2d,%2d,%2d) ", |
| 157 | local_bus, local_dev, PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 158 | |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame^] | 159 | /* Don't access the local host controller via this API */ |
| 160 | if (PCI_BUS(bdf) == local_bus && PCI_DEV(bdf) == local_dev) { |
| 161 | debug("- skipping host controller\n"); |
| 162 | *valuep = pci_get_ff(size); |
| 163 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 164 | } |
| 165 | |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame^] | 166 | /* If local dev is 0, the first other dev can only be 1 */ |
| 167 | if (PCI_BUS(bdf) == local_bus && local_dev == 0 && PCI_DEV(bdf) != 1) { |
| 168 | debug("- out of range\n"); |
| 169 | *valuep = pci_get_ff(size); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 173 | /* write address */ |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 174 | reg = PCIE_CONF_ADDR(bdf, offset); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 175 | writel(reg, pcie->base + PCIE_CONF_ADDR_OFF); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 176 | data = readl(pcie->base + PCIE_CONF_DATA_OFF); |
| 177 | debug("(addr,val)=(0x%04x, 0x%08x)\n", offset, data); |
| 178 | *valuep = pci_conv_32_to_size(data, offset, size); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 183 | static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 184 | uint offset, ulong value, |
| 185 | enum pci_size_t size) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 186 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 187 | struct mvebu_pcie *pcie = dev_get_plat(bus); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 188 | int local_bus = PCI_BUS(pcie->dev); |
| 189 | int local_dev = PCI_DEV(pcie->dev); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 190 | u32 data; |
| 191 | |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame^] | 192 | debug("PCIE CFG write: loc_bus=%d loc_dev=%d (b,d,f)=(%2d,%2d,%2d) ", |
| 193 | local_bus, local_dev, PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf)); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 194 | debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 195 | |
Stefan Roese | 8d77b0a | 2021-01-25 15:25:31 +0100 | [diff] [blame^] | 196 | /* Don't access the local host controller via this API */ |
| 197 | if (PCI_BUS(bdf) == local_bus && PCI_DEV(bdf) == local_dev) { |
| 198 | debug("- skipping host controller\n"); |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* If local dev is 0, the first other dev can only be 1 */ |
| 203 | if (PCI_BUS(bdf) == local_bus && local_dev == 0 && PCI_DEV(bdf) != 1) { |
| 204 | debug("- out of range\n"); |
| 205 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 208 | writel(PCIE_CONF_ADDR(bdf, offset), pcie->base + PCIE_CONF_ADDR_OFF); |
| 209 | data = pci_conv_size_to_32(0, value, offset, size); |
| 210 | writel(data, pcie->base + PCIE_CONF_DATA_OFF); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Setup PCIE BARs and Address Decode Wins: |
| 217 | * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks |
| 218 | * WIN[0-3] -> DRAM bank[0-3] |
| 219 | */ |
| 220 | static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie) |
| 221 | { |
| 222 | const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info(); |
| 223 | u32 size; |
| 224 | int i; |
| 225 | |
| 226 | /* First, disable and clear BARs and windows. */ |
| 227 | for (i = 1; i < 3; i++) { |
| 228 | writel(0, pcie->base + PCIE_BAR_CTRL_OFF(i)); |
| 229 | writel(0, pcie->base + PCIE_BAR_LO_OFF(i)); |
| 230 | writel(0, pcie->base + PCIE_BAR_HI_OFF(i)); |
| 231 | } |
| 232 | |
| 233 | for (i = 0; i < 5; i++) { |
| 234 | writel(0, pcie->base + PCIE_WIN04_CTRL_OFF(i)); |
| 235 | writel(0, pcie->base + PCIE_WIN04_BASE_OFF(i)); |
| 236 | writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i)); |
| 237 | } |
| 238 | |
| 239 | writel(0, pcie->base + PCIE_WIN5_CTRL_OFF); |
| 240 | writel(0, pcie->base + PCIE_WIN5_BASE_OFF); |
| 241 | writel(0, pcie->base + PCIE_WIN5_REMAP_OFF); |
| 242 | |
| 243 | /* Setup windows for DDR banks. Count total DDR size on the fly. */ |
| 244 | size = 0; |
| 245 | for (i = 0; i < dram->num_cs; i++) { |
| 246 | const struct mbus_dram_window *cs = dram->cs + i; |
| 247 | |
| 248 | writel(cs->base & 0xffff0000, |
| 249 | pcie->base + PCIE_WIN04_BASE_OFF(i)); |
| 250 | writel(0, pcie->base + PCIE_WIN04_REMAP_OFF(i)); |
| 251 | writel(((cs->size - 1) & 0xffff0000) | |
| 252 | (cs->mbus_attr << 8) | |
| 253 | (dram->mbus_dram_target_id << 4) | 1, |
| 254 | pcie->base + PCIE_WIN04_CTRL_OFF(i)); |
| 255 | |
| 256 | size += cs->size; |
| 257 | } |
| 258 | |
| 259 | /* Round up 'size' to the nearest power of two. */ |
| 260 | if ((size & (size - 1)) != 0) |
| 261 | size = 1 << fls(size); |
| 262 | |
| 263 | /* Setup BAR[1] to all DRAM banks. */ |
| 264 | writel(dram->cs[0].base | 0xc, pcie->base + PCIE_BAR_LO_OFF(1)); |
| 265 | writel(0, pcie->base + PCIE_BAR_HI_OFF(1)); |
| 266 | writel(((size - 1) & 0xffff0000) | 0x1, |
| 267 | pcie->base + PCIE_BAR_CTRL_OFF(1)); |
| 268 | } |
| 269 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 270 | static int mvebu_pcie_probe(struct udevice *dev) |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 271 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 272 | struct mvebu_pcie *pcie = dev_get_plat(dev); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 273 | struct udevice *ctlr = pci_get_controller(dev); |
| 274 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 275 | static int bus; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 276 | u32 reg; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 277 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 278 | debug("%s: PCIe %d.%d - up, base %08x\n", __func__, |
| 279 | pcie->port, pcie->lane, (u32)pcie->base); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 280 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 281 | /* Read Id info and local bus/dev */ |
| 282 | debug("direct conf read %08x, local bus %d, local dev %d\n", |
| 283 | readl(pcie->base), mvebu_pcie_get_local_bus_nr(pcie), |
| 284 | mvebu_pcie_get_local_dev_nr(pcie)); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 285 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 286 | mvebu_pcie_set_local_bus_nr(pcie, bus); |
| 287 | mvebu_pcie_set_local_dev_nr(pcie, 0); |
| 288 | pcie->dev = PCI_BDF(bus, 0, 0); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 289 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 290 | pcie->mem.start = (u32)mvebu_pcie_membase; |
| 291 | pcie->mem.end = pcie->mem.start + PCIE_MEM_SIZE - 1; |
| 292 | mvebu_pcie_membase += PCIE_MEM_SIZE; |
| 293 | |
| 294 | if (mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr, |
| 295 | (phys_addr_t)pcie->mem.start, |
| 296 | PCIE_MEM_SIZE)) { |
| 297 | printf("PCIe unable to add mbus window for mem at %08x+%08x\n", |
| 298 | (u32)pcie->mem.start, PCIE_MEM_SIZE); |
| 299 | } |
| 300 | |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 301 | pcie->io.start = (u32)mvebu_pcie_iobase; |
| 302 | pcie->io.end = pcie->io.start + MBUS_PCI_IO_SIZE - 1; |
| 303 | mvebu_pcie_iobase += MBUS_PCI_IO_SIZE; |
| 304 | |
| 305 | if (mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr, |
| 306 | (phys_addr_t)pcie->io.start, |
| 307 | MBUS_PCI_IO_SIZE)) { |
| 308 | printf("PCIe unable to add mbus window for IO at %08x+%08x\n", |
| 309 | (u32)pcie->io.start, MBUS_PCI_IO_SIZE); |
| 310 | } |
| 311 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 312 | /* Setup windows and configure host bridge */ |
| 313 | mvebu_pcie_setup_wins(pcie); |
| 314 | |
| 315 | /* Master + slave enable. */ |
| 316 | reg = readl(pcie->base + PCIE_CMD_OFF); |
| 317 | reg |= PCI_COMMAND_MEMORY; |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 318 | reg |= PCI_COMMAND_IO; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 319 | reg |= PCI_COMMAND_MASTER; |
| 320 | reg |= BIT(10); /* disable interrupts */ |
| 321 | writel(reg, pcie->base + PCIE_CMD_OFF); |
| 322 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 323 | /* PCI memory space */ |
| 324 | pci_set_region(hose->regions + 0, pcie->mem.start, |
| 325 | pcie->mem.start, PCIE_MEM_SIZE, PCI_REGION_MEM); |
| 326 | pci_set_region(hose->regions + 1, |
| 327 | 0, 0, |
| 328 | gd->ram_size, |
| 329 | PCI_REGION_MEM | PCI_REGION_SYS_MEMORY); |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 330 | pci_set_region(hose->regions + 2, pcie->io.start, |
| 331 | pcie->io.start, MBUS_PCI_IO_SIZE, PCI_REGION_IO); |
| 332 | hose->region_count = 3; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 333 | |
Marek Behún | 587816d | 2019-08-07 15:01:56 +0200 | [diff] [blame] | 334 | /* Set BAR0 to internal registers */ |
| 335 | writel(SOC_REGS_PHY_BASE, pcie->base + PCIE_BAR_LO_OFF(0)); |
| 336 | writel(0, pcie->base + PCIE_BAR_HI_OFF(0)); |
| 337 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 338 | bus++; |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static int mvebu_pcie_port_parse_dt(ofnode node, struct mvebu_pcie *pcie) |
| 344 | { |
| 345 | const u32 *addr; |
| 346 | int len; |
| 347 | |
| 348 | addr = ofnode_get_property(node, "assigned-addresses", &len); |
| 349 | if (!addr) { |
| 350 | pr_err("property \"assigned-addresses\" not found"); |
| 351 | return -FDT_ERR_NOTFOUND; |
| 352 | } |
| 353 | |
| 354 | pcie->base = (void *)(fdt32_to_cpu(addr[2]) + SOC_REGS_PHY_BASE); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03) |
| 360 | #define DT_TYPE_IO 0x1 |
| 361 | #define DT_TYPE_MEM32 0x2 |
| 362 | #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF) |
| 363 | #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF) |
| 364 | |
| 365 | static int mvebu_get_tgt_attr(ofnode node, int devfn, |
| 366 | unsigned long type, |
| 367 | unsigned int *tgt, |
| 368 | unsigned int *attr) |
| 369 | { |
| 370 | const int na = 3, ns = 2; |
| 371 | const __be32 *range; |
| 372 | int rlen, nranges, rangesz, pna, i; |
| 373 | |
| 374 | *tgt = -1; |
| 375 | *attr = -1; |
| 376 | |
| 377 | range = ofnode_get_property(node, "ranges", &rlen); |
| 378 | if (!range) |
| 379 | return -EINVAL; |
| 380 | |
Stefan Roese | 24e23bd | 2019-02-11 07:53:34 +0100 | [diff] [blame] | 381 | /* |
| 382 | * Linux uses of_n_addr_cells() to get the number of address cells |
| 383 | * here. Currently this function is only available in U-Boot when |
| 384 | * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in |
| 385 | * general, lets't hardcode the "pna" value in the U-Boot code. |
| 386 | */ |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 387 | pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */ |
| 388 | rangesz = pna + na + ns; |
| 389 | nranges = rlen / sizeof(__be32) / rangesz; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 390 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 391 | for (i = 0; i < nranges; i++, range += rangesz) { |
| 392 | u32 flags = of_read_number(range, 1); |
| 393 | u32 slot = of_read_number(range + 1, 1); |
| 394 | u64 cpuaddr = of_read_number(range + na, pna); |
| 395 | unsigned long rtype; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 396 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 397 | if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO) |
| 398 | rtype = IORESOURCE_IO; |
| 399 | else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32) |
| 400 | rtype = IORESOURCE_MEM; |
| 401 | else |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 402 | continue; |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 403 | |
| 404 | /* |
| 405 | * The Linux code used PCI_SLOT() here, which expects devfn |
| 406 | * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(), |
| 407 | * only expects devfn in 15..8, where its saved in this driver. |
| 408 | */ |
| 409 | if (slot == PCI_DEV(devfn) && type == rtype) { |
| 410 | *tgt = DT_CPUADDR_TO_TARGET(cpuaddr); |
| 411 | *attr = DT_CPUADDR_TO_ATTR(cpuaddr); |
| 412 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 413 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 414 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 415 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 416 | return -ENOENT; |
| 417 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 418 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 419 | static int mvebu_pcie_of_to_plat(struct udevice *dev) |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 420 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 421 | struct mvebu_pcie *pcie = dev_get_plat(dev); |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 422 | int ret = 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 423 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 424 | /* Get port number, lane number and memory target / attr */ |
| 425 | if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-port", |
| 426 | &pcie->port)) { |
| 427 | ret = -ENODEV; |
| 428 | goto err; |
| 429 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 430 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 431 | if (ofnode_read_u32(dev_ofnode(dev), "marvell,pcie-lane", &pcie->lane)) |
| 432 | pcie->lane = 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 433 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 434 | sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 435 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 436 | /* pci_get_devfn() returns devfn in bits 15..8, see PCI_DEV usage */ |
| 437 | pcie->devfn = pci_get_devfn(dev); |
| 438 | if (pcie->devfn < 0) { |
| 439 | ret = -ENODEV; |
| 440 | goto err; |
| 441 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 442 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 443 | ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn, |
| 444 | IORESOURCE_MEM, |
| 445 | &pcie->mem_target, &pcie->mem_attr); |
| 446 | if (ret < 0) { |
| 447 | printf("%s: cannot get tgt/attr for mem window\n", pcie->name); |
| 448 | goto err; |
| 449 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 450 | |
Phil Sutter | 09577aa | 2021-01-03 23:06:46 +0100 | [diff] [blame] | 451 | ret = mvebu_get_tgt_attr(dev_ofnode(dev->parent), pcie->devfn, |
| 452 | IORESOURCE_IO, |
| 453 | &pcie->io_target, &pcie->io_attr); |
| 454 | if (ret < 0) { |
| 455 | printf("%s: cannot get tgt/attr for IO window\n", pcie->name); |
| 456 | goto err; |
| 457 | } |
| 458 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 459 | /* Parse PCIe controller register base from DT */ |
| 460 | ret = mvebu_pcie_port_parse_dt(dev_ofnode(dev), pcie); |
| 461 | if (ret < 0) |
| 462 | goto err; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 463 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 464 | /* Check link and skip ports that have no link */ |
| 465 | if (!mvebu_pcie_link_up(pcie)) { |
| 466 | debug("%s: %s - down\n", __func__, pcie->name); |
| 467 | ret = -ENODEV; |
| 468 | goto err; |
| 469 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 470 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 471 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 472 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 473 | err: |
| 474 | return ret; |
| 475 | } |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 476 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 477 | static const struct dm_pci_ops mvebu_pcie_ops = { |
| 478 | .read_config = mvebu_pcie_read_config, |
| 479 | .write_config = mvebu_pcie_write_config, |
| 480 | }; |
Phil Sutter | 68010aa | 2015-12-25 14:41:20 +0100 | [diff] [blame] | 481 | |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 482 | static struct driver pcie_mvebu_drv = { |
| 483 | .name = "pcie_mvebu", |
| 484 | .id = UCLASS_PCI, |
| 485 | .ops = &mvebu_pcie_ops, |
| 486 | .probe = mvebu_pcie_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 487 | .of_to_plat = mvebu_pcie_of_to_plat, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 488 | .plat_auto = sizeof(struct mvebu_pcie), |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | /* |
| 492 | * Use a MISC device to bind the n instances (child nodes) of the |
| 493 | * PCIe base controller in UCLASS_PCI. |
| 494 | */ |
| 495 | static int mvebu_pcie_bind(struct udevice *parent) |
| 496 | { |
| 497 | struct mvebu_pcie *pcie; |
| 498 | struct uclass_driver *drv; |
| 499 | struct udevice *dev; |
| 500 | ofnode subnode; |
| 501 | |
| 502 | /* Lookup eth driver */ |
| 503 | drv = lists_uclass_lookup(UCLASS_PCI); |
| 504 | if (!drv) { |
| 505 | puts("Cannot find PCI driver\n"); |
| 506 | return -ENOENT; |
| 507 | } |
| 508 | |
| 509 | ofnode_for_each_subnode(subnode, dev_ofnode(parent)) { |
| 510 | if (!ofnode_is_available(subnode)) |
| 511 | continue; |
| 512 | |
| 513 | pcie = calloc(1, sizeof(*pcie)); |
| 514 | if (!pcie) |
| 515 | return -ENOMEM; |
| 516 | |
| 517 | /* Create child device UCLASS_PCI and bind it */ |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 518 | device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode, |
| 519 | &dev); |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 520 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 521 | |
| 522 | return 0; |
Anton Schubert | 98530e9 | 2015-08-11 11:54:01 +0200 | [diff] [blame] | 523 | } |
Stefan Roese | 3179ec6 | 2019-01-25 11:52:43 +0100 | [diff] [blame] | 524 | |
| 525 | static const struct udevice_id mvebu_pcie_ids[] = { |
| 526 | { .compatible = "marvell,armada-xp-pcie" }, |
| 527 | { .compatible = "marvell,armada-370-pcie" }, |
| 528 | { } |
| 529 | }; |
| 530 | |
| 531 | U_BOOT_DRIVER(pcie_mvebu_base) = { |
| 532 | .name = "pcie_mvebu_base", |
| 533 | .id = UCLASS_MISC, |
| 534 | .of_match = mvebu_pcie_ids, |
| 535 | .bind = mvebu_pcie_bind, |
| 536 | }; |