pcie-cadence.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 <linux/module.h>
  7. #include <linux/of.h>
  8. #include "pcie-cadence.h"
  9. #include "../../pci.h"
  10. u8 cdns_pcie_find_capability(struct cdns_pcie *pcie, u8 cap)
  11. {
  12. return PCI_FIND_NEXT_CAP(cdns_pcie_read_cfg, PCI_CAPABILITY_LIST,
  13. cap, NULL, pcie);
  14. }
  15. EXPORT_SYMBOL_GPL(cdns_pcie_find_capability);
  16. u16 cdns_pcie_find_ext_capability(struct cdns_pcie *pcie, u8 cap)
  17. {
  18. return PCI_FIND_NEXT_EXT_CAP(cdns_pcie_read_cfg, 0, cap, NULL, pcie);
  19. }
  20. EXPORT_SYMBOL_GPL(cdns_pcie_find_ext_capability);
  21. bool cdns_pcie_linkup(struct cdns_pcie *pcie)
  22. {
  23. u32 pl_reg_val;
  24. pl_reg_val = cdns_pcie_readl(pcie, CDNS_PCIE_LM_BASE);
  25. if (pl_reg_val & GENMASK(0, 0))
  26. return true;
  27. return false;
  28. }
  29. EXPORT_SYMBOL_GPL(cdns_pcie_linkup);
  30. void cdns_pcie_detect_quiet_min_delay_set(struct cdns_pcie *pcie)
  31. {
  32. u32 delay = 0x3;
  33. u32 ltssm_control_cap;
  34. /*
  35. * Set the LTSSM Detect Quiet state min. delay to 2ms.
  36. */
  37. ltssm_control_cap = cdns_pcie_readl(pcie, CDNS_PCIE_LTSSM_CONTROL_CAP);
  38. ltssm_control_cap = ((ltssm_control_cap &
  39. ~CDNS_PCIE_DETECT_QUIET_MIN_DELAY_MASK) |
  40. CDNS_PCIE_DETECT_QUIET_MIN_DELAY(delay));
  41. cdns_pcie_writel(pcie, CDNS_PCIE_LTSSM_CONTROL_CAP, ltssm_control_cap);
  42. }
  43. EXPORT_SYMBOL_GPL(cdns_pcie_detect_quiet_min_delay_set);
  44. void cdns_pcie_set_outbound_region(struct cdns_pcie *pcie, u8 busnr, u8 fn,
  45. u32 r, bool is_io,
  46. u64 cpu_addr, u64 pci_addr, size_t size)
  47. {
  48. /*
  49. * roundup_pow_of_two() returns an unsigned long, which is not suited
  50. * for 64bit values.
  51. */
  52. u64 sz = 1ULL << fls64(size - 1);
  53. int nbits = ilog2(sz);
  54. u32 addr0, addr1, desc0, desc1;
  55. if (nbits < 8)
  56. nbits = 8;
  57. /* Set the PCI address */
  58. addr0 = CDNS_PCIE_AT_OB_REGION_PCI_ADDR0_NBITS(nbits) |
  59. (lower_32_bits(pci_addr) & GENMASK(31, 8));
  60. addr1 = upper_32_bits(pci_addr);
  61. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), addr0);
  62. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), addr1);
  63. /* Set the PCIe header descriptor */
  64. if (is_io)
  65. desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_IO;
  66. else
  67. desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_MEM;
  68. desc1 = 0;
  69. /*
  70. * Whatever Bit [23] is set or not inside DESC0 register of the outbound
  71. * PCIe descriptor, the PCI function number must be set into
  72. * Bits [26:24] of DESC0 anyway.
  73. *
  74. * In Root Complex mode, the function number is always 0 but in Endpoint
  75. * mode, the PCIe controller may support more than one function. This
  76. * function number needs to be set properly into the outbound PCIe
  77. * descriptor.
  78. *
  79. * Besides, setting Bit [23] is mandatory when in Root Complex mode:
  80. * then the driver must provide the bus, resp. device, number in
  81. * Bits [7:0] of DESC1, resp. Bits[31:27] of DESC0. Like the function
  82. * number, the device number is always 0 in Root Complex mode.
  83. *
  84. * However when in Endpoint mode, we can clear Bit [23] of DESC0, hence
  85. * the PCIe controller will use the captured values for the bus and
  86. * device numbers.
  87. */
  88. if (pcie->is_rc) {
  89. /* The device and function numbers are always 0. */
  90. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
  91. CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
  92. desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(busnr);
  93. } else {
  94. /*
  95. * Use captured values for bus and device numbers but still
  96. * need to set the function number.
  97. */
  98. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
  99. }
  100. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
  101. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
  102. /* Set the CPU address */
  103. if (pcie->ops && pcie->ops->cpu_addr_fixup)
  104. cpu_addr = pcie->ops->cpu_addr_fixup(pcie, cpu_addr);
  105. addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(nbits) |
  106. (lower_32_bits(cpu_addr) & GENMASK(31, 8));
  107. addr1 = upper_32_bits(cpu_addr);
  108. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
  109. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
  110. }
  111. EXPORT_SYMBOL_GPL(cdns_pcie_set_outbound_region);
  112. void cdns_pcie_set_outbound_region_for_normal_msg(struct cdns_pcie *pcie,
  113. u8 busnr, u8 fn,
  114. u32 r, u64 cpu_addr)
  115. {
  116. u32 addr0, addr1, desc0, desc1;
  117. desc0 = CDNS_PCIE_AT_OB_REGION_DESC0_TYPE_NORMAL_MSG;
  118. desc1 = 0;
  119. /* See cdns_pcie_set_outbound_region() comments above. */
  120. if (pcie->is_rc) {
  121. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_HARDCODED_RID |
  122. CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(0);
  123. desc1 |= CDNS_PCIE_AT_OB_REGION_DESC1_BUS(busnr);
  124. } else {
  125. desc0 |= CDNS_PCIE_AT_OB_REGION_DESC0_DEVFN(fn);
  126. }
  127. /* Set the CPU address */
  128. if (pcie->ops && pcie->ops->cpu_addr_fixup)
  129. cpu_addr = pcie->ops->cpu_addr_fixup(pcie, cpu_addr);
  130. addr0 = CDNS_PCIE_AT_OB_REGION_CPU_ADDR0_NBITS(17) |
  131. (lower_32_bits(cpu_addr) & GENMASK(31, 8));
  132. addr1 = upper_32_bits(cpu_addr);
  133. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
  134. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
  135. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), desc0);
  136. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), desc1);
  137. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), addr0);
  138. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), addr1);
  139. }
  140. EXPORT_SYMBOL_GPL(cdns_pcie_set_outbound_region_for_normal_msg);
  141. void cdns_pcie_reset_outbound_region(struct cdns_pcie *pcie, u32 r)
  142. {
  143. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR0(r), 0);
  144. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_PCI_ADDR1(r), 0);
  145. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC0(r), 0);
  146. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_DESC1(r), 0);
  147. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR0(r), 0);
  148. cdns_pcie_writel(pcie, CDNS_PCIE_AT_OB_REGION_CPU_ADDR1(r), 0);
  149. }
  150. EXPORT_SYMBOL_GPL(cdns_pcie_reset_outbound_region);
  151. void cdns_pcie_disable_phy(struct cdns_pcie *pcie)
  152. {
  153. int i = pcie->phy_count;
  154. while (i--) {
  155. phy_power_off(pcie->phy[i]);
  156. phy_exit(pcie->phy[i]);
  157. }
  158. }
  159. EXPORT_SYMBOL_GPL(cdns_pcie_disable_phy);
  160. int cdns_pcie_enable_phy(struct cdns_pcie *pcie)
  161. {
  162. int ret;
  163. int i;
  164. for (i = 0; i < pcie->phy_count; i++) {
  165. ret = phy_init(pcie->phy[i]);
  166. if (ret < 0)
  167. goto err_phy;
  168. ret = phy_power_on(pcie->phy[i]);
  169. if (ret < 0) {
  170. phy_exit(pcie->phy[i]);
  171. goto err_phy;
  172. }
  173. }
  174. return 0;
  175. err_phy:
  176. while (--i >= 0) {
  177. phy_power_off(pcie->phy[i]);
  178. phy_exit(pcie->phy[i]);
  179. }
  180. return ret;
  181. }
  182. EXPORT_SYMBOL_GPL(cdns_pcie_enable_phy);
  183. int cdns_pcie_init_phy(struct device *dev, struct cdns_pcie *pcie)
  184. {
  185. struct device_node *np = dev->of_node;
  186. int phy_count;
  187. struct phy **phy;
  188. struct device_link **link;
  189. int i;
  190. int ret;
  191. const char *name;
  192. phy_count = of_property_count_strings(np, "phy-names");
  193. if (phy_count < 1) {
  194. dev_info(dev, "no \"phy-names\" property found; PHY will not be initialized\n");
  195. pcie->phy_count = 0;
  196. return 0;
  197. }
  198. phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
  199. if (!phy)
  200. return -ENOMEM;
  201. link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
  202. if (!link)
  203. return -ENOMEM;
  204. for (i = 0; i < phy_count; i++) {
  205. of_property_read_string_index(np, "phy-names", i, &name);
  206. phy[i] = devm_phy_get(dev, name);
  207. if (IS_ERR(phy[i])) {
  208. ret = PTR_ERR(phy[i]);
  209. goto err_phy;
  210. }
  211. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  212. if (!link[i]) {
  213. devm_phy_put(dev, phy[i]);
  214. ret = -EINVAL;
  215. goto err_phy;
  216. }
  217. }
  218. pcie->phy_count = phy_count;
  219. pcie->phy = phy;
  220. pcie->link = link;
  221. ret = cdns_pcie_enable_phy(pcie);
  222. if (ret)
  223. goto err_phy;
  224. return 0;
  225. err_phy:
  226. while (--i >= 0) {
  227. device_link_del(link[i]);
  228. devm_phy_put(dev, phy[i]);
  229. }
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(cdns_pcie_init_phy);
  233. static int cdns_pcie_suspend_noirq(struct device *dev)
  234. {
  235. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  236. cdns_pcie_disable_phy(pcie);
  237. return 0;
  238. }
  239. static int cdns_pcie_resume_noirq(struct device *dev)
  240. {
  241. struct cdns_pcie *pcie = dev_get_drvdata(dev);
  242. int ret;
  243. ret = cdns_pcie_enable_phy(pcie);
  244. if (ret) {
  245. dev_err(dev, "failed to enable PHY\n");
  246. return ret;
  247. }
  248. return 0;
  249. }
  250. const struct dev_pm_ops cdns_pcie_pm_ops = {
  251. NOIRQ_SYSTEM_SLEEP_PM_OPS(cdns_pcie_suspend_noirq,
  252. cdns_pcie_resume_noirq)
  253. };
  254. EXPORT_SYMBOL_GPL(cdns_pcie_pm_ops);
  255. MODULE_LICENSE("GPL");
  256. MODULE_DESCRIPTION("Cadence PCIe controller driver");
  257. MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@free-electrons.com>");