blob: e06d65e36bf742eacef80056ee430890c8220bcb [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
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
Felipe Balbi550a7372008-07-24 12:27:36 +03006 * 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>
Felipe Balbi550a7372008-07-24 12:27:36 +030030#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/clk.h>
33#include <linux/io.h>
34
Tony Lindgrence491cf2009-10-20 09:40:47 -070035#include <plat/mux.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030036
37#include "musb_core.h"
38#include "omap2430.h"
39
Felipe Balbi550a7372008-07-24 12:27:36 +030040
41static struct timer_list musb_idle_timer;
42
43static void musb_do_idle(unsigned long _musb)
44{
45 struct musb *musb = (void *)_musb;
46 unsigned long flags;
Ajay Kumar Guptaeef767b2008-10-29 15:10:38 +020047#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +030048 u8 power;
Ajay Kumar Guptaeef767b2008-10-29 15:10:38 +020049#endif
Felipe Balbi550a7372008-07-24 12:27:36 +030050 u8 devctl;
51
Felipe Balbi550a7372008-07-24 12:27:36 +030052 spin_lock_irqsave(&musb->lock, flags);
53
David Brownell71783e02008-11-24 13:06:49 +020054 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
55
David Brownell84e250f2009-03-31 12:30:04 -070056 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +030057 case OTG_STATE_A_WAIT_BCON:
58 devctl &= ~MUSB_DEVCTL_SESSION;
59 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
60
61 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
62 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -070063 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030064 MUSB_DEV_MODE(musb);
65 } else {
David Brownell84e250f2009-03-31 12:30:04 -070066 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030067 MUSB_HST_MODE(musb);
68 }
69 break;
70#ifdef CONFIG_USB_MUSB_HDRC_HCD
71 case OTG_STATE_A_SUSPEND:
72 /* finish RESUME signaling? */
73 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
74 power = musb_readb(musb->mregs, MUSB_POWER);
75 power &= ~MUSB_POWER_RESUME;
76 DBG(1, "root port resume stopped, power %02x\n", power);
77 musb_writeb(musb->mregs, MUSB_POWER, power);
78 musb->is_active = 1;
79 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
80 | MUSB_PORT_STAT_RESUME);
81 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
82 usb_hcd_poll_rh_status(musb_to_hcd(musb));
83 /* NOTE: it might really be A_WAIT_BCON ... */
David Brownell84e250f2009-03-31 12:30:04 -070084 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +030085 }
86 break;
87#endif
88#ifdef CONFIG_USB_MUSB_HDRC_HCD
89 case OTG_STATE_A_HOST:
90 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
91 if (devctl & MUSB_DEVCTL_BDEVICE)
David Brownell84e250f2009-03-31 12:30:04 -070092 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030093 else
David Brownell84e250f2009-03-31 12:30:04 -070094 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +030095#endif
96 default:
97 break;
98 }
99 spin_unlock_irqrestore(&musb->lock, flags);
100}
101
102
103void musb_platform_try_idle(struct musb *musb, unsigned long timeout)
104{
105 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
106 static unsigned long last_timer;
107
108 if (timeout == 0)
109 timeout = default_timeout;
110
111 /* Never idle if active, or when VBUS timeout is not set as host */
112 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700113 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300114 DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
115 del_timer(&musb_idle_timer);
116 last_timer = jiffies;
117 return;
118 }
119
120 if (time_after(last_timer, timeout)) {
121 if (!timer_pending(&musb_idle_timer))
122 last_timer = timeout;
123 else {
124 DBG(4, "Longer idle timer already pending, ignoring\n");
125 return;
126 }
127 }
128 last_timer = timeout;
129
130 DBG(4, "%s inactive, for idle timer for %lu ms\n",
131 otg_state_string(musb),
132 (unsigned long)jiffies_to_msecs(timeout - jiffies));
133 mod_timer(&musb_idle_timer, timeout);
134}
135
136void musb_platform_enable(struct musb *musb)
137{
138}
139void musb_platform_disable(struct musb *musb)
140{
141}
Felipe Balbi550a7372008-07-24 12:27:36 +0300142static void omap_set_vbus(struct musb *musb, int is_on)
143{
144 u8 devctl;
145 /* HDRC controls CPEN, but beware current surges during device
146 * connect. They can trigger transient overcurrent conditions
147 * that must be ignored.
148 */
149
150 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
151
152 if (is_on) {
153 musb->is_active = 1;
David Brownell84e250f2009-03-31 12:30:04 -0700154 musb->xceiv->default_a = 1;
155 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300156 devctl |= MUSB_DEVCTL_SESSION;
157
158 MUSB_HST_MODE(musb);
159 } else {
160 musb->is_active = 0;
161
162 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
163 * jumping right to B_IDLE...
164 */
165
David Brownell84e250f2009-03-31 12:30:04 -0700166 musb->xceiv->default_a = 0;
167 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300168 devctl &= ~MUSB_DEVCTL_SESSION;
169
170 MUSB_DEV_MODE(musb);
171 }
172 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
173
174 DBG(1, "VBUS %s, devctl %02x "
175 /* otg %3x conf %08x prcm %08x */ "\n",
176 otg_state_string(musb),
177 musb_readb(musb->mregs, MUSB_DEVCTL));
178}
Felipe Balbi550a7372008-07-24 12:27:36 +0300179
180static int musb_platform_resume(struct musb *musb);
181
David Brownell96a274d2008-11-24 13:06:47 +0200182int musb_platform_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300183{
184 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
185
186 devctl |= MUSB_DEVCTL_SESSION;
187 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
188
David Brownell96a274d2008-11-24 13:06:47 +0200189 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300190}
191
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200192int __init musb_platform_init(struct musb *musb, void *board_data)
Felipe Balbi550a7372008-07-24 12:27:36 +0300193{
194 u32 l;
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200195 struct omap_musb_board_data *data = board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300196
197#if defined(CONFIG_ARCH_OMAP2430)
198 omap_cfg_reg(AE5_2430_USB0HS_STP);
199#endif
200
David Brownell84e250f2009-03-31 12:30:04 -0700201 /* We require some kind of external transceiver, hooked
202 * up through ULPI. TWL4030-family PMICs include one,
203 * which needs a driver, drivers aren't always needed.
204 */
205 musb->xceiv = otg_get_transceiver();
206 if (!musb->xceiv) {
207 pr_err("HS USB OTG: no transceiver configured\n");
208 return -ENODEV;
209 }
210
Felipe Balbi550a7372008-07-24 12:27:36 +0300211 musb_platform_resume(musb);
212
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200213 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300214 l &= ~ENABLEWAKEUP; /* disable wakeup */
215 l &= ~NOSTDBY; /* remove possible nostdby */
216 l |= SMARTSTDBY; /* enable smart standby */
217 l &= ~AUTOIDLE; /* disable auto idle */
218 l &= ~NOIDLE; /* remove possible noidle */
219 l |= SMARTIDLE; /* enable smart idle */
Niilo Minkkinen9a4b5e32009-05-18 17:54:16 +0300220 /*
221 * MUSB AUTOIDLE don't work in 3430.
222 * Workaround by Richard Woodruff/TI
223 */
224 if (!cpu_is_omap3430())
225 l |= AUTOIDLE; /* enable auto idle */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200226 musb_writel(musb->mregs, OTG_SYSCONFIG, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300227
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200228 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200229
230 if (data->interface_type == MUSB_INTERFACE_UTMI) {
231 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
232 l &= ~ULPI_12PIN; /* Disable ULPI */
233 l |= UTMI_8BIT; /* Enable UTMI */
234 } else {
235 l |= ULPI_12PIN;
236 }
237
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200238 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300239
240 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
241 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200242 musb_readl(musb->mregs, OTG_REVISION),
243 musb_readl(musb->mregs, OTG_SYSCONFIG),
244 musb_readl(musb->mregs, OTG_SYSSTATUS),
245 musb_readl(musb->mregs, OTG_INTERFSEL),
246 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300247
Felipe Balbi550a7372008-07-24 12:27:36 +0300248 if (is_host_enabled(musb))
249 musb->board_set_vbus = omap_set_vbus;
Felipe Balbi550a7372008-07-24 12:27:36 +0300250
251 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
252
253 return 0;
254}
255
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200256#ifdef CONFIG_PM
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200257void musb_platform_save_context(struct musb *musb,
258 struct musb_context_registers *musb_context)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200259{
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200260 musb_context->otg_sysconfig = musb_readl(musb->mregs, OTG_SYSCONFIG);
261 musb_context->otg_forcestandby = musb_readl(musb->mregs, OTG_FORCESTDBY);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200262}
263
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200264void musb_platform_restore_context(struct musb *musb,
265 struct musb_context_registers *musb_context)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200266{
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200267 musb_writel(musb->mregs, OTG_SYSCONFIG, musb_context->otg_sysconfig);
268 musb_writel(musb->mregs, OTG_FORCESTDBY, musb_context->otg_forcestandby);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200269}
270#endif
271
Sergei Shtylyov5b3078b2010-03-25 13:25:22 +0200272static int musb_platform_suspend(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300273{
274 u32 l;
275
276 if (!musb->clock)
277 return 0;
278
279 /* in any role */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200280 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
Felipe Balbi550a7372008-07-24 12:27:36 +0300281 l |= ENABLEFORCE; /* enable MSTANDBY */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200282 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300283
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200284 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300285 l |= ENABLEWAKEUP; /* enable wakeup */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200286 musb_writel(musb->mregs, OTG_SYSCONFIG, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300287
David Brownell84e250f2009-03-31 12:30:04 -0700288 otg_set_suspend(musb->xceiv, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300289
290 if (musb->set_clock)
291 musb->set_clock(musb->clock, 0);
292 else
293 clk_disable(musb->clock);
294
295 return 0;
296}
297
298static int musb_platform_resume(struct musb *musb)
299{
300 u32 l;
301
302 if (!musb->clock)
303 return 0;
304
David Brownell84e250f2009-03-31 12:30:04 -0700305 otg_set_suspend(musb->xceiv, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300306
307 if (musb->set_clock)
308 musb->set_clock(musb->clock, 1);
309 else
310 clk_enable(musb->clock);
311
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200312 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300313 l &= ~ENABLEWAKEUP; /* disable wakeup */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200314 musb_writel(musb->mregs, OTG_SYSCONFIG, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300315
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200316 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
Felipe Balbi550a7372008-07-24 12:27:36 +0300317 l &= ~ENABLEFORCE; /* disable MSTANDBY */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200318 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300319
320 return 0;
321}
322
323
324int musb_platform_exit(struct musb *musb)
325{
326
Felipe Balbi550a7372008-07-24 12:27:36 +0300327 musb_platform_suspend(musb);
328
Felipe Balbi550a7372008-07-24 12:27:36 +0300329 return 0;
330}