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