pcie-armada8k.c 6.7 KB

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