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