618e84402eb505f4c7606799d5495a3fc97abd50
[platform/kernel/linux-rpi.git] / drivers / pci / controller / pcie-brcmstb.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2009 - 2019 Broadcom */
3
4 #include <linux/bitfield.h>
5 #include <linux/bitops.h>
6 #include <linux/clk.h>
7 #include <linux/compiler.h>
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/io.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>
35
36 #include "../pci.h"
37
38 /* BRCM_PCIE_CAP_REGS - Offset for the mandatory capability config regs */
39 #define BRCM_PCIE_CAP_REGS                              0x00ac
40
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
45
46 #define PCIE_RC_CFG_PRIV1_ID_VAL3                       0x043c
47 #define  PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK      0xffffff
48
49 #define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY                       0x04dc
50 #define  PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_ASPM_SUPPORT_MASK    0xc00
51
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
56
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
60
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
64
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
68
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
75
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
80
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)
84
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)
88
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
92
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
96
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
100
101 #define PCIE_MISC_MSI_BAR_CONFIG_LO                     0x4044
102 #define PCIE_MISC_MSI_BAR_CONFIG_HI                     0x4048
103
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
107
108 #define PCIE_MISC_RC_CONFIG_RETRY_TIMEOUT               0x405c
109
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
113
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
120
121 #define PCIE_MISC_REVISION                              0x406c
122 #define  BRCM_PCIE_HW_REV_33                            0x0303
123 #define  BRCM_PCIE_HW_REV_3_20                          0x0320
124
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)
130
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)
135
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)
140
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_PERST_ASSERT_MASK               0x8
144 #define  PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK                0x08000000
145 #define  PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK          0x00800000
146 #define  PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK         0x00200000
147
148 #define PCIE_MISC_CTRL_1                                        0x40A0
149 #define  PCIE_MISC_CTRL_1_OUTBOUND_TC_MASK                      0xf
150 #define  PCIE_MISC_CTRL_1_OUTBOUND_NO_SNOOP_MASK                BIT(3)
151 #define  PCIE_MISC_CTRL_1_OUTBOUND_RO_MASK                      BIT(4)
152 #define  PCIE_MISC_CTRL_1_EN_VDM_QOS_CONTROL_MASK               BIT(5)
153
154 #define PCIE_MISC_UBUS_CTRL     0x40a4
155 #define  PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_ERR_DIS_MASK       BIT(13)
156 #define  PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_DECERR_DIS_MASK    BIT(19)
157
158 #define PCIE_MISC_UBUS_TIMEOUT  0x40A8
159
160 #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP        0x40ac
161 #define  PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_ACCESS_ENABLE_MASK    BIT(0)
162 #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_HI     0x40b0
163
164 #define PCIE_MISC_UBUS_BAR2_CONFIG_REMAP        0x40b4
165 #define  PCIE_MISC_UBUS_BAR2_CONFIG_REMAP_ACCESS_ENABLE_MASK    BIT(0)
166
167 /* Additional RC BARs */
168 #define  PCIE_MISC_RC_BAR_CONFIG_LO_SIZE_MASK           0x1f
169 #define PCIE_MISC_RC_BAR4_CONFIG_LO                     0x40d4
170 #define PCIE_MISC_RC_BAR4_CONFIG_HI                     0x40d8
171 /* ... */
172 #define PCIE_MISC_RC_BAR10_CONFIG_LO                    0x4104
173 #define PCIE_MISC_RC_BAR10_CONFIG_HI                    0x4108
174
175 #define PCIE_MISC_UBUS_BAR_CONFIG_REMAP_ENABLE          0x1
176 #define PCIE_MISC_UBUS_BAR_CONFIG_REMAP_LO_MASK         0xfffff000
177 #define PCIE_MISC_UBUS_BAR_CONFIG_REMAP_HI_MASK         0xff
178 #define PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_LO             0x410c
179 #define PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_HI             0x4110
180 /* ... */
181 #define PCIE_MISC_UBUS_BAR10_CONFIG_REMAP_LO            0x413c
182 #define PCIE_MISC_UBUS_BAR10_CONFIG_REMAP_HI            0x4140
183
184 /* AXI priority forwarding - automatic level-based */
185 #define PCIE_MISC_TC_QUEUE_TO_QOS_MAP(x)                (0x4160 - (x) * 4)
186 /* Defined in quarter-fullness */
187 #define  QUEUE_THRESHOLD_34_TO_QOS_MAP_SHIFT            12
188 #define  QUEUE_THRESHOLD_23_TO_QOS_MAP_SHIFT            8
189 #define  QUEUE_THRESHOLD_12_TO_QOS_MAP_SHIFT            4
190 #define  QUEUE_THRESHOLD_01_TO_QOS_MAP_SHIFT            0
191 #define  QUEUE_THRESHOLD_MASK                           0xf
192
193 /* VDM messages indexing TCs to AXI priorities */
194 /* Indexes 8-15 */
195 #define PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_HI            0x4164
196 /* Indexes 0-7 */
197 #define PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_LO            0x4168
198 #define  VDM_PRIORITY_TO_QOS_MAP_SHIFT(x)               (4 * (x))
199 #define  VDM_PRIORITY_TO_QOS_MAP_MASK                   0xf
200
201 #define PCIE_MISC_AXI_INTF_CTRL 0x416C
202 #define  AXI_REQFIFO_EN_QOS_PROPAGATION                 BIT(7)
203 #define  AXI_BRIDGE_LOW_LATENCY_MODE                    BIT(6)
204 #define  AXI_MASTER_MAX_OUTSTANDING_REQUESTS_MASK       0x3f
205
206 #define PCIE_MISC_AXI_READ_ERROR_DATA   0x4170
207
208 #define PCIE_INTR2_CPU_BASE             (pcie->reg_offsets[INTR2_CPU])
209 #define PCIE_MSI_INTR2_BASE             0x4500
210 /* Offsets from PCIE_INTR2_CPU_BASE and PCIE_MSI_INTR2_BASE */
211 #define  MSI_INT_STATUS                 0x0
212 #define  MSI_INT_CLR                    0x8
213 #define  MSI_INT_MASK_SET               0x10
214 #define  MSI_INT_MASK_CLR               0x14
215
216 #define PCIE_EXT_CFG_DATA                               0x8000
217 #define PCIE_EXT_CFG_INDEX                              0x9000
218
219 #define  PCIE_RGR1_SW_INIT_1_PERST_MASK                 0x1
220 #define  PCIE_RGR1_SW_INIT_1_PERST_SHIFT                0x0
221
222 #define RGR1_SW_INIT_1_INIT_GENERIC_MASK                0x2
223 #define RGR1_SW_INIT_1_INIT_GENERIC_SHIFT               0x1
224 #define RGR1_SW_INIT_1_INIT_7278_MASK                   0x1
225 #define RGR1_SW_INIT_1_INIT_7278_SHIFT                  0x0
226
227 /* PCIe parameters */
228 #define BRCM_NUM_PCIE_OUT_WINS          0x4
229 #define BRCM_INT_PCI_MSI_NR             32
230 #define BRCM_INT_PCI_MSI_LEGACY_NR      8
231 #define BRCM_INT_PCI_MSI_SHIFT          0
232 #define BRCM_INT_PCI_MSI_MASK           GENMASK(BRCM_INT_PCI_MSI_NR - 1, 0)
233 #define BRCM_INT_PCI_MSI_LEGACY_MASK    GENMASK(31, \
234                                                 32 - BRCM_INT_PCI_MSI_LEGACY_NR)
235
236 /* MSI target addresses */
237 #define BRCM_MSI_TARGET_ADDR_LT_4GB     0x0fffffffcULL
238 #define BRCM_MSI_TARGET_ADDR_GT_4GB     0xffffffffcULL
239
240 /* MDIO registers */
241 #define MDIO_PORT0                      0x0
242 #define MDIO_DATA_MASK                  0x7fffffff
243 #define MDIO_PORT_MASK                  0xf0000
244 #define MDIO_REGAD_MASK                 0xffff
245 #define MDIO_CMD_MASK                   0xfff00000
246 #define MDIO_CMD_READ                   0x1
247 #define MDIO_CMD_WRITE                  0x0
248 #define MDIO_DATA_DONE_MASK             0x80000000
249 #define MDIO_RD_DONE(x)                 (((x) & MDIO_DATA_DONE_MASK) ? 1 : 0)
250 #define MDIO_WT_DONE(x)                 (((x) & MDIO_DATA_DONE_MASK) ? 0 : 1)
251 #define SSC_REGS_ADDR                   0x1100
252 #define SET_ADDR_OFFSET                 0x1f
253 #define SSC_CNTL_OFFSET                 0x2
254 #define SSC_CNTL_OVRD_EN_MASK           0x8000
255 #define SSC_CNTL_OVRD_VAL_MASK          0x4000
256 #define SSC_STATUS_OFFSET               0x1
257 #define SSC_STATUS_SSC_MASK             0x400
258 #define SSC_STATUS_PLL_LOCK_MASK        0x800
259 #define PCIE_BRCM_MAX_MEMC              3
260
261 #define IDX_ADDR(pcie)                  (pcie->reg_offsets[EXT_CFG_INDEX])
262 #define DATA_ADDR(pcie)                 (pcie->reg_offsets[EXT_CFG_DATA])
263 #define PCIE_RGR1_SW_INIT_1(pcie)       (pcie->reg_offsets[RGR1_SW_INIT_1])
264
265 /* Rescal registers */
266 #define PCIE_DVT_PMU_PCIE_PHY_CTRL                              0xc700
267 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS                  0x3
268 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_MASK         0x4
269 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_SHIFT        0x2
270 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_MASK             0x2
271 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_SHIFT            0x1
272 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_MASK             0x1
273 #define  PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_SHIFT            0x0
274
275 /* Forward declarations */
276 struct brcm_pcie;
277
278 enum {
279         RGR1_SW_INIT_1,
280         EXT_CFG_INDEX,
281         EXT_CFG_DATA,
282         PCIE_HARD_DEBUG,
283         INTR2_CPU,
284 };
285
286 enum {
287         RGR1_SW_INIT_1_INIT_MASK,
288         RGR1_SW_INIT_1_INIT_SHIFT,
289 };
290
291 enum pcie_type {
292         GENERIC,
293         BCM7425,
294         BCM7435,
295         BCM4908,
296         BCM7278,
297         BCM2711,
298         BCM2712,
299 };
300
301 struct pcie_cfg_data {
302         const int *offsets;
303         const enum pcie_type type;
304         void (*perst_set)(struct brcm_pcie *pcie, u32 val);
305         void (*bridge_sw_init_set)(struct brcm_pcie *pcie, u32 val);
306         bool (*rc_mode)(struct brcm_pcie *pcie);
307 };
308
309 struct subdev_regulators {
310         unsigned int num_supplies;
311         struct regulator_bulk_data supplies[];
312 };
313
314 struct brcm_msi {
315         struct device           *dev;
316         void __iomem            *base;
317         struct device_node      *np;
318         struct irq_domain       *msi_domain;
319         struct irq_domain       *inner_domain;
320         struct mutex            lock; /* guards the alloc/free operations */
321         u64                     target_addr;
322         int                     irq;
323         DECLARE_BITMAP(used, 64);
324         bool                    legacy;
325         /* Some chips have MSIs in bits [31..24] of a shared register. */
326         int                     legacy_shift;
327         int                     nr; /* No. of MSI available, depends on chip */
328         /* This is the base pointer for interrupt status/set/clr regs */
329         void __iomem            *intr_base;
330 };
331
332 /* Internal PCIe Host Controller Information.*/
333 struct brcm_pcie {
334         struct device           *dev;
335         void __iomem            *base;
336         struct clk              *clk;
337         struct device_node      *np;
338         bool                    ssc;
339         bool                    l1ss;
340         bool                    rcb_mps_mode;
341         int                     gen;
342         u64                     msi_target_addr;
343         struct brcm_msi         *msi;
344         const int               *reg_offsets;
345         enum pcie_type          type;
346         struct reset_control    *rescal;
347         struct reset_control    *perst_reset;
348         struct reset_control    *bridge_reset;
349         int                     num_memc;
350         u64                     memc_size[PCIE_BRCM_MAX_MEMC];
351         u32                     hw_rev;
352         u32                     qos_map;
353         void                    (*perst_set)(struct brcm_pcie *pcie, u32 val);
354         void                    (*bridge_sw_init_set)(struct brcm_pcie *pcie, u32 val);
355         bool                    (*rc_mode)(struct brcm_pcie *pcie);
356         struct subdev_regulators *sr;
357         bool                    ep_wakeup_capable;
358         u32                     tperst_clk_ms;
359 };
360
361 static inline bool is_bmips(const struct brcm_pcie *pcie)
362 {
363         return pcie->type == BCM7435 || pcie->type == BCM7425;
364 }
365
366 /*
367  * This is to convert the size of the inbound "BAR" region to the
368  * non-linear values of PCIE_X_MISC_RC_BAR[123]_CONFIG_LO.SIZE
369  */
370 static int brcm_pcie_encode_ibar_size(u64 size)
371 {
372         int log2_in = ilog2(size);
373
374         if (log2_in >= 12 && log2_in <= 15)
375                 /* Covers 4KB to 32KB (inclusive) */
376                 return (log2_in - 12) + 0x1c;
377         else if (log2_in >= 16 && log2_in <= 36)
378                 /* Covers 64KB to 64GB, (inclusive) */
379                 return log2_in - 15;
380         /* Something is awry so disable */
381         return 0;
382 }
383
384 static u32 brcm_pcie_mdio_form_pkt(int port, int regad, int cmd)
385 {
386         u32 pkt = 0;
387
388         pkt |= FIELD_PREP(MDIO_PORT_MASK, port);
389         pkt |= FIELD_PREP(MDIO_REGAD_MASK, regad);
390         pkt |= FIELD_PREP(MDIO_CMD_MASK, cmd);
391
392         return pkt;
393 }
394
395 /* negative return value indicates error */
396 static int brcm_pcie_mdio_read(void __iomem *base, u8 port, u8 regad, u32 *val)
397 {
398         u32 data;
399         int err;
400
401         writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_READ),
402                    base + PCIE_RC_DL_MDIO_ADDR);
403         readl(base + PCIE_RC_DL_MDIO_ADDR);
404         err = readl_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_RD_DATA, data,
405                                         MDIO_RD_DONE(data), 10, 100);
406         *val = FIELD_GET(MDIO_DATA_MASK, data);
407
408         return err;
409 }
410
411 /* negative return value indicates error */
412 static int brcm_pcie_mdio_write(void __iomem *base, u8 port,
413                                 u8 regad, u16 wrdata)
414 {
415         u32 data;
416         int err;
417
418         writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_WRITE),
419                    base + PCIE_RC_DL_MDIO_ADDR);
420         readl(base + PCIE_RC_DL_MDIO_ADDR);
421         writel(MDIO_DATA_DONE_MASK | wrdata, base + PCIE_RC_DL_MDIO_WR_DATA);
422
423         err = readw_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_WR_DATA, data,
424                                         MDIO_WT_DONE(data), 10, 100);
425         return err;
426 }
427
428 /*
429  * Configures device for Spread Spectrum Clocking (SSC) mode; a negative
430  * return value indicates error.
431  */
432 static int brcm_pcie_set_ssc(struct brcm_pcie *pcie)
433 {
434         int pll, ssc;
435         int ret;
436         u32 tmp;
437
438         ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
439                                    SSC_REGS_ADDR);
440         if (ret < 0)
441                 return ret;
442
443         ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
444                                   SSC_CNTL_OFFSET, &tmp);
445         if (ret < 0)
446                 return ret;
447
448         u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_EN_MASK);
449         u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_VAL_MASK);
450         ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0,
451                                    SSC_CNTL_OFFSET, tmp);
452         if (ret < 0)
453                 return ret;
454
455         usleep_range(1000, 2000);
456         ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
457                                   SSC_STATUS_OFFSET, &tmp);
458         if (ret < 0)
459                 return ret;
460
461         ssc = FIELD_GET(SSC_STATUS_SSC_MASK, tmp);
462         pll = FIELD_GET(SSC_STATUS_PLL_LOCK_MASK, tmp);
463
464         return ssc && pll ? 0 : -EIO;
465 }
466
467 static void brcm_pcie_munge_pll(struct brcm_pcie *pcie)
468 {
469         //print "MDIO block 0x1600 written per Dannys instruction"
470         //tmp = pcie_mdio_write(phyad, &h16&, &h50b9&)
471         //tmp = pcie_mdio_write(phyad, &h17&, &hbd1a&)
472         //tmp = pcie_mdio_write(phyad, &h1b&, &h5030&)
473         //tmp = pcie_mdio_write(phyad, &h1e&, &h0007&)
474
475         u32 tmp;
476         int ret, i;
477         u8 regs[] =  { 0x16,   0x17,   0x18,   0x19,   0x1b,   0x1c,   0x1e };
478         u16 data[] = { 0x50b9, 0xbda1, 0x0094, 0x97b4, 0x5030, 0x5030, 0x0007 };
479
480         ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
481                                 0x1600);
482         for (i = 0; i < ARRAY_SIZE(regs); i++) {
483                 brcm_pcie_mdio_read(pcie->base, MDIO_PORT0, regs[i], &tmp);
484                 dev_dbg(pcie->dev, "PCIE MDIO pre_refclk 0x%02x = 0x%04x\n",
485                         regs[i], tmp);
486         }
487         for (i = 0; i < ARRAY_SIZE(regs); i++) {
488                 brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, regs[i], data[i]);
489                 brcm_pcie_mdio_read(pcie->base, MDIO_PORT0, regs[i], &tmp);
490                 dev_dbg(pcie->dev, "PCIE MDIO post_refclk 0x%02x = 0x%04x\n",
491                         regs[i], tmp);
492         }
493         usleep_range(100, 200);
494 }
495
496 /* Limits operation to a specific generation (1, 2, or 3) */
497 static void brcm_pcie_set_gen(struct brcm_pcie *pcie, int gen)
498 {
499         u16 lnkctl2 = readw(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
500         u32 lnkcap = readl(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP);
501
502         lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen;
503         writel(lnkcap, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP);
504
505         lnkctl2 = (lnkctl2 & ~0xf) | gen;
506         writew(lnkctl2, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
507 }
508
509 static void brcm_pcie_set_outbound_win(struct brcm_pcie *pcie,
510                                        unsigned int win, u64 cpu_addr,
511                                        u64 pcie_addr, u64 size)
512 {
513         u32 cpu_addr_mb_high, limit_addr_mb_high;
514         phys_addr_t cpu_addr_mb, limit_addr_mb;
515         int high_addr_shift;
516         u32 tmp;
517
518         /* Set the base of the pcie_addr window */
519         writel(lower_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_LO(win));
520         writel(upper_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_HI(win));
521
522         /* Write the addr base & limit lower bits (in MBs) */
523         cpu_addr_mb = cpu_addr / SZ_1M;
524         limit_addr_mb = (cpu_addr + size - 1) / SZ_1M;
525
526         tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win));
527         u32p_replace_bits(&tmp, cpu_addr_mb,
528                           PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
529         u32p_replace_bits(&tmp, limit_addr_mb,
530                           PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK);
531         writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win));
532
533         if (is_bmips(pcie))
534                 return;
535
536         /* Write the cpu & limit addr upper bits */
537         high_addr_shift =
538                 HWEIGHT32(PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
539
540         cpu_addr_mb_high = cpu_addr_mb >> high_addr_shift;
541         tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_HI(win));
542         u32p_replace_bits(&tmp, cpu_addr_mb_high,
543                           PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK);
544         writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_HI(win));
545
546         limit_addr_mb_high = limit_addr_mb >> high_addr_shift;
547         tmp = readl(pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win));
548         u32p_replace_bits(&tmp, limit_addr_mb_high,
549                           PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK);
550         writel(tmp, pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win));
551 }
552
553 static void brcm_pcie_set_tc_qos(struct brcm_pcie *pcie)
554 {
555         int i;
556         u32 reg;
557
558         if (pcie->type != BCM2712)
559                 return;
560
561         /* XXX: BCM2712C0 is broken, disable the forwarding search */
562         reg = readl(pcie->base + PCIE_MISC_AXI_INTF_CTRL);
563         reg &= ~AXI_REQFIFO_EN_QOS_PROPAGATION;
564         writel(reg, pcie->base + PCIE_MISC_AXI_INTF_CTRL);
565
566         /* Disable VDM reception by default - QoS map defaults to 0 */
567         reg = readl(pcie->base + PCIE_MISC_CTRL_1);
568         reg &= ~PCIE_MISC_CTRL_1_EN_VDM_QOS_CONTROL_MASK;
569         writel(reg, pcie->base + PCIE_MISC_CTRL_1);
570
571         if (!of_property_read_u32(pcie->np, "brcm,fifo-qos-map", &pcie->qos_map)) {
572                 /*
573                  * Backpressure mode - bottom 4 nibbles are QoS for each
574                  * quartile of FIFO level. Each TC gets the same map, because
575                  * this mode is intended for nonrealtime EPs.
576                  */
577
578                 pcie->qos_map &= 0x0000ffff;
579                 for (i = 0; i < 8; i++)
580                         writel(pcie->qos_map, pcie->base + PCIE_MISC_TC_QUEUE_TO_QOS_MAP(i));
581
582                 return;
583         }
584
585         if (!of_property_read_u32(pcie->np, "brcm,vdm-qos-map", &pcie->qos_map)) {
586
587                 reg = readl(pcie->base + PCIE_MISC_CTRL_1);
588                 reg |= PCIE_MISC_CTRL_1_EN_VDM_QOS_CONTROL_MASK;
589                 writel(reg, pcie->base + PCIE_MISC_CTRL_1);
590
591                 /* No forwarding means no point separating panic priorities from normal */
592                 writel(pcie->qos_map, pcie->base + PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_LO);
593                 writel(pcie->qos_map, pcie->base + PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_HI);
594
595                 /* Match Vendor ID of 0 */
596                 writel(0, pcie->base + PCIE_RC_TL_VDM_CTL1);
597                 /* Forward VDMs to priority interface - at least the rx counters work */
598                 reg = readl(pcie->base + PCIE_RC_TL_VDM_CTL0);
599                 reg |= PCIE_RC_TL_VDM_CTL0_VDM_ENABLED_MASK |
600                         PCIE_RC_TL_VDM_CTL0_VDM_IGNORETAG_MASK |
601                         PCIE_RC_TL_VDM_CTL0_VDM_IGNOREVNDRID_MASK;
602                 writel(reg, pcie->base + PCIE_RC_TL_VDM_CTL0);
603         }
604 }
605
606 static void brcm_pcie_config_clkreq(struct brcm_pcie *pcie)
607 {
608         void __iomem *base = pcie->base;
609         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
610         int domain = pci_domain_nr(bridge->bus);
611         const struct pci_bus *bus = pci_find_bus(domain, 1);
612         struct pci_dev *pdev = (struct pci_dev *)bus->devices.next;
613         u32 tmp, link_cap = 0;
614         u16 link_ctl = 0;
615         int clkpm = 0;
616         int substates = 0;
617
618         pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
619         if ((link_cap & PCI_EXP_LNKCAP_CLKPM))
620                 clkpm = 1;
621
622         pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &link_ctl);
623         if (!(link_ctl & PCI_EXP_LNKCTL_CLKREQ_EN))
624                 clkpm = 0;
625
626         if (pcie->l1ss && pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS))
627                 substates = 1;
628
629         tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
630         tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK;
631         tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK;
632
633         if (substates)
634                 tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK;
635         else if (clkpm)
636                 tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK;
637
638         writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
639
640         if (substates || clkpm)
641                 dev_info(pcie->dev, "clkreq control enabled\n");
642 }
643
644 static struct irq_chip brcm_msi_irq_chip = {
645         .name            = "BRCM STB PCIe MSI",
646         .irq_ack         = irq_chip_ack_parent,
647         .irq_mask        = pci_msi_mask_irq,
648         .irq_unmask      = pci_msi_unmask_irq,
649 };
650
651 static struct msi_domain_info brcm_msi_domain_info = {
652         .flags  = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
653                    MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
654         .chip   = &brcm_msi_irq_chip,
655 };
656
657 static void brcm_pcie_msi_isr(struct irq_desc *desc)
658 {
659         struct irq_chip *chip = irq_desc_get_chip(desc);
660         unsigned long status, virq;
661         struct brcm_msi *msi;
662         struct device *dev;
663         u32 bit;
664
665         chained_irq_enter(chip, desc);
666         msi = irq_desc_get_handler_data(desc);
667         dev = msi->dev;
668
669         status = readl(msi->intr_base + MSI_INT_STATUS);
670         status >>= msi->legacy_shift;
671
672         for_each_set_bit(bit, &status, BRCM_INT_PCI_MSI_NR/*msi->nr*/) {
673                 bool found = false;
674
675                 virq = irq_find_mapping(msi->inner_domain, bit);
676                 if (virq) {
677                         found = true;
678                         dev_dbg(dev, "MSI -> %ld\n", virq);
679                         generic_handle_irq(virq);
680                 }
681                 virq = irq_find_mapping(msi->inner_domain, bit + 32);
682                 if (virq) {
683                         found = true;
684                         dev_dbg(dev, "MSI -> %ld\n", virq);
685                         generic_handle_irq(virq);
686                 }
687                 if (!found)
688                         dev_dbg(dev, "unexpected MSI\n");
689         }
690
691         chained_irq_exit(chip, desc);
692 }
693
694 static void brcm_msi_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
695 {
696         struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
697
698         msg->address_lo = lower_32_bits(msi->target_addr);
699         msg->address_hi = upper_32_bits(msi->target_addr);
700         msg->data = (0xffff & PCIE_MISC_MSI_DATA_CONFIG_VAL_32) | (data->hwirq & 0x1f);
701 }
702
703 static int brcm_msi_set_affinity(struct irq_data *irq_data,
704                                  const struct cpumask *mask, bool force)
705 {
706         return -EINVAL;
707 }
708
709 static void brcm_msi_ack_irq(struct irq_data *data)
710 {
711         struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
712         const int shift_amt = (data->hwirq & 0x1f) + msi->legacy_shift;
713
714         writel(1 << shift_amt, msi->intr_base + MSI_INT_CLR);
715 }
716
717
718 static struct irq_chip brcm_msi_bottom_irq_chip = {
719         .name                   = "BRCM STB MSI",
720         .irq_compose_msi_msg    = brcm_msi_compose_msi_msg,
721         .irq_set_affinity       = brcm_msi_set_affinity,
722         .irq_ack                = brcm_msi_ack_irq,
723 };
724
725 static int brcm_msi_alloc(struct brcm_msi *msi, unsigned int nr_irqs)
726 {
727         int hwirq;
728
729         mutex_lock(&msi->lock);
730         hwirq = bitmap_find_free_region(msi->used, msi->nr,
731                                         order_base_2(nr_irqs));
732         mutex_unlock(&msi->lock);
733
734         return hwirq;
735 }
736
737 static void brcm_msi_free(struct brcm_msi *msi, unsigned long hwirq,
738                           unsigned int nr_irqs)
739 {
740         mutex_lock(&msi->lock);
741         bitmap_release_region(msi->used, hwirq, order_base_2(nr_irqs));
742         mutex_unlock(&msi->lock);
743 }
744
745 static int brcm_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
746                                  unsigned int nr_irqs, void *args)
747 {
748         struct brcm_msi *msi = domain->host_data;
749         int hwirq, i;
750
751         hwirq = brcm_msi_alloc(msi, nr_irqs);
752
753         if (hwirq < 0)
754                 return hwirq;
755
756         for (i = 0; i < nr_irqs; i++)
757                 irq_domain_set_info(domain, virq + i, hwirq + i,
758                                     &brcm_msi_bottom_irq_chip, domain->host_data,
759                                     handle_edge_irq, NULL, NULL);
760         return 0;
761 }
762
763 static void brcm_irq_domain_free(struct irq_domain *domain,
764                                  unsigned int virq, unsigned int nr_irqs)
765 {
766         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
767         struct brcm_msi *msi = irq_data_get_irq_chip_data(d);
768
769         brcm_msi_free(msi, d->hwirq, nr_irqs);
770 }
771
772 static const struct irq_domain_ops msi_domain_ops = {
773         .alloc  = brcm_irq_domain_alloc,
774         .free   = brcm_irq_domain_free,
775 };
776
777 static int brcm_allocate_domains(struct brcm_msi *msi)
778 {
779         struct fwnode_handle *fwnode = of_node_to_fwnode(msi->np);
780         struct device *dev = msi->dev;
781
782         msi->inner_domain = irq_domain_add_linear(NULL, msi->nr, &msi_domain_ops, msi);
783         if (!msi->inner_domain) {
784                 dev_err(dev, "failed to create IRQ domain\n");
785                 return -ENOMEM;
786         }
787
788         msi->msi_domain = pci_msi_create_irq_domain(fwnode,
789                                                     &brcm_msi_domain_info,
790                                                     msi->inner_domain);
791         if (!msi->msi_domain) {
792                 dev_err(dev, "failed to create MSI domain\n");
793                 irq_domain_remove(msi->inner_domain);
794                 return -ENOMEM;
795         }
796
797         return 0;
798 }
799
800 static void brcm_free_domains(struct brcm_msi *msi)
801 {
802         irq_domain_remove(msi->msi_domain);
803         irq_domain_remove(msi->inner_domain);
804 }
805
806 static void brcm_msi_remove(struct brcm_pcie *pcie)
807 {
808         struct brcm_msi *msi = pcie->msi;
809
810         if (!msi)
811                 return;
812         irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
813         brcm_free_domains(msi);
814 }
815
816 static void brcm_msi_set_regs(struct brcm_msi *msi)
817 {
818         u32 val = msi->legacy ? BRCM_INT_PCI_MSI_LEGACY_MASK :
819                                 BRCM_INT_PCI_MSI_MASK;
820
821         writel(val, msi->intr_base + MSI_INT_MASK_CLR);
822         writel(val, msi->intr_base + MSI_INT_CLR);
823
824         /*
825          * The 0 bit of PCIE_MISC_MSI_BAR_CONFIG_LO is repurposed to MSI
826          * enable, which we set to 1.
827          */
828         writel(lower_32_bits(msi->target_addr) | 0x1,
829                msi->base + PCIE_MISC_MSI_BAR_CONFIG_LO);
830         writel(upper_32_bits(msi->target_addr),
831                msi->base + PCIE_MISC_MSI_BAR_CONFIG_HI);
832
833         val = msi->legacy ? PCIE_MISC_MSI_DATA_CONFIG_VAL_8 : PCIE_MISC_MSI_DATA_CONFIG_VAL_32;
834         writel(val, msi->base + PCIE_MISC_MSI_DATA_CONFIG);
835 }
836
837 static int brcm_pcie_enable_msi(struct brcm_pcie *pcie)
838 {
839         struct brcm_msi *msi;
840         int irq, ret;
841         struct device *dev = pcie->dev;
842
843         irq = irq_of_parse_and_map(dev->of_node, 1);
844         if (irq <= 0) {
845                 dev_err(dev, "cannot map MSI interrupt\n");
846                 return -ENODEV;
847         }
848
849         msi = devm_kzalloc(dev, sizeof(struct brcm_msi), GFP_KERNEL);
850         if (!msi)
851                 return -ENOMEM;
852
853         mutex_init(&msi->lock);
854         msi->dev = dev;
855         msi->base = pcie->base;
856         msi->np = pcie->np;
857         msi->target_addr = pcie->msi_target_addr;
858         msi->irq = irq;
859         msi->legacy = pcie->hw_rev < BRCM_PCIE_HW_REV_33;
860
861         /*
862          * Sanity check to make sure that the 'used' bitmap in struct brcm_msi
863          * is large enough.
864          */
865         BUILD_BUG_ON(BRCM_INT_PCI_MSI_LEGACY_NR > BRCM_INT_PCI_MSI_NR);
866
867         if (msi->legacy) {
868                 msi->intr_base = msi->base + PCIE_INTR2_CPU_BASE;
869                 msi->nr = BRCM_INT_PCI_MSI_LEGACY_NR;
870                 msi->legacy_shift = 24;
871         } else {
872                 msi->intr_base = msi->base + PCIE_MSI_INTR2_BASE;
873                 msi->nr = 64; //BRCM_INT_PCI_MSI_NR;
874                 msi->legacy_shift = 0;
875         }
876
877         ret = brcm_allocate_domains(msi);
878         if (ret)
879                 return ret;
880
881         irq_set_chained_handler_and_data(msi->irq, brcm_pcie_msi_isr, msi);
882
883         brcm_msi_set_regs(msi);
884         pcie->msi = msi;
885
886         return 0;
887 }
888
889 /* The controller is capable of serving in both RC and EP roles */
890 static bool brcm_pcie_rc_mode_generic(struct brcm_pcie *pcie)
891 {
892         void __iomem *base = pcie->base;
893         u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
894
895         return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK, val);
896 }
897
898 static bool brcm_pcie_rc_mode_2712(struct brcm_pcie *pcie)
899 {
900         void __iomem *base = pcie->base;
901         u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
902
903         return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK_2712, val) | 1; //XXX
904 }
905
906 static bool brcm_pcie_link_up(struct brcm_pcie *pcie)
907 {
908         u32 val = readl(pcie->base + PCIE_MISC_PCIE_STATUS);
909         u32 dla = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK, val);
910         u32 plu = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK, val);
911
912         return dla && plu;
913 }
914
915 static void __iomem *brcm_pcie_map_bus(struct pci_bus *bus,
916                                        unsigned int devfn, int where)
917 {
918         struct brcm_pcie *pcie = bus->sysdata;
919         void __iomem *base = pcie->base;
920         int idx;
921
922         /* Accesses to the RC go right to the RC registers if !devfn */
923         if (pci_is_root_bus(bus))
924                 return devfn ? NULL : base + PCIE_ECAM_REG(where);
925
926         /* An access to our HW w/o link-up will cause a CPU Abort */
927         if (!brcm_pcie_link_up(pcie))
928                 return NULL;
929
930         /* For devices, write to the config space index register */
931         idx = PCIE_ECAM_OFFSET(bus->number, devfn, 0);
932         writel(idx, pcie->base + PCIE_EXT_CFG_INDEX);
933         return base + PCIE_EXT_CFG_DATA + PCIE_ECAM_REG(where);
934 }
935
936 static void __iomem *brcm7425_pcie_map_bus(struct pci_bus *bus,
937                                            unsigned int devfn, int where)
938 {
939         struct brcm_pcie *pcie = bus->sysdata;
940         void __iomem *base = pcie->base;
941         int idx;
942
943         /* Accesses to the RC go right to the RC registers if !devfn */
944         if (pci_is_root_bus(bus))
945                 return devfn ? NULL : base + PCIE_ECAM_REG(where);
946
947         /* An access to our HW w/o link-up will cause a CPU Abort */
948         if (!brcm_pcie_link_up(pcie))
949                 return NULL;
950
951         /* For devices, write to the config space index register */
952         idx = PCIE_ECAM_OFFSET(bus->number, devfn, where);
953         writel(idx, base + IDX_ADDR(pcie));
954         return base + DATA_ADDR(pcie);
955 }
956
957 static void brcm_pcie_bridge_sw_init_set_generic(struct brcm_pcie *pcie, u32 val)
958 {
959         u32 tmp, mask =  RGR1_SW_INIT_1_INIT_GENERIC_MASK;
960         u32 shift = RGR1_SW_INIT_1_INIT_GENERIC_SHIFT;
961
962         tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
963         tmp = (tmp & ~mask) | ((val << shift) & mask);
964         writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
965 }
966
967 static void brcm_pcie_bridge_sw_init_set_7278(struct brcm_pcie *pcie, u32 val)
968 {
969         u32 tmp, mask =  RGR1_SW_INIT_1_INIT_7278_MASK;
970         u32 shift = RGR1_SW_INIT_1_INIT_7278_SHIFT;
971
972         tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
973         tmp = (tmp & ~mask) | ((val << shift) & mask);
974         writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
975 }
976
977 static void brcm_pcie_bridge_sw_init_set_2712(struct brcm_pcie *pcie, u32 val)
978 {
979         if (WARN_ONCE(!pcie->bridge_reset,
980                       "missing bridge reset controller\n"))
981                 return;
982
983         if (val)
984                 reset_control_assert(pcie->bridge_reset);
985         else
986                 reset_control_deassert(pcie->bridge_reset);
987 }
988
989 static void brcm_pcie_perst_set_4908(struct brcm_pcie *pcie, u32 val)
990 {
991         if (WARN_ONCE(!pcie->perst_reset, "missing PERST# reset controller\n"))
992                 return;
993
994         if (val)
995                 reset_control_assert(pcie->perst_reset);
996         else
997                 reset_control_deassert(pcie->perst_reset);
998 }
999
1000 static void brcm_pcie_perst_set_7278(struct brcm_pcie *pcie, u32 val)
1001 {
1002         u32 tmp;
1003
1004         /* Perst bit has moved and assert value is 0 */
1005         tmp = readl(pcie->base + PCIE_MISC_PCIE_CTRL);
1006         u32p_replace_bits(&tmp, !val, PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK);
1007         writel(tmp, pcie->base +  PCIE_MISC_PCIE_CTRL);
1008 }
1009
1010 static void brcm_pcie_perst_set_2712(struct brcm_pcie *pcie, u32 val)
1011 {
1012         u32 tmp;
1013
1014         /* Perst bit has moved and assert value is 0 */
1015         tmp = readl(pcie->base + PCIE_MISC_PCIE_CTRL);
1016         u32p_replace_bits(&tmp, !val, PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK);
1017         writel(tmp, pcie->base +  PCIE_MISC_PCIE_CTRL);
1018 }
1019
1020 static void brcm_pcie_perst_set_generic(struct brcm_pcie *pcie, u32 val)
1021 {
1022         u32 tmp;
1023
1024         tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
1025         u32p_replace_bits(&tmp, val, PCIE_RGR1_SW_INIT_1_PERST_MASK);
1026         writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
1027 }
1028
1029 static int brcm_pcie_get_rc_bar2_size_and_offset(struct brcm_pcie *pcie,
1030                                                         u64 *rc_bar2_size,
1031                                                         u64 *rc_bar2_offset)
1032 {
1033         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1034         struct resource_entry *entry;
1035         struct device *dev = pcie->dev;
1036         u64 lowest_pcie_addr = ~(u64)0;
1037         int ret, i = 0;
1038         u64 size = 0;
1039
1040         resource_list_for_each_entry(entry, &bridge->dma_ranges) {
1041                 u64 pcie_beg = entry->res->start - entry->offset;
1042
1043                 size += entry->res->end - entry->res->start + 1;
1044                 if (pcie_beg < lowest_pcie_addr)
1045                         lowest_pcie_addr = pcie_beg;
1046                 if (pcie->type == BCM2711 || pcie->type == BCM2712)
1047                         break; // Only consider the first entry
1048         }
1049
1050         if (lowest_pcie_addr == ~(u64)0) {
1051                 dev_err(dev, "DT node has no dma-ranges\n");
1052                 return -EINVAL;
1053         }
1054
1055         ret = of_property_read_variable_u64_array(pcie->np, "brcm,scb-sizes", pcie->memc_size, 1,
1056                                                   PCIE_BRCM_MAX_MEMC);
1057
1058         if (ret <= 0) {
1059                 /* Make an educated guess */
1060                 pcie->num_memc = 1;
1061                 pcie->memc_size[0] = 1ULL << fls64(size - 1);
1062         } else {
1063                 pcie->num_memc = ret;
1064         }
1065
1066         /* Each memc is viewed through a "port" that is a power of 2 */
1067         for (i = 0, size = 0; i < pcie->num_memc; i++)
1068                 size += pcie->memc_size[i];
1069
1070         /* System memory starts at this address in PCIe-space */
1071         *rc_bar2_offset = lowest_pcie_addr;
1072         /* The sum of all memc views must also be a power of 2 */
1073         *rc_bar2_size = 1ULL << fls64(size - 1);
1074
1075         /*
1076          * We validate the inbound memory view even though we should trust
1077          * whatever the device-tree provides. This is because of an HW issue on
1078          * early Raspberry Pi 4's revisions (bcm2711). It turns out its
1079          * firmware has to dynamically edit dma-ranges due to a bug on the
1080          * PCIe controller integration, which prohibits any access above the
1081          * lower 3GB of memory. Given this, we decided to keep the dma-ranges
1082          * in check, avoiding hard to debug device-tree related issues in the
1083          * future:
1084          *
1085          * The PCIe host controller by design must set the inbound viewport to
1086          * be a contiguous arrangement of all of the system's memory.  In
1087          * addition, its size mut be a power of two.  To further complicate
1088          * matters, the viewport must start on a pcie-address that is aligned
1089          * on a multiple of its size.  If a portion of the viewport does not
1090          * represent system memory -- e.g. 3GB of memory requires a 4GB
1091          * viewport -- we can map the outbound memory in or after 3GB and even
1092          * though the viewport will overlap the outbound memory the controller
1093          * will know to send outbound memory downstream and everything else
1094          * upstream.
1095          *
1096          * For example:
1097          *
1098          * - The best-case scenario, memory up to 3GB, is to place the inbound
1099          *   region in the first 4GB of pcie-space, as some legacy devices can
1100          *   only address 32bits. We would also like to put the MSI under 4GB
1101          *   as well, since some devices require a 32bit MSI target address.
1102          *
1103          * - If the system memory is 4GB or larger we cannot start the inbound
1104          *   region at location 0 (since we have to allow some space for
1105          *   outbound memory @ 3GB). So instead it will  start at the 1x
1106          *   multiple of its size
1107          */
1108         if (!*rc_bar2_size || (*rc_bar2_offset & (*rc_bar2_size - 1)) ||
1109             (*rc_bar2_offset < SZ_4G && *rc_bar2_offset > SZ_2G)) {
1110                 dev_err(dev, "Invalid rc_bar2_offset/size: size 0x%llx, off 0x%llx\n",
1111                         *rc_bar2_size, *rc_bar2_offset);
1112                 return -EINVAL;
1113         }
1114
1115         return 0;
1116 }
1117
1118 static int brcm_pcie_get_rc_bar_n(struct brcm_pcie *pcie,
1119                                   int idx,
1120                                   u64 *rc_bar_cpu,
1121                                   u64 *rc_bar_size,
1122                                   u64 *rc_bar_pci)
1123 {
1124         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1125         struct resource_entry *entry;
1126         int i = 0;
1127
1128         resource_list_for_each_entry(entry, &bridge->dma_ranges) {
1129                 if (i == idx) {
1130                         *rc_bar_cpu  = entry->res->start;
1131                         *rc_bar_size = entry->res->end - entry->res->start + 1;
1132                         *rc_bar_pci = entry->res->start - entry->offset;
1133                         return 0;
1134                 }
1135
1136                 i++;
1137         }
1138
1139         return -EINVAL;
1140 }
1141
1142 static int brcm_pcie_setup(struct brcm_pcie *pcie)
1143 {
1144         u64 rc_bar2_offset, rc_bar2_size;
1145         void __iomem *base = pcie->base;
1146         struct pci_host_bridge *bridge;
1147         struct resource_entry *entry;
1148         u32 tmp, burst, aspm_support;
1149         int num_out_wins = 0;
1150         int ret, memc, count, i;
1151
1152         /* Reset the bridge */
1153         pcie->bridge_sw_init_set(pcie, 1);
1154
1155         /* Ensure that PERST# is asserted; some bootloaders may deassert it. */
1156         if (pcie->type == BCM2711)
1157                 pcie->perst_set(pcie, 1);
1158
1159         usleep_range(100, 200);
1160
1161         /* Take the bridge out of reset */
1162         pcie->bridge_sw_init_set(pcie, 0);
1163
1164         tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1165         if (is_bmips(pcie))
1166                 tmp &= ~PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
1167         else
1168                 tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
1169         writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1170         /* Wait for SerDes to be stable */
1171         usleep_range(100, 200);
1172
1173         if (pcie->type == BCM2712) {
1174                 /* Allow a 54MHz (xosc) refclk source */
1175                 brcm_pcie_munge_pll(pcie);
1176                 /* Fix for L1SS errata */
1177                 tmp = readl(base + PCIE_RC_PL_PHY_CTL_15);
1178                 tmp &= ~PCIE_RC_PL_PHY_CTL_15_PM_CLK_PERIOD_MASK;
1179                 /* PM clock period is 18.52ns (round down) */
1180                 tmp |= 0x12;
1181                 writel(tmp, base + PCIE_RC_PL_PHY_CTL_15);
1182         }
1183
1184         /*
1185          * SCB_MAX_BURST_SIZE is a two bit field.  For GENERIC chips it
1186          * is encoded as 0=128, 1=256, 2=512, 3=Rsvd, for BCM7278 it
1187          * is encoded as 0=Rsvd, 1=128, 2=256, 3=512.
1188          */
1189         if (is_bmips(pcie))
1190                 burst = 0x1; /* 256 bytes */
1191         else if (pcie->type == BCM2711)
1192                 burst = 0x0; /* 128 bytes */
1193         else if (pcie->type == BCM2712)
1194                 burst = 0x1; /* 128 bytes */
1195         else if (pcie->type == BCM7278)
1196                 burst = 0x3; /* 512 bytes */
1197         else
1198                 burst = 0x2; /* 512 bytes */
1199
1200         /*
1201          * Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN,
1202          * RCB_MPS_MODE
1203          */
1204         tmp = readl(base + PCIE_MISC_MISC_CTRL);
1205         u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK);
1206         u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK);
1207         u32p_replace_bits(&tmp, burst, PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK);
1208         if (pcie->rcb_mps_mode)
1209                 u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_PCIE_RCB_MPS_MODE_MASK);
1210         writel(tmp, base + PCIE_MISC_MISC_CTRL);
1211
1212         brcm_pcie_set_tc_qos(pcie);
1213
1214         ret = brcm_pcie_get_rc_bar2_size_and_offset(pcie, &rc_bar2_size,
1215                                                     &rc_bar2_offset);
1216         if (ret)
1217                 return ret;
1218
1219         tmp = lower_32_bits(rc_bar2_offset);
1220         u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(rc_bar2_size),
1221                           PCIE_MISC_RC_BAR2_CONFIG_LO_SIZE_MASK);
1222         writel(tmp, base + PCIE_MISC_RC_BAR2_CONFIG_LO);
1223         writel(upper_32_bits(rc_bar2_offset),
1224                base + PCIE_MISC_RC_BAR2_CONFIG_HI);
1225
1226         tmp = readl(base + PCIE_MISC_UBUS_BAR2_CONFIG_REMAP);
1227         u32p_replace_bits(&tmp, 1, PCIE_MISC_UBUS_BAR2_CONFIG_REMAP_ACCESS_ENABLE_MASK);
1228         writel(tmp, base + PCIE_MISC_UBUS_BAR2_CONFIG_REMAP);
1229         tmp = readl(base + PCIE_MISC_MISC_CTRL);
1230
1231         for (memc = 0; memc < pcie->num_memc; memc++) {
1232                 u32 scb_size_val = ilog2(pcie->memc_size[memc]) - 15;
1233
1234                 if (memc == 0)
1235                         u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(0));
1236                 else if (memc == 1)
1237                         u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(1));
1238                 else if (memc == 2)
1239                         u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(2));
1240         }
1241
1242         writel(tmp, base + PCIE_MISC_MISC_CTRL);
1243
1244         if (pcie->type == BCM2712) {
1245                 /* Suppress AXI error responses and return 1s for read failures */
1246                 tmp = readl(base + PCIE_MISC_UBUS_CTRL);
1247                 u32p_replace_bits(&tmp, 1, PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_ERR_DIS_MASK);
1248                 u32p_replace_bits(&tmp, 1, PCIE_MISC_UBUS_CTRL_UBUS_PCIE_REPLY_DECERR_DIS_MASK);
1249                 writel(tmp, base + PCIE_MISC_UBUS_CTRL);
1250                 writel(0xffffffff, base + PCIE_MISC_AXI_READ_ERROR_DATA);
1251
1252                 /*
1253                  * Adjust timeouts. The UBUS timeout also affects CRS
1254                  * completion retries, as the request will get terminated if
1255                  * either timeout expires, so both have to be a large value
1256                  * (in clocks of 750MHz).
1257                  * Set UBUS timeout to 250ms, then set RC config retry timeout
1258                  * to be ~240ms.
1259                  *
1260                  * Setting CRSVis=1 will stop the core from blocking on a CRS
1261                  * response, but does require the device to be well-behaved...
1262                  */
1263                 writel(0xB2D0000, base + PCIE_MISC_UBUS_TIMEOUT);
1264                 writel(0xABA0000, base + PCIE_MISC_RC_CONFIG_RETRY_TIMEOUT);
1265         }
1266
1267         /*
1268          * We ideally want the MSI target address to be located in the 32bit
1269          * addressable memory area. Some devices might depend on it. This is
1270          * possible either when the inbound window is located above the lower
1271          * 4GB or when the inbound area is smaller than 4GB (taking into
1272          * account the rounding-up we're forced to perform).
1273          */
1274         if (rc_bar2_offset >= SZ_4G || (rc_bar2_size + rc_bar2_offset) < SZ_4G)
1275                 pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_LT_4GB;
1276         else
1277                 pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_GT_4GB;
1278
1279         if (!pcie->rc_mode(pcie)) {
1280                 dev_err(pcie->dev, "PCIe RC controller misconfigured as Endpoint\n");
1281                 return -EINVAL;
1282         }
1283
1284         /* disable the PCIe->GISB memory window (RC_BAR1) */
1285         tmp = readl(base + PCIE_MISC_RC_BAR1_CONFIG_LO);
1286         tmp &= ~PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK;
1287         writel(tmp, base + PCIE_MISC_RC_BAR1_CONFIG_LO);
1288
1289         /* disable the PCIe->SCB memory window (RC_BAR3) */
1290         tmp = readl(base + PCIE_MISC_RC_BAR3_CONFIG_LO);
1291         tmp &= ~PCIE_MISC_RC_BAR3_CONFIG_LO_SIZE_MASK;
1292         writel(tmp, base + PCIE_MISC_RC_BAR3_CONFIG_LO);
1293
1294         /* Don't advertise L0s capability if 'aspm-no-l0s' */
1295         aspm_support = PCIE_LINK_STATE_L1;
1296         if (!of_property_read_bool(pcie->np, "aspm-no-l0s"))
1297                 aspm_support |= PCIE_LINK_STATE_L0S;
1298         tmp = readl(base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
1299         u32p_replace_bits(&tmp, aspm_support,
1300                 PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_ASPM_SUPPORT_MASK);
1301         writel(tmp, base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
1302
1303         /* program additional inbound windows (RC_BAR4..RC_BAR10) */
1304         count = (pcie->type == BCM2712) ? 7 : 0;
1305         for (i = 0; i < count; i++) {
1306                 u64 bar_cpu, bar_size, bar_pci;
1307
1308                 ret = brcm_pcie_get_rc_bar_n(pcie, 1 + i, &bar_cpu, &bar_size,
1309                                              &bar_pci);
1310                 if (ret)
1311                         break;
1312
1313                 tmp = lower_32_bits(bar_pci);
1314                 u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(bar_size),
1315                                   PCIE_MISC_RC_BAR_CONFIG_LO_SIZE_MASK);
1316                 writel(tmp, base + PCIE_MISC_RC_BAR4_CONFIG_LO + i * 8);
1317                 writel(upper_32_bits(bar_pci),
1318                        base + PCIE_MISC_RC_BAR4_CONFIG_HI + i * 8);
1319
1320                 tmp = upper_32_bits(bar_cpu) &
1321                         PCIE_MISC_UBUS_BAR_CONFIG_REMAP_HI_MASK;
1322                 writel(tmp,
1323                        base + PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_HI + i * 8);
1324                 tmp = lower_32_bits(bar_cpu) &
1325                         PCIE_MISC_UBUS_BAR_CONFIG_REMAP_LO_MASK;
1326                 writel(tmp | PCIE_MISC_UBUS_BAR_CONFIG_REMAP_ENABLE,
1327                        base + PCIE_MISC_UBUS_BAR4_CONFIG_REMAP_LO + i * 8);
1328         }
1329
1330         if (pcie->gen) {
1331                 dev_info(pcie->dev, "Forcing gen %d\n", pcie->gen);
1332                 brcm_pcie_set_gen(pcie, pcie->gen);
1333         }
1334
1335         /*
1336          * For config space accesses on the RC, show the right class for
1337          * a PCIe-PCIe bridge (the default setting is to be EP mode).
1338          */
1339         tmp = readl(base + PCIE_RC_CFG_PRIV1_ID_VAL3);
1340         u32p_replace_bits(&tmp, 0x060400,
1341                           PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK);
1342         writel(tmp, base + PCIE_RC_CFG_PRIV1_ID_VAL3);
1343
1344         bridge = pci_host_bridge_from_priv(pcie);
1345         resource_list_for_each_entry(entry, &bridge->windows) {
1346                 struct resource *res = entry->res;
1347
1348                 if (resource_type(res) != IORESOURCE_MEM)
1349                         continue;
1350
1351                 if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) {
1352                         dev_err(pcie->dev, "too many outbound wins\n");
1353                         return -EINVAL;
1354                 }
1355
1356                 if (is_bmips(pcie)) {
1357                         u64 start = res->start;
1358                         unsigned int j, nwins = resource_size(res) / SZ_128M;
1359
1360                         /* bmips PCIe outbound windows have a 128MB max size */
1361                         if (nwins > BRCM_NUM_PCIE_OUT_WINS)
1362                                 nwins = BRCM_NUM_PCIE_OUT_WINS;
1363                         for (j = 0; j < nwins; j++, start += SZ_128M)
1364                                 brcm_pcie_set_outbound_win(pcie, j, start,
1365                                                            start - entry->offset,
1366                                                            SZ_128M);
1367                         break;
1368                 }
1369                 brcm_pcie_set_outbound_win(pcie, num_out_wins, res->start,
1370                                            res->start - entry->offset,
1371                                            resource_size(res));
1372                 num_out_wins++;
1373         }
1374
1375         /* PCIe->SCB endian mode for BAR */
1376         tmp = readl(base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1);
1377         u32p_replace_bits(&tmp, PCIE_RC_CFG_VENDOR_SPCIFIC_REG1_LITTLE_ENDIAN,
1378                 PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK);
1379         writel(tmp, base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1);
1380
1381         return 0;
1382 }
1383
1384 static int brcm_pcie_start_link(struct brcm_pcie *pcie)
1385 {
1386         struct device *dev = pcie->dev;
1387         void __iomem *base = pcie->base;
1388         u16 nlw, cls, lnksta;
1389         bool ssc_good = false;
1390         int ret, i;
1391         u32 tmp;
1392
1393         /* Unassert the fundamental reset */
1394         if (pcie->tperst_clk_ms) {
1395                 /*
1396                  * Increase Tperst_clk time by forcing PERST# output low while
1397                  * the internal reset is released, so the PLL generates stable
1398                  * refclk output further in advance of PERST# deassertion.
1399                  */
1400                 tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1401                 u32p_replace_bits(&tmp, 1, PCIE_MISC_HARD_PCIE_HARD_DEBUG_PERST_ASSERT_MASK);
1402                 writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1403
1404                 pcie->perst_set(pcie, 0);
1405                 msleep(pcie->tperst_clk_ms);
1406
1407                 tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1408                 u32p_replace_bits(&tmp, 0, PCIE_MISC_HARD_PCIE_HARD_DEBUG_PERST_ASSERT_MASK);
1409                 writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1410         } else {
1411                 pcie->perst_set(pcie, 0);
1412         }
1413
1414         /*
1415          * Wait for 100ms after PERST# deassertion; see PCIe CEM specification
1416          * sections 2.2, PCIe r5.0, 6.6.1.
1417          */
1418         msleep(100);
1419
1420         /*
1421          * Give the RC/EP even more time to wake up, before trying to
1422          * configure RC.  Intermittently check status for link-up, up to a
1423          * total of 100ms.
1424          */
1425         for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5)
1426                 msleep(5);
1427
1428         if (!brcm_pcie_link_up(pcie)) {
1429                 dev_err(dev, "link down\n");
1430                 return -ENODEV;
1431         }
1432
1433         if (pcie->gen)
1434                 brcm_pcie_set_gen(pcie, pcie->gen);
1435
1436         if (pcie->ssc) {
1437                 ret = brcm_pcie_set_ssc(pcie);
1438                 if (ret == 0)
1439                         ssc_good = true;
1440                 else
1441                         dev_err(dev, "failed attempt to enter ssc mode\n");
1442         }
1443
1444
1445         lnksta = readw(base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKSTA);
1446         cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
1447         nlw = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
1448         dev_info(dev, "link up, %s x%u %s\n",
1449                  pci_speed_string(pcie_link_speed[cls]), nlw,
1450                  ssc_good ? "(SSC)" : "(!SSC)");
1451
1452         return 0;
1453 }
1454
1455 static const char * const supplies[] = {
1456         "vpcie3v3",
1457         "vpcie3v3aux",
1458         "vpcie12v",
1459 };
1460
1461 static void *alloc_subdev_regulators(struct device *dev)
1462 {
1463         const size_t size = sizeof(struct subdev_regulators) +
1464                 sizeof(struct regulator_bulk_data) * ARRAY_SIZE(supplies);
1465         struct subdev_regulators *sr;
1466         int i;
1467
1468         sr = devm_kzalloc(dev, size, GFP_KERNEL);
1469         if (sr) {
1470                 sr->num_supplies = ARRAY_SIZE(supplies);
1471                 for (i = 0; i < ARRAY_SIZE(supplies); i++)
1472                         sr->supplies[i].supply = supplies[i];
1473         }
1474
1475         return sr;
1476 }
1477
1478 static int brcm_pcie_add_bus(struct pci_bus *bus)
1479 {
1480         struct brcm_pcie *pcie = bus->sysdata;
1481         struct device *dev = &bus->dev;
1482         struct subdev_regulators *sr;
1483         int ret;
1484
1485         if (!bus->parent || !pci_is_root_bus(bus->parent))
1486                 return 0;
1487
1488         if (dev->of_node) {
1489                 sr = alloc_subdev_regulators(dev);
1490                 if (!sr) {
1491                         dev_info(dev, "Can't allocate regulators for downstream device\n");
1492                         goto no_regulators;
1493                 }
1494
1495                 pcie->sr = sr;
1496
1497                 ret = regulator_bulk_get(dev, sr->num_supplies, sr->supplies);
1498                 if (ret) {
1499                         dev_info(dev, "No regulators for downstream device\n");
1500                         goto no_regulators;
1501                 }
1502
1503                 ret = regulator_bulk_enable(sr->num_supplies, sr->supplies);
1504                 if (ret) {
1505                         dev_err(dev, "Can't enable regulators for downstream device\n");
1506                         regulator_bulk_free(sr->num_supplies, sr->supplies);
1507                         pcie->sr = NULL;
1508                 }
1509         }
1510
1511 no_regulators:
1512         brcm_pcie_start_link(pcie);
1513         return 0;
1514 }
1515
1516 static void brcm_pcie_remove_bus(struct pci_bus *bus)
1517 {
1518         struct brcm_pcie *pcie = bus->sysdata;
1519         struct subdev_regulators *sr = pcie->sr;
1520         struct device *dev = &bus->dev;
1521
1522         if (!sr)
1523                 return;
1524
1525         if (regulator_bulk_disable(sr->num_supplies, sr->supplies))
1526                 dev_err(dev, "Failed to disable regulators for downstream device\n");
1527         regulator_bulk_free(sr->num_supplies, sr->supplies);
1528         pcie->sr = NULL;
1529 }
1530
1531 /* L23 is a low-power PCIe link state */
1532 static void brcm_pcie_enter_l23(struct brcm_pcie *pcie)
1533 {
1534         void __iomem *base = pcie->base;
1535         int l23, i;
1536         u32 tmp;
1537
1538         /* Assert request for L23 */
1539         tmp = readl(base + PCIE_MISC_PCIE_CTRL);
1540         u32p_replace_bits(&tmp, 1, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK);
1541         writel(tmp, base + PCIE_MISC_PCIE_CTRL);
1542
1543         /* Wait up to 36 msec for L23 */
1544         tmp = readl(base + PCIE_MISC_PCIE_STATUS);
1545         l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK, tmp);
1546         for (i = 0; i < 15 && !l23; i++) {
1547                 usleep_range(2000, 2400);
1548                 tmp = readl(base + PCIE_MISC_PCIE_STATUS);
1549                 l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK,
1550                                 tmp);
1551         }
1552
1553         if (!l23)
1554                 dev_err(pcie->dev, "failed to enter low-power link state\n");
1555 }
1556
1557 static int brcm_phy_cntl(struct brcm_pcie *pcie, const int start)
1558 {
1559 #if 0
1560         static const u32 shifts[PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS] = {
1561                 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_SHIFT,
1562                 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_SHIFT,
1563                 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_SHIFT,};
1564         static const u32 masks[PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS] = {
1565                 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_MASK,
1566                 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_MASK,
1567                 PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_MASK,};
1568         const int beg = start ? 0 : PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS - 1;
1569         const int end = start ? PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS : -1;
1570         u32 tmp, combined_mask = 0;
1571         u32 val;
1572         void __iomem *base = pcie->base;
1573         int i, ret;
1574
1575         for (i = beg; i != end; start ? i++ : i--) {
1576                 val = start ? BIT_MASK(shifts[i]) : 0;
1577                 tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1578                 tmp = (tmp & ~masks[i]) | (val & masks[i]);
1579                 writel(tmp, base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1580                 usleep_range(50, 200);
1581                 combined_mask |= masks[i];
1582         }
1583
1584         tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1585         val = start ? combined_mask : 0;
1586
1587         ret = (tmp & combined_mask) == val ? 0 : -EIO;
1588         if (ret)
1589                 dev_err(pcie->dev, "failed to %s phy\n", (start ? "start" : "stop"));
1590
1591         return ret;
1592 #else
1593         return 0;
1594 #endif
1595 }
1596
1597 static inline int brcm_phy_start(struct brcm_pcie *pcie)
1598 {
1599         return pcie->rescal ? brcm_phy_cntl(pcie, 1) : 0;
1600 }
1601
1602 static inline int brcm_phy_stop(struct brcm_pcie *pcie)
1603 {
1604         return pcie->rescal ? brcm_phy_cntl(pcie, 0) : 0;
1605 }
1606
1607 static void brcm_pcie_turn_off(struct brcm_pcie *pcie)
1608 {
1609         void __iomem *base = pcie->base;
1610         int tmp;
1611
1612         if (brcm_pcie_link_up(pcie))
1613                 brcm_pcie_enter_l23(pcie);
1614         /* Assert fundamental reset */
1615         pcie->perst_set(pcie, 1);
1616
1617         /* Deassert request for L23 in case it was asserted */
1618         tmp = readl(base + PCIE_MISC_PCIE_CTRL);
1619         u32p_replace_bits(&tmp, 0, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK);
1620         writel(tmp, base + PCIE_MISC_PCIE_CTRL);
1621
1622         /* Turn off SerDes */
1623         tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1624         u32p_replace_bits(&tmp, 1, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
1625         writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1626
1627         /*
1628          * Shutting down this bridge on pcie1 means accesses to rescal block
1629          * will hang the chip if another RC wants to assert/deassert rescal.
1630          */
1631         if (pcie->type == BCM2712)
1632                 return;
1633         /* Shutdown PCIe bridge */
1634         pcie->bridge_sw_init_set(pcie, 1);
1635 }
1636
1637 static int pci_dev_may_wakeup(struct pci_dev *dev, void *data)
1638 {
1639         bool *ret = data;
1640
1641         if (device_may_wakeup(&dev->dev)) {
1642                 *ret = true;
1643                 dev_info(&dev->dev, "Possible wake-up device; regulators will not be disabled\n");
1644         }
1645         return (int) *ret;
1646 }
1647
1648 static int brcm_pcie_suspend_noirq(struct device *dev)
1649 {
1650         struct brcm_pcie *pcie = dev_get_drvdata(dev);
1651         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1652         int ret;
1653
1654         brcm_pcie_turn_off(pcie);
1655         /*
1656          * If brcm_phy_stop() returns an error, just dev_err(). If we
1657          * return the error it will cause the suspend to fail and this is a
1658          * forgivable offense that will probably be erased on resume.
1659          */
1660         if (brcm_phy_stop(pcie))
1661                 dev_err(dev, "Could not stop phy for suspend\n");
1662
1663         ret = reset_control_assert(pcie->rescal);
1664         if (ret) {
1665                 dev_err(dev, "Could not assert rescal reset\n");
1666                 return ret;
1667         }
1668
1669         if (pcie->sr) {
1670                 /*
1671                  * Now turn off the regulators, but if at least one
1672                  * downstream device is enabled as a wake-up source, do not
1673                  * turn off regulators.
1674                  */
1675                 pcie->ep_wakeup_capable = false;
1676                 pci_walk_bus(bridge->bus, pci_dev_may_wakeup,
1677                              &pcie->ep_wakeup_capable);
1678                 if (!pcie->ep_wakeup_capable) {
1679                         ret = regulator_bulk_disable(pcie->sr->num_supplies,
1680                                                      pcie->sr->supplies);
1681                         if (ret) {
1682                                 dev_err(dev, "Could not turn off regulators\n");
1683                                 reset_control_reset(pcie->rescal);
1684                                 return ret;
1685                         }
1686                 }
1687         }
1688         clk_disable_unprepare(pcie->clk);
1689
1690         return 0;
1691 }
1692
1693 static int brcm_pcie_resume_noirq(struct device *dev)
1694 {
1695         struct brcm_pcie *pcie = dev_get_drvdata(dev);
1696         void __iomem *base;
1697         u32 tmp;
1698         int ret;
1699
1700         base = pcie->base;
1701         ret = clk_prepare_enable(pcie->clk);
1702         if (ret)
1703                 return ret;
1704
1705         ret = reset_control_reset(pcie->rescal);
1706         if (ret)
1707                 goto err_disable_clk;
1708
1709         ret = brcm_phy_start(pcie);
1710         if (ret)
1711                 goto err_reset;
1712
1713         /* Take bridge out of reset so we can access the SERDES reg */
1714         pcie->bridge_sw_init_set(pcie, 0);
1715
1716         /* SERDES_IDDQ = 0 */
1717         tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1718         u32p_replace_bits(&tmp, 0, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
1719         writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1720
1721         /* wait for serdes to be stable */
1722         udelay(100);
1723
1724         ret = brcm_pcie_setup(pcie);
1725         if (ret)
1726                 goto err_reset;
1727
1728         if (pcie->sr) {
1729                 if (pcie->ep_wakeup_capable) {
1730                         /*
1731                          * We are resuming from a suspend.  In the suspend we
1732                          * did not disable the power supplies, so there is
1733                          * no need to enable them (and falsely increase their
1734                          * usage count).
1735                          */
1736                         pcie->ep_wakeup_capable = false;
1737                 } else {
1738                         ret = regulator_bulk_enable(pcie->sr->num_supplies,
1739                                                     pcie->sr->supplies);
1740                         if (ret) {
1741                                 dev_err(dev, "Could not turn on regulators\n");
1742                                 goto err_reset;
1743                         }
1744                 }
1745         }
1746
1747         ret = brcm_pcie_start_link(pcie);
1748         if (ret)
1749                 goto err_regulator;
1750
1751         if (pcie->msi)
1752                 brcm_msi_set_regs(pcie->msi);
1753
1754         return 0;
1755
1756 err_regulator:
1757         if (pcie->sr)
1758                 regulator_bulk_disable(pcie->sr->num_supplies, pcie->sr->supplies);
1759 err_reset:
1760         reset_control_assert(pcie->rescal);
1761 err_disable_clk:
1762         clk_disable_unprepare(pcie->clk);
1763         return ret;
1764 }
1765
1766 static void __brcm_pcie_remove(struct brcm_pcie *pcie)
1767 {
1768         brcm_msi_remove(pcie);
1769         brcm_pcie_turn_off(pcie);
1770         if (brcm_phy_stop(pcie))
1771                 dev_err(pcie->dev, "Could not stop phy\n");
1772         if (reset_control_assert(pcie->rescal))
1773                 dev_err(pcie->dev, "Could not assert rescal reset\n");
1774         clk_disable_unprepare(pcie->clk);
1775 }
1776
1777 static void brcm_pcie_remove(struct platform_device *pdev)
1778 {
1779         struct brcm_pcie *pcie = platform_get_drvdata(pdev);
1780         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1781
1782         pci_stop_root_bus(bridge->bus);
1783         pci_remove_root_bus(bridge->bus);
1784         __brcm_pcie_remove(pcie);
1785 }
1786
1787 static const int pcie_offsets[] = {
1788         [RGR1_SW_INIT_1] = 0x9210,
1789         [EXT_CFG_INDEX]  = 0x9000,
1790         [EXT_CFG_DATA]   = 0x9004,
1791         [PCIE_HARD_DEBUG] = 0x4204,
1792         [INTR2_CPU]      = 0x4300,
1793 };
1794
1795 static const int pcie_offsets_bmips_7425[] = {
1796         [RGR1_SW_INIT_1] = 0x8010,
1797         [EXT_CFG_INDEX]  = 0x8300,
1798         [EXT_CFG_DATA]   = 0x8304,
1799         [PCIE_HARD_DEBUG] = 0x4204,
1800         [INTR2_CPU]      = 0x4300,
1801 };
1802
1803 static const struct pcie_cfg_data generic_cfg = {
1804         .offsets        = pcie_offsets,
1805         .type           = GENERIC,
1806         .perst_set      = brcm_pcie_perst_set_generic,
1807         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1808         .rc_mode        = brcm_pcie_rc_mode_generic,
1809 };
1810
1811 static const struct pcie_cfg_data bcm7425_cfg = {
1812         .offsets        = pcie_offsets_bmips_7425,
1813         .type           = BCM7425,
1814         .perst_set      = brcm_pcie_perst_set_generic,
1815         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1816         .rc_mode        = brcm_pcie_rc_mode_generic,
1817 };
1818
1819 static const struct pcie_cfg_data bcm7435_cfg = {
1820         .offsets        = pcie_offsets,
1821         .type           = BCM7435,
1822         .perst_set      = brcm_pcie_perst_set_generic,
1823         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1824 };
1825
1826 static const struct pcie_cfg_data bcm4908_cfg = {
1827         .offsets        = pcie_offsets,
1828         .type           = BCM4908,
1829         .perst_set      = brcm_pcie_perst_set_4908,
1830         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1831         .rc_mode        = brcm_pcie_rc_mode_generic,
1832 };
1833
1834 static const int pcie_offset_bcm7278[] = {
1835         [RGR1_SW_INIT_1] = 0xc010,
1836         [EXT_CFG_INDEX] = 0x9000,
1837         [EXT_CFG_DATA] = 0x9004,
1838         [PCIE_HARD_DEBUG] = 0x4204,
1839         [INTR2_CPU]      = 0x4300,
1840 };
1841
1842 static const struct pcie_cfg_data bcm7278_cfg = {
1843         .offsets        = pcie_offset_bcm7278,
1844         .type           = BCM7278,
1845         .perst_set      = brcm_pcie_perst_set_7278,
1846         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_7278,
1847         .rc_mode        = brcm_pcie_rc_mode_generic,
1848 };
1849
1850 static const struct pcie_cfg_data bcm2711_cfg = {
1851         .offsets        = pcie_offsets,
1852         .type           = BCM2711,
1853         .perst_set      = brcm_pcie_perst_set_generic,
1854         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1855         .rc_mode        = brcm_pcie_rc_mode_generic,
1856 };
1857
1858 static const int pcie_offsets_bcm2712[] = {
1859         [EXT_CFG_INDEX] = 0x9000,
1860         [EXT_CFG_DATA] = 0x9004,
1861         [PCIE_HARD_DEBUG] = 0x4304,
1862         [INTR2_CPU] = 0x4400,
1863 };
1864
1865 static const struct pcie_cfg_data bcm2712_cfg = {
1866         .offsets        = pcie_offsets_bcm2712,
1867         .type           = BCM2712,
1868         .perst_set      = brcm_pcie_perst_set_2712,
1869         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_2712,
1870         .rc_mode        = brcm_pcie_rc_mode_2712,
1871 };
1872
1873 static const struct of_device_id brcm_pcie_match[] = {
1874         { .compatible = "brcm,bcm2711-pcie", .data = &bcm2711_cfg },
1875         { .compatible = "brcm,bcm2712-pcie", .data = &bcm2712_cfg },
1876         { .compatible = "brcm,bcm4908-pcie", .data = &bcm4908_cfg },
1877         { .compatible = "brcm,bcm7211-pcie", .data = &generic_cfg },
1878         { .compatible = "brcm,bcm7278-pcie", .data = &bcm7278_cfg },
1879         { .compatible = "brcm,bcm7216-pcie", .data = &bcm7278_cfg },
1880         { .compatible = "brcm,bcm7445-pcie", .data = &generic_cfg },
1881         { .compatible = "brcm,bcm7435-pcie", .data = &bcm7435_cfg },
1882         { .compatible = "brcm,bcm7425-pcie", .data = &bcm7425_cfg },
1883         {},
1884 };
1885
1886 static struct pci_ops brcm_pcie_ops = {
1887         .map_bus = brcm_pcie_map_bus,
1888         .read = pci_generic_config_read,
1889         .write = pci_generic_config_write,
1890         .add_bus = brcm_pcie_add_bus,
1891         .remove_bus = brcm_pcie_remove_bus,
1892 };
1893
1894 static struct pci_ops brcm7425_pcie_ops = {
1895         .map_bus = brcm7425_pcie_map_bus,
1896         .read = pci_generic_config_read32,
1897         .write = pci_generic_config_write32,
1898         .add_bus = brcm_pcie_add_bus,
1899         .remove_bus = brcm_pcie_remove_bus,
1900 };
1901
1902 static int brcm_pcie_probe(struct platform_device *pdev)
1903 {
1904         struct device_node *np = pdev->dev.of_node, *msi_np;
1905         struct pci_host_bridge *bridge;
1906         const struct pcie_cfg_data *data;
1907         struct brcm_pcie *pcie;
1908         int ret;
1909
1910         bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie));
1911         if (!bridge)
1912                 return -ENOMEM;
1913
1914         data = of_device_get_match_data(&pdev->dev);
1915         if (!data) {
1916                 dev_err(&pdev->dev, "failed to look up compatible string\n");
1917                 return -EINVAL;
1918         }
1919
1920         pcie = pci_host_bridge_priv(bridge);
1921         pcie->dev = &pdev->dev;
1922         pcie->np = np;
1923         pcie->reg_offsets = data->offsets;
1924         pcie->type = data->type;
1925         pcie->perst_set = data->perst_set;
1926         pcie->bridge_sw_init_set = data->bridge_sw_init_set;
1927         pcie->rc_mode = data->rc_mode;
1928
1929         pcie->base = devm_platform_ioremap_resource(pdev, 0);
1930         if (IS_ERR(pcie->base))
1931                 return PTR_ERR(pcie->base);
1932
1933         pcie->clk = devm_clk_get_optional(&pdev->dev, "sw_pcie");
1934         if (IS_ERR(pcie->clk))
1935                 return PTR_ERR(pcie->clk);
1936
1937         ret = of_pci_get_max_link_speed(np);
1938         pcie->gen = (ret < 0) ? 0 : ret;
1939
1940         pcie->ssc = of_property_read_bool(np, "brcm,enable-ssc");
1941         pcie->l1ss = of_property_read_bool(np, "brcm,enable-l1ss");
1942         pcie->rcb_mps_mode = of_property_read_bool(np, "brcm,enable-mps-rcb");
1943         of_property_read_u32(np, "brcm,tperst-clk-ms", &pcie->tperst_clk_ms);
1944
1945         ret = clk_prepare_enable(pcie->clk);
1946         if (ret) {
1947                 dev_err(&pdev->dev, "could not enable clock\n");
1948                 return ret;
1949         }
1950         pcie->rescal = devm_reset_control_get_optional_shared(&pdev->dev, "rescal");
1951         if (IS_ERR(pcie->rescal)) {
1952                 clk_disable_unprepare(pcie->clk);
1953                 return PTR_ERR(pcie->rescal);
1954         }
1955         pcie->perst_reset = devm_reset_control_get_optional_exclusive(&pdev->dev, "perst");
1956         if (IS_ERR(pcie->perst_reset)) {
1957                 clk_disable_unprepare(pcie->clk);
1958                 return PTR_ERR(pcie->perst_reset);
1959         }
1960         pcie->bridge_reset =
1961                 devm_reset_control_get_optional_exclusive(&pdev->dev, "bridge");
1962         if (IS_ERR(pcie->bridge_reset)) {
1963                 clk_disable_unprepare(pcie->clk);
1964                 return PTR_ERR(pcie->bridge_reset);
1965         }
1966
1967         ret = reset_control_deassert(pcie->rescal);
1968         if (ret)
1969                 dev_err(&pdev->dev, "failed to deassert 'rescal'\n");
1970
1971         ret = brcm_phy_start(pcie);
1972         if (ret) {
1973                 reset_control_assert(pcie->rescal);
1974                 clk_disable_unprepare(pcie->clk);
1975                 return ret;
1976         }
1977
1978         ret = brcm_pcie_setup(pcie);
1979         if (ret)
1980                 goto fail;
1981
1982         pcie->hw_rev = readl(pcie->base + PCIE_MISC_REVISION);
1983         if (pcie->type == BCM4908 && pcie->hw_rev >= BRCM_PCIE_HW_REV_3_20) {
1984                 dev_err(pcie->dev, "hardware revision with unsupported PERST# setup\n");
1985                 ret = -ENODEV;
1986                 goto fail;
1987         }
1988
1989         msi_np = of_parse_phandle(pcie->np, "msi-parent", 0);
1990         if (pci_msi_enabled() && msi_np == pcie->np) {
1991                 ret = brcm_pcie_enable_msi(pcie);
1992                 if (ret) {
1993                         dev_err(pcie->dev, "probe of internal MSI failed");
1994                         goto fail;
1995                 }
1996         } else if (pci_msi_enabled() && msi_np != pcie->np) {
1997                 /* Use RC_BAR1 for MIP access */
1998                 u64 msi_pci_addr;
1999                 u64 msi_phys_addr;
2000
2001                 if (of_property_read_u64(msi_np, "brcm,msi-pci-addr", &msi_pci_addr)) {
2002                         dev_err(pcie->dev, "Unable to find MSI PCI address\n");
2003                         ret = -EINVAL;
2004                         goto fail;
2005                 }
2006
2007                 if (of_property_read_u64(msi_np, "reg", &msi_phys_addr)) {
2008                         dev_err(pcie->dev, "Unable to find MSI physical address\n");
2009                         ret = -EINVAL;
2010                         goto fail;
2011                 }
2012
2013                 writel(lower_32_bits(msi_pci_addr) | brcm_pcie_encode_ibar_size(0x1000),
2014                        pcie->base + PCIE_MISC_RC_BAR1_CONFIG_LO);
2015                 writel(upper_32_bits(msi_pci_addr),
2016                        pcie->base + PCIE_MISC_RC_BAR1_CONFIG_HI);
2017
2018                 writel(lower_32_bits(msi_phys_addr) |
2019                        PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_ACCESS_ENABLE_MASK,
2020                        pcie->base + PCIE_MISC_UBUS_BAR1_CONFIG_REMAP);
2021                 writel(upper_32_bits(msi_phys_addr),
2022                        pcie->base + PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_HI);
2023         }
2024
2025         bridge->ops = pcie->type == BCM7425 ? &brcm7425_pcie_ops : &brcm_pcie_ops;
2026         bridge->sysdata = pcie;
2027
2028         platform_set_drvdata(pdev, pcie);
2029
2030         ret = pci_host_probe(bridge);
2031         if (!ret && !brcm_pcie_link_up(pcie))
2032                 ret = -ENODEV;
2033
2034         if (ret) {
2035                 brcm_pcie_remove(pdev);
2036                 return ret;
2037         }
2038
2039         brcm_pcie_config_clkreq(pcie);
2040
2041         return 0;
2042
2043 fail:
2044         __brcm_pcie_remove(pcie);
2045         return ret;
2046 }
2047
2048 MODULE_DEVICE_TABLE(of, brcm_pcie_match);
2049
2050 static const struct dev_pm_ops brcm_pcie_pm_ops = {
2051         .suspend_noirq = brcm_pcie_suspend_noirq,
2052         .resume_noirq = brcm_pcie_resume_noirq,
2053 };
2054
2055 static struct platform_driver brcm_pcie_driver = {
2056         .probe = brcm_pcie_probe,
2057         .remove_new = brcm_pcie_remove,
2058         .driver = {
2059                 .name = "brcm-pcie",
2060                 .of_match_table = brcm_pcie_match,
2061                 .pm = &brcm_pcie_pm_ops,
2062         },
2063 };
2064 module_platform_driver(brcm_pcie_driver);
2065
2066 MODULE_LICENSE("GPL");
2067 MODULE_DESCRIPTION("Broadcom STB PCIe RC driver");
2068 MODULE_AUTHOR("Broadcom");