blob: 43b919175c9255de3cb7415ce66ee0c9c6686569 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Shadi Ammouri3b386452016-10-27 13:29:41 +02002/*
3 * Copyright (C) 2015 Marvell International Ltd.
4 *
5 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
6 *
7 * Based on:
8 * - drivers/pci/pcie_imx.c
9 * - drivers/pci/pci_mvebu.c
10 * - drivers/pci/pcie_xilinx.c
Shadi Ammouri3b386452016-10-27 13:29:41 +020011 */
12
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <config.h>
Shadi Ammouri3b386452016-10-27 13:29:41 +020014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Shadi Ammouri3b386452016-10-27 13:29:41 +020016#include <pci.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060017#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Shadi Ammouri3b386452016-10-27 13:29:41 +020019#include <asm/io.h>
Konstantin Porotchkin4eb09cd2017-02-08 17:34:13 +020020#include <asm-generic/gpio.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Shadi Ammouri3b386452016-10-27 13:29:41 +020022
23DECLARE_GLOBAL_DATA_PTR;
24
25/* PCI Config space registers */
26#define PCIE_CONFIG_BAR0 0x10
27#define PCIE_LINK_STATUS_REG 0x80
28#define PCIE_LINK_STATUS_SPEED_OFF 16
29#define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
30#define PCIE_LINK_STATUS_WIDTH_OFF 20
31#define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
32
33/* Resizable bar capability registers */
34#define RESIZABLE_BAR_CAP 0x250
35#define RESIZABLE_BAR_CTL0 0x254
36#define RESIZABLE_BAR_CTL1 0x258
37
38/* iATU registers */
39#define PCIE_ATU_VIEWPORT 0x900
40#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
41#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
42#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
43#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
44#define PCIE_ATU_CR1 0x904
45#define PCIE_ATU_TYPE_MEM (0x0 << 0)
46#define PCIE_ATU_TYPE_IO (0x2 << 0)
47#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
48#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
49#define PCIE_ATU_CR2 0x908
50#define PCIE_ATU_ENABLE (0x1 << 31)
51#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
52#define PCIE_ATU_LOWER_BASE 0x90C
53#define PCIE_ATU_UPPER_BASE 0x910
54#define PCIE_ATU_LIMIT 0x914
55#define PCIE_ATU_LOWER_TARGET 0x918
56#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
57#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
58#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
59#define PCIE_ATU_UPPER_TARGET 0x91C
60
61#define PCIE_LINK_CAPABILITY 0x7C
62#define PCIE_LINK_CTL_2 0xA0
63#define TARGET_LINK_SPEED_MASK 0xF
64#define LINK_SPEED_GEN_1 0x1
65#define LINK_SPEED_GEN_2 0x2
66#define LINK_SPEED_GEN_3 0x3
67
68#define PCIE_GEN3_RELATED 0x890
69#define GEN3_EQU_DISABLE (1 << 16)
70#define GEN3_ZRXDC_NON_COMP (1 << 0)
71
72#define PCIE_GEN3_EQU_CTRL 0x8A8
73#define GEN3_EQU_EVAL_2MS_DISABLE (1 << 5)
74
75#define PCIE_ROOT_COMPLEX_MODE_MASK (0xF << 4)
76
77#define PCIE_LINK_UP_TIMEOUT_MS 100
78
79#define PCIE_GLOBAL_CONTROL 0x8000
80#define PCIE_APP_LTSSM_EN (1 << 2)
81#define PCIE_DEVICE_TYPE_OFFSET (4)
82#define PCIE_DEVICE_TYPE_MASK (0xF)
83#define PCIE_DEVICE_TYPE_EP (0x0) /* Endpoint */
84#define PCIE_DEVICE_TYPE_LEP (0x1) /* Legacy endpoint */
85#define PCIE_DEVICE_TYPE_RC (0x4) /* Root complex */
86
87#define PCIE_GLOBAL_STATUS 0x8008
88#define PCIE_GLB_STS_RDLH_LINK_UP (1 << 1)
89#define PCIE_GLB_STS_PHY_LINK_UP (1 << 9)
90
91#define PCIE_ARCACHE_TRC 0x8050
92#define PCIE_AWCACHE_TRC 0x8054
93#define ARCACHE_SHAREABLE_CACHEABLE 0x3511
94#define AWCACHE_SHAREABLE_CACHEABLE 0x5311
95
96#define LINK_SPEED_GEN_1 0x1
97#define LINK_SPEED_GEN_2 0x2
98#define LINK_SPEED_GEN_3 0x3
99
100/**
101 * struct pcie_dw_mvebu - MVEBU DW PCIe controller state
102 *
103 * @ctrl_base: The base address of the register space
104 * @cfg_base: The base address of the configuration space
105 * @cfg_size: The size of the configuration space which is needed
106 * as it gets written into the PCIE_ATU_LIMIT register
107 * @first_busno: This driver supports multiple PCIe controllers.
108 * first_busno stores the bus number of the PCIe root-port
109 * number which may vary depending on the PCIe setup
110 * (PEX switches etc).
111 */
112struct pcie_dw_mvebu {
113 void *ctrl_base;
114 void *cfg_base;
115 fdt_size_t cfg_size;
116 int first_busno;
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200117
118 /* IO and MEM PCI regions */
Marcin Wojtas49e33bd2021-04-30 15:33:15 +0200119 int region_count;
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200120 struct pci_region io;
121 struct pci_region mem;
Shadi Ammouri3b386452016-10-27 13:29:41 +0200122};
123
124static int pcie_dw_get_link_speed(const void *regs_base)
125{
126 return (readl(regs_base + PCIE_LINK_STATUS_REG) &
127 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
128}
129
130static int pcie_dw_get_link_width(const void *regs_base)
131{
132 return (readl(regs_base + PCIE_LINK_STATUS_REG) &
133 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
134}
135
136/**
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200137 * pcie_dw_prog_outbound_atu() - Configure ATU for outbound accesses
138 *
139 * @pcie: Pointer to the PCI controller state
140 * @index: ATU region index
141 * @type: ATU accsess type
142 * @cpu_addr: the physical address for the translation entry
143 * @pci_addr: the pcie bus address for the translation entry
144 * @size: the size of the translation entry
145 */
146static void pcie_dw_prog_outbound_atu(struct pcie_dw_mvebu *pcie, int index,
147 int type, u64 cpu_addr, u64 pci_addr,
148 u32 size)
149{
150 writel(PCIE_ATU_REGION_OUTBOUND | index,
151 pcie->ctrl_base + PCIE_ATU_VIEWPORT);
152 writel(lower_32_bits(cpu_addr), pcie->ctrl_base + PCIE_ATU_LOWER_BASE);
153 writel(upper_32_bits(cpu_addr), pcie->ctrl_base + PCIE_ATU_UPPER_BASE);
154 writel(lower_32_bits(cpu_addr + size - 1),
155 pcie->ctrl_base + PCIE_ATU_LIMIT);
156 writel(lower_32_bits(pci_addr),
157 pcie->ctrl_base + PCIE_ATU_LOWER_TARGET);
158 writel(upper_32_bits(pci_addr),
159 pcie->ctrl_base + PCIE_ATU_UPPER_TARGET);
160 writel(type, pcie->ctrl_base + PCIE_ATU_CR1);
161 writel(PCIE_ATU_ENABLE, pcie->ctrl_base + PCIE_ATU_CR2);
162}
163
164/**
Shadi Ammouri3b386452016-10-27 13:29:41 +0200165 * set_cfg_address() - Configure the PCIe controller config space access
166 *
167 * @pcie: Pointer to the PCI controller state
168 * @d: PCI device to access
169 * @where: Offset in the configuration space
170 *
171 * Configures the PCIe controller to access the configuration space of
172 * a specific PCIe device and returns the address to use for this
173 * access.
174 *
175 * Return: Address that can be used to access the configation space
176 * of the requested device / offset
177 */
178static uintptr_t set_cfg_address(struct pcie_dw_mvebu *pcie,
179 pci_dev_t d, uint where)
180{
181 uintptr_t va_address;
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200182 u32 atu_type;
Shadi Ammouri3b386452016-10-27 13:29:41 +0200183
184 /*
185 * Region #0 is used for Outbound CFG space access.
186 * Direction = Outbound
187 * Region Index = 0
188 */
Shadi Ammouri3b386452016-10-27 13:29:41 +0200189
190 if (PCI_BUS(d) == (pcie->first_busno + 1))
191 /* For local bus, change TLP Type field to 4. */
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200192 atu_type = PCIE_ATU_TYPE_CFG0;
Shadi Ammouri3b386452016-10-27 13:29:41 +0200193 else
194 /* Otherwise, change TLP Type field to 5. */
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200195 atu_type = PCIE_ATU_TYPE_CFG1;
Shadi Ammouri3b386452016-10-27 13:29:41 +0200196
197 if (PCI_BUS(d) == pcie->first_busno) {
198 /* Accessing root port configuration space. */
199 va_address = (uintptr_t)pcie->ctrl_base;
200 } else {
Minghuan Liana1c94382017-10-20 10:45:50 +0800201 d = PCI_MASK_BUS(d) | (PCI_BUS(d) - pcie->first_busno);
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200202 pcie_dw_prog_outbound_atu(pcie, PCIE_ATU_REGION_INDEX0,
203 atu_type, (u64)pcie->cfg_base,
204 d << 8, pcie->cfg_size);
Shadi Ammouri3b386452016-10-27 13:29:41 +0200205 va_address = (uintptr_t)pcie->cfg_base;
206 }
207
208 va_address += where & ~0x3;
209
210 return va_address;
211}
212
213/**
214 * pcie_dw_addr_valid() - Check for valid bus address
215 *
216 * @d: The PCI device to access
217 * @first_busno: Bus number of the PCIe controller root complex
218 *
219 * Return 1 (true) if the PCI device can be accessed by this controller.
220 *
221 * Return: 1 on valid, 0 on invalid
222 */
223static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
224{
225 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
226 return 0;
227 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
228 return 0;
229
230 return 1;
231}
232
233/**
234 * pcie_dw_mvebu_read_config() - Read from configuration space
235 *
236 * @bus: Pointer to the PCI bus
237 * @bdf: Identifies the PCIe device to access
238 * @offset: The offset into the device's configuration space
239 * @valuep: A pointer at which to store the read value
240 * @size: Indicates the size of access to perform
241 *
242 * Read a value of size @size from offset @offset within the configuration
243 * space of the device identified by the bus, device & function numbers in @bdf
244 * on the PCI bus @bus.
245 *
246 * Return: 0 on success
247 */
Simon Glass2a311e82020-01-27 08:49:37 -0700248static int pcie_dw_mvebu_read_config(const struct udevice *bus, pci_dev_t bdf,
Shadi Ammouri3b386452016-10-27 13:29:41 +0200249 uint offset, ulong *valuep,
250 enum pci_size_t size)
251{
252 struct pcie_dw_mvebu *pcie = dev_get_priv(bus);
253 uintptr_t va_address;
254 ulong value;
255
256 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
257 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
258
259 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
260 debug("- out of range\n");
261 *valuep = pci_get_ff(size);
262 return 0;
263 }
264
265 va_address = set_cfg_address(pcie, bdf, offset);
266
267 value = readl(va_address);
268
269 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
270 *valuep = pci_conv_32_to_size(value, offset, size);
271
Marcin Wojtas49e33bd2021-04-30 15:33:15 +0200272 if (pcie->region_count > 1)
273 pcie_dw_prog_outbound_atu(pcie, PCIE_ATU_REGION_INDEX0,
274 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
275 pcie->io.bus_start, pcie->io.size);
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200276
Shadi Ammouri3b386452016-10-27 13:29:41 +0200277 return 0;
278}
279
280/**
281 * pcie_dw_mvebu_write_config() - Write to configuration space
282 *
283 * @bus: Pointer to the PCI bus
284 * @bdf: Identifies the PCIe device to access
285 * @offset: The offset into the device's configuration space
286 * @value: The value to write
287 * @size: Indicates the size of access to perform
288 *
289 * Write the value @value of size @size from offset @offset within the
290 * configuration space of the device identified by the bus, device & function
291 * numbers in @bdf on the PCI bus @bus.
292 *
293 * Return: 0 on success
294 */
295static int pcie_dw_mvebu_write_config(struct udevice *bus, pci_dev_t bdf,
296 uint offset, ulong value,
297 enum pci_size_t size)
298{
299 struct pcie_dw_mvebu *pcie = dev_get_priv(bus);
300 uintptr_t va_address;
301 ulong old;
302
303 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
304 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
305 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
306
307 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
308 debug("- out of range\n");
309 return 0;
310 }
311
312 va_address = set_cfg_address(pcie, bdf, offset);
313
314 old = readl(va_address);
315 value = pci_conv_size_to_32(old, value, offset, size);
316 writel(value, va_address);
317
Marcin Wojtas49e33bd2021-04-30 15:33:15 +0200318 if (pcie->region_count > 1)
319 pcie_dw_prog_outbound_atu(pcie, PCIE_ATU_REGION_INDEX0,
320 PCIE_ATU_TYPE_IO, pcie->io.phys_start,
321 pcie->io.bus_start, pcie->io.size);
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200322
Shadi Ammouri3b386452016-10-27 13:29:41 +0200323 return 0;
324}
325
326/**
327 * pcie_dw_configure() - Configure link capabilities and speed
328 *
329 * @regs_base: A pointer to the PCIe controller registers
330 * @cap_speed: The capabilities and speed to configure
331 *
332 * Configure the link capabilities and speed in the PCIe root complex.
333 */
334static void pcie_dw_configure(const void *regs_base, u32 cap_speed)
335{
336 /*
337 * TODO (shadi@marvell.com, sr@denx.de):
338 * Need to read the serdes speed from the dts and according to it
339 * configure the PCIe gen
340 */
341
342 /* Set link to GEN 3 */
343 clrsetbits_le32(regs_base + PCIE_LINK_CTL_2,
344 TARGET_LINK_SPEED_MASK, cap_speed);
345 clrsetbits_le32(regs_base + PCIE_LINK_CAPABILITY,
346 TARGET_LINK_SPEED_MASK, cap_speed);
347 setbits_le32(regs_base + PCIE_GEN3_EQU_CTRL, GEN3_EQU_EVAL_2MS_DISABLE);
348}
349
350/**
351 * is_link_up() - Return the link state
352 *
353 * @regs_base: A pointer to the PCIe controller registers
354 *
355 * Return: 1 (true) for active line and 0 (false) for no link
356 */
357static int is_link_up(const void *regs_base)
358{
359 u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
360 u32 reg;
361
362 reg = readl(regs_base + PCIE_GLOBAL_STATUS);
363 if ((reg & mask) == mask)
364 return 1;
365
366 return 0;
367}
368
369/**
370 * wait_link_up() - Wait for the link to come up
371 *
372 * @regs_base: A pointer to the PCIe controller registers
373 *
374 * Return: 1 (true) for active line and 0 (false) for no link (timeout)
375 */
376static int wait_link_up(const void *regs_base)
377{
378 unsigned long timeout;
379
380 timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
381 while (!is_link_up(regs_base)) {
382 if (get_timer(0) > timeout)
383 return 0;
384 };
385
386 return 1;
387}
388
389/**
390 * pcie_dw_mvebu_pcie_link_up() - Configure the PCIe root port
391 *
392 * @regs_base: A pointer to the PCIe controller registers
393 * @cap_speed: The capabilities and speed to configure
394 *
395 * Configure the PCIe controller root complex depending on the
396 * requested link capabilities and speed.
397 *
398 * Return: 1 (true) for active line and 0 (false) for no link
399 */
400static int pcie_dw_mvebu_pcie_link_up(const void *regs_base, u32 cap_speed)
401{
402 if (!is_link_up(regs_base)) {
403 /* Disable LTSSM state machine to enable configuration */
404 clrbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
405 PCIE_APP_LTSSM_EN);
406 }
407
408 clrsetbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
409 PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_OFFSET,
410 PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_OFFSET);
411
412 /* Set the PCIe master AXI attributes */
413 writel(ARCACHE_SHAREABLE_CACHEABLE, regs_base + PCIE_ARCACHE_TRC);
414 writel(AWCACHE_SHAREABLE_CACHEABLE, regs_base + PCIE_AWCACHE_TRC);
415
416 /* DW pre link configurations */
417 pcie_dw_configure(regs_base, cap_speed);
418
419 if (!is_link_up(regs_base)) {
420 /* Configuration done. Start LTSSM */
421 setbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
422 PCIE_APP_LTSSM_EN);
423 }
424
425 /* Check that link was established */
426 if (!wait_link_up(regs_base))
427 return 0;
428
429 /*
430 * Link can be established in Gen 1. still need to wait
431 * till MAC nagaotiation is completed
432 */
433 udelay(100);
434
435 return 1;
436}
437
438/**
Shadi Ammouri3b386452016-10-27 13:29:41 +0200439 * pcie_dw_set_host_bars() - Configure the host BARs
440 *
441 * @regs_base: A pointer to the PCIe controller registers
442 *
443 * Configure the host BARs of the PCIe controller root port so that
444 * PCI(e) devices may access the system memory.
445 */
446static void pcie_dw_set_host_bars(const void *regs_base)
447{
448 u32 size = gd->ram_size;
449 u64 max_size;
450 u32 reg;
451 u32 bar0;
452
453 /* Verify the maximal BAR size */
454 reg = readl(regs_base + RESIZABLE_BAR_CAP);
455 max_size = 1ULL << (5 + (reg + (1 << 4)));
456
457 if (size > max_size) {
458 size = max_size;
459 printf("Warning: PCIe BARs can't map all DRAM space\n");
460 }
461
462 /* Set the BAR base and size towards DDR */
Tom Rinibb4dd962022-11-16 13:10:37 -0500463 bar0 = CFG_SYS_SDRAM_BASE & ~0xf;
Shadi Ammouri3b386452016-10-27 13:29:41 +0200464 bar0 |= PCI_BASE_ADDRESS_MEM_TYPE_32;
Tom Rinibb4dd962022-11-16 13:10:37 -0500465 writel(CFG_SYS_SDRAM_BASE, regs_base + PCIE_CONFIG_BAR0);
Shadi Ammouri3b386452016-10-27 13:29:41 +0200466
467 reg = ((size >> 20) - 1) << 12;
468 writel(size, regs_base + RESIZABLE_BAR_CTL0);
469}
470
471/**
472 * pcie_dw_mvebu_probe() - Probe the PCIe bus for active link
473 *
474 * @dev: A pointer to the device being operated on
475 *
476 * Probe for an active link on the PCIe bus and configure the controller
477 * to enable this port.
478 *
479 * Return: 0 on success, else -ENODEV
480 */
481static int pcie_dw_mvebu_probe(struct udevice *dev)
482{
483 struct pcie_dw_mvebu *pcie = dev_get_priv(dev);
484 struct udevice *ctlr = pci_get_controller(dev);
485 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
Simon Glassfa4689a2019-12-06 21:41:35 -0700486#if CONFIG_IS_ENABLED(DM_GPIO)
Konstantin Porotchkin4eb09cd2017-02-08 17:34:13 +0200487 struct gpio_desc reset_gpio;
488
489 gpio_request_by_name(dev, "marvell,reset-gpio", 0, &reset_gpio,
490 GPIOD_IS_OUT);
491 /*
492 * Issue reset to add-in card trough the dedicated GPIO.
493 * Some boards are connecting the card reset pin to common system
494 * reset wire and others are using separate GPIO port.
495 * In the last case we have to release a reset of the addon card
496 * using this GPIO.
497 */
498 if (dm_gpio_is_valid(&reset_gpio)) {
Baruch Siach6e599c62019-02-03 15:15:39 +0200499 dm_gpio_set_value(&reset_gpio, 1); /* assert */
500 mdelay(200);
501 dm_gpio_set_value(&reset_gpio, 0); /* de-assert */
Konstantin Porotchkin4eb09cd2017-02-08 17:34:13 +0200502 mdelay(200);
503 }
504#else
505 debug("PCIE Reset on GPIO support is missing\n");
Simon Glassfa4689a2019-12-06 21:41:35 -0700506#endif /* DM_GPIO */
Shadi Ammouri3b386452016-10-27 13:29:41 +0200507
Simon Glass75e534b2020-12-16 21:20:07 -0700508 pcie->first_busno = dev_seq(dev);
Shadi Ammouri3b386452016-10-27 13:29:41 +0200509
510 /* Don't register host if link is down */
511 if (!pcie_dw_mvebu_pcie_link_up(pcie->ctrl_base, LINK_SPEED_GEN_3)) {
Simon Glass75e534b2020-12-16 21:20:07 -0700512 printf("PCIE-%d: Link down\n", dev_seq(dev));
Konstantin Porotchkind4739d32017-03-28 18:36:34 +0300513 } else {
Simon Glass75e534b2020-12-16 21:20:07 -0700514 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev_seq(dev),
Konstantin Porotchkind4739d32017-03-28 18:36:34 +0300515 pcie_dw_get_link_speed(pcie->ctrl_base),
516 pcie_dw_get_link_width(pcie->ctrl_base),
517 hose->first_busno);
Shadi Ammouri3b386452016-10-27 13:29:41 +0200518 }
519
Marcin Wojtas49e33bd2021-04-30 15:33:15 +0200520 pcie->region_count = hose->region_count - CONFIG_NR_DRAM_BANKS;
521
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200522 /* Store the IO and MEM windows settings for future use by the ATU */
Marcin Wojtas49e33bd2021-04-30 15:33:15 +0200523 if (pcie->region_count > 1) {
524 /* IO base */
525 pcie->io.phys_start = hose->regions[0].phys_start;
526 /* IO_bus_addr */
527 pcie->io.bus_start = hose->regions[0].bus_start;
528 /* IO size */
529 pcie->io.size = hose->regions[0].size;
530 }
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200531
Marcin Wojtas49e33bd2021-04-30 15:33:15 +0200532 /* MEM base */
533 pcie->mem.phys_start = hose->regions[pcie->region_count - 1].phys_start;
534 /* MEM_bus_addr */
535 pcie->mem.bus_start = hose->regions[pcie->region_count - 1].bus_start;
536 /* MEM size */
537 pcie->mem.size = hose->regions[pcie->region_count - 1].size;
Igal Libermanf09ac1a2018-02-14 19:25:23 +0200538
539 pcie_dw_prog_outbound_atu(pcie, PCIE_ATU_REGION_INDEX1,
540 PCIE_ATU_TYPE_MEM, pcie->mem.phys_start,
541 pcie->mem.bus_start, pcie->mem.size);
Shadi Ammouri3b386452016-10-27 13:29:41 +0200542
Pali Rohár25781e22022-02-18 13:18:40 +0100543 /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */
Shadi Ammouri3b386452016-10-27 13:29:41 +0200544 clrsetbits_le32(pcie->ctrl_base + PCI_CLASS_REVISION,
Pali Rohár25781e22022-02-18 13:18:40 +0100545 0xffffff << 8, PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
Shadi Ammouri3b386452016-10-27 13:29:41 +0200546
547 pcie_dw_set_host_bars(pcie->ctrl_base);
548
549 return 0;
550}
551
552/**
Simon Glassaad29ae2020-12-03 16:55:21 -0700553 * pcie_dw_mvebu_of_to_plat() - Translate from DT to device state
Shadi Ammouri3b386452016-10-27 13:29:41 +0200554 *
555 * @dev: A pointer to the device being operated on
556 *
557 * Translate relevant data from the device tree pertaining to device @dev into
558 * state that the driver will later make use of. This state is stored in the
559 * device's private data structure.
560 *
561 * Return: 0 on success, else -EINVAL
562 */
Simon Glassaad29ae2020-12-03 16:55:21 -0700563static int pcie_dw_mvebu_of_to_plat(struct udevice *dev)
Shadi Ammouri3b386452016-10-27 13:29:41 +0200564{
565 struct pcie_dw_mvebu *pcie = dev_get_priv(dev);
566
567 /* Get the controller base address */
Johan Jonkerb52189e2023-03-13 01:32:31 +0100568 pcie->ctrl_base = devfdt_get_addr_index_ptr(dev, 0);
569 if (!pcie->ctrl_base)
Shadi Ammouri3b386452016-10-27 13:29:41 +0200570 return -EINVAL;
571
572 /* Get the config space base address and size */
Johan Jonker40702782023-03-13 01:32:18 +0100573 pcie->cfg_base = devfdt_get_addr_size_index_ptr(dev, 1,
574 &pcie->cfg_size);
575 if (!pcie->cfg_base)
Shadi Ammouri3b386452016-10-27 13:29:41 +0200576 return -EINVAL;
577
578 return 0;
579}
580
581static const struct dm_pci_ops pcie_dw_mvebu_ops = {
582 .read_config = pcie_dw_mvebu_read_config,
583 .write_config = pcie_dw_mvebu_write_config,
584};
585
586static const struct udevice_id pcie_dw_mvebu_ids[] = {
587 { .compatible = "marvell,armada8k-pcie" },
588 { }
589};
590
591U_BOOT_DRIVER(pcie_dw_mvebu) = {
592 .name = "pcie_dw_mvebu",
593 .id = UCLASS_PCI,
594 .of_match = pcie_dw_mvebu_ids,
595 .ops = &pcie_dw_mvebu_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700596 .of_to_plat = pcie_dw_mvebu_of_to_plat,
Shadi Ammouri3b386452016-10-27 13:29:41 +0200597 .probe = pcie_dw_mvebu_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700598 .priv_auto = sizeof(struct pcie_dw_mvebu),
Shadi Ammouri3b386452016-10-27 13:29:41 +0200599};