blob: 98f4830912be656321f011f9dafdcdf3cfa6e60d [file] [log] [blame]
Ilya Yanok673a5242012-11-06 13:48:29 +00001/*
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 */
Ilya Yanok673a5242012-11-06 13:48:29 +000027#ifndef __UBOOT__
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/sched.h>
31#include <linux/init.h>
32#include <linux/list.h>
33#include <linux/io.h>
34#include <linux/platform_device.h>
35#include <linux/dma-mapping.h>
36#include <linux/pm_runtime.h>
37#include <linux/err.h>
38#include <linux/usb/musb-omap.h>
39#else
40#include <common.h>
41#include <asm/omap_musb.h>
42#include <twl4030.h>
43#include "linux-compat.h"
44#endif
45
46#include "musb_core.h"
47#include "omap2430.h"
48
49#ifndef __UBOOT__
50struct omap2430_glue {
51 struct device *dev;
52 struct platform_device *musb;
53 enum omap_musb_vbus_id_status status;
54 struct work_struct omap_musb_mailbox_work;
55};
56#define glue_to_musb(g) platform_get_drvdata(g->musb)
57
58struct omap2430_glue *_glue;
59
60static struct timer_list musb_idle_timer;
61
62static void musb_do_idle(unsigned long _musb)
63{
64 struct musb *musb = (void *)_musb;
65 unsigned long flags;
66 u8 power;
67 u8 devctl;
68
69 spin_lock_irqsave(&musb->lock, flags);
70
71 switch (musb->xceiv->state) {
72 case OTG_STATE_A_WAIT_BCON:
73
74 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
75 if (devctl & MUSB_DEVCTL_BDEVICE) {
76 musb->xceiv->state = OTG_STATE_B_IDLE;
77 MUSB_DEV_MODE(musb);
78 } else {
79 musb->xceiv->state = OTG_STATE_A_IDLE;
80 MUSB_HST_MODE(musb);
81 }
82 break;
83 case OTG_STATE_A_SUSPEND:
84 /* finish RESUME signaling? */
85 if (musb->port1_status & MUSB_PORT_STAT_RESUME) {
86 power = musb_readb(musb->mregs, MUSB_POWER);
87 power &= ~MUSB_POWER_RESUME;
88 dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power);
89 musb_writeb(musb->mregs, MUSB_POWER, power);
90 musb->is_active = 1;
91 musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
92 | MUSB_PORT_STAT_RESUME);
93 musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
94 usb_hcd_poll_rh_status(musb_to_hcd(musb));
95 /* NOTE: it might really be A_WAIT_BCON ... */
96 musb->xceiv->state = OTG_STATE_A_HOST;
97 }
98 break;
99 case OTG_STATE_A_HOST:
100 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
101 if (devctl & MUSB_DEVCTL_BDEVICE)
102 musb->xceiv->state = OTG_STATE_B_IDLE;
103 else
104 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
105 default:
106 break;
107 }
108 spin_unlock_irqrestore(&musb->lock, flags);
109}
110
111
112static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout)
113{
114 unsigned long default_timeout = jiffies + msecs_to_jiffies(3);
115 static unsigned long last_timer;
116
117 if (timeout == 0)
118 timeout = default_timeout;
119
120 /* Never idle if active, or when VBUS timeout is not set as host */
121 if (musb->is_active || ((musb->a_wait_bcon == 0)
122 && (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) {
123 dev_dbg(musb->controller, "%s active, deleting timer\n",
124 otg_state_string(musb->xceiv->state));
125 del_timer(&musb_idle_timer);
126 last_timer = jiffies;
127 return;
128 }
129
130 if (time_after(last_timer, timeout)) {
131 if (!timer_pending(&musb_idle_timer))
132 last_timer = timeout;
133 else {
134 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n");
135 return;
136 }
137 }
138 last_timer = timeout;
139
140 dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n",
141 otg_state_string(musb->xceiv->state),
142 (unsigned long)jiffies_to_msecs(timeout - jiffies));
143 mod_timer(&musb_idle_timer, timeout);
144}
145
146static void omap2430_musb_set_vbus(struct musb *musb, int is_on)
147{
148 struct usb_otg *otg = musb->xceiv->otg;
149 u8 devctl;
150 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
151 int ret = 1;
152 /* HDRC controls CPEN, but beware current surges during device
153 * connect. They can trigger transient overcurrent conditions
154 * that must be ignored.
155 */
156
157 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
158
159 if (is_on) {
160 if (musb->xceiv->state == OTG_STATE_A_IDLE) {
161 /* start the session */
162 devctl |= MUSB_DEVCTL_SESSION;
163 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
164 /*
165 * Wait for the musb to set as A device to enable the
166 * VBUS
167 */
168 while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) {
169
170 cpu_relax();
171
172 if (time_after(jiffies, timeout)) {
173 dev_err(musb->controller,
174 "configured as A device timeout");
175 ret = -EINVAL;
176 break;
177 }
178 }
179
180 if (ret && otg->set_vbus)
181 otg_set_vbus(otg, 1);
182 } else {
183 musb->is_active = 1;
184 otg->default_a = 1;
185 musb->xceiv->state = OTG_STATE_A_WAIT_VRISE;
186 devctl |= MUSB_DEVCTL_SESSION;
187 MUSB_HST_MODE(musb);
188 }
189 } else {
190 musb->is_active = 0;
191
192 /* NOTE: we're skipping A_WAIT_VFALL -> A_IDLE and
193 * jumping right to B_IDLE...
194 */
195
196 otg->default_a = 0;
197 musb->xceiv->state = OTG_STATE_B_IDLE;
198 devctl &= ~MUSB_DEVCTL_SESSION;
199
200 MUSB_DEV_MODE(musb);
201 }
202 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
203
204 dev_dbg(musb->controller, "VBUS %s, devctl %02x "
205 /* otg %3x conf %08x prcm %08x */ "\n",
206 otg_state_string(musb->xceiv->state),
207 musb_readb(musb->mregs, MUSB_DEVCTL));
208}
209
210static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode)
211{
212 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
213
214 devctl |= MUSB_DEVCTL_SESSION;
215 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
216
217 return 0;
218}
219#endif
220
221static inline void omap2430_low_level_exit(struct musb *musb)
222{
223 u32 l;
224
225 /* in any role */
226 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
227 l |= ENABLEFORCE; /* enable MSTANDBY */
228 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
229}
230
231static inline void omap2430_low_level_init(struct musb *musb)
232{
233 u32 l;
234
235 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
236 l &= ~ENABLEFORCE; /* disable MSTANDBY */
237 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
238}
239
240#ifndef __UBOOT__
241void omap_musb_mailbox(enum omap_musb_vbus_id_status status)
242{
243 struct omap2430_glue *glue = _glue;
244 struct musb *musb = glue_to_musb(glue);
245
246 glue->status = status;
247 if (!musb) {
248 dev_err(glue->dev, "musb core is not yet ready\n");
249 return;
250 }
251
252 schedule_work(&glue->omap_musb_mailbox_work);
253}
254EXPORT_SYMBOL_GPL(omap_musb_mailbox);
255
256static void omap_musb_set_mailbox(struct omap2430_glue *glue)
257{
258 struct musb *musb = glue_to_musb(glue);
259 struct device *dev = musb->controller;
260 struct musb_hdrc_platform_data *pdata = dev->platform_data;
261 struct omap_musb_board_data *data = pdata->board_data;
262 struct usb_otg *otg = musb->xceiv->otg;
263
264 switch (glue->status) {
265 case OMAP_MUSB_ID_GROUND:
266 dev_dbg(dev, "ID GND\n");
267
268 otg->default_a = true;
269 musb->xceiv->state = OTG_STATE_A_IDLE;
270 musb->xceiv->last_event = USB_EVENT_ID;
271 if (!is_otg_enabled(musb) || musb->gadget_driver) {
272 pm_runtime_get_sync(dev);
273 usb_phy_init(musb->xceiv);
274 omap2430_musb_set_vbus(musb, 1);
275 }
276 break;
277
278 case OMAP_MUSB_VBUS_VALID:
279 dev_dbg(dev, "VBUS Connect\n");
280
281 otg->default_a = false;
282 musb->xceiv->state = OTG_STATE_B_IDLE;
283 musb->xceiv->last_event = USB_EVENT_VBUS;
284 if (musb->gadget_driver)
285 pm_runtime_get_sync(dev);
286 usb_phy_init(musb->xceiv);
287 break;
288
289 case OMAP_MUSB_ID_FLOAT:
290 case OMAP_MUSB_VBUS_OFF:
291 dev_dbg(dev, "VBUS Disconnect\n");
292
293 musb->xceiv->last_event = USB_EVENT_NONE;
294 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
295 if (musb->gadget_driver) {
296 pm_runtime_mark_last_busy(dev);
297 pm_runtime_put_autosuspend(dev);
298 }
299
300 if (data->interface_type == MUSB_INTERFACE_UTMI) {
301 if (musb->xceiv->otg->set_vbus)
302 otg_set_vbus(musb->xceiv->otg, 0);
303 }
304 usb_phy_shutdown(musb->xceiv);
305 break;
306 default:
307 dev_dbg(dev, "ID float\n");
308 }
309}
310
311
312static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
313{
314 struct omap2430_glue *glue = container_of(mailbox_work,
315 struct omap2430_glue, omap_musb_mailbox_work);
316 omap_musb_set_mailbox(glue);
317}
318#endif
319
320static int omap2430_musb_init(struct musb *musb)
321{
322 u32 l;
323 int status = 0;
324#ifndef __UBOOT__
325 struct device *dev = musb->controller;
326 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
327 struct musb_hdrc_platform_data *plat = dev->platform_data;
328 struct omap_musb_board_data *data = plat->board_data;
329#else
330 struct omap_musb_board_data *data =
331 (struct omap_musb_board_data *)musb->controller;
332#endif
333
334
335#ifndef __UBOOT__
336 /* We require some kind of external transceiver, hooked
337 * up through ULPI. TWL4030-family PMICs include one,
338 * which needs a driver, drivers aren't always needed.
339 */
340 musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
341 if (IS_ERR_OR_NULL(musb->xceiv)) {
342 pr_err("HS USB OTG: no transceiver configured\n");
343 return -ENODEV;
344 }
345
346 status = pm_runtime_get_sync(dev);
347 if (status < 0) {
348 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
349 goto err1;
350 }
351#endif
352
353 l = musb_readl(musb->mregs, OTG_INTERFSEL);
354
355 if (data->interface_type == MUSB_INTERFACE_UTMI) {
356 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
357 l &= ~ULPI_12PIN; /* Disable ULPI */
358 l |= UTMI_8BIT; /* Enable UTMI */
359 } else {
360 l |= ULPI_12PIN;
361 }
362
363 musb_writel(musb->mregs, OTG_INTERFSEL, l);
364
365 pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
366 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
367 musb_readl(musb->mregs, OTG_REVISION),
368 musb_readl(musb->mregs, OTG_SYSCONFIG),
369 musb_readl(musb->mregs, OTG_SYSSTATUS),
370 musb_readl(musb->mregs, OTG_INTERFSEL),
371 musb_readl(musb->mregs, OTG_SIMENABLE));
372
373#ifndef __UBOOT__
374 setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
375
376 if (glue->status != OMAP_MUSB_UNKNOWN)
377 omap_musb_set_mailbox(glue);
378
379 pm_runtime_put_noidle(musb->controller);
380#endif
381 return 0;
382
383err1:
384 return status;
385}
386
387static void omap2430_musb_enable(struct musb *musb)
388{
389#ifndef __UBOOT__
390 u8 devctl;
391 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
392 struct device *dev = musb->controller;
393 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
394 struct musb_hdrc_platform_data *pdata = dev->platform_data;
395 struct omap_musb_board_data *data = pdata->board_data;
396
397 switch (glue->status) {
398
399 case OMAP_MUSB_ID_GROUND:
400 usb_phy_init(musb->xceiv);
401 if (data->interface_type != MUSB_INTERFACE_UTMI)
402 break;
403 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
404 /* start the session */
405 devctl |= MUSB_DEVCTL_SESSION;
406 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
407 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
408 MUSB_DEVCTL_BDEVICE) {
409 cpu_relax();
410
411 if (time_after(jiffies, timeout)) {
412 dev_err(dev, "configured as A device timeout");
413 break;
414 }
415 }
416 break;
417
418 case OMAP_MUSB_VBUS_VALID:
419 usb_phy_init(musb->xceiv);
420 break;
421
422 default:
423 break;
424 }
425#else
426#ifdef CONFIG_TWL4030_USB
427 if (twl4030_usb_ulpi_init()) {
428 serial_printf("ERROR: %s Could not initialize PHY\n",
429 __PRETTY_FUNCTION__);
430 }
431#endif
432#endif
433}
434
435static void omap2430_musb_disable(struct musb *musb)
436{
437#ifndef __UBOOT__
438 struct device *dev = musb->controller;
439 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
440
441 if (glue->status != OMAP_MUSB_UNKNOWN)
442 usb_phy_shutdown(musb->xceiv);
443#endif
444}
445
446static int omap2430_musb_exit(struct musb *musb)
447{
448 del_timer_sync(&musb_idle_timer);
449
450 omap2430_low_level_exit(musb);
451
452 return 0;
453}
454
455#ifndef __UBOOT__
456static const struct musb_platform_ops omap2430_ops = {
457#else
458const struct musb_platform_ops omap2430_ops = {
459#endif
460 .init = omap2430_musb_init,
461 .exit = omap2430_musb_exit,
462
463#ifndef __UBOOT__
464 .set_mode = omap2430_musb_set_mode,
465 .try_idle = omap2430_musb_try_idle,
466
467 .set_vbus = omap2430_musb_set_vbus,
468#endif
469
470 .enable = omap2430_musb_enable,
471 .disable = omap2430_musb_disable,
472};
473
474#ifndef __UBOOT__
475static u64 omap2430_dmamask = DMA_BIT_MASK(32);
476
477static int __devinit omap2430_probe(struct platform_device *pdev)
478{
479 struct musb_hdrc_platform_data *pdata = pdev->dev.platform_data;
480 struct platform_device *musb;
481 struct omap2430_glue *glue;
482 int ret = -ENOMEM;
483
484 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
485 if (!glue) {
486 dev_err(&pdev->dev, "failed to allocate glue context\n");
487 goto err0;
488 }
489
490 musb = platform_device_alloc("musb-hdrc", -1);
491 if (!musb) {
492 dev_err(&pdev->dev, "failed to allocate musb device\n");
493 goto err0;
494 }
495
496 musb->dev.parent = &pdev->dev;
497 musb->dev.dma_mask = &omap2430_dmamask;
498 musb->dev.coherent_dma_mask = omap2430_dmamask;
499
500 glue->dev = &pdev->dev;
501 glue->musb = musb;
502 glue->status = OMAP_MUSB_UNKNOWN;
503
504 pdata->platform_ops = &omap2430_ops;
505
506 platform_set_drvdata(pdev, glue);
507
508 /*
509 * REVISIT if we ever have two instances of the wrapper, we will be
510 * in big trouble
511 */
512 _glue = glue;
513
514 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
515
516 ret = platform_device_add_resources(musb, pdev->resource,
517 pdev->num_resources);
518 if (ret) {
519 dev_err(&pdev->dev, "failed to add resources\n");
520 goto err1;
521 }
522
523 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
524 if (ret) {
525 dev_err(&pdev->dev, "failed to add platform_data\n");
526 goto err1;
527 }
528
529 pm_runtime_enable(&pdev->dev);
530
531 ret = platform_device_add(musb);
532 if (ret) {
533 dev_err(&pdev->dev, "failed to register musb device\n");
534 goto err1;
535 }
536
537 return 0;
538
539err1:
540 platform_device_put(musb);
541
542err0:
543 return ret;
544}
545
546static int __devexit omap2430_remove(struct platform_device *pdev)
547{
548 struct omap2430_glue *glue = platform_get_drvdata(pdev);
549
550 cancel_work_sync(&glue->omap_musb_mailbox_work);
551 platform_device_del(glue->musb);
552 platform_device_put(glue->musb);
553
554 return 0;
555}
556
557#ifdef CONFIG_PM
558
559static int omap2430_runtime_suspend(struct device *dev)
560{
561 struct omap2430_glue *glue = dev_get_drvdata(dev);
562 struct musb *musb = glue_to_musb(glue);
563
564 if (musb) {
565 musb->context.otg_interfsel = musb_readl(musb->mregs,
566 OTG_INTERFSEL);
567
568 omap2430_low_level_exit(musb);
569 usb_phy_set_suspend(musb->xceiv, 1);
570 }
571
572 return 0;
573}
574
575static int omap2430_runtime_resume(struct device *dev)
576{
577 struct omap2430_glue *glue = dev_get_drvdata(dev);
578 struct musb *musb = glue_to_musb(glue);
579
580 if (musb) {
581 omap2430_low_level_init(musb);
582 musb_writel(musb->mregs, OTG_INTERFSEL,
583 musb->context.otg_interfsel);
584
585 usb_phy_set_suspend(musb->xceiv, 0);
586 }
587
588 return 0;
589}
590
591static struct dev_pm_ops omap2430_pm_ops = {
592 .runtime_suspend = omap2430_runtime_suspend,
593 .runtime_resume = omap2430_runtime_resume,
594};
595
596#define DEV_PM_OPS (&omap2430_pm_ops)
597#else
598#define DEV_PM_OPS NULL
599#endif
600
601static struct platform_driver omap2430_driver = {
602 .probe = omap2430_probe,
603 .remove = __devexit_p(omap2430_remove),
604 .driver = {
605 .name = "musb-omap2430",
606 .pm = DEV_PM_OPS,
607 },
608};
609
610MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
611MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
612MODULE_LICENSE("GPL v2");
613
614static int __init omap2430_init(void)
615{
616 return platform_driver_register(&omap2430_driver);
617}
618subsys_initcall(omap2430_init);
619
620static void __exit omap2430_exit(void)
621{
622 platform_driver_unregister(&omap2430_driver);
623}
624module_exit(omap2430_exit);
625#endif