pcie_plda_common.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #include <common.h>
  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. static bool plda_pcie_addr_valid(struct pcie_plda *plda, pci_dev_t bdf)
  20. {
  21. /*
  22. * Single device limitation.
  23. * PCIe controller contain HW issue that secondary bus of
  24. * host bridge emumerate duplicate devices.
  25. * Only can access device 0 in secondary bus.
  26. */
  27. if (PCI_BUS(bdf) == plda->sec_busno && PCI_DEV(bdf) > 0)
  28. return false;
  29. return true;
  30. }
  31. static int plda_pcie_conf_address(const struct udevice *udev, pci_dev_t bdf,
  32. uint offset, void **paddr)
  33. {
  34. struct pcie_plda *priv = dev_get_priv(udev);
  35. int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf) - dev_seq(udev),
  36. PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  37. if (!plda_pcie_addr_valid(priv, bdf))
  38. return -ENODEV;
  39. *paddr = (void *)(priv->cfg_base + where);
  40. return 0;
  41. }
  42. int plda_pcie_config_read(const struct udevice *udev, pci_dev_t bdf,
  43. uint offset, ulong *valuep,
  44. enum pci_size_t size)
  45. {
  46. return pci_generic_mmap_read_config(udev, plda_pcie_conf_address,
  47. bdf, offset, valuep, size);
  48. }
  49. int plda_pcie_config_write(struct udevice *udev, pci_dev_t bdf,
  50. uint offset, ulong value,
  51. enum pci_size_t size)
  52. {
  53. struct pcie_plda *priv = dev_get_priv(udev);
  54. int ret;
  55. ret = pci_generic_mmap_write_config(udev, plda_pcie_conf_address,
  56. bdf, offset, value, size);
  57. /* record secondary bus number */
  58. if (!ret && PCI_BUS(bdf) == dev_seq(udev) &&
  59. PCI_DEV(bdf) == 0 && PCI_FUNC(bdf) == 0 &&
  60. (offset == PCI_SECONDARY_BUS ||
  61. (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8))) {
  62. priv->sec_busno =
  63. ((offset == PCI_PRIMARY_BUS) ? (value >> 8) : value) & 0xff;
  64. priv->sec_busno += dev_seq(udev);
  65. debug("Secondary bus number was changed to %d\n",
  66. priv->sec_busno);
  67. }
  68. return ret;
  69. }
  70. int plda_pcie_set_atr_entry(struct pcie_plda *plda, phys_addr_t src_addr,
  71. phys_addr_t trsl_addr, phys_size_t window_size,
  72. int trsl_param)
  73. {
  74. void __iomem *base =
  75. plda->reg_base + XR3PCI_ATR_AXI4_SLV0;
  76. /* Support AXI4 Slave 0 Address Translation Tables 0-7. */
  77. if (plda->atr_table_num >= XR3PCI_ATR_MAX_TABLE_NUM) {
  78. dev_err(plda->dev, "ATR table number %d exceeds max num\n",
  79. plda->atr_table_num);
  80. return -EINVAL;
  81. }
  82. base += XR3PCI_ATR_TABLE_OFFSET * plda->atr_table_num;
  83. plda->atr_table_num++;
  84. /*
  85. * X3PCI_ATR_SRC_ADDR_LOW:
  86. * - bit 0: enable entry,
  87. * - bits 1-6: ATR window size: total size in bytes: 2^(ATR_WSIZE + 1)
  88. * - bits 7-11: reserved
  89. * - bits 12-31: start of source address
  90. */
  91. writel((lower_32_bits(src_addr) & XR3PCI_ATR_SRC_ADDR_MASK) |
  92. (fls(window_size) - 1) << XR3PCI_ATR_SRC_WIN_SIZE_SHIFT | 1,
  93. base + XR3PCI_ATR_SRC_ADDR_LOW);
  94. writel(upper_32_bits(src_addr), base + XR3PCI_ATR_SRC_ADDR_HIGH);
  95. writel((lower_32_bits(trsl_addr) & XR3PCI_ATR_TRSL_ADDR_MASK),
  96. base + XR3PCI_ATR_TRSL_ADDR_LOW);
  97. writel(upper_32_bits(trsl_addr), base + XR3PCI_ATR_TRSL_ADDR_HIGH);
  98. writel(trsl_param, base + XR3PCI_ATR_TRSL_PARAM);
  99. dev_dbg(plda->dev, "ATR entry: 0x%010llx %s 0x%010llx [0x%010llx] (param: 0x%06x)\n",
  100. src_addr, (trsl_param & XR3PCI_ATR_TRSL_DIR) ? "<-" : "->",
  101. trsl_addr, (u64)window_size, trsl_param);
  102. return 0;
  103. }