1 // SPDX-License-Identifier: GPL-2.0+
3 * PLDA XpressRich PCIe host controller common functions.
5 * Copyright (C) 2023 StarFive Technology Co., Ltd.
13 #include <asm/global_data.h>
15 #include <dm/device_compat.h>
16 #include <linux/delay.h>
17 #include <linux/iopoll.h>
18 #include "pcie_plda_common.h"
20 static bool plda_pcie_addr_valid(struct pcie_plda *plda, pci_dev_t bdf)
23 * Single device limitation.
24 * PCIe controller contain HW issue that secondary bus of
25 * host bridge emumerate duplicate devices.
26 * Only can access device 0 in secondary bus.
28 if (PCI_BUS(bdf) == plda->sec_busno && PCI_DEV(bdf) > 0)
34 static int plda_pcie_conf_address(const struct udevice *udev, pci_dev_t bdf,
35 uint offset, void **paddr)
37 struct pcie_plda *priv = dev_get_priv(udev);
38 int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf) - dev_seq(udev),
39 PCI_DEV(bdf), PCI_FUNC(bdf), offset);
41 if (!plda_pcie_addr_valid(priv, bdf))
44 *paddr = (void *)(priv->cfg_base + where);
48 int plda_pcie_config_read(const struct udevice *udev, pci_dev_t bdf,
49 uint offset, ulong *valuep,
52 return pci_generic_mmap_read_config(udev, plda_pcie_conf_address,
53 bdf, offset, valuep, size);
56 int plda_pcie_config_write(struct udevice *udev, pci_dev_t bdf,
57 uint offset, ulong value,
60 struct pcie_plda *priv = dev_get_priv(udev);
63 ret = pci_generic_mmap_write_config(udev, plda_pcie_conf_address,
64 bdf, offset, value, size);
66 /* record secondary bus number */
67 if (!ret && PCI_BUS(bdf) == dev_seq(udev) &&
68 PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
69 (offset == PCI_SECONDARY_BUS ||
70 (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8))) {
72 ((offset == PCI_PRIMARY_BUS) ? (value >> 8) : value) & 0xff;
73 priv->sec_busno += dev_seq(udev);
74 debug("Secondary bus number was changed to %d\n",
80 int plda_pcie_set_atr_entry(struct pcie_plda *plda, phys_addr_t src_addr,
81 phys_addr_t trsl_addr, phys_size_t window_size,
85 plda->reg_base + XR3PCI_ATR_AXI4_SLV0;
87 /* Support AXI4 Slave 0 Address Translation Tables 0-7. */
88 if (plda->atr_table_num >= XR3PCI_ATR_MAX_TABLE_NUM) {
89 dev_err(plda->dev, "ATR table number %d exceeds max num\n",
93 base += XR3PCI_ATR_TABLE_OFFSET * plda->atr_table_num;
94 plda->atr_table_num++;
97 * X3PCI_ATR_SRC_ADDR_LOW:
98 * - bit 0: enable entry,
99 * - bits 1-6: ATR window size: total size in bytes: 2^(ATR_WSIZE + 1)
100 * - bits 7-11: reserved
101 * - bits 12-31: start of source address
103 writel((lower_32_bits(src_addr) & XR3PCI_ATR_SRC_ADDR_MASK) |
104 (fls(window_size) - 1) << XR3PCI_ATR_SRC_WIN_SIZE_SHIFT | 1,
105 base + XR3PCI_ATR_SRC_ADDR_LOW);
106 writel(upper_32_bits(src_addr), base + XR3PCI_ATR_SRC_ADDR_HIGH);
107 writel((lower_32_bits(trsl_addr) & XR3PCI_ATR_TRSL_ADDR_MASK),
108 base + XR3PCI_ATR_TRSL_ADDR_LOW);
109 writel(upper_32_bits(trsl_addr), base + XR3PCI_ATR_TRSL_ADDR_HIGH);
110 writel(trsl_param, base + XR3PCI_ATR_TRSL_PARAM);
112 dev_dbg(plda->dev, "ATR entry: 0x%010llx %s 0x%010llx [0x%010llx] (param: 0x%06x)\n",
113 src_addr, (trsl_param & XR3PCI_ATR_TRSL_DIR) ? "<-" : "->",
114 trsl_addr, (u64)window_size, trsl_param);