mod.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. /*
  22. * host / gadget functions
  23. *
  24. * renesas_usbhs host/gadget can register itself by below functions.
  25. * these functions are called when probe
  26. *
  27. */
  28. void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id)
  29. {
  30. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  31. info->mod[id] = mod;
  32. mod->priv = priv;
  33. }
  34. struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
  35. {
  36. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  37. struct usbhs_mod *ret = NULL;
  38. switch (id) {
  39. case USBHS_HOST:
  40. case USBHS_GADGET:
  41. ret = info->mod[id];
  42. break;
  43. }
  44. return ret;
  45. }
  46. int usbhs_mod_is_host(struct usbhs_priv *priv, struct usbhs_mod *mod)
  47. {
  48. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  49. if (!mod)
  50. return -EINVAL;
  51. return info->mod[USBHS_HOST] == mod;
  52. }
  53. struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
  54. {
  55. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  56. return info->curt;
  57. }
  58. int usbhs_mod_change(struct usbhs_priv *priv, int id)
  59. {
  60. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  61. struct usbhs_mod *mod = NULL;
  62. int ret = 0;
  63. /* id < 0 mean no current */
  64. switch (id) {
  65. case USBHS_HOST:
  66. case USBHS_GADGET:
  67. mod = info->mod[id];
  68. break;
  69. default:
  70. ret = -EINVAL;
  71. }
  72. info->curt = mod;
  73. return ret;
  74. }
  75. static irqreturn_t usbhs_interrupt(int irq, void *data);
  76. int usbhs_mod_probe(struct usbhs_priv *priv)
  77. {
  78. struct device *dev = usbhs_priv_to_dev(priv);
  79. int ret;
  80. /*
  81. * install host/gadget driver
  82. */
  83. ret = usbhs_mod_gadget_probe(priv);
  84. if (ret < 0)
  85. return ret;
  86. /* irq settings */
  87. ret = request_irq(priv->irq, usbhs_interrupt,
  88. IRQF_DISABLED, dev_name(dev), priv);
  89. if (ret) {
  90. dev_err(dev, "irq request err\n");
  91. goto mod_init_gadget_err;
  92. }
  93. return ret;
  94. mod_init_gadget_err:
  95. usbhs_mod_gadget_remove(priv);
  96. return ret;
  97. }
  98. void usbhs_mod_remove(struct usbhs_priv *priv)
  99. {
  100. usbhs_mod_gadget_remove(priv);
  101. free_irq(priv->irq, priv);
  102. }
  103. /*
  104. * status functions
  105. */
  106. int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state)
  107. {
  108. switch (irq_state->dvstctr & RHST) {
  109. case RHST_LOW_SPEED:
  110. return USB_SPEED_LOW;
  111. case RHST_FULL_SPEED:
  112. return USB_SPEED_FULL;
  113. case RHST_HIGH_SPEED:
  114. return USB_SPEED_HIGH;
  115. }
  116. return USB_SPEED_UNKNOWN;
  117. }
  118. int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
  119. {
  120. int state = irq_state->intsts0 & DVSQ_MASK;
  121. switch (state) {
  122. case POWER_STATE:
  123. case DEFAULT_STATE:
  124. case ADDRESS_STATE:
  125. case CONFIGURATION_STATE:
  126. return state;
  127. }
  128. return -EIO;
  129. }
  130. int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
  131. {
  132. /*
  133. * return value
  134. *
  135. * IDLE_SETUP_STAGE
  136. * READ_DATA_STAGE
  137. * READ_STATUS_STAGE
  138. * WRITE_DATA_STAGE
  139. * WRITE_STATUS_STAGE
  140. * NODATA_STATUS_STAGE
  141. * SEQUENCE_ERROR
  142. */
  143. return (int)irq_state->intsts0 & CTSQ_MASK;
  144. }
  145. static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
  146. struct usbhs_irq_state *state)
  147. {
  148. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  149. state->intsts0 = usbhs_read(priv, INTSTS0);
  150. state->intsts1 = usbhs_read(priv, INTSTS1);
  151. state->brdysts = usbhs_read(priv, BRDYSTS);
  152. state->nrdysts = usbhs_read(priv, NRDYSTS);
  153. state->bempsts = usbhs_read(priv, BEMPSTS);
  154. state->dvstctr = usbhs_read(priv, DVSTCTR);
  155. /* mask */
  156. state->bempsts &= mod->irq_bempsts;
  157. state->brdysts &= mod->irq_brdysts;
  158. }
  159. /*
  160. * interrupt
  161. */
  162. #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
  163. #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
  164. static irqreturn_t usbhs_interrupt(int irq, void *data)
  165. {
  166. struct usbhs_priv *priv = data;
  167. struct usbhs_irq_state irq_state;
  168. usbhs_status_get_each_irq(priv, &irq_state);
  169. /*
  170. * clear interrupt
  171. *
  172. * The hardware is _very_ picky to clear interrupt bit.
  173. * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
  174. *
  175. * see
  176. * "Operation"
  177. * - "Control Transfer (DCP)"
  178. * - Function :: VALID bit should 0
  179. */
  180. usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
  181. usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
  182. usbhs_write(priv, BRDYSTS, 0);
  183. usbhs_write(priv, NRDYSTS, 0);
  184. usbhs_write(priv, BEMPSTS, 0);
  185. /*
  186. * call irq callback functions
  187. * see also
  188. * usbhs_irq_setting_update
  189. */
  190. if (irq_state.intsts0 & DVST)
  191. usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
  192. if (irq_state.intsts0 & CTRT)
  193. usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
  194. if (irq_state.intsts0 & BEMP)
  195. usbhs_mod_call(priv, irq_empty, priv, &irq_state);
  196. if (irq_state.intsts0 & BRDY)
  197. usbhs_mod_call(priv, irq_ready, priv, &irq_state);
  198. return IRQ_HANDLED;
  199. }
  200. void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
  201. {
  202. u16 intenb0 = 0;
  203. usbhs_write(priv, INTENB0, 0);
  204. usbhs_write(priv, BEMPENB, 0);
  205. usbhs_write(priv, BRDYENB, 0);
  206. /*
  207. * see also
  208. * usbhs_interrupt
  209. */
  210. /*
  211. * it don't enable DVSE (intenb0) here
  212. * but "mod->irq_dev_state" will be called.
  213. */
  214. if (mod->irq_ctrl_stage)
  215. intenb0 |= CTRE;
  216. if (mod->irq_empty && mod->irq_bempsts) {
  217. usbhs_write(priv, BEMPENB, mod->irq_bempsts);
  218. intenb0 |= BEMPE;
  219. }
  220. if (mod->irq_ready && mod->irq_brdysts) {
  221. usbhs_write(priv, BRDYENB, mod->irq_brdysts);
  222. intenb0 |= BRDYE;
  223. }
  224. usbhs_write(priv, INTENB0, intenb0);
  225. }