pcie-armada8k.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * PCIe host controller driver for Marvell Armada-8K SoCs
  3. *
  4. * Armada-8K PCIe Glue Layer Source Code
  5. *
  6. * Copyright (C) 2016 Marvell Technology Group Ltd.
  7. *
  8. * Author: Yehuda Yitshak <yehuday@marvell.com>
  9. * Author: Shadi Ammouri <shadi@marvell.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/pci.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/resource.h>
  25. #include <linux/of_pci.h>
  26. #include <linux/of_irq.h>
  27. #include "pcie-designware.h"
  28. struct armada8k_pcie {
  29. struct pcie_port pp; /* pp.dbi_base is DT ctrl */
  30. struct clk *clk;
  31. };
  32. #define PCIE_VENDOR_REGS_OFFSET 0x8000
  33. #define PCIE_GLOBAL_CONTROL_REG (PCIE_VENDOR_REGS_OFFSET + 0x0)
  34. #define PCIE_APP_LTSSM_EN BIT(2)
  35. #define PCIE_DEVICE_TYPE_SHIFT 4
  36. #define PCIE_DEVICE_TYPE_MASK 0xF
  37. #define PCIE_DEVICE_TYPE_RC 0x4 /* Root complex */
  38. #define PCIE_GLOBAL_STATUS_REG (PCIE_VENDOR_REGS_OFFSET + 0x8)
  39. #define PCIE_GLB_STS_RDLH_LINK_UP BIT(1)
  40. #define PCIE_GLB_STS_PHY_LINK_UP BIT(9)
  41. #define PCIE_GLOBAL_INT_CAUSE1_REG (PCIE_VENDOR_REGS_OFFSET + 0x1C)
  42. #define PCIE_GLOBAL_INT_MASK1_REG (PCIE_VENDOR_REGS_OFFSET + 0x20)
  43. #define PCIE_INT_A_ASSERT_MASK BIT(9)
  44. #define PCIE_INT_B_ASSERT_MASK BIT(10)
  45. #define PCIE_INT_C_ASSERT_MASK BIT(11)
  46. #define PCIE_INT_D_ASSERT_MASK BIT(12)
  47. #define PCIE_ARCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x50)
  48. #define PCIE_AWCACHE_TRC_REG (PCIE_VENDOR_REGS_OFFSET + 0x54)
  49. #define PCIE_ARUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x5C)
  50. #define PCIE_AWUSER_REG (PCIE_VENDOR_REGS_OFFSET + 0x60)
  51. /*
  52. * AR/AW Cache defauls: Normal memory, Write-Back, Read / Write
  53. * allocate
  54. */
  55. #define ARCACHE_DEFAULT_VALUE 0x3511
  56. #define AWCACHE_DEFAULT_VALUE 0x5311
  57. #define DOMAIN_OUTER_SHAREABLE 0x2
  58. #define AX_USER_DOMAIN_MASK 0x3
  59. #define AX_USER_DOMAIN_SHIFT 4
  60. #define to_armada8k_pcie(x) container_of(x, struct armada8k_pcie, pp)
  61. static int armada8k_pcie_link_up(struct pcie_port *pp)
  62. {
  63. u32 reg;
  64. u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
  65. reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_STATUS_REG);
  66. if ((reg & mask) == mask)
  67. return 1;
  68. dev_dbg(pp->dev, "No link detected (Global-Status: 0x%08x).\n", reg);
  69. return 0;
  70. }
  71. static void armada8k_pcie_establish_link(struct armada8k_pcie *pcie)
  72. {
  73. struct pcie_port *pp = &pcie->pp;
  74. u32 reg;
  75. if (!dw_pcie_link_up(pp)) {
  76. /* Disable LTSSM state machine to enable configuration */
  77. reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
  78. reg &= ~(PCIE_APP_LTSSM_EN);
  79. dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
  80. }
  81. /* Set the device to root complex mode */
  82. reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
  83. reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT);
  84. reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT;
  85. dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
  86. /* Set the PCIe master AxCache attributes */
  87. dw_pcie_writel_rc(pp, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE);
  88. dw_pcie_writel_rc(pp, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE);
  89. /* Set the PCIe master AxDomain attributes */
  90. reg = dw_pcie_readl_rc(pp, PCIE_ARUSER_REG);
  91. reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
  92. reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
  93. dw_pcie_writel_rc(pp, PCIE_ARUSER_REG, reg);
  94. reg = dw_pcie_readl_rc(pp, PCIE_AWUSER_REG);
  95. reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
  96. reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
  97. dw_pcie_writel_rc(pp, PCIE_AWUSER_REG, reg);
  98. /* Enable INT A-D interrupts */
  99. reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_MASK1_REG);
  100. reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK |
  101. PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK;
  102. dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_MASK1_REG, reg);
  103. if (!dw_pcie_link_up(pp)) {
  104. /* Configuration done. Start LTSSM */
  105. reg = dw_pcie_readl_rc(pp, PCIE_GLOBAL_CONTROL_REG);
  106. reg |= PCIE_APP_LTSSM_EN;
  107. dw_pcie_writel_rc(pp, PCIE_GLOBAL_CONTROL_REG, reg);
  108. }
  109. /* Wait until the link becomes active again */
  110. if (dw_pcie_wait_for_link(pp))
  111. dev_err(pp->dev, "Link not up after reconfiguration\n");
  112. }
  113. static void armada8k_pcie_host_init(struct pcie_port *pp)
  114. {
  115. struct armada8k_pcie *pcie = to_armada8k_pcie(pp);
  116. dw_pcie_setup_rc(pp);
  117. armada8k_pcie_establish_link(pcie);
  118. }
  119. static irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg)
  120. {
  121. struct armada8k_pcie *pcie = arg;
  122. struct pcie_port *pp = &pcie->pp;
  123. u32 val;
  124. /*
  125. * Interrupts are directly handled by the device driver of the
  126. * PCI device. However, they are also latched into the PCIe
  127. * controller, so we simply discard them.
  128. */
  129. val = dw_pcie_readl_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG);
  130. dw_pcie_writel_rc(pp, PCIE_GLOBAL_INT_CAUSE1_REG, val);
  131. return IRQ_HANDLED;
  132. }
  133. static struct pcie_host_ops armada8k_pcie_host_ops = {
  134. .link_up = armada8k_pcie_link_up,
  135. .host_init = armada8k_pcie_host_init,
  136. };
  137. static int armada8k_add_pcie_port(struct armada8k_pcie *pcie,
  138. struct platform_device *pdev)
  139. {
  140. struct pcie_port *pp = &pcie->pp;
  141. struct device *dev = &pdev->dev;
  142. int ret;
  143. pp->root_bus_nr = -1;
  144. pp->ops = &armada8k_pcie_host_ops;
  145. pp->irq = platform_get_irq(pdev, 0);
  146. if (!pp->irq) {
  147. dev_err(dev, "failed to get irq for port\n");
  148. return -ENODEV;
  149. }
  150. ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler,
  151. IRQF_SHARED, "armada8k-pcie", pcie);
  152. if (ret) {
  153. dev_err(dev, "failed to request irq %d\n", pp->irq);
  154. return ret;
  155. }
  156. ret = dw_pcie_host_init(pp);
  157. if (ret) {
  158. dev_err(dev, "failed to initialize host: %d\n", ret);
  159. return ret;
  160. }
  161. return 0;
  162. }
  163. static int armada8k_pcie_probe(struct platform_device *pdev)
  164. {
  165. struct armada8k_pcie *pcie;
  166. struct pcie_port *pp;
  167. struct device *dev = &pdev->dev;
  168. struct resource *base;
  169. int ret;
  170. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  171. if (!pcie)
  172. return -ENOMEM;
  173. pcie->clk = devm_clk_get(dev, NULL);
  174. if (IS_ERR(pcie->clk))
  175. return PTR_ERR(pcie->clk);
  176. clk_prepare_enable(pcie->clk);
  177. pp = &pcie->pp;
  178. pp->dev = dev;
  179. /* Get the dw-pcie unit configuration/control registers base. */
  180. base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl");
  181. pp->dbi_base = devm_ioremap_resource(dev, base);
  182. if (IS_ERR(pp->dbi_base)) {
  183. dev_err(dev, "couldn't remap regs base %p\n", base);
  184. ret = PTR_ERR(pp->dbi_base);
  185. goto fail;
  186. }
  187. ret = armada8k_add_pcie_port(pcie, pdev);
  188. if (ret)
  189. goto fail;
  190. return 0;
  191. fail:
  192. if (!IS_ERR(pcie->clk))
  193. clk_disable_unprepare(pcie->clk);
  194. return ret;
  195. }
  196. static const struct of_device_id armada8k_pcie_of_match[] = {
  197. { .compatible = "marvell,armada8k-pcie", },
  198. {},
  199. };
  200. static struct platform_driver armada8k_pcie_driver = {
  201. .probe = armada8k_pcie_probe,
  202. .driver = {
  203. .name = "armada8k-pcie",
  204. .of_match_table = of_match_ptr(armada8k_pcie_of_match),
  205. },
  206. };
  207. builtin_platform_driver(armada8k_pcie_driver);