pcie-layerscape-gen4.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe Gen4 host controller driver for NXP Layerscape SoCs
  4. *
  5. * Copyright 2019-2020 NXP
  6. *
  7. * Author: Zhiqiang Hou <Zhiqiang.Hou@nxp.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/of_pci.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_address.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/resource.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/regmap.h>
  21. #include "pcie-mobiveil.h"
  22. /* LUT and PF control registers */
  23. #define PCIE_LUT_OFF 0x80000
  24. #define PCIE_PF_OFF 0xc0000
  25. #define PCIE_PF_INT_STAT 0x18
  26. #define PF_INT_STAT_PABRST BIT(31)
  27. #define PCIE_PF_DBG 0x7fc
  28. #define PF_DBG_LTSSM_MASK 0x3f
  29. #define PF_DBG_LTSSM_L0 0x2d /* L0 state */
  30. #define PF_DBG_WE BIT(31)
  31. #define PF_DBG_PABR BIT(27)
  32. #define to_ls_g4_pcie(x) platform_get_drvdata((x)->pdev)
  33. struct ls_g4_pcie {
  34. struct mobiveil_pcie pci;
  35. struct delayed_work dwork;
  36. int irq;
  37. };
  38. static inline u32 ls_g4_pcie_pf_readl(struct ls_g4_pcie *pcie, u32 off)
  39. {
  40. return ioread32(pcie->pci.csr_axi_slave_base + PCIE_PF_OFF + off);
  41. }
  42. static inline void ls_g4_pcie_pf_writel(struct ls_g4_pcie *pcie,
  43. u32 off, u32 val)
  44. {
  45. iowrite32(val, pcie->pci.csr_axi_slave_base + PCIE_PF_OFF + off);
  46. }
  47. static bool ls_g4_pcie_link_up(struct mobiveil_pcie *pci)
  48. {
  49. struct ls_g4_pcie *pcie = to_ls_g4_pcie(pci);
  50. u32 state;
  51. state = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
  52. return (state & PF_DBG_LTSSM_MASK) == PF_DBG_LTSSM_L0;
  53. }
  54. static void ls_g4_pcie_disable_interrupt(struct ls_g4_pcie *pcie)
  55. {
  56. struct mobiveil_pcie *mv_pci = &pcie->pci;
  57. mobiveil_csr_writel(mv_pci, 0, PAB_INTP_AMBA_MISC_ENB);
  58. }
  59. static void ls_g4_pcie_enable_interrupt(struct ls_g4_pcie *pcie)
  60. {
  61. struct mobiveil_pcie *mv_pci = &pcie->pci;
  62. u32 val;
  63. /* Clear the interrupt status */
  64. mobiveil_csr_writel(mv_pci, 0xffffffff, PAB_INTP_AMBA_MISC_STAT);
  65. val = PAB_INTP_INTX_MASK | PAB_INTP_MSI | PAB_INTP_RESET |
  66. PAB_INTP_PCIE_UE | PAB_INTP_IE_PMREDI | PAB_INTP_IE_EC;
  67. mobiveil_csr_writel(mv_pci, val, PAB_INTP_AMBA_MISC_ENB);
  68. }
  69. static int ls_g4_pcie_reinit_hw(struct ls_g4_pcie *pcie)
  70. {
  71. struct mobiveil_pcie *mv_pci = &pcie->pci;
  72. struct device *dev = &mv_pci->pdev->dev;
  73. u32 val, act_stat;
  74. int to = 100;
  75. /* Poll for pab_csb_reset to set and PAB activity to clear */
  76. do {
  77. usleep_range(10, 15);
  78. val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_INT_STAT);
  79. act_stat = mobiveil_csr_readl(mv_pci, PAB_ACTIVITY_STAT);
  80. } while (((val & PF_INT_STAT_PABRST) == 0 || act_stat) && to--);
  81. if (to < 0) {
  82. dev_err(dev, "Poll PABRST&PABACT timeout\n");
  83. return -EIO;
  84. }
  85. /* clear PEX_RESET bit in PEX_PF0_DBG register */
  86. val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
  87. val |= PF_DBG_WE;
  88. ls_g4_pcie_pf_writel(pcie, PCIE_PF_DBG, val);
  89. val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
  90. val |= PF_DBG_PABR;
  91. ls_g4_pcie_pf_writel(pcie, PCIE_PF_DBG, val);
  92. val = ls_g4_pcie_pf_readl(pcie, PCIE_PF_DBG);
  93. val &= ~PF_DBG_WE;
  94. ls_g4_pcie_pf_writel(pcie, PCIE_PF_DBG, val);
  95. mobiveil_host_init(mv_pci, true);
  96. to = 100;
  97. while (!ls_g4_pcie_link_up(mv_pci) && to--)
  98. usleep_range(200, 250);
  99. if (to < 0) {
  100. dev_err(dev, "PCIe link training timeout\n");
  101. return -EIO;
  102. }
  103. return 0;
  104. }
  105. static irqreturn_t ls_g4_pcie_isr(int irq, void *dev_id)
  106. {
  107. struct ls_g4_pcie *pcie = (struct ls_g4_pcie *)dev_id;
  108. struct mobiveil_pcie *mv_pci = &pcie->pci;
  109. u32 val;
  110. val = mobiveil_csr_readl(mv_pci, PAB_INTP_AMBA_MISC_STAT);
  111. if (!val)
  112. return IRQ_NONE;
  113. if (val & PAB_INTP_RESET) {
  114. ls_g4_pcie_disable_interrupt(pcie);
  115. schedule_delayed_work(&pcie->dwork, msecs_to_jiffies(1));
  116. }
  117. mobiveil_csr_writel(mv_pci, val, PAB_INTP_AMBA_MISC_STAT);
  118. return IRQ_HANDLED;
  119. }
  120. static int ls_g4_pcie_interrupt_init(struct mobiveil_pcie *mv_pci)
  121. {
  122. struct ls_g4_pcie *pcie = to_ls_g4_pcie(mv_pci);
  123. struct platform_device *pdev = mv_pci->pdev;
  124. struct device *dev = &pdev->dev;
  125. int ret;
  126. pcie->irq = platform_get_irq_byname(pdev, "intr");
  127. if (pcie->irq < 0)
  128. return pcie->irq;
  129. ret = devm_request_irq(dev, pcie->irq, ls_g4_pcie_isr,
  130. IRQF_SHARED, pdev->name, pcie);
  131. if (ret) {
  132. dev_err(dev, "Can't register PCIe IRQ, errno = %d\n", ret);
  133. return ret;
  134. }
  135. return 0;
  136. }
  137. static void ls_g4_pcie_reset(struct work_struct *work)
  138. {
  139. struct delayed_work *dwork = to_delayed_work(work);
  140. struct ls_g4_pcie *pcie = container_of(dwork, struct ls_g4_pcie, dwork);
  141. struct mobiveil_pcie *mv_pci = &pcie->pci;
  142. u16 ctrl;
  143. ctrl = mobiveil_csr_readw(mv_pci, PCI_BRIDGE_CONTROL);
  144. ctrl &= ~PCI_BRIDGE_CTL_BUS_RESET;
  145. mobiveil_csr_writew(mv_pci, ctrl, PCI_BRIDGE_CONTROL);
  146. if (!ls_g4_pcie_reinit_hw(pcie))
  147. return;
  148. ls_g4_pcie_enable_interrupt(pcie);
  149. }
  150. static const struct mobiveil_rp_ops ls_g4_pcie_rp_ops = {
  151. .interrupt_init = ls_g4_pcie_interrupt_init,
  152. };
  153. static const struct mobiveil_pab_ops ls_g4_pcie_pab_ops = {
  154. .link_up = ls_g4_pcie_link_up,
  155. };
  156. static int __init ls_g4_pcie_probe(struct platform_device *pdev)
  157. {
  158. struct device *dev = &pdev->dev;
  159. struct pci_host_bridge *bridge;
  160. struct mobiveil_pcie *mv_pci;
  161. struct ls_g4_pcie *pcie;
  162. struct device_node *np = dev->of_node;
  163. int ret;
  164. if (!of_parse_phandle(np, "msi-parent", 0)) {
  165. dev_err(dev, "Failed to find msi-parent\n");
  166. return -EINVAL;
  167. }
  168. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  169. if (!bridge)
  170. return -ENOMEM;
  171. pcie = pci_host_bridge_priv(bridge);
  172. mv_pci = &pcie->pci;
  173. mv_pci->pdev = pdev;
  174. mv_pci->ops = &ls_g4_pcie_pab_ops;
  175. mv_pci->rp.ops = &ls_g4_pcie_rp_ops;
  176. mv_pci->rp.bridge = bridge;
  177. platform_set_drvdata(pdev, pcie);
  178. INIT_DELAYED_WORK(&pcie->dwork, ls_g4_pcie_reset);
  179. ret = mobiveil_pcie_host_probe(mv_pci);
  180. if (ret) {
  181. dev_err(dev, "Fail to probe\n");
  182. return ret;
  183. }
  184. ls_g4_pcie_enable_interrupt(pcie);
  185. return 0;
  186. }
  187. static const struct of_device_id ls_g4_pcie_of_match[] = {
  188. { .compatible = "fsl,lx2160a-pcie", },
  189. { },
  190. };
  191. static struct platform_driver ls_g4_pcie_driver = {
  192. .driver = {
  193. .name = "layerscape-pcie-gen4",
  194. .of_match_table = ls_g4_pcie_of_match,
  195. .suppress_bind_attrs = true,
  196. },
  197. };
  198. builtin_platform_driver_probe(ls_g4_pcie_driver, ls_g4_pcie_probe);