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