mod.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Renesas USB driver
  3. *
  4. * Copyright (C) 2011 Renesas Solutions Corp.
  5. * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. *
  16. */
  17. #include <linux/interrupt.h>
  18. #include "./common.h"
  19. #include "./mod.h"
  20. #define usbhs_priv_to_modinfo(priv) (&priv->mod_info)
  21. #define usbhs_mod_info_call(priv, func, param...) \
  22. ({ \
  23. struct usbhs_mod_info *info; \
  24. info = usbhs_priv_to_modinfo(priv); \
  25. !info->func ? 0 : \
  26. info->func(param); \
  27. })
  28. /*
  29. * autonomy
  30. *
  31. * these functions are used if platform doesn't have external phy.
  32. * -> there is no "notify_hotplug" callback from platform
  33. * -> call "notify_hotplug" by itself
  34. * -> use own interrupt to connect/disconnect
  35. * -> it mean module clock is always ON
  36. * ~~~~~~~~~~~~~~~~~~~~~~~~~
  37. */
  38. static int usbhsm_autonomy_get_vbus(struct platform_device *pdev)
  39. {
  40. struct usbhs_priv *priv = usbhs_pdev_to_priv(pdev);
  41. return VBSTS & usbhs_read(priv, INTSTS0);
  42. }
  43. static int usbhsm_autonomy_irq_vbus(struct usbhs_priv *priv,
  44. struct usbhs_irq_state *irq_state)
  45. {
  46. struct platform_device *pdev = usbhs_priv_to_pdev(priv);
  47. return usbhsc_drvcllbck_notify_hotplug(pdev);
  48. }
  49. void usbhs_mod_autonomy_mode(struct usbhs_priv *priv)
  50. {
  51. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  52. info->irq_vbus = usbhsm_autonomy_irq_vbus;
  53. priv->pfunc->get_vbus = usbhsm_autonomy_get_vbus;
  54. usbhs_irq_callback_update(priv, NULL);
  55. }
  56. /*
  57. * host / gadget functions
  58. *
  59. * renesas_usbhs host/gadget can register itself by below functions.
  60. * these functions are called when probe
  61. *
  62. */
  63. void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id)
  64. {
  65. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  66. info->mod[id] = mod;
  67. mod->priv = priv;
  68. }
  69. struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
  70. {
  71. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  72. struct usbhs_mod *ret = NULL;
  73. switch (id) {
  74. case USBHS_HOST:
  75. case USBHS_GADGET:
  76. ret = info->mod[id];
  77. break;
  78. }
  79. return ret;
  80. }
  81. int usbhs_mod_is_host(struct usbhs_priv *priv)
  82. {
  83. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  84. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  85. if (!mod)
  86. return -EINVAL;
  87. return info->mod[USBHS_HOST] == mod;
  88. }
  89. struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
  90. {
  91. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  92. return info->curt;
  93. }
  94. int usbhs_mod_change(struct usbhs_priv *priv, int id)
  95. {
  96. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  97. struct usbhs_mod *mod = NULL;
  98. int ret = 0;
  99. /* id < 0 mean no current */
  100. switch (id) {
  101. case USBHS_HOST:
  102. case USBHS_GADGET:
  103. mod = info->mod[id];
  104. break;
  105. default:
  106. ret = -EINVAL;
  107. }
  108. info->curt = mod;
  109. return ret;
  110. }
  111. static irqreturn_t usbhs_interrupt(int irq, void *data);
  112. int usbhs_mod_probe(struct usbhs_priv *priv)
  113. {
  114. struct device *dev = usbhs_priv_to_dev(priv);
  115. int ret;
  116. /*
  117. * install host/gadget driver
  118. */
  119. ret = usbhs_mod_gadget_probe(priv);
  120. if (ret < 0)
  121. return ret;
  122. /* irq settings */
  123. ret = request_irq(priv->irq, usbhs_interrupt,
  124. 0, dev_name(dev), priv);
  125. if (ret) {
  126. dev_err(dev, "irq request err\n");
  127. goto mod_init_gadget_err;
  128. }
  129. return ret;
  130. mod_init_gadget_err:
  131. usbhs_mod_gadget_remove(priv);
  132. return ret;
  133. }
  134. void usbhs_mod_remove(struct usbhs_priv *priv)
  135. {
  136. usbhs_mod_gadget_remove(priv);
  137. free_irq(priv->irq, priv);
  138. }
  139. /*
  140. * status functions
  141. */
  142. int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
  143. {
  144. int state = irq_state->intsts0 & DVSQ_MASK;
  145. switch (state) {
  146. case POWER_STATE:
  147. case DEFAULT_STATE:
  148. case ADDRESS_STATE:
  149. case CONFIGURATION_STATE:
  150. return state;
  151. }
  152. return -EIO;
  153. }
  154. int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
  155. {
  156. /*
  157. * return value
  158. *
  159. * IDLE_SETUP_STAGE
  160. * READ_DATA_STAGE
  161. * READ_STATUS_STAGE
  162. * WRITE_DATA_STAGE
  163. * WRITE_STATUS_STAGE
  164. * NODATA_STATUS_STAGE
  165. * SEQUENCE_ERROR
  166. */
  167. return (int)irq_state->intsts0 & CTSQ_MASK;
  168. }
  169. static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
  170. struct usbhs_irq_state *state)
  171. {
  172. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  173. state->intsts0 = usbhs_read(priv, INTSTS0);
  174. state->intsts1 = usbhs_read(priv, INTSTS1);
  175. /* mask */
  176. if (mod) {
  177. state->brdysts = usbhs_read(priv, BRDYSTS);
  178. state->nrdysts = usbhs_read(priv, NRDYSTS);
  179. state->bempsts = usbhs_read(priv, BEMPSTS);
  180. state->bempsts &= mod->irq_bempsts;
  181. state->brdysts &= mod->irq_brdysts;
  182. }
  183. }
  184. /*
  185. * interrupt
  186. */
  187. #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
  188. #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
  189. static irqreturn_t usbhs_interrupt(int irq, void *data)
  190. {
  191. struct usbhs_priv *priv = data;
  192. struct usbhs_irq_state irq_state;
  193. usbhs_status_get_each_irq(priv, &irq_state);
  194. /*
  195. * clear interrupt
  196. *
  197. * The hardware is _very_ picky to clear interrupt bit.
  198. * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
  199. *
  200. * see
  201. * "Operation"
  202. * - "Control Transfer (DCP)"
  203. * - Function :: VALID bit should 0
  204. */
  205. usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
  206. usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
  207. usbhs_write(priv, BRDYSTS, 0);
  208. usbhs_write(priv, NRDYSTS, 0);
  209. usbhs_write(priv, BEMPSTS, 0);
  210. /*
  211. * call irq callback functions
  212. * see also
  213. * usbhs_irq_setting_update
  214. */
  215. if (irq_state.intsts0 & VBINT)
  216. usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state);
  217. if (irq_state.intsts0 & DVST)
  218. usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
  219. if (irq_state.intsts0 & CTRT)
  220. usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
  221. if (irq_state.intsts0 & BEMP)
  222. usbhs_mod_call(priv, irq_empty, priv, &irq_state);
  223. if (irq_state.intsts0 & BRDY)
  224. usbhs_mod_call(priv, irq_ready, priv, &irq_state);
  225. return IRQ_HANDLED;
  226. }
  227. void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
  228. {
  229. u16 intenb0 = 0;
  230. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  231. /*
  232. * BEMPENB/BRDYENB are picky.
  233. * below method is required
  234. *
  235. * - clear INTSTS0
  236. * - update BEMPENB/BRDYENB
  237. * - update INTSTS0
  238. */
  239. usbhs_write(priv, INTENB0, 0);
  240. usbhs_write(priv, BEMPENB, 0);
  241. usbhs_write(priv, BRDYENB, 0);
  242. /*
  243. * see also
  244. * usbhs_interrupt
  245. */
  246. /*
  247. * it don't enable DVSE (intenb0) here
  248. * but "mod->irq_dev_state" will be called.
  249. */
  250. if (info->irq_vbus)
  251. intenb0 |= VBSE;
  252. if (mod) {
  253. if (mod->irq_ctrl_stage)
  254. intenb0 |= CTRE;
  255. if (mod->irq_empty && mod->irq_bempsts) {
  256. usbhs_write(priv, BEMPENB, mod->irq_bempsts);
  257. intenb0 |= BEMPE;
  258. }
  259. if (mod->irq_ready && mod->irq_brdysts) {
  260. usbhs_write(priv, BRDYENB, mod->irq_brdysts);
  261. intenb0 |= BRDYE;
  262. }
  263. }
  264. if (intenb0)
  265. usbhs_write(priv, INTENB0, intenb0);
  266. }