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