mod.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. renesas_usbhs_call_notify_hotplug(pdev);
  48. return 0;
  49. }
  50. void usbhs_mod_autonomy_mode(struct usbhs_priv *priv)
  51. {
  52. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  53. info->irq_vbus = usbhsm_autonomy_irq_vbus;
  54. priv->pfunc.get_vbus = usbhsm_autonomy_get_vbus;
  55. usbhs_irq_callback_update(priv, NULL);
  56. }
  57. /*
  58. * host / gadget functions
  59. *
  60. * renesas_usbhs host/gadget can register itself by below functions.
  61. * these functions are called when probe
  62. *
  63. */
  64. void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id)
  65. {
  66. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  67. info->mod[id] = mod;
  68. mod->priv = priv;
  69. }
  70. struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id)
  71. {
  72. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  73. struct usbhs_mod *ret = NULL;
  74. switch (id) {
  75. case USBHS_HOST:
  76. case USBHS_GADGET:
  77. ret = info->mod[id];
  78. break;
  79. }
  80. return ret;
  81. }
  82. int usbhs_mod_is_host(struct usbhs_priv *priv)
  83. {
  84. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  85. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  86. if (!mod)
  87. return -EINVAL;
  88. return info->mod[USBHS_HOST] == mod;
  89. }
  90. struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv)
  91. {
  92. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  93. return info->curt;
  94. }
  95. int usbhs_mod_change(struct usbhs_priv *priv, int id)
  96. {
  97. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  98. struct usbhs_mod *mod = NULL;
  99. int ret = 0;
  100. /* id < 0 mean no current */
  101. switch (id) {
  102. case USBHS_HOST:
  103. case USBHS_GADGET:
  104. mod = info->mod[id];
  105. break;
  106. default:
  107. ret = -EINVAL;
  108. }
  109. info->curt = mod;
  110. return ret;
  111. }
  112. static irqreturn_t usbhs_interrupt(int irq, void *data);
  113. int usbhs_mod_probe(struct usbhs_priv *priv)
  114. {
  115. struct device *dev = usbhs_priv_to_dev(priv);
  116. int ret;
  117. /*
  118. * install host/gadget driver
  119. */
  120. ret = usbhs_mod_host_probe(priv);
  121. if (ret < 0)
  122. return ret;
  123. ret = usbhs_mod_gadget_probe(priv);
  124. if (ret < 0)
  125. goto mod_init_host_err;
  126. /* irq settings */
  127. ret = devm_request_irq(dev, priv->irq, usbhs_interrupt,
  128. priv->irqflags, dev_name(dev), priv);
  129. if (ret) {
  130. dev_err(dev, "irq request err\n");
  131. goto mod_init_gadget_err;
  132. }
  133. return ret;
  134. mod_init_gadget_err:
  135. usbhs_mod_gadget_remove(priv);
  136. mod_init_host_err:
  137. usbhs_mod_host_remove(priv);
  138. return ret;
  139. }
  140. void usbhs_mod_remove(struct usbhs_priv *priv)
  141. {
  142. usbhs_mod_host_remove(priv);
  143. usbhs_mod_gadget_remove(priv);
  144. }
  145. /*
  146. * status functions
  147. */
  148. int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
  149. {
  150. int state = irq_state->intsts0 & DVSQ_MASK;
  151. switch (state) {
  152. case POWER_STATE:
  153. case DEFAULT_STATE:
  154. case ADDRESS_STATE:
  155. case CONFIGURATION_STATE:
  156. return state;
  157. }
  158. return -EIO;
  159. }
  160. int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
  161. {
  162. /*
  163. * return value
  164. *
  165. * IDLE_SETUP_STAGE
  166. * READ_DATA_STAGE
  167. * READ_STATUS_STAGE
  168. * WRITE_DATA_STAGE
  169. * WRITE_STATUS_STAGE
  170. * NODATA_STATUS_STAGE
  171. * SEQUENCE_ERROR
  172. */
  173. return (int)irq_state->intsts0 & CTSQ_MASK;
  174. }
  175. static int usbhs_status_get_each_irq(struct usbhs_priv *priv,
  176. struct usbhs_irq_state *state)
  177. {
  178. struct usbhs_mod *mod = usbhs_mod_get_current(priv);
  179. u16 intenb0, intenb1;
  180. unsigned long flags;
  181. /******************** spin lock ********************/
  182. usbhs_lock(priv, flags);
  183. state->intsts0 = usbhs_read(priv, INTSTS0);
  184. intenb0 = usbhs_read(priv, INTENB0);
  185. if (usbhs_mod_is_host(priv)) {
  186. state->intsts1 = usbhs_read(priv, INTSTS1);
  187. intenb1 = usbhs_read(priv, INTENB1);
  188. } else {
  189. state->intsts1 = intenb1 = 0;
  190. }
  191. /* mask */
  192. if (mod) {
  193. state->brdysts = usbhs_read(priv, BRDYSTS);
  194. state->nrdysts = usbhs_read(priv, NRDYSTS);
  195. state->bempsts = usbhs_read(priv, BEMPSTS);
  196. state->bempsts &= mod->irq_bempsts;
  197. state->brdysts &= mod->irq_brdysts;
  198. }
  199. usbhs_unlock(priv, flags);
  200. /******************** spin unlock ******************/
  201. /*
  202. * Check whether the irq enable registers and the irq status are set
  203. * when IRQF_SHARED is set.
  204. */
  205. if (priv->irqflags & IRQF_SHARED) {
  206. if (!(intenb0 & state->intsts0) &&
  207. !(intenb1 & state->intsts1) &&
  208. !(state->bempsts) &&
  209. !(state->brdysts))
  210. return -EIO;
  211. }
  212. return 0;
  213. }
  214. /*
  215. * interrupt
  216. */
  217. #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
  218. #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
  219. static irqreturn_t usbhs_interrupt(int irq, void *data)
  220. {
  221. struct usbhs_priv *priv = data;
  222. struct usbhs_irq_state irq_state;
  223. if (usbhs_status_get_each_irq(priv, &irq_state) < 0)
  224. return IRQ_NONE;
  225. /*
  226. * clear interrupt
  227. *
  228. * The hardware is _very_ picky to clear interrupt bit.
  229. * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
  230. *
  231. * see
  232. * "Operation"
  233. * - "Control Transfer (DCP)"
  234. * - Function :: VALID bit should 0
  235. */
  236. usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
  237. if (usbhs_mod_is_host(priv))
  238. usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
  239. /*
  240. * The driver should not clear the xxxSTS after the line of
  241. * "call irq callback functions" because each "if" statement is
  242. * possible to call the callback function for avoiding any side effects.
  243. */
  244. if (irq_state.intsts0 & BRDY)
  245. usbhs_write(priv, BRDYSTS, ~irq_state.brdysts);
  246. usbhs_write(priv, NRDYSTS, ~irq_state.nrdysts);
  247. if (irq_state.intsts0 & BEMP)
  248. usbhs_write(priv, BEMPSTS, ~irq_state.bempsts);
  249. /*
  250. * call irq callback functions
  251. * see also
  252. * usbhs_irq_setting_update
  253. */
  254. /* INTSTS0 */
  255. if (irq_state.intsts0 & VBINT)
  256. usbhs_mod_info_call(priv, irq_vbus, priv, &irq_state);
  257. if (irq_state.intsts0 & DVST)
  258. usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
  259. if (irq_state.intsts0 & CTRT)
  260. usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
  261. if (irq_state.intsts0 & BEMP)
  262. usbhs_mod_call(priv, irq_empty, priv, &irq_state);
  263. if (irq_state.intsts0 & BRDY)
  264. usbhs_mod_call(priv, irq_ready, priv, &irq_state);
  265. if (usbhs_mod_is_host(priv)) {
  266. /* INTSTS1 */
  267. if (irq_state.intsts1 & ATTCH)
  268. usbhs_mod_call(priv, irq_attch, priv, &irq_state);
  269. if (irq_state.intsts1 & DTCH)
  270. usbhs_mod_call(priv, irq_dtch, priv, &irq_state);
  271. if (irq_state.intsts1 & SIGN)
  272. usbhs_mod_call(priv, irq_sign, priv, &irq_state);
  273. if (irq_state.intsts1 & SACK)
  274. usbhs_mod_call(priv, irq_sack, priv, &irq_state);
  275. }
  276. return IRQ_HANDLED;
  277. }
  278. void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
  279. {
  280. u16 intenb0 = 0;
  281. u16 intenb1 = 0;
  282. struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv);
  283. /*
  284. * BEMPENB/BRDYENB are picky.
  285. * below method is required
  286. *
  287. * - clear INTSTS0
  288. * - update BEMPENB/BRDYENB
  289. * - update INTSTS0
  290. */
  291. usbhs_write(priv, INTENB0, 0);
  292. if (usbhs_mod_is_host(priv))
  293. usbhs_write(priv, INTENB1, 0);
  294. usbhs_write(priv, BEMPENB, 0);
  295. usbhs_write(priv, BRDYENB, 0);
  296. /*
  297. * see also
  298. * usbhs_interrupt
  299. */
  300. /*
  301. * it don't enable DVSE (intenb0) here
  302. * but "mod->irq_dev_state" will be called.
  303. */
  304. if (info->irq_vbus)
  305. intenb0 |= VBSE;
  306. if (mod) {
  307. /*
  308. * INTSTS0
  309. */
  310. if (mod->irq_ctrl_stage)
  311. intenb0 |= CTRE;
  312. if (mod->irq_empty && mod->irq_bempsts) {
  313. usbhs_write(priv, BEMPENB, mod->irq_bempsts);
  314. intenb0 |= BEMPE;
  315. }
  316. if (mod->irq_ready && mod->irq_brdysts) {
  317. usbhs_write(priv, BRDYENB, mod->irq_brdysts);
  318. intenb0 |= BRDYE;
  319. }
  320. if (usbhs_mod_is_host(priv)) {
  321. /*
  322. * INTSTS1
  323. */
  324. if (mod->irq_attch)
  325. intenb1 |= ATTCHE;
  326. if (mod->irq_dtch)
  327. intenb1 |= DTCHE;
  328. if (mod->irq_sign)
  329. intenb1 |= SIGNE;
  330. if (mod->irq_sack)
  331. intenb1 |= SACKE;
  332. }
  333. }
  334. if (intenb0)
  335. usbhs_write(priv, INTENB0, intenb0);
  336. if (usbhs_mod_is_host(priv) && intenb1)
  337. usbhs_write(priv, INTENB1, intenb1);
  338. }