mtu3_host.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mtu3_dr.c - dual role switch and host glue layer
  4. *
  5. * Copyright (C) 2016 MediaTek Inc.
  6. *
  7. * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
  8. */
  9. #include <dm/lists.h>
  10. #include <linux/iopoll.h>
  11. #include "mtu3.h"
  12. #include "mtu3_dr.h"
  13. static void host_ports_num_get(struct mtu3_host *u3h)
  14. {
  15. u32 xhci_cap;
  16. xhci_cap = mtu3_readl(u3h->ippc_base, U3D_SSUSB_IP_XHCI_CAP);
  17. u3h->u2_ports = SSUSB_IP_XHCI_U2_PORT_NUM(xhci_cap);
  18. u3h->u3_ports = SSUSB_IP_XHCI_U3_PORT_NUM(xhci_cap);
  19. dev_dbg(u3h->dev, "host - u2_ports:%d, u3_ports:%d\n",
  20. u3h->u2_ports, u3h->u3_ports);
  21. }
  22. /* only configure ports will be used later */
  23. static int ssusb_host_enable(struct mtu3_host *u3h)
  24. {
  25. void __iomem *ibase = u3h->ippc_base;
  26. int num_u3p = u3h->u3_ports;
  27. int num_u2p = u3h->u2_ports;
  28. int u3_ports_disabed;
  29. u32 check_clk;
  30. u32 value;
  31. int i;
  32. /* power on host ip */
  33. mtu3_clrbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
  34. /* power on and enable u3 ports except skipped ones */
  35. u3_ports_disabed = 0;
  36. for (i = 0; i < num_u3p; i++) {
  37. if ((0x1 << i) & u3h->u3p_dis_msk) {
  38. u3_ports_disabed++;
  39. continue;
  40. }
  41. value = mtu3_readl(ibase, SSUSB_U3_CTRL(i));
  42. value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
  43. value |= SSUSB_U3_PORT_HOST_SEL;
  44. mtu3_writel(ibase, SSUSB_U3_CTRL(i), value);
  45. }
  46. /* power on and enable all u2 ports */
  47. for (i = 0; i < num_u2p; i++) {
  48. value = mtu3_readl(ibase, SSUSB_U2_CTRL(i));
  49. value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
  50. value |= SSUSB_U2_PORT_HOST_SEL;
  51. mtu3_writel(ibase, SSUSB_U2_CTRL(i), value);
  52. }
  53. check_clk = SSUSB_XHCI_RST_B_STS;
  54. if (num_u3p > u3_ports_disabed)
  55. check_clk = SSUSB_U3_MAC_RST_B_STS;
  56. return ssusb_check_clocks(u3h->ssusb, check_clk);
  57. }
  58. static void ssusb_host_disable(struct mtu3_host *u3h)
  59. {
  60. void __iomem *ibase = u3h->ippc_base;
  61. int num_u3p = u3h->u3_ports;
  62. int num_u2p = u3h->u2_ports;
  63. u32 value;
  64. int i;
  65. /* power down and disable u3 ports except skipped ones */
  66. for (i = 0; i < num_u3p; i++) {
  67. if ((0x1 << i) & u3h->u3p_dis_msk)
  68. continue;
  69. value = mtu3_readl(ibase, SSUSB_U3_CTRL(i));
  70. value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS;
  71. mtu3_writel(ibase, SSUSB_U3_CTRL(i), value);
  72. }
  73. /* power down and disable all u2 ports */
  74. for (i = 0; i < num_u2p; i++) {
  75. value = mtu3_readl(ibase, SSUSB_U2_CTRL(i));
  76. value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS;
  77. mtu3_writel(ibase, SSUSB_U2_CTRL(i), value);
  78. }
  79. /* power down host ip */
  80. mtu3_setbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
  81. }
  82. /*
  83. * If host supports multiple ports, the VBUSes(5V) of ports except port0
  84. * which supports OTG are better to be enabled by default in DTS.
  85. * Because the host driver will keep link with devices attached when system
  86. * enters suspend mode, so no need to control VBUSes after initialization.
  87. */
  88. int ssusb_host_init(struct ssusb_mtk *ssusb)
  89. {
  90. struct mtu3_host *u3h = ssusb->u3h;
  91. struct udevice *dev = u3h->dev;
  92. int ret;
  93. u3h->ssusb = ssusb;
  94. u3h->hcd = ssusb->mac_base;
  95. u3h->ippc_base = ssusb->ippc_base;
  96. /* optional property, ignore the error */
  97. dev_read_u32(dev, "mediatek,u3p-dis-msk", &u3h->u3p_dis_msk);
  98. host_ports_num_get(u3h);
  99. ret = ssusb_host_enable(u3h);
  100. if (ret)
  101. return ret;
  102. ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST);
  103. ret = regulator_set_enable(ssusb->vbus_supply, true);
  104. if (ret < 0 && ret != -ENOSYS) {
  105. dev_err(dev, "failed to enable vbus %d!\n", ret);
  106. return ret;
  107. }
  108. dev_info(dev, "%s done...\n", __func__);
  109. return 0;
  110. }
  111. void ssusb_host_exit(struct ssusb_mtk *ssusb)
  112. {
  113. regulator_set_enable(ssusb->vbus_supply, false);
  114. ssusb_host_disable(ssusb->u3h);
  115. }