pcie-spear13xx.c 7.6 KB

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