pcie-spear13xx.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 <pratyush.anand@gmail.com>
  9. * Mohit Kumar <mohit.kumar.dhaka@gmail.com>
  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. bool is_gen1;
  27. };
  28. struct pcie_app_reg {
  29. u32 app_ctrl_0; /* cr0 */
  30. u32 app_ctrl_1; /* cr1 */
  31. u32 app_status_0; /* cr2 */
  32. u32 app_status_1; /* cr3 */
  33. u32 msg_status; /* cr4 */
  34. u32 msg_payload; /* cr5 */
  35. u32 int_sts; /* cr6 */
  36. u32 int_clr; /* cr7 */
  37. u32 int_mask; /* cr8 */
  38. u32 mst_bmisc; /* cr9 */
  39. u32 phy_ctrl; /* cr10 */
  40. u32 phy_status; /* cr11 */
  41. u32 cxpl_debug_info_0; /* cr12 */
  42. u32 cxpl_debug_info_1; /* cr13 */
  43. u32 ven_msg_ctrl_0; /* cr14 */
  44. u32 ven_msg_ctrl_1; /* cr15 */
  45. u32 ven_msg_data_0; /* cr16 */
  46. u32 ven_msg_data_1; /* cr17 */
  47. u32 ven_msi_0; /* cr18 */
  48. u32 ven_msi_1; /* cr19 */
  49. u32 mst_rmisc; /* cr20 */
  50. };
  51. /* CR0 ID */
  52. #define APP_LTSSM_ENABLE_ID 3
  53. #define DEVICE_TYPE_RC (4 << 25)
  54. #define MISCTRL_EN_ID 30
  55. #define REG_TRANSLATION_ENABLE 31
  56. /* CR3 ID */
  57. #define XMLH_LINK_UP (1 << 6)
  58. /* CR6 */
  59. #define MSI_CTRL_INT (1 << 26)
  60. #define EXP_CAP_ID_OFFSET 0x70
  61. #define to_spear13xx_pcie(x) dev_get_drvdata((x)->dev)
  62. static int spear13xx_pcie_establish_link(struct spear13xx_pcie *spear13xx_pcie)
  63. {
  64. struct dw_pcie *pci = spear13xx_pcie->pci;
  65. struct pcie_port *pp = &pci->pp;
  66. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  67. u32 val;
  68. u32 exp_cap_off = EXP_CAP_ID_OFFSET;
  69. if (dw_pcie_link_up(pci)) {
  70. dev_err(pci->dev, "link already up\n");
  71. return 0;
  72. }
  73. dw_pcie_setup_rc(pp);
  74. /*
  75. * this controller support only 128 bytes read size, however its
  76. * default value in capability register is 512 bytes. So force
  77. * it to 128 here.
  78. */
  79. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, &val);
  80. val &= ~PCI_EXP_DEVCTL_READRQ;
  81. dw_pcie_write(pci->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, val);
  82. dw_pcie_write(pci->dbi_base + PCI_VENDOR_ID, 2, 0x104A);
  83. dw_pcie_write(pci->dbi_base + PCI_DEVICE_ID, 2, 0xCD80);
  84. /*
  85. * if is_gen1 is set then handle it, so that some buggy card
  86. * also works
  87. */
  88. if (spear13xx_pcie->is_gen1) {
  89. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
  90. 4, &val);
  91. if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  92. val &= ~((u32)PCI_EXP_LNKCAP_SLS);
  93. val |= PCI_EXP_LNKCAP_SLS_2_5GB;
  94. dw_pcie_write(pci->dbi_base + exp_cap_off +
  95. PCI_EXP_LNKCAP, 4, val);
  96. }
  97. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
  98. 2, &val);
  99. if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  100. val &= ~((u32)PCI_EXP_LNKCAP_SLS);
  101. val |= PCI_EXP_LNKCAP_SLS_2_5GB;
  102. dw_pcie_write(pci->dbi_base + exp_cap_off +
  103. PCI_EXP_LNKCTL2, 2, val);
  104. }
  105. }
  106. /* enable ltssm */
  107. writel(DEVICE_TYPE_RC | (1 << MISCTRL_EN_ID)
  108. | (1 << APP_LTSSM_ENABLE_ID)
  109. | ((u32)1 << REG_TRANSLATION_ENABLE),
  110. &app_reg->app_ctrl_0);
  111. return dw_pcie_wait_for_link(pci);
  112. }
  113. static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)
  114. {
  115. struct spear13xx_pcie *spear13xx_pcie = arg;
  116. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  117. struct dw_pcie *pci = spear13xx_pcie->pci;
  118. struct pcie_port *pp = &pci->pp;
  119. unsigned int status;
  120. status = readl(&app_reg->int_sts);
  121. if (status & MSI_CTRL_INT) {
  122. BUG_ON(!IS_ENABLED(CONFIG_PCI_MSI));
  123. dw_handle_msi_irq(pp);
  124. }
  125. writel(status, &app_reg->int_clr);
  126. return IRQ_HANDLED;
  127. }
  128. static void spear13xx_pcie_enable_interrupts(struct spear13xx_pcie *spear13xx_pcie)
  129. {
  130. struct dw_pcie *pci = spear13xx_pcie->pci;
  131. struct pcie_port *pp = &pci->pp;
  132. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  133. /* Enable MSI interrupt */
  134. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  135. dw_pcie_msi_init(pp);
  136. writel(readl(&app_reg->int_mask) |
  137. MSI_CTRL_INT, &app_reg->int_mask);
  138. }
  139. }
  140. static int spear13xx_pcie_link_up(struct dw_pcie *pci)
  141. {
  142. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  143. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  144. if (readl(&app_reg->app_status_1) & XMLH_LINK_UP)
  145. return 1;
  146. return 0;
  147. }
  148. static int spear13xx_pcie_host_init(struct pcie_port *pp)
  149. {
  150. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  151. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  152. spear13xx_pcie_establish_link(spear13xx_pcie);
  153. spear13xx_pcie_enable_interrupts(spear13xx_pcie);
  154. return 0;
  155. }
  156. static const struct dw_pcie_host_ops spear13xx_pcie_host_ops = {
  157. .host_init = spear13xx_pcie_host_init,
  158. };
  159. static int spear13xx_add_pcie_port(struct spear13xx_pcie *spear13xx_pcie,
  160. struct platform_device *pdev)
  161. {
  162. struct dw_pcie *pci = spear13xx_pcie->pci;
  163. struct pcie_port *pp = &pci->pp;
  164. struct device *dev = &pdev->dev;
  165. int ret;
  166. pp->irq = platform_get_irq(pdev, 0);
  167. if (pp->irq < 0) {
  168. dev_err(dev, "failed to get irq\n");
  169. return pp->irq;
  170. }
  171. ret = devm_request_irq(dev, pp->irq, spear13xx_pcie_irq_handler,
  172. IRQF_SHARED | IRQF_NO_THREAD,
  173. "spear1340-pcie", spear13xx_pcie);
  174. if (ret) {
  175. dev_err(dev, "failed to request irq %d\n", pp->irq);
  176. return ret;
  177. }
  178. pp->ops = &spear13xx_pcie_host_ops;
  179. ret = dw_pcie_host_init(pp);
  180. if (ret) {
  181. dev_err(dev, "failed to initialize host\n");
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. static const struct dw_pcie_ops dw_pcie_ops = {
  187. .link_up = spear13xx_pcie_link_up,
  188. };
  189. static int spear13xx_pcie_probe(struct platform_device *pdev)
  190. {
  191. struct device *dev = &pdev->dev;
  192. struct dw_pcie *pci;
  193. struct spear13xx_pcie *spear13xx_pcie;
  194. struct device_node *np = dev->of_node;
  195. struct resource *dbi_base;
  196. int ret;
  197. spear13xx_pcie = devm_kzalloc(dev, sizeof(*spear13xx_pcie), GFP_KERNEL);
  198. if (!spear13xx_pcie)
  199. return -ENOMEM;
  200. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  201. if (!pci)
  202. return -ENOMEM;
  203. pci->dev = dev;
  204. pci->ops = &dw_pcie_ops;
  205. spear13xx_pcie->pci = pci;
  206. spear13xx_pcie->phy = devm_phy_get(dev, "pcie-phy");
  207. if (IS_ERR(spear13xx_pcie->phy)) {
  208. ret = PTR_ERR(spear13xx_pcie->phy);
  209. if (ret == -EPROBE_DEFER)
  210. dev_info(dev, "probe deferred\n");
  211. else
  212. dev_err(dev, "couldn't get pcie-phy\n");
  213. return ret;
  214. }
  215. phy_init(spear13xx_pcie->phy);
  216. spear13xx_pcie->clk = devm_clk_get(dev, NULL);
  217. if (IS_ERR(spear13xx_pcie->clk)) {
  218. dev_err(dev, "couldn't get clk for pcie\n");
  219. return PTR_ERR(spear13xx_pcie->clk);
  220. }
  221. ret = clk_prepare_enable(spear13xx_pcie->clk);
  222. if (ret) {
  223. dev_err(dev, "couldn't enable clk for pcie\n");
  224. return ret;
  225. }
  226. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  227. pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
  228. if (IS_ERR(pci->dbi_base)) {
  229. dev_err(dev, "couldn't remap dbi base %p\n", dbi_base);
  230. ret = PTR_ERR(pci->dbi_base);
  231. goto fail_clk;
  232. }
  233. spear13xx_pcie->app_base = pci->dbi_base + 0x2000;
  234. if (of_property_read_bool(np, "st,pcie-is-gen1"))
  235. spear13xx_pcie->is_gen1 = true;
  236. platform_set_drvdata(pdev, spear13xx_pcie);
  237. ret = spear13xx_add_pcie_port(spear13xx_pcie, pdev);
  238. if (ret < 0)
  239. goto fail_clk;
  240. return 0;
  241. fail_clk:
  242. clk_disable_unprepare(spear13xx_pcie->clk);
  243. return ret;
  244. }
  245. static const struct of_device_id spear13xx_pcie_of_match[] = {
  246. { .compatible = "st,spear1340-pcie", },
  247. {},
  248. };
  249. static struct platform_driver spear13xx_pcie_driver = {
  250. .probe = spear13xx_pcie_probe,
  251. .driver = {
  252. .name = "spear-pcie",
  253. .of_match_table = of_match_ptr(spear13xx_pcie_of_match),
  254. .suppress_bind_attrs = true,
  255. },
  256. };
  257. builtin_platform_driver(spear13xx_pcie_driver);