pcie-artpec6.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * PCIe host controller driver for Axis ARTPEC-6 SoC
  3. *
  4. * Author: Niklas Cassel <niklas.cassel@axis.com>
  5. *
  6. * Based on work done by Phil Edworthy <phil@edworthys.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/pci.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/resource.h>
  18. #include <linux/signal.h>
  19. #include <linux/types.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mfd/syscon.h>
  22. #include <linux/regmap.h>
  23. #include "pcie-designware.h"
  24. #define to_artpec6_pcie(x) container_of(x, struct artpec6_pcie, pp)
  25. struct artpec6_pcie {
  26. struct pcie_port pp;
  27. struct regmap *regmap;
  28. void __iomem *phy_base;
  29. };
  30. /* PCIe Port Logic registers (memory-mapped) */
  31. #define PL_OFFSET 0x700
  32. #define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
  33. #define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
  34. #define MISC_CONTROL_1_OFF (PL_OFFSET + 0x1bc)
  35. #define DBI_RO_WR_EN 1
  36. /* ARTPEC-6 specific registers */
  37. #define PCIECFG 0x18
  38. #define PCIECFG_DBG_OEN (1 << 24)
  39. #define PCIECFG_CORE_RESET_REQ (1 << 21)
  40. #define PCIECFG_LTSSM_ENABLE (1 << 20)
  41. #define PCIECFG_CLKREQ_B (1 << 11)
  42. #define PCIECFG_REFCLK_ENABLE (1 << 10)
  43. #define PCIECFG_PLL_ENABLE (1 << 9)
  44. #define PCIECFG_PCLK_ENABLE (1 << 8)
  45. #define PCIECFG_RISRCREN (1 << 4)
  46. #define PCIECFG_MODE_TX_DRV_EN (1 << 3)
  47. #define PCIECFG_CISRREN (1 << 2)
  48. #define PCIECFG_MACRO_ENABLE (1 << 0)
  49. #define NOCCFG 0x40
  50. #define NOCCFG_ENABLE_CLK_PCIE (1 << 4)
  51. #define NOCCFG_POWER_PCIE_IDLEACK (1 << 3)
  52. #define NOCCFG_POWER_PCIE_IDLE (1 << 2)
  53. #define NOCCFG_POWER_PCIE_IDLEREQ (1 << 1)
  54. #define PHY_STATUS 0x118
  55. #define PHY_COSPLLLOCK (1 << 0)
  56. #define ARTPEC6_CPU_TO_BUS_ADDR 0x0fffffff
  57. static int artpec6_pcie_establish_link(struct pcie_port *pp)
  58. {
  59. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pp);
  60. u32 val;
  61. unsigned int retries;
  62. /* Hold DW core in reset */
  63. regmap_read(artpec6_pcie->regmap, PCIECFG, &val);
  64. val |= PCIECFG_CORE_RESET_REQ;
  65. regmap_write(artpec6_pcie->regmap, PCIECFG, val);
  66. regmap_read(artpec6_pcie->regmap, PCIECFG, &val);
  67. val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
  68. PCIECFG_MODE_TX_DRV_EN |
  69. PCIECFG_CISRREN | /* Reference clock term. 100 Ohm */
  70. PCIECFG_MACRO_ENABLE;
  71. val |= PCIECFG_REFCLK_ENABLE;
  72. val &= ~PCIECFG_DBG_OEN;
  73. val &= ~PCIECFG_CLKREQ_B;
  74. regmap_write(artpec6_pcie->regmap, PCIECFG, val);
  75. usleep_range(5000, 6000);
  76. regmap_read(artpec6_pcie->regmap, NOCCFG, &val);
  77. val |= NOCCFG_ENABLE_CLK_PCIE;
  78. regmap_write(artpec6_pcie->regmap, NOCCFG, val);
  79. usleep_range(20, 30);
  80. regmap_read(artpec6_pcie->regmap, PCIECFG, &val);
  81. val |= PCIECFG_PCLK_ENABLE | PCIECFG_PLL_ENABLE;
  82. regmap_write(artpec6_pcie->regmap, PCIECFG, val);
  83. usleep_range(6000, 7000);
  84. regmap_read(artpec6_pcie->regmap, NOCCFG, &val);
  85. val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
  86. regmap_write(artpec6_pcie->regmap, NOCCFG, val);
  87. retries = 50;
  88. do {
  89. usleep_range(1000, 2000);
  90. regmap_read(artpec6_pcie->regmap, NOCCFG, &val);
  91. retries--;
  92. } while (retries &&
  93. (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
  94. retries = 50;
  95. do {
  96. usleep_range(1000, 2000);
  97. val = readl(artpec6_pcie->phy_base + PHY_STATUS);
  98. retries--;
  99. } while (retries && !(val & PHY_COSPLLLOCK));
  100. /* Take DW core out of reset */
  101. regmap_read(artpec6_pcie->regmap, PCIECFG, &val);
  102. val &= ~PCIECFG_CORE_RESET_REQ;
  103. regmap_write(artpec6_pcie->regmap, PCIECFG, val);
  104. usleep_range(100, 200);
  105. /*
  106. * Enable writing to config regs. This is required as the Synopsys
  107. * driver changes the class code. That register needs DBI write enable.
  108. */
  109. writel(DBI_RO_WR_EN, pp->dbi_base + MISC_CONTROL_1_OFF);
  110. pp->io_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  111. pp->mem_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  112. pp->cfg0_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  113. pp->cfg1_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  114. /* setup root complex */
  115. dw_pcie_setup_rc(pp);
  116. /* assert LTSSM enable */
  117. regmap_read(artpec6_pcie->regmap, PCIECFG, &val);
  118. val |= PCIECFG_LTSSM_ENABLE;
  119. regmap_write(artpec6_pcie->regmap, PCIECFG, val);
  120. /* check if the link is up or not */
  121. if (!dw_pcie_wait_for_link(pp))
  122. return 0;
  123. dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
  124. readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
  125. readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
  126. return -ETIMEDOUT;
  127. }
  128. static void artpec6_pcie_enable_interrupts(struct pcie_port *pp)
  129. {
  130. if (IS_ENABLED(CONFIG_PCI_MSI))
  131. dw_pcie_msi_init(pp);
  132. }
  133. static void artpec6_pcie_host_init(struct pcie_port *pp)
  134. {
  135. artpec6_pcie_establish_link(pp);
  136. artpec6_pcie_enable_interrupts(pp);
  137. }
  138. static int artpec6_pcie_link_up(struct pcie_port *pp)
  139. {
  140. u32 rc;
  141. /*
  142. * Get status from Synopsys IP
  143. * link is debug bit 36, debug register 1 starts at bit 32
  144. */
  145. rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) & (0x1 << (36 - 32));
  146. if (rc)
  147. return 1;
  148. return 0;
  149. }
  150. static struct pcie_host_ops artpec6_pcie_host_ops = {
  151. .link_up = artpec6_pcie_link_up,
  152. .host_init = artpec6_pcie_host_init,
  153. };
  154. static irqreturn_t artpec6_pcie_msi_handler(int irq, void *arg)
  155. {
  156. struct pcie_port *pp = arg;
  157. return dw_handle_msi_irq(pp);
  158. }
  159. static int __init artpec6_add_pcie_port(struct pcie_port *pp,
  160. struct platform_device *pdev)
  161. {
  162. int ret;
  163. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  164. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  165. if (pp->msi_irq <= 0) {
  166. dev_err(&pdev->dev, "failed to get MSI irq\n");
  167. return -ENODEV;
  168. }
  169. ret = devm_request_irq(&pdev->dev, pp->msi_irq,
  170. artpec6_pcie_msi_handler,
  171. IRQF_SHARED | IRQF_NO_THREAD,
  172. "artpec6-pcie-msi", pp);
  173. if (ret) {
  174. dev_err(&pdev->dev, "failed to request MSI irq\n");
  175. return ret;
  176. }
  177. }
  178. pp->root_bus_nr = -1;
  179. pp->ops = &artpec6_pcie_host_ops;
  180. ret = dw_pcie_host_init(pp);
  181. if (ret) {
  182. dev_err(&pdev->dev, "failed to initialize host\n");
  183. return ret;
  184. }
  185. return 0;
  186. }
  187. static int artpec6_pcie_probe(struct platform_device *pdev)
  188. {
  189. struct artpec6_pcie *artpec6_pcie;
  190. struct pcie_port *pp;
  191. struct resource *dbi_base;
  192. struct resource *phy_base;
  193. int ret;
  194. artpec6_pcie = devm_kzalloc(&pdev->dev, sizeof(*artpec6_pcie),
  195. GFP_KERNEL);
  196. if (!artpec6_pcie)
  197. return -ENOMEM;
  198. pp = &artpec6_pcie->pp;
  199. pp->dev = &pdev->dev;
  200. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  201. pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
  202. if (IS_ERR(pp->dbi_base))
  203. return PTR_ERR(pp->dbi_base);
  204. phy_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  205. artpec6_pcie->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
  206. if (IS_ERR(artpec6_pcie->phy_base))
  207. return PTR_ERR(artpec6_pcie->phy_base);
  208. artpec6_pcie->regmap =
  209. syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  210. "axis,syscon-pcie");
  211. if (IS_ERR(artpec6_pcie->regmap))
  212. return PTR_ERR(artpec6_pcie->regmap);
  213. ret = artpec6_add_pcie_port(pp, pdev);
  214. if (ret < 0)
  215. return ret;
  216. platform_set_drvdata(pdev, artpec6_pcie);
  217. return 0;
  218. }
  219. static const struct of_device_id artpec6_pcie_of_match[] = {
  220. { .compatible = "axis,artpec6-pcie", },
  221. {},
  222. };
  223. static struct platform_driver artpec6_pcie_driver = {
  224. .probe = artpec6_pcie_probe,
  225. .driver = {
  226. .name = "artpec6-pcie",
  227. .of_match_table = artpec6_pcie_of_match,
  228. },
  229. };
  230. builtin_platform_driver(artpec6_pcie_driver);