1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2009 - 2019 Broadcom */
4 #include <linux/bitfield.h>
5 #include <linux/bitops.h>
7 #include <linux/compiler.h>
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/ioport.h>
14 #include <linux/irqchip/chained_irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/kernel.h>
17 #include <linux/kthread.h>
18 #include <linux/list.h>
19 #include <linux/log2.h>
20 #include <linux/module.h>
21 #include <linux/msi.h>
22 #include <linux/of_address.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_pci.h>
25 #include <linux/of_platform.h>
26 #include <linux/pci.h>
27 #include <linux/pci-ecam.h>
28 #include <linux/printk.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/reset.h>
31 #include <linux/sizes.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/types.h>
38 /* BRCM_PCIE_CAP_REGS - Offset for the mandatory capability config regs */
39 #define BRCM_PCIE_CAP_REGS 0x00ac
41 /* Broadcom STB PCIe Register Offsets */
42 #define PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1 0x0188
43 #define PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK 0xc
44 #define PCIE_RC_CFG_VENDOR_SPCIFIC_REG1_LITTLE_ENDIAN 0x0
46 #define PCIE_RC_CFG_PRIV1_ID_VAL3 0x043c
47 #define PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK 0xffffff
49 #define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY 0x04dc
50 #define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_ASPM_SUPPORT_MASK 0xc00
52 #define PCIE_RC_TL_VDM_CTL0 0x0a20
53 #define PCIE_RC_TL_VDM_CTL0_VDM_ENABLED_MASK 0x10000
54 #define PCIE_RC_TL_VDM_CTL0_VDM_IGNORETAG_MASK 0x20000
55 #define PCIE_RC_TL_VDM_CTL0_VDM_IGNOREVNDRID_MASK 0x40000
57 #define PCIE_RC_TL_VDM_CTL1 0x0a0c
58 #define PCIE_RC_TL_VDM_CTL1_VDM_VNDRID0_MASK 0x0000ffff
59 #define PCIE_RC_TL_VDM_CTL1_VDM_VNDRID1_MASK 0xffff0000
61 #define PCIE_RC_DL_MDIO_ADDR 0x1100
62 #define PCIE_RC_DL_MDIO_WR_DATA 0x1104
63 #define PCIE_RC_DL_MDIO_RD_DATA 0x1108
65 #define PCIE_RC_PL_PHY_CTL_15 0x184c
66 #define PCIE_RC_PL_PHY_CTL_15_DIS_PLL_PD_MASK 0x400000
67 #define PCIE_RC_PL_PHY_CTL_15_PM_CLK_PERIOD_MASK 0xff
69 #define PCIE_MISC_MISC_CTRL 0x4008
70 #define PCIE_MISC_MISC_CTRL_PCIE_RCB_64B_MODE_MASK 0x80
71 #define PCIE_MISC_MISC_CTRL_PCIE_RCB_MPS_MODE_MASK 0x400
72 #define PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK 0x1000
73 #define PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK 0x2000
74 #define PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK 0x300000
76 #define PCIE_MISC_MISC_CTRL_SCB0_SIZE_MASK 0xf8000000
77 #define PCIE_MISC_MISC_CTRL_SCB1_SIZE_MASK 0x07c00000
78 #define PCIE_MISC_MISC_CTRL_SCB2_SIZE_MASK 0x0000001f
79 #define SCB_SIZE_MASK(x) PCIE_MISC_MISC_CTRL_SCB ## x ## _SIZE_MASK
81 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO 0x400c
82 #define PCIE_MEM_WIN0_LO(win) \
83 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO + ((win) * 8)
85 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI 0x4010
86 #define PCIE_MEM_WIN0_HI(win) \
87 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI + ((win) * 8)
89 #define PCIE_MISC_RC_BAR1_CONFIG_LO 0x402c
90 #define PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK 0x1f
91 #define PCIE_MISC_RC_BAR1_CONFIG_HI 0x4030
93 #define PCIE_MISC_RC_BAR2_CONFIG_LO 0x4034
94 #define PCIE_MISC_RC_BAR2_CONFIG_LO_SIZE_MASK 0x1f
95 #define PCIE_MISC_RC_BAR2_CONFIG_HI 0x4038
97 #define PCIE_MISC_RC_BAR3_CONFIG_LO 0x403c
98 #define PCIE_MISC_RC_BAR3_CONFIG_LO_SIZE_MASK 0x1f
99 #define PCIE_MISC_RC_BAR3_CONFIG_HI 0x4040
101 #define PCIE_MISC_MSI_BAR_CONFIG_LO 0x4044
102 #define PCIE_MISC_MSI_BAR_CONFIG_HI 0x4048
104 #define PCIE_MISC_MSI_DATA_CONFIG 0x404c
105 #define PCIE_MISC_MSI_DATA_CONFIG_VAL_32 0xffe06540
106 #define PCIE_MISC_MSI_DATA_CONFIG_VAL_8 0xfff86540
108 #define PCIE_MISC_RC_CONFIG_RETRY_TIMEOUT 0x405c
110 #define PCIE_MISC_PCIE_CTRL 0x4064
111 #define PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK 0x1
112 #define PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK 0x4
114 #define PCIE_MISC_PCIE_STATUS 0x4068
115 #define PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK 0x80
116 #define PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK_2712 0x40
117 #define PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK 0x20
118 #define PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK 0x10
119 #define PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK 0x40
121 #define PCIE_MISC_REVISION 0x406c
122 #define BRCM_PCIE_HW_REV_33 0x0303
123 #define BRCM_PCIE_HW_REV_3_20 0x0320
125 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT 0x4070
126 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK 0xfff00000
127 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK 0xfff0
128 #define PCIE_MEM_WIN0_BASE_LIMIT(win) \
129 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT + ((win) * 4)
131 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI 0x4080
132 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK 0xff
133 #define PCIE_MEM_WIN0_BASE_HI(win) \
134 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI + ((win) * 8)
136 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI 0x4084
137 #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK 0xff
138 #define PCIE_MEM_WIN0_LIMIT_HI(win) \
139 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI + ((win) * 8)
141 #define PCIE_MISC_HARD_PCIE_HARD_DEBUG pcie->reg_offsets[PCIE_HARD_DEBUG]
142 #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK 0x2
143 #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x08000000
144 #define PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x00800000
145 #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK 0x00200000
147 #define PCIE_MISC_CTRL_1 0x40A0
148 #define PCIE_MISC_CTRL_1_OUTBOUND_TC_MASK 0xf
149 #define PCIE_MISC_CTRL_1_OUTBOUND_NO_SNOOP_MASK BIT(3)
150 #define PCIE_MISC_CTRL_1_OUTBOUND_RO_MASK BIT(4)
151 #define PCIE_MISC_CTRL_1_EN_VDM_QOS_CONTROL_MASK BIT(5)
153 #define PCIE_MISC_UBUS_CTRL 0x40a4
154 #define PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_ERR_DIS_MASK BIT(13)
155 #define PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_DECERR_DIS_MASK BIT(19)
157 #define PCIE_MISC_UBUS_TIMEOUT 0x40A8
159 #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP 0x40ac
160 #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_ACCESS_ENABLE_MASK BIT(0)
161 #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_HI 0x40b0
163 #define PCIE_MISC_UBUS_BAR2_CONFIG_REMAP 0x40b4
164 #define PCIE_MISC_UBUS_BAR2_CONFIG_REMAP_ACCESS_ENABLE_MASK BIT(0)
166 /* Additional RC BARs */
167 #define PCIE_MISC_RC_BAR_CONFIG_LO_SIZE_MASK 0x1f
168 #define PCIE_MISC_RC_BAR4_CONFIG_LO 0x40d4
169 #define PCIE_MISC_RC_BAR4_CONFIG_HI 0x40d8
171 #define PCIE_MISC_RC_BAR10_CONFIG_LO 0x4104
172 #define PCIE_MISC_RC_BAR10_CONFIG_HI 0x4108
174 #define PCIE_MISC_UBUS_BAR_CONFIG_REMAP_ENABLE 0x1
175 #define PCIE_MISC_UBUS_BAR_CONFIG_REMAP_LO_MASK 0xfffff000
176 #define PCIE_MISC_UBUS_BAR_CONFIG_REMAP_HI_MASK 0xff
177 #define PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_LO 0x410c
178 #define PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_HI 0x4110
180 #define PCIE_MISC_UBUS_BAR10_CONFIG_REMAP_LO 0x413c
181 #define PCIE_MISC_UBUS_BAR10_CONFIG_REMAP_HI 0x4140
183 /* AXI priority forwarding - automatic level-based */
184 #define PCIE_MISC_TC_QUEUE_TO_QOS_MAP(x) (0x4160 - (x) * 4)
185 /* Defined in quarter-fullness */
186 #define QUEUE_THRESHOLD_34_TO_QOS_MAP_SHIFT 12
187 #define QUEUE_THRESHOLD_23_TO_QOS_MAP_SHIFT 8
188 #define QUEUE_THRESHOLD_12_TO_QOS_MAP_SHIFT 4
189 #define QUEUE_THRESHOLD_01_TO_QOS_MAP_SHIFT 0
190 #define QUEUE_THRESHOLD_MASK 0xf
192 /* VDM messages indexing TCs to AXI priorities */
194 #define PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_HI 0x4164
196 #define PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_LO 0x4168
197 #define VDM_PRIORITY_TO_QOS_MAP_SHIFT(x) (4 * (x))
198 #define VDM_PRIORITY_TO_QOS_MAP_MASK 0xf
200 #define PCIE_MISC_AXI_INTF_CTRL 0x416C
201 #define AXI_REQFIFO_EN_QOS_PROPAGATION BIT(7)
202 #define AXI_BRIDGE_LOW_LATENCY_MODE BIT(6)
203 #define AXI_MASTER_MAX_OUTSTANDING_REQUESTS_MASK 0x3f
205 #define PCIE_MISC_AXI_READ_ERROR_DATA 0x4170
207 #define PCIE_INTR2_CPU_BASE (pcie->reg_offsets[INTR2_CPU])
208 #define PCIE_MSI_INTR2_BASE 0x4500
209 /* Offsets from PCIE_INTR2_CPU_BASE and PCIE_MSI_INTR2_BASE */
210 #define MSI_INT_STATUS 0x0
211 #define MSI_INT_CLR 0x8
212 #define MSI_INT_MASK_SET 0x10
213 #define MSI_INT_MASK_CLR 0x14
215 #define PCIE_EXT_CFG_DATA 0x8000
216 #define PCIE_EXT_CFG_INDEX 0x9000
218 #define PCIE_RGR1_SW_INIT_1_PERST_MASK 0x1
219 #define PCIE_RGR1_SW_INIT_1_PERST_SHIFT 0x0
221 #define RGR1_SW_INIT_1_INIT_GENERIC_MASK 0x2
222 #define RGR1_SW_INIT_1_INIT_GENERIC_SHIFT 0x1
223 #define RGR1_SW_INIT_1_INIT_7278_MASK 0x1
224 #define RGR1_SW_INIT_1_INIT_7278_SHIFT 0x0
226 /* PCIe parameters */
227 #define BRCM_NUM_PCIE_OUT_WINS 0x4
228 #define BRCM_INT_PCI_MSI_NR 32
229 #define BRCM_INT_PCI_MSI_LEGACY_NR 8
230 #define BRCM_INT_PCI_MSI_SHIFT 0
231 #define BRCM_INT_PCI_MSI_MASK GENMASK(BRCM_INT_PCI_MSI_NR - 1, 0)
232 #define BRCM_INT_PCI_MSI_LEGACY_MASK GENMASK(31, \
233 32 - BRCM_INT_PCI_MSI_LEGACY_NR)
235 /* MSI target addresses */
236 #define BRCM_MSI_TARGET_ADDR_LT_4GB 0x0fffffffcULL
237 #define BRCM_MSI_TARGET_ADDR_GT_4GB 0xffffffffcULL
240 #define MDIO_PORT0 0x0
241 #define MDIO_DATA_MASK 0x7fffffff
242 #define MDIO_PORT_MASK 0xf0000
243 #define MDIO_REGAD_MASK 0xffff
244 #define MDIO_CMD_MASK 0xfff00000
245 #define MDIO_CMD_READ 0x1
246 #define MDIO_CMD_WRITE 0x0
247 #define MDIO_DATA_DONE_MASK 0x80000000
248 #define MDIO_RD_DONE(x) (((x) & MDIO_DATA_DONE_MASK) ? 1 : 0)
249 #define MDIO_WT_DONE(x) (((x) & MDIO_DATA_DONE_MASK) ? 0 : 1)
250 #define SSC_REGS_ADDR 0x1100
251 #define SET_ADDR_OFFSET 0x1f
252 #define SSC_CNTL_OFFSET 0x2
253 #define SSC_CNTL_OVRD_EN_MASK 0x8000
254 #define SSC_CNTL_OVRD_VAL_MASK 0x4000
255 #define SSC_STATUS_OFFSET 0x1
256 #define SSC_STATUS_SSC_MASK 0x400
257 #define SSC_STATUS_PLL_LOCK_MASK 0x800
258 #define PCIE_BRCM_MAX_MEMC 3
260 #define IDX_ADDR(pcie) (pcie->reg_offsets[EXT_CFG_INDEX])
261 #define DATA_ADDR(pcie) (pcie->reg_offsets[EXT_CFG_DATA])
262 #define PCIE_RGR1_SW_INIT_1(pcie) (pcie->reg_offsets[RGR1_SW_INIT_1])
264 /* Rescal registers */
265 #define PCIE_DVT_PMU_PCIE_PHY_CTRL 0xc700
266 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS 0x3
267 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_MASK 0x4
268 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_SHIFT 0x2
269 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_MASK 0x2
270 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_SHIFT 0x1
271 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_MASK 0x1
272 #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_SHIFT 0x0
274 /* Forward declarations */
286 RGR1_SW_INIT_1_INIT_MASK,
287 RGR1_SW_INIT_1_INIT_SHIFT,
300 struct pcie_cfg_data {
302 const enum pcie_type type;
303 void (*perst_set)(struct brcm_pcie *pcie, u32 val);
304 void (*bridge_sw_init_set)(struct brcm_pcie *pcie, u32 val);
305 bool (*rc_mode)(struct brcm_pcie *pcie);
308 struct subdev_regulators {
309 unsigned int num_supplies;
310 struct regulator_bulk_data supplies[];
316 struct device_node *np;
317 struct irq_domain *msi_domain;
318 struct irq_domain *inner_domain;
319 struct mutex lock; /* guards the alloc/free operations */
322 DECLARE_BITMAP(used, 64);
324 /* Some chips have MSIs in bits [31..24] of a shared register. */
326 int nr; /* No. of MSI available, depends on chip */
327 /* This is the base pointer for interrupt status/set/clr regs */
328 void __iomem *intr_base;
331 /* Internal PCIe Host Controller Information.*/
336 struct device_node *np;
342 struct brcm_msi *msi;
343 const int *reg_offsets;
345 struct reset_control *rescal;
346 struct reset_control *perst_reset;
347 struct reset_control *bridge_reset;
349 u64 memc_size[PCIE_BRCM_MAX_MEMC];
352 void (*perst_set)(struct brcm_pcie *pcie, u32 val);
353 void (*bridge_sw_init_set)(struct brcm_pcie *pcie, u32 val);
354 bool (*rc_mode)(struct brcm_pcie *pcie);
355 struct subdev_regulators *sr;
356 bool ep_wakeup_capable;
359 static inline bool is_bmips(const struct brcm_pcie *pcie)
361 return pcie->type == BCM7435 || pcie->type == BCM7425;
365 * This is to convert the size of the inbound "BAR" region to the
366 * non-linear values of PCIE_X_MISC_RC_BAR[123]_CONFIG_LO.SIZE
368 static int brcm_pcie_encode_ibar_size(u64 size)
370 int log2_in = ilog2(size);
372 if (log2_in >= 12 && log2_in <= 15)
373 /* Covers 4KB to 32KB (inclusive) */
374 return (log2_in - 12) + 0x1c;
375 else if (log2_in >= 16 && log2_in <= 36)
376 /* Covers 64KB to 64GB, (inclusive) */
378 /* Something is awry so disable */
382 static u32 brcm_pcie_mdio_form_pkt(int port, int regad, int cmd)
386 pkt |= FIELD_PREP(MDIO_PORT_MASK, port);
387 pkt |= FIELD_PREP(MDIO_REGAD_MASK, regad);
388 pkt |= FIELD_PREP(MDIO_CMD_MASK, cmd);
393 /* negative return value indicates error */
394 static int brcm_pcie_mdio_read(void __iomem *base, u8 port, u8 regad, u32 *val)
399 writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_READ),
400 base + PCIE_RC_DL_MDIO_ADDR);
401 readl(base + PCIE_RC_DL_MDIO_ADDR);
402 err = readl_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_RD_DATA, data,
403 MDIO_RD_DONE(data), 10, 100);
404 *val = FIELD_GET(MDIO_DATA_MASK, data);
409 /* negative return value indicates error */
410 static int brcm_pcie_mdio_write(void __iomem *base, u8 port,
411 u8 regad, u16 wrdata)
416 writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_WRITE),
417 base + PCIE_RC_DL_MDIO_ADDR);
418 readl(base + PCIE_RC_DL_MDIO_ADDR);
419 writel(MDIO_DATA_DONE_MASK | wrdata, base + PCIE_RC_DL_MDIO_WR_DATA);
421 err = readw_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_WR_DATA, data,
422 MDIO_WT_DONE(data), 10, 100);
427 * Configures device for Spread Spectrum Clocking (SSC) mode; a negative
428 * return value indicates error.
430 static int brcm_pcie_set_ssc(struct brcm_pcie *pcie)
436 ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
441 ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
442 SSC_CNTL_OFFSET, &tmp);
446 u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_EN_MASK);
447 u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_VAL_MASK);
448 ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0,
449 SSC_CNTL_OFFSET, tmp);
453 usleep_range(1000, 2000);
454 ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
455 SSC_STATUS_OFFSET, &tmp);
459 ssc = FIELD_GET(SSC_STATUS_SSC_MASK, tmp);
460 pll = FIELD_GET(SSC_STATUS_PLL_LOCK_MASK, tmp);
462 return ssc && pll ? 0 : -EIO;
465 static void brcm_pcie_munge_pll(struct brcm_pcie *pcie)
467 //print "MDIO block 0x1600 written per Dannys instruction"
468 //tmp = pcie_mdio_write(phyad, &h16&, &h50b9&)
469 //tmp = pcie_mdio_write(phyad, &h17&, &hbd1a&)
470 //tmp = pcie_mdio_write(phyad, &h1b&, &h5030&)
471 //tmp = pcie_mdio_write(phyad, &h1e&, &h0007&)
475 u8 regs[] = { 0x16, 0x17, 0x18, 0x19, 0x1b, 0x1c, 0x1e };
476 u16 data[] = { 0x50b9, 0xbda1, 0x0094, 0x97b4, 0x5030, 0x5030, 0x0007 };
478 ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
480 for (i = 0; i < ARRAY_SIZE(regs); i++) {
481 brcm_pcie_mdio_read(pcie->base, MDIO_PORT0, regs[i], &tmp);
482 dev_dbg(pcie->dev, "PCIE MDIO pre_refclk 0x%02x = 0x%04x\n",
485 for (i = 0; i < ARRAY_SIZE(regs); i++) {
486 brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, regs[i], data[i]);
487 brcm_pcie_mdio_read(pcie->base, MDIO_PORT0, regs[i], &tmp);
488 dev_dbg(pcie->dev, "PCIE MDIO post_refclk 0x%02x = 0x%04x\n",
491 usleep_range(100, 200);
494 /* Limits operation to a specific generation (1, 2, or 3) */
495 static void brcm_pcie_set_gen(struct brcm_pcie *pcie, int gen)
497 u16 lnkctl2 = readw(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
498 u32 lnkcap = readl(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP);
500 lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen;
501 writel(lnkcap, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP);
503 lnkctl2 = (lnkctl2 & ~0xf) | gen;
504 writew(lnkctl2, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
507 static void brcm_pcie_set_outbound_win(struct brcm_pcie *pcie,
508 unsigned int win, u64 cpu_addr,
509 u64 pcie_addr, u64 size)
511 u32 cpu_addr_mb_high, limit_addr_mb_high;
512 phys_addr_t cpu_addr_mb, limit_addr_mb;
516 /* Set the base of the pcie_addr window */
517 writel(lower_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_LO(win));
518 writel(upper_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_HI(win));
520 /* Write the addr base & limit lower bits (in MBs) */
521 cpu_addr_mb = cpu_addr / SZ_1M;
522 limit_addr_mb = (cpu_addr + size - 1) / SZ_1M;
524 tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win));
525 u32p_replace_bits(&tmp, cpu_addr_mb,
526 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
527 u32p_replace_bits(&tmp, limit_addr_mb,
528 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK);
529 writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win));
534 /* Write the cpu & limit addr upper bits */
536 HWEIGHT32(PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
538 cpu_addr_mb_high = cpu_addr_mb >> high_addr_shift;
539 tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_HI(win));
540 u32p_replace_bits(&tmp, cpu_addr_mb_high,
541 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK);
542 writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_HI(win));
544 limit_addr_mb_high = limit_addr_mb >> high_addr_shift;
545 tmp = readl(pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win));
546 u32p_replace_bits(&tmp, limit_addr_mb_high,
547 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK);
548 writel(tmp, pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win));
551 static void brcm_pcie_set_tc_qos(struct brcm_pcie *pcie)
556 if (pcie->type != BCM2712)
559 /* XXX: BCM2712C0 is broken, disable the forwarding search */
560 reg = readl(pcie->base + PCIE_MISC_AXI_INTF_CTRL);
561 reg &= ~AXI_REQFIFO_EN_QOS_PROPAGATION;
562 writel(reg, pcie->base + PCIE_MISC_AXI_INTF_CTRL);
564 /* Disable VDM reception by default - QoS map defaults to 0 */
565 reg = readl(pcie->base + PCIE_MISC_CTRL_1);
566 reg &= ~PCIE_MISC_CTRL_1_EN_VDM_QOS_CONTROL_MASK;
567 writel(reg, pcie->base + PCIE_MISC_CTRL_1);
569 if (!of_property_read_u32(pcie->np, "brcm,fifo-qos-map", &pcie->qos_map)) {
571 * Backpressure mode - bottom 4 nibbles are QoS for each
572 * quartile of FIFO level. Each TC gets the same map, because
573 * this mode is intended for nonrealtime EPs.
576 pcie->qos_map &= 0x0000ffff;
577 for (i = 0; i < 8; i++)
578 writel(pcie->qos_map, pcie->base + PCIE_MISC_TC_QUEUE_TO_QOS_MAP(i));
583 if (!of_property_read_u32(pcie->np, "brcm,vdm-qos-map", &pcie->qos_map)) {
585 reg = readl(pcie->base + PCIE_MISC_CTRL_1);
586 reg |= PCIE_MISC_CTRL_1_EN_VDM_QOS_CONTROL_MASK;
587 writel(reg, pcie->base + PCIE_MISC_CTRL_1);
589 /* No forwarding means no point separating panic priorities from normal */
590 writel(pcie->qos_map, pcie->base + PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_LO);
591 writel(pcie->qos_map, pcie->base + PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_HI);
593 /* Match Vendor ID of 0 */
594 writel(0, pcie->base + PCIE_RC_TL_VDM_CTL1);
595 /* Forward VDMs to priority interface - at least the rx counters work */
596 reg = readl(pcie->base + PCIE_RC_TL_VDM_CTL0);
597 reg |= PCIE_RC_TL_VDM_CTL0_VDM_ENABLED_MASK |
598 PCIE_RC_TL_VDM_CTL0_VDM_IGNORETAG_MASK |
599 PCIE_RC_TL_VDM_CTL0_VDM_IGNOREVNDRID_MASK;
600 writel(reg, pcie->base + PCIE_RC_TL_VDM_CTL0);
604 static void brcm_pcie_config_clkreq(struct brcm_pcie *pcie)
606 void __iomem *base = pcie->base;
607 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
608 int domain = pci_domain_nr(bridge->bus);
609 const struct pci_bus *bus = pci_find_bus(domain, 1);
610 struct pci_dev *pdev = (struct pci_dev *)bus->devices.next;
611 u32 tmp, link_cap = 0;
616 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
617 if ((link_cap & PCI_EXP_LNKCAP_CLKPM))
620 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &link_ctl);
621 if (!(link_ctl & PCI_EXP_LNKCTL_CLKREQ_EN))
624 if (pcie->l1ss && pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS))
627 tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
628 tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK;
629 tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK;
632 tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK;
634 tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK;
636 writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
638 if (substates || clkpm)
639 dev_info(pcie->dev, "clkreq control enabled\n");
642 static struct irq_chip brcm_msi_irq_chip = {
643 .name = "BRCM STB PCIe MSI",
644 .irq_ack = irq_chip_ack_parent,
645 .irq_mask = pci_msi_mask_irq,
646 .irq_unmask = pci_msi_unmask_irq,
649 static struct msi_domain_info brcm_msi_domain_info = {
650 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
651 MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
652 .chip = &brcm_msi_irq_chip,
655 static void brcm_pcie_msi_isr(struct irq_desc *desc)
657 struct irq_chip *chip = irq_desc_get_chip(desc);
658 unsigned long status, virq;
659 struct brcm_msi *msi;
663 chained_irq_enter(chip, desc);
664 msi = irq_desc_get_handler_data(desc);
667 status = readl(msi->intr_base + MSI_INT_STATUS);
668 status >>= msi->legacy_shift;
670 for_each_set_bit(bit, &status, BRCM_INT_PCI_MSI_NR/*msi->nr*/) {
673 virq = irq_find_mapping(msi->inner_domain, bit);
676 dev_dbg(dev, "MSI -> %ld\n", virq);
677 generic_handle_irq(virq);
679 virq = irq_find_mapping(msi->inner_domain, bit + 32);
682 dev_dbg(dev, "MSI -> %ld\n", virq);
683 generic_handle_irq(virq);
686 dev_dbg(dev, "unexpected MSI\n");
689 chained_irq_exit(chip, desc);
692 static void brcm_msi_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
694 struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
696 msg->address_lo = lower_32_bits(msi->target_addr);
697 msg->address_hi = upper_32_bits(msi->target_addr);
698 msg->data = (0xffff & PCIE_MISC_MSI_DATA_CONFIG_VAL_32) | (data->hwirq & 0x1f);
701 static int brcm_msi_set_affinity(struct irq_data *irq_data,
702 const struct cpumask *mask, bool force)
707 static void brcm_msi_ack_irq(struct irq_data *data)
709 struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
710 const int shift_amt = (data->hwirq & 0x1f) + msi->legacy_shift;
712 writel(1 << shift_amt, msi->intr_base + MSI_INT_CLR);
716 static struct irq_chip brcm_msi_bottom_irq_chip = {
717 .name = "BRCM STB MSI",
718 .irq_compose_msi_msg = brcm_msi_compose_msi_msg,
719 .irq_set_affinity = brcm_msi_set_affinity,
720 .irq_ack = brcm_msi_ack_irq,
723 static int brcm_msi_alloc(struct brcm_msi *msi, unsigned int nr_irqs)
727 mutex_lock(&msi->lock);
728 hwirq = bitmap_find_free_region(msi->used, msi->nr,
729 order_base_2(nr_irqs));
730 mutex_unlock(&msi->lock);
735 static void brcm_msi_free(struct brcm_msi *msi, unsigned long hwirq,
736 unsigned int nr_irqs)
738 mutex_lock(&msi->lock);
739 bitmap_release_region(msi->used, hwirq, order_base_2(nr_irqs));
740 mutex_unlock(&msi->lock);
743 static int brcm_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
744 unsigned int nr_irqs, void *args)
746 struct brcm_msi *msi = domain->host_data;
749 hwirq = brcm_msi_alloc(msi, nr_irqs);
754 for (i = 0; i < nr_irqs; i++)
755 irq_domain_set_info(domain, virq + i, hwirq + i,
756 &brcm_msi_bottom_irq_chip, domain->host_data,
757 handle_edge_irq, NULL, NULL);
761 static void brcm_irq_domain_free(struct irq_domain *domain,
762 unsigned int virq, unsigned int nr_irqs)
764 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
765 struct brcm_msi *msi = irq_data_get_irq_chip_data(d);
767 brcm_msi_free(msi, d->hwirq, nr_irqs);
770 static const struct irq_domain_ops msi_domain_ops = {
771 .alloc = brcm_irq_domain_alloc,
772 .free = brcm_irq_domain_free,
775 static int brcm_allocate_domains(struct brcm_msi *msi)
777 struct fwnode_handle *fwnode = of_node_to_fwnode(msi->np);
778 struct device *dev = msi->dev;
780 msi->inner_domain = irq_domain_add_linear(NULL, msi->nr, &msi_domain_ops, msi);
781 if (!msi->inner_domain) {
782 dev_err(dev, "failed to create IRQ domain\n");
786 msi->msi_domain = pci_msi_create_irq_domain(fwnode,
787 &brcm_msi_domain_info,
789 if (!msi->msi_domain) {
790 dev_err(dev, "failed to create MSI domain\n");
791 irq_domain_remove(msi->inner_domain);
798 static void brcm_free_domains(struct brcm_msi *msi)
800 irq_domain_remove(msi->msi_domain);
801 irq_domain_remove(msi->inner_domain);
804 static void brcm_msi_remove(struct brcm_pcie *pcie)
806 struct brcm_msi *msi = pcie->msi;
810 irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
811 brcm_free_domains(msi);
814 static void brcm_msi_set_regs(struct brcm_msi *msi)
816 u32 val = msi->legacy ? BRCM_INT_PCI_MSI_LEGACY_MASK :
817 BRCM_INT_PCI_MSI_MASK;
819 writel(val, msi->intr_base + MSI_INT_MASK_CLR);
820 writel(val, msi->intr_base + MSI_INT_CLR);
823 * The 0 bit of PCIE_MISC_MSI_BAR_CONFIG_LO is repurposed to MSI
824 * enable, which we set to 1.
826 writel(lower_32_bits(msi->target_addr) | 0x1,
827 msi->base + PCIE_MISC_MSI_BAR_CONFIG_LO);
828 writel(upper_32_bits(msi->target_addr),
829 msi->base + PCIE_MISC_MSI_BAR_CONFIG_HI);
831 val = msi->legacy ? PCIE_MISC_MSI_DATA_CONFIG_VAL_8 : PCIE_MISC_MSI_DATA_CONFIG_VAL_32;
832 writel(val, msi->base + PCIE_MISC_MSI_DATA_CONFIG);
835 static int brcm_pcie_enable_msi(struct brcm_pcie *pcie)
837 struct brcm_msi *msi;
839 struct device *dev = pcie->dev;
841 irq = irq_of_parse_and_map(dev->of_node, 1);
843 dev_err(dev, "cannot map MSI interrupt\n");
847 msi = devm_kzalloc(dev, sizeof(struct brcm_msi), GFP_KERNEL);
851 mutex_init(&msi->lock);
853 msi->base = pcie->base;
855 msi->target_addr = pcie->msi_target_addr;
857 msi->legacy = pcie->hw_rev < BRCM_PCIE_HW_REV_33;
860 * Sanity check to make sure that the 'used' bitmap in struct brcm_msi
863 BUILD_BUG_ON(BRCM_INT_PCI_MSI_LEGACY_NR > BRCM_INT_PCI_MSI_NR);
866 msi->intr_base = msi->base + PCIE_INTR2_CPU_BASE;
867 msi->nr = BRCM_INT_PCI_MSI_LEGACY_NR;
868 msi->legacy_shift = 24;
870 msi->intr_base = msi->base + PCIE_MSI_INTR2_BASE;
871 msi->nr = 64; //BRCM_INT_PCI_MSI_NR;
872 msi->legacy_shift = 0;
875 ret = brcm_allocate_domains(msi);
879 irq_set_chained_handler_and_data(msi->irq, brcm_pcie_msi_isr, msi);
881 brcm_msi_set_regs(msi);
887 /* The controller is capable of serving in both RC and EP roles */
888 static bool brcm_pcie_rc_mode_generic(struct brcm_pcie *pcie)
890 void __iomem *base = pcie->base;
891 u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
893 return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK, val);
896 static bool brcm_pcie_rc_mode_2712(struct brcm_pcie *pcie)
898 void __iomem *base = pcie->base;
899 u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
901 return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK_2712, val) | 1; //XXX
904 static bool brcm_pcie_link_up(struct brcm_pcie *pcie)
906 u32 val = readl(pcie->base + PCIE_MISC_PCIE_STATUS);
907 u32 dla = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK, val);
908 u32 plu = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK, val);
913 static void __iomem *brcm_pcie_map_bus(struct pci_bus *bus,
914 unsigned int devfn, int where)
916 struct brcm_pcie *pcie = bus->sysdata;
917 void __iomem *base = pcie->base;
920 /* Accesses to the RC go right to the RC registers if !devfn */
921 if (pci_is_root_bus(bus))
922 return devfn ? NULL : base + PCIE_ECAM_REG(where);
924 /* An access to our HW w/o link-up will cause a CPU Abort */
925 if (!brcm_pcie_link_up(pcie))
928 /* For devices, write to the config space index register */
929 idx = PCIE_ECAM_OFFSET(bus->number, devfn, 0);
930 writel(idx, pcie->base + PCIE_EXT_CFG_INDEX);
931 return base + PCIE_EXT_CFG_DATA + PCIE_ECAM_REG(where);
934 static void __iomem *brcm7425_pcie_map_bus(struct pci_bus *bus,
935 unsigned int devfn, int where)
937 struct brcm_pcie *pcie = bus->sysdata;
938 void __iomem *base = pcie->base;
941 /* Accesses to the RC go right to the RC registers if !devfn */
942 if (pci_is_root_bus(bus))
943 return devfn ? NULL : base + PCIE_ECAM_REG(where);
945 /* An access to our HW w/o link-up will cause a CPU Abort */
946 if (!brcm_pcie_link_up(pcie))
949 /* For devices, write to the config space index register */
950 idx = PCIE_ECAM_OFFSET(bus->number, devfn, where);
951 writel(idx, base + IDX_ADDR(pcie));
952 return base + DATA_ADDR(pcie);
955 static void brcm_pcie_bridge_sw_init_set_generic(struct brcm_pcie *pcie, u32 val)
957 u32 tmp, mask = RGR1_SW_INIT_1_INIT_GENERIC_MASK;
958 u32 shift = RGR1_SW_INIT_1_INIT_GENERIC_SHIFT;
960 tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
961 tmp = (tmp & ~mask) | ((val << shift) & mask);
962 writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
965 static void brcm_pcie_bridge_sw_init_set_7278(struct brcm_pcie *pcie, u32 val)
967 u32 tmp, mask = RGR1_SW_INIT_1_INIT_7278_MASK;
968 u32 shift = RGR1_SW_INIT_1_INIT_7278_SHIFT;
970 tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
971 tmp = (tmp & ~mask) | ((val << shift) & mask);
972 writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
975 static void brcm_pcie_bridge_sw_init_set_2712(struct brcm_pcie *pcie, u32 val)
977 if (WARN_ONCE(!pcie->bridge_reset,
978 "missing bridge reset controller\n"))
982 reset_control_assert(pcie->bridge_reset);
984 reset_control_deassert(pcie->bridge_reset);
987 static void brcm_pcie_perst_set_4908(struct brcm_pcie *pcie, u32 val)
989 if (WARN_ONCE(!pcie->perst_reset, "missing PERST# reset controller\n"))
993 reset_control_assert(pcie->perst_reset);
995 reset_control_deassert(pcie->perst_reset);
998 static void brcm_pcie_perst_set_7278(struct brcm_pcie *pcie, u32 val)
1002 /* Perst bit has moved and assert value is 0 */
1003 tmp = readl(pcie->base + PCIE_MISC_PCIE_CTRL);
1004 u32p_replace_bits(&tmp, !val, PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK);
1005 writel(tmp, pcie->base + PCIE_MISC_PCIE_CTRL);
1008 static void brcm_pcie_perst_set_2712(struct brcm_pcie *pcie, u32 val)
1012 /* Perst bit has moved and assert value is 0 */
1013 tmp = readl(pcie->base + PCIE_MISC_PCIE_CTRL);
1014 u32p_replace_bits(&tmp, !val, PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK);
1015 writel(tmp, pcie->base + PCIE_MISC_PCIE_CTRL);
1018 static void brcm_pcie_perst_set_generic(struct brcm_pcie *pcie, u32 val)
1022 tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
1023 u32p_replace_bits(&tmp, val, PCIE_RGR1_SW_INIT_1_PERST_MASK);
1024 writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
1027 static int brcm_pcie_get_rc_bar2_size_and_offset(struct brcm_pcie *pcie,
1029 u64 *rc_bar2_offset)
1031 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1032 struct resource_entry *entry;
1033 struct device *dev = pcie->dev;
1034 u64 lowest_pcie_addr = ~(u64)0;
1038 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
1039 u64 pcie_beg = entry->res->start - entry->offset;
1041 size += entry->res->end - entry->res->start + 1;
1042 if (pcie_beg < lowest_pcie_addr)
1043 lowest_pcie_addr = pcie_beg;
1044 if (pcie->type == BCM2711 || pcie->type == BCM2712)
1045 break; // Only consider the first entry
1048 if (lowest_pcie_addr == ~(u64)0) {
1049 dev_err(dev, "DT node has no dma-ranges\n");
1053 ret = of_property_read_variable_u64_array(pcie->np, "brcm,scb-sizes", pcie->memc_size, 1,
1054 PCIE_BRCM_MAX_MEMC);
1057 /* Make an educated guess */
1059 pcie->memc_size[0] = 1ULL << fls64(size - 1);
1061 pcie->num_memc = ret;
1064 /* Each memc is viewed through a "port" that is a power of 2 */
1065 for (i = 0, size = 0; i < pcie->num_memc; i++)
1066 size += pcie->memc_size[i];
1068 /* System memory starts at this address in PCIe-space */
1069 *rc_bar2_offset = lowest_pcie_addr;
1070 /* The sum of all memc views must also be a power of 2 */
1071 *rc_bar2_size = 1ULL << fls64(size - 1);
1074 * We validate the inbound memory view even though we should trust
1075 * whatever the device-tree provides. This is because of an HW issue on
1076 * early Raspberry Pi 4's revisions (bcm2711). It turns out its
1077 * firmware has to dynamically edit dma-ranges due to a bug on the
1078 * PCIe controller integration, which prohibits any access above the
1079 * lower 3GB of memory. Given this, we decided to keep the dma-ranges
1080 * in check, avoiding hard to debug device-tree related issues in the
1083 * The PCIe host controller by design must set the inbound viewport to
1084 * be a contiguous arrangement of all of the system's memory. In
1085 * addition, its size mut be a power of two. To further complicate
1086 * matters, the viewport must start on a pcie-address that is aligned
1087 * on a multiple of its size. If a portion of the viewport does not
1088 * represent system memory -- e.g. 3GB of memory requires a 4GB
1089 * viewport -- we can map the outbound memory in or after 3GB and even
1090 * though the viewport will overlap the outbound memory the controller
1091 * will know to send outbound memory downstream and everything else
1096 * - The best-case scenario, memory up to 3GB, is to place the inbound
1097 * region in the first 4GB of pcie-space, as some legacy devices can
1098 * only address 32bits. We would also like to put the MSI under 4GB
1099 * as well, since some devices require a 32bit MSI target address.
1101 * - If the system memory is 4GB or larger we cannot start the inbound
1102 * region at location 0 (since we have to allow some space for
1103 * outbound memory @ 3GB). So instead it will start at the 1x
1104 * multiple of its size
1106 if (!*rc_bar2_size || (*rc_bar2_offset & (*rc_bar2_size - 1)) ||
1107 (*rc_bar2_offset < SZ_4G && *rc_bar2_offset > SZ_2G)) {
1108 dev_err(dev, "Invalid rc_bar2_offset/size: size 0x%llx, off 0x%llx\n",
1109 *rc_bar2_size, *rc_bar2_offset);
1116 static int brcm_pcie_get_rc_bar_n(struct brcm_pcie *pcie,
1122 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1123 struct resource_entry *entry;
1126 resource_list_for_each_entry(entry, &bridge->dma_ranges) {
1128 *rc_bar_cpu = entry->res->start;
1129 *rc_bar_size = entry->res->end - entry->res->start + 1;
1130 *rc_bar_pci = entry->res->start - entry->offset;
1140 static int brcm_pcie_setup(struct brcm_pcie *pcie)
1142 u64 rc_bar2_offset, rc_bar2_size;
1143 void __iomem *base = pcie->base;
1144 struct pci_host_bridge *bridge;
1145 struct resource_entry *entry;
1146 u32 tmp, burst, aspm_support;
1147 int num_out_wins = 0;
1148 int ret, memc, count, i;
1150 /* Reset the bridge */
1151 pcie->bridge_sw_init_set(pcie, 1);
1153 /* Ensure that PERST# is asserted; some bootloaders may deassert it. */
1154 if (pcie->type == BCM2711)
1155 pcie->perst_set(pcie, 1);
1157 usleep_range(100, 200);
1159 /* Take the bridge out of reset */
1160 pcie->bridge_sw_init_set(pcie, 0);
1162 tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1164 tmp &= ~PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
1166 tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
1167 writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1168 /* Wait for SerDes to be stable */
1169 usleep_range(100, 200);
1171 if (pcie->type == BCM2712) {
1172 /* Allow a 54MHz (xosc) refclk source */
1173 brcm_pcie_munge_pll(pcie);
1174 /* Fix for L1SS errata */
1175 tmp = readl(base + PCIE_RC_PL_PHY_CTL_15);
1176 tmp &= ~PCIE_RC_PL_PHY_CTL_15_PM_CLK_PERIOD_MASK;
1177 /* PM clock period is 18.52ns (round down) */
1179 writel(tmp, base + PCIE_RC_PL_PHY_CTL_15);
1183 * SCB_MAX_BURST_SIZE is a two bit field. For GENERIC chips it
1184 * is encoded as 0=128, 1=256, 2=512, 3=Rsvd, for BCM7278 it
1185 * is encoded as 0=Rsvd, 1=128, 2=256, 3=512.
1188 burst = 0x1; /* 256 bytes */
1189 else if (pcie->type == BCM2711)
1190 burst = 0x0; /* 128 bytes */
1191 else if (pcie->type == BCM2712)
1192 burst = 0x1; /* 128 bytes */
1193 else if (pcie->type == BCM7278)
1194 burst = 0x3; /* 512 bytes */
1196 burst = 0x2; /* 512 bytes */
1199 * Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN,
1202 tmp = readl(base + PCIE_MISC_MISC_CTRL);
1203 u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK);
1204 u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK);
1205 u32p_replace_bits(&tmp, burst, PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK);
1206 if (pcie->rcb_mps_mode)
1207 u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_PCIE_RCB_MPS_MODE_MASK);
1208 writel(tmp, base + PCIE_MISC_MISC_CTRL);
1210 brcm_pcie_set_tc_qos(pcie);
1212 ret = brcm_pcie_get_rc_bar2_size_and_offset(pcie, &rc_bar2_size,
1217 tmp = lower_32_bits(rc_bar2_offset);
1218 u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(rc_bar2_size),
1219 PCIE_MISC_RC_BAR2_CONFIG_LO_SIZE_MASK);
1220 writel(tmp, base + PCIE_MISC_RC_BAR2_CONFIG_LO);
1221 writel(upper_32_bits(rc_bar2_offset),
1222 base + PCIE_MISC_RC_BAR2_CONFIG_HI);
1224 tmp = readl(base + PCIE_MISC_UBUS_BAR2_CONFIG_REMAP);
1225 u32p_replace_bits(&tmp, 1, PCIE_MISC_UBUS_BAR2_CONFIG_REMAP_ACCESS_ENABLE_MASK);
1226 writel(tmp, base + PCIE_MISC_UBUS_BAR2_CONFIG_REMAP);
1227 tmp = readl(base + PCIE_MISC_MISC_CTRL);
1229 for (memc = 0; memc < pcie->num_memc; memc++) {
1230 u32 scb_size_val = ilog2(pcie->memc_size[memc]) - 15;
1233 u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(0));
1235 u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(1));
1237 u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(2));
1240 writel(tmp, base + PCIE_MISC_MISC_CTRL);
1242 if (pcie->type == BCM2712) {
1243 /* Suppress AXI error responses and return 1s for read failures */
1244 tmp = readl(base + PCIE_MISC_UBUS_CTRL);
1245 u32p_replace_bits(&tmp, 1, PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_ERR_DIS_MASK);
1246 u32p_replace_bits(&tmp, 1, PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_DECERR_DIS_MASK);
1247 writel(tmp, base + PCIE_MISC_UBUS_CTRL);
1248 writel(0xffffffff, base + PCIE_MISC_AXI_READ_ERROR_DATA);
1251 * Adjust timeouts. The UBUS timeout also affects CRS
1252 * completion retries, as the request will get terminated if
1253 * either timeout expires, so both have to be a large value
1254 * (in clocks of 750MHz).
1255 * Set UBUS timeout to 250ms, then set RC config retry timeout
1258 * Setting CRSVis=1 will stop the core from blocking on a CRS
1259 * response, but does require the device to be well-behaved...
1261 writel(0xB2D0000, base + PCIE_MISC_UBUS_TIMEOUT);
1262 writel(0xABA0000, base + PCIE_MISC_RC_CONFIG_RETRY_TIMEOUT);
1266 * We ideally want the MSI target address to be located in the 32bit
1267 * addressable memory area. Some devices might depend on it. This is
1268 * possible either when the inbound window is located above the lower
1269 * 4GB or when the inbound area is smaller than 4GB (taking into
1270 * account the rounding-up we're forced to perform).
1272 if (rc_bar2_offset >= SZ_4G || (rc_bar2_size + rc_bar2_offset) < SZ_4G)
1273 pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_LT_4GB;
1275 pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_GT_4GB;
1277 if (!pcie->rc_mode(pcie)) {
1278 dev_err(pcie->dev, "PCIe RC controller misconfigured as Endpoint\n");
1282 /* disable the PCIe->GISB memory window (RC_BAR1) */
1283 tmp = readl(base + PCIE_MISC_RC_BAR1_CONFIG_LO);
1284 tmp &= ~PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK;
1285 writel(tmp, base + PCIE_MISC_RC_BAR1_CONFIG_LO);
1287 /* disable the PCIe->SCB memory window (RC_BAR3) */
1288 tmp = readl(base + PCIE_MISC_RC_BAR3_CONFIG_LO);
1289 tmp &= ~PCIE_MISC_RC_BAR3_CONFIG_LO_SIZE_MASK;
1290 writel(tmp, base + PCIE_MISC_RC_BAR3_CONFIG_LO);
1292 /* Don't advertise L0s capability if 'aspm-no-l0s' */
1293 aspm_support = PCIE_LINK_STATE_L1;
1294 if (!of_property_read_bool(pcie->np, "aspm-no-l0s"))
1295 aspm_support |= PCIE_LINK_STATE_L0S;
1296 tmp = readl(base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
1297 u32p_replace_bits(&tmp, aspm_support,
1298 PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_ASPM_SUPPORT_MASK);
1299 writel(tmp, base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
1301 /* program additional inbound windows (RC_BAR4..RC_BAR10) */
1302 count = (pcie->type == BCM2712) ? 7 : 0;
1303 for (i = 0; i < count; i++) {
1304 u64 bar_cpu, bar_size, bar_pci;
1306 ret = brcm_pcie_get_rc_bar_n(pcie, 1 + i, &bar_cpu, &bar_size,
1311 tmp = lower_32_bits(bar_pci);
1312 u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(bar_size),
1313 PCIE_MISC_RC_BAR_CONFIG_LO_SIZE_MASK);
1314 writel(tmp, base + PCIE_MISC_RC_BAR4_CONFIG_LO + i * 8);
1315 writel(upper_32_bits(bar_pci),
1316 base + PCIE_MISC_RC_BAR4_CONFIG_HI + i * 8);
1318 tmp = upper_32_bits(bar_cpu) &
1319 PCIE_MISC_UBUS_BAR_CONFIG_REMAP_HI_MASK;
1321 base + PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_HI + i * 8);
1322 tmp = lower_32_bits(bar_cpu) &
1323 PCIE_MISC_UBUS_BAR_CONFIG_REMAP_LO_MASK;
1324 writel(tmp | PCIE_MISC_UBUS_BAR_CONFIG_REMAP_ENABLE,
1325 base + PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_LO + i * 8);
1329 dev_info(pcie->dev, "Forcing gen %d\n", pcie->gen);
1330 brcm_pcie_set_gen(pcie, pcie->gen);
1334 * For config space accesses on the RC, show the right class for
1335 * a PCIe-PCIe bridge (the default setting is to be EP mode).
1337 tmp = readl(base + PCIE_RC_CFG_PRIV1_ID_VAL3);
1338 u32p_replace_bits(&tmp, 0x060400,
1339 PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK);
1340 writel(tmp, base + PCIE_RC_CFG_PRIV1_ID_VAL3);
1342 bridge = pci_host_bridge_from_priv(pcie);
1343 resource_list_for_each_entry(entry, &bridge->windows) {
1344 struct resource *res = entry->res;
1346 if (resource_type(res) != IORESOURCE_MEM)
1349 if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) {
1350 dev_err(pcie->dev, "too many outbound wins\n");
1354 if (is_bmips(pcie)) {
1355 u64 start = res->start;
1356 unsigned int j, nwins = resource_size(res) / SZ_128M;
1358 /* bmips PCIe outbound windows have a 128MB max size */
1359 if (nwins > BRCM_NUM_PCIE_OUT_WINS)
1360 nwins = BRCM_NUM_PCIE_OUT_WINS;
1361 for (j = 0; j < nwins; j++, start += SZ_128M)
1362 brcm_pcie_set_outbound_win(pcie, j, start,
1363 start - entry->offset,
1367 brcm_pcie_set_outbound_win(pcie, num_out_wins, res->start,
1368 res->start - entry->offset,
1369 resource_size(res));
1373 /* PCIe->SCB endian mode for BAR */
1374 tmp = readl(base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1);
1375 u32p_replace_bits(&tmp, PCIE_RC_CFG_VENDOR_SPCIFIC_REG1_LITTLE_ENDIAN,
1376 PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK);
1377 writel(tmp, base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1);
1382 static int brcm_pcie_start_link(struct brcm_pcie *pcie)
1384 struct device *dev = pcie->dev;
1385 void __iomem *base = pcie->base;
1386 u16 nlw, cls, lnksta;
1387 bool ssc_good = false;
1390 /* Unassert the fundamental reset */
1391 pcie->perst_set(pcie, 0);
1394 * Wait for 100ms after PERST# deassertion; see PCIe CEM specification
1395 * sections 2.2, PCIe r5.0, 6.6.1.
1400 * Give the RC/EP even more time to wake up, before trying to
1401 * configure RC. Intermittently check status for link-up, up to a
1404 for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5)
1407 if (!brcm_pcie_link_up(pcie)) {
1408 dev_err(dev, "link down\n");
1413 brcm_pcie_set_gen(pcie, pcie->gen);
1416 ret = brcm_pcie_set_ssc(pcie);
1420 dev_err(dev, "failed attempt to enter ssc mode\n");
1424 lnksta = readw(base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKSTA);
1425 cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
1426 nlw = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
1427 dev_info(dev, "link up, %s x%u %s\n",
1428 pci_speed_string(pcie_link_speed[cls]), nlw,
1429 ssc_good ? "(SSC)" : "(!SSC)");
1434 static const char * const supplies[] = {
1440 static void *alloc_subdev_regulators(struct device *dev)
1442 const size_t size = sizeof(struct subdev_regulators) +
1443 sizeof(struct regulator_bulk_data) * ARRAY_SIZE(supplies);
1444 struct subdev_regulators *sr;
1447 sr = devm_kzalloc(dev, size, GFP_KERNEL);
1449 sr->num_supplies = ARRAY_SIZE(supplies);
1450 for (i = 0; i < ARRAY_SIZE(supplies); i++)
1451 sr->supplies[i].supply = supplies[i];
1457 static int brcm_pcie_add_bus(struct pci_bus *bus)
1459 struct brcm_pcie *pcie = bus->sysdata;
1460 struct device *dev = &bus->dev;
1461 struct subdev_regulators *sr;
1464 if (!bus->parent || !pci_is_root_bus(bus->parent))
1468 sr = alloc_subdev_regulators(dev);
1470 dev_info(dev, "Can't allocate regulators for downstream device\n");
1476 ret = regulator_bulk_get(dev, sr->num_supplies, sr->supplies);
1478 dev_info(dev, "No regulators for downstream device\n");
1482 ret = regulator_bulk_enable(sr->num_supplies, sr->supplies);
1484 dev_err(dev, "Can't enable regulators for downstream device\n");
1485 regulator_bulk_free(sr->num_supplies, sr->supplies);
1491 brcm_pcie_start_link(pcie);
1495 static void brcm_pcie_remove_bus(struct pci_bus *bus)
1497 struct brcm_pcie *pcie = bus->sysdata;
1498 struct subdev_regulators *sr = pcie->sr;
1499 struct device *dev = &bus->dev;
1504 if (regulator_bulk_disable(sr->num_supplies, sr->supplies))
1505 dev_err(dev, "Failed to disable regulators for downstream device\n");
1506 regulator_bulk_free(sr->num_supplies, sr->supplies);
1510 /* L23 is a low-power PCIe link state */
1511 static void brcm_pcie_enter_l23(struct brcm_pcie *pcie)
1513 void __iomem *base = pcie->base;
1517 /* Assert request for L23 */
1518 tmp = readl(base + PCIE_MISC_PCIE_CTRL);
1519 u32p_replace_bits(&tmp, 1, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK);
1520 writel(tmp, base + PCIE_MISC_PCIE_CTRL);
1522 /* Wait up to 36 msec for L23 */
1523 tmp = readl(base + PCIE_MISC_PCIE_STATUS);
1524 l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK, tmp);
1525 for (i = 0; i < 15 && !l23; i++) {
1526 usleep_range(2000, 2400);
1527 tmp = readl(base + PCIE_MISC_PCIE_STATUS);
1528 l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK,
1533 dev_err(pcie->dev, "failed to enter low-power link state\n");
1536 static int brcm_phy_cntl(struct brcm_pcie *pcie, const int start)
1539 static const u32 shifts[PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS] = {
1540 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_SHIFT,
1541 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_SHIFT,
1542 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_SHIFT,};
1543 static const u32 masks[PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS] = {
1544 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_MASK,
1545 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_MASK,
1546 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_MASK,};
1547 const int beg = start ? 0 : PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS - 1;
1548 const int end = start ? PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS : -1;
1549 u32 tmp, combined_mask = 0;
1551 void __iomem *base = pcie->base;
1554 for (i = beg; i != end; start ? i++ : i--) {
1555 val = start ? BIT_MASK(shifts[i]) : 0;
1556 tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1557 tmp = (tmp & ~masks[i]) | (val & masks[i]);
1558 writel(tmp, base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1559 usleep_range(50, 200);
1560 combined_mask |= masks[i];
1563 tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1564 val = start ? combined_mask : 0;
1566 ret = (tmp & combined_mask) == val ? 0 : -EIO;
1568 dev_err(pcie->dev, "failed to %s phy\n", (start ? "start" : "stop"));
1576 static inline int brcm_phy_start(struct brcm_pcie *pcie)
1578 return pcie->rescal ? brcm_phy_cntl(pcie, 1) : 0;
1581 static inline int brcm_phy_stop(struct brcm_pcie *pcie)
1583 return pcie->rescal ? brcm_phy_cntl(pcie, 0) : 0;
1586 static void brcm_pcie_turn_off(struct brcm_pcie *pcie)
1588 void __iomem *base = pcie->base;
1591 if (brcm_pcie_link_up(pcie))
1592 brcm_pcie_enter_l23(pcie);
1593 /* Assert fundamental reset */
1594 pcie->perst_set(pcie, 1);
1596 /* Deassert request for L23 in case it was asserted */
1597 tmp = readl(base + PCIE_MISC_PCIE_CTRL);
1598 u32p_replace_bits(&tmp, 0, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK);
1599 writel(tmp, base + PCIE_MISC_PCIE_CTRL);
1601 /* Turn off SerDes */
1602 tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1603 u32p_replace_bits(&tmp, 1, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
1604 writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1607 * Shutting down this bridge on pcie1 means accesses to rescal block
1608 * will hang the chip if another RC wants to assert/deassert rescal.
1610 if (pcie->type == BCM2712)
1612 /* Shutdown PCIe bridge */
1613 pcie->bridge_sw_init_set(pcie, 1);
1616 static int pci_dev_may_wakeup(struct pci_dev *dev, void *data)
1620 if (device_may_wakeup(&dev->dev)) {
1622 dev_info(&dev->dev, "Possible wake-up device; regulators will not be disabled\n");
1627 static int brcm_pcie_suspend_noirq(struct device *dev)
1629 struct brcm_pcie *pcie = dev_get_drvdata(dev);
1630 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1633 brcm_pcie_turn_off(pcie);
1635 * If brcm_phy_stop() returns an error, just dev_err(). If we
1636 * return the error it will cause the suspend to fail and this is a
1637 * forgivable offense that will probably be erased on resume.
1639 if (brcm_phy_stop(pcie))
1640 dev_err(dev, "Could not stop phy for suspend\n");
1642 ret = reset_control_assert(pcie->rescal);
1644 dev_err(dev, "Could not assert rescal reset\n");
1650 * Now turn off the regulators, but if at least one
1651 * downstream device is enabled as a wake-up source, do not
1652 * turn off regulators.
1654 pcie->ep_wakeup_capable = false;
1655 pci_walk_bus(bridge->bus, pci_dev_may_wakeup,
1656 &pcie->ep_wakeup_capable);
1657 if (!pcie->ep_wakeup_capable) {
1658 ret = regulator_bulk_disable(pcie->sr->num_supplies,
1659 pcie->sr->supplies);
1661 dev_err(dev, "Could not turn off regulators\n");
1662 reset_control_reset(pcie->rescal);
1667 clk_disable_unprepare(pcie->clk);
1672 static int brcm_pcie_resume_noirq(struct device *dev)
1674 struct brcm_pcie *pcie = dev_get_drvdata(dev);
1680 ret = clk_prepare_enable(pcie->clk);
1684 ret = reset_control_reset(pcie->rescal);
1686 goto err_disable_clk;
1688 ret = brcm_phy_start(pcie);
1692 /* Take bridge out of reset so we can access the SERDES reg */
1693 pcie->bridge_sw_init_set(pcie, 0);
1695 /* SERDES_IDDQ = 0 */
1696 tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1697 u32p_replace_bits(&tmp, 0, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
1698 writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1700 /* wait for serdes to be stable */
1703 ret = brcm_pcie_setup(pcie);
1708 if (pcie->ep_wakeup_capable) {
1710 * We are resuming from a suspend. In the suspend we
1711 * did not disable the power supplies, so there is
1712 * no need to enable them (and falsely increase their
1715 pcie->ep_wakeup_capable = false;
1717 ret = regulator_bulk_enable(pcie->sr->num_supplies,
1718 pcie->sr->supplies);
1720 dev_err(dev, "Could not turn on regulators\n");
1726 ret = brcm_pcie_start_link(pcie);
1731 brcm_msi_set_regs(pcie->msi);
1737 regulator_bulk_disable(pcie->sr->num_supplies, pcie->sr->supplies);
1739 reset_control_assert(pcie->rescal);
1741 clk_disable_unprepare(pcie->clk);
1745 static void __brcm_pcie_remove(struct brcm_pcie *pcie)
1747 brcm_msi_remove(pcie);
1748 brcm_pcie_turn_off(pcie);
1749 if (brcm_phy_stop(pcie))
1750 dev_err(pcie->dev, "Could not stop phy\n");
1751 if (reset_control_assert(pcie->rescal))
1752 dev_err(pcie->dev, "Could not assert rescal reset\n");
1753 clk_disable_unprepare(pcie->clk);
1756 static void brcm_pcie_remove(struct platform_device *pdev)
1758 struct brcm_pcie *pcie = platform_get_drvdata(pdev);
1759 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1761 pci_stop_root_bus(bridge->bus);
1762 pci_remove_root_bus(bridge->bus);
1763 __brcm_pcie_remove(pcie);
1766 static const int pcie_offsets[] = {
1767 [RGR1_SW_INIT_1] = 0x9210,
1768 [EXT_CFG_INDEX] = 0x9000,
1769 [EXT_CFG_DATA] = 0x9004,
1770 [PCIE_HARD_DEBUG] = 0x4204,
1771 [INTR2_CPU] = 0x4300,
1774 static const int pcie_offsets_bmips_7425[] = {
1775 [RGR1_SW_INIT_1] = 0x8010,
1776 [EXT_CFG_INDEX] = 0x8300,
1777 [EXT_CFG_DATA] = 0x8304,
1778 [PCIE_HARD_DEBUG] = 0x4204,
1779 [INTR2_CPU] = 0x4300,
1782 static const struct pcie_cfg_data generic_cfg = {
1783 .offsets = pcie_offsets,
1785 .perst_set = brcm_pcie_perst_set_generic,
1786 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1787 .rc_mode = brcm_pcie_rc_mode_generic,
1790 static const struct pcie_cfg_data bcm7425_cfg = {
1791 .offsets = pcie_offsets_bmips_7425,
1793 .perst_set = brcm_pcie_perst_set_generic,
1794 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1795 .rc_mode = brcm_pcie_rc_mode_generic,
1798 static const struct pcie_cfg_data bcm7435_cfg = {
1799 .offsets = pcie_offsets,
1801 .perst_set = brcm_pcie_perst_set_generic,
1802 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1805 static const struct pcie_cfg_data bcm4908_cfg = {
1806 .offsets = pcie_offsets,
1808 .perst_set = brcm_pcie_perst_set_4908,
1809 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1810 .rc_mode = brcm_pcie_rc_mode_generic,
1813 static const int pcie_offset_bcm7278[] = {
1814 [RGR1_SW_INIT_1] = 0xc010,
1815 [EXT_CFG_INDEX] = 0x9000,
1816 [EXT_CFG_DATA] = 0x9004,
1817 [PCIE_HARD_DEBUG] = 0x4204,
1818 [INTR2_CPU] = 0x4300,
1821 static const struct pcie_cfg_data bcm7278_cfg = {
1822 .offsets = pcie_offset_bcm7278,
1824 .perst_set = brcm_pcie_perst_set_7278,
1825 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_7278,
1826 .rc_mode = brcm_pcie_rc_mode_generic,
1829 static const struct pcie_cfg_data bcm2711_cfg = {
1830 .offsets = pcie_offsets,
1832 .perst_set = brcm_pcie_perst_set_generic,
1833 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1834 .rc_mode = brcm_pcie_rc_mode_generic,
1837 static const int pcie_offsets_bcm2712[] = {
1838 [EXT_CFG_INDEX] = 0x9000,
1839 [EXT_CFG_DATA] = 0x9004,
1840 [PCIE_HARD_DEBUG] = 0x4304,
1841 [INTR2_CPU] = 0x4400,
1844 static const struct pcie_cfg_data bcm2712_cfg = {
1845 .offsets = pcie_offsets_bcm2712,
1847 .perst_set = brcm_pcie_perst_set_2712,
1848 .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_2712,
1849 .rc_mode = brcm_pcie_rc_mode_2712,
1852 static const struct of_device_id brcm_pcie_match[] = {
1853 { .compatible = "brcm,bcm2711-pcie", .data = &bcm2711_cfg },
1854 { .compatible = "brcm,bcm2712-pcie", .data = &bcm2712_cfg },
1855 { .compatible = "brcm,bcm4908-pcie", .data = &bcm4908_cfg },
1856 { .compatible = "brcm,bcm7211-pcie", .data = &generic_cfg },
1857 { .compatible = "brcm,bcm7278-pcie", .data = &bcm7278_cfg },
1858 { .compatible = "brcm,bcm7216-pcie", .data = &bcm7278_cfg },
1859 { .compatible = "brcm,bcm7445-pcie", .data = &generic_cfg },
1860 { .compatible = "brcm,bcm7435-pcie", .data = &bcm7435_cfg },
1861 { .compatible = "brcm,bcm7425-pcie", .data = &bcm7425_cfg },
1865 static struct pci_ops brcm_pcie_ops = {
1866 .map_bus = brcm_pcie_map_bus,
1867 .read = pci_generic_config_read,
1868 .write = pci_generic_config_write,
1869 .add_bus = brcm_pcie_add_bus,
1870 .remove_bus = brcm_pcie_remove_bus,
1873 static struct pci_ops brcm7425_pcie_ops = {
1874 .map_bus = brcm7425_pcie_map_bus,
1875 .read = pci_generic_config_read32,
1876 .write = pci_generic_config_write32,
1877 .add_bus = brcm_pcie_add_bus,
1878 .remove_bus = brcm_pcie_remove_bus,
1881 static int brcm_pcie_probe(struct platform_device *pdev)
1883 struct device_node *np = pdev->dev.of_node, *msi_np;
1884 struct pci_host_bridge *bridge;
1885 const struct pcie_cfg_data *data;
1886 struct brcm_pcie *pcie;
1889 bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie));
1893 data = of_device_get_match_data(&pdev->dev);
1895 dev_err(&pdev->dev, "failed to look up compatible string\n");
1899 pcie = pci_host_bridge_priv(bridge);
1900 pcie->dev = &pdev->dev;
1902 pcie->reg_offsets = data->offsets;
1903 pcie->type = data->type;
1904 pcie->perst_set = data->perst_set;
1905 pcie->bridge_sw_init_set = data->bridge_sw_init_set;
1906 pcie->rc_mode = data->rc_mode;
1908 pcie->base = devm_platform_ioremap_resource(pdev, 0);
1909 if (IS_ERR(pcie->base))
1910 return PTR_ERR(pcie->base);
1912 pcie->clk = devm_clk_get_optional(&pdev->dev, "sw_pcie");
1913 if (IS_ERR(pcie->clk))
1914 return PTR_ERR(pcie->clk);
1916 ret = of_pci_get_max_link_speed(np);
1917 pcie->gen = (ret < 0) ? 0 : ret;
1919 pcie->ssc = of_property_read_bool(np, "brcm,enable-ssc");
1920 pcie->l1ss = of_property_read_bool(np, "brcm,enable-l1ss");
1921 pcie->rcb_mps_mode = of_property_read_bool(np, "brcm,enable-mps-rcb");
1923 ret = clk_prepare_enable(pcie->clk);
1925 dev_err(&pdev->dev, "could not enable clock\n");
1928 pcie->rescal = devm_reset_control_get_optional_shared(&pdev->dev, "rescal");
1929 if (IS_ERR(pcie->rescal)) {
1930 clk_disable_unprepare(pcie->clk);
1931 return PTR_ERR(pcie->rescal);
1933 pcie->perst_reset = devm_reset_control_get_optional_exclusive(&pdev->dev, "perst");
1934 if (IS_ERR(pcie->perst_reset)) {
1935 clk_disable_unprepare(pcie->clk);
1936 return PTR_ERR(pcie->perst_reset);
1938 pcie->bridge_reset =
1939 devm_reset_control_get_optional_exclusive(&pdev->dev, "bridge");
1940 if (IS_ERR(pcie->bridge_reset)) {
1941 clk_disable_unprepare(pcie->clk);
1942 return PTR_ERR(pcie->bridge_reset);
1945 ret = reset_control_deassert(pcie->rescal);
1947 dev_err(&pdev->dev, "failed to deassert 'rescal'\n");
1949 ret = brcm_phy_start(pcie);
1951 reset_control_assert(pcie->rescal);
1952 clk_disable_unprepare(pcie->clk);
1956 ret = brcm_pcie_setup(pcie);
1960 pcie->hw_rev = readl(pcie->base + PCIE_MISC_REVISION);
1961 if (pcie->type == BCM4908 && pcie->hw_rev >= BRCM_PCIE_HW_REV_3_20) {
1962 dev_err(pcie->dev, "hardware revision with unsupported PERST# setup\n");
1967 msi_np = of_parse_phandle(pcie->np, "msi-parent", 0);
1968 if (pci_msi_enabled() && msi_np == pcie->np) {
1969 ret = brcm_pcie_enable_msi(pcie);
1971 dev_err(pcie->dev, "probe of internal MSI failed");
1974 } else if (pci_msi_enabled() && msi_np != pcie->np) {
1975 /* Use RC_BAR1 for MIP access */
1979 if (of_property_read_u64(msi_np, "brcm,msi-pci-addr", &msi_pci_addr)) {
1980 dev_err(pcie->dev, "Unable to find MSI PCI address\n");
1985 if (of_property_read_u64(msi_np, "reg", &msi_phys_addr)) {
1986 dev_err(pcie->dev, "Unable to find MSI physical address\n");
1991 writel(lower_32_bits(msi_pci_addr) | brcm_pcie_encode_ibar_size(0x1000),
1992 pcie->base + PCIE_MISC_RC_BAR1_CONFIG_LO);
1993 writel(upper_32_bits(msi_pci_addr),
1994 pcie->base + PCIE_MISC_RC_BAR1_CONFIG_HI);
1996 writel(lower_32_bits(msi_phys_addr) |
1997 PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_ACCESS_ENABLE_MASK,
1998 pcie->base + PCIE_MISC_UBUS_BAR1_CONFIG_REMAP);
1999 writel(upper_32_bits(msi_phys_addr),
2000 pcie->base + PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_HI);
2003 bridge->ops = pcie->type == BCM7425 ? &brcm7425_pcie_ops : &brcm_pcie_ops;
2004 bridge->sysdata = pcie;
2006 platform_set_drvdata(pdev, pcie);
2008 ret = pci_host_probe(bridge);
2009 if (!ret && !brcm_pcie_link_up(pcie))
2013 brcm_pcie_remove(pdev);
2017 brcm_pcie_config_clkreq(pcie);
2022 __brcm_pcie_remove(pcie);
2026 MODULE_DEVICE_TABLE(of, brcm_pcie_match);
2028 static const struct dev_pm_ops brcm_pcie_pm_ops = {
2029 .suspend_noirq = brcm_pcie_suspend_noirq,
2030 .resume_noirq = brcm_pcie_resume_noirq,
2033 static struct platform_driver brcm_pcie_driver = {
2034 .probe = brcm_pcie_probe,
2035 .remove_new = brcm_pcie_remove,
2037 .name = "brcm-pcie",
2038 .of_match_table = brcm_pcie_match,
2039 .pm = &brcm_pcie_pm_ops,
2042 module_platform_driver(brcm_pcie_driver);
2044 MODULE_LICENSE("GPL");
2045 MODULE_DESCRIPTION("Broadcom STB PCIe RC driver");
2046 MODULE_AUTHOR("Broadcom");