mod.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, struct usbhs_mod *mod)
  82. {
  83. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  84. if (!mod)
  85. return -EINVAL;
  86. return info->mod[USBHS_HOST] == mod;
  87. }
  88. struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
  89. {
  90. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  91. return info->curt;
  92. }
  93. int usbhs_mod_change(struct usbhs_priv *priv, int id)
  94. {
  95. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  96. struct usbhs_mod *mod = NULL;
  97. int ret = 0;
  98. /* id < 0 mean no current */
  99. switch (id) {
  100. case USBHS_HOST:
  101. case USBHS_GADGET:
  102. mod = info->mod[id];
  103. break;
  104. default:
  105. ret = -EINVAL;
  106. }
  107. info->curt = mod;
  108. return ret;
  109. }
  110. static irqreturn_t usbhs_interrupt(int irq, void *data);
  111. int usbhs_mod_probe(struct usbhs_priv *priv)
  112. {
  113. struct device *dev = usbhs_priv_to_dev(priv);
  114. int ret;
  115. /*
  116. * install host/gadget driver
  117. */
  118. ret = usbhs_mod_gadget_probe(priv);
  119. if (ret < 0)
  120. return ret;
  121. /* irq settings */
  122. ret = request_irq(priv->irq, usbhs_interrupt,
  123. IRQF_DISABLED, dev_name(dev), priv);
  124. if (ret) {
  125. dev_err(dev, "irq request err\n");
  126. goto mod_init_gadget_err;
  127. }
  128. return ret;
  129. mod_init_gadget_err:
  130. usbhs_mod_gadget_remove(priv);
  131. return ret;
  132. }
  133. void usbhs_mod_remove(struct usbhs_priv *priv)
  134. {
  135. usbhs_mod_gadget_remove(priv);
  136. free_irq(priv->irq, priv);
  137. }
  138. /*
  139. * status functions
  140. */
  141. int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state)
  142. {
  143. switch (irq_state->dvstctr & RHST) {
  144. case RHST_LOW_SPEED:
  145. return USB_SPEED_LOW;
  146. case RHST_FULL_SPEED:
  147. return USB_SPEED_FULL;
  148. case RHST_HIGH_SPEED:
  149. return USB_SPEED_HIGH;
  150. }
  151. return USB_SPEED_UNKNOWN;
  152. }
  153. int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
  154. {
  155. int state = irq_state->intsts0 & DVSQ_MASK;
  156. switch (state) {
  157. case POWER_STATE:
  158. case DEFAULT_STATE:
  159. case ADDRESS_STATE:
  160. case CONFIGURATION_STATE:
  161. return state;
  162. }
  163. return -EIO;
  164. }
  165. int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
  166. {
  167. /*
  168. * return value
  169. *
  170. * IDLE_SETUP_STAGE
  171. * READ_DATA_STAGE
  172. * READ_STATUS_STAGE
  173. * WRITE_DATA_STAGE
  174. * WRITE_STATUS_STAGE
  175. * NODATA_STATUS_STAGE
  176. * SEQUENCE_ERROR
  177. */
  178. return (int)irq_state->intsts0 & CTSQ_MASK;
  179. }
  180. static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
  181. struct usbhs_irq_state *state)
  182. {
  183. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  184. state->intsts0 = usbhs_read(priv, INTSTS0);
  185. state->intsts1 = usbhs_read(priv, INTSTS1);
  186. state->dvstctr = usbhs_read(priv, DVSTCTR);
  187. /* mask */
  188. if (mod) {
  189. state->brdysts = usbhs_read(priv, BRDYSTS);
  190. state->nrdysts = usbhs_read(priv, NRDYSTS);
  191. state->bempsts = usbhs_read(priv, BEMPSTS);
  192. state->bempsts &= mod->irq_bempsts;
  193. state->brdysts &= mod->irq_brdysts;
  194. }
  195. }
  196. /*
  197. * interrupt
  198. */
  199. #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
  200. #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
  201. static irqreturn_t usbhs_interrupt(int irq, void *data)
  202. {
  203. struct usbhs_priv *priv = data;
  204. struct usbhs_irq_state irq_state;
  205. usbhs_status_get_each_irq(priv, &irq_state);
  206. /*
  207. * clear interrupt
  208. *
  209. * The hardware is _very_ picky to clear interrupt bit.
  210. * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
  211. *
  212. * see
  213. * "Operation"
  214. * - "Control Transfer (DCP)"
  215. * - Function :: VALID bit should 0
  216. */
  217. usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
  218. usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
  219. usbhs_write(priv, BRDYSTS, 0);
  220. usbhs_write(priv, NRDYSTS, 0);
  221. usbhs_write(priv, BEMPSTS, 0);
  222. /*
  223. * call irq callback functions
  224. * see also
  225. * usbhs_irq_setting_update
  226. */
  227. if (irq_state.intsts0 & VBINT)
  228. usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state);
  229. if (irq_state.intsts0 & DVST)
  230. usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
  231. if (irq_state.intsts0 & CTRT)
  232. usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
  233. if (irq_state.intsts0 & BEMP)
  234. usbhs_mod_call(priv, irq_empty, priv, &irq_state);
  235. if (irq_state.intsts0 & BRDY)
  236. usbhs_mod_call(priv, irq_ready, priv, &irq_state);
  237. return IRQ_HANDLED;
  238. }
  239. void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
  240. {
  241. u16 intenb0 = 0;
  242. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  243. usbhs_write(priv, INTENB0, 0);
  244. usbhs_write(priv, BEMPENB, 0);
  245. usbhs_write(priv, BRDYENB, 0);
  246. /*
  247. * see also
  248. * usbhs_interrupt
  249. */
  250. /*
  251. * it don't enable DVSE (intenb0) here
  252. * but "mod->irq_dev_state" will be called.
  253. */
  254. if (info->irq_vbus)
  255. intenb0 |= VBSE;
  256. if (mod) {
  257. if (mod->irq_ctrl_stage)
  258. intenb0 |= CTRE;
  259. if (mod->irq_empty && mod->irq_bempsts) {
  260. usbhs_write(priv, BEMPENB, mod->irq_bempsts);
  261. intenb0 |= BEMPE;
  262. }
  263. if (mod->irq_ready && mod->irq_brdysts) {
  264. usbhs_write(priv, BRDYENB, mod->irq_brdysts);
  265. intenb0 |= BRDYE;
  266. }
  267. }
  268. usbhs_write(priv, INTENB0, intenb0);
  269. }