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