drd.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drd.c - DesignWare USB2 DRD Controller Dual-role support
  4. *
  5. * Copyright (C) 2020 STMicroelectronics
  6. *
  7. * Author(s): Amelie Delaunay <amelie.delaunay@st.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/usb/role.h>
  13. #include "core.h"
  14. #define dwc2_ovr_gotgctl(gotgctl) \
  15. ((gotgctl) |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN | \
  16. GOTGCTL_DBNCE_FLTR_BYPASS)
  17. static void dwc2_ovr_init(struct dwc2_hsotg *hsotg)
  18. {
  19. unsigned long flags;
  20. u32 gotgctl;
  21. spin_lock_irqsave(&hsotg->lock, flags);
  22. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  23. dwc2_ovr_gotgctl(gotgctl);
  24. gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
  25. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  26. gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
  27. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  28. gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
  29. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  30. spin_unlock_irqrestore(&hsotg->lock, flags);
  31. dwc2_force_mode(hsotg, (hsotg->dr_mode == USB_DR_MODE_HOST) ||
  32. (hsotg->role_sw_default_mode == USB_DR_MODE_HOST));
  33. }
  34. static int dwc2_ovr_avalid(struct dwc2_hsotg *hsotg, bool valid)
  35. {
  36. u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
  37. /* Check if A-Session is already in the right state */
  38. if ((valid && (gotgctl & GOTGCTL_ASESVLD)) ||
  39. (!valid && !(gotgctl & GOTGCTL_ASESVLD)))
  40. return -EALREADY;
  41. /* Always enable overrides to handle the resume case */
  42. dwc2_ovr_gotgctl(gotgctl);
  43. gotgctl &= ~GOTGCTL_BVALOVAL;
  44. if (valid)
  45. gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
  46. else
  47. gotgctl &= ~(GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
  48. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  49. return 0;
  50. }
  51. static int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid)
  52. {
  53. u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
  54. /* Check if B-Session is already in the right state */
  55. if ((valid && (gotgctl & GOTGCTL_BSESVLD)) ||
  56. (!valid && !(gotgctl & GOTGCTL_BSESVLD)))
  57. return -EALREADY;
  58. /* Always enable overrides to handle the resume case */
  59. dwc2_ovr_gotgctl(gotgctl);
  60. gotgctl &= ~GOTGCTL_AVALOVAL;
  61. if (valid)
  62. gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
  63. else
  64. gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL);
  65. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  66. return 0;
  67. }
  68. static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
  69. {
  70. struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw);
  71. unsigned long flags;
  72. int already = 0;
  73. /* Skip session not in line with dr_mode */
  74. if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) ||
  75. (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  76. return -EINVAL;
  77. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  78. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  79. /* Skip session if core is in test mode */
  80. if (role == USB_ROLE_NONE && hsotg->test_mode) {
  81. dev_dbg(hsotg->dev, "Core is in test mode\n");
  82. return -EBUSY;
  83. }
  84. #endif
  85. /*
  86. * In case of USB_DR_MODE_PERIPHERAL, clock is disabled at the end of
  87. * the probe and enabled on udc_start.
  88. * If role-switch set is called before the udc_start, we need to enable
  89. * the clock to read/write GOTGCTL and GUSBCFG registers to override
  90. * mode and sessions. It is the case if cable is plugged at boot.
  91. */
  92. if (!hsotg->ll_hw_enabled && hsotg->clk) {
  93. int ret = clk_prepare_enable(hsotg->clk);
  94. if (ret)
  95. return ret;
  96. }
  97. spin_lock_irqsave(&hsotg->lock, flags);
  98. if (role == USB_ROLE_NONE) {
  99. /* default operation mode when usb role is USB_ROLE_NONE */
  100. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  101. role = USB_ROLE_HOST;
  102. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  103. role = USB_ROLE_DEVICE;
  104. }
  105. if ((IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) ||
  106. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)) &&
  107. dwc2_is_device_mode(hsotg) &&
  108. hsotg->lx_state == DWC2_L2 &&
  109. hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
  110. hsotg->bus_suspended &&
  111. !hsotg->params.no_clock_gating)
  112. dwc2_gadget_exit_clock_gating(hsotg, 0);
  113. if (role == USB_ROLE_HOST) {
  114. already = dwc2_ovr_avalid(hsotg, true);
  115. } else if (role == USB_ROLE_DEVICE) {
  116. already = dwc2_ovr_bvalid(hsotg, true);
  117. if (dwc2_is_device_enabled(hsotg)) {
  118. /* This clear DCTL.SFTDISCON bit */
  119. dwc2_hsotg_core_connect(hsotg);
  120. }
  121. } else {
  122. if (dwc2_is_device_mode(hsotg)) {
  123. if (!dwc2_ovr_bvalid(hsotg, false))
  124. /* This set DCTL.SFTDISCON bit */
  125. dwc2_hsotg_core_disconnect(hsotg);
  126. } else {
  127. dwc2_ovr_avalid(hsotg, false);
  128. }
  129. }
  130. spin_unlock_irqrestore(&hsotg->lock, flags);
  131. if (!already && hsotg->dr_mode == USB_DR_MODE_OTG)
  132. /* This will raise a Connector ID Status Change Interrupt */
  133. dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
  134. if (!hsotg->ll_hw_enabled && hsotg->clk)
  135. clk_disable_unprepare(hsotg->clk);
  136. dev_dbg(hsotg->dev, "%s-session valid\n",
  137. role == USB_ROLE_NONE ? "No" :
  138. role == USB_ROLE_HOST ? "A" : "B");
  139. return 0;
  140. }
  141. int dwc2_drd_init(struct dwc2_hsotg *hsotg)
  142. {
  143. struct usb_role_switch_desc role_sw_desc = {0};
  144. struct usb_role_switch *role_sw;
  145. int ret;
  146. if (!device_property_read_bool(hsotg->dev, "usb-role-switch"))
  147. return 0;
  148. hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev);
  149. role_sw_desc.driver_data = hsotg;
  150. role_sw_desc.fwnode = dev_fwnode(hsotg->dev);
  151. role_sw_desc.set = dwc2_drd_role_sw_set;
  152. role_sw_desc.allow_userspace_control = true;
  153. role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc);
  154. if (IS_ERR(role_sw)) {
  155. ret = PTR_ERR(role_sw);
  156. dev_err(hsotg->dev,
  157. "failed to register role switch: %d\n", ret);
  158. return ret;
  159. }
  160. hsotg->role_sw = role_sw;
  161. /* Enable override and initialize values */
  162. dwc2_ovr_init(hsotg);
  163. return 0;
  164. }
  165. void dwc2_drd_suspend(struct dwc2_hsotg *hsotg)
  166. {
  167. u32 gintsts, gintmsk;
  168. if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
  169. gintmsk = dwc2_readl(hsotg, GINTMSK);
  170. gintmsk &= ~GINTSTS_CONIDSTSCHNG;
  171. dwc2_writel(hsotg, gintmsk, GINTMSK);
  172. gintsts = dwc2_readl(hsotg, GINTSTS);
  173. dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
  174. }
  175. }
  176. void dwc2_drd_resume(struct dwc2_hsotg *hsotg)
  177. {
  178. u32 gintsts, gintmsk;
  179. enum usb_role role;
  180. if (hsotg->role_sw) {
  181. /* get last known role (as the get ops isn't implemented by this driver) */
  182. role = usb_role_switch_get_role(hsotg->role_sw);
  183. if (role == USB_ROLE_NONE) {
  184. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  185. role = USB_ROLE_HOST;
  186. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  187. role = USB_ROLE_DEVICE;
  188. }
  189. /* restore last role that may have been lost */
  190. if (role == USB_ROLE_HOST)
  191. dwc2_ovr_avalid(hsotg, true);
  192. else if (role == USB_ROLE_DEVICE)
  193. dwc2_ovr_bvalid(hsotg, true);
  194. dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
  195. dev_dbg(hsotg->dev, "resuming %s-session valid\n",
  196. role == USB_ROLE_NONE ? "No" :
  197. role == USB_ROLE_HOST ? "A" : "B");
  198. }
  199. if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
  200. gintsts = dwc2_readl(hsotg, GINTSTS);
  201. dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
  202. gintmsk = dwc2_readl(hsotg, GINTMSK);
  203. gintmsk |= GINTSTS_CONIDSTSCHNG;
  204. dwc2_writel(hsotg, gintmsk, GINTMSK);
  205. }
  206. }
  207. void dwc2_drd_exit(struct dwc2_hsotg *hsotg)
  208. {
  209. if (hsotg->role_sw)
  210. usb_role_switch_unregister(hsotg->role_sw);
  211. }