blob: 78bb1e5cb9f898060843982802c6cb11e60916eb [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>
Felipe Balbidc098862010-12-01 15:01:11 +020034#include <linux/platform_device.h>
35#include <linux/dma-mapping.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030036
Felipe Balbi550a7372008-07-24 12:27:36 +030037#include "musb_core.h"
38#include "omap2430.h"
39
Felipe Balbi550a7372008-07-24 12:27:36 +030040static struct timer_list musb_idle_timer;
41
42static void musb_do_idle(unsigned long _musb)
43{
44 struct musb *musb = (void *)_musb;
45 unsigned long flags;
Ajay Kumar Guptaeef767b2008-10-29 15:10:38 +020046#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +030047 u8 power;
Ajay Kumar Guptaeef767b2008-10-29 15:10:38 +020048#endif
Felipe Balbi550a7372008-07-24 12:27:36 +030049 u8 devctl;
50
Felipe Balbi550a7372008-07-24 12:27:36 +030051 spin_lock_irqsave(&musb->lock, flags);
52
David Brownell71783e02008-11-24 13:06:49 +020053 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
54
David Brownell84e250f2009-03-31 12:30:04 -070055 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +030056 case OTG_STATE_A_WAIT_BCON:
57 devctl &= ~MUSB_DEVCTL_SESSION;
58 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
59
60 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
61 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -070062 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030063 MUSB_DEV_MODE(musb);
64 } else {
David Brownell84e250f2009-03-31 12:30:04 -070065 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030066 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 ... */
David Brownell84e250f2009-03-31 12:30:04 -070083 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +030084 }
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)
David Brownell84e250f2009-03-31 12:30:04 -070091 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +030092 else
David Brownell84e250f2009-03-31 12:30:04 -070093 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +030094#endif
95 default:
96 break;
97 }
98 spin_unlock_irqrestore(&musb->lock, flags);
99}
100
101
Felipe Balbi743411b2010-12-01 13:22:05 +0200102static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
Felipe Balbi550a7372008-07-24 12:27:36 +0300103{
104 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
105 static unsigned long last_timer;
106
107 if (timeout == 0)
108 timeout = default_timeout;
109
110 /* Never idle if active, or when VBUS timeout is not set as host */
111 if (musb->is_active || ((musb->a_wait_bcon == 0)
David Brownell84e250f2009-03-31 12:30:04 -0700112 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300113 DBG(4, "%s active, deleting timer\n", otg_state_string(musb));
114 del_timer(&musb_idle_timer);
115 last_timer = jiffies;
116 return;
117 }
118
119 if (time_after(last_timer, timeout)) {
120 if (!timer_pending(&musb_idle_timer))
121 last_timer = timeout;
122 else {
123 DBG(4, "Longer idle timer already pending, ignoring\n");
124 return;
125 }
126 }
127 last_timer = timeout;
128
129 DBG(4, "%s inactive, for idle timer for %lu ms\n",
130 otg_state_string(musb),
131 (unsigned long)jiffies_to_msecs(timeout - jiffies));
132 mod_timer(&musb_idle_timer, timeout);
133}
134
Felipe Balbi743411b2010-12-01 13:22:05 +0200135static void omap2430_musb_enable(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300136{
137}
Felipe Balbi743411b2010-12-01 13:22:05 +0200138
139static void omap2430_musb_disable(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300140{
141}
Felipe Balbi743411b2010-12-01 13:22:05 +0200142
143static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
Felipe Balbi550a7372008-07-24 12:27:36 +0300144{
145 u8 devctl;
146 /* HDRC controls CPEN, but beware current surges during device
147 * connect. They can trigger transient overcurrent conditions
148 * that must be ignored.
149 */
150
151 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
152
153 if (is_on) {
154 musb->is_active = 1;
David Brownell84e250f2009-03-31 12:30:04 -0700155 musb->xceiv->default_a = 1;
156 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300157 devctl |= MUSB_DEVCTL_SESSION;
158
159 MUSB_HST_MODE(musb);
160 } else {
161 musb->is_active = 0;
162
163 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
164 * jumping right to B_IDLE...
165 */
166
David Brownell84e250f2009-03-31 12:30:04 -0700167 musb->xceiv->default_a = 0;
168 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300169 devctl &= ~MUSB_DEVCTL_SESSION;
170
171 MUSB_DEV_MODE(musb);
172 }
173 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
174
175 DBG(1, "VBUS %s, devctl %02x "
176 /* otg %3x conf %08x prcm %08x */ "\n",
177 otg_state_string(musb),
178 musb_readb(musb->mregs, MUSB_DEVCTL));
179}
Felipe Balbi550a7372008-07-24 12:27:36 +0300180
Felipe Balbi743411b2010-12-01 13:22:05 +0200181static int omap2430_musb_resume(struct musb *musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300182
Felipe Balbi743411b2010-12-01 13:22:05 +0200183static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
Felipe Balbi550a7372008-07-24 12:27:36 +0300184{
185 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
186
187 devctl |= MUSB_DEVCTL_SESSION;
188 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
189
David Brownell96a274d2008-11-24 13:06:47 +0200190 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300191}
192
Felipe Balbi743411b2010-12-01 13:22:05 +0200193static int omap2430_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300194{
195 u32 l;
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500196 struct device *dev = musb->controller;
197 struct musb_hdrc_platform_data *plat = dev->platform_data;
198 struct omap_musb_board_data *data = plat->board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300199
David Brownell84e250f2009-03-31 12:30:04 -0700200 /* We require some kind of external transceiver, hooked
201 * up through ULPI. TWL4030-family PMICs include one,
202 * which needs a driver, drivers aren't always needed.
203 */
204 musb->xceiv = otg_get_transceiver();
205 if (!musb->xceiv) {
206 pr_err("HS USB OTG: no transceiver configured\n");
207 return -ENODEV;
208 }
209
Felipe Balbi743411b2010-12-01 13:22:05 +0200210 omap2430_musb_resume(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300211
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200212 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300213 l &= ~ENABLEWAKEUP; /* disable wakeup */
214 l &= ~NOSTDBY; /* remove possible nostdby */
215 l |= SMARTSTDBY; /* enable smart standby */
216 l &= ~AUTOIDLE; /* disable auto idle */
217 l &= ~NOIDLE; /* remove possible noidle */
218 l |= SMARTIDLE; /* enable smart idle */
Niilo Minkkinen9a4b5e32009-05-18 17:54:16 +0300219 /*
220 * MUSB AUTOIDLE don't work in 3430.
221 * Workaround by Richard Woodruff/TI
222 */
223 if (!cpu_is_omap3430())
224 l |= AUTOIDLE; /* enable auto idle */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200225 musb_writel(musb->mregs, OTG_SYSCONFIG, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300226
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200227 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200228
229 if (data->interface_type == MUSB_INTERFACE_UTMI) {
230 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
231 l &= ~ULPI_12PIN; /* Disable ULPI */
232 l |= UTMI_8BIT; /* Enable UTMI */
233 } else {
234 l |= ULPI_12PIN;
235 }
236
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200237 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300238
239 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
240 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200241 musb_readl(musb->mregs, OTG_REVISION),
242 musb_readl(musb->mregs, OTG_SYSCONFIG),
243 musb_readl(musb->mregs, OTG_SYSSTATUS),
244 musb_readl(musb->mregs, OTG_INTERFSEL),
245 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300246
Felipe Balbi550a7372008-07-24 12:27:36 +0300247 if (is_host_enabled(musb))
Felipe Balbi743411b2010-12-01 13:22:05 +0200248 musb->board_set_vbus = omap2430_musb_set_vbus;
Felipe Balbi550a7372008-07-24 12:27:36 +0300249
250 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
251
252 return 0;
253}
254
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200255#ifdef CONFIG_PM
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200256void musb_platform_save_context(struct musb *musb,
257 struct musb_context_registers *musb_context)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200258{
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200259 musb_context->otg_sysconfig = musb_readl(musb->mregs, OTG_SYSCONFIG);
260 musb_context->otg_forcestandby = musb_readl(musb->mregs, OTG_FORCESTDBY);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200261}
262
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200263void musb_platform_restore_context(struct musb *musb,
264 struct musb_context_registers *musb_context)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200265{
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200266 musb_writel(musb->mregs, OTG_SYSCONFIG, musb_context->otg_sysconfig);
267 musb_writel(musb->mregs, OTG_FORCESTDBY, musb_context->otg_forcestandby);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +0200268}
269#endif
270
Felipe Balbi743411b2010-12-01 13:22:05 +0200271static int omap2430_musb_suspend(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300272{
273 u32 l;
274
275 if (!musb->clock)
276 return 0;
277
278 /* in any role */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200279 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
Felipe Balbi550a7372008-07-24 12:27:36 +0300280 l |= ENABLEFORCE; /* enable MSTANDBY */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200281 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300282
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200283 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300284 l |= ENABLEWAKEUP; /* enable wakeup */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200285 musb_writel(musb->mregs, OTG_SYSCONFIG, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300286
David Brownell84e250f2009-03-31 12:30:04 -0700287 otg_set_suspend(musb->xceiv, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300288
289 if (musb->set_clock)
290 musb->set_clock(musb->clock, 0);
291 else
292 clk_disable(musb->clock);
293
294 return 0;
295}
296
Felipe Balbi743411b2010-12-01 13:22:05 +0200297static int omap2430_musb_resume(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300298{
299 u32 l;
300
301 if (!musb->clock)
302 return 0;
303
David Brownell84e250f2009-03-31 12:30:04 -0700304 otg_set_suspend(musb->xceiv, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300305
306 if (musb->set_clock)
307 musb->set_clock(musb->clock, 1);
308 else
309 clk_enable(musb->clock);
310
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200311 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300312 l &= ~ENABLEWAKEUP; /* disable wakeup */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200313 musb_writel(musb->mregs, OTG_SYSCONFIG, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300314
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200315 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
Felipe Balbi550a7372008-07-24 12:27:36 +0300316 l &= ~ENABLEFORCE; /* disable MSTANDBY */
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200317 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300318
319 return 0;
320}
321
Felipe Balbi743411b2010-12-01 13:22:05 +0200322static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300323{
324
Felipe Balbi743411b2010-12-01 13:22:05 +0200325 omap2430_musb_suspend(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300326
Sergei Shtylyovf4053872010-09-29 09:54:29 +0300327 otg_put_transceiver(musb->xceiv);
Felipe Balbi550a7372008-07-24 12:27:36 +0300328 return 0;
329}
Felipe Balbi743411b2010-12-01 13:22:05 +0200330
331const struct musb_platform_ops musb_ops = {
332 .init = omap2430_musb_init,
333 .exit = omap2430_musb_exit,
334
335 .suspend = omap2430_musb_suspend,
336 .resume = omap2430_musb_resume,
337
338 .enable = omap2430_musb_enable,
339 .disable = omap2430_musb_disable,
340
341 .set_mode = omap2430_musb_set_mode,
342 .try_idle = omap2430_musb_try_idle,
343
344 .set_vbus = omap2430_musb_set_vbus,
345};
Felipe Balbidc098862010-12-01 15:01:11 +0200346
347static u64 omap2430_dmamask = DMA_BIT_MASK(32);
348
349static int __init omap2430_probe(struct platform_device *pdev)
350{
351 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
352 struct platform_device *musb;
353
354 int ret = -ENOMEM;
355
356 musb = platform_device_alloc("musb-hdrc", -1);
357 if (!musb) {
358 dev_err(&pdev->dev, "failed to allocate musb device\n");
359 goto err0;
360 }
361
362 musb->dev.parent = &pdev->dev;
363 musb->dev.dma_mask = &omap2430_dmamask;
364 musb->dev.coherent_dma_mask = omap2430_dmamask;
365
366 platform_set_drvdata(pdev, musb);
367
368 ret = platform_device_add_resources(musb, pdev->resource,
369 pdev->num_resources);
370 if (ret) {
371 dev_err(&pdev->dev, "failed to add resources\n");
372 goto err1;
373 }
374
375 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
376 if (ret) {
377 dev_err(&pdev->dev, "failed to add platform_data\n");
378 goto err1;
379 }
380
381 ret = platform_device_add(musb);
382 if (ret) {
383 dev_err(&pdev->dev, "failed to register musb device\n");
384 goto err1;
385 }
386
387 return 0;
388
389err1:
390 platform_device_put(musb);
391
392err0:
393 return ret;
394}
395
396static int __exit omap2430_remove(struct platform_device *pdev)
397{
398 struct platform_device *musb = platform_get_drvdata(pdev);
399
400 platform_device_del(musb);
401 platform_device_put(musb);
402
403 return 0;
404}
405
406static struct platform_driver omap2430_driver = {
407 .remove = __exit_p(omap2430_remove),
408 .driver = {
409 .name = "musb-omap2430",
410 },
411};
412
413MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
414MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
415MODULE_LICENSE("GPL v2");
416
417static int __init omap2430_init(void)
418{
419 return platform_driver_probe(&omap2430_driver, omap2430_probe);
420}
421subsys_initcall(omap2430_init);
422
423static void __exit omap2430_exit(void)
424{
425 platform_driver_unregister(&omap2430_driver);
426}
427module_exit(omap2430_exit);