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