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