omap2430.c 8.0 KB

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