pcie-artpec6.c 7.7 KB

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