pcie-artpec6.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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; /* pp.dbi_base is DT dbi */
  27. struct regmap *regmap; /* DT axis,syscon-pcie */
  28. void __iomem *phy_base; /* DT phy */
  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 u32 artpec6_pcie_readl(struct artpec6_pcie *artpec6_pcie, u32 offset)
  58. {
  59. u32 val;
  60. regmap_read(artpec6_pcie->regmap, offset, &val);
  61. return val;
  62. }
  63. static void artpec6_pcie_writel(struct artpec6_pcie *artpec6_pcie, u32 offset, u32 val)
  64. {
  65. regmap_write(artpec6_pcie->regmap, offset, val);
  66. }
  67. static int artpec6_pcie_establish_link(struct artpec6_pcie *artpec6_pcie)
  68. {
  69. struct pcie_port *pp = &artpec6_pcie->pp;
  70. u32 val;
  71. unsigned int retries;
  72. /* Hold DW core in reset */
  73. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  74. val |= PCIECFG_CORE_RESET_REQ;
  75. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  76. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  77. val |= PCIECFG_RISRCREN | /* Receiver term. 50 Ohm */
  78. PCIECFG_MODE_TX_DRV_EN |
  79. PCIECFG_CISRREN | /* Reference clock term. 100 Ohm */
  80. PCIECFG_MACRO_ENABLE;
  81. val |= PCIECFG_REFCLK_ENABLE;
  82. val &= ~PCIECFG_DBG_OEN;
  83. val &= ~PCIECFG_CLKREQ_B;
  84. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  85. usleep_range(5000, 6000);
  86. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  87. val |= NOCCFG_ENABLE_CLK_PCIE;
  88. artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
  89. usleep_range(20, 30);
  90. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  91. val |= PCIECFG_PCLK_ENABLE | PCIECFG_PLL_ENABLE;
  92. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  93. usleep_range(6000, 7000);
  94. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  95. val &= ~NOCCFG_POWER_PCIE_IDLEREQ;
  96. artpec6_pcie_writel(artpec6_pcie, NOCCFG, val);
  97. retries = 50;
  98. do {
  99. usleep_range(1000, 2000);
  100. val = artpec6_pcie_readl(artpec6_pcie, NOCCFG);
  101. retries--;
  102. } while (retries &&
  103. (val & (NOCCFG_POWER_PCIE_IDLEACK | NOCCFG_POWER_PCIE_IDLE)));
  104. retries = 50;
  105. do {
  106. usleep_range(1000, 2000);
  107. val = readl(artpec6_pcie->phy_base + PHY_STATUS);
  108. retries--;
  109. } while (retries && !(val & PHY_COSPLLLOCK));
  110. /* Take DW core out of reset */
  111. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  112. val &= ~PCIECFG_CORE_RESET_REQ;
  113. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  114. usleep_range(100, 200);
  115. /*
  116. * Enable writing to config regs. This is required as the Synopsys
  117. * driver changes the class code. That register needs DBI write enable.
  118. */
  119. dw_pcie_writel_rc(pp, MISC_CONTROL_1_OFF, DBI_RO_WR_EN);
  120. pp->io_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  121. pp->mem_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  122. pp->cfg0_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  123. pp->cfg1_base &= ARTPEC6_CPU_TO_BUS_ADDR;
  124. /* setup root complex */
  125. dw_pcie_setup_rc(pp);
  126. /* assert LTSSM enable */
  127. val = artpec6_pcie_readl(artpec6_pcie, PCIECFG);
  128. val |= PCIECFG_LTSSM_ENABLE;
  129. artpec6_pcie_writel(artpec6_pcie, PCIECFG, val);
  130. /* check if the link is up or not */
  131. if (!dw_pcie_wait_for_link(pp))
  132. return 0;
  133. dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
  134. dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R0),
  135. dw_pcie_readl_rc(pp, PCIE_PHY_DEBUG_R1));
  136. return -ETIMEDOUT;
  137. }
  138. static void artpec6_pcie_enable_interrupts(struct artpec6_pcie *artpec6_pcie)
  139. {
  140. struct pcie_port *pp = &artpec6_pcie->pp;
  141. if (IS_ENABLED(CONFIG_PCI_MSI))
  142. dw_pcie_msi_init(pp);
  143. }
  144. static void artpec6_pcie_host_init(struct pcie_port *pp)
  145. {
  146. struct artpec6_pcie *artpec6_pcie = to_artpec6_pcie(pp);
  147. artpec6_pcie_establish_link(artpec6_pcie);
  148. artpec6_pcie_enable_interrupts(artpec6_pcie);
  149. }
  150. static struct pcie_host_ops artpec6_pcie_host_ops = {
  151. .host_init = artpec6_pcie_host_init,
  152. };
  153. static irqreturn_t artpec6_pcie_msi_handler(int irq, void *arg)
  154. {
  155. struct artpec6_pcie *artpec6_pcie = arg;
  156. struct pcie_port *pp = &artpec6_pcie->pp;
  157. return dw_handle_msi_irq(pp);
  158. }
  159. static int artpec6_add_pcie_port(struct artpec6_pcie *artpec6_pcie,
  160. struct platform_device *pdev)
  161. {
  162. struct pcie_port *pp = &artpec6_pcie->pp;
  163. struct device *dev = pp->dev;
  164. int ret;
  165. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  166. pp->msi_irq = platform_get_irq_byname(pdev, "msi");
  167. if (pp->msi_irq <= 0) {
  168. dev_err(dev, "failed to get MSI irq\n");
  169. return -ENODEV;
  170. }
  171. ret = devm_request_irq(dev, pp->msi_irq,
  172. artpec6_pcie_msi_handler,
  173. IRQF_SHARED | IRQF_NO_THREAD,
  174. "artpec6-pcie-msi", artpec6_pcie);
  175. if (ret) {
  176. dev_err(dev, "failed to request MSI irq\n");
  177. return ret;
  178. }
  179. }
  180. pp->root_bus_nr = -1;
  181. pp->ops = &artpec6_pcie_host_ops;
  182. ret = dw_pcie_host_init(pp);
  183. if (ret) {
  184. dev_err(dev, "failed to initialize host\n");
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static int artpec6_pcie_probe(struct platform_device *pdev)
  190. {
  191. struct device *dev = &pdev->dev;
  192. struct artpec6_pcie *artpec6_pcie;
  193. struct pcie_port *pp;
  194. struct resource *dbi_base;
  195. struct resource *phy_base;
  196. int ret;
  197. artpec6_pcie = devm_kzalloc(dev, sizeof(*artpec6_pcie), GFP_KERNEL);
  198. if (!artpec6_pcie)
  199. return -ENOMEM;
  200. pp = &artpec6_pcie->pp;
  201. pp->dev = dev;
  202. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  203. pp->dbi_base = devm_ioremap_resource(dev, dbi_base);
  204. if (IS_ERR(pp->dbi_base))
  205. return PTR_ERR(pp->dbi_base);
  206. phy_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy");
  207. artpec6_pcie->phy_base = devm_ioremap_resource(dev, phy_base);
  208. if (IS_ERR(artpec6_pcie->phy_base))
  209. return PTR_ERR(artpec6_pcie->phy_base);
  210. artpec6_pcie->regmap =
  211. syscon_regmap_lookup_by_phandle(dev->of_node,
  212. "axis,syscon-pcie");
  213. if (IS_ERR(artpec6_pcie->regmap))
  214. return PTR_ERR(artpec6_pcie->regmap);
  215. ret = artpec6_add_pcie_port(artpec6_pcie, pdev);
  216. if (ret < 0)
  217. return ret;
  218. return 0;
  219. }
  220. static const struct of_device_id artpec6_pcie_of_match[] = {
  221. { .compatible = "axis,artpec6-pcie", },
  222. {},
  223. };
  224. static struct platform_driver artpec6_pcie_driver = {
  225. .probe = artpec6_pcie_probe,
  226. .driver = {
  227. .name = "artpec6-pcie",
  228. .of_match_table = artpec6_pcie_of_match,
  229. },
  230. };
  231. builtin_platform_driver(artpec6_pcie_driver);