]> Git Repo - J-u-boot.git/blob - drivers/usb/musb-new/musb_core.c
63ce8a5655cd815281a738127ba696b48123cd30
[J-u-boot.git] / drivers / usb / musb-new / musb_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MUSB OTG driver core code
4  *
5  * Copyright 2005 Mentor Graphics Corporation
6  * Copyright (C) 2005-2006 by Texas Instruments
7  * Copyright (C) 2006-2007 Nokia Corporation
8  */
9
10 /*
11  * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
12  *
13  * This consists of a Host Controller Driver (HCD) and a peripheral
14  * controller driver implementing the "Gadget" API; OTG support is
15  * in the works.  These are normal Linux-USB controller drivers which
16  * use IRQs and have no dedicated thread.
17  *
18  * This version of the driver has only been used with products from
19  * Texas Instruments.  Those products integrate the Inventra logic
20  * with other DMA, IRQ, and bus modules, as well as other logic that
21  * needs to be reflected in this driver.
22  *
23  *
24  * NOTE:  the original Mentor code here was pretty much a collection
25  * of mechanisms that don't seem to have been fully integrated/working
26  * for *any* Linux kernel version.  This version aims at Linux 2.6.now,
27  * Key open issues include:
28  *
29  *  - Lack of host-side transaction scheduling, for all transfer types.
30  *    The hardware doesn't do it; instead, software must.
31  *
32  *    This is not an issue for OTG devices that don't support external
33  *    hubs, but for more "normal" USB hosts it's a user issue that the
34  *    "multipoint" support doesn't scale in the expected ways.  That
35  *    includes DaVinci EVM in a common non-OTG mode.
36  *
37  *      * Control and bulk use dedicated endpoints, and there's as
38  *        yet no mechanism to either (a) reclaim the hardware when
39  *        peripherals are NAKing, which gets complicated with bulk
40  *        endpoints, or (b) use more than a single bulk endpoint in
41  *        each direction.
42  *
43  *        RESULT:  one device may be perceived as blocking another one.
44  *
45  *      * Interrupt and isochronous will dynamically allocate endpoint
46  *        hardware, but (a) there's no record keeping for bandwidth;
47  *        (b) in the common case that few endpoints are available, there
48  *        is no mechanism to reuse endpoints to talk to multiple devices.
49  *
50  *        RESULT:  At one extreme, bandwidth can be overcommitted in
51  *        some hardware configurations, no faults will be reported.
52  *        At the other extreme, the bandwidth capabilities which do
53  *        exist tend to be severely undercommitted.  You can't yet hook
54  *        up both a keyboard and a mouse to an external USB hub.
55  */
56
57 /*
58  * This gets many kinds of configuration information:
59  *      - Kconfig for everything user-configurable
60  *      - platform_device for addressing, irq, and platform_data
61  *      - platform_data is mostly for board-specific informarion
62  *        (plus recentrly, SOC or family details)
63  *
64  * Most of the conditional compilation will (someday) vanish.
65  */
66
67 #ifndef __UBOOT__
68 #include <log.h>
69 #include <dm/device_compat.h>
70 #include <dm/devres.h>
71 #include <linux/module.h>
72 #include <linux/kernel.h>
73 #include <linux/sched.h>
74 #include <linux/slab.h>
75 #include <linux/init.h>
76 #include <linux/list.h>
77 #include <linux/kobject.h>
78 #include <linux/prefetch.h>
79 #include <linux/platform_device.h>
80 #include <linux/io.h>
81 #else
82 #include <common.h>
83 #include <usb.h>
84 #include <linux/errno.h>
85 #include <linux/usb/ch9.h>
86 #include <linux/usb/gadget.h>
87 #include <linux/usb/musb.h>
88 #include <asm/io.h>
89 #include "linux-compat.h"
90 #include "usb-compat.h"
91 #endif
92
93 #include "musb_core.h"
94
95 #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
96
97
98 #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
99 #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
100
101 #define MUSB_VERSION "6.0"
102
103 #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
104
105 #define MUSB_DRIVER_NAME "musb-hdrc"
106 const char musb_driver_name[] = MUSB_DRIVER_NAME;
107
108 MODULE_DESCRIPTION(DRIVER_INFO);
109 MODULE_AUTHOR(DRIVER_AUTHOR);
110 MODULE_LICENSE("GPL");
111 MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
112
113
114 #ifndef __UBOOT__
115 /*-------------------------------------------------------------------------*/
116
117 static inline struct musb *dev_to_musb(struct device *dev)
118 {
119         return dev_get_drvdata(dev);
120 }
121
122 /*-------------------------------------------------------------------------*/
123
124 static int musb_ulpi_read(struct usb_phy *phy, u32 offset)
125 {
126         void __iomem *addr = phy->io_priv;
127         int     i = 0;
128         u8      r;
129         u8      power;
130         int     ret;
131
132         pm_runtime_get_sync(phy->io_dev);
133
134         /* Make sure the transceiver is not in low power mode */
135         power = musb_readb(addr, MUSB_POWER);
136         power &= ~MUSB_POWER_SUSPENDM;
137         musb_writeb(addr, MUSB_POWER, power);
138
139         /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
140          * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
141          */
142
143         musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
144         musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
145                         MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
146
147         while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
148                                 & MUSB_ULPI_REG_CMPLT)) {
149                 i++;
150                 if (i == 10000) {
151                         ret = -ETIMEDOUT;
152                         goto out;
153                 }
154
155         }
156         r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
157         r &= ~MUSB_ULPI_REG_CMPLT;
158         musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
159
160         ret = musb_readb(addr, MUSB_ULPI_REG_DATA);
161
162 out:
163         pm_runtime_put(phy->io_dev);
164
165         return ret;
166 }
167
168 static int musb_ulpi_write(struct usb_phy *phy, u32 offset, u32 data)
169 {
170         void __iomem *addr = phy->io_priv;
171         int     i = 0;
172         u8      r = 0;
173         u8      power;
174         int     ret = 0;
175
176         pm_runtime_get_sync(phy->io_dev);
177
178         /* Make sure the transceiver is not in low power mode */
179         power = musb_readb(addr, MUSB_POWER);
180         power &= ~MUSB_POWER_SUSPENDM;
181         musb_writeb(addr, MUSB_POWER, power);
182
183         musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
184         musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
185         musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
186
187         while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
188                                 & MUSB_ULPI_REG_CMPLT)) {
189                 i++;
190                 if (i == 10000) {
191                         ret = -ETIMEDOUT;
192                         goto out;
193                 }
194         }
195
196         r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
197         r &= ~MUSB_ULPI_REG_CMPLT;
198         musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
199
200 out:
201         pm_runtime_put(phy->io_dev);
202
203         return ret;
204 }
205
206 static struct usb_phy_io_ops musb_ulpi_access = {
207         .read = musb_ulpi_read,
208         .write = musb_ulpi_write,
209 };
210 #endif
211
212 /*-------------------------------------------------------------------------*/
213
214 #if !defined(CONFIG_USB_MUSB_TUSB6010)
215
216 /*
217  * Load an endpoint's FIFO
218  */
219 void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
220 {
221         struct musb *musb = hw_ep->musb;
222         void __iomem *fifo = hw_ep->fifo;
223
224         prefetch((u8 *)src);
225
226         dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
227                         'T', hw_ep->epnum, fifo, len, src);
228
229         /* we can't assume unaligned reads work */
230         if (likely((0x01 & (unsigned long) src) == 0)) {
231                 u16     index = 0;
232
233                 /* best case is 32bit-aligned source address */
234                 if ((0x02 & (unsigned long) src) == 0) {
235                         if (len >= 4) {
236                                 writesl(fifo, src + index, len >> 2);
237                                 index += len & ~0x03;
238                         }
239                         if (len & 0x02) {
240                                 musb_writew(fifo, 0, *(u16 *)&src[index]);
241                                 index += 2;
242                         }
243                 } else {
244                         if (len >= 2) {
245                                 writesw(fifo, src + index, len >> 1);
246                                 index += len & ~0x01;
247                         }
248                 }
249                 if (len & 0x01)
250                         musb_writeb(fifo, 0, src[index]);
251         } else  {
252                 /* byte aligned */
253                 writesb(fifo, src, len);
254         }
255 }
256
257 #if !defined(CONFIG_USB_MUSB_AM35X) && !defined(CONFIG_USB_MUSB_PIC32)
258 /*
259  * Unload an endpoint's FIFO
260  */
261 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
262 {
263         struct musb *musb = hw_ep->musb;
264         void __iomem *fifo = hw_ep->fifo;
265
266         dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
267                         'R', hw_ep->epnum, fifo, len, dst);
268
269         /* we can't assume unaligned writes work */
270         if (likely((0x01 & (unsigned long) dst) == 0)) {
271                 u16     index = 0;
272
273                 /* best case is 32bit-aligned destination address */
274                 if ((0x02 & (unsigned long) dst) == 0) {
275                         if (len >= 4) {
276                                 readsl(fifo, dst, len >> 2);
277                                 index = len & ~0x03;
278                         }
279                         if (len & 0x02) {
280                                 *(u16 *)&dst[index] = musb_readw(fifo, 0);
281                                 index += 2;
282                         }
283                 } else {
284                         if (len >= 2) {
285                                 readsw(fifo, dst, len >> 1);
286                                 index = len & ~0x01;
287                         }
288                 }
289                 if (len & 0x01)
290                         dst[index] = musb_readb(fifo, 0);
291         } else  {
292                 /* byte aligned */
293                 readsb(fifo, dst, len);
294         }
295 }
296 #endif
297
298 #endif  /* normal PIO */
299
300
301 /*-------------------------------------------------------------------------*/
302
303 /* for high speed test mode; see USB 2.0 spec 7.1.20 */
304 static const u8 musb_test_packet[53] = {
305         /* implicit SYNC then DATA0 to start */
306
307         /* JKJKJKJK x9 */
308         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309         /* JJKKJJKK x8 */
310         0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
311         /* JJJJKKKK x8 */
312         0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
313         /* JJJJJJJKKKKKKK x8 */
314         0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
315         /* JJJJJJJK x8 */
316         0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
317         /* JKKKKKKK x10, JK */
318         0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
319
320         /* implicit CRC16 then EOP to end */
321 };
322
323 void musb_load_testpacket(struct musb *musb)
324 {
325         void __iomem    *regs = musb->endpoints[0].regs;
326
327         musb_ep_select(musb->mregs, 0);
328         musb_write_fifo(musb->control_ep,
329                         sizeof(musb_test_packet), musb_test_packet);
330         musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
331 }
332
333 #ifndef __UBOOT__
334 /*-------------------------------------------------------------------------*/
335
336 /*
337  * Handles OTG hnp timeouts, such as b_ase0_brst
338  */
339 void musb_otg_timer_func(unsigned long data)
340 {
341         struct musb     *musb = (struct musb *)data;
342         unsigned long   flags;
343
344         spin_lock_irqsave(&musb->lock, flags);
345         switch (musb->xceiv->state) {
346         case OTG_STATE_B_WAIT_ACON:
347                 dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
348                 musb_g_disconnect(musb);
349                 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
350                 musb->is_active = 0;
351                 break;
352         case OTG_STATE_A_SUSPEND:
353         case OTG_STATE_A_WAIT_BCON:
354                 dev_dbg(musb->controller, "HNP: %s timeout\n",
355                         otg_state_string(musb->xceiv->state));
356                 musb_platform_set_vbus(musb, 0);
357                 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
358                 break;
359         default:
360                 dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
361                         otg_state_string(musb->xceiv->state));
362         }
363         musb->ignore_disconnect = 0;
364         spin_unlock_irqrestore(&musb->lock, flags);
365 }
366
367 /*
368  * Stops the HNP transition. Caller must take care of locking.
369  */
370 void musb_hnp_stop(struct musb *musb)
371 {
372         struct usb_hcd  *hcd = musb_to_hcd(musb);
373         void __iomem    *mbase = musb->mregs;
374         u8      reg;
375
376         dev_dbg(musb->controller, "HNP: stop from %s\n", otg_state_string(musb->xceiv->state));
377
378         switch (musb->xceiv->state) {
379         case OTG_STATE_A_PERIPHERAL:
380                 musb_g_disconnect(musb);
381                 dev_dbg(musb->controller, "HNP: back to %s\n",
382                         otg_state_string(musb->xceiv->state));
383                 break;
384         case OTG_STATE_B_HOST:
385                 dev_dbg(musb->controller, "HNP: Disabling HR\n");
386                 hcd->self.is_b_host = 0;
387                 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
388                 MUSB_DEV_MODE(musb);
389                 reg = musb_readb(mbase, MUSB_POWER);
390                 reg |= MUSB_POWER_SUSPENDM;
391                 musb_writeb(mbase, MUSB_POWER, reg);
392                 /* REVISIT: Start SESSION_REQUEST here? */
393                 break;
394         default:
395                 dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
396                         otg_state_string(musb->xceiv->state));
397         }
398
399         /*
400          * When returning to A state after HNP, avoid hub_port_rebounce(),
401          * which cause occasional OPT A "Did not receive reset after connect"
402          * errors.
403          */
404         musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
405 }
406 #endif
407
408 /*
409  * Interrupt Service Routine to record USB "global" interrupts.
410  * Since these do not happen often and signify things of
411  * paramount importance, it seems OK to check them individually;
412  * the order of the tests is specified in the manual
413  *
414  * @param musb instance pointer
415  * @param int_usb register contents
416  * @param devctl
417  * @param power
418  */
419
420 static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
421                                 u8 devctl, u8 power)
422 {
423 #ifndef __UBOOT__
424         struct usb_otg *otg = musb->xceiv->otg;
425 #endif
426         irqreturn_t handled = IRQ_NONE;
427
428         dev_dbg(musb->controller, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
429                 int_usb);
430
431 #ifndef __UBOOT__
432         /* in host mode, the peripheral may issue remote wakeup.
433          * in peripheral mode, the host may resume the link.
434          * spurious RESUME irqs happen too, paired with SUSPEND.
435          */
436         if (int_usb & MUSB_INTR_RESUME) {
437                 handled = IRQ_HANDLED;
438                 dev_dbg(musb->controller, "RESUME (%s)\n", otg_state_string(musb->xceiv->state));
439
440                 if (devctl & MUSB_DEVCTL_HM) {
441                         void __iomem *mbase = musb->mregs;
442
443                         switch (musb->xceiv->state) {
444                         case OTG_STATE_A_SUSPEND:
445                                 /* remote wakeup?  later, GetPortStatus
446                                  * will stop RESUME signaling
447                                  */
448
449                                 if (power & MUSB_POWER_SUSPENDM) {
450                                         /* spurious */
451                                         musb->int_usb &= ~MUSB_INTR_SUSPEND;
452                                         dev_dbg(musb->controller, "Spurious SUSPENDM\n");
453                                         break;
454                                 }
455
456                                 power &= ~MUSB_POWER_SUSPENDM;
457                                 musb_writeb(mbase, MUSB_POWER,
458                                                 power | MUSB_POWER_RESUME);
459
460                                 musb->port1_status |=
461                                                 (USB_PORT_STAT_C_SUSPEND << 16)
462                                                 | MUSB_PORT_STAT_RESUME;
463                                 musb->rh_timer = jiffies
464                                                 + msecs_to_jiffies(20);
465
466                                 musb->xceiv->state = OTG_STATE_A_HOST;
467                                 musb->is_active = 1;
468                                 usb_hcd_resume_root_hub(musb_to_hcd(musb));
469                                 break;
470                         case OTG_STATE_B_WAIT_ACON:
471                                 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
472                                 musb->is_active = 1;
473                                 MUSB_DEV_MODE(musb);
474                                 break;
475                         default:
476                                 WARNING("bogus %s RESUME (%s)\n",
477                                         "host",
478                                         otg_state_string(musb->xceiv->state));
479                         }
480                 } else {
481                         switch (musb->xceiv->state) {
482                         case OTG_STATE_A_SUSPEND:
483                                 /* possibly DISCONNECT is upcoming */
484                                 musb->xceiv->state = OTG_STATE_A_HOST;
485                                 usb_hcd_resume_root_hub(musb_to_hcd(musb));
486                                 break;
487                         case OTG_STATE_B_WAIT_ACON:
488                         case OTG_STATE_B_PERIPHERAL:
489                                 /* disconnect while suspended?  we may
490                                  * not get a disconnect irq...
491                                  */
492                                 if ((devctl & MUSB_DEVCTL_VBUS)
493                                                 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
494                                                 ) {
495                                         musb->int_usb |= MUSB_INTR_DISCONNECT;
496                                         musb->int_usb &= ~MUSB_INTR_SUSPEND;
497                                         break;
498                                 }
499                                 musb_g_resume(musb);
500                                 break;
501                         case OTG_STATE_B_IDLE:
502                                 musb->int_usb &= ~MUSB_INTR_SUSPEND;
503                                 break;
504                         default:
505                                 WARNING("bogus %s RESUME (%s)\n",
506                                         "peripheral",
507                                         otg_state_string(musb->xceiv->state));
508                         }
509                 }
510         }
511
512         /* see manual for the order of the tests */
513         if (int_usb & MUSB_INTR_SESSREQ) {
514                 void __iomem *mbase = musb->mregs;
515
516                 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
517                                 && (devctl & MUSB_DEVCTL_BDEVICE)) {
518                         dev_dbg(musb->controller, "SessReq while on B state\n");
519                         return IRQ_HANDLED;
520                 }
521
522                 dev_dbg(musb->controller, "SESSION_REQUEST (%s)\n",
523                         otg_state_string(musb->xceiv->state));
524
525                 /* IRQ arrives from ID pin sense or (later, if VBUS power
526                  * is removed) SRP.  responses are time critical:
527                  *  - turn on VBUS (with silicon-specific mechanism)
528                  *  - go through A_WAIT_VRISE
529                  *  - ... to A_WAIT_BCON.
530                  * a_wait_vrise_tmout triggers VBUS_ERROR transitions
531                  */
532                 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
533                 musb->ep0_stage = MUSB_EP0_START;
534                 musb->xceiv->state = OTG_STATE_A_IDLE;
535                 MUSB_HST_MODE(musb);
536                 musb_platform_set_vbus(musb, 1);
537
538                 handled = IRQ_HANDLED;
539         }
540
541         if (int_usb & MUSB_INTR_VBUSERROR) {
542                 int     ignore = 0;
543
544                 /* During connection as an A-Device, we may see a short
545                  * current spikes causing voltage drop, because of cable
546                  * and peripheral capacitance combined with vbus draw.
547                  * (So: less common with truly self-powered devices, where
548                  * vbus doesn't act like a power supply.)
549                  *
550                  * Such spikes are short; usually less than ~500 usec, max
551                  * of ~2 msec.  That is, they're not sustained overcurrent
552                  * errors, though they're reported using VBUSERROR irqs.
553                  *
554                  * Workarounds:  (a) hardware: use self powered devices.
555                  * (b) software:  ignore non-repeated VBUS errors.
556                  *
557                  * REVISIT:  do delays from lots of DEBUG_KERNEL checks
558                  * make trouble here, keeping VBUS < 4.4V ?
559                  */
560                 switch (musb->xceiv->state) {
561                 case OTG_STATE_A_HOST:
562                         /* recovery is dicey once we've gotten past the
563                          * initial stages of enumeration, but if VBUS
564                          * stayed ok at the other end of the link, and
565                          * another reset is due (at least for high speed,
566                          * to redo the chirp etc), it might work OK...
567                          */
568                 case OTG_STATE_A_WAIT_BCON:
569                 case OTG_STATE_A_WAIT_VRISE:
570                         if (musb->vbuserr_retry) {
571                                 void __iomem *mbase = musb->mregs;
572
573                                 musb->vbuserr_retry--;
574                                 ignore = 1;
575                                 devctl |= MUSB_DEVCTL_SESSION;
576                                 musb_writeb(mbase, MUSB_DEVCTL, devctl);
577                         } else {
578                                 musb->port1_status |=
579                                           USB_PORT_STAT_OVERCURRENT
580                                         | (USB_PORT_STAT_C_OVERCURRENT << 16);
581                         }
582                         break;
583                 default:
584                         break;
585                 }
586
587                 dev_dbg(musb->controller, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
588                                 otg_state_string(musb->xceiv->state),
589                                 devctl,
590                                 ({ char *s;
591                                 switch (devctl & MUSB_DEVCTL_VBUS) {
592                                 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
593                                         s = "<SessEnd"; break;
594                                 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
595                                         s = "<AValid"; break;
596                                 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
597                                         s = "<VBusValid"; break;
598                                 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
599                                 default:
600                                         s = "VALID"; break;
601                                 }; s; }),
602                                 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
603                                 musb->port1_status);
604
605                 /* go through A_WAIT_VFALL then start a new session */
606                 if (!ignore)
607                         musb_platform_set_vbus(musb, 0);
608                 handled = IRQ_HANDLED;
609         }
610
611         if (int_usb & MUSB_INTR_SUSPEND) {
612                 dev_dbg(musb->controller, "SUSPEND (%s) devctl %02x power %02x\n",
613                         otg_state_string(musb->xceiv->state), devctl, power);
614                 handled = IRQ_HANDLED;
615
616                 switch (musb->xceiv->state) {
617                 case OTG_STATE_A_PERIPHERAL:
618                         /* We also come here if the cable is removed, since
619                          * this silicon doesn't report ID-no-longer-grounded.
620                          *
621                          * We depend on T(a_wait_bcon) to shut us down, and
622                          * hope users don't do anything dicey during this
623                          * undesired detour through A_WAIT_BCON.
624                          */
625                         musb_hnp_stop(musb);
626                         usb_hcd_resume_root_hub(musb_to_hcd(musb));
627                         musb_root_disconnect(musb);
628                         musb_platform_try_idle(musb, jiffies
629                                         + msecs_to_jiffies(musb->a_wait_bcon
630                                                 ? : OTG_TIME_A_WAIT_BCON));
631
632                         break;
633                 case OTG_STATE_B_IDLE:
634                         if (!musb->is_active)
635                                 break;
636                 case OTG_STATE_B_PERIPHERAL:
637                         musb_g_suspend(musb);
638                         musb->is_active = is_otg_enabled(musb)
639                                         && otg->gadget->b_hnp_enable;
640                         if (musb->is_active) {
641                                 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
642                                 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
643                                 mod_timer(&musb->otg_timer, jiffies
644                                         + msecs_to_jiffies(
645                                                         OTG_TIME_B_ASE0_BRST));
646                         }
647                         break;
648                 case OTG_STATE_A_WAIT_BCON:
649                         if (musb->a_wait_bcon != 0)
650                                 musb_platform_try_idle(musb, jiffies
651                                         + msecs_to_jiffies(musb->a_wait_bcon));
652                         break;
653                 case OTG_STATE_A_HOST:
654                         musb->xceiv->state = OTG_STATE_A_SUSPEND;
655                         musb->is_active = is_otg_enabled(musb)
656                                         && otg->host->b_hnp_enable;
657                         break;
658                 case OTG_STATE_B_HOST:
659                         /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
660                         dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
661                         break;
662                 default:
663                         /* "should not happen" */
664                         musb->is_active = 0;
665                         break;
666                 }
667         }
668 #endif
669
670         if (int_usb & MUSB_INTR_CONNECT) {
671                 struct usb_hcd *hcd = musb_to_hcd(musb);
672
673                 handled = IRQ_HANDLED;
674                 musb->is_active = 1;
675
676                 musb->ep0_stage = MUSB_EP0_START;
677
678                 /* flush endpoints when transitioning from Device Mode */
679                 if (is_peripheral_active(musb)) {
680                         /* REVISIT HNP; just force disconnect */
681                 }
682                 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
683                 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
684                 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
685 #ifndef __UBOOT__
686                 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
687                                         |USB_PORT_STAT_HIGH_SPEED
688                                         |USB_PORT_STAT_ENABLE
689                                         );
690                 musb->port1_status |= USB_PORT_STAT_CONNECTION
691                                         |(USB_PORT_STAT_C_CONNECTION << 16);
692
693                 /* high vs full speed is just a guess until after reset */
694                 if (devctl & MUSB_DEVCTL_LSDEV)
695                         musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
696
697                 /* indicate new connection to OTG machine */
698                 switch (musb->xceiv->state) {
699                 case OTG_STATE_B_PERIPHERAL:
700                         if (int_usb & MUSB_INTR_SUSPEND) {
701                                 dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
702                                 int_usb &= ~MUSB_INTR_SUSPEND;
703                                 goto b_host;
704                         } else
705                                 dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
706                         break;
707                 case OTG_STATE_B_WAIT_ACON:
708                         dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
709 b_host:
710                         musb->xceiv->state = OTG_STATE_B_HOST;
711                         hcd->self.is_b_host = 1;
712                         musb->ignore_disconnect = 0;
713                         del_timer(&musb->otg_timer);
714                         break;
715                 default:
716                         if ((devctl & MUSB_DEVCTL_VBUS)
717                                         == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
718                                 musb->xceiv->state = OTG_STATE_A_HOST;
719                                 hcd->self.is_b_host = 0;
720                         }
721                         break;
722                 }
723
724                 /* poke the root hub */
725                 MUSB_HST_MODE(musb);
726                 if (hcd->status_urb)
727                         usb_hcd_poll_rh_status(hcd);
728                 else
729                         usb_hcd_resume_root_hub(hcd);
730
731                 dev_dbg(musb->controller, "CONNECT (%s) devctl %02x\n",
732                                 otg_state_string(musb->xceiv->state), devctl);
733 #endif
734         }
735
736 #ifndef __UBOOT__
737         if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
738                 dev_dbg(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
739                                 otg_state_string(musb->xceiv->state),
740                                 MUSB_MODE(musb), devctl);
741                 handled = IRQ_HANDLED;
742
743                 switch (musb->xceiv->state) {
744                 case OTG_STATE_A_HOST:
745                 case OTG_STATE_A_SUSPEND:
746                         usb_hcd_resume_root_hub(musb_to_hcd(musb));
747                         musb_root_disconnect(musb);
748                         if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
749                                 musb_platform_try_idle(musb, jiffies
750                                         + msecs_to_jiffies(musb->a_wait_bcon));
751                         break;
752                 case OTG_STATE_B_HOST:
753                         /* REVISIT this behaves for "real disconnect"
754                          * cases; make sure the other transitions from
755                          * from B_HOST act right too.  The B_HOST code
756                          * in hnp_stop() is currently not used...
757                          */
758                         musb_root_disconnect(musb);
759                         musb_to_hcd(musb)->self.is_b_host = 0;
760                         musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
761                         MUSB_DEV_MODE(musb);
762                         musb_g_disconnect(musb);
763                         break;
764                 case OTG_STATE_A_PERIPHERAL:
765                         musb_hnp_stop(musb);
766                         musb_root_disconnect(musb);
767                         /* FALLTHROUGH */
768                 case OTG_STATE_B_WAIT_ACON:
769                         /* FALLTHROUGH */
770                 case OTG_STATE_B_PERIPHERAL:
771                 case OTG_STATE_B_IDLE:
772                         musb_g_disconnect(musb);
773                         break;
774                 default:
775                         WARNING("unhandled DISCONNECT transition (%s)\n",
776                                 otg_state_string(musb->xceiv->state));
777                         break;
778                 }
779         }
780
781         /* mentor saves a bit: bus reset and babble share the same irq.
782          * only host sees babble; only peripheral sees bus reset.
783          */
784         if (int_usb & MUSB_INTR_RESET) {
785                 handled = IRQ_HANDLED;
786                 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
787                         /*
788                          * Looks like non-HS BABBLE can be ignored, but
789                          * HS BABBLE is an error condition. For HS the solution
790                          * is to avoid babble in the first place and fix what
791                          * caused BABBLE. When HS BABBLE happens we can only
792                          * stop the session.
793                          */
794                         if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
795                                 dev_dbg(musb->controller, "BABBLE devctl: %02x\n", devctl);
796                         else {
797                                 ERR("Stopping host session -- babble\n");
798                                 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
799                         }
800                 } else if (is_peripheral_capable()) {
801                         dev_dbg(musb->controller, "BUS RESET as %s\n",
802                                 otg_state_string(musb->xceiv->state));
803                         switch (musb->xceiv->state) {
804                         case OTG_STATE_A_SUSPEND:
805                                 /* We need to ignore disconnect on suspend
806                                  * otherwise tusb 2.0 won't reconnect after a
807                                  * power cycle, which breaks otg compliance.
808                                  */
809                                 musb->ignore_disconnect = 1;
810                                 musb_g_reset(musb);
811                                 /* FALLTHROUGH */
812                         case OTG_STATE_A_WAIT_BCON:     /* OPT TD.4.7-900ms */
813                                 /* never use invalid T(a_wait_bcon) */
814                                 dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
815                                         otg_state_string(musb->xceiv->state),
816                                         TA_WAIT_BCON(musb));
817                                 mod_timer(&musb->otg_timer, jiffies
818                                         + msecs_to_jiffies(TA_WAIT_BCON(musb)));
819                                 break;
820                         case OTG_STATE_A_PERIPHERAL:
821                                 musb->ignore_disconnect = 0;
822                                 del_timer(&musb->otg_timer);
823                                 musb_g_reset(musb);
824                                 break;
825                         case OTG_STATE_B_WAIT_ACON:
826                                 dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
827                                         otg_state_string(musb->xceiv->state));
828                                 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
829                                 musb_g_reset(musb);
830                                 break;
831                         case OTG_STATE_B_IDLE:
832                                 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
833                                 /* FALLTHROUGH */
834                         case OTG_STATE_B_PERIPHERAL:
835                                 musb_g_reset(musb);
836                                 break;
837                         default:
838                                 dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
839                                         otg_state_string(musb->xceiv->state));
840                         }
841                 }
842         }
843 #endif
844
845 #if 0
846 /* REVISIT ... this would be for multiplexing periodic endpoints, or
847  * supporting transfer phasing to prevent exceeding ISO bandwidth
848  * limits of a given frame or microframe.
849  *
850  * It's not needed for peripheral side, which dedicates endpoints;
851  * though it _might_ use SOF irqs for other purposes.
852  *
853  * And it's not currently needed for host side, which also dedicates
854  * endpoints, relies on TX/RX interval registers, and isn't claimed
855  * to support ISO transfers yet.
856  */
857         if (int_usb & MUSB_INTR_SOF) {
858                 void __iomem *mbase = musb->mregs;
859                 struct musb_hw_ep       *ep;
860                 u8 epnum;
861                 u16 frame;
862
863                 dev_dbg(musb->controller, "START_OF_FRAME\n");
864                 handled = IRQ_HANDLED;
865
866                 /* start any periodic Tx transfers waiting for current frame */
867                 frame = musb_readw(mbase, MUSB_FRAME);
868                 ep = musb->endpoints;
869                 for (epnum = 1; (epnum < musb->nr_endpoints)
870                                         && (musb->epmask >= (1 << epnum));
871                                 epnum++, ep++) {
872                         /*
873                          * FIXME handle framecounter wraps (12 bits)
874                          * eliminate duplicated StartUrb logic
875                          */
876                         if (ep->dwWaitFrame >= frame) {
877                                 ep->dwWaitFrame = 0;
878                                 pr_debug("SOF --> periodic TX%s on %d\n",
879                                         ep->tx_channel ? " DMA" : "",
880                                         epnum);
881                                 if (!ep->tx_channel)
882                                         musb_h_tx_start(musb, epnum);
883                                 else
884                                         cppi_hostdma_start(musb, epnum);
885                         }
886                 }               /* end of for loop */
887         }
888 #endif
889
890         schedule_work(&musb->irq_work);
891
892         return handled;
893 }
894
895 /*-------------------------------------------------------------------------*/
896
897 /*
898 * Program the HDRC to start (enable interrupts, dma, etc.).
899 */
900 #ifndef __UBOOT__
901 void musb_start(struct musb *musb)
902 #else
903 int musb_start(struct musb *musb)
904 #endif
905 {
906         void __iomem    *regs = musb->mregs;
907         u8              devctl = musb_readb(regs, MUSB_DEVCTL);
908 #ifdef __UBOOT__
909         int ret;
910 #endif
911
912         dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
913
914         /*  Set INT enable registers, enable interrupts */
915         musb_writew(regs, MUSB_INTRTXE, musb->epmask);
916         musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
917         musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
918
919         musb_writeb(regs, MUSB_TESTMODE, 0);
920
921         /* put into basic highspeed mode and start session */
922         musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
923                                                 | MUSB_POWER_HSENAB
924                                                 /* ENSUSPEND wedges tusb */
925                                                 /* | MUSB_POWER_ENSUSPEND */
926                                                 );
927
928         musb->is_active = 0;
929         devctl = musb_readb(regs, MUSB_DEVCTL);
930         devctl &= ~MUSB_DEVCTL_SESSION;
931
932         if (is_otg_enabled(musb)) {
933 #ifndef __UBOOT__
934                 /* session started after:
935                  * (a) ID-grounded irq, host mode;
936                  * (b) vbus present/connect IRQ, peripheral mode;
937                  * (c) peripheral initiates, using SRP
938                  */
939                 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
940                         musb->is_active = 1;
941                 else
942                         devctl |= MUSB_DEVCTL_SESSION;
943 #endif
944
945         } else if (is_host_enabled(musb)) {
946                 /* assume ID pin is hard-wired to ground */
947                 devctl |= MUSB_DEVCTL_SESSION;
948
949         } else /* peripheral is enabled */ {
950                 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
951                         musb->is_active = 1;
952         }
953
954 #ifndef __UBOOT__
955         musb_platform_enable(musb);
956 #else
957         ret = musb_platform_enable(musb);
958         if (ret) {
959                 musb->is_active = 0;
960                 return ret;
961         }
962 #endif
963         musb_writeb(regs, MUSB_DEVCTL, devctl);
964
965 #ifdef __UBOOT__
966         return 0;
967 #endif
968 }
969
970
971 static void musb_generic_disable(struct musb *musb)
972 {
973         void __iomem    *mbase = musb->mregs;
974         u16     temp;
975
976         /* disable interrupts */
977         musb_writeb(mbase, MUSB_INTRUSBE, 0);
978         musb_writew(mbase, MUSB_INTRTXE, 0);
979         musb_writew(mbase, MUSB_INTRRXE, 0);
980
981         /* off */
982         musb_writeb(mbase, MUSB_DEVCTL, 0);
983
984         /*  flush pending interrupts */
985         temp = musb_readb(mbase, MUSB_INTRUSB);
986         temp = musb_readw(mbase, MUSB_INTRTX);
987         temp = musb_readw(mbase, MUSB_INTRRX);
988
989 }
990
991 /*
992  * Make the HDRC stop (disable interrupts, etc.);
993  * reversible by musb_start
994  * called on gadget driver unregister
995  * with controller locked, irqs blocked
996  * acts as a NOP unless some role activated the hardware
997  */
998 void musb_stop(struct musb *musb)
999 {
1000         /* stop IRQs, timers, ... */
1001         musb_platform_disable(musb);
1002         musb_generic_disable(musb);
1003         dev_dbg(musb->controller, "HDRC disabled\n");
1004
1005         /* FIXME
1006          *  - mark host and/or peripheral drivers unusable/inactive
1007          *  - disable DMA (and enable it in HdrcStart)
1008          *  - make sure we can musb_start() after musb_stop(); with
1009          *    OTG mode, gadget driver module rmmod/modprobe cycles that
1010          *  - ...
1011          */
1012         musb_platform_try_idle(musb, 0);
1013         musb_platform_exit(musb);
1014 }
1015
1016 #ifndef __UBOOT__
1017 static void musb_shutdown(struct platform_device *pdev)
1018 {
1019         struct musb     *musb = dev_to_musb(&pdev->dev);
1020         unsigned long   flags;
1021
1022         pm_runtime_get_sync(musb->controller);
1023
1024         musb_gadget_cleanup(musb);
1025
1026         spin_lock_irqsave(&musb->lock, flags);
1027         musb_platform_disable(musb);
1028         musb_generic_disable(musb);
1029         spin_unlock_irqrestore(&musb->lock, flags);
1030
1031         if (!is_otg_enabled(musb) && is_host_enabled(musb))
1032                 usb_remove_hcd(musb_to_hcd(musb));
1033         musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1034         musb_platform_exit(musb);
1035
1036         pm_runtime_put(musb->controller);
1037         /* FIXME power down */
1038 }
1039 #endif
1040
1041
1042 /*-------------------------------------------------------------------------*/
1043
1044 /*
1045  * The silicon either has hard-wired endpoint configurations, or else
1046  * "dynamic fifo" sizing.  The driver has support for both, though at this
1047  * writing only the dynamic sizing is very well tested.   Since we switched
1048  * away from compile-time hardware parameters, we can no longer rely on
1049  * dead code elimination to leave only the relevant one in the object file.
1050  *
1051  * We don't currently use dynamic fifo setup capability to do anything
1052  * more than selecting one of a bunch of predefined configurations.
1053  */
1054 #if defined(CONFIG_USB_MUSB_TUSB6010)                   \
1055         || defined(CONFIG_USB_MUSB_TUSB6010_MODULE)     \
1056         || defined(CONFIG_USB_MUSB_OMAP2PLUS)           \
1057         || defined(CONFIG_USB_MUSB_OMAP2PLUS_MODULE)    \
1058         || defined(CONFIG_USB_MUSB_AM35X)               \
1059         || defined(CONFIG_USB_MUSB_AM35X_MODULE)        \
1060         || defined(CONFIG_USB_MUSB_DSPS)                \
1061         || defined(CONFIG_USB_MUSB_DSPS_MODULE)
1062 static ushort __devinitdata fifo_mode = 4;
1063 #elif defined(CONFIG_USB_MUSB_UX500)                    \
1064         || defined(CONFIG_USB_MUSB_UX500_MODULE)
1065 static ushort __devinitdata fifo_mode = 5;
1066 #else
1067 static ushort __devinitdata fifo_mode = 2;
1068 #endif
1069
1070 /* "modprobe ... fifo_mode=1" etc */
1071 module_param(fifo_mode, ushort, 0);
1072 MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1073
1074 /*
1075  * tables defining fifo_mode values.  define more if you like.
1076  * for host side, make sure both halves of ep1 are set up.
1077  */
1078
1079 /* mode 0 - fits in 2KB */
1080 static struct musb_fifo_cfg __devinitdata mode_0_cfg[] = {
1081 { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, },
1082 { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, },
1083 { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1084 { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1085 { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1086 };
1087
1088 /* mode 1 - fits in 4KB */
1089 static struct musb_fifo_cfg __devinitdata mode_1_cfg[] = {
1090 { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1091 { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1092 { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1093 { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1094 { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1095 };
1096
1097 /* mode 2 - fits in 4KB */
1098 static struct musb_fifo_cfg __devinitdata mode_2_cfg[] = {
1099 { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, },
1100 { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, },
1101 { .hw_ep_num = 2, .style = FIFO_TX,   .maxpacket = 512, },
1102 { .hw_ep_num = 2, .style = FIFO_RX,   .maxpacket = 512, },
1103 { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1104 { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1105 };
1106
1107 /* mode 3 - fits in 4KB */
1108 static struct musb_fifo_cfg __devinitdata mode_3_cfg[] = {
1109 { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1110 { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1111 { .hw_ep_num = 2, .style = FIFO_TX,   .maxpacket = 512, },
1112 { .hw_ep_num = 2, .style = FIFO_RX,   .maxpacket = 512, },
1113 { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1114 { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1115 };
1116
1117 /* mode 4 - fits in 16KB */
1118 static struct musb_fifo_cfg __devinitdata mode_4_cfg[] = {
1119 { .hw_ep_num =  1, .style = FIFO_TX,   .maxpacket = 512, },
1120 { .hw_ep_num =  1, .style = FIFO_RX,   .maxpacket = 512, },
1121 { .hw_ep_num =  2, .style = FIFO_TX,   .maxpacket = 512, },
1122 { .hw_ep_num =  2, .style = FIFO_RX,   .maxpacket = 512, },
1123 { .hw_ep_num =  3, .style = FIFO_TX,   .maxpacket = 512, },
1124 { .hw_ep_num =  3, .style = FIFO_RX,   .maxpacket = 512, },
1125 { .hw_ep_num =  4, .style = FIFO_TX,   .maxpacket = 512, },
1126 { .hw_ep_num =  4, .style = FIFO_RX,   .maxpacket = 512, },
1127 { .hw_ep_num =  5, .style = FIFO_TX,   .maxpacket = 512, },
1128 { .hw_ep_num =  5, .style = FIFO_RX,   .maxpacket = 512, },
1129 { .hw_ep_num =  6, .style = FIFO_TX,   .maxpacket = 512, },
1130 { .hw_ep_num =  6, .style = FIFO_RX,   .maxpacket = 512, },
1131 { .hw_ep_num =  7, .style = FIFO_TX,   .maxpacket = 512, },
1132 { .hw_ep_num =  7, .style = FIFO_RX,   .maxpacket = 512, },
1133 { .hw_ep_num =  8, .style = FIFO_TX,   .maxpacket = 512, },
1134 { .hw_ep_num =  8, .style = FIFO_RX,   .maxpacket = 512, },
1135 { .hw_ep_num =  9, .style = FIFO_TX,   .maxpacket = 512, },
1136 { .hw_ep_num =  9, .style = FIFO_RX,   .maxpacket = 512, },
1137 { .hw_ep_num = 10, .style = FIFO_TX,   .maxpacket = 256, },
1138 { .hw_ep_num = 10, .style = FIFO_RX,   .maxpacket = 64, },
1139 { .hw_ep_num = 11, .style = FIFO_TX,   .maxpacket = 256, },
1140 { .hw_ep_num = 11, .style = FIFO_RX,   .maxpacket = 64, },
1141 { .hw_ep_num = 12, .style = FIFO_TX,   .maxpacket = 256, },
1142 { .hw_ep_num = 12, .style = FIFO_RX,   .maxpacket = 64, },
1143 { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
1144 { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1145 { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1146 };
1147
1148 /* mode 5 - fits in 8KB */
1149 static struct musb_fifo_cfg __devinitdata mode_5_cfg[] = {
1150 { .hw_ep_num =  1, .style = FIFO_TX,   .maxpacket = 512, },
1151 { .hw_ep_num =  1, .style = FIFO_RX,   .maxpacket = 512, },
1152 { .hw_ep_num =  2, .style = FIFO_TX,   .maxpacket = 512, },
1153 { .hw_ep_num =  2, .style = FIFO_RX,   .maxpacket = 512, },
1154 { .hw_ep_num =  3, .style = FIFO_TX,   .maxpacket = 512, },
1155 { .hw_ep_num =  3, .style = FIFO_RX,   .maxpacket = 512, },
1156 { .hw_ep_num =  4, .style = FIFO_TX,   .maxpacket = 512, },
1157 { .hw_ep_num =  4, .style = FIFO_RX,   .maxpacket = 512, },
1158 { .hw_ep_num =  5, .style = FIFO_TX,   .maxpacket = 512, },
1159 { .hw_ep_num =  5, .style = FIFO_RX,   .maxpacket = 512, },
1160 { .hw_ep_num =  6, .style = FIFO_TX,   .maxpacket = 32, },
1161 { .hw_ep_num =  6, .style = FIFO_RX,   .maxpacket = 32, },
1162 { .hw_ep_num =  7, .style = FIFO_TX,   .maxpacket = 32, },
1163 { .hw_ep_num =  7, .style = FIFO_RX,   .maxpacket = 32, },
1164 { .hw_ep_num =  8, .style = FIFO_TX,   .maxpacket = 32, },
1165 { .hw_ep_num =  8, .style = FIFO_RX,   .maxpacket = 32, },
1166 { .hw_ep_num =  9, .style = FIFO_TX,   .maxpacket = 32, },
1167 { .hw_ep_num =  9, .style = FIFO_RX,   .maxpacket = 32, },
1168 { .hw_ep_num = 10, .style = FIFO_TX,   .maxpacket = 32, },
1169 { .hw_ep_num = 10, .style = FIFO_RX,   .maxpacket = 32, },
1170 { .hw_ep_num = 11, .style = FIFO_TX,   .maxpacket = 32, },
1171 { .hw_ep_num = 11, .style = FIFO_RX,   .maxpacket = 32, },
1172 { .hw_ep_num = 12, .style = FIFO_TX,   .maxpacket = 32, },
1173 { .hw_ep_num = 12, .style = FIFO_RX,   .maxpacket = 32, },
1174 { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1175 { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1176 { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1177 };
1178
1179 /*
1180  * configure a fifo; for non-shared endpoints, this may be called
1181  * once for a tx fifo and once for an rx fifo.
1182  *
1183  * returns negative errno or offset for next fifo.
1184  */
1185 static int __devinit
1186 fifo_setup(struct musb *musb, struct musb_hw_ep  *hw_ep,
1187                 const struct musb_fifo_cfg *cfg, u16 offset)
1188 {
1189         void __iomem    *mbase = musb->mregs;
1190         int     size = 0;
1191         u16     maxpacket = cfg->maxpacket;
1192         u16     c_off = offset >> 3;
1193         u8      c_size;
1194
1195         /* expect hw_ep has already been zero-initialized */
1196
1197         size = ffs(max(maxpacket, (u16) 8)) - 1;
1198         maxpacket = 1 << size;
1199
1200         c_size = size - 3;
1201         if (cfg->mode == BUF_DOUBLE) {
1202                 if ((offset + (maxpacket << 1)) >
1203                                 (1 << (musb->config->ram_bits + 2)))
1204                         return -EMSGSIZE;
1205                 c_size |= MUSB_FIFOSZ_DPB;
1206         } else {
1207                 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
1208                         return -EMSGSIZE;
1209         }
1210
1211         /* configure the FIFO */
1212         musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1213
1214         /* EP0 reserved endpoint for control, bidirectional;
1215          * EP1 reserved for bulk, two unidirection halves.
1216          */
1217         if (hw_ep->epnum == 1)
1218                 musb->bulk_ep = hw_ep;
1219         /* REVISIT error check:  be sure ep0 can both rx and tx ... */
1220         switch (cfg->style) {
1221         case FIFO_TX:
1222                 musb_write_txfifosz(mbase, c_size);
1223                 musb_write_txfifoadd(mbase, c_off);
1224                 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1225                 hw_ep->max_packet_sz_tx = maxpacket;
1226                 break;
1227         case FIFO_RX:
1228                 musb_write_rxfifosz(mbase, c_size);
1229                 musb_write_rxfifoadd(mbase, c_off);
1230                 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1231                 hw_ep->max_packet_sz_rx = maxpacket;
1232                 break;
1233         case FIFO_RXTX:
1234                 musb_write_txfifosz(mbase, c_size);
1235                 musb_write_txfifoadd(mbase, c_off);
1236                 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1237                 hw_ep->max_packet_sz_rx = maxpacket;
1238
1239                 musb_write_rxfifosz(mbase, c_size);
1240                 musb_write_rxfifoadd(mbase, c_off);
1241                 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1242                 hw_ep->max_packet_sz_tx = maxpacket;
1243
1244                 hw_ep->is_shared_fifo = true;
1245                 break;
1246         }
1247
1248         /* NOTE rx and tx endpoint irqs aren't managed separately,
1249          * which happens to be ok
1250          */
1251         musb->epmask |= (1 << hw_ep->epnum);
1252
1253         return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1254 }
1255
1256 static struct musb_fifo_cfg __devinitdata ep0_cfg = {
1257         .style = FIFO_RXTX, .maxpacket = 64,
1258 };
1259
1260 static int __devinit ep_config_from_table(struct musb *musb)
1261 {
1262         const struct musb_fifo_cfg      *cfg;
1263         unsigned                i, n;
1264         int                     offset;
1265         struct musb_hw_ep       *hw_ep = musb->endpoints;
1266
1267         if (musb->config->fifo_cfg) {
1268                 cfg = musb->config->fifo_cfg;
1269                 n = musb->config->fifo_cfg_size;
1270                 goto done;
1271         }
1272
1273         switch (fifo_mode) {
1274         default:
1275                 fifo_mode = 0;
1276                 /* FALLTHROUGH */
1277         case 0:
1278                 cfg = mode_0_cfg;
1279                 n = ARRAY_SIZE(mode_0_cfg);
1280                 break;
1281         case 1:
1282                 cfg = mode_1_cfg;
1283                 n = ARRAY_SIZE(mode_1_cfg);
1284                 break;
1285         case 2:
1286                 cfg = mode_2_cfg;
1287                 n = ARRAY_SIZE(mode_2_cfg);
1288                 break;
1289         case 3:
1290                 cfg = mode_3_cfg;
1291                 n = ARRAY_SIZE(mode_3_cfg);
1292                 break;
1293         case 4:
1294                 cfg = mode_4_cfg;
1295                 n = ARRAY_SIZE(mode_4_cfg);
1296                 break;
1297         case 5:
1298                 cfg = mode_5_cfg;
1299                 n = ARRAY_SIZE(mode_5_cfg);
1300                 break;
1301         }
1302
1303         pr_debug("%s: setup fifo_mode %d\n", musb_driver_name, fifo_mode);
1304
1305 done:
1306         offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1307         /* assert(offset > 0) */
1308
1309         /* NOTE:  for RTL versions >= 1.400 EPINFO and RAMINFO would
1310          * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
1311          */
1312
1313         for (i = 0; i < n; i++) {
1314                 u8      epn = cfg->hw_ep_num;
1315
1316                 if (epn >= musb->config->num_eps) {
1317                         pr_debug("%s: invalid ep %d\n",
1318                                         musb_driver_name, epn);
1319                         return -EINVAL;
1320                 }
1321                 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1322                 if (offset < 0) {
1323                         pr_debug("%s: mem overrun, ep %d\n",
1324                                         musb_driver_name, epn);
1325                         return -EINVAL;
1326                 }
1327                 epn++;
1328                 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1329         }
1330
1331         pr_debug("%s: %d/%d max ep, %d/%d memory\n", musb_driver_name, n + 1,
1332                  musb->config->num_eps * 2 - 1, offset,
1333                  (1 << (musb->config->ram_bits + 2)));
1334
1335         if (!musb->bulk_ep) {
1336                 pr_debug("%s: missing bulk\n", musb_driver_name);
1337                 return -EINVAL;
1338         }
1339
1340         return 0;
1341 }
1342
1343
1344 /*
1345  * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1346  * @param musb the controller
1347  */
1348 static int __devinit ep_config_from_hw(struct musb *musb)
1349 {
1350         u8 epnum = 0;
1351         struct musb_hw_ep *hw_ep;
1352         void *mbase = musb->mregs;
1353         int ret = 0;
1354
1355         dev_dbg(musb->controller, "<== static silicon ep config\n");
1356
1357         /* FIXME pick up ep0 maxpacket size */
1358
1359         for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
1360                 musb_ep_select(mbase, epnum);
1361                 hw_ep = musb->endpoints + epnum;
1362
1363                 ret = musb_read_fifosize(musb, hw_ep, epnum);
1364                 if (ret < 0)
1365                         break;
1366
1367                 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1368
1369                 /* pick an RX/TX endpoint for bulk */
1370                 if (hw_ep->max_packet_sz_tx < 512
1371                                 || hw_ep->max_packet_sz_rx < 512)
1372                         continue;
1373
1374                 /* REVISIT:  this algorithm is lazy, we should at least
1375                  * try to pick a double buffered endpoint.
1376                  */
1377                 if (musb->bulk_ep)
1378                         continue;
1379                 musb->bulk_ep = hw_ep;
1380         }
1381
1382         if (!musb->bulk_ep) {
1383                 pr_debug("%s: missing bulk\n", musb_driver_name);
1384                 return -EINVAL;
1385         }
1386
1387         return 0;
1388 }
1389
1390 enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1391
1392 /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1393  * configure endpoints, or take their config from silicon
1394  */
1395 static int __devinit musb_core_init(u16 musb_type, struct musb *musb)
1396 {
1397         u8 reg;
1398         char *type;
1399         char aInfo[90], aRevision[32], aDate[12];
1400         void __iomem    *mbase = musb->mregs;
1401         int             status = 0;
1402         int             i;
1403
1404         /* log core options (read using indexed model) */
1405         reg = musb_read_configdata(mbase);
1406
1407         strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
1408         if (reg & MUSB_CONFIGDATA_DYNFIFO) {
1409                 strcat(aInfo, ", dyn FIFOs");
1410                 musb->dyn_fifo = true;
1411         }
1412 #ifndef CONFIG_USB_MUSB_DISABLE_BULK_COMBINE_SPLIT
1413         if (reg & MUSB_CONFIGDATA_MPRXE) {
1414                 strcat(aInfo, ", bulk combine");
1415                 musb->bulk_combine = true;
1416         }
1417         if (reg & MUSB_CONFIGDATA_MPTXE) {
1418                 strcat(aInfo, ", bulk split");
1419                 musb->bulk_split = true;
1420         }
1421 #else
1422         musb->bulk_combine = false;
1423         musb->bulk_split = false;
1424 #endif
1425         if (reg & MUSB_CONFIGDATA_HBRXE) {
1426                 strcat(aInfo, ", HB-ISO Rx");
1427                 musb->hb_iso_rx = true;
1428         }
1429         if (reg & MUSB_CONFIGDATA_HBTXE) {
1430                 strcat(aInfo, ", HB-ISO Tx");
1431                 musb->hb_iso_tx = true;
1432         }
1433         if (reg & MUSB_CONFIGDATA_SOFTCONE)
1434                 strcat(aInfo, ", SoftConn");
1435
1436         pr_debug("%s:ConfigData=0x%02x (%s)\n", musb_driver_name, reg, aInfo);
1437
1438         aDate[0] = 0;
1439         if (MUSB_CONTROLLER_MHDRC == musb_type) {
1440                 musb->is_multipoint = 1;
1441                 type = "M";
1442         } else {
1443                 musb->is_multipoint = 0;
1444                 type = "";
1445 #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1446                 printk(KERN_ERR
1447                         "%s: kernel must blacklist external hubs\n",
1448                         musb_driver_name);
1449 #endif
1450         }
1451
1452         /* log release info */
1453         musb->hwvers = musb_read_hwvers(mbase);
1454         snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1455                 MUSB_HWVERS_MINOR(musb->hwvers),
1456                 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
1457         pr_debug("%s: %sHDRC RTL version %s %s\n", musb_driver_name, type,
1458                  aRevision, aDate);
1459
1460         /* configure ep0 */
1461         musb_configure_ep0(musb);
1462
1463         /* discover endpoint configuration */
1464         musb->nr_endpoints = 1;
1465         musb->epmask = 1;
1466
1467         if (musb->dyn_fifo)
1468                 status = ep_config_from_table(musb);
1469         else
1470                 status = ep_config_from_hw(musb);
1471
1472         if (status < 0)
1473                 return status;
1474
1475         /* finish init, and print endpoint config */
1476         for (i = 0; i < musb->nr_endpoints; i++) {
1477                 struct musb_hw_ep       *hw_ep = musb->endpoints + i;
1478
1479                 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
1480 #if defined(CONFIG_USB_MUSB_TUSB6010) || defined (CONFIG_USB_MUSB_TUSB6010_MODULE)
1481                 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1482                 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1483                 hw_ep->fifo_sync_va =
1484                         musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1485
1486                 if (i == 0)
1487                         hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1488                 else
1489                         hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1490 #endif
1491
1492                 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1493                 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
1494                 hw_ep->rx_reinit = 1;
1495                 hw_ep->tx_reinit = 1;
1496
1497                 if (hw_ep->max_packet_sz_tx) {
1498                         dev_dbg(musb->controller,
1499                                 "%s: hw_ep %d%s, %smax %d\n",
1500                                 musb_driver_name, i,
1501                                 hw_ep->is_shared_fifo ? "shared" : "tx",
1502                                 hw_ep->tx_double_buffered
1503                                         ? "doublebuffer, " : "",
1504                                 hw_ep->max_packet_sz_tx);
1505                 }
1506                 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
1507                         dev_dbg(musb->controller,
1508                                 "%s: hw_ep %d%s, %smax %d\n",
1509                                 musb_driver_name, i,
1510                                 "rx",
1511                                 hw_ep->rx_double_buffered
1512                                         ? "doublebuffer, " : "",
1513                                 hw_ep->max_packet_sz_rx);
1514                 }
1515                 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1516                         dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
1517         }
1518
1519         return 0;
1520 }
1521
1522 /*-------------------------------------------------------------------------*/
1523
1524 #if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
1525         defined(CONFIG_ARCH_OMAP4)
1526
1527 static irqreturn_t generic_interrupt(int irq, void *__hci)
1528 {
1529         unsigned long   flags;
1530         irqreturn_t     retval = IRQ_NONE;
1531         struct musb     *musb = __hci;
1532
1533         spin_lock_irqsave(&musb->lock, flags);
1534
1535         musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1536         musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1537         musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1538
1539         if (musb->int_usb || musb->int_tx || musb->int_rx)
1540                 retval = musb_interrupt(musb);
1541
1542         spin_unlock_irqrestore(&musb->lock, flags);
1543
1544         return retval;
1545 }
1546
1547 #else
1548 #define generic_interrupt       NULL
1549 #endif
1550
1551 /*
1552  * handle all the irqs defined by the HDRC core. for now we expect:  other
1553  * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1554  * will be assigned, and the irq will already have been acked.
1555  *
1556  * called in irq context with spinlock held, irqs blocked
1557  */
1558 irqreturn_t musb_interrupt(struct musb *musb)
1559 {
1560         irqreturn_t     retval = IRQ_NONE;
1561         u8              devctl, power;
1562         int             ep_num;
1563         u32             reg;
1564
1565         devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1566         power = musb_readb(musb->mregs, MUSB_POWER);
1567
1568         dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
1569                 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1570                 musb->int_usb, musb->int_tx, musb->int_rx);
1571
1572         /* the core can interrupt us for multiple reasons; docs have
1573          * a generic interrupt flowchart to follow
1574          */
1575         if (musb->int_usb)
1576                 retval |= musb_stage0_irq(musb, musb->int_usb,
1577                                 devctl, power);
1578
1579         /* "stage 1" is handling endpoint irqs */
1580
1581         /* handle endpoint 0 first */
1582         if (musb->int_tx & 1) {
1583                 if (devctl & MUSB_DEVCTL_HM) {
1584                         if (is_host_capable())
1585                                 retval |= musb_h_ep0_irq(musb);
1586                 } else {
1587                         if (is_peripheral_capable())
1588                                 retval |= musb_g_ep0_irq(musb);
1589                 }
1590         }
1591
1592         /* RX on endpoints 1-15 */
1593         reg = musb->int_rx >> 1;
1594         ep_num = 1;
1595         while (reg) {
1596                 if (reg & 1) {
1597                         /* musb_ep_select(musb->mregs, ep_num); */
1598                         /* REVISIT just retval = ep->rx_irq(...) */
1599                         retval = IRQ_HANDLED;
1600                         if (devctl & MUSB_DEVCTL_HM) {
1601                                 if (is_host_capable())
1602                                         musb_host_rx(musb, ep_num);
1603                         } else {
1604                                 if (is_peripheral_capable())
1605                                         musb_g_rx(musb, ep_num);
1606                         }
1607                 }
1608
1609                 reg >>= 1;
1610                 ep_num++;
1611         }
1612
1613         /* TX on endpoints 1-15 */
1614         reg = musb->int_tx >> 1;
1615         ep_num = 1;
1616         while (reg) {
1617                 if (reg & 1) {
1618                         /* musb_ep_select(musb->mregs, ep_num); */
1619                         /* REVISIT just retval |= ep->tx_irq(...) */
1620                         retval = IRQ_HANDLED;
1621                         if (devctl & MUSB_DEVCTL_HM) {
1622                                 if (is_host_capable())
1623                                         musb_host_tx(musb, ep_num);
1624                         } else {
1625                                 if (is_peripheral_capable())
1626                                         musb_g_tx(musb, ep_num);
1627                         }
1628                 }
1629                 reg >>= 1;
1630                 ep_num++;
1631         }
1632
1633         return retval;
1634 }
1635 EXPORT_SYMBOL_GPL(musb_interrupt);
1636
1637 #ifndef CONFIG_USB_MUSB_PIO_ONLY
1638 static bool __devinitdata use_dma = 1;
1639
1640 /* "modprobe ... use_dma=0" etc */
1641 module_param(use_dma, bool, 0);
1642 MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1643
1644 void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1645 {
1646         u8      devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1647
1648         /* called with controller lock already held */
1649
1650         if (!epnum) {
1651 #ifndef CONFIG_USB_TUSB_OMAP_DMA
1652                 if (!is_cppi_enabled()) {
1653                         /* endpoint 0 */
1654                         if (devctl & MUSB_DEVCTL_HM)
1655                                 musb_h_ep0_irq(musb);
1656                         else
1657                                 musb_g_ep0_irq(musb);
1658                 }
1659 #endif
1660         } else {
1661                 /* endpoints 1..15 */
1662                 if (transmit) {
1663                         if (devctl & MUSB_DEVCTL_HM) {
1664                                 if (is_host_capable())
1665                                         musb_host_tx(musb, epnum);
1666                         } else {
1667                                 if (is_peripheral_capable())
1668                                         musb_g_tx(musb, epnum);
1669                         }
1670                 } else {
1671                         /* receive */
1672                         if (devctl & MUSB_DEVCTL_HM) {
1673                                 if (is_host_capable())
1674                                         musb_host_rx(musb, epnum);
1675                         } else {
1676                                 if (is_peripheral_capable())
1677                                         musb_g_rx(musb, epnum);
1678                         }
1679                 }
1680         }
1681 }
1682 EXPORT_SYMBOL_GPL(musb_dma_completion);
1683
1684 #else
1685 #define use_dma                 0
1686 #endif
1687
1688 /*-------------------------------------------------------------------------*/
1689
1690 #ifdef CONFIG_SYSFS
1691
1692 static ssize_t
1693 musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1694 {
1695         struct musb *musb = dev_to_musb(dev);
1696         unsigned long flags;
1697         int ret = -EINVAL;
1698
1699         spin_lock_irqsave(&musb->lock, flags);
1700         ret = sprintf(buf, "%s\n", otg_state_string(musb->xceiv->state));
1701         spin_unlock_irqrestore(&musb->lock, flags);
1702
1703         return ret;
1704 }
1705
1706 static ssize_t
1707 musb_mode_store(struct device *dev, struct device_attribute *attr,
1708                 const char *buf, size_t n)
1709 {
1710         struct musb     *musb = dev_to_musb(dev);
1711         unsigned long   flags;
1712         int             status;
1713
1714         spin_lock_irqsave(&musb->lock, flags);
1715         if (sysfs_streq(buf, "host"))
1716                 status = musb_platform_set_mode(musb, MUSB_HOST);
1717         else if (sysfs_streq(buf, "peripheral"))
1718                 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1719         else if (sysfs_streq(buf, "otg"))
1720                 status = musb_platform_set_mode(musb, MUSB_OTG);
1721         else
1722                 status = -EINVAL;
1723         spin_unlock_irqrestore(&musb->lock, flags);
1724
1725         return (status == 0) ? n : status;
1726 }
1727 static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1728
1729 static ssize_t
1730 musb_vbus_store(struct device *dev, struct device_attribute *attr,
1731                 const char *buf, size_t n)
1732 {
1733         struct musb     *musb = dev_to_musb(dev);
1734         unsigned long   flags;
1735         unsigned long   val;
1736
1737         if (sscanf(buf, "%lu", &val) < 1) {
1738                 dev_err(dev, "Invalid VBUS timeout ms value\n");
1739                 return -EINVAL;
1740         }
1741
1742         spin_lock_irqsave(&musb->lock, flags);
1743         /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1744         musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
1745         if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
1746                 musb->is_active = 0;
1747         musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1748         spin_unlock_irqrestore(&musb->lock, flags);
1749
1750         return n;
1751 }
1752
1753 static ssize_t
1754 musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1755 {
1756         struct musb     *musb = dev_to_musb(dev);
1757         unsigned long   flags;
1758         unsigned long   val;
1759         int             vbus;
1760
1761         spin_lock_irqsave(&musb->lock, flags);
1762         val = musb->a_wait_bcon;
1763         /* FIXME get_vbus_status() is normally #defined as false...
1764          * and is effectively TUSB-specific.
1765          */
1766         vbus = musb_platform_get_vbus_status(musb);
1767         spin_unlock_irqrestore(&musb->lock, flags);
1768
1769         return sprintf(buf, "Vbus %s, timeout %lu msec\n",
1770                         vbus ? "on" : "off", val);
1771 }
1772 static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1773
1774 /* Gadget drivers can't know that a host is connected so they might want
1775  * to start SRP, but users can.  This allows userspace to trigger SRP.
1776  */
1777 static ssize_t
1778 musb_srp_store(struct device *dev, struct device_attribute *attr,
1779                 const char *buf, size_t n)
1780 {
1781         struct musb     *musb = dev_to_musb(dev);
1782         unsigned short  srp;
1783
1784         if (sscanf(buf, "%hu", &srp) != 1
1785                         || (srp != 1)) {
1786                 dev_err(dev, "SRP: Value must be 1\n");
1787                 return -EINVAL;
1788         }
1789
1790         if (srp == 1)
1791                 musb_g_wakeup(musb);
1792
1793         return n;
1794 }
1795 static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1796
1797 static struct attribute *musb_attributes[] = {
1798         &dev_attr_mode.attr,
1799         &dev_attr_vbus.attr,
1800         &dev_attr_srp.attr,
1801         NULL
1802 };
1803
1804 static const struct attribute_group musb_attr_group = {
1805         .attrs = musb_attributes,
1806 };
1807
1808 #endif  /* sysfs */
1809
1810 #ifndef __UBOOT__
1811 /* Only used to provide driver mode change events */
1812 static void musb_irq_work(struct work_struct *data)
1813 {
1814         struct musb *musb = container_of(data, struct musb, irq_work);
1815         static int old_state;
1816
1817         if (musb->xceiv->state != old_state) {
1818                 old_state = musb->xceiv->state;
1819                 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1820         }
1821 }
1822 #endif
1823
1824 /* --------------------------------------------------------------------------
1825  * Init support
1826  */
1827
1828 static struct musb *__devinit
1829 allocate_instance(struct device *dev,
1830                 struct musb_hdrc_config *config, void __iomem *mbase)
1831 {
1832         struct musb             *musb;
1833         struct musb_hw_ep       *ep;
1834         int                     epnum;
1835 #ifndef __UBOOT__
1836         struct usb_hcd  *hcd;
1837
1838         hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
1839         if (!hcd)
1840                 return NULL;
1841         /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1842
1843         musb = hcd_to_musb(hcd);
1844 #else
1845         musb = calloc(1, sizeof(*musb));
1846         if (!musb)
1847                 return NULL;
1848 #endif
1849         INIT_LIST_HEAD(&musb->control);
1850         INIT_LIST_HEAD(&musb->in_bulk);
1851         INIT_LIST_HEAD(&musb->out_bulk);
1852
1853 #ifndef __UBOOT__
1854         hcd->uses_new_polling = 1;
1855         hcd->has_tt = 1;
1856 #endif
1857
1858         musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
1859         musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
1860         dev_set_drvdata(dev, musb);
1861         musb->mregs = mbase;
1862         musb->ctrl_base = mbase;
1863         musb->nIrq = -ENODEV;
1864         musb->config = config;
1865 #ifdef __UBOOT__
1866         assert_noisy(musb->config->num_eps <= MUSB_C_NUM_EPS);
1867 #else
1868         BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
1869 #endif
1870         for (epnum = 0, ep = musb->endpoints;
1871                         epnum < musb->config->num_eps;
1872                         epnum++, ep++) {
1873                 ep->musb = musb;
1874                 ep->epnum = epnum;
1875         }
1876
1877         musb->controller = dev;
1878
1879         return musb;
1880 }
1881
1882 static void musb_free(struct musb *musb)
1883 {
1884         /* this has multiple entry modes. it handles fault cleanup after
1885          * probe(), where things may be partially set up, as well as rmmod
1886          * cleanup after everything's been de-activated.
1887          */
1888
1889 #ifdef CONFIG_SYSFS
1890         sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
1891 #endif
1892
1893         if (musb->nIrq >= 0) {
1894                 if (musb->irq_wake)
1895                         disable_irq_wake(musb->nIrq);
1896                 free_irq(musb->nIrq, musb);
1897         }
1898         if (is_dma_capable() && musb->dma_controller) {
1899                 struct dma_controller   *c = musb->dma_controller;
1900
1901                 (void) c->stop(c);
1902                 dma_controller_destroy(c);
1903         }
1904
1905         kfree(musb);
1906 }
1907
1908 /*
1909  * Perform generic per-controller initialization.
1910  *
1911  * @pDevice: the controller (already clocked, etc)
1912  * @nIrq: irq
1913  * @mregs: virtual address of controller registers,
1914  *      not yet corrected for platform-specific offsets
1915  */
1916 #ifndef __UBOOT__
1917 static int __devinit
1918 musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1919 #else
1920 struct musb *
1921 musb_init_controller(struct musb_hdrc_platform_data *plat, struct device *dev,
1922                              void *ctrl)
1923 #endif
1924 {
1925         int                     status;
1926         struct musb             *musb;
1927 #ifndef __UBOOT__
1928         struct musb_hdrc_platform_data *plat = dev->platform_data;
1929 #else
1930         int nIrq = 0;
1931 #endif
1932
1933         /* The driver might handle more features than the board; OK.
1934          * Fail when the board needs a feature that's not enabled.
1935          */
1936         if (!plat) {
1937                 dev_dbg(dev, "no platform_data?\n");
1938                 status = -ENODEV;
1939                 goto fail0;
1940         }
1941
1942         /* allocate */
1943         musb = allocate_instance(dev, plat->config, ctrl);
1944         if (!musb) {
1945                 status = -ENOMEM;
1946                 goto fail0;
1947         }
1948
1949         pm_runtime_use_autosuspend(musb->controller);
1950         pm_runtime_set_autosuspend_delay(musb->controller, 200);
1951         pm_runtime_enable(musb->controller);
1952
1953         spin_lock_init(&musb->lock);
1954         musb->board_mode = plat->mode;
1955         musb->board_set_power = plat->set_power;
1956         musb->min_power = plat->min_power;
1957         musb->ops = plat->platform_ops;
1958
1959         /* The musb_platform_init() call:
1960          *   - adjusts musb->mregs and musb->isr if needed,
1961          *   - may initialize an integrated tranceiver
1962          *   - initializes musb->xceiv, usually by otg_get_phy()
1963          *   - stops powering VBUS
1964          *
1965          * There are various transceiver configurations.  Blackfin,
1966          * DaVinci, TUSB60x0, and others integrate them.  OMAP3 uses
1967          * external/discrete ones in various flavors (twl4030 family,
1968          * isp1504, non-OTG, etc) mostly hooking up through ULPI.
1969          */
1970         musb->isr = generic_interrupt;
1971         status = musb_platform_init(musb);
1972         if (status < 0)
1973                 goto fail1;
1974
1975         if (!musb->isr) {
1976                 status = -ENODEV;
1977                 goto fail2;
1978         }
1979
1980 #ifndef __UBOOT__
1981         if (!musb->xceiv->io_ops) {
1982                 musb->xceiv->io_dev = musb->controller;
1983                 musb->xceiv->io_priv = musb->mregs;
1984                 musb->xceiv->io_ops = &musb_ulpi_access;
1985         }
1986 #endif
1987
1988         pm_runtime_get_sync(musb->controller);
1989
1990 #ifndef CONFIG_USB_MUSB_PIO_ONLY
1991         if (use_dma && dev->dma_mask) {
1992                 struct dma_controller   *c;
1993
1994                 c = dma_controller_create(musb, musb->mregs);
1995                 musb->dma_controller = c;
1996                 if (c)
1997                         (void) c->start(c);
1998         }
1999 #endif
2000 #ifndef __UBOOT__
2001         /* ideally this would be abstracted in platform setup */
2002         if (!is_dma_capable() || !musb->dma_controller)
2003                 dev->dma_mask = NULL;
2004 #endif
2005
2006         /* be sure interrupts are disabled before connecting ISR */
2007         musb_platform_disable(musb);
2008         musb_generic_disable(musb);
2009
2010         /* setup musb parts of the core (especially endpoints) */
2011         status = musb_core_init(plat->config->multipoint
2012                         ? MUSB_CONTROLLER_MHDRC
2013                         : MUSB_CONTROLLER_HDRC, musb);
2014         if (status < 0)
2015                 goto fail3;
2016
2017         setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2018
2019         /* Init IRQ workqueue before request_irq */
2020         INIT_WORK(&musb->irq_work, musb_irq_work);
2021
2022         /* attach to the IRQ */
2023         if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
2024                 dev_err(dev, "request_irq %d failed!\n", nIrq);
2025                 status = -ENODEV;
2026                 goto fail3;
2027         }
2028         musb->nIrq = nIrq;
2029 /* FIXME this handles wakeup irqs wrong */
2030         if (enable_irq_wake(nIrq) == 0) {
2031                 musb->irq_wake = 1;
2032                 device_init_wakeup(dev, 1);
2033         } else {
2034                 musb->irq_wake = 0;
2035         }
2036
2037 #ifndef __UBOOT__
2038         /* host side needs more setup */
2039         if (is_host_enabled(musb)) {
2040                 struct usb_hcd  *hcd = musb_to_hcd(musb);
2041
2042                 otg_set_host(musb->xceiv->otg, &hcd->self);
2043
2044                 if (is_otg_enabled(musb))
2045                         hcd->self.otg_port = 1;
2046                 musb->xceiv->otg->host = &hcd->self;
2047                 hcd->power_budget = 2 * (plat->power ? : 250);
2048
2049                 /* program PHY to use external vBus if required */
2050                 if (plat->extvbus) {
2051                         u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
2052                         busctl |= MUSB_ULPI_USE_EXTVBUS;
2053                         musb_write_ulpi_buscontrol(musb->mregs, busctl);
2054                 }
2055         }
2056 #endif
2057
2058         /* For the host-only role, we can activate right away.
2059          * (We expect the ID pin to be forcibly grounded!!)
2060          * Otherwise, wait till the gadget driver hooks up.
2061          */
2062         if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
2063                 struct usb_hcd  *hcd = musb_to_hcd(musb);
2064
2065                 MUSB_HST_MODE(musb);
2066 #ifndef __UBOOT__
2067                 musb->xceiv->otg->default_a = 1;
2068                 musb->xceiv->state = OTG_STATE_A_IDLE;
2069
2070                 status = usb_add_hcd(musb_to_hcd(musb), 0, 0);
2071
2072                 hcd->self.uses_pio_for_control = 1;
2073                 dev_dbg(musb->controller, "%s mode, status %d, devctl %02x %c\n",
2074                         "HOST", status,
2075                         musb_readb(musb->mregs, MUSB_DEVCTL),
2076                         (musb_readb(musb->mregs, MUSB_DEVCTL)
2077                                         & MUSB_DEVCTL_BDEVICE
2078                                 ? 'B' : 'A'));
2079 #endif
2080
2081         } else /* peripheral is enabled */ {
2082                 MUSB_DEV_MODE(musb);
2083 #ifndef __UBOOT__
2084                 musb->xceiv->otg->default_a = 0;
2085                 musb->xceiv->state = OTG_STATE_B_IDLE;
2086 #endif
2087
2088                 if (is_peripheral_capable())
2089                         status = musb_gadget_setup(musb);
2090
2091 #ifndef __UBOOT__
2092                 dev_dbg(musb->controller, "%s mode, status %d, dev%02x\n",
2093                         is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2094                         status,
2095                         musb_readb(musb->mregs, MUSB_DEVCTL));
2096 #endif
2097
2098         }
2099         if (status < 0)
2100                 goto fail3;
2101
2102         status = musb_init_debugfs(musb);
2103         if (status < 0)
2104                 goto fail4;
2105
2106 #ifdef CONFIG_SYSFS
2107         status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
2108         if (status)
2109                 goto fail5;
2110 #endif
2111
2112         pm_runtime_put(musb->controller);
2113
2114         pr_debug("USB %s mode controller at %p using %s, IRQ %d\n",
2115                         ({char *s;
2116                          switch (musb->board_mode) {
2117                          case MUSB_HOST:                s = "Host"; break;
2118                          case MUSB_PERIPHERAL:  s = "Peripheral"; break;
2119                          default:               s = "OTG"; break;
2120                          }; s; }),
2121                         ctrl,
2122                         (is_dma_capable() && musb->dma_controller)
2123                         ? "DMA" : "PIO",
2124                         musb->nIrq);
2125
2126 #ifndef __UBOOT__
2127         return 0;
2128 #else
2129         return status == 0 ? musb : NULL;
2130 #endif
2131
2132 fail5:
2133         musb_exit_debugfs(musb);
2134
2135 fail4:
2136 #ifndef __UBOOT__
2137         if (!is_otg_enabled(musb) && is_host_enabled(musb))
2138                 usb_remove_hcd(musb_to_hcd(musb));
2139         else
2140 #endif
2141                 musb_gadget_cleanup(musb);
2142
2143 fail3:
2144         pm_runtime_put_sync(musb->controller);
2145
2146 fail2:
2147         if (musb->irq_wake)
2148                 device_init_wakeup(dev, 0);
2149         musb_platform_exit(musb);
2150
2151 fail1:
2152         dev_err(musb->controller,
2153                 "musb_init_controller failed with status %d\n", status);
2154
2155         musb_free(musb);
2156
2157 fail0:
2158
2159 #ifndef __UBOOT__
2160         return status;
2161 #else
2162         return status == 0 ? musb : NULL;
2163 #endif
2164
2165 }
2166
2167 /*-------------------------------------------------------------------------*/
2168
2169 /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2170  * bridge to a platform device; this driver then suffices.
2171  */
2172
2173 #ifndef CONFIG_USB_MUSB_PIO_ONLY
2174 static u64      *orig_dma_mask;
2175 #endif
2176
2177 #ifndef __UBOOT__
2178 static int __devinit musb_probe(struct platform_device *pdev)
2179 {
2180         struct device   *dev = &pdev->dev;
2181         int             irq = platform_get_irq_byname(pdev, "mc");
2182         int             status;
2183         struct resource *iomem;
2184         void __iomem    *base;
2185
2186         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2187         if (!iomem || irq <= 0)
2188                 return -ENODEV;
2189
2190         base = ioremap(iomem->start, resource_size(iomem));
2191         if (!base) {
2192                 dev_err(dev, "ioremap failed\n");
2193                 return -ENOMEM;
2194         }
2195
2196 #ifndef CONFIG_USB_MUSB_PIO_ONLY
2197         /* clobbered by use_dma=n */
2198         orig_dma_mask = dev->dma_mask;
2199 #endif
2200         status = musb_init_controller(dev, irq, base);
2201         if (status < 0)
2202                 iounmap(base);
2203
2204         return status;
2205 }
2206
2207 static int __devexit musb_remove(struct platform_device *pdev)
2208 {
2209         struct musb     *musb = dev_to_musb(&pdev->dev);
2210         void __iomem    *ctrl_base = musb->ctrl_base;
2211
2212         /* this gets called on rmmod.
2213          *  - Host mode: host may still be active
2214          *  - Peripheral mode: peripheral is deactivated (or never-activated)
2215          *  - OTG mode: both roles are deactivated (or never-activated)
2216          */
2217         musb_exit_debugfs(musb);
2218         musb_shutdown(pdev);
2219
2220         musb_free(musb);
2221         iounmap(ctrl_base);
2222         device_init_wakeup(&pdev->dev, 0);
2223 #ifndef CONFIG_USB_MUSB_PIO_ONLY
2224         pdev->dev.dma_mask = orig_dma_mask;
2225 #endif
2226         return 0;
2227 }
2228
2229 #ifdef  CONFIG_PM
2230
2231 static void musb_save_context(struct musb *musb)
2232 {
2233         int i;
2234         void __iomem *musb_base = musb->mregs;
2235         void __iomem *epio;
2236
2237         if (is_host_enabled(musb)) {
2238                 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2239                 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2240                 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
2241         }
2242         musb->context.power = musb_readb(musb_base, MUSB_POWER);
2243         musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
2244         musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
2245         musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2246         musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2247         musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
2248
2249         for (i = 0; i < musb->config->num_eps; ++i) {
2250                 struct musb_hw_ep       *hw_ep;
2251
2252                 hw_ep = &musb->endpoints[i];
2253                 if (!hw_ep)
2254                         continue;
2255
2256                 epio = hw_ep->regs;
2257                 if (!epio)
2258                         continue;
2259
2260                 musb_writeb(musb_base, MUSB_INDEX, i);
2261                 musb->context.index_regs[i].txmaxp =
2262                         musb_readw(epio, MUSB_TXMAXP);
2263                 musb->context.index_regs[i].txcsr =
2264                         musb_readw(epio, MUSB_TXCSR);
2265                 musb->context.index_regs[i].rxmaxp =
2266                         musb_readw(epio, MUSB_RXMAXP);
2267                 musb->context.index_regs[i].rxcsr =
2268                         musb_readw(epio, MUSB_RXCSR);
2269
2270                 if (musb->dyn_fifo) {
2271                         musb->context.index_regs[i].txfifoadd =
2272                                         musb_read_txfifoadd(musb_base);
2273                         musb->context.index_regs[i].rxfifoadd =
2274                                         musb_read_rxfifoadd(musb_base);
2275                         musb->context.index_regs[i].txfifosz =
2276                                         musb_read_txfifosz(musb_base);
2277                         musb->context.index_regs[i].rxfifosz =
2278                                         musb_read_rxfifosz(musb_base);
2279                 }
2280                 if (is_host_enabled(musb)) {
2281                         musb->context.index_regs[i].txtype =
2282                                 musb_readb(epio, MUSB_TXTYPE);
2283                         musb->context.index_regs[i].txinterval =
2284                                 musb_readb(epio, MUSB_TXINTERVAL);
2285                         musb->context.index_regs[i].rxtype =
2286                                 musb_readb(epio, MUSB_RXTYPE);
2287                         musb->context.index_regs[i].rxinterval =
2288                                 musb_readb(epio, MUSB_RXINTERVAL);
2289
2290                         musb->context.index_regs[i].txfunaddr =
2291                                 musb_read_txfunaddr(musb_base, i);
2292                         musb->context.index_regs[i].txhubaddr =
2293                                 musb_read_txhubaddr(musb_base, i);
2294                         musb->context.index_regs[i].txhubport =
2295                                 musb_read_txhubport(musb_base, i);
2296
2297                         musb->context.index_regs[i].rxfunaddr =
2298                                 musb_read_rxfunaddr(musb_base, i);
2299                         musb->context.index_regs[i].rxhubaddr =
2300                                 musb_read_rxhubaddr(musb_base, i);
2301                         musb->context.index_regs[i].rxhubport =
2302                                 musb_read_rxhubport(musb_base, i);
2303                 }
2304         }
2305 }
2306
2307 static void musb_restore_context(struct musb *musb)
2308 {
2309         int i;
2310         void __iomem *musb_base = musb->mregs;
2311         void __iomem *ep_target_regs;
2312         void __iomem *epio;
2313
2314         if (is_host_enabled(musb)) {
2315                 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2316                 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2317                 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
2318         }
2319         musb_writeb(musb_base, MUSB_POWER, musb->context.power);
2320         musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
2321         musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
2322         musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2323         musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
2324
2325         for (i = 0; i < musb->config->num_eps; ++i) {
2326                 struct musb_hw_ep       *hw_ep;
2327
2328                 hw_ep = &musb->endpoints[i];
2329                 if (!hw_ep)
2330                         continue;
2331
2332                 epio = hw_ep->regs;
2333                 if (!epio)
2334                         continue;
2335
2336                 musb_writeb(musb_base, MUSB_INDEX, i);
2337                 musb_writew(epio, MUSB_TXMAXP,
2338                         musb->context.index_regs[i].txmaxp);
2339                 musb_writew(epio, MUSB_TXCSR,
2340                         musb->context.index_regs[i].txcsr);
2341                 musb_writew(epio, MUSB_RXMAXP,
2342                         musb->context.index_regs[i].rxmaxp);
2343                 musb_writew(epio, MUSB_RXCSR,
2344                         musb->context.index_regs[i].rxcsr);
2345
2346                 if (musb->dyn_fifo) {
2347                         musb_write_txfifosz(musb_base,
2348                                 musb->context.index_regs[i].txfifosz);
2349                         musb_write_rxfifosz(musb_base,
2350                                 musb->context.index_regs[i].rxfifosz);
2351                         musb_write_txfifoadd(musb_base,
2352                                 musb->context.index_regs[i].txfifoadd);
2353                         musb_write_rxfifoadd(musb_base,
2354                                 musb->context.index_regs[i].rxfifoadd);
2355                 }
2356
2357                 if (is_host_enabled(musb)) {
2358                         musb_writeb(epio, MUSB_TXTYPE,
2359                                 musb->context.index_regs[i].txtype);
2360                         musb_writeb(epio, MUSB_TXINTERVAL,
2361                                 musb->context.index_regs[i].txinterval);
2362                         musb_writeb(epio, MUSB_RXTYPE,
2363                                 musb->context.index_regs[i].rxtype);
2364                         musb_writeb(epio, MUSB_RXINTERVAL,
2365
2366                         musb->context.index_regs[i].rxinterval);
2367                         musb_write_txfunaddr(musb_base, i,
2368                                 musb->context.index_regs[i].txfunaddr);
2369                         musb_write_txhubaddr(musb_base, i,
2370                                 musb->context.index_regs[i].txhubaddr);
2371                         musb_write_txhubport(musb_base, i,
2372                                 musb->context.index_regs[i].txhubport);
2373
2374                         ep_target_regs =
2375                                 musb_read_target_reg_base(i, musb_base);
2376
2377                         musb_write_rxfunaddr(ep_target_regs,
2378                                 musb->context.index_regs[i].rxfunaddr);
2379                         musb_write_rxhubaddr(ep_target_regs,
2380                                 musb->context.index_regs[i].rxhubaddr);
2381                         musb_write_rxhubport(ep_target_regs,
2382                                 musb->context.index_regs[i].rxhubport);
2383                 }
2384         }
2385         musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
2386 }
2387
2388 static int musb_suspend(struct device *dev)
2389 {
2390         struct musb     *musb = dev_to_musb(dev);
2391         unsigned long   flags;
2392
2393         spin_lock_irqsave(&musb->lock, flags);
2394
2395         if (is_peripheral_active(musb)) {
2396                 /* FIXME force disconnect unless we know USB will wake
2397                  * the system up quickly enough to respond ...
2398                  */
2399         } else if (is_host_active(musb)) {
2400                 /* we know all the children are suspended; sometimes
2401                  * they will even be wakeup-enabled.
2402                  */
2403         }
2404
2405         spin_unlock_irqrestore(&musb->lock, flags);
2406         return 0;
2407 }
2408
2409 static int musb_resume_noirq(struct device *dev)
2410 {
2411         /* for static cmos like DaVinci, register values were preserved
2412          * unless for some reason the whole soc powered down or the USB
2413          * module got reset through the PSC (vs just being disabled).
2414          */
2415         return 0;
2416 }
2417
2418 static int musb_runtime_suspend(struct device *dev)
2419 {
2420         struct musb     *musb = dev_to_musb(dev);
2421
2422         musb_save_context(musb);
2423
2424         return 0;
2425 }
2426
2427 static int musb_runtime_resume(struct device *dev)
2428 {
2429         struct musb     *musb = dev_to_musb(dev);
2430         static int      first = 1;
2431
2432         /*
2433          * When pm_runtime_get_sync called for the first time in driver
2434          * init,  some of the structure is still not initialized which is
2435          * used in restore function. But clock needs to be
2436          * enabled before any register access, so
2437          * pm_runtime_get_sync has to be called.
2438          * Also context restore without save does not make
2439          * any sense
2440          */
2441         if (!first)
2442                 musb_restore_context(musb);
2443         first = 0;
2444
2445         return 0;
2446 }
2447
2448 static const struct dev_pm_ops musb_dev_pm_ops = {
2449         .suspend        = musb_suspend,
2450         .resume_noirq   = musb_resume_noirq,
2451         .runtime_suspend = musb_runtime_suspend,
2452         .runtime_resume = musb_runtime_resume,
2453 };
2454
2455 #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
2456 #else
2457 #define MUSB_DEV_PM_OPS NULL
2458 #endif
2459
2460 static struct platform_driver musb_driver = {
2461         .driver = {
2462                 .name           = (char *)musb_driver_name,
2463                 .bus            = &platform_bus_type,
2464                 .owner          = THIS_MODULE,
2465                 .pm             = MUSB_DEV_PM_OPS,
2466         },
2467         .probe          = musb_probe,
2468         .remove         = __devexit_p(musb_remove),
2469         .shutdown       = musb_shutdown,
2470 };
2471
2472 /*-------------------------------------------------------------------------*/
2473
2474 static int __init musb_init(void)
2475 {
2476         if (usb_disabled())
2477                 return 0;
2478
2479         pr_info("%s: version " MUSB_VERSION ", "
2480                 "?dma?"
2481                 ", "
2482                 "otg (peripheral+host)",
2483                 musb_driver_name);
2484         return platform_driver_register(&musb_driver);
2485 }
2486 module_init(musb_init);
2487
2488 static void __exit musb_cleanup(void)
2489 {
2490         platform_driver_unregister(&musb_driver);
2491 }
2492 module_exit(musb_cleanup);
2493 #endif
This page took 0.165057 seconds and 2 git commands to generate.