1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Marvell International Ltd.
5 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
8 * - drivers/pci/pcie_imx.c
9 * - drivers/pci/pci_mvebu.c
10 * - drivers/pci/pcie_xilinx.c
18 #include <asm/global_data.h>
20 #include <asm-generic/gpio.h>
21 #include <linux/delay.h>
23 DECLARE_GLOBAL_DATA_PTR;
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)
33 /* Resizable bar capability registers */
34 #define RESIZABLE_BAR_CAP 0x250
35 #define RESIZABLE_BAR_CTL0 0x254
36 #define RESIZABLE_BAR_CTL1 0x258
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
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
68 #define PCIE_GEN3_RELATED 0x890
69 #define GEN3_EQU_DISABLE (1 << 16)
70 #define GEN3_ZRXDC_NON_COMP (1 << 0)
72 #define PCIE_GEN3_EQU_CTRL 0x8A8
73 #define GEN3_EQU_EVAL_2MS_DISABLE (1 << 5)
75 #define PCIE_ROOT_COMPLEX_MODE_MASK (0xF << 4)
77 #define PCIE_LINK_UP_TIMEOUT_MS 100
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 */
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)
91 #define PCIE_ARCACHE_TRC 0x8050
92 #define PCIE_AWCACHE_TRC 0x8054
93 #define ARCACHE_SHAREABLE_CACHEABLE 0x3511
94 #define AWCACHE_SHAREABLE_CACHEABLE 0x5311
96 #define LINK_SPEED_GEN_1 0x1
97 #define LINK_SPEED_GEN_2 0x2
98 #define LINK_SPEED_GEN_3 0x3
101 * struct pcie_dw_mvebu - MVEBU DW PCIe controller state
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).
112 struct pcie_dw_mvebu {
118 /* IO and MEM PCI regions */
120 struct pci_region io;
121 struct pci_region mem;
124 static int pcie_dw_get_link_speed(const void *regs_base)
126 return (readl(regs_base + PCIE_LINK_STATUS_REG) &
127 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
130 static int pcie_dw_get_link_width(const void *regs_base)
132 return (readl(regs_base + PCIE_LINK_STATUS_REG) &
133 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
137 * pcie_dw_prog_outbound_atu() - Configure ATU for outbound accesses
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
146 static void pcie_dw_prog_outbound_atu(struct pcie_dw_mvebu *pcie, int index,
147 int type, u64 cpu_addr, u64 pci_addr,
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);
165 * set_cfg_address() - Configure the PCIe controller config space access
167 * @pcie: Pointer to the PCI controller state
168 * @d: PCI device to access
169 * @where: Offset in the configuration space
171 * Configures the PCIe controller to access the configuration space of
172 * a specific PCIe device and returns the address to use for this
175 * Return: Address that can be used to access the configation space
176 * of the requested device / offset
178 static uintptr_t set_cfg_address(struct pcie_dw_mvebu *pcie,
179 pci_dev_t d, uint where)
181 uintptr_t va_address;
185 * Region #0 is used for Outbound CFG space access.
186 * Direction = Outbound
190 if (PCI_BUS(d) == (pcie->first_busno + 1))
191 /* For local bus, change TLP Type field to 4. */
192 atu_type = PCIE_ATU_TYPE_CFG0;
194 /* Otherwise, change TLP Type field to 5. */
195 atu_type = PCIE_ATU_TYPE_CFG1;
197 if (PCI_BUS(d) == pcie->first_busno) {
198 /* Accessing root port configuration space. */
199 va_address = (uintptr_t)pcie->ctrl_base;
201 d = PCI_MASK_BUS(d) | (PCI_BUS(d) - pcie->first_busno);
202 pcie_dw_prog_outbound_atu(pcie, PCIE_ATU_REGION_INDEX0,
203 atu_type, (u64)pcie->cfg_base,
204 d << 8, pcie->cfg_size);
205 va_address = (uintptr_t)pcie->cfg_base;
208 va_address += where & ~0x3;
214 * pcie_dw_addr_valid() - Check for valid bus address
216 * @d: The PCI device to access
217 * @first_busno: Bus number of the PCIe controller root complex
219 * Return 1 (true) if the PCI device can be accessed by this controller.
221 * Return: 1 on valid, 0 on invalid
223 static int pcie_dw_addr_valid(pci_dev_t d, int first_busno)
225 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
227 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
234 * pcie_dw_mvebu_read_config() - Read from configuration space
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
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.
246 * Return: 0 on success
248 static int pcie_dw_mvebu_read_config(const struct udevice *bus, pci_dev_t bdf,
249 uint offset, ulong *valuep,
250 enum pci_size_t size)
252 struct pcie_dw_mvebu *pcie = dev_get_priv(bus);
253 uintptr_t va_address;
256 debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
257 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
259 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
260 debug("- out of range\n");
261 *valuep = pci_get_ff(size);
265 va_address = set_cfg_address(pcie, bdf, offset);
267 value = readl(va_address);
269 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
270 *valuep = pci_conv_32_to_size(value, offset, size);
272 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);
281 * pcie_dw_mvebu_write_config() - Write to configuration space
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
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.
293 * Return: 0 on success
295 static int pcie_dw_mvebu_write_config(struct udevice *bus, pci_dev_t bdf,
296 uint offset, ulong value,
297 enum pci_size_t size)
299 struct pcie_dw_mvebu *pcie = dev_get_priv(bus);
300 uintptr_t va_address;
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);
307 if (!pcie_dw_addr_valid(bdf, pcie->first_busno)) {
308 debug("- out of range\n");
312 va_address = set_cfg_address(pcie, bdf, offset);
314 old = readl(va_address);
315 value = pci_conv_size_to_32(old, value, offset, size);
316 writel(value, va_address);
318 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);
327 * pcie_dw_configure() - Configure link capabilities and speed
329 * @regs_base: A pointer to the PCIe controller registers
330 * @cap_speed: The capabilities and speed to configure
332 * Configure the link capabilities and speed in the PCIe root complex.
334 static void pcie_dw_configure(const void *regs_base, u32 cap_speed)
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
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);
351 * is_link_up() - Return the link state
353 * @regs_base: A pointer to the PCIe controller registers
355 * Return: 1 (true) for active line and 0 (false) for no link
357 static int is_link_up(const void *regs_base)
359 u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
362 reg = readl(regs_base + PCIE_GLOBAL_STATUS);
363 if ((reg & mask) == mask)
370 * wait_link_up() - Wait for the link to come up
372 * @regs_base: A pointer to the PCIe controller registers
374 * Return: 1 (true) for active line and 0 (false) for no link (timeout)
376 static int wait_link_up(const void *regs_base)
378 unsigned long timeout;
380 timeout = get_timer(0) + PCIE_LINK_UP_TIMEOUT_MS;
381 while (!is_link_up(regs_base)) {
382 if (get_timer(0) > timeout)
390 * pcie_dw_mvebu_pcie_link_up() - Configure the PCIe root port
392 * @regs_base: A pointer to the PCIe controller registers
393 * @cap_speed: The capabilities and speed to configure
395 * Configure the PCIe controller root complex depending on the
396 * requested link capabilities and speed.
398 * Return: 1 (true) for active line and 0 (false) for no link
400 static int pcie_dw_mvebu_pcie_link_up(const void *regs_base, u32 cap_speed)
402 if (!is_link_up(regs_base)) {
403 /* Disable LTSSM state machine to enable configuration */
404 clrbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
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);
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);
416 /* DW pre link configurations */
417 pcie_dw_configure(regs_base, cap_speed);
419 if (!is_link_up(regs_base)) {
420 /* Configuration done. Start LTSSM */
421 setbits_le32(regs_base + PCIE_GLOBAL_CONTROL,
425 /* Check that link was established */
426 if (!wait_link_up(regs_base))
430 * Link can be established in Gen 1. still need to wait
431 * till MAC nagaotiation is completed
439 * pcie_dw_set_host_bars() - Configure the host BARs
441 * @regs_base: A pointer to the PCIe controller registers
443 * Configure the host BARs of the PCIe controller root port so that
444 * PCI(e) devices may access the system memory.
446 static void pcie_dw_set_host_bars(const void *regs_base)
448 u32 size = gd->ram_size;
453 /* Verify the maximal BAR size */
454 reg = readl(regs_base + RESIZABLE_BAR_CAP);
455 max_size = 1ULL << (5 + (reg + (1 << 4)));
457 if (size > max_size) {
459 printf("Warning: PCIe BARs can't map all DRAM space\n");
462 /* Set the BAR base and size towards DDR */
463 bar0 = CFG_SYS_SDRAM_BASE & ~0xf;
464 bar0 |= PCI_BASE_ADDRESS_MEM_TYPE_32;
465 writel(CFG_SYS_SDRAM_BASE, regs_base + PCIE_CONFIG_BAR0);
467 reg = ((size >> 20) - 1) << 12;
468 writel(size, regs_base + RESIZABLE_BAR_CTL0);
472 * pcie_dw_mvebu_probe() - Probe the PCIe bus for active link
474 * @dev: A pointer to the device being operated on
476 * Probe for an active link on the PCIe bus and configure the controller
477 * to enable this port.
479 * Return: 0 on success, else -ENODEV
481 static int pcie_dw_mvebu_probe(struct udevice *dev)
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);
486 #if CONFIG_IS_ENABLED(DM_GPIO)
487 struct gpio_desc reset_gpio;
489 gpio_request_by_name(dev, "marvell,reset-gpio", 0, &reset_gpio,
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
498 if (dm_gpio_is_valid(&reset_gpio)) {
499 dm_gpio_set_value(&reset_gpio, 1); /* assert */
501 dm_gpio_set_value(&reset_gpio, 0); /* de-assert */
505 debug("PCIE Reset on GPIO support is missing\n");
508 pcie->first_busno = dev_seq(dev);
510 /* Don't register host if link is down */
511 if (!pcie_dw_mvebu_pcie_link_up(pcie->ctrl_base, LINK_SPEED_GEN_3)) {
512 printf("PCIE-%d: Link down\n", dev_seq(dev));
514 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", dev_seq(dev),
515 pcie_dw_get_link_speed(pcie->ctrl_base),
516 pcie_dw_get_link_width(pcie->ctrl_base),
520 pcie->region_count = hose->region_count - CONFIG_NR_DRAM_BANKS;
522 /* Store the IO and MEM windows settings for future use by the ATU */
523 if (pcie->region_count > 1) {
525 pcie->io.phys_start = hose->regions[0].phys_start;
527 pcie->io.bus_start = hose->regions[0].bus_start;
529 pcie->io.size = hose->regions[0].size;
533 pcie->mem.phys_start = hose->regions[pcie->region_count - 1].phys_start;
535 pcie->mem.bus_start = hose->regions[pcie->region_count - 1].bus_start;
537 pcie->mem.size = hose->regions[pcie->region_count - 1].size;
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);
543 /* Set the CLASS_REV of RC CFG header to PCI_CLASS_BRIDGE_PCI_NORMAL */
544 clrsetbits_le32(pcie->ctrl_base + PCI_CLASS_REVISION,
545 0xffffff << 8, PCI_CLASS_BRIDGE_PCI_NORMAL << 8);
547 pcie_dw_set_host_bars(pcie->ctrl_base);
553 * pcie_dw_mvebu_of_to_plat() - Translate from DT to device state
555 * @dev: A pointer to the device being operated on
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.
561 * Return: 0 on success, else -EINVAL
563 static int pcie_dw_mvebu_of_to_plat(struct udevice *dev)
565 struct pcie_dw_mvebu *pcie = dev_get_priv(dev);
567 /* Get the controller base address */
568 pcie->ctrl_base = devfdt_get_addr_index_ptr(dev, 0);
569 if (!pcie->ctrl_base)
572 /* Get the config space base address and size */
573 pcie->cfg_base = devfdt_get_addr_size_index_ptr(dev, 1,
581 static 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,
586 static const struct udevice_id pcie_dw_mvebu_ids[] = {
587 { .compatible = "marvell,armada8k-pcie" },
591 U_BOOT_DRIVER(pcie_dw_mvebu) = {
592 .name = "pcie_dw_mvebu",
594 .of_match = pcie_dw_mvebu_ids,
595 .ops = &pcie_dw_mvebu_ops,
596 .of_to_plat = pcie_dw_mvebu_of_to_plat,
597 .probe = pcie_dw_mvebu_probe,
598 .priv_auto = sizeof(struct pcie_dw_mvebu),