blob: cd45f0bee9b7996b9305eb80fb282904a87821e4 [file] [log] [blame]
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Broadcom STB PCIe controller driver
4 *
5 * Copyright (C) 2020 Samsung Electronics Co., Ltd.
6 *
7 * Based on upstream Linux kernel driver:
8 * drivers/pci/controller/pcie-brcmstb.c
9 * Copyright (C) 2009 - 2017 Broadcom
10 *
11 * Based driver by Nicolas Saenz Julienne
12 * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
13 */
14
15#include <common.h>
16#include <errno.h>
17#include <dm.h>
18#include <dm/ofnode.h>
19#include <pci.h>
20#include <asm/io.h>
21#include <linux/bitfield.h>
22#include <linux/log2.h>
23#include <linux/iopoll.h>
24
25/* Offset of the mandatory PCIe capability config registers */
26#define BRCM_PCIE_CAP_REGS 0x00ac
27
28/* The PCIe controller register offsets */
29#define PCIE_RC_CFG_VENDOR_SPECIFIC_REG1 0x0188
30#define VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK 0xc
31#define VENDOR_SPECIFIC_REG1_LITTLE_ENDIAN 0x0
32
33#define PCIE_RC_CFG_PRIV1_ID_VAL3 0x043c
34#define CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK 0xffffff
35
Sam Edwardsfa8c9882023-08-16 15:27:53 -070036#define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY 0x04dc
37#define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_ASPM_SUPPORT_MASK 0xc00
38
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +020039#define PCIE_RC_DL_MDIO_ADDR 0x1100
40#define PCIE_RC_DL_MDIO_WR_DATA 0x1104
41#define PCIE_RC_DL_MDIO_RD_DATA 0x1108
42
43#define PCIE_MISC_MISC_CTRL 0x4008
44#define MISC_CTRL_SCB_ACCESS_EN_MASK 0x1000
45#define MISC_CTRL_CFG_READ_UR_MODE_MASK 0x2000
46#define MISC_CTRL_MAX_BURST_SIZE_MASK 0x300000
47#define MISC_CTRL_MAX_BURST_SIZE_128 0x0
48#define MISC_CTRL_SCB0_SIZE_MASK 0xf8000000
49
50#define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO 0x400c
51#define PCIE_MEM_WIN0_LO(win) \
52 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO + ((win) * 4)
53
54#define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI 0x4010
55#define PCIE_MEM_WIN0_HI(win) \
56 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI + ((win) * 4)
57
58#define PCIE_MISC_RC_BAR1_CONFIG_LO 0x402c
59#define RC_BAR1_CONFIG_LO_SIZE_MASK 0x1f
60
61#define PCIE_MISC_RC_BAR2_CONFIG_LO 0x4034
62#define RC_BAR2_CONFIG_LO_SIZE_MASK 0x1f
63#define PCIE_MISC_RC_BAR2_CONFIG_HI 0x4038
64
65#define PCIE_MISC_RC_BAR3_CONFIG_LO 0x403c
66#define RC_BAR3_CONFIG_LO_SIZE_MASK 0x1f
67
68#define PCIE_MISC_PCIE_STATUS 0x4068
69#define STATUS_PCIE_PORT_MASK 0x80
70#define STATUS_PCIE_PORT_SHIFT 7
71#define STATUS_PCIE_DL_ACTIVE_MASK 0x20
72#define STATUS_PCIE_DL_ACTIVE_SHIFT 5
73#define STATUS_PCIE_PHYLINKUP_MASK 0x10
74#define STATUS_PCIE_PHYLINKUP_SHIFT 4
75
76#define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT 0x4070
77#define MEM_WIN0_BASE_LIMIT_LIMIT_MASK 0xfff00000
78#define MEM_WIN0_BASE_LIMIT_BASE_MASK 0xfff0
79#define MEM_WIN0_BASE_LIMIT_BASE_HI_SHIFT 12
80#define PCIE_MEM_WIN0_BASE_LIMIT(win) \
81 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT + ((win) * 4)
82
83#define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI 0x4080
84#define MEM_WIN0_BASE_HI_BASE_MASK 0xff
85#define PCIE_MEM_WIN0_BASE_HI(win) \
86 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI + ((win) * 8)
87
88#define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI 0x4084
89#define PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK 0xff
90#define PCIE_MEM_WIN0_LIMIT_HI(win) \
91 PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI + ((win) * 8)
92
93#define PCIE_MISC_HARD_PCIE_HARD_DEBUG 0x4204
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +020094#define PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x08000000
95
96#define PCIE_MSI_INTR2_CLR 0x4508
97#define PCIE_MSI_INTR2_MASK_SET 0x4510
98
99#define PCIE_EXT_CFG_DATA 0x8000
100
101#define PCIE_EXT_CFG_INDEX 0x9000
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200102
103#define PCIE_RGR1_SW_INIT_1 0x9210
104#define RGR1_SW_INIT_1_PERST_MASK 0x1
105#define RGR1_SW_INIT_1_INIT_MASK 0x2
106
107/* PCIe parameters */
108#define BRCM_NUM_PCIE_OUT_WINS 4
109
110/* MDIO registers */
111#define MDIO_PORT0 0x0
112#define MDIO_DATA_MASK 0x7fffffff
113#define MDIO_DATA_SHIFT 0
114#define MDIO_PORT_MASK 0xf0000
115#define MDIO_PORT_SHIFT 16
116#define MDIO_REGAD_MASK 0xffff
117#define MDIO_REGAD_SHIFT 0
118#define MDIO_CMD_MASK 0xfff00000
119#define MDIO_CMD_SHIFT 20
120#define MDIO_CMD_READ 0x1
121#define MDIO_CMD_WRITE 0x0
122#define MDIO_DATA_DONE_MASK 0x80000000
123#define SSC_REGS_ADDR 0x1100
124#define SET_ADDR_OFFSET 0x1f
125#define SSC_CNTL_OFFSET 0x2
126#define SSC_CNTL_OVRD_EN_MASK 0x8000
127#define SSC_CNTL_OVRD_VAL_MASK 0x4000
128#define SSC_STATUS_OFFSET 0x1
129#define SSC_STATUS_SSC_MASK 0x400
130#define SSC_STATUS_SSC_SHIFT 10
131#define SSC_STATUS_PLL_LOCK_MASK 0x800
132#define SSC_STATUS_PLL_LOCK_SHIFT 11
133
134/**
135 * struct brcm_pcie - the PCIe controller state
136 * @base: Base address of memory mapped IO registers of the controller
137 * @gen: Non-zero value indicates limitation of the PCIe controller operation
138 * to a specific generation (1, 2 or 3)
139 * @ssc: true indicates active Spread Spectrum Clocking operation
140 */
141struct brcm_pcie {
142 void __iomem *base;
143
144 int gen;
145 bool ssc;
146};
147
148/**
149 * brcm_pcie_encode_ibar_size() - Encode the inbound "BAR" region size
150 * @size: The inbound region size
151 *
152 * This function converts size of the inbound "BAR" region to the non-linear
153 * values of the PCIE_MISC_RC_BAR[123]_CONFIG_LO register SIZE field.
154 *
155 * Return: The encoded inbound region size
156 */
157static int brcm_pcie_encode_ibar_size(u64 size)
158{
159 int log2_in = ilog2(size);
160
161 if (log2_in >= 12 && log2_in <= 15)
162 /* Covers 4KB to 32KB (inclusive) */
163 return (log2_in - 12) + 0x1c;
164 else if (log2_in >= 16 && log2_in <= 37)
165 /* Covers 64KB to 32GB, (inclusive) */
166 return log2_in - 15;
167
168 /* Something is awry so disable */
169 return 0;
170}
171
172/**
173 * brcm_pcie_rc_mode() - Check if PCIe controller is in RC mode
174 * @pcie: Pointer to the PCIe controller state
175 *
176 * The controller is capable of serving in both RC and EP roles.
177 *
178 * Return: true for RC mode, false for EP mode.
179 */
180static bool brcm_pcie_rc_mode(struct brcm_pcie *pcie)
181{
182 u32 val;
183
184 val = readl(pcie->base + PCIE_MISC_PCIE_STATUS);
185
186 return (val & STATUS_PCIE_PORT_MASK) >> STATUS_PCIE_PORT_SHIFT;
187}
188
189/**
190 * brcm_pcie_link_up() - Check whether the PCIe link is up
191 * @pcie: Pointer to the PCIe controller state
192 *
193 * Return: true if the link is up, false otherwise.
194 */
195static bool brcm_pcie_link_up(struct brcm_pcie *pcie)
196{
197 u32 val, dla, plu;
198
199 val = readl(pcie->base + PCIE_MISC_PCIE_STATUS);
200 dla = (val & STATUS_PCIE_DL_ACTIVE_MASK) >> STATUS_PCIE_DL_ACTIVE_SHIFT;
201 plu = (val & STATUS_PCIE_PHYLINKUP_MASK) >> STATUS_PCIE_PHYLINKUP_SHIFT;
202
203 return dla && plu;
204}
205
206static int brcm_pcie_config_address(const struct udevice *dev, pci_dev_t bdf,
207 uint offset, void **paddress)
208{
209 struct brcm_pcie *pcie = dev_get_priv(dev);
210 unsigned int pci_bus = PCI_BUS(bdf);
211 unsigned int pci_dev = PCI_DEV(bdf);
212 unsigned int pci_func = PCI_FUNC(bdf);
213 int idx;
214
215 /*
216 * Busses 0 (host PCIe bridge) and 1 (its immediate child)
217 * are limited to a single device each
218 */
219 if (pci_bus < 2 && pci_dev > 0)
220 return -EINVAL;
221
222 /* Accesses to the RC go right to the RC registers */
223 if (pci_bus == 0) {
224 *paddress = pcie->base + offset;
225 return 0;
226 }
227
Sam Edwards30e58592023-08-14 16:34:13 -0600228 /* An access to our HW w/o link-up will cause a CPU Abort */
229 if (!brcm_pcie_link_up(pcie))
230 return -EINVAL;
231
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200232 /* For devices, write to the config space index register */
Pali Rohár9e98eb72021-11-24 18:00:31 +0100233 idx = PCIE_ECAM_OFFSET(pci_bus, pci_dev, pci_func, 0);
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200234
235 writel(idx, pcie->base + PCIE_EXT_CFG_INDEX);
236 *paddress = pcie->base + PCIE_EXT_CFG_DATA + offset;
237
238 return 0;
239}
240
241static int brcm_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
242 uint offset, ulong *valuep,
243 enum pci_size_t size)
244{
245 return pci_generic_mmap_read_config(bus, brcm_pcie_config_address,
246 bdf, offset, valuep, size);
247}
248
249static int brcm_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
250 uint offset, ulong value,
251 enum pci_size_t size)
252{
253 return pci_generic_mmap_write_config(bus, brcm_pcie_config_address,
254 bdf, offset, value, size);
255}
256
257static const char *link_speed_to_str(unsigned int cls)
258{
259 switch (cls) {
260 case PCI_EXP_LNKSTA_CLS_2_5GB: return "2.5";
261 case PCI_EXP_LNKSTA_CLS_5_0GB: return "5.0";
262 case PCI_EXP_LNKSTA_CLS_8_0GB: return "8.0";
263 default:
264 break;
265 }
266
267 return "??";
268}
269
270static u32 brcm_pcie_mdio_form_pkt(unsigned int port, unsigned int regad,
271 unsigned int cmd)
272{
273 u32 pkt;
274
275 pkt = (port << MDIO_PORT_SHIFT) & MDIO_PORT_MASK;
276 pkt |= (regad << MDIO_REGAD_SHIFT) & MDIO_REGAD_MASK;
277 pkt |= (cmd << MDIO_CMD_SHIFT) & MDIO_CMD_MASK;
278
279 return pkt;
280}
281
282/**
283 * brcm_pcie_mdio_read() - Perform a register read on the internal MDIO bus
284 * @base: Pointer to the PCIe controller IO registers
285 * @port: The MDIO port number
286 * @regad: The register address
287 * @val: A pointer at which to store the read value
288 *
289 * Return: 0 on success and register value in @val, negative error value
290 * on failure.
291 */
292static int brcm_pcie_mdio_read(void __iomem *base, unsigned int port,
293 unsigned int regad, u32 *val)
294{
295 u32 data, addr;
296 int ret;
297
298 addr = brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_READ);
299 writel(addr, base + PCIE_RC_DL_MDIO_ADDR);
300 readl(base + PCIE_RC_DL_MDIO_ADDR);
301
302 ret = readl_poll_timeout(base + PCIE_RC_DL_MDIO_RD_DATA, data,
303 (data & MDIO_DATA_DONE_MASK), 100);
304
305 *val = data & MDIO_DATA_MASK;
306
307 return ret;
308}
309
310/**
311 * brcm_pcie_mdio_write() - Perform a register write on the internal MDIO bus
312 * @base: Pointer to the PCIe controller IO registers
313 * @port: The MDIO port number
314 * @regad: Address of the register
315 * @wrdata: The value to write
316 *
317 * Return: 0 on success, negative error value on failure.
318 */
319static int brcm_pcie_mdio_write(void __iomem *base, unsigned int port,
320 unsigned int regad, u16 wrdata)
321{
322 u32 data, addr;
323
324 addr = brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_WRITE);
325 writel(addr, base + PCIE_RC_DL_MDIO_ADDR);
326 readl(base + PCIE_RC_DL_MDIO_ADDR);
327 writel(MDIO_DATA_DONE_MASK | wrdata, base + PCIE_RC_DL_MDIO_WR_DATA);
328
329 return readl_poll_timeout(base + PCIE_RC_DL_MDIO_WR_DATA, data,
330 !(data & MDIO_DATA_DONE_MASK), 100);
331}
332
333/**
334 * brcm_pcie_set_ssc() - Configure the controller for Spread Spectrum Clocking
335 * @base: pointer to the PCIe controller IO registers
336 *
337 * Return: 0 on success, negative error value on failure.
338 */
339static int brcm_pcie_set_ssc(void __iomem *base)
340{
341 int pll, ssc;
342 int ret;
343 u32 tmp;
344
345 ret = brcm_pcie_mdio_write(base, MDIO_PORT0, SET_ADDR_OFFSET,
346 SSC_REGS_ADDR);
347 if (ret < 0)
348 return ret;
349
350 ret = brcm_pcie_mdio_read(base, MDIO_PORT0, SSC_CNTL_OFFSET, &tmp);
351 if (ret < 0)
352 return ret;
353
354 tmp |= (SSC_CNTL_OVRD_EN_MASK | SSC_CNTL_OVRD_VAL_MASK);
355
356 ret = brcm_pcie_mdio_write(base, MDIO_PORT0, SSC_CNTL_OFFSET, tmp);
357 if (ret < 0)
358 return ret;
359
360 udelay(1000);
361 ret = brcm_pcie_mdio_read(base, MDIO_PORT0, SSC_STATUS_OFFSET, &tmp);
362 if (ret < 0)
363 return ret;
364
365 ssc = (tmp & SSC_STATUS_SSC_MASK) >> SSC_STATUS_SSC_SHIFT;
366 pll = (tmp & SSC_STATUS_PLL_LOCK_MASK) >> SSC_STATUS_PLL_LOCK_SHIFT;
367
368 return ssc && pll ? 0 : -EIO;
369}
370
371/**
372 * brcm_pcie_set_gen() - Limits operation to a specific generation (1, 2 or 3)
373 * @pcie: pointer to the PCIe controller state
374 * @gen: PCIe generation to limit the controller's operation to
375 */
376static void brcm_pcie_set_gen(struct brcm_pcie *pcie, unsigned int gen)
377{
378 void __iomem *cap_base = pcie->base + BRCM_PCIE_CAP_REGS;
379
380 u16 lnkctl2 = readw(cap_base + PCI_EXP_LNKCTL2);
381 u32 lnkcap = readl(cap_base + PCI_EXP_LNKCAP);
382
383 lnkcap = (lnkcap & ~PCI_EXP_LNKCAP_SLS) | gen;
384 writel(lnkcap, cap_base + PCI_EXP_LNKCAP);
385
386 lnkctl2 = (lnkctl2 & ~0xf) | gen;
387 writew(lnkctl2, cap_base + PCI_EXP_LNKCTL2);
388}
389
390static void brcm_pcie_set_outbound_win(struct brcm_pcie *pcie,
391 unsigned int win, u64 phys_addr,
392 u64 pcie_addr, u64 size)
393{
394 void __iomem *base = pcie->base;
395 u32 phys_addr_mb_high, limit_addr_mb_high;
396 phys_addr_t phys_addr_mb, limit_addr_mb;
397 int high_addr_shift;
398 u32 tmp;
399
400 /* Set the base of the pcie_addr window */
401 writel(lower_32_bits(pcie_addr), base + PCIE_MEM_WIN0_LO(win));
402 writel(upper_32_bits(pcie_addr), base + PCIE_MEM_WIN0_HI(win));
403
404 /* Write the addr base & limit lower bits (in MBs) */
405 phys_addr_mb = phys_addr / SZ_1M;
406 limit_addr_mb = (phys_addr + size - 1) / SZ_1M;
407
408 tmp = readl(base + PCIE_MEM_WIN0_BASE_LIMIT(win));
409 u32p_replace_bits(&tmp, phys_addr_mb,
410 MEM_WIN0_BASE_LIMIT_BASE_MASK);
411 u32p_replace_bits(&tmp, limit_addr_mb,
412 MEM_WIN0_BASE_LIMIT_LIMIT_MASK);
413 writel(tmp, base + PCIE_MEM_WIN0_BASE_LIMIT(win));
414
415 /* Write the cpu & limit addr upper bits */
416 high_addr_shift = MEM_WIN0_BASE_LIMIT_BASE_HI_SHIFT;
417 phys_addr_mb_high = phys_addr_mb >> high_addr_shift;
418 tmp = readl(base + PCIE_MEM_WIN0_BASE_HI(win));
419 u32p_replace_bits(&tmp, phys_addr_mb_high,
420 MEM_WIN0_BASE_HI_BASE_MASK);
421 writel(tmp, base + PCIE_MEM_WIN0_BASE_HI(win));
422
423 limit_addr_mb_high = limit_addr_mb >> high_addr_shift;
424 tmp = readl(base + PCIE_MEM_WIN0_LIMIT_HI(win));
425 u32p_replace_bits(&tmp, limit_addr_mb_high,
426 PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK);
427 writel(tmp, base + PCIE_MEM_WIN0_LIMIT_HI(win));
428}
429
430static int brcm_pcie_probe(struct udevice *dev)
431{
432 struct udevice *ctlr = pci_get_controller(dev);
433 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
434 struct brcm_pcie *pcie = dev_get_priv(dev);
435 void __iomem *base = pcie->base;
Nicolas Saenz Julienne038876b2021-01-12 13:55:21 +0100436 struct pci_region region;
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200437 bool ssc_good = false;
438 int num_out_wins = 0;
439 u64 rc_bar2_offset, rc_bar2_size;
440 unsigned int scb_size_val;
441 int i, ret;
442 u16 nlw, cls, lnksta;
443 u32 tmp;
444
445 /*
446 * Reset the bridge, assert the fundamental reset. Note for some SoCs,
447 * e.g. BCM7278, the fundamental reset should not be asserted here.
448 * This will need to be changed when support for other SoCs is added.
449 */
450 setbits_le32(base + PCIE_RGR1_SW_INIT_1,
451 RGR1_SW_INIT_1_INIT_MASK | RGR1_SW_INIT_1_PERST_MASK);
452 /*
453 * The delay is a safety precaution to preclude the reset signal
454 * from looking like a glitch.
455 */
456 udelay(100);
457
458 /* Take the bridge out of reset */
459 clrbits_le32(base + PCIE_RGR1_SW_INIT_1, RGR1_SW_INIT_1_INIT_MASK);
460
461 clrbits_le32(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG,
462 PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
463
464 /* Wait for SerDes to be stable */
465 udelay(100);
466
467 /* Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN */
468 clrsetbits_le32(base + PCIE_MISC_MISC_CTRL,
469 MISC_CTRL_MAX_BURST_SIZE_MASK,
470 MISC_CTRL_SCB_ACCESS_EN_MASK |
471 MISC_CTRL_CFG_READ_UR_MODE_MASK |
472 MISC_CTRL_MAX_BURST_SIZE_128);
Nicolas Saenz Julienne038876b2021-01-12 13:55:21 +0100473
474 pci_get_dma_regions(dev, &region, 0);
475 rc_bar2_offset = region.bus_start - region.phys_start;
476 rc_bar2_size = 1ULL << fls64(region.size - 1);
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200477
478 tmp = lower_32_bits(rc_bar2_offset);
479 u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(rc_bar2_size),
480 RC_BAR2_CONFIG_LO_SIZE_MASK);
481 writel(tmp, base + PCIE_MISC_RC_BAR2_CONFIG_LO);
482 writel(upper_32_bits(rc_bar2_offset),
483 base + PCIE_MISC_RC_BAR2_CONFIG_HI);
484
485 scb_size_val = rc_bar2_size ?
486 ilog2(rc_bar2_size) - 15 : 0xf; /* 0xf is 1GB */
487
488 tmp = readl(base + PCIE_MISC_MISC_CTRL);
489 u32p_replace_bits(&tmp, scb_size_val,
490 MISC_CTRL_SCB0_SIZE_MASK);
491 writel(tmp, base + PCIE_MISC_MISC_CTRL);
492
493 /* Disable the PCIe->GISB memory window (RC_BAR1) */
494 clrbits_le32(base + PCIE_MISC_RC_BAR1_CONFIG_LO,
495 RC_BAR1_CONFIG_LO_SIZE_MASK);
496
497 /* Disable the PCIe->SCB memory window (RC_BAR3) */
498 clrbits_le32(base + PCIE_MISC_RC_BAR3_CONFIG_LO,
499 RC_BAR3_CONFIG_LO_SIZE_MASK);
500
501 /* Mask all interrupts since we are not handling any yet */
502 writel(0xffffffff, base + PCIE_MSI_INTR2_MASK_SET);
503
504 /* Clear any interrupts we find on boot */
505 writel(0xffffffff, base + PCIE_MSI_INTR2_CLR);
506
507 if (pcie->gen)
508 brcm_pcie_set_gen(pcie, pcie->gen);
509
510 /* Unassert the fundamental reset */
511 clrbits_le32(pcie->base + PCIE_RGR1_SW_INIT_1,
512 RGR1_SW_INIT_1_PERST_MASK);
513
Sam Edwards30e58592023-08-14 16:34:13 -0600514 /*
515 * Wait for 100ms after PERST# deassertion; see PCIe CEM specification
516 * sections 2.2, PCIe r5.0, 6.6.1.
517 */
518 mdelay(100);
519
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200520 /* Give the RC/EP time to wake up, before trying to configure RC.
521 * Intermittently check status for link-up, up to a total of 100ms.
522 */
523 for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5)
524 mdelay(5);
525
526 if (!brcm_pcie_link_up(pcie)) {
527 printf("PCIe BRCM: link down\n");
528 return -EINVAL;
529 }
530
531 if (!brcm_pcie_rc_mode(pcie)) {
532 printf("PCIe misconfigured; is in EP mode\n");
533 return -EINVAL;
534 }
535
536 for (i = 0; i < hose->region_count; i++) {
537 struct pci_region *reg = &hose->regions[i];
538
539 if (reg->flags != PCI_REGION_MEM)
540 continue;
541
542 if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS)
543 return -EINVAL;
544
545 brcm_pcie_set_outbound_win(pcie, num_out_wins, reg->phys_start,
546 reg->bus_start, reg->size);
547
548 num_out_wins++;
549 }
550
551 /*
552 * For config space accesses on the RC, show the right class for
553 * a PCIe-PCIe bridge (the default setting is to be EP mode).
554 */
555 clrsetbits_le32(base + PCIE_RC_CFG_PRIV1_ID_VAL3,
556 CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK, 0x060400);
557
558 if (pcie->ssc) {
559 ret = brcm_pcie_set_ssc(pcie->base);
560 if (!ret)
561 ssc_good = true;
562 else
563 printf("PCIe BRCM: failed attempt to enter SSC mode\n");
564 }
565
566 lnksta = readw(base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKSTA);
567 cls = lnksta & PCI_EXP_LNKSTA_CLS;
568 nlw = (lnksta & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT;
569
570 printf("PCIe BRCM: link up, %s Gbps x%u %s\n", link_speed_to_str(cls),
571 nlw, ssc_good ? "(SSC)" : "(!SSC)");
572
573 /* PCIe->SCB endian mode for BAR */
574 clrsetbits_le32(base + PCIE_RC_CFG_VENDOR_SPECIFIC_REG1,
575 VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK,
576 VENDOR_SPECIFIC_REG1_LITTLE_ENDIAN);
Sam Edwardsfa8c9882023-08-16 15:27:53 -0700577
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200578 /*
Sam Edwardsfa8c9882023-08-16 15:27:53 -0700579 * We used to enable the CLKREQ# input here, but a few PCIe cards don't
580 * attach anything to the CLKREQ# line, so we shouldn't assume that
581 * it's connected and working. The controller does allow detecting
582 * whether the port on the other side of our link is/was driving this
583 * signal, so we could check before we assume. But because this signal
584 * is for power management, which doesn't make sense in a bootloader,
585 * let's instead just unadvertise ASPM support.
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200586 */
Sam Edwardsfa8c9882023-08-16 15:27:53 -0700587 clrbits_le32(base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY,
588 PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_ASPM_SUPPORT_MASK);
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200589
590 return 0;
591}
592
Nicolas Saenz Juliennedf10fe82021-01-14 16:49:01 +0100593static int brcm_pcie_remove(struct udevice *dev)
594{
595 struct brcm_pcie *pcie = dev_get_priv(dev);
596 void __iomem *base = pcie->base;
597
598 /* Assert fundamental reset */
599 setbits_le32(base + PCIE_RGR1_SW_INIT_1, RGR1_SW_INIT_1_PERST_MASK);
600
601 /* Turn off SerDes */
602 setbits_le32(base + PCIE_MISC_HARD_PCIE_HARD_DEBUG,
603 PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
604
605 /* Shutdown bridge */
606 setbits_le32(base + PCIE_RGR1_SW_INIT_1, RGR1_SW_INIT_1_INIT_MASK);
607
608 return 0;
609}
610
Simon Glassaad29ae2020-12-03 16:55:21 -0700611static int brcm_pcie_of_to_plat(struct udevice *dev)
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200612{
613 struct brcm_pcie *pcie = dev_get_priv(dev);
614 ofnode dn = dev_ofnode(dev);
615 u32 max_link_speed;
616 int ret;
617
618 /* Get the controller base address */
619 pcie->base = dev_read_addr_ptr(dev);
620 if (!pcie->base)
621 return -EINVAL;
622
623 pcie->ssc = ofnode_read_bool(dn, "brcm,enable-ssc");
624
625 ret = ofnode_read_u32(dn, "max-link-speed", &max_link_speed);
626 if (ret < 0 || max_link_speed > 4)
627 pcie->gen = 0;
628 else
629 pcie->gen = max_link_speed;
630
631 return 0;
632}
633
634static const struct dm_pci_ops brcm_pcie_ops = {
635 .read_config = brcm_pcie_read_config,
636 .write_config = brcm_pcie_write_config,
637};
638
639static const struct udevice_id brcm_pcie_ids[] = {
640 { .compatible = "brcm,bcm2711-pcie" },
641 { }
642};
643
644U_BOOT_DRIVER(pcie_brcm_base) = {
645 .name = "pcie_brcm",
646 .id = UCLASS_PCI,
647 .ops = &brcm_pcie_ops,
648 .of_match = brcm_pcie_ids,
649 .probe = brcm_pcie_probe,
Nicolas Saenz Juliennedf10fe82021-01-14 16:49:01 +0100650 .remove = brcm_pcie_remove,
Simon Glassaad29ae2020-12-03 16:55:21 -0700651 .of_to_plat = brcm_pcie_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700652 .priv_auto = sizeof(struct brcm_pcie),
Nicolas Saenz Juliennedf10fe82021-01-14 16:49:01 +0100653 .flags = DM_FLAG_OS_PREPARE,
Sylwester Nawrocki88f51f72020-05-25 13:39:58 +0200654};