Jim Quinlan | c045213 | 2019-12-16 12:01:09 +0100 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* Copyright (C) 2009 - 2019 Broadcom */ |
| 3 | |
| 4 | #include <linux/bitfield.h> |
| 5 | #include <linux/clk.h> |
| 6 | #include <linux/compiler.h> |
| 7 | #include <linux/delay.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/io.h> |
| 11 | #include <linux/ioport.h> |
| 12 | #include <linux/irqdomain.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/list.h> |
| 15 | #include <linux/log2.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/of_address.h> |
| 18 | #include <linux/of_irq.h> |
| 19 | #include <linux/of_pci.h> |
| 20 | #include <linux/of_platform.h> |
| 21 | #include <linux/pci.h> |
| 22 | #include <linux/printk.h> |
| 23 | #include <linux/sizes.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/types.h> |
| 27 | |
| 28 | #include "../pci.h" |
| 29 | |
| 30 | /* BRCM_PCIE_CAP_REGS - Offset for the mandatory capability config regs */ |
| 31 | #define BRCM_PCIE_CAP_REGS 0x00ac |
| 32 | |
| 33 | /* Broadcom STB PCIe Register Offsets */ |
| 34 | #define PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1 0x0188 |
| 35 | #define PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK 0xc |
| 36 | #define PCIE_RC_CFG_VENDOR_SPCIFIC_REG1_LITTLE_ENDIAN 0x0 |
| 37 | |
| 38 | #define PCIE_RC_CFG_PRIV1_ID_VAL3 0x043c |
| 39 | #define PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK 0xffffff |
| 40 | |
| 41 | #define PCIE_RC_DL_MDIO_ADDR 0x1100 |
| 42 | #define PCIE_RC_DL_MDIO_WR_DATA 0x1104 |
| 43 | #define PCIE_RC_DL_MDIO_RD_DATA 0x1108 |
| 44 | |
| 45 | #define PCIE_MISC_MISC_CTRL 0x4008 |
| 46 | #define PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK 0x1000 |
| 47 | #define PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK 0x2000 |
| 48 | #define PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK 0x300000 |
| 49 | #define PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_128 0x0 |
| 50 | #define PCIE_MISC_MISC_CTRL_SCB0_SIZE_MASK 0xf8000000 |
| 51 | |
| 52 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO 0x400c |
| 53 | #define PCIE_MEM_WIN0_LO(win) \ |
| 54 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO + ((win) * 4) |
| 55 | |
| 56 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI 0x4010 |
| 57 | #define PCIE_MEM_WIN0_HI(win) \ |
| 58 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI + ((win) * 4) |
| 59 | |
| 60 | #define PCIE_MISC_RC_BAR1_CONFIG_LO 0x402c |
| 61 | #define PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK 0x1f |
| 62 | |
| 63 | #define PCIE_MISC_RC_BAR2_CONFIG_LO 0x4034 |
| 64 | #define PCIE_MISC_RC_BAR2_CONFIG_LO_SIZE_MASK 0x1f |
| 65 | #define PCIE_MISC_RC_BAR2_CONFIG_HI 0x4038 |
| 66 | |
| 67 | #define PCIE_MISC_RC_BAR3_CONFIG_LO 0x403c |
| 68 | #define PCIE_MISC_RC_BAR3_CONFIG_LO_SIZE_MASK 0x1f |
| 69 | |
| 70 | #define PCIE_MISC_PCIE_CTRL 0x4064 |
| 71 | #define PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK 0x1 |
| 72 | |
| 73 | #define PCIE_MISC_PCIE_STATUS 0x4068 |
| 74 | #define PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK 0x80 |
| 75 | #define PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK 0x20 |
| 76 | #define PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK 0x10 |
| 77 | #define PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK 0x40 |
| 78 | |
| 79 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT 0x4070 |
| 80 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK 0xfff00000 |
| 81 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK 0xfff0 |
| 82 | #define PCIE_MEM_WIN0_BASE_LIMIT(win) \ |
| 83 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT + ((win) * 4) |
| 84 | |
| 85 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI 0x4080 |
| 86 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK 0xff |
| 87 | #define PCIE_MEM_WIN0_BASE_HI(win) \ |
| 88 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI + ((win) * 8) |
| 89 | |
| 90 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI 0x4084 |
| 91 | #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK 0xff |
| 92 | #define PCIE_MEM_WIN0_LIMIT_HI(win) \ |
| 93 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI + ((win) * 8) |
| 94 | |
| 95 | #define PCIE_MISC_HARD_PCIE_HARD_DEBUG 0x4204 |
| 96 | #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK 0x2 |
| 97 | #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x08000000 |
| 98 | |
| 99 | #define PCIE_MSI_INTR2_STATUS 0x4500 |
| 100 | #define PCIE_MSI_INTR2_CLR 0x4508 |
| 101 | #define PCIE_MSI_INTR2_MASK_SET 0x4510 |
| 102 | #define PCIE_MSI_INTR2_MASK_CLR 0x4514 |
| 103 | |
| 104 | #define PCIE_EXT_CFG_DATA 0x8000 |
| 105 | |
| 106 | #define PCIE_EXT_CFG_INDEX 0x9000 |
| 107 | #define PCIE_EXT_BUSNUM_SHIFT 20 |
| 108 | #define PCIE_EXT_SLOT_SHIFT 15 |
| 109 | #define PCIE_EXT_FUNC_SHIFT 12 |
| 110 | |
| 111 | #define PCIE_RGR1_SW_INIT_1 0x9210 |
| 112 | #define PCIE_RGR1_SW_INIT_1_PERST_MASK 0x1 |
| 113 | #define PCIE_RGR1_SW_INIT_1_INIT_MASK 0x2 |
| 114 | |
| 115 | /* PCIe parameters */ |
| 116 | #define BRCM_NUM_PCIE_OUT_WINS 0x4 |
| 117 | |
| 118 | /* MDIO registers */ |
| 119 | #define MDIO_PORT0 0x0 |
| 120 | #define MDIO_DATA_MASK 0x7fffffff |
| 121 | #define MDIO_PORT_MASK 0xf0000 |
| 122 | #define MDIO_REGAD_MASK 0xffff |
| 123 | #define MDIO_CMD_MASK 0xfff00000 |
| 124 | #define MDIO_CMD_READ 0x1 |
| 125 | #define MDIO_CMD_WRITE 0x0 |
| 126 | #define MDIO_DATA_DONE_MASK 0x80000000 |
| 127 | #define MDIO_RD_DONE(x) (((x) & MDIO_DATA_DONE_MASK) ? 1 : 0) |
| 128 | #define MDIO_WT_DONE(x) (((x) & MDIO_DATA_DONE_MASK) ? 0 : 1) |
| 129 | #define SSC_REGS_ADDR 0x1100 |
| 130 | #define SET_ADDR_OFFSET 0x1f |
| 131 | #define SSC_CNTL_OFFSET 0x2 |
| 132 | #define SSC_CNTL_OVRD_EN_MASK 0x8000 |
| 133 | #define SSC_CNTL_OVRD_VAL_MASK 0x4000 |
| 134 | #define SSC_STATUS_OFFSET 0x1 |
| 135 | #define SSC_STATUS_SSC_MASK 0x400 |
| 136 | #define SSC_STATUS_PLL_LOCK_MASK 0x800 |
| 137 | |
| 138 | /* Internal PCIe Host Controller Information.*/ |
| 139 | struct brcm_pcie { |
| 140 | struct device *dev; |
| 141 | void __iomem *base; |
| 142 | struct clk *clk; |
| 143 | struct pci_bus *root_bus; |
| 144 | struct device_node *np; |
| 145 | bool ssc; |
| 146 | int gen; |
| 147 | }; |
| 148 | |
| 149 | /* |
| 150 | * This is to convert the size of the inbound "BAR" region to the |
| 151 | * non-linear values of PCIE_X_MISC_RC_BAR[123]_CONFIG_LO.SIZE |
| 152 | */ |
| 153 | static int brcm_pcie_encode_ibar_size(u64 size) |
| 154 | { |
| 155 | int log2_in = ilog2(size); |
| 156 | |
| 157 | if (log2_in >= 12 && log2_in <= 15) |
| 158 | /* Covers 4KB to 32KB (inclusive) */ |
| 159 | return (log2_in - 12) + 0x1c; |
| 160 | else if (log2_in >= 16 && log2_in <= 35) |
| 161 | /* Covers 64KB to 32GB, (inclusive) */ |
| 162 | return log2_in - 15; |
| 163 | /* Something is awry so disable */ |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static u32 brcm_pcie_mdio_form_pkt(int port, int regad, int cmd) |
| 168 | { |
| 169 | u32 pkt = 0; |
| 170 | |
| 171 | pkt |= FIELD_PREP(MDIO_PORT_MASK, port); |
| 172 | pkt |= FIELD_PREP(MDIO_REGAD_MASK, regad); |
| 173 | pkt |= FIELD_PREP(MDIO_CMD_MASK, cmd); |
| 174 | |
| 175 | return pkt; |
| 176 | } |
| 177 | |
| 178 | /* negative return value indicates error */ |
| 179 | static int brcm_pcie_mdio_read(void __iomem *base, u8 port, u8 regad, u32 *val) |
| 180 | { |
| 181 | int tries; |
| 182 | u32 data; |
| 183 | |
| 184 | writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_READ), |
| 185 | base + PCIE_RC_DL_MDIO_ADDR); |
| 186 | readl(base + PCIE_RC_DL_MDIO_ADDR); |
| 187 | |
| 188 | data = readl(base + PCIE_RC_DL_MDIO_RD_DATA); |
| 189 | for (tries = 0; !MDIO_RD_DONE(data) && tries < 10; tries++) { |
| 190 | udelay(10); |
| 191 | data = readl(base + PCIE_RC_DL_MDIO_RD_DATA); |
| 192 | } |
| 193 | |
| 194 | *val = FIELD_GET(MDIO_DATA_MASK, data); |
| 195 | return MDIO_RD_DONE(data) ? 0 : -EIO; |
| 196 | } |
| 197 | |
| 198 | /* negative return value indicates error */ |
| 199 | static int brcm_pcie_mdio_write(void __iomem *base, u8 port, |
| 200 | u8 regad, u16 wrdata) |
| 201 | { |
| 202 | int tries; |
| 203 | u32 data; |
| 204 | |
| 205 | writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_WRITE), |
| 206 | base + PCIE_RC_DL_MDIO_ADDR); |
| 207 | readl(base + PCIE_RC_DL_MDIO_ADDR); |
| 208 | writel(MDIO_DATA_DONE_MASK | wrdata, base + PCIE_RC_DL_MDIO_WR_DATA); |
| 209 | |
| 210 | data = readl(base + PCIE_RC_DL_MDIO_WR_DATA); |
| 211 | for (tries = 0; !MDIO_WT_DONE(data) && tries < 10; tries++) { |
| 212 | udelay(10); |
| 213 | data = readl(base + PCIE_RC_DL_MDIO_WR_DATA); |
| 214 | } |
| 215 | |
| 216 | return MDIO_WT_DONE(data) ? 0 : -EIO; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Configures device for Spread Spectrum Clocking (SSC) mode; a negative |
| 221 | * return value indicates error. |
| 222 | */ |
| 223 | static int brcm_pcie_set_ssc(struct brcm_pcie *pcie) |
| 224 | { |
| 225 | int pll, ssc; |
| 226 | int ret; |
| 227 | u32 tmp; |
| 228 | |
| 229 | ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET, |
| 230 | SSC_REGS_ADDR); |
| 231 | if (ret < 0) |
| 232 | return ret; |
| 233 | |
| 234 | ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0, |
| 235 | SSC_CNTL_OFFSET, &tmp); |
| 236 | if (ret < 0) |
| 237 | return ret; |
| 238 | |
| 239 | u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_EN_MASK); |
| 240 | u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_VAL_MASK); |
| 241 | ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, |
| 242 | SSC_CNTL_OFFSET, tmp); |
| 243 | if (ret < 0) |
| 244 | return ret; |
| 245 | |
| 246 | usleep_range(1000, 2000); |
| 247 | ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0, |
| 248 | SSC_STATUS_OFFSET, &tmp); |
| 249 | if (ret < 0) |
| 250 | return ret; |
| 251 | |
| 252 | ssc = FIELD_GET(SSC_STATUS_SSC_MASK, tmp); |
| 253 | pll = FIELD_GET(SSC_STATUS_PLL_LOCK_MASK, tmp); |
| 254 | |
| 255 | return ssc && pll ? 0 : -EIO; |
| 256 | } |
| 257 | |
| 258 | /* Limits operation to a specific generation (1, 2, or 3) */ |
| 259 | static void brcm_pcie_set_gen(struct brcm_pcie *pcie, int gen) |
| 260 | { |
| 261 | u16 lnkctl2 = readw(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2); |
| 262 | u32 lnkcap = readl(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP); |
| 263 | |
| 264 | lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen; |
| 265 | writel(lnkcap, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP); |
| 266 | |
| 267 | lnkctl2 = (lnkctl2 & ~0xf) | gen; |
| 268 | writew(lnkctl2, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2); |
| 269 | } |
| 270 | |
| 271 | static void brcm_pcie_set_outbound_win(struct brcm_pcie *pcie, |
| 272 | unsigned int win, u64 cpu_addr, |
| 273 | u64 pcie_addr, u64 size) |
| 274 | { |
| 275 | u32 cpu_addr_mb_high, limit_addr_mb_high; |
| 276 | phys_addr_t cpu_addr_mb, limit_addr_mb; |
| 277 | int high_addr_shift; |
| 278 | u32 tmp; |
| 279 | |
| 280 | /* Set the base of the pcie_addr window */ |
| 281 | writel(lower_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_LO(win)); |
| 282 | writel(upper_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_HI(win)); |
| 283 | |
| 284 | /* Write the addr base & limit lower bits (in MBs) */ |
| 285 | cpu_addr_mb = cpu_addr / SZ_1M; |
| 286 | limit_addr_mb = (cpu_addr + size - 1) / SZ_1M; |
| 287 | |
| 288 | tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win)); |
| 289 | u32p_replace_bits(&tmp, cpu_addr_mb, |
| 290 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK); |
| 291 | u32p_replace_bits(&tmp, limit_addr_mb, |
| 292 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK); |
| 293 | writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win)); |
| 294 | |
| 295 | /* Write the cpu & limit addr upper bits */ |
| 296 | high_addr_shift = |
| 297 | HWEIGHT32(PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK); |
| 298 | |
| 299 | cpu_addr_mb_high = cpu_addr_mb >> high_addr_shift; |
| 300 | tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_HI(win)); |
| 301 | u32p_replace_bits(&tmp, cpu_addr_mb_high, |
| 302 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK); |
| 303 | writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_HI(win)); |
| 304 | |
| 305 | limit_addr_mb_high = limit_addr_mb >> high_addr_shift; |
| 306 | tmp = readl(pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win)); |
| 307 | u32p_replace_bits(&tmp, limit_addr_mb_high, |
| 308 | PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK); |
| 309 | writel(tmp, pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win)); |
| 310 | } |
| 311 | |
| 312 | /* The controller is capable of serving in both RC and EP roles */ |
| 313 | static bool brcm_pcie_rc_mode(struct brcm_pcie *pcie) |
| 314 | { |
| 315 | void __iomem *base = pcie->base; |
| 316 | u32 val = readl(base + PCIE_MISC_PCIE_STATUS); |
| 317 | |
| 318 | return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK, val); |
| 319 | } |
| 320 | |
| 321 | static bool brcm_pcie_link_up(struct brcm_pcie *pcie) |
| 322 | { |
| 323 | u32 val = readl(pcie->base + PCIE_MISC_PCIE_STATUS); |
| 324 | u32 dla = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK, val); |
| 325 | u32 plu = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK, val); |
| 326 | |
| 327 | return dla && plu; |
| 328 | } |
| 329 | |
| 330 | /* Configuration space read/write support */ |
| 331 | static inline int brcm_pcie_cfg_index(int busnr, int devfn, int reg) |
| 332 | { |
| 333 | return ((PCI_SLOT(devfn) & 0x1f) << PCIE_EXT_SLOT_SHIFT) |
| 334 | | ((PCI_FUNC(devfn) & 0x07) << PCIE_EXT_FUNC_SHIFT) |
| 335 | | (busnr << PCIE_EXT_BUSNUM_SHIFT) |
| 336 | | (reg & ~3); |
| 337 | } |
| 338 | |
| 339 | static void __iomem *brcm_pcie_map_conf(struct pci_bus *bus, unsigned int devfn, |
| 340 | int where) |
| 341 | { |
| 342 | struct brcm_pcie *pcie = bus->sysdata; |
| 343 | void __iomem *base = pcie->base; |
| 344 | int idx; |
| 345 | |
| 346 | /* Accesses to the RC go right to the RC registers if slot==0 */ |
| 347 | if (pci_is_root_bus(bus)) |
| 348 | return PCI_SLOT(devfn) ? NULL : base + where; |
| 349 | |
| 350 | /* For devices, write to the config space index register */ |
| 351 | idx = brcm_pcie_cfg_index(bus->number, devfn, 0); |
| 352 | writel(idx, pcie->base + PCIE_EXT_CFG_INDEX); |
| 353 | return base + PCIE_EXT_CFG_DATA + where; |
| 354 | } |
| 355 | |
| 356 | static struct pci_ops brcm_pcie_ops = { |
| 357 | .map_bus = brcm_pcie_map_conf, |
| 358 | .read = pci_generic_config_read, |
| 359 | .write = pci_generic_config_write, |
| 360 | }; |
| 361 | |
| 362 | static inline void brcm_pcie_bridge_sw_init_set(struct brcm_pcie *pcie, u32 val) |
| 363 | { |
| 364 | u32 tmp; |
| 365 | |
| 366 | tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1); |
| 367 | u32p_replace_bits(&tmp, val, PCIE_RGR1_SW_INIT_1_INIT_MASK); |
| 368 | writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1); |
| 369 | } |
| 370 | |
| 371 | static inline void brcm_pcie_perst_set(struct brcm_pcie *pcie, u32 val) |
| 372 | { |
| 373 | u32 tmp; |
| 374 | |
| 375 | tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1); |
| 376 | u32p_replace_bits(&tmp, val, PCIE_RGR1_SW_INIT_1_PERST_MASK); |
| 377 | writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1); |
| 378 | } |
| 379 | |
| 380 | static inline int brcm_pcie_get_rc_bar2_size_and_offset(struct brcm_pcie *pcie, |
| 381 | u64 *rc_bar2_size, |
| 382 | u64 *rc_bar2_offset) |
| 383 | { |
| 384 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); |
| 385 | struct device *dev = pcie->dev; |
| 386 | struct resource_entry *entry; |
| 387 | |
| 388 | entry = resource_list_first_type(&bridge->dma_ranges, IORESOURCE_MEM); |
| 389 | if (!entry) |
| 390 | return -ENODEV; |
| 391 | |
| 392 | |
| 393 | /* |
| 394 | * The controller expects the inbound window offset to be calculated as |
| 395 | * the difference between PCIe's address space and CPU's. The offset |
| 396 | * provided by the firmware is calculated the opposite way, so we |
| 397 | * negate it. |
| 398 | */ |
| 399 | *rc_bar2_offset = -entry->offset; |
| 400 | *rc_bar2_size = 1ULL << fls64(entry->res->end - entry->res->start); |
| 401 | |
| 402 | /* |
| 403 | * We validate the inbound memory view even though we should trust |
| 404 | * whatever the device-tree provides. This is because of an HW issue on |
| 405 | * early Raspberry Pi 4's revisions (bcm2711). It turns out its |
| 406 | * firmware has to dynamically edit dma-ranges due to a bug on the |
| 407 | * PCIe controller integration, which prohibits any access above the |
| 408 | * lower 3GB of memory. Given this, we decided to keep the dma-ranges |
| 409 | * in check, avoiding hard to debug device-tree related issues in the |
| 410 | * future: |
| 411 | * |
| 412 | * The PCIe host controller by design must set the inbound viewport to |
| 413 | * be a contiguous arrangement of all of the system's memory. In |
| 414 | * addition, its size mut be a power of two. To further complicate |
| 415 | * matters, the viewport must start on a pcie-address that is aligned |
| 416 | * on a multiple of its size. If a portion of the viewport does not |
| 417 | * represent system memory -- e.g. 3GB of memory requires a 4GB |
| 418 | * viewport -- we can map the outbound memory in or after 3GB and even |
| 419 | * though the viewport will overlap the outbound memory the controller |
| 420 | * will know to send outbound memory downstream and everything else |
| 421 | * upstream. |
| 422 | * |
| 423 | * For example: |
| 424 | * |
| 425 | * - The best-case scenario, memory up to 3GB, is to place the inbound |
| 426 | * region in the first 4GB of pcie-space, as some legacy devices can |
| 427 | * only address 32bits. We would also like to put the MSI under 4GB |
| 428 | * as well, since some devices require a 32bit MSI target address. |
| 429 | * |
| 430 | * - If the system memory is 4GB or larger we cannot start the inbound |
| 431 | * region at location 0 (since we have to allow some space for |
| 432 | * outbound memory @ 3GB). So instead it will start at the 1x |
| 433 | * multiple of its size |
| 434 | */ |
| 435 | if (!*rc_bar2_size || *rc_bar2_offset % *rc_bar2_size || |
| 436 | (*rc_bar2_offset < SZ_4G && *rc_bar2_offset > SZ_2G)) { |
| 437 | dev_err(dev, "Invalid rc_bar2_offset/size: size 0x%llx, off 0x%llx\n", |
| 438 | *rc_bar2_size, *rc_bar2_offset); |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | static int brcm_pcie_setup(struct brcm_pcie *pcie) |
| 446 | { |
| 447 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); |
| 448 | u64 rc_bar2_offset, rc_bar2_size; |
| 449 | void __iomem *base = pcie->base; |
| 450 | struct device *dev = pcie->dev; |
| 451 | struct resource_entry *entry; |
| 452 | unsigned int scb_size_val; |
| 453 | bool ssc_good = false; |
| 454 | struct resource *res; |
| 455 | int num_out_wins = 0; |
| 456 | u16 nlw, cls, lnksta; |
| 457 | int i, ret; |
| 458 | u32 tmp; |
| 459 | |
| 460 | /* Reset the bridge */ |
| 461 | brcm_pcie_bridge_sw_init_set(pcie, 1); |
| 462 | |
| 463 | usleep_range(100, 200); |
| 464 | |
| 465 | /* Take the bridge out of reset */ |
| 466 | brcm_pcie_bridge_sw_init_set(pcie, 0); |
| 467 | |
| 468 | tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG); |
| 469 | tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK; |
| 470 | writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG); |
| 471 | /* Wait for SerDes to be stable */ |
| 472 | usleep_range(100, 200); |
| 473 | |
| 474 | /* Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN */ |
| 475 | u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK); |
| 476 | u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK); |
| 477 | u32p_replace_bits(&tmp, PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_128, |
| 478 | PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK); |
| 479 | writel(tmp, base + PCIE_MISC_MISC_CTRL); |
| 480 | |
| 481 | ret = brcm_pcie_get_rc_bar2_size_and_offset(pcie, &rc_bar2_size, |
| 482 | &rc_bar2_offset); |
| 483 | if (ret) |
| 484 | return ret; |
| 485 | |
| 486 | tmp = lower_32_bits(rc_bar2_offset); |
| 487 | u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(rc_bar2_size), |
| 488 | PCIE_MISC_RC_BAR2_CONFIG_LO_SIZE_MASK); |
| 489 | writel(tmp, base + PCIE_MISC_RC_BAR2_CONFIG_LO); |
| 490 | writel(upper_32_bits(rc_bar2_offset), |
| 491 | base + PCIE_MISC_RC_BAR2_CONFIG_HI); |
| 492 | |
| 493 | scb_size_val = rc_bar2_size ? |
| 494 | ilog2(rc_bar2_size) - 15 : 0xf; /* 0xf is 1GB */ |
| 495 | tmp = readl(base + PCIE_MISC_MISC_CTRL); |
| 496 | u32p_replace_bits(&tmp, scb_size_val, |
| 497 | PCIE_MISC_MISC_CTRL_SCB0_SIZE_MASK); |
| 498 | writel(tmp, base + PCIE_MISC_MISC_CTRL); |
| 499 | |
| 500 | /* disable the PCIe->GISB memory window (RC_BAR1) */ |
| 501 | tmp = readl(base + PCIE_MISC_RC_BAR1_CONFIG_LO); |
| 502 | tmp &= ~PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK; |
| 503 | writel(tmp, base + PCIE_MISC_RC_BAR1_CONFIG_LO); |
| 504 | |
| 505 | /* disable the PCIe->SCB memory window (RC_BAR3) */ |
| 506 | tmp = readl(base + PCIE_MISC_RC_BAR3_CONFIG_LO); |
| 507 | tmp &= ~PCIE_MISC_RC_BAR3_CONFIG_LO_SIZE_MASK; |
| 508 | writel(tmp, base + PCIE_MISC_RC_BAR3_CONFIG_LO); |
| 509 | |
| 510 | /* Mask all interrupts since we are not handling any yet */ |
| 511 | writel(0xffffffff, pcie->base + PCIE_MSI_INTR2_MASK_SET); |
| 512 | |
| 513 | /* clear any interrupts we find on boot */ |
| 514 | writel(0xffffffff, pcie->base + PCIE_MSI_INTR2_CLR); |
| 515 | |
| 516 | if (pcie->gen) |
| 517 | brcm_pcie_set_gen(pcie, pcie->gen); |
| 518 | |
| 519 | /* Unassert the fundamental reset */ |
| 520 | brcm_pcie_perst_set(pcie, 0); |
| 521 | |
| 522 | /* |
| 523 | * Give the RC/EP time to wake up, before trying to configure RC. |
| 524 | * Intermittently check status for link-up, up to a total of 100ms. |
| 525 | */ |
| 526 | for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5) |
| 527 | msleep(5); |
| 528 | |
| 529 | if (!brcm_pcie_link_up(pcie)) { |
| 530 | dev_err(dev, "link down\n"); |
| 531 | return -ENODEV; |
| 532 | } |
| 533 | |
| 534 | if (!brcm_pcie_rc_mode(pcie)) { |
| 535 | dev_err(dev, "PCIe misconfigured; is in EP mode\n"); |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | resource_list_for_each_entry(entry, &bridge->windows) { |
| 540 | res = entry->res; |
| 541 | |
| 542 | if (resource_type(res) != IORESOURCE_MEM) |
| 543 | continue; |
| 544 | |
| 545 | if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) { |
| 546 | dev_err(pcie->dev, "too many outbound wins\n"); |
| 547 | return -EINVAL; |
| 548 | } |
| 549 | |
| 550 | brcm_pcie_set_outbound_win(pcie, num_out_wins, res->start, |
| 551 | res->start - entry->offset, |
| 552 | resource_size(res)); |
| 553 | num_out_wins++; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * For config space accesses on the RC, show the right class for |
| 558 | * a PCIe-PCIe bridge (the default setting is to be EP mode). |
| 559 | */ |
| 560 | tmp = readl(base + PCIE_RC_CFG_PRIV1_ID_VAL3); |
| 561 | u32p_replace_bits(&tmp, 0x060400, |
| 562 | PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK); |
| 563 | writel(tmp, base + PCIE_RC_CFG_PRIV1_ID_VAL3); |
| 564 | |
| 565 | if (pcie->ssc) { |
| 566 | ret = brcm_pcie_set_ssc(pcie); |
| 567 | if (ret == 0) |
| 568 | ssc_good = true; |
| 569 | else |
| 570 | dev_err(dev, "failed attempt to enter ssc mode\n"); |
| 571 | } |
| 572 | |
| 573 | lnksta = readw(base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKSTA); |
| 574 | cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta); |
| 575 | nlw = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta); |
| 576 | dev_info(dev, "link up, %s x%u %s\n", |
| 577 | PCIE_SPEED2STR(cls + PCI_SPEED_133MHz_PCIX_533), |
| 578 | nlw, ssc_good ? "(SSC)" : "(!SSC)"); |
| 579 | |
| 580 | /* PCIe->SCB endian mode for BAR */ |
| 581 | tmp = readl(base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1); |
| 582 | u32p_replace_bits(&tmp, PCIE_RC_CFG_VENDOR_SPCIFIC_REG1_LITTLE_ENDIAN, |
| 583 | PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK); |
| 584 | writel(tmp, base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1); |
| 585 | |
| 586 | /* |
| 587 | * Refclk from RC should be gated with CLKREQ# input when ASPM L0s,L1 |
| 588 | * is enabled => setting the CLKREQ_DEBUG_ENABLE field to 1. |
| 589 | */ |
| 590 | tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG); |
| 591 | tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK; |
| 592 | writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | /* L23 is a low-power PCIe link state */ |
| 598 | static void brcm_pcie_enter_l23(struct brcm_pcie *pcie) |
| 599 | { |
| 600 | void __iomem *base = pcie->base; |
| 601 | int l23, i; |
| 602 | u32 tmp; |
| 603 | |
| 604 | /* Assert request for L23 */ |
| 605 | tmp = readl(base + PCIE_MISC_PCIE_CTRL); |
| 606 | u32p_replace_bits(&tmp, 1, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK); |
| 607 | writel(tmp, base + PCIE_MISC_PCIE_CTRL); |
| 608 | |
| 609 | /* Wait up to 36 msec for L23 */ |
| 610 | tmp = readl(base + PCIE_MISC_PCIE_STATUS); |
| 611 | l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK, tmp); |
| 612 | for (i = 0; i < 15 && !l23; i++) { |
| 613 | usleep_range(2000, 2400); |
| 614 | tmp = readl(base + PCIE_MISC_PCIE_STATUS); |
| 615 | l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK, |
| 616 | tmp); |
| 617 | } |
| 618 | |
| 619 | if (!l23) |
| 620 | dev_err(pcie->dev, "failed to enter low-power link state\n"); |
| 621 | } |
| 622 | |
| 623 | static void brcm_pcie_turn_off(struct brcm_pcie *pcie) |
| 624 | { |
| 625 | void __iomem *base = pcie->base; |
| 626 | int tmp; |
| 627 | |
| 628 | if (brcm_pcie_link_up(pcie)) |
| 629 | brcm_pcie_enter_l23(pcie); |
| 630 | /* Assert fundamental reset */ |
| 631 | brcm_pcie_perst_set(pcie, 1); |
| 632 | |
| 633 | /* Deassert request for L23 in case it was asserted */ |
| 634 | tmp = readl(base + PCIE_MISC_PCIE_CTRL); |
| 635 | u32p_replace_bits(&tmp, 0, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK); |
| 636 | writel(tmp, base + PCIE_MISC_PCIE_CTRL); |
| 637 | |
| 638 | /* Turn off SerDes */ |
| 639 | tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG); |
| 640 | u32p_replace_bits(&tmp, 1, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK); |
| 641 | writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG); |
| 642 | |
| 643 | /* Shutdown PCIe bridge */ |
| 644 | brcm_pcie_bridge_sw_init_set(pcie, 1); |
| 645 | } |
| 646 | |
| 647 | static void __brcm_pcie_remove(struct brcm_pcie *pcie) |
| 648 | { |
| 649 | brcm_pcie_turn_off(pcie); |
| 650 | clk_disable_unprepare(pcie->clk); |
| 651 | clk_put(pcie->clk); |
| 652 | } |
| 653 | |
| 654 | static int brcm_pcie_remove(struct platform_device *pdev) |
| 655 | { |
| 656 | struct brcm_pcie *pcie = platform_get_drvdata(pdev); |
| 657 | |
| 658 | pci_stop_root_bus(pcie->root_bus); |
| 659 | pci_remove_root_bus(pcie->root_bus); |
| 660 | __brcm_pcie_remove(pcie); |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int brcm_pcie_probe(struct platform_device *pdev) |
| 666 | { |
| 667 | struct device_node *np = pdev->dev.of_node; |
| 668 | struct pci_host_bridge *bridge; |
| 669 | struct brcm_pcie *pcie; |
| 670 | struct pci_bus *child; |
| 671 | struct resource *res; |
| 672 | int ret; |
| 673 | |
| 674 | bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie)); |
| 675 | if (!bridge) |
| 676 | return -ENOMEM; |
| 677 | |
| 678 | pcie = pci_host_bridge_priv(bridge); |
| 679 | pcie->dev = &pdev->dev; |
| 680 | pcie->np = np; |
| 681 | |
| 682 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 683 | pcie->base = devm_ioremap_resource(&pdev->dev, res); |
| 684 | if (IS_ERR(pcie->base)) |
| 685 | return PTR_ERR(pcie->base); |
| 686 | |
| 687 | pcie->clk = devm_clk_get_optional(&pdev->dev, "sw_pcie"); |
| 688 | if (IS_ERR(pcie->clk)) |
| 689 | return PTR_ERR(pcie->clk); |
| 690 | |
| 691 | ret = of_pci_get_max_link_speed(np); |
| 692 | pcie->gen = (ret < 0) ? 0 : ret; |
| 693 | |
| 694 | pcie->ssc = of_property_read_bool(np, "brcm,enable-ssc"); |
| 695 | |
| 696 | ret = pci_parse_request_of_pci_ranges(pcie->dev, &bridge->windows, |
| 697 | &bridge->dma_ranges, NULL); |
| 698 | if (ret) |
| 699 | return ret; |
| 700 | |
| 701 | ret = clk_prepare_enable(pcie->clk); |
| 702 | if (ret) { |
| 703 | dev_err(&pdev->dev, "could not enable clock\n"); |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | ret = brcm_pcie_setup(pcie); |
| 708 | if (ret) |
| 709 | goto fail; |
| 710 | |
| 711 | bridge->dev.parent = &pdev->dev; |
| 712 | bridge->busnr = 0; |
| 713 | bridge->ops = &brcm_pcie_ops; |
| 714 | bridge->sysdata = pcie; |
| 715 | bridge->map_irq = of_irq_parse_and_map_pci; |
| 716 | bridge->swizzle_irq = pci_common_swizzle; |
| 717 | |
| 718 | ret = pci_scan_root_bus_bridge(bridge); |
| 719 | if (ret < 0) { |
| 720 | dev_err(pcie->dev, "Scanning root bridge failed\n"); |
| 721 | goto fail; |
| 722 | } |
| 723 | |
| 724 | pci_assign_unassigned_bus_resources(bridge->bus); |
| 725 | list_for_each_entry(child, &bridge->bus->children, node) |
| 726 | pcie_bus_configure_settings(child); |
| 727 | pci_bus_add_devices(bridge->bus); |
| 728 | platform_set_drvdata(pdev, pcie); |
| 729 | pcie->root_bus = bridge->bus; |
| 730 | |
| 731 | return 0; |
| 732 | fail: |
| 733 | __brcm_pcie_remove(pcie); |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | static const struct of_device_id brcm_pcie_match[] = { |
| 738 | { .compatible = "brcm,bcm2711-pcie" }, |
| 739 | {}, |
| 740 | }; |
| 741 | MODULE_DEVICE_TABLE(of, brcm_pcie_match); |
| 742 | |
| 743 | static struct platform_driver brcm_pcie_driver = { |
| 744 | .probe = brcm_pcie_probe, |
| 745 | .remove = brcm_pcie_remove, |
| 746 | .driver = { |
| 747 | .name = "brcm-pcie", |
| 748 | .of_match_table = brcm_pcie_match, |
| 749 | }, |
| 750 | }; |
| 751 | module_platform_driver(brcm_pcie_driver); |
| 752 | |
| 753 | MODULE_LICENSE("GPL"); |
| 754 | MODULE_DESCRIPTION("Broadcom STB PCIe RC driver"); |
| 755 | MODULE_AUTHOR("Broadcom"); |