]> git.sur5r.net Git - u-boot/blob - drivers/usb/musb-new/omap2430.c
Juno: don't print PCI debug information by default
[u-boot] / drivers / usb / musb-new / omap2430.c
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 #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__
50 struct 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
58 struct omap2430_glue            *_glue;
59
60 static struct timer_list musb_idle_timer;
61
62 static 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
112 static 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
146 static 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
210 static 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
221 static 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
231 static 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__
241 void 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 }
254 EXPORT_SYMBOL_GPL(omap_musb_mailbox);
255
256 static 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
312 static 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
320 static int omap2430_musb_init(struct musb *musb)
321 {
322         u32 l;
323         int status = 0;
324         unsigned long int start;
325 #ifndef __UBOOT__
326         struct device *dev = musb->controller;
327         struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
328         struct musb_hdrc_platform_data *plat = dev->platform_data;
329         struct omap_musb_board_data *data = plat->board_data;
330 #else
331         struct omap_musb_board_data *data =
332                 (struct omap_musb_board_data *)musb->controller;
333 #endif
334
335         /* Reset the controller */
336         musb_writel(musb->mregs, OTG_SYSCONFIG, SOFTRST);
337
338         start = get_timer(0);
339
340         while (1) {
341                 l = musb_readl(musb->mregs, OTG_SYSCONFIG);
342                 if ((l & SOFTRST) == 0)
343                         break;
344
345                 if (get_timer(start) > (CONFIG_SYS_HZ / 1000)) {
346                         dev_err(musb->controller, "MUSB reset is taking too long\n");
347                         return -ENODEV;
348                 }
349         }
350
351 #ifndef __UBOOT__
352         /* We require some kind of external transceiver, hooked
353          * up through ULPI.  TWL4030-family PMICs include one,
354          * which needs a driver, drivers aren't always needed.
355          */
356         musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
357         if (IS_ERR_OR_NULL(musb->xceiv)) {
358                 pr_err("HS USB OTG: no transceiver configured\n");
359                 return -ENODEV;
360         }
361
362         status = pm_runtime_get_sync(dev);
363         if (status < 0) {
364                 dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status);
365                 goto err1;
366         }
367 #endif
368
369         l = musb_readl(musb->mregs, OTG_INTERFSEL);
370
371         if (data->interface_type == MUSB_INTERFACE_UTMI) {
372                 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
373                 l &= ~ULPI_12PIN;       /* Disable ULPI */
374                 l |= UTMI_8BIT;         /* Enable UTMI  */
375         } else {
376                 l |= ULPI_12PIN;
377         }
378
379         musb_writel(musb->mregs, OTG_INTERFSEL, l);
380
381         pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
382                         "sysstatus 0x%x, intrfsel 0x%x, simenable  0x%x\n",
383                         musb_readl(musb->mregs, OTG_REVISION),
384                         musb_readl(musb->mregs, OTG_SYSCONFIG),
385                         musb_readl(musb->mregs, OTG_SYSSTATUS),
386                         musb_readl(musb->mregs, OTG_INTERFSEL),
387                         musb_readl(musb->mregs, OTG_SIMENABLE));
388
389 #ifndef __UBOOT__
390         setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb);
391
392         if (glue->status != OMAP_MUSB_UNKNOWN)
393                 omap_musb_set_mailbox(glue);
394
395         pm_runtime_put_noidle(musb->controller);
396 #endif
397         return 0;
398
399 err1:
400         return status;
401 }
402
403 #ifndef __UBOOT__
404 static void omap2430_musb_enable(struct musb *musb)
405 #else
406 static int omap2430_musb_enable(struct musb *musb)
407 #endif
408 {
409 #ifndef __UBOOT__
410         u8              devctl;
411         unsigned long timeout = jiffies + msecs_to_jiffies(1000);
412         struct device *dev = musb->controller;
413         struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
414         struct musb_hdrc_platform_data *pdata = dev->platform_data;
415         struct omap_musb_board_data *data = pdata->board_data;
416
417         switch (glue->status) {
418
419         case OMAP_MUSB_ID_GROUND:
420                 usb_phy_init(musb->xceiv);
421                 if (data->interface_type != MUSB_INTERFACE_UTMI)
422                         break;
423                 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
424                 /* start the session */
425                 devctl |= MUSB_DEVCTL_SESSION;
426                 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
427                 while (musb_readb(musb->mregs, MUSB_DEVCTL) &
428                                 MUSB_DEVCTL_BDEVICE) {
429                         cpu_relax();
430
431                         if (time_after(jiffies, timeout)) {
432                                 dev_err(dev, "configured as A device timeout");
433                                 break;
434                         }
435                 }
436                 break;
437
438         case OMAP_MUSB_VBUS_VALID:
439                 usb_phy_init(musb->xceiv);
440                 break;
441
442         default:
443                 break;
444         }
445 #else
446 #ifdef CONFIG_TWL4030_USB
447         if (twl4030_usb_ulpi_init()) {
448                 serial_printf("ERROR: %s Could not initialize PHY\n",
449                                 __PRETTY_FUNCTION__);
450         }
451 #endif
452         return 0;
453 #endif
454 }
455
456 static void omap2430_musb_disable(struct musb *musb)
457 {
458 #ifndef __UBOOT__
459         struct device *dev = musb->controller;
460         struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
461
462         if (glue->status != OMAP_MUSB_UNKNOWN)
463                 usb_phy_shutdown(musb->xceiv);
464 #endif
465 }
466
467 static int omap2430_musb_exit(struct musb *musb)
468 {
469         del_timer_sync(&musb_idle_timer);
470
471         omap2430_low_level_exit(musb);
472
473         return 0;
474 }
475
476 #ifndef __UBOOT__
477 static const struct musb_platform_ops omap2430_ops = {
478 #else
479 const struct musb_platform_ops omap2430_ops = {
480 #endif
481         .init           = omap2430_musb_init,
482         .exit           = omap2430_musb_exit,
483
484 #ifndef __UBOOT__
485         .set_mode       = omap2430_musb_set_mode,
486         .try_idle       = omap2430_musb_try_idle,
487
488         .set_vbus       = omap2430_musb_set_vbus,
489 #endif
490
491         .enable         = omap2430_musb_enable,
492         .disable        = omap2430_musb_disable,
493 };
494
495 #ifndef __UBOOT__
496 static u64 omap2430_dmamask = DMA_BIT_MASK(32);
497
498 static int __devinit omap2430_probe(struct platform_device *pdev)
499 {
500         struct musb_hdrc_platform_data  *pdata = pdev->dev.platform_data;
501         struct platform_device          *musb;
502         struct omap2430_glue            *glue;
503         int                             ret = -ENOMEM;
504
505         glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
506         if (!glue) {
507                 dev_err(&pdev->dev, "failed to allocate glue context\n");
508                 goto err0;
509         }
510
511         musb = platform_device_alloc("musb-hdrc", -1);
512         if (!musb) {
513                 dev_err(&pdev->dev, "failed to allocate musb device\n");
514                 goto err0;
515         }
516
517         musb->dev.parent                = &pdev->dev;
518         musb->dev.dma_mask              = &omap2430_dmamask;
519         musb->dev.coherent_dma_mask     = omap2430_dmamask;
520
521         glue->dev                       = &pdev->dev;
522         glue->musb                      = musb;
523         glue->status                    = OMAP_MUSB_UNKNOWN;
524
525         pdata->platform_ops             = &omap2430_ops;
526
527         platform_set_drvdata(pdev, glue);
528
529         /*
530          * REVISIT if we ever have two instances of the wrapper, we will be
531          * in big trouble
532          */
533         _glue   = glue;
534
535         INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
536
537         ret = platform_device_add_resources(musb, pdev->resource,
538                         pdev->num_resources);
539         if (ret) {
540                 dev_err(&pdev->dev, "failed to add resources\n");
541                 goto err1;
542         }
543
544         ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
545         if (ret) {
546                 dev_err(&pdev->dev, "failed to add platform_data\n");
547                 goto err1;
548         }
549
550         pm_runtime_enable(&pdev->dev);
551
552         ret = platform_device_add(musb);
553         if (ret) {
554                 dev_err(&pdev->dev, "failed to register musb device\n");
555                 goto err1;
556         }
557
558         return 0;
559
560 err1:
561         platform_device_put(musb);
562
563 err0:
564         return ret;
565 }
566
567 static int __devexit omap2430_remove(struct platform_device *pdev)
568 {
569         struct omap2430_glue            *glue = platform_get_drvdata(pdev);
570
571         cancel_work_sync(&glue->omap_musb_mailbox_work);
572         platform_device_del(glue->musb);
573         platform_device_put(glue->musb);
574
575         return 0;
576 }
577
578 #ifdef CONFIG_PM
579
580 static int omap2430_runtime_suspend(struct device *dev)
581 {
582         struct omap2430_glue            *glue = dev_get_drvdata(dev);
583         struct musb                     *musb = glue_to_musb(glue);
584
585         if (musb) {
586                 musb->context.otg_interfsel = musb_readl(musb->mregs,
587                                 OTG_INTERFSEL);
588
589                 omap2430_low_level_exit(musb);
590                 usb_phy_set_suspend(musb->xceiv, 1);
591         }
592
593         return 0;
594 }
595
596 static int omap2430_runtime_resume(struct device *dev)
597 {
598         struct omap2430_glue            *glue = dev_get_drvdata(dev);
599         struct musb                     *musb = glue_to_musb(glue);
600
601         if (musb) {
602                 omap2430_low_level_init(musb);
603                 musb_writel(musb->mregs, OTG_INTERFSEL,
604                                 musb->context.otg_interfsel);
605
606                 usb_phy_set_suspend(musb->xceiv, 0);
607         }
608
609         return 0;
610 }
611
612 static struct dev_pm_ops omap2430_pm_ops = {
613         .runtime_suspend = omap2430_runtime_suspend,
614         .runtime_resume = omap2430_runtime_resume,
615 };
616
617 #define DEV_PM_OPS      (&omap2430_pm_ops)
618 #else
619 #define DEV_PM_OPS      NULL
620 #endif
621
622 static struct platform_driver omap2430_driver = {
623         .probe          = omap2430_probe,
624         .remove         = __devexit_p(omap2430_remove),
625         .driver         = {
626                 .name   = "musb-omap2430",
627                 .pm     = DEV_PM_OPS,
628         },
629 };
630
631 MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
632 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
633 MODULE_LICENSE("GPL v2");
634
635 static int __init omap2430_init(void)
636 {
637         return platform_driver_register(&omap2430_driver);
638 }
639 subsys_initcall(omap2430_init);
640
641 static void __exit omap2430_exit(void)
642 {
643         platform_driver_unregister(&omap2430_driver);
644 }
645 module_exit(omap2430_exit);
646 #endif