Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Broadcom STB PCIe controller driver |
| 4 | * |
| 5 | * Copyright (C) 2020 Samsung Electronics Co., Ltd. |
| 6 | * |
| 7 | * Based on upstream Linux kernel driver: |
| 8 | * drivers/pci/controller/pcie-brcmstb.c |
| 9 | * Copyright (C) 2009 - 2017 Broadcom |
| 10 | * |
| 11 | * Based driver by Nicolas Saenz Julienne |
| 12 | * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de> |
| 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <errno.h> |
| 17 | #include <dm.h> |
| 18 | #include <dm/ofnode.h> |
| 19 | #include <pci.h> |
| 20 | #include <asm/io.h> |
| 21 | #include <linux/bitfield.h> |
| 22 | #include <linux/log2.h> |
| 23 | #include <linux/iopoll.h> |
| 24 | |
| 25 | /* Offset of the mandatory PCIe capability config registers */ |
| 26 | #define BRCM_PCIE_CAP_REGS 0x00ac |
| 27 | |
| 28 | /* The PCIe controller register offsets */ |
| 29 | #define PCIE_RC_CFG_VENDOR_SPECIFIC_REG1 0x0188 |
| 30 | #define VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK 0xc |
| 31 | #define VENDOR_SPECIFIC_REG1_LITTLE_ENDIAN 0x0 |
| 32 | |
| 33 | #define PCIE_RC_CFG_PRIV1_ID_VAL3 0x043c |
| 34 | #define CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK 0xffffff |
| 35 | |
| 36 | #define PCIE_RC_DL_MDIO_ADDR 0x1100 |
| 37 | #define PCIE_RC_DL_MDIO_WR_DATA 0x1104 |
| 38 | #define PCIE_RC_DL_MDIO_RD_DATA 0x1108 |
| 39 | |
| 40 | #define PCIE_MISC_MISC_CTRL 0x4008 |
| 41 | #define MISC_CTRL_SCB_ACCESS_EN_MASK 0x1000 |
| 42 | #define MISC_CTRL_CFG_READ_UR_MODE_MASK 0x2000 |
| 43 | #define MISC_CTRL_MAX_BURST_SIZE_MASK 0x300000 |
| 44 | #define MISC_CTRL_MAX_BURST_SIZE_128 0x0 |
| 45 | #define MISC_CTRL_SCB0_SIZE_MASK 0xf8000000 |
| 46 | |
| 47 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO 0x400c |
| 48 | #define PCIE_MEM_WIN0_LO(win) \ |
| 49 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO + ((win) * 4) |
| 50 | |
| 51 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI 0x4010 |
| 52 | #define PCIE_MEM_WIN0_HI(win) \ |
| 53 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI + ((win) * 4) |
| 54 | |
| 55 | #define PCIE_MISC_RC_BAR1_CONFIG_LO 0x402c |
| 56 | #define RC_BAR1_CONFIG_LO_SIZE_MASK 0x1f |
| 57 | |
| 58 | #define PCIE_MISC_RC_BAR2_CONFIG_LO 0x4034 |
| 59 | #define RC_BAR2_CONFIG_LO_SIZE_MASK 0x1f |
| 60 | #define PCIE_MISC_RC_BAR2_CONFIG_HI 0x4038 |
| 61 | |
| 62 | #define PCIE_MISC_RC_BAR3_CONFIG_LO 0x403c |
| 63 | #define RC_BAR3_CONFIG_LO_SIZE_MASK 0x1f |
| 64 | |
| 65 | #define PCIE_MISC_PCIE_STATUS 0x4068 |
| 66 | #define STATUS_PCIE_PORT_MASK 0x80 |
| 67 | #define STATUS_PCIE_PORT_SHIFT 7 |
| 68 | #define STATUS_PCIE_DL_ACTIVE_MASK 0x20 |
| 69 | #define STATUS_PCIE_DL_ACTIVE_SHIFT 5 |
| 70 | #define STATUS_PCIE_PHYLINKUP_MASK 0x10 |
| 71 | #define STATUS_PCIE_PHYLINKUP_SHIFT 4 |
| 72 | |
| 73 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT 0x4070 |
| 74 | #define MEM_WIN0_BASE_LIMIT_LIMIT_MASK 0xfff00000 |
| 75 | #define MEM_WIN0_BASE_LIMIT_BASE_MASK 0xfff0 |
| 76 | #define MEM_WIN0_BASE_LIMIT_BASE_HI_SHIFT 12 |
| 77 | #define PCIE_MEM_WIN0_BASE_LIMIT(win) \ |
| 78 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT + ((win) * 4) |
| 79 | |
| 80 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI 0x4080 |
| 81 | #define MEM_WIN0_BASE_HI_BASE_MASK 0xff |
| 82 | #define PCIE_MEM_WIN0_BASE_HI(win) \ |
| 83 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI + ((win) * 8) |
| 84 | |
| 85 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI 0x4084 |
| 86 | #define PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK 0xff |
| 87 | #define PCIE_MEM_WIN0_LIMIT_HI(win) \ |
| 88 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI + ((win) * 8) |
| 89 | |
| 90 | #define PCIE_MISC_HARD_PCIE_HARD_DEBUG 0x4204 |
| 91 | #define PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK 0x2 |
| 92 | #define PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x08000000 |
| 93 | |
| 94 | #define PCIE_MSI_INTR2_CLR 0x4508 |
| 95 | #define PCIE_MSI_INTR2_MASK_SET 0x4510 |
| 96 | |
| 97 | #define PCIE_EXT_CFG_DATA 0x8000 |
| 98 | |
| 99 | #define PCIE_EXT_CFG_INDEX 0x9000 |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 100 | |
| 101 | #define PCIE_RGR1_SW_INIT_1 0x9210 |
| 102 | #define RGR1_SW_INIT_1_PERST_MASK 0x1 |
| 103 | #define RGR1_SW_INIT_1_INIT_MASK 0x2 |
| 104 | |
| 105 | /* PCIe parameters */ |
| 106 | #define BRCM_NUM_PCIE_OUT_WINS 4 |
| 107 | |
| 108 | /* MDIO registers */ |
| 109 | #define MDIO_PORT0 0x0 |
| 110 | #define MDIO_DATA_MASK 0x7fffffff |
| 111 | #define MDIO_DATA_SHIFT 0 |
| 112 | #define MDIO_PORT_MASK 0xf0000 |
| 113 | #define MDIO_PORT_SHIFT 16 |
| 114 | #define MDIO_REGAD_MASK 0xffff |
| 115 | #define MDIO_REGAD_SHIFT 0 |
| 116 | #define MDIO_CMD_MASK 0xfff00000 |
| 117 | #define MDIO_CMD_SHIFT 20 |
| 118 | #define MDIO_CMD_READ 0x1 |
| 119 | #define MDIO_CMD_WRITE 0x0 |
| 120 | #define MDIO_DATA_DONE_MASK 0x80000000 |
| 121 | #define SSC_REGS_ADDR 0x1100 |
| 122 | #define SET_ADDR_OFFSET 0x1f |
| 123 | #define SSC_CNTL_OFFSET 0x2 |
| 124 | #define SSC_CNTL_OVRD_EN_MASK 0x8000 |
| 125 | #define SSC_CNTL_OVRD_VAL_MASK 0x4000 |
| 126 | #define SSC_STATUS_OFFSET 0x1 |
| 127 | #define SSC_STATUS_SSC_MASK 0x400 |
| 128 | #define SSC_STATUS_SSC_SHIFT 10 |
| 129 | #define SSC_STATUS_PLL_LOCK_MASK 0x800 |
| 130 | #define SSC_STATUS_PLL_LOCK_SHIFT 11 |
| 131 | |
| 132 | /** |
| 133 | * struct brcm_pcie - the PCIe controller state |
| 134 | * @base: Base address of memory mapped IO registers of the controller |
| 135 | * @gen: Non-zero value indicates limitation of the PCIe controller operation |
| 136 | * to a specific generation (1, 2 or 3) |
| 137 | * @ssc: true indicates active Spread Spectrum Clocking operation |
| 138 | */ |
| 139 | struct brcm_pcie { |
| 140 | void __iomem *base; |
| 141 | |
| 142 | int gen; |
| 143 | bool ssc; |
| 144 | }; |
| 145 | |
| 146 | /** |
| 147 | * brcm_pcie_encode_ibar_size() - Encode the inbound "BAR" region size |
| 148 | * @size: The inbound region size |
| 149 | * |
| 150 | * This function converts size of the inbound "BAR" region to the non-linear |
| 151 | * values of the PCIE_MISC_RC_BAR[123]_CONFIG_LO register SIZE field. |
| 152 | * |
| 153 | * Return: The encoded inbound region size |
| 154 | */ |
| 155 | static int brcm_pcie_encode_ibar_size(u64 size) |
| 156 | { |
| 157 | int log2_in = ilog2(size); |
| 158 | |
| 159 | if (log2_in >= 12 && log2_in <= 15) |
| 160 | /* Covers 4KB to 32KB (inclusive) */ |
| 161 | return (log2_in - 12) + 0x1c; |
| 162 | else if (log2_in >= 16 && log2_in <= 37) |
| 163 | /* Covers 64KB to 32GB, (inclusive) */ |
| 164 | return log2_in - 15; |
| 165 | |
| 166 | /* Something is awry so disable */ |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * brcm_pcie_rc_mode() - Check if PCIe controller is in RC mode |
| 172 | * @pcie: Pointer to the PCIe controller state |
| 173 | * |
| 174 | * The controller is capable of serving in both RC and EP roles. |
| 175 | * |
| 176 | * Return: true for RC mode, false for EP mode. |
| 177 | */ |
| 178 | static bool brcm_pcie_rc_mode(struct brcm_pcie *pcie) |
| 179 | { |
| 180 | u32 val; |
| 181 | |
| 182 | val = readl(pcie->base + PCIE_MISC_PCIE_STATUS); |
| 183 | |
| 184 | return (val & STATUS_PCIE_PORT_MASK) >> STATUS_PCIE_PORT_SHIFT; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * brcm_pcie_link_up() - Check whether the PCIe link is up |
| 189 | * @pcie: Pointer to the PCIe controller state |
| 190 | * |
| 191 | * Return: true if the link is up, false otherwise. |
| 192 | */ |
| 193 | static bool brcm_pcie_link_up(struct brcm_pcie *pcie) |
| 194 | { |
| 195 | u32 val, dla, plu; |
| 196 | |
| 197 | val = readl(pcie->base + PCIE_MISC_PCIE_STATUS); |
| 198 | dla = (val & STATUS_PCIE_DL_ACTIVE_MASK) >> STATUS_PCIE_DL_ACTIVE_SHIFT; |
| 199 | plu = (val & STATUS_PCIE_PHYLINKUP_MASK) >> STATUS_PCIE_PHYLINKUP_SHIFT; |
| 200 | |
| 201 | return dla && plu; |
| 202 | } |
| 203 | |
| 204 | static int brcm_pcie_config_address(const struct udevice *dev, pci_dev_t bdf, |
| 205 | uint offset, void **paddress) |
| 206 | { |
| 207 | struct brcm_pcie *pcie = dev_get_priv(dev); |
| 208 | unsigned int pci_bus = PCI_BUS(bdf); |
| 209 | unsigned int pci_dev = PCI_DEV(bdf); |
| 210 | unsigned int pci_func = PCI_FUNC(bdf); |
| 211 | int idx; |
| 212 | |
| 213 | /* |
| 214 | * Busses 0 (host PCIe bridge) and 1 (its immediate child) |
| 215 | * are limited to a single device each |
| 216 | */ |
| 217 | if (pci_bus < 2 && pci_dev > 0) |
| 218 | return -EINVAL; |
| 219 | |
| 220 | /* Accesses to the RC go right to the RC registers */ |
| 221 | if (pci_bus == 0) { |
| 222 | *paddress = pcie->base + offset; |
| 223 | return 0; |
| 224 | } |
| 225 | |
Sam Edwards | 30e5859 | 2023-08-14 16:34:13 -0600 | [diff] [blame^] | 226 | /* An access to our HW w/o link-up will cause a CPU Abort */ |
| 227 | if (!brcm_pcie_link_up(pcie)) |
| 228 | return -EINVAL; |
| 229 | |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 230 | /* For devices, write to the config space index register */ |
Pali Rohár | 9e98eb7 | 2021-11-24 18:00:31 +0100 | [diff] [blame] | 231 | idx = PCIE_ECAM_OFFSET(pci_bus, pci_dev, pci_func, 0); |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 232 | |
| 233 | writel(idx, pcie->base + PCIE_EXT_CFG_INDEX); |
| 234 | *paddress = pcie->base + PCIE_EXT_CFG_DATA + offset; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int brcm_pcie_read_config(const struct udevice *bus, pci_dev_t bdf, |
| 240 | uint offset, ulong *valuep, |
| 241 | enum pci_size_t size) |
| 242 | { |
| 243 | return pci_generic_mmap_read_config(bus, brcm_pcie_config_address, |
| 244 | bdf, offset, valuep, size); |
| 245 | } |
| 246 | |
| 247 | static int brcm_pcie_write_config(struct udevice *bus, pci_dev_t bdf, |
| 248 | uint offset, ulong value, |
| 249 | enum pci_size_t size) |
| 250 | { |
| 251 | return pci_generic_mmap_write_config(bus, brcm_pcie_config_address, |
| 252 | bdf, offset, value, size); |
| 253 | } |
| 254 | |
| 255 | static const char *link_speed_to_str(unsigned int cls) |
| 256 | { |
| 257 | switch (cls) { |
| 258 | case PCI_EXP_LNKSTA_CLS_2_5GB: return "2.5"; |
| 259 | case PCI_EXP_LNKSTA_CLS_5_0GB: return "5.0"; |
| 260 | case PCI_EXP_LNKSTA_CLS_8_0GB: return "8.0"; |
| 261 | default: |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | return "??"; |
| 266 | } |
| 267 | |
| 268 | static u32 brcm_pcie_mdio_form_pkt(unsigned int port, unsigned int regad, |
| 269 | unsigned int cmd) |
| 270 | { |
| 271 | u32 pkt; |
| 272 | |
| 273 | pkt = (port << MDIO_PORT_SHIFT) & MDIO_PORT_MASK; |
| 274 | pkt |= (regad << MDIO_REGAD_SHIFT) & MDIO_REGAD_MASK; |
| 275 | pkt |= (cmd << MDIO_CMD_SHIFT) & MDIO_CMD_MASK; |
| 276 | |
| 277 | return pkt; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * brcm_pcie_mdio_read() - Perform a register read on the internal MDIO bus |
| 282 | * @base: Pointer to the PCIe controller IO registers |
| 283 | * @port: The MDIO port number |
| 284 | * @regad: The register address |
| 285 | * @val: A pointer at which to store the read value |
| 286 | * |
| 287 | * Return: 0 on success and register value in @val, negative error value |
| 288 | * on failure. |
| 289 | */ |
| 290 | static int brcm_pcie_mdio_read(void __iomem *base, unsigned int port, |
| 291 | unsigned int regad, u32 *val) |
| 292 | { |
| 293 | u32 data, addr; |
| 294 | int ret; |
| 295 | |
| 296 | addr = brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_READ); |
| 297 | writel(addr, base + PCIE_RC_DL_MDIO_ADDR); |
| 298 | readl(base + PCIE_RC_DL_MDIO_ADDR); |
| 299 | |
| 300 | ret = readl_poll_timeout(base + PCIE_RC_DL_MDIO_RD_DATA, data, |
| 301 | (data & MDIO_DATA_DONE_MASK), 100); |
| 302 | |
| 303 | *val = data & MDIO_DATA_MASK; |
| 304 | |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * brcm_pcie_mdio_write() - Perform a register write on the internal MDIO bus |
| 310 | * @base: Pointer to the PCIe controller IO registers |
| 311 | * @port: The MDIO port number |
| 312 | * @regad: Address of the register |
| 313 | * @wrdata: The value to write |
| 314 | * |
| 315 | * Return: 0 on success, negative error value on failure. |
| 316 | */ |
| 317 | static int brcm_pcie_mdio_write(void __iomem *base, unsigned int port, |
| 318 | unsigned int regad, u16 wrdata) |
| 319 | { |
| 320 | u32 data, addr; |
| 321 | |
| 322 | addr = brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_WRITE); |
| 323 | writel(addr, base + PCIE_RC_DL_MDIO_ADDR); |
| 324 | readl(base + PCIE_RC_DL_MDIO_ADDR); |
| 325 | writel(MDIO_DATA_DONE_MASK | wrdata, base + PCIE_RC_DL_MDIO_WR_DATA); |
| 326 | |
| 327 | return readl_poll_timeout(base + PCIE_RC_DL_MDIO_WR_DATA, data, |
| 328 | !(data & MDIO_DATA_DONE_MASK), 100); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * brcm_pcie_set_ssc() - Configure the controller for Spread Spectrum Clocking |
| 333 | * @base: pointer to the PCIe controller IO registers |
| 334 | * |
| 335 | * Return: 0 on success, negative error value on failure. |
| 336 | */ |
| 337 | static int brcm_pcie_set_ssc(void __iomem *base) |
| 338 | { |
| 339 | int pll, ssc; |
| 340 | int ret; |
| 341 | u32 tmp; |
| 342 | |
| 343 | ret = brcm_pcie_mdio_write(base, MDIO_PORT0, SET_ADDR_OFFSET, |
| 344 | SSC_REGS_ADDR); |
| 345 | if (ret < 0) |
| 346 | return ret; |
| 347 | |
| 348 | ret = brcm_pcie_mdio_read(base, MDIO_PORT0, SSC_CNTL_OFFSET, &tmp); |
| 349 | if (ret < 0) |
| 350 | return ret; |
| 351 | |
| 352 | tmp |= (SSC_CNTL_OVRD_EN_MASK | SSC_CNTL_OVRD_VAL_MASK); |
| 353 | |
| 354 | ret = brcm_pcie_mdio_write(base, MDIO_PORT0, SSC_CNTL_OFFSET, tmp); |
| 355 | if (ret < 0) |
| 356 | return ret; |
| 357 | |
| 358 | udelay(1000); |
| 359 | ret = brcm_pcie_mdio_read(base, MDIO_PORT0, SSC_STATUS_OFFSET, &tmp); |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
| 363 | ssc = (tmp & SSC_STATUS_SSC_MASK) >> SSC_STATUS_SSC_SHIFT; |
| 364 | pll = (tmp & SSC_STATUS_PLL_LOCK_MASK) >> SSC_STATUS_PLL_LOCK_SHIFT; |
| 365 | |
| 366 | return ssc && pll ? 0 : -EIO; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * brcm_pcie_set_gen() - Limits operation to a specific generation (1, 2 or 3) |
| 371 | * @pcie: pointer to the PCIe controller state |
| 372 | * @gen: PCIe generation to limit the controller's operation to |
| 373 | */ |
| 374 | static void brcm_pcie_set_gen(struct brcm_pcie *pcie, unsigned int gen) |
| 375 | { |
| 376 | void __iomem *cap_base = pcie->base + BRCM_PCIE_CAP_REGS; |
| 377 | |
| 378 | u16 lnkctl2 = readw(cap_base + PCI_EXP_LNKCTL2); |
| 379 | u32 lnkcap = readl(cap_base + PCI_EXP_LNKCAP); |
| 380 | |
| 381 | lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen; |
| 382 | writel(lnkcap, cap_base + PCI_EXP_LNKCAP); |
| 383 | |
| 384 | lnkctl2 = (lnkctl2 & ~0xf) | gen; |
| 385 | writew(lnkctl2, cap_base + PCI_EXP_LNKCTL2); |
| 386 | } |
| 387 | |
| 388 | static void brcm_pcie_set_outbound_win(struct brcm_pcie *pcie, |
| 389 | unsigned int win, u64 phys_addr, |
| 390 | u64 pcie_addr, u64 size) |
| 391 | { |
| 392 | void __iomem *base = pcie->base; |
| 393 | u32 phys_addr_mb_high, limit_addr_mb_high; |
| 394 | phys_addr_t phys_addr_mb, limit_addr_mb; |
| 395 | int high_addr_shift; |
| 396 | u32 tmp; |
| 397 | |
| 398 | /* Set the base of the pcie_addr window */ |
| 399 | writel(lower_32_bits(pcie_addr), base + PCIE_MEM_WIN0_LO(win)); |
| 400 | writel(upper_32_bits(pcie_addr), base + PCIE_MEM_WIN0_HI(win)); |
| 401 | |
| 402 | /* Write the addr base & limit lower bits (in MBs) */ |
| 403 | phys_addr_mb = phys_addr / SZ_1M; |
| 404 | limit_addr_mb = (phys_addr + size - 1) / SZ_1M; |
| 405 | |
| 406 | tmp = readl(base + PCIE_MEM_WIN0_BASE_LIMIT(win)); |
| 407 | u32p_replace_bits(&tmp, phys_addr_mb, |
| 408 | MEM_WIN0_BASE_LIMIT_BASE_MASK); |
| 409 | u32p_replace_bits(&tmp, limit_addr_mb, |
| 410 | MEM_WIN0_BASE_LIMIT_LIMIT_MASK); |
| 411 | writel(tmp, base + PCIE_MEM_WIN0_BASE_LIMIT(win)); |
| 412 | |
| 413 | /* Write the cpu & limit addr upper bits */ |
| 414 | high_addr_shift = MEM_WIN0_BASE_LIMIT_BASE_HI_SHIFT; |
| 415 | phys_addr_mb_high = phys_addr_mb >> high_addr_shift; |
| 416 | tmp = readl(base + PCIE_MEM_WIN0_BASE_HI(win)); |
| 417 | u32p_replace_bits(&tmp, phys_addr_mb_high, |
| 418 | MEM_WIN0_BASE_HI_BASE_MASK); |
| 419 | writel(tmp, base + PCIE_MEM_WIN0_BASE_HI(win)); |
| 420 | |
| 421 | limit_addr_mb_high = limit_addr_mb >> high_addr_shift; |
| 422 | tmp = readl(base + PCIE_MEM_WIN0_LIMIT_HI(win)); |
| 423 | u32p_replace_bits(&tmp, limit_addr_mb_high, |
| 424 | PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK); |
| 425 | writel(tmp, base + PCIE_MEM_WIN0_LIMIT_HI(win)); |
| 426 | } |
| 427 | |
| 428 | static int brcm_pcie_probe(struct udevice *dev) |
| 429 | { |
| 430 | struct udevice *ctlr = pci_get_controller(dev); |
| 431 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 432 | struct brcm_pcie *pcie = dev_get_priv(dev); |
| 433 | void __iomem *base = pcie->base; |
Nicolas Saenz Julienne | 038876b | 2021-01-12 13:55:21 +0100 | [diff] [blame] | 434 | struct pci_region region; |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 435 | bool ssc_good = false; |
| 436 | int num_out_wins = 0; |
| 437 | u64 rc_bar2_offset, rc_bar2_size; |
| 438 | unsigned int scb_size_val; |
| 439 | int i, ret; |
| 440 | u16 nlw, cls, lnksta; |
| 441 | u32 tmp; |
| 442 | |
| 443 | /* |
| 444 | * Reset the bridge, assert the fundamental reset. Note for some SoCs, |
| 445 | * e.g. BCM7278, the fundamental reset should not be asserted here. |
| 446 | * This will need to be changed when support for other SoCs is added. |
| 447 | */ |
| 448 | setbits_le32(base + PCIE_RGR1_SW_INIT_1, |
| 449 | RGR1_SW_INIT_1_INIT_MASK | RGR1_SW_INIT_1_PERST_MASK); |
| 450 | /* |
| 451 | * The delay is a safety precaution to preclude the reset signal |
| 452 | * from looking like a glitch. |
| 453 | */ |
| 454 | udelay(100); |
| 455 | |
| 456 | /* Take the bridge out of reset */ |
| 457 | clrbits_le32(base + PCIE_RGR1_SW_INIT_1, RGR1_SW_INIT_1_INIT_MASK); |
| 458 | |
| 459 | clrbits_le32(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG, |
| 460 | PCIE_HARD_DEBUG_SERDES_IDDQ_MASK); |
| 461 | |
| 462 | /* Wait for SerDes to be stable */ |
| 463 | udelay(100); |
| 464 | |
| 465 | /* Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN */ |
| 466 | clrsetbits_le32(base + PCIE_MISC_MISC_CTRL, |
| 467 | MISC_CTRL_MAX_BURST_SIZE_MASK, |
| 468 | MISC_CTRL_SCB_ACCESS_EN_MASK | |
| 469 | MISC_CTRL_CFG_READ_UR_MODE_MASK | |
| 470 | MISC_CTRL_MAX_BURST_SIZE_128); |
Nicolas Saenz Julienne | 038876b | 2021-01-12 13:55:21 +0100 | [diff] [blame] | 471 | |
| 472 | pci_get_dma_regions(dev, ®ion, 0); |
| 473 | rc_bar2_offset = region.bus_start - region.phys_start; |
| 474 | rc_bar2_size = 1ULL << fls64(region.size - 1); |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 475 | |
| 476 | tmp = lower_32_bits(rc_bar2_offset); |
| 477 | u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(rc_bar2_size), |
| 478 | RC_BAR2_CONFIG_LO_SIZE_MASK); |
| 479 | writel(tmp, base + PCIE_MISC_RC_BAR2_CONFIG_LO); |
| 480 | writel(upper_32_bits(rc_bar2_offset), |
| 481 | base + PCIE_MISC_RC_BAR2_CONFIG_HI); |
| 482 | |
| 483 | scb_size_val = rc_bar2_size ? |
| 484 | ilog2(rc_bar2_size) - 15 : 0xf; /* 0xf is 1GB */ |
| 485 | |
| 486 | tmp = readl(base + PCIE_MISC_MISC_CTRL); |
| 487 | u32p_replace_bits(&tmp, scb_size_val, |
| 488 | MISC_CTRL_SCB0_SIZE_MASK); |
| 489 | writel(tmp, base + PCIE_MISC_MISC_CTRL); |
| 490 | |
| 491 | /* Disable the PCIe->GISB memory window (RC_BAR1) */ |
| 492 | clrbits_le32(base + PCIE_MISC_RC_BAR1_CONFIG_LO, |
| 493 | RC_BAR1_CONFIG_LO_SIZE_MASK); |
| 494 | |
| 495 | /* Disable the PCIe->SCB memory window (RC_BAR3) */ |
| 496 | clrbits_le32(base + PCIE_MISC_RC_BAR3_CONFIG_LO, |
| 497 | RC_BAR3_CONFIG_LO_SIZE_MASK); |
| 498 | |
| 499 | /* Mask all interrupts since we are not handling any yet */ |
| 500 | writel(0xffffffff, base + PCIE_MSI_INTR2_MASK_SET); |
| 501 | |
| 502 | /* Clear any interrupts we find on boot */ |
| 503 | writel(0xffffffff, base + PCIE_MSI_INTR2_CLR); |
| 504 | |
| 505 | if (pcie->gen) |
| 506 | brcm_pcie_set_gen(pcie, pcie->gen); |
| 507 | |
| 508 | /* Unassert the fundamental reset */ |
| 509 | clrbits_le32(pcie->base + PCIE_RGR1_SW_INIT_1, |
| 510 | RGR1_SW_INIT_1_PERST_MASK); |
| 511 | |
Sam Edwards | 30e5859 | 2023-08-14 16:34:13 -0600 | [diff] [blame^] | 512 | /* |
| 513 | * Wait for 100ms after PERST# deassertion; see PCIe CEM specification |
| 514 | * sections 2.2, PCIe r5.0, 6.6.1. |
| 515 | */ |
| 516 | mdelay(100); |
| 517 | |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 518 | /* Give the RC/EP time to wake up, before trying to configure RC. |
| 519 | * Intermittently check status for link-up, up to a total of 100ms. |
| 520 | */ |
| 521 | for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5) |
| 522 | mdelay(5); |
| 523 | |
| 524 | if (!brcm_pcie_link_up(pcie)) { |
| 525 | printf("PCIe BRCM: link down\n"); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | if (!brcm_pcie_rc_mode(pcie)) { |
| 530 | printf("PCIe misconfigured; is in EP mode\n"); |
| 531 | return -EINVAL; |
| 532 | } |
| 533 | |
| 534 | for (i = 0; i < hose->region_count; i++) { |
| 535 | struct pci_region *reg = &hose->regions[i]; |
| 536 | |
| 537 | if (reg->flags != PCI_REGION_MEM) |
| 538 | continue; |
| 539 | |
| 540 | if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | brcm_pcie_set_outbound_win(pcie, num_out_wins, reg->phys_start, |
| 544 | reg->bus_start, reg->size); |
| 545 | |
| 546 | num_out_wins++; |
| 547 | } |
| 548 | |
| 549 | /* |
| 550 | * For config space accesses on the RC, show the right class for |
| 551 | * a PCIe-PCIe bridge (the default setting is to be EP mode). |
| 552 | */ |
| 553 | clrsetbits_le32(base + PCIE_RC_CFG_PRIV1_ID_VAL3, |
| 554 | CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK, 0x060400); |
| 555 | |
| 556 | if (pcie->ssc) { |
| 557 | ret = brcm_pcie_set_ssc(pcie->base); |
| 558 | if (!ret) |
| 559 | ssc_good = true; |
| 560 | else |
| 561 | printf("PCIe BRCM: failed attempt to enter SSC mode\n"); |
| 562 | } |
| 563 | |
| 564 | lnksta = readw(base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKSTA); |
| 565 | cls = lnksta & PCI_EXP_LNKSTA_CLS; |
| 566 | nlw = (lnksta & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT; |
| 567 | |
| 568 | printf("PCIe BRCM: link up, %s Gbps x%u %s\n", link_speed_to_str(cls), |
| 569 | nlw, ssc_good ? "(SSC)" : "(!SSC)"); |
| 570 | |
| 571 | /* PCIe->SCB endian mode for BAR */ |
| 572 | clrsetbits_le32(base + PCIE_RC_CFG_VENDOR_SPECIFIC_REG1, |
| 573 | VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK, |
| 574 | VENDOR_SPECIFIC_REG1_LITTLE_ENDIAN); |
| 575 | /* |
| 576 | * Refclk from RC should be gated with CLKREQ# input when ASPM L0s,L1 |
| 577 | * is enabled => setting the CLKREQ_DEBUG_ENABLE field to 1. |
| 578 | */ |
| 579 | setbits_le32(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG, |
| 580 | PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK); |
| 581 | |
| 582 | return 0; |
| 583 | } |
| 584 | |
Nicolas Saenz Julienne | df10fe8 | 2021-01-14 16:49:01 +0100 | [diff] [blame] | 585 | static int brcm_pcie_remove(struct udevice *dev) |
| 586 | { |
| 587 | struct brcm_pcie *pcie = dev_get_priv(dev); |
| 588 | void __iomem *base = pcie->base; |
| 589 | |
| 590 | /* Assert fundamental reset */ |
| 591 | setbits_le32(base + PCIE_RGR1_SW_INIT_1, RGR1_SW_INIT_1_PERST_MASK); |
| 592 | |
| 593 | /* Turn off SerDes */ |
| 594 | setbits_le32(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG, |
| 595 | PCIE_HARD_DEBUG_SERDES_IDDQ_MASK); |
| 596 | |
| 597 | /* Shutdown bridge */ |
| 598 | setbits_le32(base + PCIE_RGR1_SW_INIT_1, RGR1_SW_INIT_1_INIT_MASK); |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 603 | static int brcm_pcie_of_to_plat(struct udevice *dev) |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 604 | { |
| 605 | struct brcm_pcie *pcie = dev_get_priv(dev); |
| 606 | ofnode dn = dev_ofnode(dev); |
| 607 | u32 max_link_speed; |
| 608 | int ret; |
| 609 | |
| 610 | /* Get the controller base address */ |
| 611 | pcie->base = dev_read_addr_ptr(dev); |
| 612 | if (!pcie->base) |
| 613 | return -EINVAL; |
| 614 | |
| 615 | pcie->ssc = ofnode_read_bool(dn, "brcm,enable-ssc"); |
| 616 | |
| 617 | ret = ofnode_read_u32(dn, "max-link-speed", &max_link_speed); |
| 618 | if (ret < 0 || max_link_speed > 4) |
| 619 | pcie->gen = 0; |
| 620 | else |
| 621 | pcie->gen = max_link_speed; |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | static const struct dm_pci_ops brcm_pcie_ops = { |
| 627 | .read_config = brcm_pcie_read_config, |
| 628 | .write_config = brcm_pcie_write_config, |
| 629 | }; |
| 630 | |
| 631 | static const struct udevice_id brcm_pcie_ids[] = { |
| 632 | { .compatible = "brcm,bcm2711-pcie" }, |
| 633 | { } |
| 634 | }; |
| 635 | |
| 636 | U_BOOT_DRIVER(pcie_brcm_base) = { |
| 637 | .name = "pcie_brcm", |
| 638 | .id = UCLASS_PCI, |
| 639 | .ops = &brcm_pcie_ops, |
| 640 | .of_match = brcm_pcie_ids, |
| 641 | .probe = brcm_pcie_probe, |
Nicolas Saenz Julienne | df10fe8 | 2021-01-14 16:49:01 +0100 | [diff] [blame] | 642 | .remove = brcm_pcie_remove, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 643 | .of_to_plat = brcm_pcie_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 644 | .priv_auto = sizeof(struct brcm_pcie), |
Nicolas Saenz Julienne | df10fe8 | 2021-01-14 16:49:01 +0100 | [diff] [blame] | 645 | .flags = DM_FLAG_OS_PREPARE, |
Sylwester Nawrocki | 88f51f7 | 2020-05-25 13:39:58 +0200 | [diff] [blame] | 646 | }; |