pcie-spear13xx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/module.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. void __iomem *app_base;
  26. struct phy *phy;
  27. struct clk *clk;
  28. struct pcie_port pp;
  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 RX_LANE_FLIP_EN_ID 0
  56. #define TX_LANE_FLIP_EN_ID 1
  57. #define SYS_AUX_PWR_DET_ID 2
  58. #define APP_LTSSM_ENABLE_ID 3
  59. #define SYS_ATTEN_BUTTON_PRESSED_ID 4
  60. #define SYS_MRL_SENSOR_STATE_ID 5
  61. #define SYS_PWR_FAULT_DET_ID 6
  62. #define SYS_MRL_SENSOR_CHGED_ID 7
  63. #define SYS_PRE_DET_CHGED_ID 8
  64. #define SYS_CMD_CPLED_INT_ID 9
  65. #define APP_INIT_RST_0_ID 11
  66. #define APP_REQ_ENTR_L1_ID 12
  67. #define APP_READY_ENTR_L23_ID 13
  68. #define APP_REQ_EXIT_L1_ID 14
  69. #define DEVICE_TYPE_EP (0 << 25)
  70. #define DEVICE_TYPE_LEP (1 << 25)
  71. #define DEVICE_TYPE_RC (4 << 25)
  72. #define SYS_INT_ID 29
  73. #define MISCTRL_EN_ID 30
  74. #define REG_TRANSLATION_ENABLE 31
  75. /* CR1 ID */
  76. #define APPS_PM_XMT_TURNOFF_ID 2
  77. #define APPS_PM_XMT_PME_ID 5
  78. /* CR3 ID */
  79. #define XMLH_LTSSM_STATE_DETECT_QUIET 0x00
  80. #define XMLH_LTSSM_STATE_DETECT_ACT 0x01
  81. #define XMLH_LTSSM_STATE_POLL_ACTIVE 0x02
  82. #define XMLH_LTSSM_STATE_POLL_COMPLIANCE 0x03
  83. #define XMLH_LTSSM_STATE_POLL_CONFIG 0x04
  84. #define XMLH_LTSSM_STATE_PRE_DETECT_QUIET 0x05
  85. #define XMLH_LTSSM_STATE_DETECT_WAIT 0x06
  86. #define XMLH_LTSSM_STATE_CFG_LINKWD_START 0x07
  87. #define XMLH_LTSSM_STATE_CFG_LINKWD_ACEPT 0x08
  88. #define XMLH_LTSSM_STATE_CFG_LANENUM_WAIT 0x09
  89. #define XMLH_LTSSM_STATE_CFG_LANENUM_ACEPT 0x0A
  90. #define XMLH_LTSSM_STATE_CFG_COMPLETE 0x0B
  91. #define XMLH_LTSSM_STATE_CFG_IDLE 0x0C
  92. #define XMLH_LTSSM_STATE_RCVRY_LOCK 0x0D
  93. #define XMLH_LTSSM_STATE_RCVRY_SPEED 0x0E
  94. #define XMLH_LTSSM_STATE_RCVRY_RCVRCFG 0x0F
  95. #define XMLH_LTSSM_STATE_RCVRY_IDLE 0x10
  96. #define XMLH_LTSSM_STATE_L0 0x11
  97. #define XMLH_LTSSM_STATE_L0S 0x12
  98. #define XMLH_LTSSM_STATE_L123_SEND_EIDLE 0x13
  99. #define XMLH_LTSSM_STATE_L1_IDLE 0x14
  100. #define XMLH_LTSSM_STATE_L2_IDLE 0x15
  101. #define XMLH_LTSSM_STATE_L2_WAKE 0x16
  102. #define XMLH_LTSSM_STATE_DISABLED_ENTRY 0x17
  103. #define XMLH_LTSSM_STATE_DISABLED_IDLE 0x18
  104. #define XMLH_LTSSM_STATE_DISABLED 0x19
  105. #define XMLH_LTSSM_STATE_LPBK_ENTRY 0x1A
  106. #define XMLH_LTSSM_STATE_LPBK_ACTIVE 0x1B
  107. #define XMLH_LTSSM_STATE_LPBK_EXIT 0x1C
  108. #define XMLH_LTSSM_STATE_LPBK_EXIT_TIMEOUT 0x1D
  109. #define XMLH_LTSSM_STATE_HOT_RESET_ENTRY 0x1E
  110. #define XMLH_LTSSM_STATE_HOT_RESET 0x1F
  111. #define XMLH_LTSSM_STATE_MASK 0x3F
  112. #define XMLH_LINK_UP (1 << 6)
  113. /* CR4 ID */
  114. #define CFG_MSI_EN_ID 18
  115. /* CR6 */
  116. #define INTA_CTRL_INT (1 << 7)
  117. #define INTB_CTRL_INT (1 << 8)
  118. #define INTC_CTRL_INT (1 << 9)
  119. #define INTD_CTRL_INT (1 << 10)
  120. #define MSI_CTRL_INT (1 << 26)
  121. /* CR19 ID */
  122. #define VEN_MSI_REQ_ID 11
  123. #define VEN_MSI_FUN_NUM_ID 8
  124. #define VEN_MSI_TC_ID 5
  125. #define VEN_MSI_VECTOR_ID 0
  126. #define VEN_MSI_REQ_EN ((u32)0x1 << VEN_MSI_REQ_ID)
  127. #define VEN_MSI_FUN_NUM_MASK ((u32)0x7 << VEN_MSI_FUN_NUM_ID)
  128. #define VEN_MSI_TC_MASK ((u32)0x7 << VEN_MSI_TC_ID)
  129. #define VEN_MSI_VECTOR_MASK ((u32)0x1F << VEN_MSI_VECTOR_ID)
  130. #define EXP_CAP_ID_OFFSET 0x70
  131. #define to_spear13xx_pcie(x) container_of(x, struct spear13xx_pcie, pp)
  132. static int spear13xx_pcie_establish_link(struct pcie_port *pp)
  133. {
  134. u32 val;
  135. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pp);
  136. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  137. u32 exp_cap_off = EXP_CAP_ID_OFFSET;
  138. if (dw_pcie_link_up(pp)) {
  139. dev_err(pp->dev, "link already up\n");
  140. return 0;
  141. }
  142. dw_pcie_setup_rc(pp);
  143. /*
  144. * this controller support only 128 bytes read size, however its
  145. * default value in capability register is 512 bytes. So force
  146. * it to 128 here.
  147. */
  148. dw_pcie_cfg_read(pp->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, &val);
  149. val &= ~PCI_EXP_DEVCTL_READRQ;
  150. dw_pcie_cfg_write(pp->dbi_base + exp_cap_off + PCI_EXP_DEVCTL, 2, val);
  151. dw_pcie_cfg_write(pp->dbi_base + PCI_VENDOR_ID, 2, 0x104A);
  152. dw_pcie_cfg_write(pp->dbi_base + PCI_DEVICE_ID, 2, 0xCD80);
  153. /*
  154. * if is_gen1 is set then handle it, so that some buggy card
  155. * also works
  156. */
  157. if (spear13xx_pcie->is_gen1) {
  158. dw_pcie_cfg_read(pp->dbi_base + exp_cap_off + PCI_EXP_LNKCAP,
  159. 4, &val);
  160. if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  161. val &= ~((u32)PCI_EXP_LNKCAP_SLS);
  162. val |= PCI_EXP_LNKCAP_SLS_2_5GB;
  163. dw_pcie_cfg_write(pp->dbi_base + exp_cap_off +
  164. PCI_EXP_LNKCAP, 4, val);
  165. }
  166. dw_pcie_cfg_read(pp->dbi_base + exp_cap_off + PCI_EXP_LNKCTL2,
  167. 2, &val);
  168. if ((val & PCI_EXP_LNKCAP_SLS) != PCI_EXP_LNKCAP_SLS_2_5GB) {
  169. val &= ~((u32)PCI_EXP_LNKCAP_SLS);
  170. val |= PCI_EXP_LNKCAP_SLS_2_5GB;
  171. dw_pcie_cfg_write(pp->dbi_base + exp_cap_off +
  172. PCI_EXP_LNKCTL2, 2, val);
  173. }
  174. }
  175. /* enable ltssm */
  176. writel(DEVICE_TYPE_RC | (1 << MISCTRL_EN_ID)
  177. | (1 << APP_LTSSM_ENABLE_ID)
  178. | ((u32)1 << REG_TRANSLATION_ENABLE),
  179. &app_reg->app_ctrl_0);
  180. return dw_pcie_wait_for_link(pp);
  181. }
  182. static irqreturn_t spear13xx_pcie_irq_handler(int irq, void *arg)
  183. {
  184. struct pcie_port *pp = arg;
  185. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pp);
  186. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  187. unsigned int status;
  188. status = readl(&app_reg->int_sts);
  189. if (status & MSI_CTRL_INT) {
  190. BUG_ON(!IS_ENABLED(CONFIG_PCI_MSI));
  191. dw_handle_msi_irq(pp);
  192. }
  193. writel(status, &app_reg->int_clr);
  194. return IRQ_HANDLED;
  195. }
  196. static void spear13xx_pcie_enable_interrupts(struct pcie_port *pp)
  197. {
  198. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pp);
  199. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  200. /* Enable MSI interrupt */
  201. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  202. dw_pcie_msi_init(pp);
  203. writel(readl(&app_reg->int_mask) |
  204. MSI_CTRL_INT, &app_reg->int_mask);
  205. }
  206. }
  207. static int spear13xx_pcie_link_up(struct pcie_port *pp)
  208. {
  209. struct spear13xx_pcie *spear13xx_pcie = to_spear13xx_pcie(pp);
  210. struct pcie_app_reg *app_reg = spear13xx_pcie->app_base;
  211. if (readl(&app_reg->app_status_1) & XMLH_LINK_UP)
  212. return 1;
  213. return 0;
  214. }
  215. static void spear13xx_pcie_host_init(struct pcie_port *pp)
  216. {
  217. spear13xx_pcie_establish_link(pp);
  218. spear13xx_pcie_enable_interrupts(pp);
  219. }
  220. static struct pcie_host_ops spear13xx_pcie_host_ops = {
  221. .link_up = spear13xx_pcie_link_up,
  222. .host_init = spear13xx_pcie_host_init,
  223. };
  224. static int spear13xx_add_pcie_port(struct pcie_port *pp,
  225. struct platform_device *pdev)
  226. {
  227. struct device *dev = &pdev->dev;
  228. int ret;
  229. pp->irq = platform_get_irq(pdev, 0);
  230. if (!pp->irq) {
  231. dev_err(dev, "failed to get irq\n");
  232. return -ENODEV;
  233. }
  234. ret = devm_request_irq(dev, pp->irq, spear13xx_pcie_irq_handler,
  235. IRQF_SHARED | IRQF_NO_THREAD,
  236. "spear1340-pcie", pp);
  237. if (ret) {
  238. dev_err(dev, "failed to request irq %d\n", pp->irq);
  239. return ret;
  240. }
  241. pp->root_bus_nr = -1;
  242. pp->ops = &spear13xx_pcie_host_ops;
  243. ret = dw_pcie_host_init(pp);
  244. if (ret) {
  245. dev_err(dev, "failed to initialize host\n");
  246. return ret;
  247. }
  248. return 0;
  249. }
  250. static int spear13xx_pcie_probe(struct platform_device *pdev)
  251. {
  252. struct spear13xx_pcie *spear13xx_pcie;
  253. struct pcie_port *pp;
  254. struct device *dev = &pdev->dev;
  255. struct device_node *np = pdev->dev.of_node;
  256. struct resource *dbi_base;
  257. int ret;
  258. spear13xx_pcie = devm_kzalloc(dev, sizeof(*spear13xx_pcie), GFP_KERNEL);
  259. if (!spear13xx_pcie)
  260. return -ENOMEM;
  261. spear13xx_pcie->phy = devm_phy_get(dev, "pcie-phy");
  262. if (IS_ERR(spear13xx_pcie->phy)) {
  263. ret = PTR_ERR(spear13xx_pcie->phy);
  264. if (ret == -EPROBE_DEFER)
  265. dev_info(dev, "probe deferred\n");
  266. else
  267. dev_err(dev, "couldn't get pcie-phy\n");
  268. return ret;
  269. }
  270. phy_init(spear13xx_pcie->phy);
  271. spear13xx_pcie->clk = devm_clk_get(dev, NULL);
  272. if (IS_ERR(spear13xx_pcie->clk)) {
  273. dev_err(dev, "couldn't get clk for pcie\n");
  274. return PTR_ERR(spear13xx_pcie->clk);
  275. }
  276. ret = clk_prepare_enable(spear13xx_pcie->clk);
  277. if (ret) {
  278. dev_err(dev, "couldn't enable clk for pcie\n");
  279. return ret;
  280. }
  281. pp = &spear13xx_pcie->pp;
  282. pp->dev = dev;
  283. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  284. pp->dbi_base = devm_ioremap_resource(dev, dbi_base);
  285. if (IS_ERR(pp->dbi_base)) {
  286. dev_err(dev, "couldn't remap dbi base %p\n", dbi_base);
  287. ret = PTR_ERR(pp->dbi_base);
  288. goto fail_clk;
  289. }
  290. spear13xx_pcie->app_base = pp->dbi_base + 0x2000;
  291. if (of_property_read_bool(np, "st,pcie-is-gen1"))
  292. spear13xx_pcie->is_gen1 = true;
  293. ret = spear13xx_add_pcie_port(pp, pdev);
  294. if (ret < 0)
  295. goto fail_clk;
  296. platform_set_drvdata(pdev, spear13xx_pcie);
  297. return 0;
  298. fail_clk:
  299. clk_disable_unprepare(spear13xx_pcie->clk);
  300. return ret;
  301. }
  302. static const struct of_device_id spear13xx_pcie_of_match[] = {
  303. { .compatible = "st,spear1340-pcie", },
  304. {},
  305. };
  306. MODULE_DEVICE_TABLE(of, spear13xx_pcie_of_match);
  307. static struct platform_driver spear13xx_pcie_driver = {
  308. .probe = spear13xx_pcie_probe,
  309. .driver = {
  310. .name = "spear-pcie",
  311. .of_match_table = of_match_ptr(spear13xx_pcie_of_match),
  312. },
  313. };
  314. /* SPEAr13xx PCIe driver does not allow module unload */
  315. static int __init spear13xx_pcie_init(void)
  316. {
  317. return platform_driver_register(&spear13xx_pcie_driver);
  318. }
  319. module_init(spear13xx_pcie_init);
  320. MODULE_DESCRIPTION("ST Microelectronics SPEAr13xx PCIe host controller driver");
  321. MODULE_AUTHOR("Pratyush Anand <pratyush.anand@gmail.com>");
  322. MODULE_LICENSE("GPL v2");