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