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