pcie-cadence.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. }
  108. void cdns_pcie_disable_phy(struct cdns_pcie *pcie)
  109. {
  110. int i = pcie->phy_count;
  111. while (i--) {
  112. phy_power_off(pcie->phy[i]);
  113. phy_exit(pcie->phy[i]);
  114. }
  115. }
  116. int cdns_pcie_enable_phy(struct cdns_pcie *pcie)
  117. {
  118. int ret;
  119. int i;
  120. for (i = 0; i < pcie->phy_count; i++) {
  121. ret = phy_init(pcie->phy[i]);
  122. if (ret < 0)
  123. goto err_phy;
  124. ret = phy_power_on(pcie->phy[i]);
  125. if (ret < 0) {
  126. phy_exit(pcie->phy[i]);
  127. goto err_phy;
  128. }
  129. }
  130. return 0;
  131. err_phy:
  132. while (--i >= 0) {
  133. phy_power_off(pcie->phy[i]);
  134. phy_exit(pcie->phy[i]);
  135. }
  136. return ret;
  137. }
  138. int cdns_pcie_init_phy(struct device *dev, struct cdns_pcie *pcie)
  139. {
  140. struct device_node *np = dev->of_node;
  141. int phy_count;
  142. struct phy **phy;
  143. struct device_link **link;
  144. int i;
  145. int ret;
  146. const char *name;
  147. phy_count = of_property_count_strings(np, "phy-names");
  148. if (phy_count < 1) {
  149. dev_err(dev, "no phy-names. PHY will not be initialized\n");
  150. pcie->phy_count = 0;
  151. return 0;
  152. }
  153. phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
  154. if (!phy)
  155. return -ENOMEM;
  156. link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
  157. if (!link)
  158. return -ENOMEM;
  159. for (i = 0; i < phy_count; i++) {
  160. of_property_read_string_index(np, "phy-names", i, &name);
  161. phy[i] = devm_phy_get(dev, name);
  162. if (IS_ERR(phy[i])) {
  163. ret = PTR_ERR(phy[i]);
  164. goto err_phy;
  165. }
  166. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  167. if (!link[i]) {
  168. devm_phy_put(dev, phy[i]);
  169. ret = -EINVAL;
  170. goto err_phy;
  171. }
  172. }
  173. pcie->phy_count = phy_count;
  174. pcie->phy = phy;
  175. pcie->link = link;
  176. ret = cdns_pcie_enable_phy(pcie);
  177. if (ret)
  178. goto err_phy;
  179. return 0;
  180. err_phy:
  181. while (--i >= 0) {
  182. device_link_del(link[i]);
  183. devm_phy_put(dev, phy[i]);
  184. }
  185. return ret;
  186. }
  187. #ifdef CONFIG_PM_SLEEP
  188. static int cdns_pcie_suspend_noirq(struct device *dev)
  189. {
  190. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  191. cdns_pcie_disable_phy(pcie);
  192. return 0;
  193. }
  194. static int cdns_pcie_resume_noirq(struct device *dev)
  195. {
  196. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  197. int ret;
  198. ret = cdns_pcie_enable_phy(pcie);
  199. if (ret) {
  200. dev_err(dev, "failed to enable phy\n");
  201. return ret;
  202. }
  203. return 0;
  204. }
  205. #endif
  206. const struct dev_pm_ops cdns_pcie_pm_ops = {
  207. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cdns_pcie_suspend_noirq,
  208. cdns_pcie_resume_noirq)
  209. };