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