1 // SPDX-License-Identifier: GPL-2.0+
3 * Rockchip DesignWare based PCIe host controller driver
5 * Copyright (c) 2021 Rockchip, Inc.
11 #include <generic-phy.h>
13 #include <power-domain.h>
16 #include <asm/arch-rockchip/clock.h>
18 #include <asm-generic/gpio.h>
19 #include <dm/device_compat.h>
20 #include <linux/iopoll.h>
21 #include <linux/delay.h>
22 #include <power/regulator.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 * struct rk_pcie - RK DW PCIe controller state
29 * @vpcie3v3: The 3.3v power supply for slot
30 * @dbi_base: The base address of dwc core regs
31 * @apb_base: The base address of vendor regs
32 * @cfg_base: The base address of config header space
33 * @cfg_size: The size of the configuration space which is needed
34 * as it gets written into the PCIE_ATU_LIMIT register
35 * @first_busno: This driver supports multiple PCIe controllers.
36 * first_busno stores the bus number of the PCIe root-port
37 * number which may vary depending on the PCIe setup
39 * @rst_gpio: The #PERST signal for slot
40 * @io: The IO space for EP's BAR
41 * @mem: The memory space for EP's BAR
45 struct udevice *vpcie3v3;
53 struct reset_ctl_bulk rsts;
54 struct gpio_desc rst_gpio;
56 struct pci_region mem;
59 /* Parameters for the waiting for iATU enabled routine */
60 #define PCIE_CLIENT_GENERAL_DEBUG 0x104
61 #define PCIE_CLIENT_HOT_RESET_CTRL 0x180
62 #define PCIE_LTSSM_ENABLE_ENHANCE BIT(4)
63 #define PCIE_CLIENT_LTSSM_STATUS 0x300
64 #define SMLH_LINKUP BIT(16)
65 #define RDLH_LINKUP BIT(17)
66 #define PCIE_CLIENT_DBG_FIFO_MODE_CON 0x310
67 #define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0 0x320
68 #define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1 0x324
69 #define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0 0x328
70 #define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1 0x32c
71 #define PCIE_CLIENT_DBG_FIFO_STATUS 0x350
72 #define PCIE_CLIENT_DBG_TRANSITION_DATA 0xffff0000
73 #define PCIE_CLIENT_DBF_EN 0xffff0003
75 /* PCI DBICS registers */
76 #define PCIE_LINK_STATUS_REG 0x80
77 #define PCIE_LINK_STATUS_SPEED_OFF 16
78 #define PCIE_LINK_STATUS_SPEED_MASK (0xf << PCIE_LINK_STATUS_SPEED_OFF)
79 #define PCIE_LINK_STATUS_WIDTH_OFF 20
80 #define PCIE_LINK_STATUS_WIDTH_MASK (0xf << PCIE_LINK_STATUS_WIDTH_OFF)
82 #define PCIE_LINK_CAPABILITY 0x7c
83 #define PCIE_LINK_CTL_2 0xa0
84 #define TARGET_LINK_SPEED_MASK 0xf
85 #define LINK_SPEED_GEN_1 0x1
86 #define LINK_SPEED_GEN_2 0x2
87 #define LINK_SPEED_GEN_3 0x3
89 #define PCIE_MISC_CONTROL_1_OFF 0x8bc
90 #define PCIE_DBI_RO_WR_EN BIT(0)
92 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80c
93 #define PORT_LOGIC_SPEED_CHANGE BIT(17)
96 * iATU Unroll-specific register definitions
97 * From 4.80 core version the address translation will be made by unroll.
98 * The registers are offset from atu_base
100 #define PCIE_ATU_UNR_REGION_CTRL1 0x00
101 #define PCIE_ATU_UNR_REGION_CTRL2 0x04
102 #define PCIE_ATU_UNR_LOWER_BASE 0x08
103 #define PCIE_ATU_UNR_UPPER_BASE 0x0c
104 #define PCIE_ATU_UNR_LIMIT 0x10
105 #define PCIE_ATU_UNR_LOWER_TARGET 0x14
106 #define PCIE_ATU_UNR_UPPER_TARGET 0x18
108 #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
109 #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
110 #define PCIE_ATU_TYPE_MEM (0x0 << 0)
111 #define PCIE_ATU_TYPE_IO (0x2 << 0)
112 #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
113 #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
114 #define PCIE_ATU_ENABLE (0x1 << 31)
115 #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
116 #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
117 #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
118 #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
120 /* Register address builder */
121 #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) \
122 ((0x3 << 20) | ((region) << 9))
124 /* Parameters for the waiting for iATU enabled routine */
125 #define LINK_WAIT_MAX_IATU_RETRIES 5
126 #define LINK_WAIT_IATU_US 10000
128 /* Parameters for the waiting for #perst signal */
129 #define PERST_WAIT_MS 1000
131 static int rk_pcie_read(void __iomem *addr, int size, u32 *val)
133 if ((uintptr_t)addr & (size - 1)) {
135 return PCIBIOS_UNSUPPORTED;
140 } else if (size == 2) {
142 } else if (size == 1) {
152 static int rk_pcie_write(void __iomem *addr, int size, u32 val)
154 if ((uintptr_t)addr & (size - 1))
155 return PCIBIOS_UNSUPPORTED;
169 static u32 __rk_pcie_read_apb(struct rk_pcie *rk_pcie, void __iomem *base,
170 u32 reg, size_t size)
175 ret = rk_pcie_read(base + reg, size, &val);
177 dev_err(rk_pcie->dev, "Read APB address failed\n");
182 static void __rk_pcie_write_apb(struct rk_pcie *rk_pcie, void __iomem *base,
183 u32 reg, size_t size, u32 val)
187 ret = rk_pcie_write(base + reg, size, val);
189 dev_err(rk_pcie->dev, "Write APB address failed\n");
193 * rk_pcie_readl_apb() - Read vendor regs
195 * @rk_pcie: Pointer to the PCI controller state
196 * @reg: Offset of regs
198 static inline u32 rk_pcie_readl_apb(struct rk_pcie *rk_pcie, u32 reg)
200 return __rk_pcie_read_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4);
204 * rk_pcie_writel_apb() - Write vendor regs
206 * @rk_pcie: Pointer to the PCI controller state
207 * @reg: Offset of regs
208 * @val: Value to be writen
210 static inline void rk_pcie_writel_apb(struct rk_pcie *rk_pcie, u32 reg,
213 __rk_pcie_write_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4, val);
216 static int rk_pcie_get_link_speed(struct rk_pcie *rk_pcie)
218 return (readl(rk_pcie->dbi_base + PCIE_LINK_STATUS_REG) &
219 PCIE_LINK_STATUS_SPEED_MASK) >> PCIE_LINK_STATUS_SPEED_OFF;
222 static int rk_pcie_get_link_width(struct rk_pcie *rk_pcie)
224 return (readl(rk_pcie->dbi_base + PCIE_LINK_STATUS_REG) &
225 PCIE_LINK_STATUS_WIDTH_MASK) >> PCIE_LINK_STATUS_WIDTH_OFF;
228 static void rk_pcie_writel_ob_unroll(struct rk_pcie *rk_pcie, u32 index,
231 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
232 void __iomem *base = rk_pcie->dbi_base;
234 writel(val, base + offset + reg);
237 static u32 rk_pcie_readl_ob_unroll(struct rk_pcie *rk_pcie, u32 index, u32 reg)
239 u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
240 void __iomem *base = rk_pcie->dbi_base;
242 return readl(base + offset + reg);
245 static inline void rk_pcie_dbi_write_enable(struct rk_pcie *rk_pcie, bool en)
249 val = readl(rk_pcie->dbi_base + PCIE_MISC_CONTROL_1_OFF);
252 val |= PCIE_DBI_RO_WR_EN;
254 val &= ~PCIE_DBI_RO_WR_EN;
255 writel(val, rk_pcie->dbi_base + PCIE_MISC_CONTROL_1_OFF);
259 * rockchip_pcie_setup_host() - Setup the PCIe controller for RC opertaion
261 * @rk_pcie: Pointer to the PCI controller state
263 * Configure the host BARs of the PCIe controller root port so that
264 * PCI(e) devices may access the system memory.
266 static void rk_pcie_setup_host(struct rk_pcie *rk_pcie)
270 rk_pcie_dbi_write_enable(rk_pcie, true);
273 writel(PCI_BASE_ADDRESS_MEM_TYPE_64,
274 rk_pcie->dbi_base + PCI_BASE_ADDRESS_0);
275 writel(0x0, rk_pcie->dbi_base + PCI_BASE_ADDRESS_1);
277 /* setup interrupt pins */
278 clrsetbits_le32(rk_pcie->dbi_base + PCI_INTERRUPT_LINE,
281 /* setup bus numbers */
282 clrsetbits_le32(rk_pcie->dbi_base + PCI_PRIMARY_BUS,
283 0xffffff, 0x00ff0100);
285 /* setup command register */
286 clrsetbits_le32(rk_pcie->dbi_base + PCI_PRIMARY_BUS,
288 PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
289 PCI_COMMAND_MASTER | PCI_COMMAND_SERR);
291 /* program correct class for RC */
292 writew(PCI_CLASS_BRIDGE_PCI, rk_pcie->dbi_base + PCI_CLASS_DEVICE);
293 /* Better disable write permission right after the update */
295 setbits_le32(rk_pcie->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL,
296 PORT_LOGIC_SPEED_CHANGE)
298 rk_pcie_dbi_write_enable(rk_pcie, false);
302 * rk_pcie_configure() - Configure link capabilities and speed
304 * @rk_pcie: Pointer to the PCI controller state
305 * @cap_speed: The capabilities and speed to configure
307 * Configure the link capabilities and speed in the PCIe root complex.
309 static void rk_pcie_configure(struct rk_pcie *pci, u32 cap_speed)
313 rk_pcie_dbi_write_enable(pci, true);
315 clrsetbits_le32(pci->dbi_base + PCIE_LINK_CAPABILITY,
316 TARGET_LINK_SPEED_MASK, cap_speed);
318 clrsetbits_le32(pci->dbi_base + PCIE_LINK_CTL_2,
319 TARGET_LINK_SPEED_MASK, cap_speed);
321 rk_pcie_dbi_write_enable(pci, false);
325 * rk_pcie_dw_prog_outbound_atu_unroll() - Configure ATU for outbound accesses
327 * @rk_pcie: Pointer to the PCI controller state
328 * @index: ATU region index
329 * @type: ATU accsess type
330 * @cpu_addr: the physical address for the translation entry
331 * @pci_addr: the pcie bus address for the translation entry
332 * @size: the size of the translation entry
334 * Return: 0 is successful and -1 is failure
336 static int rk_pcie_prog_outbound_atu_unroll(struct rk_pcie *pci, int index,
337 int type, u64 cpu_addr,
338 u64 pci_addr, u32 size)
342 dev_dbg(pci->dev, "ATU programmed with: index: %d, type: %d, cpu addr: %8llx, pci addr: %8llx, size: %8x\n",
343 index, type, cpu_addr, pci_addr, size);
345 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_BASE,
346 lower_32_bits(cpu_addr));
347 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_BASE,
348 upper_32_bits(cpu_addr));
349 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LIMIT,
350 lower_32_bits(cpu_addr + size - 1));
351 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_LOWER_TARGET,
352 lower_32_bits(pci_addr));
353 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_UPPER_TARGET,
354 upper_32_bits(pci_addr));
355 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL1,
357 rk_pcie_writel_ob_unroll(pci, index, PCIE_ATU_UNR_REGION_CTRL2,
361 * Make sure ATU enable takes effect before any subsequent config
364 for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
365 val = rk_pcie_readl_ob_unroll(pci, index,
366 PCIE_ATU_UNR_REGION_CTRL2);
367 if (val & PCIE_ATU_ENABLE)
370 udelay(LINK_WAIT_IATU_US);
372 dev_err(pci->dev, "outbound iATU is not being enabled\n");
378 * rk_pcie_dw_addr_valid() - Check for valid bus address
380 * @d: The PCI device to access
381 * @first_busno: Bus number of the PCIe controller root complex
383 * Return 1 (true) if the PCI device can be accessed by this controller.
385 * Return: 1 on valid, 0 on invalid
387 static int rk_pcie_addr_valid(pci_dev_t d, int first_busno)
389 if ((PCI_BUS(d) == first_busno) && (PCI_DEV(d) > 0))
391 if ((PCI_BUS(d) == first_busno + 1) && (PCI_DEV(d) > 0))
398 * set_cfg_address() - Configure the PCIe controller config space access
400 * @rk_pcie: Pointer to the PCI controller state
401 * @d: PCI device to access
402 * @where: Offset in the configuration space
404 * Configures the PCIe controller to access the configuration space of
405 * a specific PCIe device and returns the address to use for this
408 * Return: Address that can be used to access the configation space
409 * of the requested device / offset
411 static uintptr_t set_cfg_address(struct rk_pcie *pcie,
412 pci_dev_t d, uint where)
414 int rel_bus = PCI_BUS(d) - pcie->first_busno;
415 uintptr_t va_address;
419 /* Use dbi_base for own configuration read and write */
421 va_address = (uintptr_t)pcie->dbi_base;
427 * For local bus whose primary bus number is root bridge,
428 * change TLP Type field to 4.
430 atu_type = PCIE_ATU_TYPE_CFG0;
432 /* Otherwise, change TLP Type field to 5. */
433 atu_type = PCIE_ATU_TYPE_CFG1;
436 * Not accessing root port configuration space?
437 * Region #0 is used for Outbound CFG space access.
438 * Direction = Outbound
442 d = PCI_ADD_BUS(rel_bus, d);
443 ret = rk_pcie_prog_outbound_atu_unroll(pcie, PCIE_ATU_REGION_INDEX1,
444 atu_type, (u64)pcie->cfg_base,
445 d << 8, pcie->cfg_size);
447 return (uintptr_t)ret;
449 va_address = (uintptr_t)pcie->cfg_base;
452 va_address += where & ~0x3;
458 * rockchip_pcie_rd_conf() - Read from configuration space
460 * @bus: Pointer to the PCI bus
461 * @bdf: Identifies the PCIe device to access
462 * @offset: The offset into the device's configuration space
463 * @valuep: A pointer at which to store the read value
464 * @size: Indicates the size of access to perform
466 * Read a value of size @size from offset @offset within the configuration
467 * space of the device identified by the bus, device & function numbers in @bdf
468 * on the PCI bus @bus.
470 * Return: 0 on success
472 static int rockchip_pcie_rd_conf(const struct udevice *bus, pci_dev_t bdf,
473 uint offset, ulong *valuep,
474 enum pci_size_t size)
476 struct rk_pcie *pcie = dev_get_priv(bus);
477 uintptr_t va_address;
480 debug("PCIE CFG read: bdf=%2x:%2x:%2x\n",
481 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
483 if (!rk_pcie_addr_valid(bdf, pcie->first_busno)) {
484 debug("- out of range\n");
485 *valuep = pci_get_ff(size);
489 va_address = set_cfg_address(pcie, bdf, offset);
491 value = readl(va_address);
493 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
494 *valuep = pci_conv_32_to_size(value, offset, size);
496 return rk_pcie_prog_outbound_atu_unroll(pcie,
497 PCIE_ATU_REGION_INDEX1,
505 * rockchip_pcie_wr_conf() - Write to configuration space
507 * @bus: Pointer to the PCI bus
508 * @bdf: Identifies the PCIe device to access
509 * @offset: The offset into the device's configuration space
510 * @value: The value to write
511 * @size: Indicates the size of access to perform
513 * Write the value @value of size @size from offset @offset within the
514 * configuration space of the device identified by the bus, device & function
515 * numbers in @bdf on the PCI bus @bus.
517 * Return: 0 on success
519 static int rockchip_pcie_wr_conf(struct udevice *bus, pci_dev_t bdf,
520 uint offset, ulong value,
521 enum pci_size_t size)
523 struct rk_pcie *pcie = dev_get_priv(bus);
524 uintptr_t va_address;
527 debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d)\n",
528 PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
529 debug("(addr,val)=(0x%04x, 0x%08lx)\n", offset, value);
531 if (!rk_pcie_addr_valid(bdf, pcie->first_busno)) {
532 debug("- out of range\n");
536 va_address = set_cfg_address(pcie, bdf, offset);
538 old = readl(va_address);
539 value = pci_conv_size_to_32(old, value, offset, size);
540 writel(value, va_address);
542 return rk_pcie_prog_outbound_atu_unroll(pcie,
543 PCIE_ATU_REGION_INDEX1,
551 static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie)
553 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0,
554 PCIE_CLIENT_DBG_TRANSITION_DATA);
555 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1,
556 PCIE_CLIENT_DBG_TRANSITION_DATA);
557 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0,
558 PCIE_CLIENT_DBG_TRANSITION_DATA);
559 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1,
560 PCIE_CLIENT_DBG_TRANSITION_DATA);
561 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON,
565 static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie)
569 debug("ltssm = 0x%x\n",
570 rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS));
571 for (loop = 0; loop < 64; loop++)
572 debug("fifo_status = 0x%x\n",
573 rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_STATUS));
576 static inline void rk_pcie_link_status_clear(struct rk_pcie *rk_pcie)
578 rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG, 0x0);
581 static inline void rk_pcie_disable_ltssm(struct rk_pcie *rk_pcie)
583 rk_pcie_writel_apb(rk_pcie, 0x0, 0xc0008);
586 static inline void rk_pcie_enable_ltssm(struct rk_pcie *rk_pcie)
588 rk_pcie_writel_apb(rk_pcie, 0x0, 0xc000c);
591 static int is_link_up(struct rk_pcie *priv)
595 val = rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS);
596 if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 &&
597 (val & GENMASK(5, 0)) == 0x11)
604 * rk_pcie_link_up() - Wait for the link to come up
606 * @rk_pcie: Pointer to the PCI controller state
607 * @cap_speed: Desired link speed
609 * Return: 1 (true) for active line and negetive (false) for no link (timeout)
611 static int rk_pcie_link_up(struct rk_pcie *priv, u32 cap_speed)
615 if (is_link_up(priv)) {
616 printf("PCI Link already up before configuration!\n");
620 /* DW pre link configurations */
621 rk_pcie_configure(priv, cap_speed);
623 /* Rest the device */
624 if (dm_gpio_is_valid(&priv->rst_gpio)) {
625 dm_gpio_set_value(&priv->rst_gpio, 0);
627 * Minimal is 100ms from spec but we see
628 * some wired devices need much more, such as 600ms.
629 * Add a enough delay to cover all cases.
631 msleep(PERST_WAIT_MS);
632 dm_gpio_set_value(&priv->rst_gpio, 1);
635 rk_pcie_disable_ltssm(priv);
636 rk_pcie_link_status_clear(priv);
637 rk_pcie_enable_debug(priv);
640 rk_pcie_enable_ltssm(priv);
642 for (retries = 0; retries < 5; retries++) {
643 if (is_link_up(priv)) {
644 dev_info(priv->dev, "PCIe Link up, LTSSM is 0x%x\n",
645 rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS));
646 rk_pcie_debug_dump(priv);
650 dev_info(priv->dev, "PCIe Linking... LTSSM is 0x%x\n",
651 rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS));
652 rk_pcie_debug_dump(priv);
656 dev_err(priv->dev, "PCIe-%d Link Fail\n", dev_seq(priv->dev));
657 /* Link maybe in Gen switch recovery but we need to wait more 1s */
662 static int rockchip_pcie_init_port(struct udevice *dev)
666 struct rk_pcie *priv = dev_get_priv(dev);
668 /* Set power and maybe external ref clk input */
669 if (priv->vpcie3v3) {
670 ret = regulator_set_value(priv->vpcie3v3, 3300000);
672 dev_err(priv->dev, "failed to enable vpcie3v3 (ret=%d)\n",
680 ret = generic_phy_init(&priv->phy);
682 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
686 ret = generic_phy_power_on(&priv->phy);
688 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
692 ret = reset_deassert_bulk(&priv->rsts);
694 dev_err(dev, "failed to deassert resets (ret=%d)\n", ret);
695 goto err_power_off_phy;
698 ret = clk_enable_bulk(&priv->clks);
700 dev_err(dev, "failed to enable clks (ret=%d)\n", ret);
701 goto err_deassert_bulk;
704 /* LTSSM EN ctrl mode */
705 val = rk_pcie_readl_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL);
706 val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16);
707 rk_pcie_writel_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL, val);
710 rk_pcie_writel_apb(priv, 0x0, 0xf00040);
711 rk_pcie_setup_host(priv);
713 ret = rk_pcie_link_up(priv, LINK_SPEED_GEN_3);
719 clk_disable_bulk(&priv->clks);
721 reset_assert_bulk(&priv->rsts);
723 generic_phy_power_off(&priv->phy);
725 generic_phy_exit(&priv->phy);
730 static int rockchip_pcie_parse_dt(struct udevice *dev)
732 struct rk_pcie *priv = dev_get_priv(dev);
735 priv->dbi_base = (void *)dev_read_addr_index(dev, 0);
739 dev_dbg(dev, "DBI address is 0x%p\n", priv->dbi_base);
741 priv->apb_base = (void *)dev_read_addr_index(dev, 1);
745 dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base);
747 ret = gpio_request_by_name(dev, "reset-gpios", 0,
748 &priv->rst_gpio, GPIOD_IS_OUT);
750 dev_err(dev, "failed to find reset-gpios property\n");
754 ret = reset_get_bulk(dev, &priv->rsts);
756 dev_err(dev, "Can't get reset: %d\n", ret);
760 ret = clk_get_bulk(dev, &priv->clks);
762 dev_err(dev, "Can't get clock: %d\n", ret);
766 ret = device_get_supply_regulator(dev, "vpcie3v3-supply",
768 if (ret && ret != -ENOENT) {
769 dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret);
773 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
775 dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret);
783 * rockchip_pcie_probe() - Probe the PCIe bus for active link
785 * @dev: A pointer to the device being operated on
787 * Probe for an active link on the PCIe bus and configure the controller
788 * to enable this port.
790 * Return: 0 on success, else -ENODEV
792 static int rockchip_pcie_probe(struct udevice *dev)
794 struct rk_pcie *priv = dev_get_priv(dev);
795 struct udevice *ctlr = pci_get_controller(dev);
796 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
799 priv->first_busno = dev_seq(dev);
802 ret = rockchip_pcie_parse_dt(dev);
806 ret = rockchip_pcie_init_port(dev);
810 dev_info(dev, "PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n",
811 dev_seq(dev), rk_pcie_get_link_speed(priv),
812 rk_pcie_get_link_width(priv),
815 for (ret = 0; ret < hose->region_count; ret++) {
816 if (hose->regions[ret].flags == PCI_REGION_IO) {
817 priv->io.phys_start = hose->regions[ret].phys_start; /* IO base */
818 priv->io.bus_start = hose->regions[ret].bus_start; /* IO_bus_addr */
819 priv->io.size = hose->regions[ret].size; /* IO size */
820 } else if (hose->regions[ret].flags == PCI_REGION_MEM) {
821 priv->mem.phys_start = hose->regions[ret].phys_start; /* MEM base */
822 priv->mem.bus_start = hose->regions[ret].bus_start; /* MEM_bus_addr */
823 priv->mem.size = hose->regions[ret].size; /* MEM size */
824 } else if (hose->regions[ret].flags == PCI_REGION_SYS_MEMORY) {
825 priv->cfg_base = (void *)(priv->io.phys_start - priv->io.size);
826 priv->cfg_size = priv->io.size;
828 dev_err(dev, "invalid flags type!\n");
832 dev_dbg(dev, "Config space: [0x%p - 0x%p, size 0x%llx]\n",
833 priv->cfg_base, priv->cfg_base + priv->cfg_size,
836 dev_dbg(dev, "IO space: [0x%llx - 0x%llx, size 0x%lx]\n",
837 priv->io.phys_start, priv->io.phys_start + priv->io.size,
840 dev_dbg(dev, "IO bus: [0x%lx - 0x%lx, size 0x%lx]\n",
841 priv->io.bus_start, priv->io.bus_start + priv->io.size,
844 dev_dbg(dev, "MEM space: [0x%llx - 0x%llx, size 0x%lx]\n",
845 priv->mem.phys_start, priv->mem.phys_start + priv->mem.size,
848 dev_dbg(dev, "MEM bus: [0x%lx - 0x%lx, size 0x%lx]\n",
849 priv->mem.bus_start, priv->mem.bus_start + priv->mem.size,
852 return rk_pcie_prog_outbound_atu_unroll(priv,
853 PCIE_ATU_REGION_INDEX0,
855 priv->mem.phys_start,
860 static const struct dm_pci_ops rockchip_pcie_ops = {
861 .read_config = rockchip_pcie_rd_conf,
862 .write_config = rockchip_pcie_wr_conf,
865 static const struct udevice_id rockchip_pcie_ids[] = {
866 { .compatible = "rockchip,rk3568-pcie" },
870 U_BOOT_DRIVER(rockchip_dw_pcie) = {
871 .name = "pcie_dw_rockchip",
873 .of_match = rockchip_pcie_ids,
874 .ops = &rockchip_pcie_ops,
875 .probe = rockchip_pcie_probe,
876 .priv_auto = sizeof(struct rk_pcie),