pcie-spear13xx.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for ST Microelectronics SPEAr13xx SoCs
  4. *
  5. * SPEAr13xx PCIe Glue Layer Source Code
  6. *
  7. * Copyright (C) 2010-2014 ST Microelectronics
  8. * Pratyush Anand <[email protected]>
  9. * Mohit Kumar <[email protected]>
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/of.h>
  16. #include <linux/pci.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/resource.h>
  20. #include "pcie-designware.h"
  21. struct spear13xx_pcie {
  22. struct dw_pcie *pci;
  23. void __iomem *app_base;
  24. struct phy *phy;
  25. struct clk *clk;
  26. };
  27. struct pcie_app_reg {
  28. u32 app_ctrl_0; /* cr0 */
  29. u32 app_ctrl_1; /* cr1 */
  30. u32 app_status_0; /* cr2 */
  31. u32 app_status_1; /* cr3 */
  32. u32 msg_status; /* cr4 */
  33. u32 msg_payload; /* cr5 */
  34. u32 int_sts; /* cr6 */
  35. u32 int_clr; /* cr7 */
  36. u32 int_mask; /* cr8 */
  37. u32 mst_bmisc; /* cr9 */
  38. u32 phy_ctrl; /* cr10 */
  39. u32 phy_status; /* cr11 */
  40. u32 cxpl_debug_info_0; /* cr12 */
  41. u32 cxpl_debug_info_1; /* cr13 */
  42. u32 ven_msg_ctrl_0; /* cr14 */
  43. u32 ven_msg_ctrl_1; /* cr15 */
  44. u32 ven_msg_data_0; /* cr16 */
  45. u32 ven_msg_data_1; /* cr17 */
  46. u32 ven_msi_0; /* cr18 */
  47. u32 ven_msi_1; /* cr19 */
  48. u32 mst_rmisc; /* cr20 */
  49. };
  50. /* CR0 ID */
  51. #define APP_LTSSM_ENABLE_ID 3
  52. #define DEVICE_TYPE_RC (4 << 25)
  53. #define MISCTRL_EN_ID 30
  54. #define REG_TRANSLATION_ENABLE 31
  55. /* CR3 ID */
  56. #define XMLH_LINK_UP (1 << 6)
  57. /* CR6 */
  58. #define MSI_CTRL_INT (1 << 26)
  59. #define to_spear13xx_pcie(x) dev_get_drvdata((x)->dev)
  60. static int spear13xx_pcie_start_link(struct dw_pcie *pci)
  61. {
  62. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  63. struct pcie_app_reg __iomem *app_reg = spear13xx_pcie->app_base;
  64. /* enable ltssm */
  65. writel(DEVICE_TYPE_RC | (1 << MISCTRL_EN_ID)
  66. | (1 << APP_LTSSM_ENABLE_ID)
  67. | ((u32)1 << REG_TRANSLATION_ENABLE),
  68. &app_reg->app_ctrl_0);
  69. return 0;
  70. }
  71. static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)
  72. {
  73. struct spear13xx_pcie *spear13xx_pcie = arg;
  74. struct pcie_app_reg __iomem *app_reg = spear13xx_pcie->app_base;
  75. struct dw_pcie *pci = spear13xx_pcie->pci;
  76. struct dw_pcie_rp *pp = &pci->pp;
  77. unsigned int status;
  78. status = readl(&app_reg->int_sts);
  79. if (status & MSI_CTRL_INT) {
  80. BUG_ON(!IS_ENABLED(CONFIG_PCI_MSI));
  81. dw_handle_msi_irq(pp);
  82. }
  83. writel(status, &app_reg->int_clr);
  84. return IRQ_HANDLED;
  85. }
  86. static void spear13xx_pcie_enable_interrupts(struct spear13xx_pcie *spear13xx_pcie)
  87. {
  88. struct pcie_app_reg __iomem *app_reg = spear13xx_pcie->app_base;
  89. /* Enable MSI interrupt */
  90. if (IS_ENABLED(CONFIG_PCI_MSI))
  91. writel(readl(&app_reg->int_mask) |
  92. MSI_CTRL_INT, &app_reg->int_mask);
  93. }
  94. static int spear13xx_pcie_link_up(struct dw_pcie *pci)
  95. {
  96. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  97. struct pcie_app_reg __iomem *app_reg = spear13xx_pcie->app_base;
  98. if (readl(&app_reg->app_status_1) & XMLH_LINK_UP)
  99. return 1;
  100. return 0;
  101. }
  102. static int spear13xx_pcie_host_init(struct dw_pcie_rp *pp)
  103. {
  104. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  105. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  106. u32 exp_cap_off = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
  107. u32 val;
  108. spear13xx_pcie->app_base = pci->dbi_base + 0x2000;
  109. /*
  110. * this controller support only 128 bytes read size, however its
  111. * default value in capability register is 512 bytes. So force
  112. * it to 128 here.
  113. */
  114. val = dw_pcie_readw_dbi(pci, exp_cap_off + PCI_EXP_DEVCTL);
  115. val &= ~PCI_EXP_DEVCTL_READRQ;
  116. dw_pcie_writew_dbi(pci, exp_cap_off + PCI_EXP_DEVCTL, val);
  117. dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, 0x104A);
  118. dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, 0xCD80);
  119. spear13xx_pcie_enable_interrupts(spear13xx_pcie);
  120. return 0;
  121. }
  122. static const struct dw_pcie_host_ops spear13xx_pcie_host_ops = {
  123. .host_init = spear13xx_pcie_host_init,
  124. };
  125. static int spear13xx_add_pcie_port(struct spear13xx_pcie *spear13xx_pcie,
  126. struct platform_device *pdev)
  127. {
  128. struct dw_pcie *pci = spear13xx_pcie->pci;
  129. struct dw_pcie_rp *pp = &pci->pp;
  130. struct device *dev = &pdev->dev;
  131. int ret;
  132. pp->irq = platform_get_irq(pdev, 0);
  133. if (pp->irq < 0)
  134. return pp->irq;
  135. ret = devm_request_irq(dev, pp->irq, spear13xx_pcie_irq_handler,
  136. IRQF_SHARED | IRQF_NO_THREAD,
  137. "spear1340-pcie", spear13xx_pcie);
  138. if (ret) {
  139. dev_err(dev, "failed to request irq %d\n", pp->irq);
  140. return ret;
  141. }
  142. pp->ops = &spear13xx_pcie_host_ops;
  143. pp->msi_irq[0] = -ENODEV;
  144. ret = dw_pcie_host_init(pp);
  145. if (ret) {
  146. dev_err(dev, "failed to initialize host\n");
  147. return ret;
  148. }
  149. return 0;
  150. }
  151. static const struct dw_pcie_ops dw_pcie_ops = {
  152. .link_up = spear13xx_pcie_link_up,
  153. .start_link = spear13xx_pcie_start_link,
  154. };
  155. static int spear13xx_pcie_probe(struct platform_device *pdev)
  156. {
  157. struct device *dev = &pdev->dev;
  158. struct dw_pcie *pci;
  159. struct spear13xx_pcie *spear13xx_pcie;
  160. struct device_node *np = dev->of_node;
  161. int ret;
  162. spear13xx_pcie = devm_kzalloc(dev, sizeof(*spear13xx_pcie), GFP_KERNEL);
  163. if (!spear13xx_pcie)
  164. return -ENOMEM;
  165. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  166. if (!pci)
  167. return -ENOMEM;
  168. pci->dev = dev;
  169. pci->ops = &dw_pcie_ops;
  170. spear13xx_pcie->pci = pci;
  171. spear13xx_pcie->phy = devm_phy_get(dev, "pcie-phy");
  172. if (IS_ERR(spear13xx_pcie->phy)) {
  173. ret = PTR_ERR(spear13xx_pcie->phy);
  174. if (ret == -EPROBE_DEFER)
  175. dev_info(dev, "probe deferred\n");
  176. else
  177. dev_err(dev, "couldn't get pcie-phy\n");
  178. return ret;
  179. }
  180. phy_init(spear13xx_pcie->phy);
  181. spear13xx_pcie->clk = devm_clk_get(dev, NULL);
  182. if (IS_ERR(spear13xx_pcie->clk)) {
  183. dev_err(dev, "couldn't get clk for pcie\n");
  184. return PTR_ERR(spear13xx_pcie->clk);
  185. }
  186. ret = clk_prepare_enable(spear13xx_pcie->clk);
  187. if (ret) {
  188. dev_err(dev, "couldn't enable clk for pcie\n");
  189. return ret;
  190. }
  191. if (of_property_read_bool(np, "st,pcie-is-gen1"))
  192. pci->link_gen = 1;
  193. platform_set_drvdata(pdev, spear13xx_pcie);
  194. ret = spear13xx_add_pcie_port(spear13xx_pcie, pdev);
  195. if (ret < 0)
  196. goto fail_clk;
  197. return 0;
  198. fail_clk:
  199. clk_disable_unprepare(spear13xx_pcie->clk);
  200. return ret;
  201. }
  202. static const struct of_device_id spear13xx_pcie_of_match[] = {
  203. { .compatible = "st,spear1340-pcie", },
  204. {},
  205. };
  206. static struct platform_driver spear13xx_pcie_driver = {
  207. .probe = spear13xx_pcie_probe,
  208. .driver = {
  209. .name = "spear-pcie",
  210. .of_match_table = spear13xx_pcie_of_match,
  211. .suppress_bind_attrs = true,
  212. },
  213. };
  214. builtin_platform_driver(spear13xx_pcie_driver);