drd.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <linux/gpio/consumer.h>
  14. #include "core.h"
  15. #define dwc2_ovr_gotgctl(gotgctl) \
  16. ((gotgctl) |= GOTGCTL_BVALOEN | GOTGCTL_AVALOEN | GOTGCTL_VBVALOEN | \
  17. GOTGCTL_DBNCE_FLTR_BYPASS)
  18. static void ark_sys_usb_init(struct dwc2_hsotg *hsotg)
  19. {
  20. u32 regval;
  21. void __iomem *sys_id_base = (void __iomem *)((int)hsotg->sys_regs + hsotg->usb_id_reg);
  22. int usb_id_bit_offset = hsotg->usb_id_bit_offset;
  23. regval = readl(sys_id_base);
  24. regval &= (~(1 << usb_id_bit_offset));
  25. if (hsotg->dr_mode != USB_DR_MODE_PERIPHERAL) {
  26. printk("sys usb set host mode\n");
  27. regval &= (~(1 << (usb_id_bit_offset + 1)));
  28. } else {
  29. printk("sys usb set dev mode\n");
  30. regval = ((1 << (usb_id_bit_offset + 1)));
  31. }
  32. regval &= (~(1 << (usb_id_bit_offset + 2)));
  33. regval &= (~(1 << (usb_id_bit_offset + 3)));
  34. regval &= (~(1 << (usb_id_bit_offset + 4)));
  35. writel(regval, sys_id_base);
  36. }
  37. static void ark_sys_usb_mode(struct dwc2_hsotg *hsotg, enum usb_role role)
  38. {
  39. u32 regval;
  40. void __iomem *sys_id_base = (void __iomem *)((int)hsotg->sys_regs + hsotg->usb_id_reg);
  41. int usb_id_bit_offset = hsotg->usb_id_bit_offset;
  42. regval = readl(sys_id_base);
  43. if (role != USB_ROLE_DEVICE) {
  44. printk("sys usb set host mode\n");
  45. regval &= (~(1 << (usb_id_bit_offset + 1)));
  46. } else {
  47. printk("sys usb set dev mode\n");
  48. regval = ((1 << (usb_id_bit_offset + 1)));
  49. }
  50. writel(regval, sys_id_base);
  51. }
  52. static void dwc2_ovr_init(struct dwc2_hsotg *hsotg)
  53. {
  54. unsigned long flags;
  55. u32 gotgctl;
  56. spin_lock_irqsave(&hsotg->lock, flags);
  57. gotgctl = dwc2_readl(hsotg, GOTGCTL);
  58. dwc2_ovr_gotgctl(gotgctl);
  59. gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
  60. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  61. gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
  62. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  63. gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
  64. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  65. spin_unlock_irqrestore(&hsotg->lock, flags);
  66. dwc2_force_mode(hsotg, (hsotg->dr_mode == USB_DR_MODE_HOST) ||
  67. (hsotg->role_sw_default_mode == USB_DR_MODE_HOST));
  68. }
  69. static int dwc2_ovr_avalid(struct dwc2_hsotg *hsotg, bool valid)
  70. {
  71. u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
  72. /* Check if A-Session is already in the right state */
  73. if ((valid && (gotgctl & GOTGCTL_ASESVLD)) ||
  74. (!valid && !(gotgctl & GOTGCTL_ASESVLD)))
  75. return -EALREADY;
  76. /* Always enable overrides to handle the resume case */
  77. dwc2_ovr_gotgctl(gotgctl);
  78. gotgctl &= ~GOTGCTL_BVALOVAL;
  79. if (valid)
  80. gotgctl |= GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL;
  81. else
  82. gotgctl &= ~(GOTGCTL_AVALOVAL | GOTGCTL_VBVALOVAL);
  83. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  84. return 0;
  85. }
  86. static int dwc2_ovr_bvalid(struct dwc2_hsotg *hsotg, bool valid)
  87. {
  88. u32 gotgctl = dwc2_readl(hsotg, GOTGCTL);
  89. /* Check if B-Session is already in the right state */
  90. if ((valid && (gotgctl & GOTGCTL_BSESVLD)) ||
  91. (!valid && !(gotgctl & GOTGCTL_BSESVLD)))
  92. return -EALREADY;
  93. /* Always enable overrides to handle the resume case */
  94. dwc2_ovr_gotgctl(gotgctl);
  95. gotgctl &= ~GOTGCTL_AVALOVAL;
  96. if (valid)
  97. gotgctl |= GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL;
  98. else
  99. gotgctl &= ~(GOTGCTL_BVALOVAL | GOTGCTL_VBVALOVAL);
  100. dwc2_writel(hsotg, gotgctl, GOTGCTL);
  101. return 0;
  102. }
  103. static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role)
  104. {
  105. struct dwc2_hsotg *hsotg = usb_role_switch_get_drvdata(sw);
  106. unsigned long flags;
  107. int already = 0;
  108. /* Skip session not in line with dr_mode */
  109. if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) ||
  110. (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL))
  111. return -EINVAL;
  112. #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
  113. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
  114. /* Skip session if core is in test mode */
  115. if (role == USB_ROLE_NONE && hsotg->test_mode) {
  116. dev_dbg(hsotg->dev, "Core is in test mode\n");
  117. return -EBUSY;
  118. }
  119. #endif
  120. /*
  121. * In case of USB_DR_MODE_PERIPHERAL, clock is disabled at the end of
  122. * the probe and enabled on udc_start.
  123. * If role-switch set is called before the udc_start, we need to enable
  124. * the clock to read/write GOTGCTL and GUSBCFG registers to override
  125. * mode and sessions. It is the case if cable is plugged at boot.
  126. */
  127. if (!hsotg->ll_hw_enabled && hsotg->clk) {
  128. int ret = clk_prepare_enable(hsotg->clk);
  129. if (ret)
  130. return ret;
  131. }
  132. /* Power reset */
  133. if (!IS_ERR(hsotg->gpiod_reset)) {
  134. gpiod_direction_output(hsotg->gpiod_reset, 0);
  135. usleep_range(20000, 30000);
  136. gpiod_direction_output(hsotg->gpiod_reset, 1);
  137. }
  138. spin_lock_irqsave(&hsotg->lock, flags);
  139. ark_sys_usb_mode(hsotg, role);
  140. if (role == USB_ROLE_NONE) {
  141. /* default operation mode when usb role is USB_ROLE_NONE */
  142. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  143. role = USB_ROLE_HOST;
  144. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  145. role = USB_ROLE_DEVICE;
  146. }
  147. if ((IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) ||
  148. IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)) &&
  149. dwc2_is_device_mode(hsotg) &&
  150. hsotg->lx_state == DWC2_L2 &&
  151. hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE &&
  152. hsotg->bus_suspended &&
  153. !hsotg->params.no_clock_gating)
  154. dwc2_gadget_exit_clock_gating(hsotg, 0);
  155. if (role == USB_ROLE_HOST) {
  156. already = dwc2_ovr_avalid(hsotg, true);
  157. } else if (role == USB_ROLE_DEVICE) {
  158. already = dwc2_ovr_bvalid(hsotg, true);
  159. if (dwc2_is_device_enabled(hsotg)) {
  160. /* This clear DCTL.SFTDISCON bit */
  161. dwc2_hsotg_core_connect(hsotg);
  162. }
  163. } else {
  164. if (dwc2_is_device_mode(hsotg)) {
  165. if (!dwc2_ovr_bvalid(hsotg, false))
  166. /* This set DCTL.SFTDISCON bit */
  167. dwc2_hsotg_core_disconnect(hsotg);
  168. } else {
  169. dwc2_ovr_avalid(hsotg, false);
  170. }
  171. }
  172. spin_unlock_irqrestore(&hsotg->lock, flags);
  173. if (!already && hsotg->dr_mode == USB_DR_MODE_OTG)
  174. /* This will raise a Connector ID Status Change Interrupt */
  175. dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
  176. if (!hsotg->ll_hw_enabled && hsotg->clk)
  177. clk_disable_unprepare(hsotg->clk);
  178. dev_dbg(hsotg->dev, "%s-session valid\n",
  179. role == USB_ROLE_NONE ? "No" :
  180. role == USB_ROLE_HOST ? "A" : "B");
  181. return 0;
  182. }
  183. int dwc2_drd_init(struct dwc2_hsotg *hsotg)
  184. {
  185. struct usb_role_switch_desc role_sw_desc = {0};
  186. struct usb_role_switch *role_sw;
  187. int ret;printk("%s:%d\n", __func__, __LINE__);
  188. if (!device_property_read_bool(hsotg->dev, "usb-role-switch"))
  189. return 0;
  190. ark_sys_usb_init(hsotg);
  191. hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev);
  192. role_sw_desc.driver_data = hsotg;
  193. role_sw_desc.fwnode = dev_fwnode(hsotg->dev);
  194. role_sw_desc.set = dwc2_drd_role_sw_set;
  195. role_sw_desc.allow_userspace_control = true;
  196. role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc);
  197. if (IS_ERR(role_sw)) {
  198. ret = PTR_ERR(role_sw);
  199. dev_err(hsotg->dev,
  200. "failed to register role switch: %d\n", ret);
  201. return ret;
  202. }
  203. hsotg->role_sw = role_sw;
  204. /* Enable override and initialize values */
  205. dwc2_ovr_init(hsotg);
  206. printk("%s:%d\n", __func__, __LINE__);
  207. return 0;
  208. }
  209. void dwc2_drd_suspend(struct dwc2_hsotg *hsotg)
  210. {
  211. u32 gintsts, gintmsk;
  212. if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
  213. gintmsk = dwc2_readl(hsotg, GINTMSK);
  214. gintmsk &= ~GINTSTS_CONIDSTSCHNG;
  215. dwc2_writel(hsotg, gintmsk, GINTMSK);
  216. gintsts = dwc2_readl(hsotg, GINTSTS);
  217. dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
  218. }
  219. }
  220. void dwc2_drd_resume(struct dwc2_hsotg *hsotg)
  221. {
  222. u32 gintsts, gintmsk;
  223. enum usb_role role;
  224. if (hsotg->role_sw) {
  225. /* get last known role (as the get ops isn't implemented by this driver) */
  226. role = usb_role_switch_get_role(hsotg->role_sw);
  227. if (role == USB_ROLE_NONE) {
  228. if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)
  229. role = USB_ROLE_HOST;
  230. else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL)
  231. role = USB_ROLE_DEVICE;
  232. }
  233. /* restore last role that may have been lost */
  234. if (role == USB_ROLE_HOST)
  235. dwc2_ovr_avalid(hsotg, true);
  236. else if (role == USB_ROLE_DEVICE)
  237. dwc2_ovr_bvalid(hsotg, true);
  238. dwc2_force_mode(hsotg, role == USB_ROLE_HOST);
  239. dev_dbg(hsotg->dev, "resuming %s-session valid\n",
  240. role == USB_ROLE_NONE ? "No" :
  241. role == USB_ROLE_HOST ? "A" : "B");
  242. }
  243. if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) {
  244. gintsts = dwc2_readl(hsotg, GINTSTS);
  245. dwc2_writel(hsotg, gintsts | GINTSTS_CONIDSTSCHNG, GINTSTS);
  246. gintmsk = dwc2_readl(hsotg, GINTMSK);
  247. gintmsk |= GINTSTS_CONIDSTSCHNG;
  248. dwc2_writel(hsotg, gintmsk, GINTMSK);
  249. }
  250. }
  251. void dwc2_drd_exit(struct dwc2_hsotg *hsotg)
  252. {
  253. if (hsotg->role_sw)
  254. usb_role_switch_unregister(hsotg->role_sw);
  255. }