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