pcie-spear13xx.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 dw_pcie *pci;
  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) dev_get_drvdata((x)->dev)
  65. static int spear13xx_pcie_establish_link(struct spear13xx_pcie *spear13xx_pcie)
  66. {
  67. struct dw_pcie *pci = spear13xx_pcie->pci;
  68. struct pcie_port *pp = &pci->pp;
  69. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  70. u32 val;
  71. u32 exp_cap_off = EXP_CAP_ID_OFFSET;
  72. if (dw_pcie_link_up(pci)) {
  73. dev_err(pci->dev, "link already up\n");
  74. return 0;
  75. }
  76. dw_pcie_setup_rc(pp);
  77. /*
  78. * this controller support only 128 bytes read size, however its
  79. * default value in capability register is 512 bytes. So force
  80. * it to 128 here.
  81. */
  82. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, &val);
  83. val &= ~PCI_EXP_DEVCTL_READRQ;
  84. dw_pcie_write(pci->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, val);
  85. dw_pcie_write(pci->dbi_base + PCI_VENDOR_ID, 2, 0x104A);
  86. dw_pcie_write(pci->dbi_base + PCI_DEVICE_ID, 2, 0xCD80);
  87. /*
  88. * if is_gen1 is set then handle it, so that some buggy card
  89. * also works
  90. */
  91. if (spear13xx_pcie->is_gen1) {
  92. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
  93. 4, &val);
  94. if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  95. val &= ~((u32)PCI_EXP_LNKCAP_SLS);
  96. val |= PCI_EXP_LNKCAP_SLS_2_5GB;
  97. dw_pcie_write(pci->dbi_base + exp_cap_off +
  98. PCI_EXP_LNKCAP, 4, val);
  99. }
  100. dw_pcie_read(pci->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
  101. 2, &val);
  102. if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  103. val &= ~((u32)PCI_EXP_LNKCAP_SLS);
  104. val |= PCI_EXP_LNKCAP_SLS_2_5GB;
  105. dw_pcie_write(pci->dbi_base + exp_cap_off +
  106. PCI_EXP_LNKCTL2, 2, val);
  107. }
  108. }
  109. /* enable ltssm */
  110. writel(DEVICE_TYPE_RC | (1 << MISCTRL_EN_ID)
  111. | (1 << APP_LTSSM_ENABLE_ID)
  112. | ((u32)1 << REG_TRANSLATION_ENABLE),
  113. &app_reg->app_ctrl_0);
  114. return dw_pcie_wait_for_link(pci);
  115. }
  116. static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)
  117. {
  118. struct spear13xx_pcie *spear13xx_pcie = arg;
  119. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  120. struct dw_pcie *pci = spear13xx_pcie->pci;
  121. struct pcie_port *pp = &pci->pp;
  122. unsigned int status;
  123. status = readl(&app_reg->int_sts);
  124. if (status & MSI_CTRL_INT) {
  125. BUG_ON(!IS_ENABLED(CONFIG_PCI_MSI));
  126. dw_handle_msi_irq(pp);
  127. }
  128. writel(status, &app_reg->int_clr);
  129. return IRQ_HANDLED;
  130. }
  131. static void spear13xx_pcie_enable_interrupts(struct spear13xx_pcie *spear13xx_pcie)
  132. {
  133. struct dw_pcie *pci = spear13xx_pcie->pci;
  134. struct pcie_port *pp = &pci->pp;
  135. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  136. /* Enable MSI interrupt */
  137. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  138. dw_pcie_msi_init(pp);
  139. writel(readl(&app_reg->int_mask) |
  140. MSI_CTRL_INT, &app_reg->int_mask);
  141. }
  142. }
  143. static int spear13xx_pcie_link_up(struct dw_pcie *pci)
  144. {
  145. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  146. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  147. if (readl(&app_reg->app_status_1) & XMLH_LINK_UP)
  148. return 1;
  149. return 0;
  150. }
  151. static void spear13xx_pcie_host_init(struct pcie_port *pp)
  152. {
  153. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  154. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pci);
  155. spear13xx_pcie_establish_link(spear13xx_pcie);
  156. spear13xx_pcie_enable_interrupts(spear13xx_pcie);
  157. }
  158. static struct dw_pcie_host_ops spear13xx_pcie_host_ops = {
  159. .host_init = spear13xx_pcie_host_init,
  160. };
  161. static int spear13xx_add_pcie_port(struct spear13xx_pcie *spear13xx_pcie,
  162. struct platform_device *pdev)
  163. {
  164. struct dw_pcie *pci = spear13xx_pcie->pci;
  165. struct pcie_port *pp = &pci->pp;
  166. struct device *dev = &pdev->dev;
  167. int ret;
  168. pp->irq = platform_get_irq(pdev, 0);
  169. if (!pp->irq) {
  170. dev_err(dev, "failed to get irq\n");
  171. return -ENODEV;
  172. }
  173. ret = devm_request_irq(dev, pp->irq, spear13xx_pcie_irq_handler,
  174. IRQF_SHARED | IRQF_NO_THREAD,
  175. "spear1340-pcie", spear13xx_pcie);
  176. if (ret) {
  177. dev_err(dev, "failed to request irq %d\n", pp->irq);
  178. return ret;
  179. }
  180. pp->root_bus_nr = -1;
  181. pp->ops = &spear13xx_pcie_host_ops;
  182. ret = dw_pcie_host_init(pp);
  183. if (ret) {
  184. dev_err(dev, "failed to initialize host\n");
  185. return ret;
  186. }
  187. return 0;
  188. }
  189. static const struct dw_pcie_ops dw_pcie_ops = {
  190. .link_up = spear13xx_pcie_link_up,
  191. };
  192. static int spear13xx_pcie_probe(struct platform_device *pdev)
  193. {
  194. struct device *dev = &pdev->dev;
  195. struct dw_pcie *pci;
  196. struct spear13xx_pcie *spear13xx_pcie;
  197. struct device_node *np = dev->of_node;
  198. struct resource *dbi_base;
  199. int ret;
  200. spear13xx_pcie = devm_kzalloc(dev, sizeof(*spear13xx_pcie), GFP_KERNEL);
  201. if (!spear13xx_pcie)
  202. return -ENOMEM;
  203. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  204. if (!pci)
  205. return -ENOMEM;
  206. pci->dev = dev;
  207. pci->ops = &dw_pcie_ops;
  208. spear13xx_pcie->pci = pci;
  209. spear13xx_pcie->phy = devm_phy_get(dev, "pcie-phy");
  210. if (IS_ERR(spear13xx_pcie->phy)) {
  211. ret = PTR_ERR(spear13xx_pcie->phy);
  212. if (ret == -EPROBE_DEFER)
  213. dev_info(dev, "probe deferred\n");
  214. else
  215. dev_err(dev, "couldn't get pcie-phy\n");
  216. return ret;
  217. }
  218. phy_init(spear13xx_pcie->phy);
  219. spear13xx_pcie->clk = devm_clk_get(dev, NULL);
  220. if (IS_ERR(spear13xx_pcie->clk)) {
  221. dev_err(dev, "couldn't get clk for pcie\n");
  222. return PTR_ERR(spear13xx_pcie->clk);
  223. }
  224. ret = clk_prepare_enable(spear13xx_pcie->clk);
  225. if (ret) {
  226. dev_err(dev, "couldn't enable clk for pcie\n");
  227. return ret;
  228. }
  229. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  230. pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
  231. if (IS_ERR(pci->dbi_base)) {
  232. dev_err(dev, "couldn't remap dbi base %p\n", dbi_base);
  233. ret = PTR_ERR(pci->dbi_base);
  234. goto fail_clk;
  235. }
  236. spear13xx_pcie->app_base = pci->dbi_base + 0x2000;
  237. if (of_property_read_bool(np, "st,pcie-is-gen1"))
  238. spear13xx_pcie->is_gen1 = true;
  239. platform_set_drvdata(pdev, spear13xx_pcie);
  240. ret = spear13xx_add_pcie_port(spear13xx_pcie, pdev);
  241. if (ret < 0)
  242. goto fail_clk;
  243. return 0;
  244. fail_clk:
  245. clk_disable_unprepare(spear13xx_pcie->clk);
  246. return ret;
  247. }
  248. static const struct of_device_id spear13xx_pcie_of_match[] = {
  249. { .compatible = "st,spear1340-pcie", },
  250. {},
  251. };
  252. static struct platform_driver spear13xx_pcie_driver = {
  253. .probe = spear13xx_pcie_probe,
  254. .driver = {
  255. .name = "spear-pcie",
  256. .of_match_table = of_match_ptr(spear13xx_pcie_of_match),
  257. },
  258. };
  259. static int __init spear13xx_pcie_init(void)
  260. {
  261. return platform_driver_register(&spear13xx_pcie_driver);
  262. }
  263. device_initcall(spear13xx_pcie_init);