otg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * otg.c - ChipIdea USB IP core OTG driver
  4. *
  5. * Copyright (C) 2013 Freescale Semiconductor, Inc.
  6. *
  7. * Author: Peter Chen
  8. */
  9. /*
  10. * This file mainly handles otgsc register, OTG fsm operations for HNP and SRP
  11. * are also included.
  12. */
  13. #include <linux/usb/otg.h>
  14. #include <linux/usb/gadget.h>
  15. #include <linux/usb/chipidea.h>
  16. #include "ci.h"
  17. #include "bits.h"
  18. #include "otg.h"
  19. #include "otg_fsm.h"
  20. /**
  21. * hw_read_otgsc returns otgsc register bits value.
  22. * @mask: bitfield mask
  23. */
  24. u32 hw_read_otgsc(struct ci_hdrc *ci, u32 mask)
  25. {
  26. struct ci_hdrc_cable *cable;
  27. u32 val = hw_read(ci, OP_OTGSC, mask);
  28. /*
  29. * If using extcon framework for VBUS and/or ID signal
  30. * detection overwrite OTGSC register value
  31. */
  32. cable = &ci->platdata->vbus_extcon;
  33. if (!IS_ERR(cable->edev)) {
  34. if (cable->changed)
  35. val |= OTGSC_BSVIS;
  36. else
  37. val &= ~OTGSC_BSVIS;
  38. if (cable->connected)
  39. val |= OTGSC_BSV;
  40. else
  41. val &= ~OTGSC_BSV;
  42. if (cable->enabled)
  43. val |= OTGSC_BSVIE;
  44. else
  45. val &= ~OTGSC_BSVIE;
  46. }
  47. cable = &ci->platdata->id_extcon;
  48. if (!IS_ERR(cable->edev)) {
  49. if (cable->changed)
  50. val |= OTGSC_IDIS;
  51. else
  52. val &= ~OTGSC_IDIS;
  53. if (cable->connected)
  54. val &= ~OTGSC_ID; /* host */
  55. else
  56. val |= OTGSC_ID; /* device */
  57. if (cable->enabled)
  58. val |= OTGSC_IDIE;
  59. else
  60. val &= ~OTGSC_IDIE;
  61. }
  62. return val & mask;
  63. }
  64. /**
  65. * hw_write_otgsc updates target bits of OTGSC register.
  66. * @mask: bitfield mask
  67. * @data: to be written
  68. */
  69. void hw_write_otgsc(struct ci_hdrc *ci, u32 mask, u32 data)
  70. {
  71. struct ci_hdrc_cable *cable;
  72. cable = &ci->platdata->vbus_extcon;
  73. if (!IS_ERR(cable->edev)) {
  74. if (data & mask & OTGSC_BSVIS)
  75. cable->changed = false;
  76. /* Don't enable vbus interrupt if using external notifier */
  77. if (data & mask & OTGSC_BSVIE) {
  78. cable->enabled = true;
  79. data &= ~OTGSC_BSVIE;
  80. } else if (mask & OTGSC_BSVIE) {
  81. cable->enabled = false;
  82. }
  83. }
  84. cable = &ci->platdata->id_extcon;
  85. if (!IS_ERR(cable->edev)) {
  86. if (data & mask & OTGSC_IDIS)
  87. cable->changed = false;
  88. /* Don't enable id interrupt if using external notifier */
  89. if (data & mask & OTGSC_IDIE) {
  90. cable->enabled = true;
  91. data &= ~OTGSC_IDIE;
  92. } else if (mask & OTGSC_IDIE) {
  93. cable->enabled = false;
  94. }
  95. }
  96. hw_write(ci, OP_OTGSC, mask | OTGSC_INT_STATUS_BITS, data);
  97. }
  98. /**
  99. * ci_otg_role - pick role based on ID pin state
  100. * @ci: the controller
  101. */
  102. enum ci_role ci_otg_role(struct ci_hdrc *ci)
  103. {
  104. enum ci_role role = hw_read_otgsc(ci, OTGSC_ID)
  105. ? CI_ROLE_GADGET
  106. : CI_ROLE_HOST;
  107. return role;
  108. }
  109. void ci_handle_vbus_change(struct ci_hdrc *ci)
  110. {
  111. if (!ci->is_otg)
  112. return;
  113. if (hw_read_otgsc(ci, OTGSC_BSV) && !ci->vbus_active)
  114. usb_gadget_vbus_connect(&ci->gadget);
  115. else if (!hw_read_otgsc(ci, OTGSC_BSV) && ci->vbus_active)
  116. usb_gadget_vbus_disconnect(&ci->gadget);
  117. }
  118. /**
  119. * When we switch to device mode, the vbus value should be lower
  120. * than OTGSC_BSV before connecting to host.
  121. *
  122. * @ci: the controller
  123. *
  124. * This function returns an error code if timeout
  125. */
  126. static int hw_wait_vbus_lower_bsv(struct ci_hdrc *ci)
  127. {
  128. unsigned long elapse = jiffies + msecs_to_jiffies(5000);
  129. u32 mask = OTGSC_BSV;
  130. while (hw_read_otgsc(ci, mask)) {
  131. if (time_after(jiffies, elapse)) {
  132. dev_err(ci->dev, "timeout waiting for %08x in OTGSC\n",
  133. mask);
  134. return -ETIMEDOUT;
  135. }
  136. msleep(20);
  137. }
  138. return 0;
  139. }
  140. static void ci_handle_id_switch(struct ci_hdrc *ci)
  141. {
  142. enum ci_role role = ci_otg_role(ci);
  143. if (role != ci->role) {
  144. dev_dbg(ci->dev, "switching from %s to %s\n",
  145. ci_role(ci)->name, ci->roles[role]->name);
  146. ci_role_stop(ci);
  147. if (role == CI_ROLE_GADGET &&
  148. IS_ERR(ci->platdata->vbus_extcon.edev))
  149. /*
  150. * Wait vbus lower than OTGSC_BSV before connecting
  151. * to host. If connecting status is from an external
  152. * connector instead of register, we don't need to
  153. * care vbus on the board, since it will not affect
  154. * external connector status.
  155. */
  156. hw_wait_vbus_lower_bsv(ci);
  157. ci_role_start(ci, role);
  158. /* vbus change may have already occurred */
  159. if (role == CI_ROLE_GADGET)
  160. ci_handle_vbus_change(ci);
  161. }
  162. }
  163. /**
  164. * ci_otg_work - perform otg (vbus/id) event handle
  165. * @work: work struct
  166. */
  167. static void ci_otg_work(struct work_struct *work)
  168. {
  169. struct ci_hdrc *ci = container_of(work, struct ci_hdrc, work);
  170. if (ci_otg_is_fsm_mode(ci) && !ci_otg_fsm_work(ci)) {
  171. enable_irq(ci->irq);
  172. return;
  173. }
  174. pm_runtime_get_sync(ci->dev);
  175. if (ci->id_event) {
  176. ci->id_event = false;
  177. ci_handle_id_switch(ci);
  178. }
  179. if (ci->b_sess_valid_event) {
  180. ci->b_sess_valid_event = false;
  181. ci_handle_vbus_change(ci);
  182. }
  183. pm_runtime_put_sync(ci->dev);
  184. enable_irq(ci->irq);
  185. }
  186. /**
  187. * ci_hdrc_otg_init - initialize otg struct
  188. * ci: the controller
  189. */
  190. int ci_hdrc_otg_init(struct ci_hdrc *ci)
  191. {
  192. INIT_WORK(&ci->work, ci_otg_work);
  193. ci->wq = create_freezable_workqueue("ci_otg");
  194. if (!ci->wq) {
  195. dev_err(ci->dev, "can't create workqueue\n");
  196. return -ENODEV;
  197. }
  198. if (ci_otg_is_fsm_mode(ci))
  199. return ci_hdrc_otg_fsm_init(ci);
  200. return 0;
  201. }
  202. /**
  203. * ci_hdrc_otg_destroy - destroy otg struct
  204. * ci: the controller
  205. */
  206. void ci_hdrc_otg_destroy(struct ci_hdrc *ci)
  207. {
  208. if (ci->wq) {
  209. flush_workqueue(ci->wq);
  210. destroy_workqueue(ci->wq);
  211. }
  212. /* Disable all OTG irq and clear status */
  213. hw_write_otgsc(ci, OTGSC_INT_EN_BITS | OTGSC_INT_STATUS_BITS,
  214. OTGSC_INT_STATUS_BITS);
  215. if (ci_otg_is_fsm_mode(ci))
  216. ci_hdrc_otg_fsm_remove(ci);
  217. }