omap2430.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2005-2007 by Texas Instruments
  3. * Some code has been taken from tusb6010.c
  4. * Copyrights for that are attributable to:
  5. * Copyright (C) 2006 Nokia Corporation
  6. * Tony Lindgren <tony@atomide.com>
  7. *
  8. * This file is part of the Inventra Controller Driver for Linux.
  9. *
  10. * The Inventra Controller Driver for Linux is free software; you
  11. * can redistribute it and/or modify it under the terms of the GNU
  12. * General Public License version 2 as published by the Free Software
  13. * Foundation.
  14. *
  15. * The Inventra Controller Driver for Linux is distributed in
  16. * the hope that it will be useful, but WITHOUT ANY WARRANTY;
  17. * without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  19. * License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with The Inventra Controller Driver for Linux ; if not,
  23. * write to the Free Software Foundation, Inc., 59 Temple Place,
  24. * Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/clk.h>
  33. #include <linux/io.h>
  34. #include "musb_core.h"
  35. #include "omap2430.h"
  36. static struct timer_list musb_idle_timer;
  37. static void musb_do_idle(unsigned long _musb)
  38. {
  39. struct musb *musb = (void *)_musb;
  40. unsigned long flags;
  41. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  42. u8 power;
  43. #endif
  44. u8 devctl;
  45. spin_lock_irqsave(&musb->lock, flags);
  46. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  47. switch (musb->xceiv->state) {
  48. case OTG_STATE_A_WAIT_BCON:
  49. devctl &= ~MUSB_DEVCTL_SESSION;
  50. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  51. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  52. if (devctl & MUSB_DEVCTL_BDEVICE) {
  53. musb->xceiv->state = OTG_STATE_B_IDLE;
  54. MUSB_DEV_MODE(musb);
  55. } else {
  56. musb->xceiv->state = OTG_STATE_A_IDLE;
  57. MUSB_HST_MODE(musb);
  58. }
  59. break;
  60. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  61. case OTG_STATE_A_SUSPEND:
  62. /* finish RESUME signaling? */
  63. if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
  64. power = musb_readb(musb->mregs, MUSB_POWER);
  65. power &= ~MUSB_POWER_RESUME;
  66. DBG(1, "root port resume stopped, power %02x\n", power);
  67. musb_writeb(musb->mregs, MUSB_POWER, power);
  68. musb->is_active = 1;
  69. musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
  70. | MUSB_PORT_STAT_RESUME);
  71. musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
  72. usb_hcd_poll_rh_status(musb_to_hcd(musb));
  73. /* NOTE: it might really be A_WAIT_BCON ... */
  74. musb->xceiv->state = OTG_STATE_A_HOST;
  75. }
  76. break;
  77. #endif
  78. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  79. case OTG_STATE_A_HOST:
  80. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  81. if (devctl & MUSB_DEVCTL_BDEVICE)
  82. musb->xceiv->state = OTG_STATE_B_IDLE;
  83. else
  84. musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
  85. #endif
  86. default:
  87. break;
  88. }
  89. spin_unlock_irqrestore(&musb->lock, flags);
  90. }
  91. void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
  92. {
  93. unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
  94. static unsigned long last_timer;
  95. if (timeout == 0)
  96. timeout = default_timeout;
  97. /* Never idle if active, or when VBUS timeout is not set as host */
  98. if (musb->is_active || ((musb->a_wait_bcon == 0)
  99. && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
  100. DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
  101. del_timer(&musb_idle_timer);
  102. last_timer = jiffies;
  103. return;
  104. }
  105. if (time_after(last_timer, timeout)) {
  106. if (!timer_pending(&musb_idle_timer))
  107. last_timer = timeout;
  108. else {
  109. DBG(4, "Longer idle timer already pending, ignoring\n");
  110. return;
  111. }
  112. }
  113. last_timer = timeout;
  114. DBG(4, "%s inactive, for idle timer for %lu ms\n",
  115. otg_state_string(musb),
  116. (unsigned long)jiffies_to_msecs(timeout - jiffies));
  117. mod_timer(&musb_idle_timer, timeout);
  118. }
  119. void musb_platform_enable(struct musb *musb)
  120. {
  121. }
  122. void musb_platform_disable(struct musb *musb)
  123. {
  124. }
  125. static void omap_set_vbus(struct musb *musb, int is_on)
  126. {
  127. u8 devctl;
  128. /* HDRC controls CPEN, but beware current surges during device
  129. * connect. They can trigger transient overcurrent conditions
  130. * that must be ignored.
  131. */
  132. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  133. if (is_on) {
  134. musb->is_active = 1;
  135. musb->xceiv->default_a = 1;
  136. musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
  137. devctl |= MUSB_DEVCTL_SESSION;
  138. MUSB_HST_MODE(musb);
  139. } else {
  140. musb->is_active = 0;
  141. /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
  142. * jumping right to B_IDLE...
  143. */
  144. musb->xceiv->default_a = 0;
  145. musb->xceiv->state = OTG_STATE_B_IDLE;
  146. devctl &= ~MUSB_DEVCTL_SESSION;
  147. MUSB_DEV_MODE(musb);
  148. }
  149. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  150. DBG(1, "VBUS %s, devctl %02x "
  151. /* otg %3x conf %08x prcm %08x */ "\n",
  152. otg_state_string(musb),
  153. musb_readb(musb->mregs, MUSB_DEVCTL));
  154. }
  155. static int musb_platform_resume(struct musb *musb);
  156. int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
  157. {
  158. u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  159. devctl |= MUSB_DEVCTL_SESSION;
  160. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  161. return 0;
  162. }
  163. int __init musb_platform_init(struct musb *musb, void *board_data)
  164. {
  165. u32 l;
  166. struct omap_musb_board_data *data = board_data;
  167. /* We require some kind of external transceiver, hooked
  168. * up through ULPI. TWL4030-family PMICs include one,
  169. * which needs a driver, drivers aren't always needed.
  170. */
  171. musb->xceiv = otg_get_transceiver();
  172. if (!musb->xceiv) {
  173. pr_err("HS USB OTG: no transceiver configured\n");
  174. return -ENODEV;
  175. }
  176. musb_platform_resume(musb);
  177. l = musb_readl(musb->mregs, OTG_SYSCONFIG);
  178. l &= ~ENABLEWAKEUP; /* disable wakeup */
  179. l &= ~NOSTDBY; /* remove possible nostdby */
  180. l |= SMARTSTDBY; /* enable smart standby */
  181. l &= ~AUTOIDLE; /* disable auto idle */
  182. l &= ~NOIDLE; /* remove possible noidle */
  183. l |= SMARTIDLE; /* enable smart idle */
  184. /*
  185. * MUSB AUTOIDLE don't work in 3430.
  186. * Workaround by Richard Woodruff/TI
  187. */
  188. if (!cpu_is_omap3430())
  189. l |= AUTOIDLE; /* enable auto idle */
  190. musb_writel(musb->mregs, OTG_SYSCONFIG, l);
  191. l = musb_readl(musb->mregs, OTG_INTERFSEL);
  192. if (data->interface_type == MUSB_INTERFACE_UTMI) {
  193. /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
  194. l &= ~ULPI_12PIN; /* Disable ULPI */
  195. l |= UTMI_8BIT; /* Enable UTMI */
  196. } else {
  197. l |= ULPI_12PIN;
  198. }
  199. musb_writel(musb->mregs, OTG_INTERFSEL, l);
  200. pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
  201. "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
  202. musb_readl(musb->mregs, OTG_REVISION),
  203. musb_readl(musb->mregs, OTG_SYSCONFIG),
  204. musb_readl(musb->mregs, OTG_SYSSTATUS),
  205. musb_readl(musb->mregs, OTG_INTERFSEL),
  206. musb_readl(musb->mregs, OTG_SIMENABLE));
  207. if (is_host_enabled(musb))
  208. musb->board_set_vbus = omap_set_vbus;
  209. setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
  210. return 0;
  211. }
  212. #ifdef CONFIG_PM
  213. void musb_platform_save_context(struct musb *musb,
  214. struct musb_context_registers *musb_context)
  215. {
  216. musb_context->otg_sysconfig = musb_readl(musb->mregs, OTG_SYSCONFIG);
  217. musb_context->otg_forcestandby = musb_readl(musb->mregs, OTG_FORCESTDBY);
  218. }
  219. void musb_platform_restore_context(struct musb *musb,
  220. struct musb_context_registers *musb_context)
  221. {
  222. musb_writel(musb->mregs, OTG_SYSCONFIG, musb_context->otg_sysconfig);
  223. musb_writel(musb->mregs, OTG_FORCESTDBY, musb_context->otg_forcestandby);
  224. }
  225. #endif
  226. static int musb_platform_suspend(struct musb *musb)
  227. {
  228. u32 l;
  229. if (!musb->clock)
  230. return 0;
  231. /* in any role */
  232. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  233. l |= ENABLEFORCE; /* enable MSTANDBY */
  234. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  235. l = musb_readl(musb->mregs, OTG_SYSCONFIG);
  236. l |= ENABLEWAKEUP; /* enable wakeup */
  237. musb_writel(musb->mregs, OTG_SYSCONFIG, l);
  238. otg_set_suspend(musb->xceiv, 1);
  239. if (musb->set_clock)
  240. musb->set_clock(musb->clock, 0);
  241. else
  242. clk_disable(musb->clock);
  243. return 0;
  244. }
  245. static int musb_platform_resume(struct musb *musb)
  246. {
  247. u32 l;
  248. if (!musb->clock)
  249. return 0;
  250. otg_set_suspend(musb->xceiv, 0);
  251. if (musb->set_clock)
  252. musb->set_clock(musb->clock, 1);
  253. else
  254. clk_enable(musb->clock);
  255. l = musb_readl(musb->mregs, OTG_SYSCONFIG);
  256. l &= ~ENABLEWAKEUP; /* disable wakeup */
  257. musb_writel(musb->mregs, OTG_SYSCONFIG, l);
  258. l = musb_readl(musb->mregs, OTG_FORCESTDBY);
  259. l &= ~ENABLEFORCE; /* disable MSTANDBY */
  260. musb_writel(musb->mregs, OTG_FORCESTDBY, l);
  261. return 0;
  262. }
  263. int musb_platform_exit(struct musb *musb)
  264. {
  265. musb_platform_suspend(musb);
  266. return 0;
  267. }