drd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 <[email protected]>
  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 (role == USB_ROLE_HOST) {
  106. already = dwc2_ovr_avalid(hsotg, true);
  107. } else if (role == USB_ROLE_DEVICE) {
  108. already = dwc2_ovr_bvalid(hsotg, true);
  109. if (dwc2_is_device_enabled(hsotg)) {
  110. /* This clear DCTL.SFTDISCON bit */
  111. dwc2_hsotg_core_connect(hsotg);
  112. }
  113. } else {
  114. if (dwc2_is_device_mode(hsotg)) {
  115. if (!dwc2_ovr_bvalid(hsotg, false))
  116. /* This set DCTL.SFTDISCON bit */
  117. dwc2_hsotg_core_disconnect(hsotg);
  118. } else {
  119. dwc2_ovr_avalid(hsotg, false);
  120. }
  121. }
  122. spin_unlock_irqrestore(&hsotg->lock, flags);
  123. if (!already && hsotg->dr_mode == USB_DR_MODE_OTG)
  124. /* This will raise a Connector ID Status Change Interrupt */
  125. dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
  126. if (!hsotg->ll_hw_enabled && hsotg->clk)
  127. clk_disable_unprepare(hsotg->clk);
  128. dev_dbg(hsotg->dev, "%s-session valid\n",
  129. role == USB_ROLE_NONE ? "No" :
  130. role == USB_ROLE_HOST ? "A" : "B");
  131. return 0;
  132. }
  133. int dwc2_drd_init(struct dwc2_hsotg *hsotg)
  134. {
  135. struct usb_role_switch_desc role_sw_desc = {0};
  136. struct usb_role_switch *role_sw;
  137. int ret;
  138. if (!device_property_read_bool(hsotg->dev, "usb-role-switch"))
  139. return 0;
  140. hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev);
  141. role_sw_desc.driver_data = hsotg;
  142. role_sw_desc.fwnode = dev_fwnode(hsotg->dev);
  143. role_sw_desc.set = dwc2_drd_role_sw_set;
  144. role_sw_desc.allow_userspace_control = true;
  145. role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc);
  146. if (IS_ERR(role_sw)) {
  147. ret = PTR_ERR(role_sw);
  148. dev_err(hsotg->dev,
  149. "failed to register role switch: %d\n", ret);
  150. return ret;
  151. }
  152. hsotg->role_sw = role_sw;
  153. /* Enable override and initialize values */
  154. dwc2_ovr_init(hsotg);
  155. return 0;
  156. }
  157. void dwc2_drd_suspend(struct dwc2_hsotg *hsotg)
  158. {
  159. u32 gintsts, gintmsk;
  160. if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
  161. gintmsk = dwc2_readl(hsotg, GINTMSK);
  162. gintmsk &= ~GINTSTS_CONIDSTSCHNG;
  163. dwc2_writel(hsotg, gintmsk, GINTMSK);
  164. gintsts = dwc2_readl(hsotg, GINTSTS);
  165. dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
  166. }
  167. }
  168. void dwc2_drd_resume(struct dwc2_hsotg *hsotg)
  169. {
  170. u32 gintsts, gintmsk;
  171. enum usb_role role;
  172. if (hsotg->role_sw) {
  173. /* get last known role (as the get ops isn't implemented by this driver) */
  174. role = usb_role_switch_get_role(hsotg->role_sw);
  175. if (role == USB_ROLE_NONE) {
  176. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  177. role = USB_ROLE_HOST;
  178. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  179. role = USB_ROLE_DEVICE;
  180. }
  181. /* restore last role that may have been lost */
  182. if (role == USB_ROLE_HOST)
  183. dwc2_ovr_avalid(hsotg, true);
  184. else if (role == USB_ROLE_DEVICE)
  185. dwc2_ovr_bvalid(hsotg, true);
  186. dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
  187. dev_dbg(hsotg->dev, "resuming %s-session valid\n",
  188. role == USB_ROLE_NONE ? "No" :
  189. role == USB_ROLE_HOST ? "A" : "B");
  190. }
  191. if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
  192. gintsts = dwc2_readl(hsotg, GINTSTS);
  193. dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
  194. gintmsk = dwc2_readl(hsotg, GINTMSK);
  195. gintmsk |= GINTSTS_CONIDSTSCHNG;
  196. dwc2_writel(hsotg, gintmsk, GINTMSK);
  197. }
  198. }
  199. void dwc2_drd_exit(struct dwc2_hsotg *hsotg)
  200. {
  201. if (hsotg->role_sw)
  202. usb_role_switch_unregister(hsotg->role_sw);
  203. }