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