1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2009 - 2019 Broadcom */
4 #include <linux/bitfield.h>
6 #include <linux/compiler.h>
7 #include <linux/delay.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.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>
30 /* BRCM_PCIE_CAP_REGS - Offset for the mandatory capability config regs */
31 #define BRCM_PCIE_CAP_REGS 0x00ac
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
38 #define PCIE_RC_CFG_PRIV1_ID_VAL3 0x043c
39 #define PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK 0xffffff
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
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
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)
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)
60 #define PCIE_MISC_RC_BAR1_CONFIG_LO 0x402c
61 #define PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK 0x1f
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
67 #define PCIE_MISC_RC_BAR3_CONFIG_LO 0x403c
68 #define PCIE_MISC_RC_BAR3_CONFIG_LO_SIZE_MASK 0x1f
70 #define PCIE_MISC_PCIE_CTRL 0x4064
71 #define PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK 0x1
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
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)
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)
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)
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
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
104 #define PCIE_EXT_CFG_DATA 0x8000
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
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
115 /* PCIe parameters */
116 #define BRCM_NUM_PCIE_OUT_WINS 0x4
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
138 /* Internal PCIe Host Controller Information.*/
143 struct pci_bus *root_bus;
144 struct device_node *np;
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
153 static int brcm_pcie_encode_ibar_size(u64 size)
155 int log2_in = ilog2(size);
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) */
163 /* Something is awry so disable */
167 static u32 brcm_pcie_mdio_form_pkt(int port, int regad, int cmd)
171 pkt |= FIELD_PREP(MDIO_PORT_MASK, port);
172 pkt |= FIELD_PREP(MDIO_REGAD_MASK, regad);
173 pkt |= FIELD_PREP(MDIO_CMD_MASK, cmd);
178 /* negative return value indicates error */
179 static int brcm_pcie_mdio_read(void __iomem *base, u8 port, u8 regad, u32 *val)
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);
188 data = readl(base + PCIE_RC_DL_MDIO_RD_DATA);
189 for (tries = 0; !MDIO_RD_DONE(data) && tries < 10; tries++) {
191 data = readl(base + PCIE_RC_DL_MDIO_RD_DATA);
194 *val = FIELD_GET(MDIO_DATA_MASK, data);
195 return MDIO_RD_DONE(data) ? 0 : -EIO;
198 /* negative return value indicates error */
199 static int brcm_pcie_mdio_write(void __iomem *base, u8 port,
200 u8 regad, u16 wrdata)
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);
210 data = readl(base + PCIE_RC_DL_MDIO_WR_DATA);
211 for (tries = 0; !MDIO_WT_DONE(data) && tries < 10; tries++) {
213 data = readl(base + PCIE_RC_DL_MDIO_WR_DATA);
216 return MDIO_WT_DONE(data) ? 0 : -EIO;
220 * Configures device for Spread Spectrum Clocking (SSC) mode; a negative
221 * return value indicates error.
223 static int brcm_pcie_set_ssc(struct brcm_pcie *pcie)
229 ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
234 ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
235 SSC_CNTL_OFFSET, &tmp);
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);
246 usleep_range(1000, 2000);
247 ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
248 SSC_STATUS_OFFSET, &tmp);
252 ssc = FIELD_GET(SSC_STATUS_SSC_MASK, tmp);
253 pll = FIELD_GET(SSC_STATUS_PLL_LOCK_MASK, tmp);
255 return ssc && pll ? 0 : -EIO;
258 /* Limits operation to a specific generation (1, 2, or 3) */
259 static void brcm_pcie_set_gen(struct brcm_pcie *pcie, int gen)
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);
264 lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen;
265 writel(lnkcap, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP);
267 lnkctl2 = (lnkctl2 & ~0xf) | gen;
268 writew(lnkctl2, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
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)
275 u32 cpu_addr_mb_high, limit_addr_mb_high;
276 phys_addr_t cpu_addr_mb, limit_addr_mb;
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));
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;
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));
295 /* Write the cpu & limit addr upper bits */
297 HWEIGHT32(PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
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));
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));
312 /* The controller is capable of serving in both RC and EP roles */
313 static bool brcm_pcie_rc_mode(struct brcm_pcie *pcie)
315 void __iomem *base = pcie->base;
316 u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
318 return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK, val);
321 static bool brcm_pcie_link_up(struct brcm_pcie *pcie)
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);
330 /* Configuration space read/write support */
331 static inline int brcm_pcie_cfg_index(int busnr, int devfn, int reg)
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)
339 static void __iomem *brcm_pcie_map_conf(struct pci_bus *bus, unsigned int devfn,
342 struct brcm_pcie *pcie = bus->sysdata;
343 void __iomem *base = pcie->base;
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;
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;
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,
362 static inline void brcm_pcie_bridge_sw_init_set(struct brcm_pcie *pcie, u32 val)
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);
371 static inline void brcm_pcie_perst_set(struct brcm_pcie *pcie, u32 val)
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);
380 static inline int brcm_pcie_get_rc_bar2_size_and_offset(struct brcm_pcie *pcie,
384 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
385 struct device *dev = pcie->dev;
386 struct resource_entry *entry;
388 entry = resource_list_first_type(&bridge->dma_ranges, IORESOURCE_MEM);
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
399 *rc_bar2_offset = -entry->offset;
400 *rc_bar2_size = 1ULL << fls64(entry->res->end - entry->res->start);
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
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
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.
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
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);
445 static int brcm_pcie_setup(struct brcm_pcie *pcie)
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;
460 /* Reset the bridge */
461 brcm_pcie_bridge_sw_init_set(pcie, 1);
463 usleep_range(100, 200);
465 /* Take the bridge out of reset */
466 brcm_pcie_bridge_sw_init_set(pcie, 0);
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);
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);
481 ret = brcm_pcie_get_rc_bar2_size_and_offset(pcie, &rc_bar2_size,
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);
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);
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);
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);
510 /* Mask all interrupts since we are not handling any yet */
511 writel(0xffffffff, pcie->base + PCIE_MSI_INTR2_MASK_SET);
513 /* clear any interrupts we find on boot */
514 writel(0xffffffff, pcie->base + PCIE_MSI_INTR2_CLR);
517 brcm_pcie_set_gen(pcie, pcie->gen);
519 /* Unassert the fundamental reset */
520 brcm_pcie_perst_set(pcie, 0);
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.
526 for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5)
529 if (!brcm_pcie_link_up(pcie)) {
530 dev_err(dev, "link down\n");
534 if (!brcm_pcie_rc_mode(pcie)) {
535 dev_err(dev, "PCIe misconfigured; is in EP mode\n");
539 resource_list_for_each_entry(entry, &bridge->windows) {
542 if (resource_type(res) != IORESOURCE_MEM)
545 if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) {
546 dev_err(pcie->dev, "too many outbound wins\n");
550 brcm_pcie_set_outbound_win(pcie, num_out_wins, res->start,
551 res->start - entry->offset,
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).
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);
566 ret = brcm_pcie_set_ssc(pcie);
570 dev_err(dev, "failed attempt to enter ssc mode\n");
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)");
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);
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.
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);
597 /* L23 is a low-power PCIe link state */
598 static void brcm_pcie_enter_l23(struct brcm_pcie *pcie)
600 void __iomem *base = pcie->base;
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);
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,
620 dev_err(pcie->dev, "failed to enter low-power link state\n");
623 static void brcm_pcie_turn_off(struct brcm_pcie *pcie)
625 void __iomem *base = pcie->base;
628 if (brcm_pcie_link_up(pcie))
629 brcm_pcie_enter_l23(pcie);
630 /* Assert fundamental reset */
631 brcm_pcie_perst_set(pcie, 1);
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);
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);
643 /* Shutdown PCIe bridge */
644 brcm_pcie_bridge_sw_init_set(pcie, 1);
647 static void __brcm_pcie_remove(struct brcm_pcie *pcie)
649 brcm_pcie_turn_off(pcie);
650 clk_disable_unprepare(pcie->clk);
654 static int brcm_pcie_remove(struct platform_device *pdev)
656 struct brcm_pcie *pcie = platform_get_drvdata(pdev);
658 pci_stop_root_bus(pcie->root_bus);
659 pci_remove_root_bus(pcie->root_bus);
660 __brcm_pcie_remove(pcie);
665 static int brcm_pcie_probe(struct platform_device *pdev)
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;
674 bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie));
678 pcie = pci_host_bridge_priv(bridge);
679 pcie->dev = &pdev->dev;
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);
687 pcie->clk = devm_clk_get_optional(&pdev->dev, "sw_pcie");
688 if (IS_ERR(pcie->clk))
689 return PTR_ERR(pcie->clk);
691 ret = of_pci_get_max_link_speed(np);
692 pcie->gen = (ret < 0) ? 0 : ret;
694 pcie->ssc = of_property_read_bool(np, "brcm,enable-ssc");
696 ret = pci_parse_request_of_pci_ranges(pcie->dev, &bridge->windows,
697 &bridge->dma_ranges, NULL);
701 ret = clk_prepare_enable(pcie->clk);
703 dev_err(&pdev->dev, "could not enable clock\n");
707 ret = brcm_pcie_setup(pcie);
711 bridge->dev.parent = &pdev->dev;
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;
718 ret = pci_scan_root_bus_bridge(bridge);
720 dev_err(pcie->dev, "Scanning root bridge failed\n");
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;
733 __brcm_pcie_remove(pcie);
737 static const struct of_device_id brcm_pcie_match[] = {
738 { .compatible = "brcm,bcm2711-pcie" },
741 MODULE_DEVICE_TABLE(of, brcm_pcie_match);
743 static struct platform_driver brcm_pcie_driver = {
744 .probe = brcm_pcie_probe,
745 .remove = brcm_pcie_remove,
748 .of_match_table = brcm_pcie_match,
751 module_platform_driver(brcm_pcie_driver);
753 MODULE_LICENSE("GPL");
754 MODULE_DESCRIPTION("Broadcom STB PCIe RC driver");
755 MODULE_AUTHOR("Broadcom");