omap2430.c 8.0 KB

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