pcie-spear13xx.c 10 KB

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