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