Mason Huo | 08059f0 | 2023-07-25 17:46:48 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * PLDA XpressRich PCIe host controller common functions. |
| 4 | * |
| 5 | * Copyright (C) 2023 StarFive Technology Co., Ltd. |
| 6 | * |
| 7 | */ |
| 8 | |
Mason Huo | 08059f0 | 2023-07-25 17:46:48 +0800 | [diff] [blame] | 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <pci.h> |
| 12 | #include <pci_ids.h> |
| 13 | #include <asm/global_data.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <dm/device_compat.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/iopoll.h> |
| 18 | #include "pcie_plda_common.h" |
| 19 | |
| 20 | static bool plda_pcie_addr_valid(struct pcie_plda *plda, pci_dev_t bdf) |
| 21 | { |
| 22 | /* |
| 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. |
| 27 | */ |
| 28 | if (PCI_BUS(bdf) == plda->sec_busno && PCI_DEV(bdf) > 0) |
| 29 | return false; |
| 30 | |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | static int plda_pcie_conf_address(const struct udevice *udev, pci_dev_t bdf, |
| 35 | uint offset, void **paddr) |
| 36 | { |
| 37 | struct pcie_plda *priv = dev_get_priv(udev); |
Minda Chen | e1cdf6c | 2023-08-07 16:53:35 +0800 | [diff] [blame] | 38 | int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf) - dev_seq(udev), |
| 39 | PCI_DEV(bdf), PCI_FUNC(bdf), offset); |
Mason Huo | 08059f0 | 2023-07-25 17:46:48 +0800 | [diff] [blame] | 40 | |
| 41 | if (!plda_pcie_addr_valid(priv, bdf)) |
| 42 | return -ENODEV; |
| 43 | |
| 44 | *paddr = (void *)(priv->cfg_base + where); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int plda_pcie_config_read(const struct udevice *udev, pci_dev_t bdf, |
| 49 | uint offset, ulong *valuep, |
| 50 | enum pci_size_t size) |
| 51 | { |
| 52 | return pci_generic_mmap_read_config(udev, plda_pcie_conf_address, |
| 53 | bdf, offset, valuep, size); |
| 54 | } |
| 55 | |
| 56 | int plda_pcie_config_write(struct udevice *udev, pci_dev_t bdf, |
| 57 | uint offset, ulong value, |
| 58 | enum pci_size_t size) |
| 59 | { |
| 60 | struct pcie_plda *priv = dev_get_priv(udev); |
| 61 | int ret; |
| 62 | |
| 63 | ret = pci_generic_mmap_write_config(udev, plda_pcie_conf_address, |
| 64 | bdf, offset, value, size); |
| 65 | |
| 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))) { |
| 71 | priv->sec_busno = |
| 72 | ((offset == PCI_PRIMARY_BUS) ? (value >> 8) : value) & 0xff; |
Minda Chen | e1cdf6c | 2023-08-07 16:53:35 +0800 | [diff] [blame] | 73 | priv->sec_busno += dev_seq(udev); |
Mason Huo | 08059f0 | 2023-07-25 17:46:48 +0800 | [diff] [blame] | 74 | debug("Secondary bus number was changed to %d\n", |
| 75 | priv->sec_busno); |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 | |
| 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, |
| 82 | int trsl_param) |
| 83 | { |
| 84 | void __iomem *base = |
| 85 | plda->reg_base + XR3PCI_ATR_AXI4_SLV0; |
| 86 | |
| 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", |
| 90 | plda->atr_table_num); |
| 91 | return -EINVAL; |
| 92 | } |
| 93 | base += XR3PCI_ATR_TABLE_OFFSET * plda->atr_table_num; |
| 94 | plda->atr_table_num++; |
| 95 | |
| 96 | /* |
| 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 |
| 102 | */ |
| 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); |
| 111 | |
| 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); |
| 115 | return 0; |
| 116 | } |