pcie-cadence.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017 Cadence
  3. // Cadence PCIe controller driver.
  4. // Author: Cyrille Pitchen <cyrille.pitchen@free-electrons.com>
  5. #include <linux/kernel.h>
  6. #include "pcie-cadence.h"
  7. void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u8 fn,
  8. u32 r, bool is_io,
  9. u64 cpu_addr, u64 pci_addr, size_t size)
  10. {
  11. /*
  12. * roundup_pow_of_two() returns an unsigned long, which is not suited
  13. * for 64bit values.
  14. */
  15. u64 sz = 1ULL << fls64(size - 1);
  16. int nbits = ilog2(sz);
  17. u32 addr0, addr1, desc0, desc1;
  18. if (nbits < 8)
  19. nbits = 8;
  20. /* Set the PCI address */
  21. addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) |
  22. (lower_32_bits(pci_addr) & GENMASK(31, 8));
  23. addr1 = upper_32_bits(pci_addr);
  24. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), addr0);
  25. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), addr1);
  26. /* Set the PCIe header descriptor */
  27. if (is_io)
  28. desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
  29. else
  30. desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
  31. desc1 = 0;
  32. /*
  33. * Whatever Bit [23] is set or not inside DESC0 register of the outbound
  34. * PCIe descriptor, the PCI function number must be set into
  35. * Bits [26:24] of DESC0 anyway.
  36. *
  37. * In Root Complex mode, the function number is always 0 but in Endpoint
  38. * mode, the PCIe controller may support more than one function. This
  39. * function number needs to be set properly into the outbound PCIe
  40. * descriptor.
  41. *
  42. * Besides, setting Bit [23] is mandatory when in Root Complex mode:
  43. * then the driver must provide the bus, resp. device, number in
  44. * Bits [7:0] of DESC1, resp. Bits[31:27] of DESC0. Like the function
  45. * number, the device number is always 0 in Root Complex mode.
  46. *
  47. * However when in Endpoint mode, we can clear Bit [23] of DESC0, hence
  48. * the PCIe controller will use the captured values for the bus and
  49. * device numbers.
  50. */
  51. if (pcie->is_rc) {
  52. /* The device and function numbers are always 0. */
  53. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
  54. CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
  55. desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
  56. } else {
  57. /*
  58. * Use captured values for bus and device numbers but still
  59. * need to set the function number.
  60. */
  61. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
  62. }
  63. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
  64. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
  65. /* Set the CPU address */
  66. cpu_addr -= pcie->mem_res->start;
  67. addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) |
  68. (lower_32_bits(cpu_addr) & GENMASK(31, 8));
  69. addr1 = upper_32_bits(cpu_addr);
  70. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
  71. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
  72. }
  73. void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie, u8 fn,
  74. u32 r, u64 cpu_addr)
  75. {
  76. u32 addr0, addr1, desc0, desc1;
  77. desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
  78. desc1 = 0;
  79. /* See cdns_pcie_set_outbound_region() comments above. */
  80. if (pcie->is_rc) {
  81. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
  82. CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
  83. desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(pcie->bus);
  84. } else {
  85. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
  86. }
  87. /* Set the CPU address */
  88. cpu_addr -= pcie->mem_res->start;
  89. addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
  90. (lower_32_bits(cpu_addr) & GENMASK(31, 8));
  91. addr1 = upper_32_bits(cpu_addr);
  92. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
  93. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
  94. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
  95. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
  96. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
  97. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
  98. }
  99. void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
  100. {
  101. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
  102. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
  103. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), 0);
  104. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), 0);
  105. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), 0);
  106. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), 0);
  107. }