PCI: brcmstb: Change RCB_{MPS,64B}_MODE bits
[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_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
146
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)
152
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)
156
157 #define PCIE_MISC_UBUS_TIMEOUT  0x40A8
158
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
162
163 #define PCIE_MISC_UBUS_BAR2_CONFIG_REMAP        0x40b4
164 #define  PCIE_MISC_UBUS_BAR2_CONFIG_REMAP_ACCESS_ENABLE_MASK    BIT(0)
165
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
170 /* ... */
171 #define PCIE_MISC_RC_BAR10_CONFIG_LO                    0x4104
172 #define PCIE_MISC_RC_BAR10_CONFIG_HI                    0x4108
173
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
179 /* ... */
180 #define PCIE_MISC_UBUS_BAR10_CONFIG_REMAP_LO            0x413c
181 #define PCIE_MISC_UBUS_BAR10_CONFIG_REMAP_HI            0x4140
182
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
191
192 /* VDM messages indexing TCs to AXI priorities */
193 /* Indexes 8-15 */
194 #define PCIE_MISC_VDM_PRIORITY_TO_QOS_MAP_HI            0x4164
195 /* Indexes 0-7 */
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
199
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
204
205 #define PCIE_MISC_AXI_READ_ERROR_DATA   0x4170
206
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
214
215 #define PCIE_EXT_CFG_DATA                               0x8000
216 #define PCIE_EXT_CFG_INDEX                              0x9000
217
218 #define  PCIE_RGR1_SW_INIT_1_PERST_MASK                 0x1
219 #define  PCIE_RGR1_SW_INIT_1_PERST_SHIFT                0x0
220
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
225
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)
234
235 /* MSI target addresses */
236 #define BRCM_MSI_TARGET_ADDR_LT_4GB     0x0fffffffcULL
237 #define BRCM_MSI_TARGET_ADDR_GT_4GB     0xffffffffcULL
238
239 /* MDIO registers */
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
259
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])
263
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
273
274 /* Forward declarations */
275 struct brcm_pcie;
276
277 enum {
278         RGR1_SW_INIT_1,
279         EXT_CFG_INDEX,
280         EXT_CFG_DATA,
281         PCIE_HARD_DEBUG,
282         INTR2_CPU,
283 };
284
285 enum {
286         RGR1_SW_INIT_1_INIT_MASK,
287         RGR1_SW_INIT_1_INIT_SHIFT,
288 };
289
290 enum pcie_type {
291         GENERIC,
292         BCM7425,
293         BCM7435,
294         BCM4908,
295         BCM7278,
296         BCM2711,
297         BCM2712,
298 };
299
300 struct pcie_cfg_data {
301         const int *offsets;
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);
306 };
307
308 struct subdev_regulators {
309         unsigned int num_supplies;
310         struct regulator_bulk_data supplies[];
311 };
312
313 struct brcm_msi {
314         struct device           *dev;
315         void __iomem            *base;
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 */
320         u64                     target_addr;
321         int                     irq;
322         DECLARE_BITMAP(used, 64);
323         bool                    legacy;
324         /* Some chips have MSIs in bits [31..24] of a shared register. */
325         int                     legacy_shift;
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;
329 };
330
331 /* Internal PCIe Host Controller Information.*/
332 struct brcm_pcie {
333         struct device           *dev;
334         void __iomem            *base;
335         struct clk              *clk;
336         struct device_node      *np;
337         bool                    ssc;
338         bool                    l1ss;
339         bool                    rcb_mps_mode;
340         int                     gen;
341         u64                     msi_target_addr;
342         struct brcm_msi         *msi;
343         const int               *reg_offsets;
344         enum pcie_type          type;
345         struct reset_control    *rescal;
346         struct reset_control    *perst_reset;
347         struct reset_control    *bridge_reset;
348         int                     num_memc;
349         u64                     memc_size[PCIE_BRCM_MAX_MEMC];
350         u32                     hw_rev;
351         u32                     qos_map;
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;
357 };
358
359 static inline bool is_bmips(const struct brcm_pcie *pcie)
360 {
361         return pcie->type == BCM7435 || pcie->type == BCM7425;
362 }
363
364 /*
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
367  */
368 static int brcm_pcie_encode_ibar_size(u64 size)
369 {
370         int log2_in = ilog2(size);
371
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) */
377                 return log2_in - 15;
378         /* Something is awry so disable */
379         return 0;
380 }
381
382 static u32 brcm_pcie_mdio_form_pkt(int port, int regad, int cmd)
383 {
384         u32 pkt = 0;
385
386         pkt |= FIELD_PREP(MDIO_PORT_MASK, port);
387         pkt |= FIELD_PREP(MDIO_REGAD_MASK, regad);
388         pkt |= FIELD_PREP(MDIO_CMD_MASK, cmd);
389
390         return pkt;
391 }
392
393 /* negative return value indicates error */
394 static int brcm_pcie_mdio_read(void __iomem *base, u8 port, u8 regad, u32 *val)
395 {
396         u32 data;
397         int err;
398
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);
405
406         return err;
407 }
408
409 /* negative return value indicates error */
410 static int brcm_pcie_mdio_write(void __iomem *base, u8 port,
411                                 u8 regad, u16 wrdata)
412 {
413         u32 data;
414         int err;
415
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);
420
421         err = readw_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_WR_DATA, data,
422                                         MDIO_WT_DONE(data), 10, 100);
423         return err;
424 }
425
426 /*
427  * Configures device for Spread Spectrum Clocking (SSC) mode; a negative
428  * return value indicates error.
429  */
430 static int brcm_pcie_set_ssc(struct brcm_pcie *pcie)
431 {
432         int pll, ssc;
433         int ret;
434         u32 tmp;
435
436         ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
437                                    SSC_REGS_ADDR);
438         if (ret < 0)
439                 return ret;
440
441         ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
442                                   SSC_CNTL_OFFSET, &tmp);
443         if (ret < 0)
444                 return ret;
445
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);
450         if (ret < 0)
451                 return ret;
452
453         usleep_range(1000, 2000);
454         ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
455                                   SSC_STATUS_OFFSET, &tmp);
456         if (ret < 0)
457                 return ret;
458
459         ssc = FIELD_GET(SSC_STATUS_SSC_MASK, tmp);
460         pll = FIELD_GET(SSC_STATUS_PLL_LOCK_MASK, tmp);
461
462         return ssc && pll ? 0 : -EIO;
463 }
464
465 static void brcm_pcie_munge_pll(struct brcm_pcie *pcie)
466 {
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&)
472
473         u32 tmp;
474         int ret, i;
475         u8 regs[] =  { 0x16,   0x17,   0x18,   0x19,   0x1b,   0x1c,   0x1e };
476         u16 data[] = { 0x50b9, 0xbda1, 0x0094, 0x97b4, 0x5030, 0x5030, 0x0007 };
477
478         ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
479                                 0x1600);
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",
483                         regs[i], tmp);
484         }
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",
489                         regs[i], tmp);
490         }
491         usleep_range(100, 200);
492 }
493
494 /* Limits operation to a specific generation (1, 2, or 3) */
495 static void brcm_pcie_set_gen(struct brcm_pcie *pcie, int gen)
496 {
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);
499
500         lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen;
501         writel(lnkcap, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCAP);
502
503         lnkctl2 = (lnkctl2 & ~0xf) | gen;
504         writew(lnkctl2, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
505 }
506
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)
510 {
511         u32 cpu_addr_mb_high, limit_addr_mb_high;
512         phys_addr_t cpu_addr_mb, limit_addr_mb;
513         int high_addr_shift;
514         u32 tmp;
515
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));
519
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;
523
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));
530
531         if (is_bmips(pcie))
532                 return;
533
534         /* Write the cpu & limit addr upper bits */
535         high_addr_shift =
536                 HWEIGHT32(PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
537
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));
543
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));
549 }
550
551 static void brcm_pcie_set_tc_qos(struct brcm_pcie *pcie)
552 {
553         int i;
554         u32 reg;
555
556         if (pcie->type != BCM2712)
557                 return;
558
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);
563
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);
568
569         if (!of_property_read_u32(pcie->np, "brcm,fifo-qos-map", &pcie->qos_map)) {
570                 /*
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.
574                  */
575
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));
579
580                 return;
581         }
582
583         if (!of_property_read_u32(pcie->np, "brcm,vdm-qos-map", &pcie->qos_map)) {
584
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);
588
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);
592
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);
601         }
602 }
603
604 static void brcm_pcie_config_clkreq(struct brcm_pcie *pcie)
605 {
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;
612         u16 link_ctl = 0;
613         int clkpm = 0;
614         int substates = 0;
615
616         pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
617         if ((link_cap & PCI_EXP_LNKCAP_CLKPM))
618                 clkpm = 1;
619
620         pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &link_ctl);
621         if (!(link_ctl & PCI_EXP_LNKCTL_CLKREQ_EN))
622                 clkpm = 0;
623
624         if (pcie->l1ss && pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_L1SS))
625                 substates = 1;
626
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;
630
631         if (substates)
632                 tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_L1SS_ENABLE_MASK;
633         else if (clkpm)
634                 tmp |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK;
635
636         writel(tmp, base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
637
638         if (substates || clkpm)
639                 dev_info(pcie->dev, "clkreq control enabled\n");
640 }
641
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,
647 };
648
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,
653 };
654
655 static void brcm_pcie_msi_isr(struct irq_desc *desc)
656 {
657         struct irq_chip *chip = irq_desc_get_chip(desc);
658         unsigned long status, virq;
659         struct brcm_msi *msi;
660         struct device *dev;
661         u32 bit;
662
663         chained_irq_enter(chip, desc);
664         msi = irq_desc_get_handler_data(desc);
665         dev = msi->dev;
666
667         status = readl(msi->intr_base + MSI_INT_STATUS);
668         status >>= msi->legacy_shift;
669
670         for_each_set_bit(bit, &status, BRCM_INT_PCI_MSI_NR/*msi->nr*/) {
671                 bool found = false;
672
673                 virq = irq_find_mapping(msi->inner_domain, bit);
674                 if (virq) {
675                         found = true;
676                         dev_dbg(dev, "MSI -> %ld\n", virq);
677                         generic_handle_irq(virq);
678                 }
679                 virq = irq_find_mapping(msi->inner_domain, bit + 32);
680                 if (virq) {
681                         found = true;
682                         dev_dbg(dev, "MSI -> %ld\n", virq);
683                         generic_handle_irq(virq);
684                 }
685                 if (!found)
686                         dev_dbg(dev, "unexpected MSI\n");
687         }
688
689         chained_irq_exit(chip, desc);
690 }
691
692 static void brcm_msi_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
693 {
694         struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
695
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);
699 }
700
701 static int brcm_msi_set_affinity(struct irq_data *irq_data,
702                                  const struct cpumask *mask, bool force)
703 {
704         return -EINVAL;
705 }
706
707 static void brcm_msi_ack_irq(struct irq_data *data)
708 {
709         struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
710         const int shift_amt = (data->hwirq & 0x1f) + msi->legacy_shift;
711
712         writel(1 << shift_amt, msi->intr_base + MSI_INT_CLR);
713 }
714
715
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,
721 };
722
723 static int brcm_msi_alloc(struct brcm_msi *msi, unsigned int nr_irqs)
724 {
725         int hwirq;
726
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);
731
732         return hwirq;
733 }
734
735 static void brcm_msi_free(struct brcm_msi *msi, unsigned long hwirq,
736                           unsigned int nr_irqs)
737 {
738         mutex_lock(&msi->lock);
739         bitmap_release_region(msi->used, hwirq, order_base_2(nr_irqs));
740         mutex_unlock(&msi->lock);
741 }
742
743 static int brcm_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
744                                  unsigned int nr_irqs, void *args)
745 {
746         struct brcm_msi *msi = domain->host_data;
747         int hwirq, i;
748
749         hwirq = brcm_msi_alloc(msi, nr_irqs);
750
751         if (hwirq < 0)
752                 return hwirq;
753
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);
758         return 0;
759 }
760
761 static void brcm_irq_domain_free(struct irq_domain *domain,
762                                  unsigned int virq, unsigned int nr_irqs)
763 {
764         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
765         struct brcm_msi *msi = irq_data_get_irq_chip_data(d);
766
767         brcm_msi_free(msi, d->hwirq, nr_irqs);
768 }
769
770 static const struct irq_domain_ops msi_domain_ops = {
771         .alloc  = brcm_irq_domain_alloc,
772         .free   = brcm_irq_domain_free,
773 };
774
775 static int brcm_allocate_domains(struct brcm_msi *msi)
776 {
777         struct fwnode_handle *fwnode = of_node_to_fwnode(msi->np);
778         struct device *dev = msi->dev;
779
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");
783                 return -ENOMEM;
784         }
785
786         msi->msi_domain = pci_msi_create_irq_domain(fwnode,
787                                                     &brcm_msi_domain_info,
788                                                     msi->inner_domain);
789         if (!msi->msi_domain) {
790                 dev_err(dev, "failed to create MSI domain\n");
791                 irq_domain_remove(msi->inner_domain);
792                 return -ENOMEM;
793         }
794
795         return 0;
796 }
797
798 static void brcm_free_domains(struct brcm_msi *msi)
799 {
800         irq_domain_remove(msi->msi_domain);
801         irq_domain_remove(msi->inner_domain);
802 }
803
804 static void brcm_msi_remove(struct brcm_pcie *pcie)
805 {
806         struct brcm_msi *msi = pcie->msi;
807
808         if (!msi)
809                 return;
810         irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
811         brcm_free_domains(msi);
812 }
813
814 static void brcm_msi_set_regs(struct brcm_msi *msi)
815 {
816         u32 val = msi->legacy ? BRCM_INT_PCI_MSI_LEGACY_MASK :
817                                 BRCM_INT_PCI_MSI_MASK;
818
819         writel(val, msi->intr_base + MSI_INT_MASK_CLR);
820         writel(val, msi->intr_base + MSI_INT_CLR);
821
822         /*
823          * The 0 bit of PCIE_MISC_MSI_BAR_CONFIG_LO is repurposed to MSI
824          * enable, which we set to 1.
825          */
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);
830
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);
833 }
834
835 static int brcm_pcie_enable_msi(struct brcm_pcie *pcie)
836 {
837         struct brcm_msi *msi;
838         int irq, ret;
839         struct device *dev = pcie->dev;
840
841         irq = irq_of_parse_and_map(dev->of_node, 1);
842         if (irq <= 0) {
843                 dev_err(dev, "cannot map MSI interrupt\n");
844                 return -ENODEV;
845         }
846
847         msi = devm_kzalloc(dev, sizeof(struct brcm_msi), GFP_KERNEL);
848         if (!msi)
849                 return -ENOMEM;
850
851         mutex_init(&msi->lock);
852         msi->dev = dev;
853         msi->base = pcie->base;
854         msi->np = pcie->np;
855         msi->target_addr = pcie->msi_target_addr;
856         msi->irq = irq;
857         msi->legacy = pcie->hw_rev < BRCM_PCIE_HW_REV_33;
858
859         /*
860          * Sanity check to make sure that the 'used' bitmap in struct brcm_msi
861          * is large enough.
862          */
863         BUILD_BUG_ON(BRCM_INT_PCI_MSI_LEGACY_NR > BRCM_INT_PCI_MSI_NR);
864
865         if (msi->legacy) {
866                 msi->intr_base = msi->base + PCIE_INTR2_CPU_BASE;
867                 msi->nr = BRCM_INT_PCI_MSI_LEGACY_NR;
868                 msi->legacy_shift = 24;
869         } else {
870                 msi->intr_base = msi->base + PCIE_MSI_INTR2_BASE;
871                 msi->nr = 64; //BRCM_INT_PCI_MSI_NR;
872                 msi->legacy_shift = 0;
873         }
874
875         ret = brcm_allocate_domains(msi);
876         if (ret)
877                 return ret;
878
879         irq_set_chained_handler_and_data(msi->irq, brcm_pcie_msi_isr, msi);
880
881         brcm_msi_set_regs(msi);
882         pcie->msi = msi;
883
884         return 0;
885 }
886
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)
889 {
890         void __iomem *base = pcie->base;
891         u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
892
893         return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK, val);
894 }
895
896 static bool brcm_pcie_rc_mode_2712(struct brcm_pcie *pcie)
897 {
898         void __iomem *base = pcie->base;
899         u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
900
901         return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK_2712, val) | 1; //XXX
902 }
903
904 static bool brcm_pcie_link_up(struct brcm_pcie *pcie)
905 {
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);
909
910         return dla && plu;
911 }
912
913 static void __iomem *brcm_pcie_map_bus(struct pci_bus *bus,
914                                        unsigned int devfn, int where)
915 {
916         struct brcm_pcie *pcie = bus->sysdata;
917         void __iomem *base = pcie->base;
918         int idx;
919
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);
923
924         /* An access to our HW w/o link-up will cause a CPU Abort */
925         if (!brcm_pcie_link_up(pcie))
926                 return NULL;
927
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);
932 }
933
934 static void __iomem *brcm7425_pcie_map_bus(struct pci_bus *bus,
935                                            unsigned int devfn, int where)
936 {
937         struct brcm_pcie *pcie = bus->sysdata;
938         void __iomem *base = pcie->base;
939         int idx;
940
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);
944
945         /* An access to our HW w/o link-up will cause a CPU Abort */
946         if (!brcm_pcie_link_up(pcie))
947                 return NULL;
948
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);
953 }
954
955 static void brcm_pcie_bridge_sw_init_set_generic(struct brcm_pcie *pcie, u32 val)
956 {
957         u32 tmp, mask =  RGR1_SW_INIT_1_INIT_GENERIC_MASK;
958         u32 shift = RGR1_SW_INIT_1_INIT_GENERIC_SHIFT;
959
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));
963 }
964
965 static void brcm_pcie_bridge_sw_init_set_7278(struct brcm_pcie *pcie, u32 val)
966 {
967         u32 tmp, mask =  RGR1_SW_INIT_1_INIT_7278_MASK;
968         u32 shift = RGR1_SW_INIT_1_INIT_7278_SHIFT;
969
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));
973 }
974
975 static void brcm_pcie_bridge_sw_init_set_2712(struct brcm_pcie *pcie, u32 val)
976 {
977         if (WARN_ONCE(!pcie->bridge_reset,
978                       "missing bridge reset controller\n"))
979                 return;
980
981         if (val)
982                 reset_control_assert(pcie->bridge_reset);
983         else
984                 reset_control_deassert(pcie->bridge_reset);
985 }
986
987 static void brcm_pcie_perst_set_4908(struct brcm_pcie *pcie, u32 val)
988 {
989         if (WARN_ONCE(!pcie->perst_reset, "missing PERST# reset controller\n"))
990                 return;
991
992         if (val)
993                 reset_control_assert(pcie->perst_reset);
994         else
995                 reset_control_deassert(pcie->perst_reset);
996 }
997
998 static void brcm_pcie_perst_set_7278(struct brcm_pcie *pcie, u32 val)
999 {
1000         u32 tmp;
1001
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);
1006 }
1007
1008 static void brcm_pcie_perst_set_2712(struct brcm_pcie *pcie, u32 val)
1009 {
1010         u32 tmp;
1011
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);
1016 }
1017
1018 static void brcm_pcie_perst_set_generic(struct brcm_pcie *pcie, u32 val)
1019 {
1020         u32 tmp;
1021
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));
1025 }
1026
1027 static int brcm_pcie_get_rc_bar2_size_and_offset(struct brcm_pcie *pcie,
1028                                                         u64 *rc_bar2_size,
1029                                                         u64 *rc_bar2_offset)
1030 {
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;
1035         int ret, i = 0;
1036         u64 size = 0;
1037
1038         resource_list_for_each_entry(entry, &bridge->dma_ranges) {
1039                 u64 pcie_beg = entry->res->start - entry->offset;
1040
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
1046         }
1047
1048         if (lowest_pcie_addr == ~(u64)0) {
1049                 dev_err(dev, "DT node has no dma-ranges\n");
1050                 return -EINVAL;
1051         }
1052
1053         ret = of_property_read_variable_u64_array(pcie->np, "brcm,scb-sizes", pcie->memc_size, 1,
1054                                                   PCIE_BRCM_MAX_MEMC);
1055
1056         if (ret <= 0) {
1057                 /* Make an educated guess */
1058                 pcie->num_memc = 1;
1059                 pcie->memc_size[0] = 1ULL << fls64(size - 1);
1060         } else {
1061                 pcie->num_memc = ret;
1062         }
1063
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];
1067
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);
1072
1073         /*
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
1081          * future:
1082          *
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
1092          * upstream.
1093          *
1094          * For example:
1095          *
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.
1100          *
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
1105          */
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);
1110                 return -EINVAL;
1111         }
1112
1113         return 0;
1114 }
1115
1116 static int brcm_pcie_get_rc_bar_n(struct brcm_pcie *pcie,
1117                                   int idx,
1118                                   u64 *rc_bar_cpu,
1119                                   u64 *rc_bar_size,
1120                                   u64 *rc_bar_pci)
1121 {
1122         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1123         struct resource_entry *entry;
1124         int i = 0;
1125
1126         resource_list_for_each_entry(entry, &bridge->dma_ranges) {
1127                 if (i == idx) {
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;
1131                         return 0;
1132                 }
1133
1134                 i++;
1135         }
1136
1137         return -EINVAL;
1138 }
1139
1140 static int brcm_pcie_setup(struct brcm_pcie *pcie)
1141 {
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;
1149
1150         /* Reset the bridge */
1151         pcie->bridge_sw_init_set(pcie, 1);
1152
1153         /* Ensure that PERST# is asserted; some bootloaders may deassert it. */
1154         if (pcie->type == BCM2711)
1155                 pcie->perst_set(pcie, 1);
1156
1157         usleep_range(100, 200);
1158
1159         /* Take the bridge out of reset */
1160         pcie->bridge_sw_init_set(pcie, 0);
1161
1162         tmp = readl(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG);
1163         if (is_bmips(pcie))
1164                 tmp &= ~PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
1165         else
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);
1170
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) */
1178                 tmp |= 0x12;
1179                 writel(tmp, base + PCIE_RC_PL_PHY_CTL_15);
1180         }
1181
1182         /*
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.
1186          */
1187         if (is_bmips(pcie))
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 */
1195         else
1196                 burst = 0x2; /* 512 bytes */
1197
1198         /*
1199          * Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN,
1200          * RCB_MPS_MODE
1201          */
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);
1209
1210         brcm_pcie_set_tc_qos(pcie);
1211
1212         ret = brcm_pcie_get_rc_bar2_size_and_offset(pcie, &rc_bar2_size,
1213                                                     &rc_bar2_offset);
1214         if (ret)
1215                 return ret;
1216
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);
1223
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);
1228
1229         for (memc = 0; memc < pcie->num_memc; memc++) {
1230                 u32 scb_size_val = ilog2(pcie->memc_size[memc]) - 15;
1231
1232                 if (memc == 0)
1233                         u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(0));
1234                 else if (memc == 1)
1235                         u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(1));
1236                 else if (memc == 2)
1237                         u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(2));
1238         }
1239
1240         writel(tmp, base + PCIE_MISC_MISC_CTRL);
1241
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);
1249
1250                 /*
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
1256                  * to be ~240ms.
1257                  *
1258                  * Setting CRSVis=1 will stop the core from blocking on a CRS
1259                  * response, but does require the device to be well-behaved...
1260                  */
1261                 writel(0xB2D0000, base + PCIE_MISC_UBUS_TIMEOUT);
1262                 writel(0xABA0000, base + PCIE_MISC_RC_CONFIG_RETRY_TIMEOUT);
1263         }
1264
1265         /*
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).
1271          */
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;
1274         else
1275                 pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_GT_4GB;
1276
1277         if (!pcie->rc_mode(pcie)) {
1278                 dev_err(pcie->dev, "PCIe RC controller misconfigured as Endpoint\n");
1279                 return -EINVAL;
1280         }
1281
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);
1286
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);
1291
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);
1300
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;
1305
1306                 ret = brcm_pcie_get_rc_bar_n(pcie, 1 + i, &bar_cpu, &bar_size,
1307                                              &bar_pci);
1308                 if (ret)
1309                         break;
1310
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);
1317
1318                 tmp = upper_32_bits(bar_cpu) &
1319                         PCIE_MISC_UBUS_BAR_CONFIG_REMAP_HI_MASK;
1320                 writel(tmp,
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);
1326         }
1327
1328         if (pcie->gen) {
1329                 dev_info(pcie->dev, "Forcing gen %d\n", pcie->gen);
1330                 brcm_pcie_set_gen(pcie, pcie->gen);
1331         }
1332
1333         /*
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).
1336          */
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);
1341
1342         bridge = pci_host_bridge_from_priv(pcie);
1343         resource_list_for_each_entry(entry, &bridge->windows) {
1344                 struct resource *res = entry->res;
1345
1346                 if (resource_type(res) != IORESOURCE_MEM)
1347                         continue;
1348
1349                 if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) {
1350                         dev_err(pcie->dev, "too many outbound wins\n");
1351                         return -EINVAL;
1352                 }
1353
1354                 if (is_bmips(pcie)) {
1355                         u64 start = res->start;
1356                         unsigned int j, nwins = resource_size(res) / SZ_128M;
1357
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,
1364                                                            SZ_128M);
1365                         break;
1366                 }
1367                 brcm_pcie_set_outbound_win(pcie, num_out_wins, res->start,
1368                                            res->start - entry->offset,
1369                                            resource_size(res));
1370                 num_out_wins++;
1371         }
1372
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);
1378
1379         return 0;
1380 }
1381
1382 static int brcm_pcie_start_link(struct brcm_pcie *pcie)
1383 {
1384         struct device *dev = pcie->dev;
1385         void __iomem *base = pcie->base;
1386         u16 nlw, cls, lnksta;
1387         bool ssc_good = false;
1388         int ret, i;
1389
1390         /* Unassert the fundamental reset */
1391         pcie->perst_set(pcie, 0);
1392
1393         /*
1394          * Wait for 100ms after PERST# deassertion; see PCIe CEM specification
1395          * sections 2.2, PCIe r5.0, 6.6.1.
1396          */
1397         msleep(100);
1398
1399         /*
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
1402          * total of 100ms.
1403          */
1404         for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5)
1405                 msleep(5);
1406
1407         if (!brcm_pcie_link_up(pcie)) {
1408                 dev_err(dev, "link down\n");
1409                 return -ENODEV;
1410         }
1411
1412         if (pcie->gen)
1413                 brcm_pcie_set_gen(pcie, pcie->gen);
1414
1415         if (pcie->ssc) {
1416                 ret = brcm_pcie_set_ssc(pcie);
1417                 if (ret == 0)
1418                         ssc_good = true;
1419                 else
1420                         dev_err(dev, "failed attempt to enter ssc mode\n");
1421         }
1422
1423
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)");
1430
1431         return 0;
1432 }
1433
1434 static const char * const supplies[] = {
1435         "vpcie3v3",
1436         "vpcie3v3aux",
1437         "vpcie12v",
1438 };
1439
1440 static void *alloc_subdev_regulators(struct device *dev)
1441 {
1442         const size_t size = sizeof(struct subdev_regulators) +
1443                 sizeof(struct regulator_bulk_data) * ARRAY_SIZE(supplies);
1444         struct subdev_regulators *sr;
1445         int i;
1446
1447         sr = devm_kzalloc(dev, size, GFP_KERNEL);
1448         if (sr) {
1449                 sr->num_supplies = ARRAY_SIZE(supplies);
1450                 for (i = 0; i < ARRAY_SIZE(supplies); i++)
1451                         sr->supplies[i].supply = supplies[i];
1452         }
1453
1454         return sr;
1455 }
1456
1457 static int brcm_pcie_add_bus(struct pci_bus *bus)
1458 {
1459         struct brcm_pcie *pcie = bus->sysdata;
1460         struct device *dev = &bus->dev;
1461         struct subdev_regulators *sr;
1462         int ret;
1463
1464         if (!bus->parent || !pci_is_root_bus(bus->parent))
1465                 return 0;
1466
1467         if (dev->of_node) {
1468                 sr = alloc_subdev_regulators(dev);
1469                 if (!sr) {
1470                         dev_info(dev, "Can't allocate regulators for downstream device\n");
1471                         goto no_regulators;
1472                 }
1473
1474                 pcie->sr = sr;
1475
1476                 ret = regulator_bulk_get(dev, sr->num_supplies, sr->supplies);
1477                 if (ret) {
1478                         dev_info(dev, "No regulators for downstream device\n");
1479                         goto no_regulators;
1480                 }
1481
1482                 ret = regulator_bulk_enable(sr->num_supplies, sr->supplies);
1483                 if (ret) {
1484                         dev_err(dev, "Can't enable regulators for downstream device\n");
1485                         regulator_bulk_free(sr->num_supplies, sr->supplies);
1486                         pcie->sr = NULL;
1487                 }
1488         }
1489
1490 no_regulators:
1491         brcm_pcie_start_link(pcie);
1492         return 0;
1493 }
1494
1495 static void brcm_pcie_remove_bus(struct pci_bus *bus)
1496 {
1497         struct brcm_pcie *pcie = bus->sysdata;
1498         struct subdev_regulators *sr = pcie->sr;
1499         struct device *dev = &bus->dev;
1500
1501         if (!sr)
1502                 return;
1503
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);
1507         pcie->sr = NULL;
1508 }
1509
1510 /* L23 is a low-power PCIe link state */
1511 static void brcm_pcie_enter_l23(struct brcm_pcie *pcie)
1512 {
1513         void __iomem *base = pcie->base;
1514         int l23, i;
1515         u32 tmp;
1516
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);
1521
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,
1529                                 tmp);
1530         }
1531
1532         if (!l23)
1533                 dev_err(pcie->dev, "failed to enter low-power link state\n");
1534 }
1535
1536 static int brcm_phy_cntl(struct brcm_pcie *pcie, const int start)
1537 {
1538 #if 0
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;
1550         u32 val;
1551         void __iomem *base = pcie->base;
1552         int i, ret;
1553
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];
1561         }
1562
1563         tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
1564         val = start ? combined_mask : 0;
1565
1566         ret = (tmp & combined_mask) == val ? 0 : -EIO;
1567         if (ret)
1568                 dev_err(pcie->dev, "failed to %s phy\n", (start ? "start" : "stop"));
1569
1570         return ret;
1571 #else
1572         return 0;
1573 #endif
1574 }
1575
1576 static inline int brcm_phy_start(struct brcm_pcie *pcie)
1577 {
1578         return pcie->rescal ? brcm_phy_cntl(pcie, 1) : 0;
1579 }
1580
1581 static inline int brcm_phy_stop(struct brcm_pcie *pcie)
1582 {
1583         return pcie->rescal ? brcm_phy_cntl(pcie, 0) : 0;
1584 }
1585
1586 static void brcm_pcie_turn_off(struct brcm_pcie *pcie)
1587 {
1588         void __iomem *base = pcie->base;
1589         int tmp;
1590
1591         if (brcm_pcie_link_up(pcie))
1592                 brcm_pcie_enter_l23(pcie);
1593         /* Assert fundamental reset */
1594         pcie->perst_set(pcie, 1);
1595
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);
1600
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);
1605
1606         /*
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.
1609          */
1610         if (pcie->type == BCM2712)
1611                 return;
1612         /* Shutdown PCIe bridge */
1613         pcie->bridge_sw_init_set(pcie, 1);
1614 }
1615
1616 static int pci_dev_may_wakeup(struct pci_dev *dev, void *data)
1617 {
1618         bool *ret = data;
1619
1620         if (device_may_wakeup(&dev->dev)) {
1621                 *ret = true;
1622                 dev_info(&dev->dev, "Possible wake-up device; regulators will not be disabled\n");
1623         }
1624         return (int) *ret;
1625 }
1626
1627 static int brcm_pcie_suspend_noirq(struct device *dev)
1628 {
1629         struct brcm_pcie *pcie = dev_get_drvdata(dev);
1630         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1631         int ret;
1632
1633         brcm_pcie_turn_off(pcie);
1634         /*
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.
1638          */
1639         if (brcm_phy_stop(pcie))
1640                 dev_err(dev, "Could not stop phy for suspend\n");
1641
1642         ret = reset_control_assert(pcie->rescal);
1643         if (ret) {
1644                 dev_err(dev, "Could not assert rescal reset\n");
1645                 return ret;
1646         }
1647
1648         if (pcie->sr) {
1649                 /*
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.
1653                  */
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);
1660                         if (ret) {
1661                                 dev_err(dev, "Could not turn off regulators\n");
1662                                 reset_control_reset(pcie->rescal);
1663                                 return ret;
1664                         }
1665                 }
1666         }
1667         clk_disable_unprepare(pcie->clk);
1668
1669         return 0;
1670 }
1671
1672 static int brcm_pcie_resume_noirq(struct device *dev)
1673 {
1674         struct brcm_pcie *pcie = dev_get_drvdata(dev);
1675         void __iomem *base;
1676         u32 tmp;
1677         int ret;
1678
1679         base = pcie->base;
1680         ret = clk_prepare_enable(pcie->clk);
1681         if (ret)
1682                 return ret;
1683
1684         ret = reset_control_reset(pcie->rescal);
1685         if (ret)
1686                 goto err_disable_clk;
1687
1688         ret = brcm_phy_start(pcie);
1689         if (ret)
1690                 goto err_reset;
1691
1692         /* Take bridge out of reset so we can access the SERDES reg */
1693         pcie->bridge_sw_init_set(pcie, 0);
1694
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);
1699
1700         /* wait for serdes to be stable */
1701         udelay(100);
1702
1703         ret = brcm_pcie_setup(pcie);
1704         if (ret)
1705                 goto err_reset;
1706
1707         if (pcie->sr) {
1708                 if (pcie->ep_wakeup_capable) {
1709                         /*
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
1713                          * usage count).
1714                          */
1715                         pcie->ep_wakeup_capable = false;
1716                 } else {
1717                         ret = regulator_bulk_enable(pcie->sr->num_supplies,
1718                                                     pcie->sr->supplies);
1719                         if (ret) {
1720                                 dev_err(dev, "Could not turn on regulators\n");
1721                                 goto err_reset;
1722                         }
1723                 }
1724         }
1725
1726         ret = brcm_pcie_start_link(pcie);
1727         if (ret)
1728                 goto err_regulator;
1729
1730         if (pcie->msi)
1731                 brcm_msi_set_regs(pcie->msi);
1732
1733         return 0;
1734
1735 err_regulator:
1736         if (pcie->sr)
1737                 regulator_bulk_disable(pcie->sr->num_supplies, pcie->sr->supplies);
1738 err_reset:
1739         reset_control_assert(pcie->rescal);
1740 err_disable_clk:
1741         clk_disable_unprepare(pcie->clk);
1742         return ret;
1743 }
1744
1745 static void __brcm_pcie_remove(struct brcm_pcie *pcie)
1746 {
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);
1754 }
1755
1756 static void brcm_pcie_remove(struct platform_device *pdev)
1757 {
1758         struct brcm_pcie *pcie = platform_get_drvdata(pdev);
1759         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1760
1761         pci_stop_root_bus(bridge->bus);
1762         pci_remove_root_bus(bridge->bus);
1763         __brcm_pcie_remove(pcie);
1764 }
1765
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,
1772 };
1773
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,
1780 };
1781
1782 static const struct pcie_cfg_data generic_cfg = {
1783         .offsets        = pcie_offsets,
1784         .type           = GENERIC,
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,
1788 };
1789
1790 static const struct pcie_cfg_data bcm7425_cfg = {
1791         .offsets        = pcie_offsets_bmips_7425,
1792         .type           = BCM7425,
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,
1796 };
1797
1798 static const struct pcie_cfg_data bcm7435_cfg = {
1799         .offsets        = pcie_offsets,
1800         .type           = BCM7435,
1801         .perst_set      = brcm_pcie_perst_set_generic,
1802         .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
1803 };
1804
1805 static const struct pcie_cfg_data bcm4908_cfg = {
1806         .offsets        = pcie_offsets,
1807         .type           = BCM4908,
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,
1811 };
1812
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,
1819 };
1820
1821 static const struct pcie_cfg_data bcm7278_cfg = {
1822         .offsets        = pcie_offset_bcm7278,
1823         .type           = 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,
1827 };
1828
1829 static const struct pcie_cfg_data bcm2711_cfg = {
1830         .offsets        = pcie_offsets,
1831         .type           = BCM2711,
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,
1835 };
1836
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,
1842 };
1843
1844 static const struct pcie_cfg_data bcm2712_cfg = {
1845         .offsets        = pcie_offsets_bcm2712,
1846         .type           = 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,
1850 };
1851
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 },
1862         {},
1863 };
1864
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,
1871 };
1872
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,
1879 };
1880
1881 static int brcm_pcie_probe(struct platform_device *pdev)
1882 {
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;
1887         int ret;
1888
1889         bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie));
1890         if (!bridge)
1891                 return -ENOMEM;
1892
1893         data = of_device_get_match_data(&pdev->dev);
1894         if (!data) {
1895                 dev_err(&pdev->dev, "failed to look up compatible string\n");
1896                 return -EINVAL;
1897         }
1898
1899         pcie = pci_host_bridge_priv(bridge);
1900         pcie->dev = &pdev->dev;
1901         pcie->np = np;
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;
1907
1908         pcie->base = devm_platform_ioremap_resource(pdev, 0);
1909         if (IS_ERR(pcie->base))
1910                 return PTR_ERR(pcie->base);
1911
1912         pcie->clk = devm_clk_get_optional(&pdev->dev, "sw_pcie");
1913         if (IS_ERR(pcie->clk))
1914                 return PTR_ERR(pcie->clk);
1915
1916         ret = of_pci_get_max_link_speed(np);
1917         pcie->gen = (ret < 0) ? 0 : ret;
1918
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");
1922
1923         ret = clk_prepare_enable(pcie->clk);
1924         if (ret) {
1925                 dev_err(&pdev->dev, "could not enable clock\n");
1926                 return ret;
1927         }
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);
1932         }
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);
1937         }
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);
1943         }
1944
1945         ret = reset_control_deassert(pcie->rescal);
1946         if (ret)
1947                 dev_err(&pdev->dev, "failed to deassert 'rescal'\n");
1948
1949         ret = brcm_phy_start(pcie);
1950         if (ret) {
1951                 reset_control_assert(pcie->rescal);
1952                 clk_disable_unprepare(pcie->clk);
1953                 return ret;
1954         }
1955
1956         ret = brcm_pcie_setup(pcie);
1957         if (ret)
1958                 goto fail;
1959
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");
1963                 ret = -ENODEV;
1964                 goto fail;
1965         }
1966
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);
1970                 if (ret) {
1971                         dev_err(pcie->dev, "probe of internal MSI failed");
1972                         goto fail;
1973                 }
1974         } else if (pci_msi_enabled() && msi_np != pcie->np) {
1975                 /* Use RC_BAR1 for MIP access */
1976                 u64 msi_pci_addr;
1977                 u64 msi_phys_addr;
1978
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");
1981                         ret = -EINVAL;
1982                         goto fail;
1983                 }
1984
1985                 if (of_property_read_u64(msi_np, "reg", &msi_phys_addr)) {
1986                         dev_err(pcie->dev, "Unable to find MSI physical address\n");
1987                         ret = -EINVAL;
1988                         goto fail;
1989                 }
1990
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);
1995
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);
2001         }
2002
2003         bridge->ops = pcie->type == BCM7425 ? &brcm7425_pcie_ops : &brcm_pcie_ops;
2004         bridge->sysdata = pcie;
2005
2006         platform_set_drvdata(pdev, pcie);
2007
2008         ret = pci_host_probe(bridge);
2009         if (!ret && !brcm_pcie_link_up(pcie))
2010                 ret = -ENODEV;
2011
2012         if (ret) {
2013                 brcm_pcie_remove(pdev);
2014                 return ret;
2015         }
2016
2017         brcm_pcie_config_clkreq(pcie);
2018
2019         return 0;
2020
2021 fail:
2022         __brcm_pcie_remove(pcie);
2023         return ret;
2024 }
2025
2026 MODULE_DEVICE_TABLE(of, brcm_pcie_match);
2027
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,
2031 };
2032
2033 static struct platform_driver brcm_pcie_driver = {
2034         .probe = brcm_pcie_probe,
2035         .remove_new = brcm_pcie_remove,
2036         .driver = {
2037                 .name = "brcm-pcie",
2038                 .of_match_table = brcm_pcie_match,
2039                 .pm = &brcm_pcie_pm_ops,
2040         },
2041 };
2042 module_platform_driver(brcm_pcie_driver);
2043
2044 MODULE_LICENSE("GPL");
2045 MODULE_DESCRIPTION("Broadcom STB PCIe RC driver");
2046 MODULE_AUTHOR("Broadcom");