xref: /openbmc/linux/drivers/usb/musb/musb_core.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2550a7375SFelipe Balbi /*
3550a7375SFelipe Balbi  * MUSB OTG driver core code
4550a7375SFelipe Balbi  *
5550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
6550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
7550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
8550a7375SFelipe Balbi  */
9550a7375SFelipe Balbi 
10550a7375SFelipe Balbi /*
11550a7375SFelipe Balbi  * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
12550a7375SFelipe Balbi  *
13550a7375SFelipe Balbi  * This consists of a Host Controller Driver (HCD) and a peripheral
14550a7375SFelipe Balbi  * controller driver implementing the "Gadget" API; OTG support is
15550a7375SFelipe Balbi  * in the works.  These are normal Linux-USB controller drivers which
16550a7375SFelipe Balbi  * use IRQs and have no dedicated thread.
17550a7375SFelipe Balbi  *
18550a7375SFelipe Balbi  * This version of the driver has only been used with products from
19550a7375SFelipe Balbi  * Texas Instruments.  Those products integrate the Inventra logic
20550a7375SFelipe Balbi  * with other DMA, IRQ, and bus modules, as well as other logic that
21550a7375SFelipe Balbi  * needs to be reflected in this driver.
22550a7375SFelipe Balbi  *
23550a7375SFelipe Balbi  *
24550a7375SFelipe Balbi  * NOTE:  the original Mentor code here was pretty much a collection
25550a7375SFelipe Balbi  * of mechanisms that don't seem to have been fully integrated/working
26550a7375SFelipe Balbi  * for *any* Linux kernel version.  This version aims at Linux 2.6.now,
27550a7375SFelipe Balbi  * Key open issues include:
28550a7375SFelipe Balbi  *
29550a7375SFelipe Balbi  *  - Lack of host-side transaction scheduling, for all transfer types.
30550a7375SFelipe Balbi  *    The hardware doesn't do it; instead, software must.
31550a7375SFelipe Balbi  *
32550a7375SFelipe Balbi  *    This is not an issue for OTG devices that don't support external
33550a7375SFelipe Balbi  *    hubs, but for more "normal" USB hosts it's a user issue that the
34550a7375SFelipe Balbi  *    "multipoint" support doesn't scale in the expected ways.  That
35550a7375SFelipe Balbi  *    includes DaVinci EVM in a common non-OTG mode.
36550a7375SFelipe Balbi  *
37550a7375SFelipe Balbi  *      * Control and bulk use dedicated endpoints, and there's as
38550a7375SFelipe Balbi  *        yet no mechanism to either (a) reclaim the hardware when
39550a7375SFelipe Balbi  *        peripherals are NAKing, which gets complicated with bulk
40550a7375SFelipe Balbi  *        endpoints, or (b) use more than a single bulk endpoint in
41550a7375SFelipe Balbi  *        each direction.
42550a7375SFelipe Balbi  *
43550a7375SFelipe Balbi  *        RESULT:  one device may be perceived as blocking another one.
44550a7375SFelipe Balbi  *
45550a7375SFelipe Balbi  *      * Interrupt and isochronous will dynamically allocate endpoint
46550a7375SFelipe Balbi  *        hardware, but (a) there's no record keeping for bandwidth;
47550a7375SFelipe Balbi  *        (b) in the common case that few endpoints are available, there
48550a7375SFelipe Balbi  *        is no mechanism to reuse endpoints to talk to multiple devices.
49550a7375SFelipe Balbi  *
50550a7375SFelipe Balbi  *        RESULT:  At one extreme, bandwidth can be overcommitted in
51550a7375SFelipe Balbi  *        some hardware configurations, no faults will be reported.
52550a7375SFelipe Balbi  *        At the other extreme, the bandwidth capabilities which do
53550a7375SFelipe Balbi  *        exist tend to be severely undercommitted.  You can't yet hook
54550a7375SFelipe Balbi  *        up both a keyboard and a mouse to an external USB hub.
55550a7375SFelipe Balbi  */
56550a7375SFelipe Balbi 
57550a7375SFelipe Balbi /*
58550a7375SFelipe Balbi  * This gets many kinds of configuration information:
59550a7375SFelipe Balbi  *	- Kconfig for everything user-configurable
60550a7375SFelipe Balbi  *	- platform_device for addressing, irq, and platform_data
615ae477b0SRahul Bedarkar  *	- platform_data is mostly for board-specific information
62c767c1c6SDavid Brownell  *	  (plus recentrly, SOC or family details)
63550a7375SFelipe Balbi  *
64550a7375SFelipe Balbi  * Most of the conditional compilation will (someday) vanish.
65550a7375SFelipe Balbi  */
66550a7375SFelipe Balbi 
67550a7375SFelipe Balbi #include <linux/module.h>
68550a7375SFelipe Balbi #include <linux/kernel.h>
69550a7375SFelipe Balbi #include <linux/sched.h>
70550a7375SFelipe Balbi #include <linux/slab.h>
71550a7375SFelipe Balbi #include <linux/list.h>
72550a7375SFelipe Balbi #include <linux/kobject.h>
739303961fSMike Frysinger #include <linux/prefetch.h>
74550a7375SFelipe Balbi #include <linux/platform_device.h>
75550a7375SFelipe Balbi #include <linux/io.h>
7693dc2568STony Lindgren #include <linux/iopoll.h>
778d2421e6SAjay Kumar Gupta #include <linux/dma-mapping.h>
78309be239SFelipe Balbi #include <linux/usb.h>
79830fc64cSPetr Kulhavy #include <linux/usb/of.h>
80550a7375SFelipe Balbi 
81550a7375SFelipe Balbi #include "musb_core.h"
82c74173fdSBin Liu #include "musb_trace.h"
83550a7375SFelipe Balbi 
84f7f9d63eSDavid Brownell #define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
85550a7375SFelipe Balbi 
86550a7375SFelipe Balbi 
87550a7375SFelipe Balbi #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
88550a7375SFelipe Balbi #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
89550a7375SFelipe Balbi 
90e8164f64SFelipe Balbi #define MUSB_VERSION "6.0"
91550a7375SFelipe Balbi 
92550a7375SFelipe Balbi #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
93550a7375SFelipe Balbi 
9405ac10ddSFelipe Balbi #define MUSB_DRIVER_NAME "musb-hdrc"
95550a7375SFelipe Balbi const char musb_driver_name[] = MUSB_DRIVER_NAME;
96550a7375SFelipe Balbi 
97550a7375SFelipe Balbi MODULE_DESCRIPTION(DRIVER_INFO);
98550a7375SFelipe Balbi MODULE_AUTHOR(DRIVER_AUTHOR);
99550a7375SFelipe Balbi MODULE_LICENSE("GPL");
100550a7375SFelipe Balbi MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
101550a7375SFelipe Balbi 
102550a7375SFelipe Balbi 
103550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
104550a7375SFelipe Balbi 
dev_to_musb(struct device * dev)105550a7375SFelipe Balbi static inline struct musb *dev_to_musb(struct device *dev)
106550a7375SFelipe Balbi {
107550a7375SFelipe Balbi 	return dev_get_drvdata(dev);
108550a7375SFelipe Balbi }
109550a7375SFelipe Balbi 
musb_get_mode(struct device * dev)110830fc64cSPetr Kulhavy enum musb_mode musb_get_mode(struct device *dev)
111830fc64cSPetr Kulhavy {
112830fc64cSPetr Kulhavy 	enum usb_dr_mode mode;
113830fc64cSPetr Kulhavy 
114830fc64cSPetr Kulhavy 	mode = usb_get_dr_mode(dev);
115830fc64cSPetr Kulhavy 	switch (mode) {
116830fc64cSPetr Kulhavy 	case USB_DR_MODE_HOST:
117830fc64cSPetr Kulhavy 		return MUSB_HOST;
118830fc64cSPetr Kulhavy 	case USB_DR_MODE_PERIPHERAL:
119830fc64cSPetr Kulhavy 		return MUSB_PERIPHERAL;
120830fc64cSPetr Kulhavy 	case USB_DR_MODE_OTG:
121830fc64cSPetr Kulhavy 	case USB_DR_MODE_UNKNOWN:
122830fc64cSPetr Kulhavy 	default:
123830fc64cSPetr Kulhavy 		return MUSB_OTG;
124830fc64cSPetr Kulhavy 	}
125830fc64cSPetr Kulhavy }
126830fc64cSPetr Kulhavy EXPORT_SYMBOL_GPL(musb_get_mode);
127830fc64cSPetr Kulhavy 
128550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
129550a7375SFelipe Balbi 
musb_ulpi_read(struct usb_phy * phy,u32 reg)130705e63d2SUwe Kleine-König static int musb_ulpi_read(struct usb_phy *phy, u32 reg)
131ffb865b1SHeikki Krogerus {
132b96d3b08SHeikki Krogerus 	void __iomem *addr = phy->io_priv;
133ffb865b1SHeikki Krogerus 	int	i = 0;
134ffb865b1SHeikki Krogerus 	u8	r;
135ffb865b1SHeikki Krogerus 	u8	power;
136bf070bc1SGrazvydas Ignotas 	int	ret;
137bf070bc1SGrazvydas Ignotas 
138bf070bc1SGrazvydas Ignotas 	pm_runtime_get_sync(phy->io_dev);
139ffb865b1SHeikki Krogerus 
140ffb865b1SHeikki Krogerus 	/* Make sure the transceiver is not in low power mode */
141ffb865b1SHeikki Krogerus 	power = musb_readb(addr, MUSB_POWER);
142ffb865b1SHeikki Krogerus 	power &= ~MUSB_POWER_SUSPENDM;
143ffb865b1SHeikki Krogerus 	musb_writeb(addr, MUSB_POWER, power);
144ffb865b1SHeikki Krogerus 
145ffb865b1SHeikki Krogerus 	/* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
146ffb865b1SHeikki Krogerus 	 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
147ffb865b1SHeikki Krogerus 	 */
148ffb865b1SHeikki Krogerus 
149705e63d2SUwe Kleine-König 	musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
150ffb865b1SHeikki Krogerus 	musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
151ffb865b1SHeikki Krogerus 			MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
152ffb865b1SHeikki Krogerus 
153ffb865b1SHeikki Krogerus 	while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
154ffb865b1SHeikki Krogerus 				& MUSB_ULPI_REG_CMPLT)) {
155ffb865b1SHeikki Krogerus 		i++;
156bf070bc1SGrazvydas Ignotas 		if (i == 10000) {
157bf070bc1SGrazvydas Ignotas 			ret = -ETIMEDOUT;
158bf070bc1SGrazvydas Ignotas 			goto out;
159bf070bc1SGrazvydas Ignotas 		}
160ffb865b1SHeikki Krogerus 
161ffb865b1SHeikki Krogerus 	}
162ffb865b1SHeikki Krogerus 	r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
163ffb865b1SHeikki Krogerus 	r &= ~MUSB_ULPI_REG_CMPLT;
164ffb865b1SHeikki Krogerus 	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
165ffb865b1SHeikki Krogerus 
166bf070bc1SGrazvydas Ignotas 	ret = musb_readb(addr, MUSB_ULPI_REG_DATA);
167bf070bc1SGrazvydas Ignotas 
168bf070bc1SGrazvydas Ignotas out:
169bf070bc1SGrazvydas Ignotas 	pm_runtime_put(phy->io_dev);
170bf070bc1SGrazvydas Ignotas 
171bf070bc1SGrazvydas Ignotas 	return ret;
172ffb865b1SHeikki Krogerus }
173ffb865b1SHeikki Krogerus 
musb_ulpi_write(struct usb_phy * phy,u32 val,u32 reg)174705e63d2SUwe Kleine-König static int musb_ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
175ffb865b1SHeikki Krogerus {
176b96d3b08SHeikki Krogerus 	void __iomem *addr = phy->io_priv;
177ffb865b1SHeikki Krogerus 	int	i = 0;
178ffb865b1SHeikki Krogerus 	u8	r = 0;
179ffb865b1SHeikki Krogerus 	u8	power;
180bf070bc1SGrazvydas Ignotas 	int	ret = 0;
181bf070bc1SGrazvydas Ignotas 
182bf070bc1SGrazvydas Ignotas 	pm_runtime_get_sync(phy->io_dev);
183ffb865b1SHeikki Krogerus 
184ffb865b1SHeikki Krogerus 	/* Make sure the transceiver is not in low power mode */
185ffb865b1SHeikki Krogerus 	power = musb_readb(addr, MUSB_POWER);
186ffb865b1SHeikki Krogerus 	power &= ~MUSB_POWER_SUSPENDM;
187ffb865b1SHeikki Krogerus 	musb_writeb(addr, MUSB_POWER, power);
188ffb865b1SHeikki Krogerus 
189705e63d2SUwe Kleine-König 	musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)reg);
190705e63d2SUwe Kleine-König 	musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)val);
191ffb865b1SHeikki Krogerus 	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
192ffb865b1SHeikki Krogerus 
193ffb865b1SHeikki Krogerus 	while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
194ffb865b1SHeikki Krogerus 				& MUSB_ULPI_REG_CMPLT)) {
195ffb865b1SHeikki Krogerus 		i++;
196bf070bc1SGrazvydas Ignotas 		if (i == 10000) {
197bf070bc1SGrazvydas Ignotas 			ret = -ETIMEDOUT;
198bf070bc1SGrazvydas Ignotas 			goto out;
199bf070bc1SGrazvydas Ignotas 		}
200ffb865b1SHeikki Krogerus 	}
201ffb865b1SHeikki Krogerus 
202ffb865b1SHeikki Krogerus 	r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
203ffb865b1SHeikki Krogerus 	r &= ~MUSB_ULPI_REG_CMPLT;
204ffb865b1SHeikki Krogerus 	musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
205ffb865b1SHeikki Krogerus 
206bf070bc1SGrazvydas Ignotas out:
207bf070bc1SGrazvydas Ignotas 	pm_runtime_put(phy->io_dev);
208bf070bc1SGrazvydas Ignotas 
209bf070bc1SGrazvydas Ignotas 	return ret;
210ffb865b1SHeikki Krogerus }
211ffb865b1SHeikki Krogerus 
212b96d3b08SHeikki Krogerus static struct usb_phy_io_ops musb_ulpi_access = {
213ffb865b1SHeikki Krogerus 	.read = musb_ulpi_read,
214ffb865b1SHeikki Krogerus 	.write = musb_ulpi_write,
215ffb865b1SHeikki Krogerus };
216ffb865b1SHeikki Krogerus 
217ffb865b1SHeikki Krogerus /*-------------------------------------------------------------------------*/
218ffb865b1SHeikki Krogerus 
musb_default_fifo_offset(u8 epnum)2191b40fc57STony Lindgren static u32 musb_default_fifo_offset(u8 epnum)
2201b40fc57STony Lindgren {
2211b40fc57STony Lindgren 	return 0x20 + (epnum * 4);
2221b40fc57STony Lindgren }
2231b40fc57STony Lindgren 
224d026e9c7STony Lindgren /* "flat" mapping: each endpoint has its own i/o address */
musb_flat_ep_select(void __iomem * mbase,u8 epnum)225d026e9c7STony Lindgren static void musb_flat_ep_select(void __iomem *mbase, u8 epnum)
226d026e9c7STony Lindgren {
227d026e9c7STony Lindgren }
228d026e9c7STony Lindgren 
musb_flat_ep_offset(u8 epnum,u16 offset)229d026e9c7STony Lindgren static u32 musb_flat_ep_offset(u8 epnum, u16 offset)
230d026e9c7STony Lindgren {
231d026e9c7STony Lindgren 	return 0x100 + (0x10 * epnum) + offset;
232d026e9c7STony Lindgren }
233d026e9c7STony Lindgren 
234d026e9c7STony Lindgren /* "indexed" mapping: INDEX register controls register bank select */
musb_indexed_ep_select(void __iomem * mbase,u8 epnum)235d026e9c7STony Lindgren static void musb_indexed_ep_select(void __iomem *mbase, u8 epnum)
236d026e9c7STony Lindgren {
237d026e9c7STony Lindgren 	musb_writeb(mbase, MUSB_INDEX, epnum);
238d026e9c7STony Lindgren }
239d026e9c7STony Lindgren 
musb_indexed_ep_offset(u8 epnum,u16 offset)240d026e9c7STony Lindgren static u32 musb_indexed_ep_offset(u8 epnum, u16 offset)
241d026e9c7STony Lindgren {
242d026e9c7STony Lindgren 	return 0x10 + offset;
243d026e9c7STony Lindgren }
244d026e9c7STony Lindgren 
musb_default_busctl_offset(u8 epnum,u16 offset)2456cc2af6dSHans de Goede static u32 musb_default_busctl_offset(u8 epnum, u16 offset)
2466cc2af6dSHans de Goede {
2476cc2af6dSHans de Goede 	return 0x80 + (0x08 * epnum) + offset;
2486cc2af6dSHans de Goede }
2496cc2af6dSHans de Goede 
musb_default_readb(void __iomem * addr,u32 offset)2509c93d7fdSMin Guo static u8 musb_default_readb(void __iomem *addr, u32 offset)
2511b40fc57STony Lindgren {
252c74173fdSBin Liu 	u8 data =  __raw_readb(addr + offset);
253c74173fdSBin Liu 
254c74173fdSBin Liu 	trace_musb_readb(__builtin_return_address(0), addr, offset, data);
255c74173fdSBin Liu 	return data;
2561b40fc57STony Lindgren }
2571b40fc57STony Lindgren 
musb_default_writeb(void __iomem * addr,u32 offset,u8 data)2589c93d7fdSMin Guo static void musb_default_writeb(void __iomem *addr, u32 offset, u8 data)
2591b40fc57STony Lindgren {
260c74173fdSBin Liu 	trace_musb_writeb(__builtin_return_address(0), addr, offset, data);
2611b40fc57STony Lindgren 	__raw_writeb(data, addr + offset);
2621b40fc57STony Lindgren }
2631b40fc57STony Lindgren 
musb_default_readw(void __iomem * addr,u32 offset)2649c93d7fdSMin Guo static u16 musb_default_readw(void __iomem *addr, u32 offset)
2651b40fc57STony Lindgren {
266c74173fdSBin Liu 	u16 data = __raw_readw(addr + offset);
267c74173fdSBin Liu 
268c74173fdSBin Liu 	trace_musb_readw(__builtin_return_address(0), addr, offset, data);
269c74173fdSBin Liu 	return data;
2701b40fc57STony Lindgren }
2711b40fc57STony Lindgren 
musb_default_writew(void __iomem * addr,u32 offset,u16 data)2729c93d7fdSMin Guo static void musb_default_writew(void __iomem *addr, u32 offset, u16 data)
2731b40fc57STony Lindgren {
274c74173fdSBin Liu 	trace_musb_writew(__builtin_return_address(0), addr, offset, data);
2751b40fc57STony Lindgren 	__raw_writew(data, addr + offset);
2761b40fc57STony Lindgren }
2771b40fc57STony Lindgren 
musb_default_get_toggle(struct musb_qh * qh,int is_out)278fe3bbd6bSMin Guo static u16 musb_default_get_toggle(struct musb_qh *qh, int is_out)
279fe3bbd6bSMin Guo {
280fe3bbd6bSMin Guo 	void __iomem *epio = qh->hw_ep->regs;
281fe3bbd6bSMin Guo 	u16 csr;
282fe3bbd6bSMin Guo 
283fe3bbd6bSMin Guo 	if (is_out)
284fe3bbd6bSMin Guo 		csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
285fe3bbd6bSMin Guo 	else
286fe3bbd6bSMin Guo 		csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
287fe3bbd6bSMin Guo 
288fe3bbd6bSMin Guo 	return csr;
289fe3bbd6bSMin Guo }
290fe3bbd6bSMin Guo 
musb_default_set_toggle(struct musb_qh * qh,int is_out,struct urb * urb)291fe3bbd6bSMin Guo static u16 musb_default_set_toggle(struct musb_qh *qh, int is_out,
292fe3bbd6bSMin Guo 				   struct urb *urb)
293fe3bbd6bSMin Guo {
294fe3bbd6bSMin Guo 	u16 csr;
295fe3bbd6bSMin Guo 	u16 toggle;
296fe3bbd6bSMin Guo 
297fe3bbd6bSMin Guo 	toggle = usb_gettoggle(urb->dev, qh->epnum, is_out);
298fe3bbd6bSMin Guo 
299fe3bbd6bSMin Guo 	if (is_out)
300fe3bbd6bSMin Guo 		csr = toggle ? (MUSB_TXCSR_H_WR_DATATOGGLE
301fe3bbd6bSMin Guo 				| MUSB_TXCSR_H_DATATOGGLE)
302fe3bbd6bSMin Guo 				: MUSB_TXCSR_CLRDATATOG;
303fe3bbd6bSMin Guo 	else
304fe3bbd6bSMin Guo 		csr = toggle ? (MUSB_RXCSR_H_WR_DATATOGGLE
305fe3bbd6bSMin Guo 				| MUSB_RXCSR_H_DATATOGGLE) : 0;
306fe3bbd6bSMin Guo 
307fe3bbd6bSMin Guo 	return csr;
308fe3bbd6bSMin Guo }
309fe3bbd6bSMin Guo 
310550a7375SFelipe Balbi /*
311550a7375SFelipe Balbi  * Load an endpoint's FIFO
312550a7375SFelipe Balbi  */
musb_default_write_fifo(struct musb_hw_ep * hw_ep,u16 len,const u8 * src)3131b40fc57STony Lindgren static void musb_default_write_fifo(struct musb_hw_ep *hw_ep, u16 len,
3141b40fc57STony Lindgren 				    const u8 *src)
315550a7375SFelipe Balbi {
3165c8a86e1SFelipe Balbi 	struct musb *musb = hw_ep->musb;
317550a7375SFelipe Balbi 	void __iomem *fifo = hw_ep->fifo;
318550a7375SFelipe Balbi 
319603fe2b2SAjay Kumar Gupta 	if (unlikely(len == 0))
320603fe2b2SAjay Kumar Gupta 		return;
321603fe2b2SAjay Kumar Gupta 
322550a7375SFelipe Balbi 	prefetch((u8 *)src);
323550a7375SFelipe Balbi 
3245c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
325550a7375SFelipe Balbi 			'T', hw_ep->epnum, fifo, len, src);
326550a7375SFelipe Balbi 
327550a7375SFelipe Balbi 	/* we can't assume unaligned reads work */
328550a7375SFelipe Balbi 	if (likely((0x01 & (unsigned long) src) == 0)) {
329550a7375SFelipe Balbi 		u16	index = 0;
330550a7375SFelipe Balbi 
331550a7375SFelipe Balbi 		/* best case is 32bit-aligned source address */
332550a7375SFelipe Balbi 		if ((0x02 & (unsigned long) src) == 0) {
333550a7375SFelipe Balbi 			if (len >= 4) {
3342bf0a8f6SMatthew Leach 				iowrite32_rep(fifo, src + index, len >> 2);
335550a7375SFelipe Balbi 				index += len & ~0x03;
336550a7375SFelipe Balbi 			}
337550a7375SFelipe Balbi 			if (len & 0x02) {
338be780381SHans de Goede 				__raw_writew(*(u16 *)&src[index], fifo);
339550a7375SFelipe Balbi 				index += 2;
340550a7375SFelipe Balbi 			}
341550a7375SFelipe Balbi 		} else {
342550a7375SFelipe Balbi 			if (len >= 2) {
3432bf0a8f6SMatthew Leach 				iowrite16_rep(fifo, src + index, len >> 1);
344550a7375SFelipe Balbi 				index += len & ~0x01;
345550a7375SFelipe Balbi 			}
346550a7375SFelipe Balbi 		}
347550a7375SFelipe Balbi 		if (len & 0x01)
348be780381SHans de Goede 			__raw_writeb(src[index], fifo);
349550a7375SFelipe Balbi 	} else  {
350550a7375SFelipe Balbi 		/* byte aligned */
3512bf0a8f6SMatthew Leach 		iowrite8_rep(fifo, src, len);
352550a7375SFelipe Balbi 	}
353550a7375SFelipe Balbi }
354550a7375SFelipe Balbi 
355550a7375SFelipe Balbi /*
356550a7375SFelipe Balbi  * Unload an endpoint's FIFO
357550a7375SFelipe Balbi  */
musb_default_read_fifo(struct musb_hw_ep * hw_ep,u16 len,u8 * dst)3581b40fc57STony Lindgren static void musb_default_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
359550a7375SFelipe Balbi {
3605c8a86e1SFelipe Balbi 	struct musb *musb = hw_ep->musb;
361550a7375SFelipe Balbi 	void __iomem *fifo = hw_ep->fifo;
362550a7375SFelipe Balbi 
363603fe2b2SAjay Kumar Gupta 	if (unlikely(len == 0))
364603fe2b2SAjay Kumar Gupta 		return;
365603fe2b2SAjay Kumar Gupta 
3665c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
367550a7375SFelipe Balbi 			'R', hw_ep->epnum, fifo, len, dst);
368550a7375SFelipe Balbi 
369550a7375SFelipe Balbi 	/* we can't assume unaligned writes work */
370550a7375SFelipe Balbi 	if (likely((0x01 & (unsigned long) dst) == 0)) {
371550a7375SFelipe Balbi 		u16	index = 0;
372550a7375SFelipe Balbi 
373550a7375SFelipe Balbi 		/* best case is 32bit-aligned destination address */
374550a7375SFelipe Balbi 		if ((0x02 & (unsigned long) dst) == 0) {
375550a7375SFelipe Balbi 			if (len >= 4) {
3762bf0a8f6SMatthew Leach 				ioread32_rep(fifo, dst, len >> 2);
377550a7375SFelipe Balbi 				index = len & ~0x03;
378550a7375SFelipe Balbi 			}
379550a7375SFelipe Balbi 			if (len & 0x02) {
380be780381SHans de Goede 				*(u16 *)&dst[index] = __raw_readw(fifo);
381550a7375SFelipe Balbi 				index += 2;
382550a7375SFelipe Balbi 			}
383550a7375SFelipe Balbi 		} else {
384550a7375SFelipe Balbi 			if (len >= 2) {
3852bf0a8f6SMatthew Leach 				ioread16_rep(fifo, dst, len >> 1);
386550a7375SFelipe Balbi 				index = len & ~0x01;
387550a7375SFelipe Balbi 			}
388550a7375SFelipe Balbi 		}
389550a7375SFelipe Balbi 		if (len & 0x01)
390be780381SHans de Goede 			dst[index] = __raw_readb(fifo);
391550a7375SFelipe Balbi 	} else  {
392550a7375SFelipe Balbi 		/* byte aligned */
3932bf0a8f6SMatthew Leach 		ioread8_rep(fifo, dst, len);
394550a7375SFelipe Balbi 	}
395550a7375SFelipe Balbi }
396550a7375SFelipe Balbi 
3971b40fc57STony Lindgren /*
3981b40fc57STony Lindgren  * Old style IO functions
3991b40fc57STony Lindgren  */
4009c93d7fdSMin Guo u8 (*musb_readb)(void __iomem *addr, u32 offset);
4011b40fc57STony Lindgren EXPORT_SYMBOL_GPL(musb_readb);
402550a7375SFelipe Balbi 
4039c93d7fdSMin Guo void (*musb_writeb)(void __iomem *addr, u32 offset, u8 data);
4041b40fc57STony Lindgren EXPORT_SYMBOL_GPL(musb_writeb);
4051b40fc57STony Lindgren 
4069c93d7fdSMin Guo u8 (*musb_clearb)(void __iomem *addr, u32 offset);
4079c93d7fdSMin Guo EXPORT_SYMBOL_GPL(musb_clearb);
4089c93d7fdSMin Guo 
4099c93d7fdSMin Guo u16 (*musb_readw)(void __iomem *addr, u32 offset);
4101b40fc57STony Lindgren EXPORT_SYMBOL_GPL(musb_readw);
4111b40fc57STony Lindgren 
4129c93d7fdSMin Guo void (*musb_writew)(void __iomem *addr, u32 offset, u16 data);
4131b40fc57STony Lindgren EXPORT_SYMBOL_GPL(musb_writew);
4141b40fc57STony Lindgren 
4159c93d7fdSMin Guo u16 (*musb_clearw)(void __iomem *addr, u32 offset);
4169c93d7fdSMin Guo EXPORT_SYMBOL_GPL(musb_clearw);
4179c93d7fdSMin Guo 
musb_readl(void __iomem * addr,u32 offset)4189c93d7fdSMin Guo u32 musb_readl(void __iomem *addr, u32 offset)
41942e990eaSBin Liu {
42042e990eaSBin Liu 	u32 data = __raw_readl(addr + offset);
42142e990eaSBin Liu 
42242e990eaSBin Liu 	trace_musb_readl(__builtin_return_address(0), addr, offset, data);
42342e990eaSBin Liu 	return data;
42442e990eaSBin Liu }
4251b40fc57STony Lindgren EXPORT_SYMBOL_GPL(musb_readl);
4261b40fc57STony Lindgren 
musb_writel(void __iomem * addr,u32 offset,u32 data)4279c93d7fdSMin Guo void musb_writel(void __iomem *addr, u32 offset, u32 data)
42842e990eaSBin Liu {
42942e990eaSBin Liu 	trace_musb_writel(__builtin_return_address(0), addr, offset, data);
43042e990eaSBin Liu 	__raw_writel(data, addr + offset);
43142e990eaSBin Liu }
4321b40fc57STony Lindgren EXPORT_SYMBOL_GPL(musb_writel);
4331b40fc57STony Lindgren 
4347f6283edSTony Lindgren #ifndef CONFIG_MUSB_PIO_ONLY
4357f6283edSTony Lindgren struct dma_controller *
4367f6283edSTony Lindgren (*musb_dma_controller_create)(struct musb *musb, void __iomem *base);
4377f6283edSTony Lindgren EXPORT_SYMBOL(musb_dma_controller_create);
4387f6283edSTony Lindgren 
4397f6283edSTony Lindgren void (*musb_dma_controller_destroy)(struct dma_controller *c);
4407f6283edSTony Lindgren EXPORT_SYMBOL(musb_dma_controller_destroy);
4417f6283edSTony Lindgren #endif
4427f6283edSTony Lindgren 
4431b40fc57STony Lindgren /*
4441b40fc57STony Lindgren  * New style IO functions
4451b40fc57STony Lindgren  */
musb_read_fifo(struct musb_hw_ep * hw_ep,u16 len,u8 * dst)4461b40fc57STony Lindgren void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
4471b40fc57STony Lindgren {
4481b40fc57STony Lindgren 	return hw_ep->musb->io.read_fifo(hw_ep, len, dst);
4491b40fc57STony Lindgren }
4501b40fc57STony Lindgren 
musb_write_fifo(struct musb_hw_ep * hw_ep,u16 len,const u8 * src)4511b40fc57STony Lindgren void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
4521b40fc57STony Lindgren {
4531b40fc57STony Lindgren 	return hw_ep->musb->io.write_fifo(hw_ep, len, src);
4541b40fc57STony Lindgren }
455550a7375SFelipe Balbi 
musb_read_devctl(struct musb * musb)45693dc2568STony Lindgren static u8 musb_read_devctl(struct musb *musb)
45793dc2568STony Lindgren {
45893dc2568STony Lindgren 	return musb_readb(musb->mregs, MUSB_DEVCTL);
45993dc2568STony Lindgren }
46093dc2568STony Lindgren 
46193dc2568STony Lindgren /**
46293dc2568STony Lindgren  * musb_set_host - set and initialize host mode
46393dc2568STony Lindgren  * @musb: musb controller driver data
46493dc2568STony Lindgren  *
46593dc2568STony Lindgren  * At least some musb revisions need to enable devctl session bit in
46693dc2568STony Lindgren  * peripheral mode to switch to host mode. Initializes things to host
46793dc2568STony Lindgren  * mode and sets A_IDLE. SoC glue needs to advance state further
46893dc2568STony Lindgren  * based on phy provided VBUS state.
46993dc2568STony Lindgren  *
47093dc2568STony Lindgren  * Note that the SoC glue code may need to wait for musb to settle
47193dc2568STony Lindgren  * on enable before calling this to avoid babble.
47293dc2568STony Lindgren  */
musb_set_host(struct musb * musb)47393dc2568STony Lindgren int musb_set_host(struct musb *musb)
47493dc2568STony Lindgren {
47593dc2568STony Lindgren 	int error = 0;
47693dc2568STony Lindgren 	u8 devctl;
47793dc2568STony Lindgren 
47893dc2568STony Lindgren 	if (!musb)
47993dc2568STony Lindgren 		return -EINVAL;
48093dc2568STony Lindgren 
48193dc2568STony Lindgren 	devctl = musb_read_devctl(musb);
48293dc2568STony Lindgren 	if (!(devctl & MUSB_DEVCTL_BDEVICE)) {
483318324e6STony Lindgren 		trace_musb_state(musb, devctl, "Already in host mode");
48493dc2568STony Lindgren 		goto init_data;
48593dc2568STony Lindgren 	}
48693dc2568STony Lindgren 
48793dc2568STony Lindgren 	devctl |= MUSB_DEVCTL_SESSION;
48893dc2568STony Lindgren 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
48993dc2568STony Lindgren 
49093dc2568STony Lindgren 	error = readx_poll_timeout(musb_read_devctl, musb, devctl,
49193dc2568STony Lindgren 				   !(devctl & MUSB_DEVCTL_BDEVICE), 5000,
49293dc2568STony Lindgren 				   1000000);
49393dc2568STony Lindgren 	if (error) {
49493dc2568STony Lindgren 		dev_err(musb->controller, "%s: could not set host: %02x\n",
49593dc2568STony Lindgren 			__func__, devctl);
49693dc2568STony Lindgren 
49793dc2568STony Lindgren 		return error;
49893dc2568STony Lindgren 	}
49993dc2568STony Lindgren 
500318324e6STony Lindgren 	devctl = musb_read_devctl(musb);
501318324e6STony Lindgren 	trace_musb_state(musb, devctl, "Host mode set");
502318324e6STony Lindgren 
50393dc2568STony Lindgren init_data:
50493dc2568STony Lindgren 	musb->is_active = 1;
50521acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_A_IDLE);
50693dc2568STony Lindgren 	MUSB_HST_MODE(musb);
50793dc2568STony Lindgren 
50893dc2568STony Lindgren 	return error;
50993dc2568STony Lindgren }
51093dc2568STony Lindgren EXPORT_SYMBOL_GPL(musb_set_host);
51193dc2568STony Lindgren 
51293dc2568STony Lindgren /**
51393dc2568STony Lindgren  * musb_set_peripheral - set and initialize peripheral mode
51493dc2568STony Lindgren  * @musb: musb controller driver data
51593dc2568STony Lindgren  *
51693dc2568STony Lindgren  * Clears devctl session bit and initializes things for peripheral
51793dc2568STony Lindgren  * mode and sets B_IDLE. SoC glue needs to advance state further
51893dc2568STony Lindgren  * based on phy provided VBUS state.
51993dc2568STony Lindgren  */
musb_set_peripheral(struct musb * musb)52093dc2568STony Lindgren int musb_set_peripheral(struct musb *musb)
52193dc2568STony Lindgren {
52293dc2568STony Lindgren 	int error = 0;
52393dc2568STony Lindgren 	u8 devctl;
52493dc2568STony Lindgren 
52593dc2568STony Lindgren 	if (!musb)
52693dc2568STony Lindgren 		return -EINVAL;
52793dc2568STony Lindgren 
52893dc2568STony Lindgren 	devctl = musb_read_devctl(musb);
52993dc2568STony Lindgren 	if (devctl & MUSB_DEVCTL_BDEVICE) {
530318324e6STony Lindgren 		trace_musb_state(musb, devctl, "Already in peripheral mode");
53193dc2568STony Lindgren 		goto init_data;
53293dc2568STony Lindgren 	}
53393dc2568STony Lindgren 
53493dc2568STony Lindgren 	devctl &= ~MUSB_DEVCTL_SESSION;
53593dc2568STony Lindgren 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
53693dc2568STony Lindgren 
53793dc2568STony Lindgren 	error = readx_poll_timeout(musb_read_devctl, musb, devctl,
53893dc2568STony Lindgren 				   devctl & MUSB_DEVCTL_BDEVICE, 5000,
53993dc2568STony Lindgren 				   1000000);
54093dc2568STony Lindgren 	if (error) {
5411e31d3caSColin Ian King 		dev_err(musb->controller, "%s: could not set peripheral: %02x\n",
54293dc2568STony Lindgren 			__func__, devctl);
54393dc2568STony Lindgren 
54493dc2568STony Lindgren 		return error;
54593dc2568STony Lindgren 	}
54693dc2568STony Lindgren 
547318324e6STony Lindgren 	devctl = musb_read_devctl(musb);
548318324e6STony Lindgren 	trace_musb_state(musb, devctl, "Peripheral mode set");
549318324e6STony Lindgren 
55093dc2568STony Lindgren init_data:
55193dc2568STony Lindgren 	musb->is_active = 0;
55221acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_B_IDLE);
55393dc2568STony Lindgren 	MUSB_DEV_MODE(musb);
55493dc2568STony Lindgren 
55593dc2568STony Lindgren 	return error;
55693dc2568STony Lindgren }
55793dc2568STony Lindgren EXPORT_SYMBOL_GPL(musb_set_peripheral);
55893dc2568STony Lindgren 
559550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
560550a7375SFelipe Balbi 
561550a7375SFelipe Balbi /* for high speed test mode; see USB 2.0 spec 7.1.20 */
562550a7375SFelipe Balbi static const u8 musb_test_packet[53] = {
563550a7375SFelipe Balbi 	/* implicit SYNC then DATA0 to start */
564550a7375SFelipe Balbi 
565550a7375SFelipe Balbi 	/* JKJKJKJK x9 */
566550a7375SFelipe Balbi 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
567550a7375SFelipe Balbi 	/* JJKKJJKK x8 */
568550a7375SFelipe Balbi 	0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
569550a7375SFelipe Balbi 	/* JJJJKKKK x8 */
570550a7375SFelipe Balbi 	0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
571550a7375SFelipe Balbi 	/* JJJJJJJKKKKKKK x8 */
572550a7375SFelipe Balbi 	0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
573550a7375SFelipe Balbi 	/* JJJJJJJK x8 */
574550a7375SFelipe Balbi 	0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
575550a7375SFelipe Balbi 	/* JKKKKKKK x10, JK */
576550a7375SFelipe Balbi 	0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
577550a7375SFelipe Balbi 
578550a7375SFelipe Balbi 	/* implicit CRC16 then EOP to end */
579550a7375SFelipe Balbi };
580550a7375SFelipe Balbi 
musb_load_testpacket(struct musb * musb)581550a7375SFelipe Balbi void musb_load_testpacket(struct musb *musb)
582550a7375SFelipe Balbi {
583550a7375SFelipe Balbi 	void __iomem	*regs = musb->endpoints[0].regs;
584550a7375SFelipe Balbi 
585550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, 0);
586550a7375SFelipe Balbi 	musb_write_fifo(musb->control_ep,
587550a7375SFelipe Balbi 			sizeof(musb_test_packet), musb_test_packet);
588550a7375SFelipe Balbi 	musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
589550a7375SFelipe Balbi }
590550a7375SFelipe Balbi 
591550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
592550a7375SFelipe Balbi 
593550a7375SFelipe Balbi /*
594550a7375SFelipe Balbi  * Handles OTG hnp timeouts, such as b_ase0_brst
595550a7375SFelipe Balbi  */
musb_otg_timer_func(struct timer_list * t)59605678497SKees Cook static void musb_otg_timer_func(struct timer_list *t)
597550a7375SFelipe Balbi {
59805678497SKees Cook 	struct musb	*musb = from_timer(musb, t, otg_timer);
599550a7375SFelipe Balbi 	unsigned long	flags;
600550a7375SFelipe Balbi 
601550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
60221acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
603550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
604b99d3659SBin Liu 		musb_dbg(musb,
605b99d3659SBin Liu 			"HNP: b_wait_acon timeout; back to b_peripheral");
606550a7375SFelipe Balbi 		musb_g_disconnect(musb);
60721acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
608550a7375SFelipe Balbi 		musb->is_active = 0;
609550a7375SFelipe Balbi 		break;
610ab983f2aSDavid Brownell 	case OTG_STATE_A_SUSPEND:
611550a7375SFelipe Balbi 	case OTG_STATE_A_WAIT_BCON:
612b99d3659SBin Liu 		musb_dbg(musb, "HNP: %s timeout",
613285f28bfSPaul Cercueil 			 musb_otg_state_string(musb));
614743411b3SFelipe Balbi 		musb_platform_set_vbus(musb, 0);
61521acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_WAIT_VFALL);
616550a7375SFelipe Balbi 		break;
617550a7375SFelipe Balbi 	default:
618b99d3659SBin Liu 		musb_dbg(musb, "HNP: Unhandled mode %s",
619285f28bfSPaul Cercueil 			 musb_otg_state_string(musb));
620550a7375SFelipe Balbi 	}
621550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
622550a7375SFelipe Balbi }
623550a7375SFelipe Balbi 
624550a7375SFelipe Balbi /*
625f7f9d63eSDavid Brownell  * Stops the HNP transition. Caller must take care of locking.
626550a7375SFelipe Balbi  */
musb_hnp_stop(struct musb * musb)627550a7375SFelipe Balbi void musb_hnp_stop(struct musb *musb)
628550a7375SFelipe Balbi {
6298b125df5SDaniel Mack 	struct usb_hcd	*hcd = musb->hcd;
630550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
631550a7375SFelipe Balbi 	u8	reg;
632550a7375SFelipe Balbi 
633285f28bfSPaul Cercueil 	musb_dbg(musb, "HNP: stop from %s", musb_otg_state_string(musb));
634ab983f2aSDavid Brownell 
63521acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
636550a7375SFelipe Balbi 	case OTG_STATE_A_PERIPHERAL:
637550a7375SFelipe Balbi 		musb_g_disconnect(musb);
638285f28bfSPaul Cercueil 		musb_dbg(musb, "HNP: back to %s", musb_otg_state_string(musb));
639550a7375SFelipe Balbi 		break;
640550a7375SFelipe Balbi 	case OTG_STATE_B_HOST:
641b99d3659SBin Liu 		musb_dbg(musb, "HNP: Disabling HR");
64274c2e936SDaniel Mack 		if (hcd)
643550a7375SFelipe Balbi 			hcd->self.is_b_host = 0;
64421acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
645550a7375SFelipe Balbi 		MUSB_DEV_MODE(musb);
646550a7375SFelipe Balbi 		reg = musb_readb(mbase, MUSB_POWER);
647550a7375SFelipe Balbi 		reg |= MUSB_POWER_SUSPENDM;
648550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_POWER, reg);
649550a7375SFelipe Balbi 		/* REVISIT: Start SESSION_REQUEST here? */
650550a7375SFelipe Balbi 		break;
651550a7375SFelipe Balbi 	default:
652b99d3659SBin Liu 		musb_dbg(musb, "HNP: Stopping in unknown state %s",
653285f28bfSPaul Cercueil 			 musb_otg_state_string(musb));
654550a7375SFelipe Balbi 	}
655550a7375SFelipe Balbi 
656550a7375SFelipe Balbi 	/*
657550a7375SFelipe Balbi 	 * When returning to A state after HNP, avoid hub_port_rebounce(),
658550a7375SFelipe Balbi 	 * which cause occasional OPT A "Did not receive reset after connect"
659550a7375SFelipe Balbi 	 * errors.
660550a7375SFelipe Balbi 	 */
661749da5f8SAlan Stern 	musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
662550a7375SFelipe Balbi }
663550a7375SFelipe Balbi 
66483b8f5b8SFelipe Balbi static void musb_recover_from_babble(struct musb *musb);
665e1eb3eb8SFelipe Balbi 
musb_handle_intr_resume(struct musb * musb,u8 devctl)666bcb8fd3aSBin Liu static void musb_handle_intr_resume(struct musb *musb, u8 devctl)
667550a7375SFelipe Balbi {
668285f28bfSPaul Cercueil 	musb_dbg(musb, "RESUME (%s)", musb_otg_state_string(musb));
669550a7375SFelipe Balbi 
670550a7375SFelipe Balbi 	if (devctl & MUSB_DEVCTL_HM) {
67121acc656SPaul Cercueil 		switch (musb_get_state(musb)) {
672550a7375SFelipe Balbi 		case OTG_STATE_A_SUSPEND:
67365322797SJohan Hovold 			/* remote wakeup? */
674550a7375SFelipe Balbi 			musb->port1_status |=
675550a7375SFelipe Balbi 					(USB_PORT_STAT_C_SUSPEND << 16)
676550a7375SFelipe Balbi 					| MUSB_PORT_STAT_RESUME;
67730d361bfSDaniel Mack 			musb->rh_timer = jiffies
678309be239SFelipe Balbi 				+ msecs_to_jiffies(USB_RESUME_TIMEOUT);
67921acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_A_HOST);
680550a7375SFelipe Balbi 			musb->is_active = 1;
6819298b4aaSBin Liu 			musb_host_resume_root_hub(musb);
682407788b5STony Lindgren 			schedule_delayed_work(&musb->finish_resume_work,
683407788b5STony Lindgren 				msecs_to_jiffies(USB_RESUME_TIMEOUT));
684550a7375SFelipe Balbi 			break;
685550a7375SFelipe Balbi 		case OTG_STATE_B_WAIT_ACON:
68621acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
687550a7375SFelipe Balbi 			musb->is_active = 1;
688550a7375SFelipe Balbi 			MUSB_DEV_MODE(musb);
689550a7375SFelipe Balbi 			break;
690550a7375SFelipe Balbi 		default:
691550a7375SFelipe Balbi 			WARNING("bogus %s RESUME (%s)\n",
692550a7375SFelipe Balbi 				"host",
693285f28bfSPaul Cercueil 				musb_otg_state_string(musb));
694550a7375SFelipe Balbi 		}
695550a7375SFelipe Balbi 	} else {
69621acc656SPaul Cercueil 		switch (musb_get_state(musb)) {
697550a7375SFelipe Balbi 		case OTG_STATE_A_SUSPEND:
698550a7375SFelipe Balbi 			/* possibly DISCONNECT is upcoming */
69921acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_A_HOST);
7000b3eba44SDaniel Mack 			musb_host_resume_root_hub(musb);
701550a7375SFelipe Balbi 			break;
702550a7375SFelipe Balbi 		case OTG_STATE_B_WAIT_ACON:
703550a7375SFelipe Balbi 		case OTG_STATE_B_PERIPHERAL:
704550a7375SFelipe Balbi 			/* disconnect while suspended?  we may
705550a7375SFelipe Balbi 			 * not get a disconnect irq...
706550a7375SFelipe Balbi 			 */
707550a7375SFelipe Balbi 			if ((devctl & MUSB_DEVCTL_VBUS)
708550a7375SFelipe Balbi 					!= (3 << MUSB_DEVCTL_VBUS_SHIFT)
709550a7375SFelipe Balbi 					) {
710550a7375SFelipe Balbi 				musb->int_usb |= MUSB_INTR_DISCONNECT;
711550a7375SFelipe Balbi 				musb->int_usb &= ~MUSB_INTR_SUSPEND;
712550a7375SFelipe Balbi 				break;
713550a7375SFelipe Balbi 			}
714550a7375SFelipe Balbi 			musb_g_resume(musb);
715550a7375SFelipe Balbi 			break;
716550a7375SFelipe Balbi 		case OTG_STATE_B_IDLE:
717550a7375SFelipe Balbi 			musb->int_usb &= ~MUSB_INTR_SUSPEND;
718550a7375SFelipe Balbi 			break;
719550a7375SFelipe Balbi 		default:
720550a7375SFelipe Balbi 			WARNING("bogus %s RESUME (%s)\n",
721550a7375SFelipe Balbi 				"peripheral",
722285f28bfSPaul Cercueil 				musb_otg_state_string(musb));
723550a7375SFelipe Balbi 		}
724550a7375SFelipe Balbi 	}
725550a7375SFelipe Balbi }
726550a7375SFelipe Balbi 
727bcb8fd3aSBin Liu /* return IRQ_HANDLED to tell the caller to return immediately */
musb_handle_intr_sessreq(struct musb * musb,u8 devctl)728bcb8fd3aSBin Liu static irqreturn_t musb_handle_intr_sessreq(struct musb *musb, u8 devctl)
729bcb8fd3aSBin Liu {
730aa471456SFelipe Balbi 	void __iomem *mbase = musb->mregs;
731aa471456SFelipe Balbi 
73219aab56cSHeikki Krogerus 	if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
73319aab56cSHeikki Krogerus 			&& (devctl & MUSB_DEVCTL_BDEVICE)) {
734b99d3659SBin Liu 		musb_dbg(musb, "SessReq while on B state");
735a6038ee7SHeikki Krogerus 		return IRQ_HANDLED;
736a6038ee7SHeikki Krogerus 	}
737a6038ee7SHeikki Krogerus 
738285f28bfSPaul Cercueil 	musb_dbg(musb, "SESSION_REQUEST (%s)", musb_otg_state_string(musb));
739550a7375SFelipe Balbi 
740550a7375SFelipe Balbi 	/* IRQ arrives from ID pin sense or (later, if VBUS power
741550a7375SFelipe Balbi 	 * is removed) SRP.  responses are time critical:
742550a7375SFelipe Balbi 	 *  - turn on VBUS (with silicon-specific mechanism)
743550a7375SFelipe Balbi 	 *  - go through A_WAIT_VRISE
744550a7375SFelipe Balbi 	 *  - ... to A_WAIT_BCON.
745550a7375SFelipe Balbi 	 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
746550a7375SFelipe Balbi 	 */
747550a7375SFelipe Balbi 	musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
748550a7375SFelipe Balbi 	musb->ep0_stage = MUSB_EP0_START;
74921acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_A_IDLE);
750550a7375SFelipe Balbi 	MUSB_HST_MODE(musb);
751743411b3SFelipe Balbi 	musb_platform_set_vbus(musb, 1);
752550a7375SFelipe Balbi 
753bcb8fd3aSBin Liu 	return IRQ_NONE;
754550a7375SFelipe Balbi }
755550a7375SFelipe Balbi 
musb_handle_intr_vbuserr(struct musb * musb,u8 devctl)756bcb8fd3aSBin Liu static void musb_handle_intr_vbuserr(struct musb *musb, u8 devctl)
757bcb8fd3aSBin Liu {
758550a7375SFelipe Balbi 	int	ignore = 0;
759550a7375SFelipe Balbi 
760550a7375SFelipe Balbi 	/* During connection as an A-Device, we may see a short
761550a7375SFelipe Balbi 	 * current spikes causing voltage drop, because of cable
762550a7375SFelipe Balbi 	 * and peripheral capacitance combined with vbus draw.
763550a7375SFelipe Balbi 	 * (So: less common with truly self-powered devices, where
764550a7375SFelipe Balbi 	 * vbus doesn't act like a power supply.)
765550a7375SFelipe Balbi 	 *
766550a7375SFelipe Balbi 	 * Such spikes are short; usually less than ~500 usec, max
767550a7375SFelipe Balbi 	 * of ~2 msec.  That is, they're not sustained overcurrent
768550a7375SFelipe Balbi 	 * errors, though they're reported using VBUSERROR irqs.
769550a7375SFelipe Balbi 	 *
770550a7375SFelipe Balbi 	 * Workarounds:  (a) hardware: use self powered devices.
771550a7375SFelipe Balbi 	 * (b) software:  ignore non-repeated VBUS errors.
772550a7375SFelipe Balbi 	 *
773550a7375SFelipe Balbi 	 * REVISIT:  do delays from lots of DEBUG_KERNEL checks
774550a7375SFelipe Balbi 	 * make trouble here, keeping VBUS < 4.4V ?
775550a7375SFelipe Balbi 	 */
77621acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
777550a7375SFelipe Balbi 	case OTG_STATE_A_HOST:
778550a7375SFelipe Balbi 		/* recovery is dicey once we've gotten past the
779550a7375SFelipe Balbi 		 * initial stages of enumeration, but if VBUS
780550a7375SFelipe Balbi 		 * stayed ok at the other end of the link, and
781550a7375SFelipe Balbi 		 * another reset is due (at least for high speed,
782550a7375SFelipe Balbi 		 * to redo the chirp etc), it might work OK...
783550a7375SFelipe Balbi 		 */
784550a7375SFelipe Balbi 	case OTG_STATE_A_WAIT_BCON:
785550a7375SFelipe Balbi 	case OTG_STATE_A_WAIT_VRISE:
786550a7375SFelipe Balbi 		if (musb->vbuserr_retry) {
787aa471456SFelipe Balbi 			void __iomem *mbase = musb->mregs;
788aa471456SFelipe Balbi 
789550a7375SFelipe Balbi 			musb->vbuserr_retry--;
790550a7375SFelipe Balbi 			ignore = 1;
791550a7375SFelipe Balbi 			devctl |= MUSB_DEVCTL_SESSION;
792550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_DEVCTL, devctl);
793550a7375SFelipe Balbi 		} else {
794550a7375SFelipe Balbi 			musb->port1_status |=
795749da5f8SAlan Stern 				  USB_PORT_STAT_OVERCURRENT
796749da5f8SAlan Stern 				| (USB_PORT_STAT_C_OVERCURRENT << 16);
797550a7375SFelipe Balbi 		}
798550a7375SFelipe Balbi 		break;
799550a7375SFelipe Balbi 	default:
800550a7375SFelipe Balbi 		break;
801550a7375SFelipe Balbi 	}
802550a7375SFelipe Balbi 
80354485116SGrazvydas Ignotas 	dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
80454485116SGrazvydas Ignotas 			"VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
805285f28bfSPaul Cercueil 			musb_otg_state_string(musb),
806550a7375SFelipe Balbi 			devctl,
807550a7375SFelipe Balbi 			({ char *s;
808550a7375SFelipe Balbi 			switch (devctl & MUSB_DEVCTL_VBUS) {
809550a7375SFelipe Balbi 			case 0 << MUSB_DEVCTL_VBUS_SHIFT:
810550a7375SFelipe Balbi 				s = "<SessEnd"; break;
811550a7375SFelipe Balbi 			case 1 << MUSB_DEVCTL_VBUS_SHIFT:
812550a7375SFelipe Balbi 				s = "<AValid"; break;
813550a7375SFelipe Balbi 			case 2 << MUSB_DEVCTL_VBUS_SHIFT:
814550a7375SFelipe Balbi 				s = "<VBusValid"; break;
815550a7375SFelipe Balbi 			/* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
816550a7375SFelipe Balbi 			default:
817550a7375SFelipe Balbi 				s = "VALID"; break;
8182b84f92bSJoe Perches 			} s; }),
819550a7375SFelipe Balbi 			VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
820550a7375SFelipe Balbi 			musb->port1_status);
821550a7375SFelipe Balbi 
822550a7375SFelipe Balbi 	/* go through A_WAIT_VFALL then start a new session */
823550a7375SFelipe Balbi 	if (!ignore)
824743411b3SFelipe Balbi 		musb_platform_set_vbus(musb, 0);
825550a7375SFelipe Balbi }
826550a7375SFelipe Balbi 
musb_handle_intr_suspend(struct musb * musb,u8 devctl)827bcb8fd3aSBin Liu static void musb_handle_intr_suspend(struct musb *musb, u8 devctl)
828bcb8fd3aSBin Liu {
829b99d3659SBin Liu 	musb_dbg(musb, "SUSPEND (%s) devctl %02x",
830285f28bfSPaul Cercueil 		 musb_otg_state_string(musb), devctl);
8311c25fda4SArnaud Mandy 
83221acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
8331c25fda4SArnaud Mandy 	case OTG_STATE_A_PERIPHERAL:
8341c25fda4SArnaud Mandy 		/* We also come here if the cable is removed, since
8351c25fda4SArnaud Mandy 		 * this silicon doesn't report ID-no-longer-grounded.
8361c25fda4SArnaud Mandy 		 *
8371c25fda4SArnaud Mandy 		 * We depend on T(a_wait_bcon) to shut us down, and
8381c25fda4SArnaud Mandy 		 * hope users don't do anything dicey during this
8391c25fda4SArnaud Mandy 		 * undesired detour through A_WAIT_BCON.
8401c25fda4SArnaud Mandy 		 */
8411c25fda4SArnaud Mandy 		musb_hnp_stop(musb);
8420b3eba44SDaniel Mack 		musb_host_resume_root_hub(musb);
8431c25fda4SArnaud Mandy 		musb_root_disconnect(musb);
8441c25fda4SArnaud Mandy 		musb_platform_try_idle(musb, jiffies
8451c25fda4SArnaud Mandy 				+ msecs_to_jiffies(musb->a_wait_bcon
8461c25fda4SArnaud Mandy 					? : OTG_TIME_A_WAIT_BCON));
8471c25fda4SArnaud Mandy 
8481c25fda4SArnaud Mandy 		break;
8491c25fda4SArnaud Mandy 	case OTG_STATE_B_IDLE:
8501c25fda4SArnaud Mandy 		if (!musb->is_active)
8511c25fda4SArnaud Mandy 			break;
852df561f66SGustavo A. R. Silva 		fallthrough;
8531c25fda4SArnaud Mandy 	case OTG_STATE_B_PERIPHERAL:
8541c25fda4SArnaud Mandy 		musb_g_suspend(musb);
855eee3f15dSFelipe Balbi 		musb->is_active = musb->g.b_hnp_enable;
8561c25fda4SArnaud Mandy 		if (musb->is_active) {
85721acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_B_WAIT_ACON);
858b99d3659SBin Liu 			musb_dbg(musb, "HNP: Setting timer for b_ase0_brst");
8591c25fda4SArnaud Mandy 			mod_timer(&musb->otg_timer, jiffies
8601c25fda4SArnaud Mandy 				+ msecs_to_jiffies(
8611c25fda4SArnaud Mandy 						OTG_TIME_B_ASE0_BRST));
8621c25fda4SArnaud Mandy 		}
8631c25fda4SArnaud Mandy 		break;
8641c25fda4SArnaud Mandy 	case OTG_STATE_A_WAIT_BCON:
8651c25fda4SArnaud Mandy 		if (musb->a_wait_bcon != 0)
8661c25fda4SArnaud Mandy 			musb_platform_try_idle(musb, jiffies
8671c25fda4SArnaud Mandy 				+ msecs_to_jiffies(musb->a_wait_bcon));
8681c25fda4SArnaud Mandy 		break;
8691c25fda4SArnaud Mandy 	case OTG_STATE_A_HOST:
87021acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_SUSPEND);
871eee3f15dSFelipe Balbi 		musb->is_active = musb->hcd->self.b_hnp_enable;
8721c25fda4SArnaud Mandy 		break;
8731c25fda4SArnaud Mandy 	case OTG_STATE_B_HOST:
8741c25fda4SArnaud Mandy 		/* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
875b99d3659SBin Liu 		musb_dbg(musb, "REVISIT: SUSPEND as B_HOST");
8761c25fda4SArnaud Mandy 		break;
8771c25fda4SArnaud Mandy 	default:
8781c25fda4SArnaud Mandy 		/* "should not happen" */
8791c25fda4SArnaud Mandy 		musb->is_active = 0;
8801c25fda4SArnaud Mandy 		break;
8811c25fda4SArnaud Mandy 	}
8821c25fda4SArnaud Mandy }
8831c25fda4SArnaud Mandy 
musb_handle_intr_connect(struct musb * musb,u8 devctl,u8 int_usb)884bcb8fd3aSBin Liu static void musb_handle_intr_connect(struct musb *musb, u8 devctl, u8 int_usb)
885bcb8fd3aSBin Liu {
8868b125df5SDaniel Mack 	struct usb_hcd *hcd = musb->hcd;
887550a7375SFelipe Balbi 
888550a7375SFelipe Balbi 	musb->is_active = 1;
889550a7375SFelipe Balbi 	musb->ep0_stage = MUSB_EP0_START;
890550a7375SFelipe Balbi 
891b18d26f6SSebastian Andrzej Siewior 	musb->intrtxe = musb->epmask;
892b18d26f6SSebastian Andrzej Siewior 	musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
893af5ec14dSSebastian Andrzej Siewior 	musb->intrrxe = musb->epmask & 0xfffe;
894af5ec14dSSebastian Andrzej Siewior 	musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
895d709d22eSAjay Kumar Gupta 	musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
896550a7375SFelipe Balbi 	musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
897550a7375SFelipe Balbi 				|USB_PORT_STAT_HIGH_SPEED
898550a7375SFelipe Balbi 				|USB_PORT_STAT_ENABLE
899550a7375SFelipe Balbi 				);
900550a7375SFelipe Balbi 	musb->port1_status |= USB_PORT_STAT_CONNECTION
901550a7375SFelipe Balbi 				|(USB_PORT_STAT_C_CONNECTION << 16);
902550a7375SFelipe Balbi 
903550a7375SFelipe Balbi 	/* high vs full speed is just a guess until after reset */
904550a7375SFelipe Balbi 	if (devctl & MUSB_DEVCTL_LSDEV)
905550a7375SFelipe Balbi 		musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
906550a7375SFelipe Balbi 
907550a7375SFelipe Balbi 	/* indicate new connection to OTG machine */
90821acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
909550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
910550a7375SFelipe Balbi 		if (int_usb & MUSB_INTR_SUSPEND) {
911b99d3659SBin Liu 			musb_dbg(musb, "HNP: SUSPEND+CONNECT, now b_host");
912550a7375SFelipe Balbi 			int_usb &= ~MUSB_INTR_SUSPEND;
9131de00daeSDavid Brownell 			goto b_host;
914550a7375SFelipe Balbi 		} else
915b99d3659SBin Liu 			musb_dbg(musb, "CONNECT as b_peripheral???");
916550a7375SFelipe Balbi 		break;
917550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
918b99d3659SBin Liu 		musb_dbg(musb, "HNP: CONNECT, now b_host");
9191de00daeSDavid Brownell b_host:
92021acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_HOST);
92174c2e936SDaniel Mack 		if (musb->hcd)
92274c2e936SDaniel Mack 			musb->hcd->self.is_b_host = 1;
9231de00daeSDavid Brownell 		del_timer(&musb->otg_timer);
924550a7375SFelipe Balbi 		break;
925550a7375SFelipe Balbi 	default:
926550a7375SFelipe Balbi 		if ((devctl & MUSB_DEVCTL_VBUS)
927550a7375SFelipe Balbi 				== (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
92821acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_A_HOST);
9290b3eba44SDaniel Mack 			if (hcd)
930550a7375SFelipe Balbi 				hcd->self.is_b_host = 0;
931550a7375SFelipe Balbi 		}
932550a7375SFelipe Balbi 		break;
933550a7375SFelipe Balbi 	}
9341de00daeSDavid Brownell 
9350b3eba44SDaniel Mack 	musb_host_poke_root_hub(musb);
9361de00daeSDavid Brownell 
937b99d3659SBin Liu 	musb_dbg(musb, "CONNECT (%s) devctl %02x",
938285f28bfSPaul Cercueil 			musb_otg_state_string(musb), devctl);
939550a7375SFelipe Balbi }
940550a7375SFelipe Balbi 
musb_handle_intr_disconnect(struct musb * musb,u8 devctl)941bcb8fd3aSBin Liu static void musb_handle_intr_disconnect(struct musb *musb, u8 devctl)
942bcb8fd3aSBin Liu {
943b99d3659SBin Liu 	musb_dbg(musb, "DISCONNECT (%s) as %s, devctl %02x",
944285f28bfSPaul Cercueil 			musb_otg_state_string(musb),
945550a7375SFelipe Balbi 			MUSB_MODE(musb), devctl);
946550a7375SFelipe Balbi 
94721acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
948550a7375SFelipe Balbi 	case OTG_STATE_A_HOST:
949550a7375SFelipe Balbi 	case OTG_STATE_A_SUSPEND:
9500b3eba44SDaniel Mack 		musb_host_resume_root_hub(musb);
951550a7375SFelipe Balbi 		musb_root_disconnect(musb);
952032ec49fSFelipe Balbi 		if (musb->a_wait_bcon != 0)
953550a7375SFelipe Balbi 			musb_platform_try_idle(musb, jiffies
954550a7375SFelipe Balbi 				+ msecs_to_jiffies(musb->a_wait_bcon));
955550a7375SFelipe Balbi 		break;
956550a7375SFelipe Balbi 	case OTG_STATE_B_HOST:
957ab983f2aSDavid Brownell 		/* REVISIT this behaves for "real disconnect"
958ab983f2aSDavid Brownell 		 * cases; make sure the other transitions from
959ab983f2aSDavid Brownell 		 * from B_HOST act right too.  The B_HOST code
960ab983f2aSDavid Brownell 		 * in hnp_stop() is currently not used...
961ab983f2aSDavid Brownell 		 */
962ab983f2aSDavid Brownell 		musb_root_disconnect(musb);
96374c2e936SDaniel Mack 		if (musb->hcd)
96474c2e936SDaniel Mack 			musb->hcd->self.is_b_host = 0;
96521acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
966ab983f2aSDavid Brownell 		MUSB_DEV_MODE(musb);
967ab983f2aSDavid Brownell 		musb_g_disconnect(musb);
968550a7375SFelipe Balbi 		break;
969550a7375SFelipe Balbi 	case OTG_STATE_A_PERIPHERAL:
970550a7375SFelipe Balbi 		musb_hnp_stop(musb);
971550a7375SFelipe Balbi 		musb_root_disconnect(musb);
972df561f66SGustavo A. R. Silva 		fallthrough;
973550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
974550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
975550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
976550a7375SFelipe Balbi 		musb_g_disconnect(musb);
977550a7375SFelipe Balbi 		break;
978550a7375SFelipe Balbi 	default:
979550a7375SFelipe Balbi 		WARNING("unhandled DISCONNECT transition (%s)\n",
980285f28bfSPaul Cercueil 			musb_otg_state_string(musb));
981550a7375SFelipe Balbi 		break;
982550a7375SFelipe Balbi 	}
983550a7375SFelipe Balbi }
984550a7375SFelipe Balbi 
985bcb8fd3aSBin Liu /*
986bcb8fd3aSBin Liu  * mentor saves a bit: bus reset and babble share the same irq.
9871c25fda4SArnaud Mandy  * only host sees babble; only peripheral sees bus reset.
988550a7375SFelipe Balbi  */
musb_handle_intr_reset(struct musb * musb)989bcb8fd3aSBin Liu static void musb_handle_intr_reset(struct musb *musb)
990bcb8fd3aSBin Liu {
991445ef615SJonathan Liu 	if (is_host_active(musb)) {
9921c25fda4SArnaud Mandy 		/*
99334754decSFelipe Balbi 		 * When BABBLE happens what we can depends on which
99428378d5eSFelipe Balbi 		 * platform MUSB is running, because some platforms
99528378d5eSFelipe Balbi 		 * implemented proprietary means for 'recovering' from
99628378d5eSFelipe Balbi 		 * Babble conditions. One such platform is AM335x. In
99734754decSFelipe Balbi 		 * most cases, however, the only thing we can do is
99834754decSFelipe Balbi 		 * drop the session.
9991c25fda4SArnaud Mandy 		 */
100052b9e6ebSFelipe Balbi 		dev_err(musb->controller, "Babble\n");
100183b8f5b8SFelipe Balbi 		musb_recover_from_babble(musb);
1002a04d46d0SFelipe Balbi 	} else {
1003285f28bfSPaul Cercueil 		musb_dbg(musb, "BUS RESET as %s", musb_otg_state_string(musb));
100421acc656SPaul Cercueil 		switch (musb_get_state(musb)) {
10051c25fda4SArnaud Mandy 		case OTG_STATE_A_SUSPEND:
10061c25fda4SArnaud Mandy 			musb_g_reset(musb);
1007df561f66SGustavo A. R. Silva 			fallthrough;
10081c25fda4SArnaud Mandy 		case OTG_STATE_A_WAIT_BCON:	/* OPT TD.4.7-900ms */
10091c25fda4SArnaud Mandy 			/* never use invalid T(a_wait_bcon) */
1010b99d3659SBin Liu 			musb_dbg(musb, "HNP: in %s, %d msec timeout",
1011285f28bfSPaul Cercueil 				 musb_otg_state_string(musb),
10121c25fda4SArnaud Mandy 				TA_WAIT_BCON(musb));
10131c25fda4SArnaud Mandy 			mod_timer(&musb->otg_timer, jiffies
10141c25fda4SArnaud Mandy 				+ msecs_to_jiffies(TA_WAIT_BCON(musb)));
1015550a7375SFelipe Balbi 			break;
10161c25fda4SArnaud Mandy 		case OTG_STATE_A_PERIPHERAL:
10171c25fda4SArnaud Mandy 			del_timer(&musb->otg_timer);
10181c25fda4SArnaud Mandy 			musb_g_reset(musb);
1019550a7375SFelipe Balbi 			break;
10201c25fda4SArnaud Mandy 		case OTG_STATE_B_WAIT_ACON:
1021b99d3659SBin Liu 			musb_dbg(musb, "HNP: RESET (%s), to b_peripheral",
1022285f28bfSPaul Cercueil 				 musb_otg_state_string(musb));
102321acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
10241c25fda4SArnaud Mandy 			musb_g_reset(musb);
1025550a7375SFelipe Balbi 			break;
10261c25fda4SArnaud Mandy 		case OTG_STATE_B_IDLE:
102721acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
1028df561f66SGustavo A. R. Silva 			fallthrough;
10291c25fda4SArnaud Mandy 		case OTG_STATE_B_PERIPHERAL:
10301c25fda4SArnaud Mandy 			musb_g_reset(musb);
1031550a7375SFelipe Balbi 			break;
1032550a7375SFelipe Balbi 		default:
1033b99d3659SBin Liu 			musb_dbg(musb, "Unhandled BUS RESET as %s",
1034285f28bfSPaul Cercueil 				 musb_otg_state_string(musb));
1035550a7375SFelipe Balbi 		}
10361c25fda4SArnaud Mandy 	}
1037550a7375SFelipe Balbi }
1038550a7375SFelipe Balbi 
1039bcb8fd3aSBin Liu /*
1040bcb8fd3aSBin Liu  * Interrupt Service Routine to record USB "global" interrupts.
1041bcb8fd3aSBin Liu  * Since these do not happen often and signify things of
1042bcb8fd3aSBin Liu  * paramount importance, it seems OK to check them individually;
1043bcb8fd3aSBin Liu  * the order of the tests is specified in the manual
1044bcb8fd3aSBin Liu  *
1045bcb8fd3aSBin Liu  * @param musb instance pointer
1046bcb8fd3aSBin Liu  * @param int_usb register contents
1047bcb8fd3aSBin Liu  * @param devctl
1048bcb8fd3aSBin Liu  */
1049bcb8fd3aSBin Liu 
musb_stage0_irq(struct musb * musb,u8 int_usb,u8 devctl)1050bcb8fd3aSBin Liu static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
1051bcb8fd3aSBin Liu 				u8 devctl)
1052bcb8fd3aSBin Liu {
1053bcb8fd3aSBin Liu 	irqreturn_t handled = IRQ_NONE;
1054bcb8fd3aSBin Liu 
1055bcb8fd3aSBin Liu 	musb_dbg(musb, "<== DevCtl=%02x, int_usb=0x%x", devctl, int_usb);
1056bcb8fd3aSBin Liu 
1057bcb8fd3aSBin Liu 	/* in host mode, the peripheral may issue remote wakeup.
1058bcb8fd3aSBin Liu 	 * in peripheral mode, the host may resume the link.
1059bcb8fd3aSBin Liu 	 * spurious RESUME irqs happen too, paired with SUSPEND.
1060bcb8fd3aSBin Liu 	 */
1061bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_RESUME) {
1062bcb8fd3aSBin Liu 		musb_handle_intr_resume(musb, devctl);
1063bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1064bcb8fd3aSBin Liu 	}
1065bcb8fd3aSBin Liu 
1066bcb8fd3aSBin Liu 	/* see manual for the order of the tests */
1067bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_SESSREQ) {
1068bcb8fd3aSBin Liu 		if (musb_handle_intr_sessreq(musb, devctl))
1069bcb8fd3aSBin Liu 			return IRQ_HANDLED;
1070bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1071bcb8fd3aSBin Liu 	}
1072bcb8fd3aSBin Liu 
1073bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_VBUSERROR) {
1074bcb8fd3aSBin Liu 		musb_handle_intr_vbuserr(musb, devctl);
1075bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1076bcb8fd3aSBin Liu 	}
1077bcb8fd3aSBin Liu 
1078bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_SUSPEND) {
1079bcb8fd3aSBin Liu 		musb_handle_intr_suspend(musb, devctl);
1080bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1081bcb8fd3aSBin Liu 	}
1082bcb8fd3aSBin Liu 
1083bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_CONNECT) {
1084bcb8fd3aSBin Liu 		musb_handle_intr_connect(musb, devctl, int_usb);
1085bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1086bcb8fd3aSBin Liu 	}
1087bcb8fd3aSBin Liu 
1088bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_DISCONNECT) {
1089bcb8fd3aSBin Liu 		musb_handle_intr_disconnect(musb, devctl);
1090bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1091bcb8fd3aSBin Liu 	}
1092bcb8fd3aSBin Liu 
1093bcb8fd3aSBin Liu 	if (int_usb & MUSB_INTR_RESET) {
1094bcb8fd3aSBin Liu 		musb_handle_intr_reset(musb);
1095bcb8fd3aSBin Liu 		handled = IRQ_HANDLED;
1096bcb8fd3aSBin Liu 	}
1097bcb8fd3aSBin Liu 
10981c25fda4SArnaud Mandy #if 0
10991c25fda4SArnaud Mandy /* REVISIT ... this would be for multiplexing periodic endpoints, or
11001c25fda4SArnaud Mandy  * supporting transfer phasing to prevent exceeding ISO bandwidth
11011c25fda4SArnaud Mandy  * limits of a given frame or microframe.
11021c25fda4SArnaud Mandy  *
11031c25fda4SArnaud Mandy  * It's not needed for peripheral side, which dedicates endpoints;
11041c25fda4SArnaud Mandy  * though it _might_ use SOF irqs for other purposes.
11051c25fda4SArnaud Mandy  *
11061c25fda4SArnaud Mandy  * And it's not currently needed for host side, which also dedicates
11071c25fda4SArnaud Mandy  * endpoints, relies on TX/RX interval registers, and isn't claimed
11081c25fda4SArnaud Mandy  * to support ISO transfers yet.
11091c25fda4SArnaud Mandy  */
11101c25fda4SArnaud Mandy 	if (int_usb & MUSB_INTR_SOF) {
11111c25fda4SArnaud Mandy 		void __iomem *mbase = musb->mregs;
11121c25fda4SArnaud Mandy 		struct musb_hw_ep	*ep;
11131c25fda4SArnaud Mandy 		u8 epnum;
11141c25fda4SArnaud Mandy 		u16 frame;
11151c25fda4SArnaud Mandy 
11165c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "START_OF_FRAME\n");
11171c25fda4SArnaud Mandy 		handled = IRQ_HANDLED;
11181c25fda4SArnaud Mandy 
11191c25fda4SArnaud Mandy 		/* start any periodic Tx transfers waiting for current frame */
11201c25fda4SArnaud Mandy 		frame = musb_readw(mbase, MUSB_FRAME);
11211c25fda4SArnaud Mandy 		ep = musb->endpoints;
11221c25fda4SArnaud Mandy 		for (epnum = 1; (epnum < musb->nr_endpoints)
11231c25fda4SArnaud Mandy 					&& (musb->epmask >= (1 << epnum));
11241c25fda4SArnaud Mandy 				epnum++, ep++) {
11251c25fda4SArnaud Mandy 			/*
11261c25fda4SArnaud Mandy 			 * FIXME handle framecounter wraps (12 bits)
11271c25fda4SArnaud Mandy 			 * eliminate duplicated StartUrb logic
11281c25fda4SArnaud Mandy 			 */
11291c25fda4SArnaud Mandy 			if (ep->dwWaitFrame >= frame) {
11301c25fda4SArnaud Mandy 				ep->dwWaitFrame = 0;
11311c25fda4SArnaud Mandy 				pr_debug("SOF --> periodic TX%s on %d\n",
11321c25fda4SArnaud Mandy 					ep->tx_channel ? " DMA" : "",
11331c25fda4SArnaud Mandy 					epnum);
11341c25fda4SArnaud Mandy 				if (!ep->tx_channel)
11351c25fda4SArnaud Mandy 					musb_h_tx_start(musb, epnum);
11361c25fda4SArnaud Mandy 				else
11371c25fda4SArnaud Mandy 					cppi_hostdma_start(musb, epnum);
11381c25fda4SArnaud Mandy 			}
11391c25fda4SArnaud Mandy 		}		/* end of for loop */
11401c25fda4SArnaud Mandy 	}
11411c25fda4SArnaud Mandy #endif
11421c25fda4SArnaud Mandy 
11432bff3916STony Lindgren 	schedule_delayed_work(&musb->irq_work, 0);
1144550a7375SFelipe Balbi 
1145550a7375SFelipe Balbi 	return handled;
1146550a7375SFelipe Balbi }
1147550a7375SFelipe Balbi 
1148550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
1149550a7375SFelipe Balbi 
musb_disable_interrupts(struct musb * musb)1150e1eb3eb8SFelipe Balbi static void musb_disable_interrupts(struct musb *musb)
1151550a7375SFelipe Balbi {
1152550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
1153550a7375SFelipe Balbi 
1154550a7375SFelipe Balbi 	/* disable interrupts */
1155550a7375SFelipe Balbi 	musb_writeb(mbase, MUSB_INTRUSBE, 0);
1156b18d26f6SSebastian Andrzej Siewior 	musb->intrtxe = 0;
1157550a7375SFelipe Balbi 	musb_writew(mbase, MUSB_INTRTXE, 0);
1158af5ec14dSSebastian Andrzej Siewior 	musb->intrrxe = 0;
1159550a7375SFelipe Balbi 	musb_writew(mbase, MUSB_INTRRXE, 0);
1160550a7375SFelipe Balbi 
1161550a7375SFelipe Balbi 	/*  flush pending interrupts */
11629c93d7fdSMin Guo 	musb_clearb(mbase, MUSB_INTRUSB);
11639c93d7fdSMin Guo 	musb_clearw(mbase, MUSB_INTRTX);
11649c93d7fdSMin Guo 	musb_clearw(mbase, MUSB_INTRRX);
1165e1eb3eb8SFelipe Balbi }
1166550a7375SFelipe Balbi 
musb_enable_interrupts(struct musb * musb)1167e1eb3eb8SFelipe Balbi static void musb_enable_interrupts(struct musb *musb)
1168e1eb3eb8SFelipe Balbi {
1169e1eb3eb8SFelipe Balbi 	void __iomem    *regs = musb->mregs;
1170e1eb3eb8SFelipe Balbi 
1171e1eb3eb8SFelipe Balbi 	/*  Set INT enable registers, enable interrupts */
1172e1eb3eb8SFelipe Balbi 	musb->intrtxe = musb->epmask;
1173e1eb3eb8SFelipe Balbi 	musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
1174e1eb3eb8SFelipe Balbi 	musb->intrrxe = musb->epmask & 0xfffe;
1175e1eb3eb8SFelipe Balbi 	musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
1176e1eb3eb8SFelipe Balbi 	musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
1177e1eb3eb8SFelipe Balbi 
1178e1eb3eb8SFelipe Balbi }
1179e1eb3eb8SFelipe Balbi 
1180550a7375SFelipe Balbi /*
1181001dd84aSSebastian Andrzej Siewior  * Program the HDRC to start (enable interrupts, dma, etc.).
1182001dd84aSSebastian Andrzej Siewior  */
musb_start(struct musb * musb)1183001dd84aSSebastian Andrzej Siewior void musb_start(struct musb *musb)
1184001dd84aSSebastian Andrzej Siewior {
1185001dd84aSSebastian Andrzej Siewior 	void __iomem    *regs = musb->mregs;
1186001dd84aSSebastian Andrzej Siewior 	u8              devctl = musb_readb(regs, MUSB_DEVCTL);
11879b753764SBin Liu 	u8		power;
1188001dd84aSSebastian Andrzej Siewior 
1189b99d3659SBin Liu 	musb_dbg(musb, "<== devctl %02x", devctl);
1190001dd84aSSebastian Andrzej Siewior 
1191e1eb3eb8SFelipe Balbi 	musb_enable_interrupts(musb);
1192001dd84aSSebastian Andrzej Siewior 	musb_writeb(regs, MUSB_TESTMODE, 0);
1193001dd84aSSebastian Andrzej Siewior 
11949b753764SBin Liu 	power = MUSB_POWER_ISOUPDATE;
11959b753764SBin Liu 	/*
11969b753764SBin Liu 	 * treating UNKNOWN as unspecified maximum speed, in which case
11979b753764SBin Liu 	 * we will default to high-speed.
11989b753764SBin Liu 	 */
11999b753764SBin Liu 	if (musb->config->maximum_speed == USB_SPEED_HIGH ||
12009b753764SBin Liu 			musb->config->maximum_speed == USB_SPEED_UNKNOWN)
12019b753764SBin Liu 		power |= MUSB_POWER_HSENAB;
12029b753764SBin Liu 	musb_writeb(regs, MUSB_POWER, power);
1203001dd84aSSebastian Andrzej Siewior 
1204001dd84aSSebastian Andrzej Siewior 	musb->is_active = 0;
1205001dd84aSSebastian Andrzej Siewior 	devctl = musb_readb(regs, MUSB_DEVCTL);
1206001dd84aSSebastian Andrzej Siewior 	devctl &= ~MUSB_DEVCTL_SESSION;
1207001dd84aSSebastian Andrzej Siewior 
1208001dd84aSSebastian Andrzej Siewior 	/* session started after:
1209001dd84aSSebastian Andrzej Siewior 	 * (a) ID-grounded irq, host mode;
1210001dd84aSSebastian Andrzej Siewior 	 * (b) vbus present/connect IRQ, peripheral mode;
1211001dd84aSSebastian Andrzej Siewior 	 * (c) peripheral initiates, using SRP
1212001dd84aSSebastian Andrzej Siewior 	 */
12137ad76955SBin Liu 	if (musb->port_mode != MUSB_HOST &&
121421acc656SPaul Cercueil 	    musb_get_state(musb) != OTG_STATE_A_WAIT_BCON &&
1215001dd84aSSebastian Andrzej Siewior 	    (devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
1216001dd84aSSebastian Andrzej Siewior 		musb->is_active = 1;
1217001dd84aSSebastian Andrzej Siewior 	} else {
1218001dd84aSSebastian Andrzej Siewior 		devctl |= MUSB_DEVCTL_SESSION;
1219001dd84aSSebastian Andrzej Siewior 	}
1220001dd84aSSebastian Andrzej Siewior 
1221001dd84aSSebastian Andrzej Siewior 	musb_platform_enable(musb);
1222001dd84aSSebastian Andrzej Siewior 	musb_writeb(regs, MUSB_DEVCTL, devctl);
1223001dd84aSSebastian Andrzej Siewior }
1224001dd84aSSebastian Andrzej Siewior 
1225001dd84aSSebastian Andrzej Siewior /*
1226550a7375SFelipe Balbi  * Make the HDRC stop (disable interrupts, etc.);
1227550a7375SFelipe Balbi  * reversible by musb_start
1228550a7375SFelipe Balbi  * called on gadget driver unregister
1229550a7375SFelipe Balbi  * with controller locked, irqs blocked
1230550a7375SFelipe Balbi  * acts as a NOP unless some role activated the hardware
1231550a7375SFelipe Balbi  */
musb_stop(struct musb * musb)1232550a7375SFelipe Balbi void musb_stop(struct musb *musb)
1233550a7375SFelipe Balbi {
1234550a7375SFelipe Balbi 	/* stop IRQs, timers, ... */
1235550a7375SFelipe Balbi 	musb_platform_disable(musb);
1236e945953dSBin Liu 	musb_disable_interrupts(musb);
1237e945953dSBin Liu 	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1238550a7375SFelipe Balbi 
1239550a7375SFelipe Balbi 	/* FIXME
1240550a7375SFelipe Balbi 	 *  - mark host and/or peripheral drivers unusable/inactive
1241550a7375SFelipe Balbi 	 *  - disable DMA (and enable it in HdrcStart)
1242550a7375SFelipe Balbi 	 *  - make sure we can musb_start() after musb_stop(); with
1243550a7375SFelipe Balbi 	 *    OTG mode, gadget driver module rmmod/modprobe cycles that
1244550a7375SFelipe Balbi 	 *  - ...
1245550a7375SFelipe Balbi 	 */
1246550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
1247550a7375SFelipe Balbi }
1248550a7375SFelipe Balbi 
1249550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
1250550a7375SFelipe Balbi 
1251550a7375SFelipe Balbi /*
1252550a7375SFelipe Balbi  * The silicon either has hard-wired endpoint configurations, or else
1253550a7375SFelipe Balbi  * "dynamic fifo" sizing.  The driver has support for both, though at this
1254c767c1c6SDavid Brownell  * writing only the dynamic sizing is very well tested.   Since we switched
1255c767c1c6SDavid Brownell  * away from compile-time hardware parameters, we can no longer rely on
1256c767c1c6SDavid Brownell  * dead code elimination to leave only the relevant one in the object file.
1257550a7375SFelipe Balbi  *
1258550a7375SFelipe Balbi  * We don't currently use dynamic fifo setup capability to do anything
1259550a7375SFelipe Balbi  * more than selecting one of a bunch of predefined configurations.
1260550a7375SFelipe Balbi  */
12618a77f05aSTony Lindgren static ushort fifo_mode;
1262550a7375SFelipe Balbi 
1263550a7375SFelipe Balbi /* "modprobe ... fifo_mode=1" etc */
1264550a7375SFelipe Balbi module_param(fifo_mode, ushort, 0);
1265550a7375SFelipe Balbi MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1266550a7375SFelipe Balbi 
1267550a7375SFelipe Balbi /*
1268550a7375SFelipe Balbi  * tables defining fifo_mode values.  define more if you like.
1269550a7375SFelipe Balbi  * for host side, make sure both halves of ep1 are set up.
1270550a7375SFelipe Balbi  */
1271550a7375SFelipe Balbi 
1272550a7375SFelipe Balbi /* mode 0 - fits in 2KB */
1273d3608b6dSBill Pemberton static struct musb_fifo_cfg mode_0_cfg[] = {
1274550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, },
1275550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, },
1276550a7375SFelipe Balbi { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1277550a7375SFelipe Balbi { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1278550a7375SFelipe Balbi { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1279550a7375SFelipe Balbi };
1280550a7375SFelipe Balbi 
1281550a7375SFelipe Balbi /* mode 1 - fits in 4KB */
1282d3608b6dSBill Pemberton static struct musb_fifo_cfg mode_1_cfg[] = {
1283550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1284550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1285550a7375SFelipe Balbi { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1286550a7375SFelipe Balbi { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1287550a7375SFelipe Balbi { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1288550a7375SFelipe Balbi };
1289550a7375SFelipe Balbi 
1290550a7375SFelipe Balbi /* mode 2 - fits in 4KB */
1291d3608b6dSBill Pemberton static struct musb_fifo_cfg mode_2_cfg[] = {
1292550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, },
1293550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, },
1294550a7375SFelipe Balbi { .hw_ep_num = 2, .style = FIFO_TX,   .maxpacket = 512, },
1295550a7375SFelipe Balbi { .hw_ep_num = 2, .style = FIFO_RX,   .maxpacket = 512, },
129655aad53fSBin Liu { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 960, },
129755aad53fSBin Liu { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 1024, },
1298550a7375SFelipe Balbi };
1299550a7375SFelipe Balbi 
1300550a7375SFelipe Balbi /* mode 3 - fits in 4KB */
1301d3608b6dSBill Pemberton static struct musb_fifo_cfg mode_3_cfg[] = {
1302550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_TX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1303550a7375SFelipe Balbi { .hw_ep_num = 1, .style = FIFO_RX,   .maxpacket = 512, .mode = BUF_DOUBLE, },
1304550a7375SFelipe Balbi { .hw_ep_num = 2, .style = FIFO_TX,   .maxpacket = 512, },
1305550a7375SFelipe Balbi { .hw_ep_num = 2, .style = FIFO_RX,   .maxpacket = 512, },
1306550a7375SFelipe Balbi { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1307550a7375SFelipe Balbi { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1308550a7375SFelipe Balbi };
1309550a7375SFelipe Balbi 
1310550a7375SFelipe Balbi /* mode 4 - fits in 16KB */
1311d3608b6dSBill Pemberton static struct musb_fifo_cfg mode_4_cfg[] = {
1312550a7375SFelipe Balbi { .hw_ep_num =  1, .style = FIFO_TX,   .maxpacket = 512, },
1313550a7375SFelipe Balbi { .hw_ep_num =  1, .style = FIFO_RX,   .maxpacket = 512, },
1314550a7375SFelipe Balbi { .hw_ep_num =  2, .style = FIFO_TX,   .maxpacket = 512, },
1315550a7375SFelipe Balbi { .hw_ep_num =  2, .style = FIFO_RX,   .maxpacket = 512, },
1316550a7375SFelipe Balbi { .hw_ep_num =  3, .style = FIFO_TX,   .maxpacket = 512, },
1317550a7375SFelipe Balbi { .hw_ep_num =  3, .style = FIFO_RX,   .maxpacket = 512, },
1318550a7375SFelipe Balbi { .hw_ep_num =  4, .style = FIFO_TX,   .maxpacket = 512, },
1319550a7375SFelipe Balbi { .hw_ep_num =  4, .style = FIFO_RX,   .maxpacket = 512, },
1320550a7375SFelipe Balbi { .hw_ep_num =  5, .style = FIFO_TX,   .maxpacket = 512, },
1321550a7375SFelipe Balbi { .hw_ep_num =  5, .style = FIFO_RX,   .maxpacket = 512, },
1322550a7375SFelipe Balbi { .hw_ep_num =  6, .style = FIFO_TX,   .maxpacket = 512, },
1323550a7375SFelipe Balbi { .hw_ep_num =  6, .style = FIFO_RX,   .maxpacket = 512, },
1324550a7375SFelipe Balbi { .hw_ep_num =  7, .style = FIFO_TX,   .maxpacket = 512, },
1325550a7375SFelipe Balbi { .hw_ep_num =  7, .style = FIFO_RX,   .maxpacket = 512, },
1326550a7375SFelipe Balbi { .hw_ep_num =  8, .style = FIFO_TX,   .maxpacket = 512, },
1327550a7375SFelipe Balbi { .hw_ep_num =  8, .style = FIFO_RX,   .maxpacket = 512, },
1328550a7375SFelipe Balbi { .hw_ep_num =  9, .style = FIFO_TX,   .maxpacket = 512, },
1329550a7375SFelipe Balbi { .hw_ep_num =  9, .style = FIFO_RX,   .maxpacket = 512, },
1330a483d706SAjay Kumar Gupta { .hw_ep_num = 10, .style = FIFO_TX,   .maxpacket = 256, },
1331a483d706SAjay Kumar Gupta { .hw_ep_num = 10, .style = FIFO_RX,   .maxpacket = 64, },
1332a483d706SAjay Kumar Gupta { .hw_ep_num = 11, .style = FIFO_TX,   .maxpacket = 256, },
1333a483d706SAjay Kumar Gupta { .hw_ep_num = 11, .style = FIFO_RX,   .maxpacket = 64, },
1334a483d706SAjay Kumar Gupta { .hw_ep_num = 12, .style = FIFO_TX,   .maxpacket = 256, },
1335a483d706SAjay Kumar Gupta { .hw_ep_num = 12, .style = FIFO_RX,   .maxpacket = 64, },
1336a483d706SAjay Kumar Gupta { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
1337550a7375SFelipe Balbi { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1338550a7375SFelipe Balbi { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1339550a7375SFelipe Balbi };
1340550a7375SFelipe Balbi 
13413b151526SAjay Kumar Gupta /* mode 5 - fits in 8KB */
1342d3608b6dSBill Pemberton static struct musb_fifo_cfg mode_5_cfg[] = {
13433b151526SAjay Kumar Gupta { .hw_ep_num =  1, .style = FIFO_TX,   .maxpacket = 512, },
13443b151526SAjay Kumar Gupta { .hw_ep_num =  1, .style = FIFO_RX,   .maxpacket = 512, },
13453b151526SAjay Kumar Gupta { .hw_ep_num =  2, .style = FIFO_TX,   .maxpacket = 512, },
13463b151526SAjay Kumar Gupta { .hw_ep_num =  2, .style = FIFO_RX,   .maxpacket = 512, },
13473b151526SAjay Kumar Gupta { .hw_ep_num =  3, .style = FIFO_TX,   .maxpacket = 512, },
13483b151526SAjay Kumar Gupta { .hw_ep_num =  3, .style = FIFO_RX,   .maxpacket = 512, },
13493b151526SAjay Kumar Gupta { .hw_ep_num =  4, .style = FIFO_TX,   .maxpacket = 512, },
13503b151526SAjay Kumar Gupta { .hw_ep_num =  4, .style = FIFO_RX,   .maxpacket = 512, },
13513b151526SAjay Kumar Gupta { .hw_ep_num =  5, .style = FIFO_TX,   .maxpacket = 512, },
13523b151526SAjay Kumar Gupta { .hw_ep_num =  5, .style = FIFO_RX,   .maxpacket = 512, },
13533b151526SAjay Kumar Gupta { .hw_ep_num =  6, .style = FIFO_TX,   .maxpacket = 32, },
13543b151526SAjay Kumar Gupta { .hw_ep_num =  6, .style = FIFO_RX,   .maxpacket = 32, },
13553b151526SAjay Kumar Gupta { .hw_ep_num =  7, .style = FIFO_TX,   .maxpacket = 32, },
13563b151526SAjay Kumar Gupta { .hw_ep_num =  7, .style = FIFO_RX,   .maxpacket = 32, },
13573b151526SAjay Kumar Gupta { .hw_ep_num =  8, .style = FIFO_TX,   .maxpacket = 32, },
13583b151526SAjay Kumar Gupta { .hw_ep_num =  8, .style = FIFO_RX,   .maxpacket = 32, },
13593b151526SAjay Kumar Gupta { .hw_ep_num =  9, .style = FIFO_TX,   .maxpacket = 32, },
13603b151526SAjay Kumar Gupta { .hw_ep_num =  9, .style = FIFO_RX,   .maxpacket = 32, },
13613b151526SAjay Kumar Gupta { .hw_ep_num = 10, .style = FIFO_TX,   .maxpacket = 32, },
13623b151526SAjay Kumar Gupta { .hw_ep_num = 10, .style = FIFO_RX,   .maxpacket = 32, },
13633b151526SAjay Kumar Gupta { .hw_ep_num = 11, .style = FIFO_TX,   .maxpacket = 32, },
13643b151526SAjay Kumar Gupta { .hw_ep_num = 11, .style = FIFO_RX,   .maxpacket = 32, },
13653b151526SAjay Kumar Gupta { .hw_ep_num = 12, .style = FIFO_TX,   .maxpacket = 32, },
13663b151526SAjay Kumar Gupta { .hw_ep_num = 12, .style = FIFO_RX,   .maxpacket = 32, },
13673b151526SAjay Kumar Gupta { .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
13683b151526SAjay Kumar Gupta { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
13693b151526SAjay Kumar Gupta { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
13703b151526SAjay Kumar Gupta };
1371550a7375SFelipe Balbi 
1372550a7375SFelipe Balbi /*
1373550a7375SFelipe Balbi  * configure a fifo; for non-shared endpoints, this may be called
1374550a7375SFelipe Balbi  * once for a tx fifo and once for an rx fifo.
1375550a7375SFelipe Balbi  *
1376550a7375SFelipe Balbi  * returns negative errno or offset for next fifo.
1377550a7375SFelipe Balbi  */
137841ac7b3aSBill Pemberton static int
fifo_setup(struct musb * musb,struct musb_hw_ep * hw_ep,const struct musb_fifo_cfg * cfg,u16 offset)1379550a7375SFelipe Balbi fifo_setup(struct musb *musb, struct musb_hw_ep  *hw_ep,
1380e6c213b2SFelipe Balbi 		const struct musb_fifo_cfg *cfg, u16 offset)
1381550a7375SFelipe Balbi {
1382550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
1383550a7375SFelipe Balbi 	int	size = 0;
1384550a7375SFelipe Balbi 	u16	maxpacket = cfg->maxpacket;
1385550a7375SFelipe Balbi 	u16	c_off = offset >> 3;
1386550a7375SFelipe Balbi 	u8	c_size;
1387550a7375SFelipe Balbi 
1388550a7375SFelipe Balbi 	/* expect hw_ep has already been zero-initialized */
1389550a7375SFelipe Balbi 
1390550a7375SFelipe Balbi 	size = ffs(max(maxpacket, (u16) 8)) - 1;
1391550a7375SFelipe Balbi 	maxpacket = 1 << size;
1392550a7375SFelipe Balbi 
1393550a7375SFelipe Balbi 	c_size = size - 3;
1394550a7375SFelipe Balbi 	if (cfg->mode == BUF_DOUBLE) {
1395ca6d1b13SFelipe Balbi 		if ((offset + (maxpacket << 1)) >
1396ca6d1b13SFelipe Balbi 				(1 << (musb->config->ram_bits + 2)))
1397550a7375SFelipe Balbi 			return -EMSGSIZE;
1398550a7375SFelipe Balbi 		c_size |= MUSB_FIFOSZ_DPB;
1399550a7375SFelipe Balbi 	} else {
1400ca6d1b13SFelipe Balbi 		if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
1401550a7375SFelipe Balbi 			return -EMSGSIZE;
1402550a7375SFelipe Balbi 	}
1403550a7375SFelipe Balbi 
1404550a7375SFelipe Balbi 	/* configure the FIFO */
1405550a7375SFelipe Balbi 	musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1406550a7375SFelipe Balbi 
1407550a7375SFelipe Balbi 	/* EP0 reserved endpoint for control, bidirectional;
14085ae477b0SRahul Bedarkar 	 * EP1 reserved for bulk, two unidirectional halves.
1409550a7375SFelipe Balbi 	 */
1410550a7375SFelipe Balbi 	if (hw_ep->epnum == 1)
1411550a7375SFelipe Balbi 		musb->bulk_ep = hw_ep;
1412550a7375SFelipe Balbi 	/* REVISIT error check:  be sure ep0 can both rx and tx ... */
1413550a7375SFelipe Balbi 	switch (cfg->style) {
1414550a7375SFelipe Balbi 	case FIFO_TX:
1415113ad151SBin Liu 		musb_writeb(mbase, MUSB_TXFIFOSZ, c_size);
1416113ad151SBin Liu 		musb_writew(mbase, MUSB_TXFIFOADD, c_off);
1417550a7375SFelipe Balbi 		hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1418550a7375SFelipe Balbi 		hw_ep->max_packet_sz_tx = maxpacket;
1419550a7375SFelipe Balbi 		break;
1420550a7375SFelipe Balbi 	case FIFO_RX:
1421113ad151SBin Liu 		musb_writeb(mbase, MUSB_RXFIFOSZ, c_size);
1422113ad151SBin Liu 		musb_writew(mbase, MUSB_RXFIFOADD, c_off);
1423550a7375SFelipe Balbi 		hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1424550a7375SFelipe Balbi 		hw_ep->max_packet_sz_rx = maxpacket;
1425550a7375SFelipe Balbi 		break;
1426550a7375SFelipe Balbi 	case FIFO_RXTX:
1427113ad151SBin Liu 		musb_writeb(mbase, MUSB_TXFIFOSZ, c_size);
1428113ad151SBin Liu 		musb_writew(mbase, MUSB_TXFIFOADD, c_off);
1429550a7375SFelipe Balbi 		hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1430550a7375SFelipe Balbi 		hw_ep->max_packet_sz_rx = maxpacket;
1431550a7375SFelipe Balbi 
1432113ad151SBin Liu 		musb_writeb(mbase, MUSB_RXFIFOSZ, c_size);
1433113ad151SBin Liu 		musb_writew(mbase, MUSB_RXFIFOADD, c_off);
1434550a7375SFelipe Balbi 		hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1435550a7375SFelipe Balbi 		hw_ep->max_packet_sz_tx = maxpacket;
1436550a7375SFelipe Balbi 
1437550a7375SFelipe Balbi 		hw_ep->is_shared_fifo = true;
1438550a7375SFelipe Balbi 		break;
1439550a7375SFelipe Balbi 	}
1440550a7375SFelipe Balbi 
1441550a7375SFelipe Balbi 	/* NOTE rx and tx endpoint irqs aren't managed separately,
1442550a7375SFelipe Balbi 	 * which happens to be ok
1443550a7375SFelipe Balbi 	 */
1444550a7375SFelipe Balbi 	musb->epmask |= (1 << hw_ep->epnum);
1445550a7375SFelipe Balbi 
1446550a7375SFelipe Balbi 	return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1447550a7375SFelipe Balbi }
1448550a7375SFelipe Balbi 
1449d3608b6dSBill Pemberton static struct musb_fifo_cfg ep0_cfg = {
1450550a7375SFelipe Balbi 	.style = FIFO_RXTX, .maxpacket = 64,
1451550a7375SFelipe Balbi };
1452550a7375SFelipe Balbi 
ep_config_from_table(struct musb * musb)145341ac7b3aSBill Pemberton static int ep_config_from_table(struct musb *musb)
1454550a7375SFelipe Balbi {
1455e6c213b2SFelipe Balbi 	const struct musb_fifo_cfg	*cfg;
1456550a7375SFelipe Balbi 	unsigned		i, n;
1457550a7375SFelipe Balbi 	int			offset;
1458550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints;
1459550a7375SFelipe Balbi 
1460e6c213b2SFelipe Balbi 	if (musb->config->fifo_cfg) {
1461e6c213b2SFelipe Balbi 		cfg = musb->config->fifo_cfg;
1462e6c213b2SFelipe Balbi 		n = musb->config->fifo_cfg_size;
1463e6c213b2SFelipe Balbi 		goto done;
1464e6c213b2SFelipe Balbi 	}
1465e6c213b2SFelipe Balbi 
1466550a7375SFelipe Balbi 	switch (fifo_mode) {
1467550a7375SFelipe Balbi 	default:
1468550a7375SFelipe Balbi 		fifo_mode = 0;
1469df561f66SGustavo A. R. Silva 		fallthrough;
1470550a7375SFelipe Balbi 	case 0:
1471550a7375SFelipe Balbi 		cfg = mode_0_cfg;
1472550a7375SFelipe Balbi 		n = ARRAY_SIZE(mode_0_cfg);
1473550a7375SFelipe Balbi 		break;
1474550a7375SFelipe Balbi 	case 1:
1475550a7375SFelipe Balbi 		cfg = mode_1_cfg;
1476550a7375SFelipe Balbi 		n = ARRAY_SIZE(mode_1_cfg);
1477550a7375SFelipe Balbi 		break;
1478550a7375SFelipe Balbi 	case 2:
1479550a7375SFelipe Balbi 		cfg = mode_2_cfg;
1480550a7375SFelipe Balbi 		n = ARRAY_SIZE(mode_2_cfg);
1481550a7375SFelipe Balbi 		break;
1482550a7375SFelipe Balbi 	case 3:
1483550a7375SFelipe Balbi 		cfg = mode_3_cfg;
1484550a7375SFelipe Balbi 		n = ARRAY_SIZE(mode_3_cfg);
1485550a7375SFelipe Balbi 		break;
1486550a7375SFelipe Balbi 	case 4:
1487550a7375SFelipe Balbi 		cfg = mode_4_cfg;
1488550a7375SFelipe Balbi 		n = ARRAY_SIZE(mode_4_cfg);
1489550a7375SFelipe Balbi 		break;
14903b151526SAjay Kumar Gupta 	case 5:
14913b151526SAjay Kumar Gupta 		cfg = mode_5_cfg;
14923b151526SAjay Kumar Gupta 		n = ARRAY_SIZE(mode_5_cfg);
14933b151526SAjay Kumar Gupta 		break;
1494550a7375SFelipe Balbi 	}
1495550a7375SFelipe Balbi 
14963ff4b573SRasmus Villemoes 	pr_debug("%s: setup fifo_mode %d\n", musb_driver_name, fifo_mode);
1497550a7375SFelipe Balbi 
1498550a7375SFelipe Balbi 
1499e6c213b2SFelipe Balbi done:
1500550a7375SFelipe Balbi 	offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1501550a7375SFelipe Balbi 	/* assert(offset > 0) */
1502550a7375SFelipe Balbi 
1503550a7375SFelipe Balbi 	/* NOTE:  for RTL versions >= 1.400 EPINFO and RAMINFO would
1504ca6d1b13SFelipe Balbi 	 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
1505550a7375SFelipe Balbi 	 */
1506550a7375SFelipe Balbi 
1507550a7375SFelipe Balbi 	for (i = 0; i < n; i++) {
1508550a7375SFelipe Balbi 		u8	epn = cfg->hw_ep_num;
1509550a7375SFelipe Balbi 
1510ca6d1b13SFelipe Balbi 		if (epn >= musb->config->num_eps) {
1511550a7375SFelipe Balbi 			pr_debug("%s: invalid ep %d\n",
1512550a7375SFelipe Balbi 					musb_driver_name, epn);
1513bb1c9ef1SDavid Brownell 			return -EINVAL;
1514550a7375SFelipe Balbi 		}
1515550a7375SFelipe Balbi 		offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1516550a7375SFelipe Balbi 		if (offset < 0) {
1517550a7375SFelipe Balbi 			pr_debug("%s: mem overrun, ep %d\n",
1518550a7375SFelipe Balbi 					musb_driver_name, epn);
1519f69dfa1fSShubhrajyoti D 			return offset;
1520550a7375SFelipe Balbi 		}
1521550a7375SFelipe Balbi 		epn++;
1522550a7375SFelipe Balbi 		musb->nr_endpoints = max(epn, musb->nr_endpoints);
1523550a7375SFelipe Balbi 	}
1524550a7375SFelipe Balbi 
15253ff4b573SRasmus Villemoes 	pr_debug("%s: %d/%d max ep, %d/%d memory\n",
1526550a7375SFelipe Balbi 			musb_driver_name,
1527ca6d1b13SFelipe Balbi 			n + 1, musb->config->num_eps * 2 - 1,
1528ca6d1b13SFelipe Balbi 			offset, (1 << (musb->config->ram_bits + 2)));
1529550a7375SFelipe Balbi 
1530550a7375SFelipe Balbi 	if (!musb->bulk_ep) {
1531550a7375SFelipe Balbi 		pr_debug("%s: missing bulk\n", musb_driver_name);
1532550a7375SFelipe Balbi 		return -EINVAL;
1533550a7375SFelipe Balbi 	}
1534550a7375SFelipe Balbi 
1535550a7375SFelipe Balbi 	return 0;
1536550a7375SFelipe Balbi }
1537550a7375SFelipe Balbi 
1538550a7375SFelipe Balbi 
1539550a7375SFelipe Balbi /*
1540550a7375SFelipe Balbi  * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1541550a7375SFelipe Balbi  * @param musb the controller
1542550a7375SFelipe Balbi  */
ep_config_from_hw(struct musb * musb)154341ac7b3aSBill Pemberton static int ep_config_from_hw(struct musb *musb)
1544550a7375SFelipe Balbi {
1545c6cf8b00SBryan Wu 	u8 epnum = 0;
1546550a7375SFelipe Balbi 	struct musb_hw_ep *hw_ep;
1547a156544bSFelipe Balbi 	void __iomem *mbase = musb->mregs;
1548c6cf8b00SBryan Wu 	int ret = 0;
1549550a7375SFelipe Balbi 
1550b99d3659SBin Liu 	musb_dbg(musb, "<== static silicon ep config");
1551550a7375SFelipe Balbi 
1552550a7375SFelipe Balbi 	/* FIXME pick up ep0 maxpacket size */
1553550a7375SFelipe Balbi 
1554ca6d1b13SFelipe Balbi 	for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
1555550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1556550a7375SFelipe Balbi 		hw_ep = musb->endpoints + epnum;
1557550a7375SFelipe Balbi 
1558c6cf8b00SBryan Wu 		ret = musb_read_fifosize(musb, hw_ep, epnum);
1559c6cf8b00SBryan Wu 		if (ret < 0)
1560550a7375SFelipe Balbi 			break;
1561550a7375SFelipe Balbi 
1562550a7375SFelipe Balbi 		/* FIXME set up hw_ep->{rx,tx}_double_buffered */
1563550a7375SFelipe Balbi 
1564550a7375SFelipe Balbi 		/* pick an RX/TX endpoint for bulk */
1565550a7375SFelipe Balbi 		if (hw_ep->max_packet_sz_tx < 512
1566550a7375SFelipe Balbi 				|| hw_ep->max_packet_sz_rx < 512)
1567550a7375SFelipe Balbi 			continue;
1568550a7375SFelipe Balbi 
1569550a7375SFelipe Balbi 		/* REVISIT:  this algorithm is lazy, we should at least
1570550a7375SFelipe Balbi 		 * try to pick a double buffered endpoint.
1571550a7375SFelipe Balbi 		 */
1572550a7375SFelipe Balbi 		if (musb->bulk_ep)
1573550a7375SFelipe Balbi 			continue;
1574550a7375SFelipe Balbi 		musb->bulk_ep = hw_ep;
1575550a7375SFelipe Balbi 	}
1576550a7375SFelipe Balbi 
1577550a7375SFelipe Balbi 	if (!musb->bulk_ep) {
1578550a7375SFelipe Balbi 		pr_debug("%s: missing bulk\n", musb_driver_name);
1579550a7375SFelipe Balbi 		return -EINVAL;
1580550a7375SFelipe Balbi 	}
1581550a7375SFelipe Balbi 
1582550a7375SFelipe Balbi 	return 0;
1583550a7375SFelipe Balbi }
1584550a7375SFelipe Balbi 
1585550a7375SFelipe Balbi enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1586550a7375SFelipe Balbi 
1587550a7375SFelipe Balbi /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1588550a7375SFelipe Balbi  * configure endpoints, or take their config from silicon
1589550a7375SFelipe Balbi  */
musb_core_init(u16 musb_type,struct musb * musb)159041ac7b3aSBill Pemberton static int musb_core_init(u16 musb_type, struct musb *musb)
1591550a7375SFelipe Balbi {
1592550a7375SFelipe Balbi 	u8 reg;
1593550a7375SFelipe Balbi 	char *type;
159421b031fbSRasmus Villemoes 	char aInfo[90];
1595550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
1596550a7375SFelipe Balbi 	int		status = 0;
1597550a7375SFelipe Balbi 	int		i;
1598550a7375SFelipe Balbi 
1599550a7375SFelipe Balbi 	/* log core options (read using indexed model) */
1600c6cf8b00SBryan Wu 	reg = musb_read_configdata(mbase);
1601550a7375SFelipe Balbi 
1602550a7375SFelipe Balbi 	strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
160351bf0d0eSAjay Kumar Gupta 	if (reg & MUSB_CONFIGDATA_DYNFIFO) {
1604550a7375SFelipe Balbi 		strcat(aInfo, ", dyn FIFOs");
160551bf0d0eSAjay Kumar Gupta 		musb->dyn_fifo = true;
160651bf0d0eSAjay Kumar Gupta 	}
1607550a7375SFelipe Balbi 	if (reg & MUSB_CONFIGDATA_MPRXE) {
1608550a7375SFelipe Balbi 		strcat(aInfo, ", bulk combine");
1609550a7375SFelipe Balbi 		musb->bulk_combine = true;
1610550a7375SFelipe Balbi 	}
1611550a7375SFelipe Balbi 	if (reg & MUSB_CONFIGDATA_MPTXE) {
1612550a7375SFelipe Balbi 		strcat(aInfo, ", bulk split");
1613550a7375SFelipe Balbi 		musb->bulk_split = true;
1614550a7375SFelipe Balbi 	}
1615550a7375SFelipe Balbi 	if (reg & MUSB_CONFIGDATA_HBRXE) {
1616550a7375SFelipe Balbi 		strcat(aInfo, ", HB-ISO Rx");
1617a483d706SAjay Kumar Gupta 		musb->hb_iso_rx = true;
1618550a7375SFelipe Balbi 	}
1619550a7375SFelipe Balbi 	if (reg & MUSB_CONFIGDATA_HBTXE) {
1620550a7375SFelipe Balbi 		strcat(aInfo, ", HB-ISO Tx");
1621a483d706SAjay Kumar Gupta 		musb->hb_iso_tx = true;
1622550a7375SFelipe Balbi 	}
1623550a7375SFelipe Balbi 	if (reg & MUSB_CONFIGDATA_SOFTCONE)
1624550a7375SFelipe Balbi 		strcat(aInfo, ", SoftConn");
1625550a7375SFelipe Balbi 
16263ff4b573SRasmus Villemoes 	pr_debug("%s: ConfigData=0x%02x (%s)\n", musb_driver_name, reg, aInfo);
1627550a7375SFelipe Balbi 
1628550a7375SFelipe Balbi 	if (MUSB_CONTROLLER_MHDRC == musb_type) {
1629550a7375SFelipe Balbi 		musb->is_multipoint = 1;
1630550a7375SFelipe Balbi 		type = "M";
1631550a7375SFelipe Balbi 	} else {
1632550a7375SFelipe Balbi 		musb->is_multipoint = 0;
1633550a7375SFelipe Balbi 		type = "";
163441386bc8SPaul Cercueil 		if (IS_ENABLED(CONFIG_USB) &&
16359af54301SGreg Kroah-Hartman 		    !IS_ENABLED(CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB)) {
16369af54301SGreg Kroah-Hartman 			pr_err("%s: kernel must disable external hubs, please fix the configuration\n",
1637550a7375SFelipe Balbi 			       musb_driver_name);
163841386bc8SPaul Cercueil 		}
1639550a7375SFelipe Balbi 	}
1640550a7375SFelipe Balbi 
1641550a7375SFelipe Balbi 	/* log release info */
1642113ad151SBin Liu 	musb->hwvers = musb_readw(mbase, MUSB_HWVERS);
164321b031fbSRasmus Villemoes 	pr_debug("%s: %sHDRC RTL version %d.%d%s\n",
164421b031fbSRasmus Villemoes 		 musb_driver_name, type, MUSB_HWVERS_MAJOR(musb->hwvers),
164532c3b94eSAnand Gadiyar 		 MUSB_HWVERS_MINOR(musb->hwvers),
164632c3b94eSAnand Gadiyar 		 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
1647550a7375SFelipe Balbi 
1648550a7375SFelipe Balbi 	/* configure ep0 */
1649c6cf8b00SBryan Wu 	musb_configure_ep0(musb);
1650550a7375SFelipe Balbi 
1651550a7375SFelipe Balbi 	/* discover endpoint configuration */
1652550a7375SFelipe Balbi 	musb->nr_endpoints = 1;
1653550a7375SFelipe Balbi 	musb->epmask = 1;
1654550a7375SFelipe Balbi 
1655ad517e9eSFelipe Balbi 	if (musb->dyn_fifo)
1656550a7375SFelipe Balbi 		status = ep_config_from_table(musb);
1657ad517e9eSFelipe Balbi 	else
1658550a7375SFelipe Balbi 		status = ep_config_from_hw(musb);
1659550a7375SFelipe Balbi 
1660550a7375SFelipe Balbi 	if (status < 0)
1661550a7375SFelipe Balbi 		return status;
1662550a7375SFelipe Balbi 
1663550a7375SFelipe Balbi 	/* finish init, and print endpoint config */
1664550a7375SFelipe Balbi 	for (i = 0; i < musb->nr_endpoints; i++) {
1665550a7375SFelipe Balbi 		struct musb_hw_ep	*hw_ep = musb->endpoints + i;
1666550a7375SFelipe Balbi 
16671b40fc57STony Lindgren 		hw_ep->fifo = musb->io.fifo_offset(i) + mbase;
1668ebf39920STony Lindgren #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
1669dc8fca6cSBin Liu 		if (musb->ops->quirks & MUSB_IN_TUSB) {
16701b40fc57STony Lindgren 			hw_ep->fifo_async = musb->async + 0x400 +
16711b40fc57STony Lindgren 				musb->io.fifo_offset(i);
16721b40fc57STony Lindgren 			hw_ep->fifo_sync = musb->sync + 0x400 +
16731b40fc57STony Lindgren 				musb->io.fifo_offset(i);
1674550a7375SFelipe Balbi 			hw_ep->fifo_sync_va =
16751b40fc57STony Lindgren 				musb->sync_va + 0x400 + musb->io.fifo_offset(i);
1676550a7375SFelipe Balbi 
1677550a7375SFelipe Balbi 			if (i == 0)
1678550a7375SFelipe Balbi 				hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1679550a7375SFelipe Balbi 			else
16801b40fc57STony Lindgren 				hw_ep->conf = mbase + 0x400 +
16811b40fc57STony Lindgren 					(((i - 1) & 0xf) << 2);
16821b40fc57STony Lindgren 		}
1683550a7375SFelipe Balbi #endif
1684550a7375SFelipe Balbi 
1685d026e9c7STony Lindgren 		hw_ep->regs = musb->io.ep_offset(i, 0) + mbase;
1686550a7375SFelipe Balbi 		hw_ep->rx_reinit = 1;
1687550a7375SFelipe Balbi 		hw_ep->tx_reinit = 1;
1688550a7375SFelipe Balbi 
1689550a7375SFelipe Balbi 		if (hw_ep->max_packet_sz_tx) {
1690b99d3659SBin Liu 			musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
1691550a7375SFelipe Balbi 				musb_driver_name, i,
1692550a7375SFelipe Balbi 				hw_ep->is_shared_fifo ? "shared" : "tx",
1693550a7375SFelipe Balbi 				hw_ep->tx_double_buffered
1694550a7375SFelipe Balbi 					? "doublebuffer, " : "",
1695550a7375SFelipe Balbi 				hw_ep->max_packet_sz_tx);
1696550a7375SFelipe Balbi 		}
1697550a7375SFelipe Balbi 		if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
1698b99d3659SBin Liu 			musb_dbg(musb, "%s: hw_ep %d%s, %smax %d",
1699550a7375SFelipe Balbi 				musb_driver_name, i,
1700550a7375SFelipe Balbi 				"rx",
1701550a7375SFelipe Balbi 				hw_ep->rx_double_buffered
1702550a7375SFelipe Balbi 					? "doublebuffer, " : "",
1703550a7375SFelipe Balbi 				hw_ep->max_packet_sz_rx);
1704550a7375SFelipe Balbi 		}
1705550a7375SFelipe Balbi 		if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1706b99d3659SBin Liu 			musb_dbg(musb, "hw_ep %d not configured", i);
1707550a7375SFelipe Balbi 	}
1708550a7375SFelipe Balbi 
1709550a7375SFelipe Balbi 	return 0;
1710550a7375SFelipe Balbi }
1711550a7375SFelipe Balbi 
1712550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
1713550a7375SFelipe Balbi 
1714550a7375SFelipe Balbi /*
1715550a7375SFelipe Balbi  * handle all the irqs defined by the HDRC core. for now we expect:  other
1716550a7375SFelipe Balbi  * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1717550a7375SFelipe Balbi  * will be assigned, and the irq will already have been acked.
1718550a7375SFelipe Balbi  *
1719550a7375SFelipe Balbi  * called in irq context with spinlock held, irqs blocked
1720550a7375SFelipe Balbi  */
musb_interrupt(struct musb * musb)1721550a7375SFelipe Balbi irqreturn_t musb_interrupt(struct musb *musb)
1722550a7375SFelipe Balbi {
1723550a7375SFelipe Balbi 	irqreturn_t	retval = IRQ_NONE;
172431a0ede0SFelipe Balbi 	unsigned long	status;
172531a0ede0SFelipe Balbi 	unsigned long	epnum;
1726b11e94d0SSebastian Andrzej Siewior 	u8		devctl;
172731a0ede0SFelipe Balbi 
172831a0ede0SFelipe Balbi 	if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
172931a0ede0SFelipe Balbi 		return IRQ_NONE;
1730550a7375SFelipe Balbi 
1731550a7375SFelipe Balbi 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1732550a7375SFelipe Balbi 
1733cfb9a1bcSBin Liu 	trace_musb_isr(musb);
1734550a7375SFelipe Balbi 
1735e3c93e1aSFelipe Balbi 	/**
1736e3c93e1aSFelipe Balbi 	 * According to Mentor Graphics' documentation, flowchart on page 98,
1737e3c93e1aSFelipe Balbi 	 * IRQ should be handled as follows:
1738e3c93e1aSFelipe Balbi 	 *
1739e3c93e1aSFelipe Balbi 	 * . Resume IRQ
1740e3c93e1aSFelipe Balbi 	 * . Session Request IRQ
1741e3c93e1aSFelipe Balbi 	 * . VBUS Error IRQ
1742e3c93e1aSFelipe Balbi 	 * . Suspend IRQ
1743e3c93e1aSFelipe Balbi 	 * . Connect IRQ
1744e3c93e1aSFelipe Balbi 	 * . Disconnect IRQ
1745e3c93e1aSFelipe Balbi 	 * . Reset/Babble IRQ
1746e3c93e1aSFelipe Balbi 	 * . SOF IRQ (we're not using this one)
1747e3c93e1aSFelipe Balbi 	 * . Endpoint 0 IRQ
1748e3c93e1aSFelipe Balbi 	 * . TX Endpoints
1749e3c93e1aSFelipe Balbi 	 * . RX Endpoints
1750e3c93e1aSFelipe Balbi 	 *
1751e3c93e1aSFelipe Balbi 	 * We will be following that flowchart in order to avoid any problems
1752e3c93e1aSFelipe Balbi 	 * that might arise with internal Finite State Machine.
1753550a7375SFelipe Balbi 	 */
1754e3c93e1aSFelipe Balbi 
17557d9645fdSSergei Shtylyov 	if (musb->int_usb)
175631a0ede0SFelipe Balbi 		retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
1757550a7375SFelipe Balbi 
1758550a7375SFelipe Balbi 	if (musb->int_tx & 1) {
1759c03da38dSDaniel Mack 		if (is_host_active(musb))
1760550a7375SFelipe Balbi 			retval |= musb_h_ep0_irq(musb);
1761550a7375SFelipe Balbi 		else
1762550a7375SFelipe Balbi 			retval |= musb_g_ep0_irq(musb);
176331a0ede0SFelipe Balbi 
176431a0ede0SFelipe Balbi 		/* we have just handled endpoint 0 IRQ, clear it */
176531a0ede0SFelipe Balbi 		musb->int_tx &= ~BIT(0);
1766550a7375SFelipe Balbi 	}
1767550a7375SFelipe Balbi 
176831a0ede0SFelipe Balbi 	status = musb->int_tx;
176931a0ede0SFelipe Balbi 
177031a0ede0SFelipe Balbi 	for_each_set_bit(epnum, &status, 16) {
1771e3c93e1aSFelipe Balbi 		retval = IRQ_HANDLED;
1772e3c93e1aSFelipe Balbi 		if (is_host_active(musb))
177331a0ede0SFelipe Balbi 			musb_host_tx(musb, epnum);
1774e3c93e1aSFelipe Balbi 		else
177531a0ede0SFelipe Balbi 			musb_g_tx(musb, epnum);
1776e3c93e1aSFelipe Balbi 	}
1777e3c93e1aSFelipe Balbi 
177831a0ede0SFelipe Balbi 	status = musb->int_rx;
177931a0ede0SFelipe Balbi 
178031a0ede0SFelipe Balbi 	for_each_set_bit(epnum, &status, 16) {
1781550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1782c03da38dSDaniel Mack 		if (is_host_active(musb))
178331a0ede0SFelipe Balbi 			musb_host_rx(musb, epnum);
1784a04d46d0SFelipe Balbi 		else
178531a0ede0SFelipe Balbi 			musb_g_rx(musb, epnum);
1786550a7375SFelipe Balbi 	}
1787550a7375SFelipe Balbi 
1788550a7375SFelipe Balbi 	return retval;
1789550a7375SFelipe Balbi }
1790981430a1SFelipe Balbi EXPORT_SYMBOL_GPL(musb_interrupt);
1791550a7375SFelipe Balbi 
1792550a7375SFelipe Balbi #ifndef CONFIG_MUSB_PIO_ONLY
1793e62361c7SJason Yan static bool use_dma = true;
1794550a7375SFelipe Balbi 
1795550a7375SFelipe Balbi /* "modprobe ... use_dma=0" etc */
179651676c8dSBin Liu module_param(use_dma, bool, 0644);
1797550a7375SFelipe Balbi MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1798550a7375SFelipe Balbi 
musb_dma_completion(struct musb * musb,u8 epnum,u8 transmit)1799550a7375SFelipe Balbi void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1800550a7375SFelipe Balbi {
1801550a7375SFelipe Balbi 	/* called with controller lock already held */
1802550a7375SFelipe Balbi 
1803550a7375SFelipe Balbi 	if (!epnum) {
1804f8e9f34fSTony Lindgren 		if (!is_cppi_enabled(musb)) {
1805550a7375SFelipe Balbi 			/* endpoint 0 */
1806c03da38dSDaniel Mack 			if (is_host_active(musb))
1807550a7375SFelipe Balbi 				musb_h_ep0_irq(musb);
1808550a7375SFelipe Balbi 			else
1809550a7375SFelipe Balbi 				musb_g_ep0_irq(musb);
1810550a7375SFelipe Balbi 		}
1811550a7375SFelipe Balbi 	} else {
1812550a7375SFelipe Balbi 		/* endpoints 1..15 */
1813550a7375SFelipe Balbi 		if (transmit) {
1814c03da38dSDaniel Mack 			if (is_host_active(musb))
1815550a7375SFelipe Balbi 				musb_host_tx(musb, epnum);
1816a04d46d0SFelipe Balbi 			else
1817550a7375SFelipe Balbi 				musb_g_tx(musb, epnum);
1818550a7375SFelipe Balbi 		} else {
1819550a7375SFelipe Balbi 			/* receive */
1820c03da38dSDaniel Mack 			if (is_host_active(musb))
1821550a7375SFelipe Balbi 				musb_host_rx(musb, epnum);
1822a04d46d0SFelipe Balbi 			else
1823550a7375SFelipe Balbi 				musb_g_rx(musb, epnum);
1824550a7375SFelipe Balbi 		}
1825550a7375SFelipe Balbi 	}
1826550a7375SFelipe Balbi }
18279a35f876SArnd Bergmann EXPORT_SYMBOL_GPL(musb_dma_completion);
1828550a7375SFelipe Balbi 
1829550a7375SFelipe Balbi #else
1830550a7375SFelipe Balbi #define use_dma			0
1831550a7375SFelipe Balbi #endif
1832550a7375SFelipe Balbi 
183312b7db2bSTony Lindgren static int (*musb_phy_callback)(enum musb_vbus_id_status status);
18348055555fSTony Lindgren 
18358055555fSTony Lindgren /*
18368055555fSTony Lindgren  * musb_mailbox - optional phy notifier function
18378055555fSTony Lindgren  * @status phy state change
18388055555fSTony Lindgren  *
18398055555fSTony Lindgren  * Optionally gets called from the USB PHY. Note that the USB PHY must be
18408055555fSTony Lindgren  * disabled at the point the phy_callback is registered or unregistered.
18418055555fSTony Lindgren  */
musb_mailbox(enum musb_vbus_id_status status)184212b7db2bSTony Lindgren int musb_mailbox(enum musb_vbus_id_status status)
18438055555fSTony Lindgren {
18448055555fSTony Lindgren 	if (musb_phy_callback)
184512b7db2bSTony Lindgren 		return musb_phy_callback(status);
18468055555fSTony Lindgren 
184712b7db2bSTony Lindgren 	return -ENODEV;
18488055555fSTony Lindgren };
18498055555fSTony Lindgren EXPORT_SYMBOL_GPL(musb_mailbox);
18508055555fSTony Lindgren 
1851550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
1852550a7375SFelipe Balbi 
1853550a7375SFelipe Balbi static ssize_t
mode_show(struct device * dev,struct device_attribute * attr,char * buf)1854ed5bd7a4SGreg Kroah-Hartman mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1855550a7375SFelipe Balbi {
1856550a7375SFelipe Balbi 	struct musb *musb = dev_to_musb(dev);
1857550a7375SFelipe Balbi 	unsigned long flags;
185882e17a09SColin Ian King 	int ret;
1859550a7375SFelipe Balbi 
1860550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1861285f28bfSPaul Cercueil 	ret = sprintf(buf, "%s\n", musb_otg_state_string(musb));
1862550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1863550a7375SFelipe Balbi 
1864550a7375SFelipe Balbi 	return ret;
1865550a7375SFelipe Balbi }
1866550a7375SFelipe Balbi 
1867550a7375SFelipe Balbi static ssize_t
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)1868ed5bd7a4SGreg Kroah-Hartman mode_store(struct device *dev, struct device_attribute *attr,
1869550a7375SFelipe Balbi 		const char *buf, size_t n)
1870550a7375SFelipe Balbi {
1871550a7375SFelipe Balbi 	struct musb	*musb = dev_to_musb(dev);
1872550a7375SFelipe Balbi 	unsigned long	flags;
187396a274d1SDavid Brownell 	int		status;
1874550a7375SFelipe Balbi 
1875550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
187696a274d1SDavid Brownell 	if (sysfs_streq(buf, "host"))
187796a274d1SDavid Brownell 		status = musb_platform_set_mode(musb, MUSB_HOST);
187896a274d1SDavid Brownell 	else if (sysfs_streq(buf, "peripheral"))
187996a274d1SDavid Brownell 		status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
188096a274d1SDavid Brownell 	else if (sysfs_streq(buf, "otg"))
188196a274d1SDavid Brownell 		status = musb_platform_set_mode(musb, MUSB_OTG);
188296a274d1SDavid Brownell 	else
188396a274d1SDavid Brownell 		status = -EINVAL;
1884550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1885550a7375SFelipe Balbi 
188696a274d1SDavid Brownell 	return (status == 0) ? n : status;
1887550a7375SFelipe Balbi }
1888ed5bd7a4SGreg Kroah-Hartman static DEVICE_ATTR_RW(mode);
1889550a7375SFelipe Balbi 
1890550a7375SFelipe Balbi static ssize_t
vbus_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)1891ed5bd7a4SGreg Kroah-Hartman vbus_store(struct device *dev, struct device_attribute *attr,
1892550a7375SFelipe Balbi 		const char *buf, size_t n)
1893550a7375SFelipe Balbi {
1894550a7375SFelipe Balbi 	struct musb	*musb = dev_to_musb(dev);
1895550a7375SFelipe Balbi 	unsigned long	flags;
1896550a7375SFelipe Balbi 	unsigned long	val;
1897550a7375SFelipe Balbi 
1898550a7375SFelipe Balbi 	if (sscanf(buf, "%lu", &val) < 1) {
1899b3b1cc3bSFelipe Balbi 		dev_err(dev, "Invalid VBUS timeout ms value\n");
1900550a7375SFelipe Balbi 		return -EINVAL;
1901550a7375SFelipe Balbi 	}
1902550a7375SFelipe Balbi 
1903550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1904f7f9d63eSDavid Brownell 	/* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1905f7f9d63eSDavid Brownell 	musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
190621acc656SPaul Cercueil 	if (musb_get_state(musb) == OTG_STATE_A_WAIT_BCON)
1907550a7375SFelipe Balbi 		musb->is_active = 0;
1908550a7375SFelipe Balbi 	musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1909550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1910550a7375SFelipe Balbi 
1911550a7375SFelipe Balbi 	return n;
1912550a7375SFelipe Balbi }
1913550a7375SFelipe Balbi 
1914550a7375SFelipe Balbi static ssize_t
vbus_show(struct device * dev,struct device_attribute * attr,char * buf)1915ed5bd7a4SGreg Kroah-Hartman vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1916550a7375SFelipe Balbi {
1917550a7375SFelipe Balbi 	struct musb	*musb = dev_to_musb(dev);
1918550a7375SFelipe Balbi 	unsigned long	flags;
1919550a7375SFelipe Balbi 	unsigned long	val;
1920550a7375SFelipe Balbi 	int		vbus;
19213bbafac8SRoman Alyautdin 	u8		devctl;
1922550a7375SFelipe Balbi 
1923df6b074dSMerlijn Wajer 	pm_runtime_get_sync(dev);
1924550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1925550a7375SFelipe Balbi 	val = musb->a_wait_bcon;
1926550a7375SFelipe Balbi 	vbus = musb_platform_get_vbus_status(musb);
19273bbafac8SRoman Alyautdin 	if (vbus < 0) {
19283bbafac8SRoman Alyautdin 		/* Use default MUSB method by means of DEVCTL register */
19293bbafac8SRoman Alyautdin 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
19303bbafac8SRoman Alyautdin 		if ((devctl & MUSB_DEVCTL_VBUS)
19313bbafac8SRoman Alyautdin 				== (3 << MUSB_DEVCTL_VBUS_SHIFT))
19323bbafac8SRoman Alyautdin 			vbus = 1;
19333bbafac8SRoman Alyautdin 		else
19343bbafac8SRoman Alyautdin 			vbus = 0;
19353bbafac8SRoman Alyautdin 	}
1936550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1937df6b074dSMerlijn Wajer 	pm_runtime_put_sync(dev);
1938550a7375SFelipe Balbi 
1939f7f9d63eSDavid Brownell 	return sprintf(buf, "Vbus %s, timeout %lu msec\n",
1940550a7375SFelipe Balbi 			vbus ? "on" : "off", val);
1941550a7375SFelipe Balbi }
1942ed5bd7a4SGreg Kroah-Hartman static DEVICE_ATTR_RW(vbus);
1943550a7375SFelipe Balbi 
1944550a7375SFelipe Balbi /* Gadget drivers can't know that a host is connected so they might want
1945550a7375SFelipe Balbi  * to start SRP, but users can.  This allows userspace to trigger SRP.
1946550a7375SFelipe Balbi  */
srp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t n)19476e4294d0SGreg Kroah-Hartman static ssize_t srp_store(struct device *dev, struct device_attribute *attr,
1948550a7375SFelipe Balbi 		const char *buf, size_t n)
1949550a7375SFelipe Balbi {
1950550a7375SFelipe Balbi 	struct musb	*musb = dev_to_musb(dev);
1951550a7375SFelipe Balbi 	unsigned short	srp;
1952550a7375SFelipe Balbi 
1953550a7375SFelipe Balbi 	if (sscanf(buf, "%hu", &srp) != 1
1954550a7375SFelipe Balbi 			|| (srp != 1)) {
1955b3b1cc3bSFelipe Balbi 		dev_err(dev, "SRP: Value must be 1\n");
1956550a7375SFelipe Balbi 		return -EINVAL;
1957550a7375SFelipe Balbi 	}
1958550a7375SFelipe Balbi 
1959550a7375SFelipe Balbi 	if (srp == 1)
1960550a7375SFelipe Balbi 		musb_g_wakeup(musb);
1961550a7375SFelipe Balbi 
1962550a7375SFelipe Balbi 	return n;
1963550a7375SFelipe Balbi }
19646e4294d0SGreg Kroah-Hartman static DEVICE_ATTR_WO(srp);
1965550a7375SFelipe Balbi 
1966d3b5e319SGreg Kroah-Hartman static struct attribute *musb_attrs[] = {
196794375751SFelipe Balbi 	&dev_attr_mode.attr,
196894375751SFelipe Balbi 	&dev_attr_vbus.attr,
196994375751SFelipe Balbi 	&dev_attr_srp.attr,
197094375751SFelipe Balbi 	NULL
197194375751SFelipe Balbi };
1972d3b5e319SGreg Kroah-Hartman ATTRIBUTE_GROUPS(musb);
197394375751SFelipe Balbi 
1974467d5c98STony Lindgren #define MUSB_QUIRK_B_INVALID_VBUS_91	(MUSB_DEVCTL_BDEVICE | \
1975467d5c98STony Lindgren 					 (2 << MUSB_DEVCTL_VBUS_SHIFT) | \
1976467d5c98STony Lindgren 					 MUSB_DEVCTL_SESSION)
19775fbf7a25STony Lindgren #define MUSB_QUIRK_B_DISCONNECT_99	(MUSB_DEVCTL_BDEVICE | \
19785fbf7a25STony Lindgren 					 (3 << MUSB_DEVCTL_VBUS_SHIFT) | \
19795fbf7a25STony Lindgren 					 MUSB_DEVCTL_SESSION)
1980467d5c98STony Lindgren #define MUSB_QUIRK_A_DISCONNECT_19	((3 << MUSB_DEVCTL_VBUS_SHIFT) | \
1981467d5c98STony Lindgren 					 MUSB_DEVCTL_SESSION)
1982467d5c98STony Lindgren 
musb_state_needs_recheck(struct musb * musb,u8 devctl,const char * desc)1983318324e6STony Lindgren static bool musb_state_needs_recheck(struct musb *musb, u8 devctl,
1984318324e6STony Lindgren 				     const char *desc)
1985e2ff8815STony Lindgren {
1986e2ff8815STony Lindgren 	if (musb->quirk_retries && !musb->flush_irq_work) {
1987318324e6STony Lindgren 		trace_musb_state(musb, devctl, desc);
1988e2ff8815STony Lindgren 		schedule_delayed_work(&musb->irq_work,
1989e2ff8815STony Lindgren 				      msecs_to_jiffies(1000));
1990e2ff8815STony Lindgren 		musb->quirk_retries--;
1991e2ff8815STony Lindgren 
1992e2ff8815STony Lindgren 		return true;
1993e2ff8815STony Lindgren 	}
1994e2ff8815STony Lindgren 
1995e2ff8815STony Lindgren 	return false;
1996e2ff8815STony Lindgren }
1997e2ff8815STony Lindgren 
1998467d5c98STony Lindgren /*
1999467d5c98STony Lindgren  * Check the musb devctl session bit to determine if we want to
2000467d5c98STony Lindgren  * allow PM runtime for the device. In general, we want to keep things
2001467d5c98STony Lindgren  * active when the session bit is set except after host disconnect.
2002467d5c98STony Lindgren  *
2003467d5c98STony Lindgren  * Only called from musb_irq_work. If this ever needs to get called
2004467d5c98STony Lindgren  * elsewhere, proper locking must be implemented for musb->session.
2005467d5c98STony Lindgren  */
musb_pm_runtime_check_session(struct musb * musb)2006467d5c98STony Lindgren static void musb_pm_runtime_check_session(struct musb *musb)
2007467d5c98STony Lindgren {
2008467d5c98STony Lindgren 	u8 devctl, s;
2009467d5c98STony Lindgren 	int error;
2010467d5c98STony Lindgren 
2011467d5c98STony Lindgren 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2012467d5c98STony Lindgren 
2013467d5c98STony Lindgren 	/* Handle session status quirks first */
2014467d5c98STony Lindgren 	s = MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV |
2015467d5c98STony Lindgren 		MUSB_DEVCTL_HR;
2016467d5c98STony Lindgren 	switch (devctl & ~s) {
20175fbf7a25STony Lindgren 	case MUSB_QUIRK_B_DISCONNECT_99:
2018318324e6STony Lindgren 		musb_state_needs_recheck(musb, devctl,
2019318324e6STony Lindgren 			"Poll devctl in case of suspend after disconnect");
2020b65ba0c3SThomas Petazzoni 		break;
2021467d5c98STony Lindgren 	case MUSB_QUIRK_B_INVALID_VBUS_91:
2022318324e6STony Lindgren 		if (musb_state_needs_recheck(musb, devctl,
2023e2ff8815STony Lindgren 				"Poll devctl on invalid vbus, assume no session"))
2024467d5c98STony Lindgren 			return;
2025df561f66SGustavo A. R. Silva 		fallthrough;
2026467d5c98STony Lindgren 	case MUSB_QUIRK_A_DISCONNECT_19:
2027318324e6STony Lindgren 		if (musb_state_needs_recheck(musb, devctl,
2028e2ff8815STony Lindgren 				"Poll devctl on possible host mode disconnect"))
20292bff3916STony Lindgren 			return;
2030467d5c98STony Lindgren 		if (!musb->session)
2031467d5c98STony Lindgren 			break;
2032318324e6STony Lindgren 		trace_musb_state(musb, devctl, "Allow PM on possible host mode disconnect");
2033467d5c98STony Lindgren 		pm_runtime_mark_last_busy(musb->controller);
2034467d5c98STony Lindgren 		pm_runtime_put_autosuspend(musb->controller);
2035467d5c98STony Lindgren 		musb->session = false;
2036467d5c98STony Lindgren 		return;
2037467d5c98STony Lindgren 	default:
2038467d5c98STony Lindgren 		break;
2039467d5c98STony Lindgren 	}
2040467d5c98STony Lindgren 
2041467d5c98STony Lindgren 	/* No need to do anything if session has not changed */
2042467d5c98STony Lindgren 	s = devctl & MUSB_DEVCTL_SESSION;
2043467d5c98STony Lindgren 	if (s == musb->session)
2044467d5c98STony Lindgren 		return;
2045467d5c98STony Lindgren 
2046467d5c98STony Lindgren 	/* Block PM or allow PM? */
2047467d5c98STony Lindgren 	if (s) {
2048318324e6STony Lindgren 		trace_musb_state(musb, devctl, "Block PM on active session");
2049467d5c98STony Lindgren 		error = pm_runtime_get_sync(musb->controller);
2050467d5c98STony Lindgren 		if (error < 0)
2051467d5c98STony Lindgren 			dev_err(musb->controller, "Could not enable: %i\n",
2052467d5c98STony Lindgren 				error);
20532bff3916STony Lindgren 		musb->quirk_retries = 3;
20547d076c2fSTony Lindgren 
20557d076c2fSTony Lindgren 		/*
20567d076c2fSTony Lindgren 		 * We can get a spurious MUSB_INTR_SESSREQ interrupt on start-up
20577d076c2fSTony Lindgren 		 * in B-peripheral mode with nothing connected and the session
20587d076c2fSTony Lindgren 		 * bit clears silently. Check status again in 3 seconds.
20597d076c2fSTony Lindgren 		 */
20607d076c2fSTony Lindgren 		if (devctl & MUSB_DEVCTL_BDEVICE)
20617d076c2fSTony Lindgren 			schedule_delayed_work(&musb->irq_work,
20627d076c2fSTony Lindgren 					      msecs_to_jiffies(3000));
2063467d5c98STony Lindgren 	} else {
2064318324e6STony Lindgren 		trace_musb_state(musb, devctl, "Allow PM with no session");
2065467d5c98STony Lindgren 		pm_runtime_mark_last_busy(musb->controller);
2066467d5c98STony Lindgren 		pm_runtime_put_autosuspend(musb->controller);
2067467d5c98STony Lindgren 	}
2068467d5c98STony Lindgren 
2069467d5c98STony Lindgren 	musb->session = s;
2070467d5c98STony Lindgren }
2071467d5c98STony Lindgren 
2072550a7375SFelipe Balbi /* Only used to provide driver mode change events */
musb_irq_work(struct work_struct * data)2073550a7375SFelipe Balbi static void musb_irq_work(struct work_struct *data)
2074550a7375SFelipe Balbi {
20752bff3916STony Lindgren 	struct musb *musb = container_of(data, struct musb, irq_work.work);
20763ba7b779STony Lindgren 	int error;
20773ba7b779STony Lindgren 
20789535b995SBixuan Cui 	error = pm_runtime_resume_and_get(musb->controller);
20793ba7b779STony Lindgren 	if (error < 0) {
20803ba7b779STony Lindgren 		dev_err(musb->controller, "Could not enable: %i\n", error);
20813ba7b779STony Lindgren 
20823ba7b779STony Lindgren 		return;
20833ba7b779STony Lindgren 	}
2084550a7375SFelipe Balbi 
2085467d5c98STony Lindgren 	musb_pm_runtime_check_session(musb);
2086467d5c98STony Lindgren 
208721acc656SPaul Cercueil 	if (musb_get_state(musb) != musb->xceiv_old_state) {
208821acc656SPaul Cercueil 		musb->xceiv_old_state = musb_get_state(musb);
2089550a7375SFelipe Balbi 		sysfs_notify(&musb->controller->kobj, NULL, "mode");
2090550a7375SFelipe Balbi 	}
20913ba7b779STony Lindgren 
20923ba7b779STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
20933ba7b779STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
2094550a7375SFelipe Balbi }
2095550a7375SFelipe Balbi 
musb_recover_from_babble(struct musb * musb)209683b8f5b8SFelipe Balbi static void musb_recover_from_babble(struct musb *musb)
2097ca88fc2eSDaniel Mack {
2098b4dc38fdSFelipe Balbi 	int ret;
2099b4dc38fdSFelipe Balbi 	u8 devctl;
2100ca88fc2eSDaniel Mack 
21010244336fSFelipe Balbi 	musb_disable_interrupts(musb);
21020244336fSFelipe Balbi 
210383b8f5b8SFelipe Balbi 	/*
210483b8f5b8SFelipe Balbi 	 * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
210583b8f5b8SFelipe Balbi 	 * it some slack and wait for 10us.
210683b8f5b8SFelipe Balbi 	 */
210783b8f5b8SFelipe Balbi 	udelay(10);
210883b8f5b8SFelipe Balbi 
2109b28a6432SFelipe Balbi 	ret  = musb_platform_recover(musb);
2110ba7ee8bbSFelipe Balbi 	if (ret) {
2111ba7ee8bbSFelipe Balbi 		musb_enable_interrupts(musb);
2112d871c622SGeorge Cherian 		return;
2113ba7ee8bbSFelipe Balbi 	}
2114ca88fc2eSDaniel Mack 
2115b4dc38fdSFelipe Balbi 	/* drop session bit */
2116b4dc38fdSFelipe Balbi 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2117b4dc38fdSFelipe Balbi 	devctl &= ~MUSB_DEVCTL_SESSION;
2118b4dc38fdSFelipe Balbi 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
2119ca88fc2eSDaniel Mack 
2120b4dc38fdSFelipe Balbi 	/* tell usbcore about it */
2121b4dc38fdSFelipe Balbi 	musb_root_disconnect(musb);
2122ca88fc2eSDaniel Mack 
2123ca88fc2eSDaniel Mack 	/*
2124d871c622SGeorge Cherian 	 * When a babble condition occurs, the musb controller
2125d871c622SGeorge Cherian 	 * removes the session bit and the endpoint config is lost.
2126ca88fc2eSDaniel Mack 	 */
2127ca88fc2eSDaniel Mack 	if (musb->dyn_fifo)
2128b4dc38fdSFelipe Balbi 		ret = ep_config_from_table(musb);
2129ca88fc2eSDaniel Mack 	else
2130b4dc38fdSFelipe Balbi 		ret = ep_config_from_hw(musb);
2131ca88fc2eSDaniel Mack 
2132b4dc38fdSFelipe Balbi 	/* restart session */
2133b4dc38fdSFelipe Balbi 	if (ret == 0)
2134ca88fc2eSDaniel Mack 		musb_start(musb);
2135ca88fc2eSDaniel Mack }
2136ca88fc2eSDaniel Mack 
2137550a7375SFelipe Balbi /* --------------------------------------------------------------------------
2138550a7375SFelipe Balbi  * Init support
2139550a7375SFelipe Balbi  */
2140550a7375SFelipe Balbi 
allocate_instance(struct device * dev,const struct musb_hdrc_config * config,void __iomem * mbase)214141ac7b3aSBill Pemberton static struct musb *allocate_instance(struct device *dev,
2142ead22cafSPetr Kulhavy 		const struct musb_hdrc_config *config, void __iomem *mbase)
2143550a7375SFelipe Balbi {
2144550a7375SFelipe Balbi 	struct musb		*musb;
2145550a7375SFelipe Balbi 	struct musb_hw_ep	*ep;
2146550a7375SFelipe Balbi 	int			epnum;
214774c2e936SDaniel Mack 	int			ret;
2148550a7375SFelipe Balbi 
214974c2e936SDaniel Mack 	musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
215074c2e936SDaniel Mack 	if (!musb)
2151550a7375SFelipe Balbi 		return NULL;
2152550a7375SFelipe Balbi 
2153550a7375SFelipe Balbi 	INIT_LIST_HEAD(&musb->control);
2154550a7375SFelipe Balbi 	INIT_LIST_HEAD(&musb->in_bulk);
2155550a7375SFelipe Balbi 	INIT_LIST_HEAD(&musb->out_bulk);
2156ea2f35c0STony Lindgren 	INIT_LIST_HEAD(&musb->pending_list);
2157550a7375SFelipe Balbi 
2158550a7375SFelipe Balbi 	musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
2159f7f9d63eSDavid Brownell 	musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
2160550a7375SFelipe Balbi 	musb->mregs = mbase;
2161550a7375SFelipe Balbi 	musb->ctrl_base = mbase;
2162550a7375SFelipe Balbi 	musb->nIrq = -ENODEV;
2163ca6d1b13SFelipe Balbi 	musb->config = config;
216402582b92SKevin Hilman 	BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
2165550a7375SFelipe Balbi 	for (epnum = 0, ep = musb->endpoints;
2166ca6d1b13SFelipe Balbi 			epnum < musb->config->num_eps;
2167550a7375SFelipe Balbi 			epnum++, ep++) {
2168550a7375SFelipe Balbi 		ep->musb = musb;
2169550a7375SFelipe Balbi 		ep->epnum = epnum;
2170550a7375SFelipe Balbi 	}
2171550a7375SFelipe Balbi 
2172550a7375SFelipe Balbi 	musb->controller = dev;
2173743411b3SFelipe Balbi 
217474c2e936SDaniel Mack 	ret = musb_host_alloc(musb);
217574c2e936SDaniel Mack 	if (ret < 0)
217674c2e936SDaniel Mack 		goto err_free;
217774c2e936SDaniel Mack 
217874c2e936SDaniel Mack 	dev_set_drvdata(dev, musb);
217974c2e936SDaniel Mack 
2180550a7375SFelipe Balbi 	return musb;
218174c2e936SDaniel Mack 
218274c2e936SDaniel Mack err_free:
218374c2e936SDaniel Mack 	return NULL;
2184550a7375SFelipe Balbi }
2185550a7375SFelipe Balbi 
musb_free(struct musb * musb)2186550a7375SFelipe Balbi static void musb_free(struct musb *musb)
2187550a7375SFelipe Balbi {
2188550a7375SFelipe Balbi 	/* this has multiple entry modes. it handles fault cleanup after
2189550a7375SFelipe Balbi 	 * probe(), where things may be partially set up, as well as rmmod
2190550a7375SFelipe Balbi 	 * cleanup after everything's been de-activated.
2191550a7375SFelipe Balbi 	 */
2192550a7375SFelipe Balbi 
219397a39896SAjay Kumar Gupta 	if (musb->nIrq >= 0) {
219497a39896SAjay Kumar Gupta 		if (musb->irq_wake)
2195550a7375SFelipe Balbi 			disable_irq_wake(musb->nIrq);
2196550a7375SFelipe Balbi 		free_irq(musb->nIrq, musb);
2197550a7375SFelipe Balbi 	}
2198550a7375SFelipe Balbi 
219974c2e936SDaniel Mack 	musb_host_free(musb);
2200550a7375SFelipe Balbi }
2201550a7375SFelipe Balbi 
2202ea2f35c0STony Lindgren struct musb_pending_work {
2203ea2f35c0STony Lindgren 	int (*callback)(struct musb *musb, void *data);
2204ea2f35c0STony Lindgren 	void *data;
2205ea2f35c0STony Lindgren 	struct list_head node;
2206ea2f35c0STony Lindgren };
2207ea2f35c0STony Lindgren 
2208c8bd2ac3SJérémy Lefaure #ifdef CONFIG_PM
2209ea2f35c0STony Lindgren /*
2210ea2f35c0STony Lindgren  * Called from musb_runtime_resume(), musb_resume(), and
2211ea2f35c0STony Lindgren  * musb_queue_resume_work(). Callers must take musb->lock.
2212ea2f35c0STony Lindgren  */
musb_run_resume_work(struct musb * musb)2213ea2f35c0STony Lindgren static int musb_run_resume_work(struct musb *musb)
2214ea2f35c0STony Lindgren {
2215ea2f35c0STony Lindgren 	struct musb_pending_work *w, *_w;
2216ea2f35c0STony Lindgren 	unsigned long flags;
2217ea2f35c0STony Lindgren 	int error = 0;
2218ea2f35c0STony Lindgren 
2219ea2f35c0STony Lindgren 	spin_lock_irqsave(&musb->list_lock, flags);
2220ea2f35c0STony Lindgren 	list_for_each_entry_safe(w, _w, &musb->pending_list, node) {
2221ea2f35c0STony Lindgren 		if (w->callback) {
2222ea2f35c0STony Lindgren 			error = w->callback(musb, w->data);
2223ea2f35c0STony Lindgren 			if (error < 0) {
2224ea2f35c0STony Lindgren 				dev_err(musb->controller,
2225ea2f35c0STony Lindgren 					"resume callback %p failed: %i\n",
2226ea2f35c0STony Lindgren 					w->callback, error);
2227ea2f35c0STony Lindgren 			}
2228ea2f35c0STony Lindgren 		}
2229ea2f35c0STony Lindgren 		list_del(&w->node);
2230ea2f35c0STony Lindgren 		devm_kfree(musb->controller, w);
2231ea2f35c0STony Lindgren 	}
2232ea2f35c0STony Lindgren 	spin_unlock_irqrestore(&musb->list_lock, flags);
2233ea2f35c0STony Lindgren 
2234ea2f35c0STony Lindgren 	return error;
2235ea2f35c0STony Lindgren }
2236c8bd2ac3SJérémy Lefaure #endif
2237ea2f35c0STony Lindgren 
2238ea2f35c0STony Lindgren /*
2239ea2f35c0STony Lindgren  * Called to run work if device is active or else queue the work to happen
2240ea2f35c0STony Lindgren  * on resume. Caller must take musb->lock and must hold an RPM reference.
2241ea2f35c0STony Lindgren  *
2242ea2f35c0STony Lindgren  * Note that we cowardly refuse queuing work after musb PM runtime
2243ea2f35c0STony Lindgren  * resume is done calling musb_run_resume_work() and return -EINPROGRESS
2244ea2f35c0STony Lindgren  * instead.
2245ea2f35c0STony Lindgren  */
musb_queue_resume_work(struct musb * musb,int (* callback)(struct musb * musb,void * data),void * data)2246ea2f35c0STony Lindgren int musb_queue_resume_work(struct musb *musb,
2247ea2f35c0STony Lindgren 			   int (*callback)(struct musb *musb, void *data),
2248ea2f35c0STony Lindgren 			   void *data)
2249ea2f35c0STony Lindgren {
2250ea2f35c0STony Lindgren 	struct musb_pending_work *w;
2251ea2f35c0STony Lindgren 	unsigned long flags;
22520eaa1a37SPaul Cercueil 	bool is_suspended;
2253ea2f35c0STony Lindgren 	int error;
2254ea2f35c0STony Lindgren 
2255ea2f35c0STony Lindgren 	if (WARN_ON(!callback))
2256ea2f35c0STony Lindgren 		return -EINVAL;
2257ea2f35c0STony Lindgren 
22580eaa1a37SPaul Cercueil 	spin_lock_irqsave(&musb->list_lock, flags);
22590eaa1a37SPaul Cercueil 	is_suspended = musb->is_runtime_suspended;
2260ea2f35c0STony Lindgren 
22610eaa1a37SPaul Cercueil 	if (is_suspended) {
2262ea2f35c0STony Lindgren 		w = devm_kzalloc(musb->controller, sizeof(*w), GFP_ATOMIC);
22630eaa1a37SPaul Cercueil 		if (!w) {
22640eaa1a37SPaul Cercueil 			error = -ENOMEM;
22650eaa1a37SPaul Cercueil 			goto out_unlock;
22660eaa1a37SPaul Cercueil 		}
2267ea2f35c0STony Lindgren 
2268ea2f35c0STony Lindgren 		w->callback = callback;
2269ea2f35c0STony Lindgren 		w->data = data;
22700eaa1a37SPaul Cercueil 
2271ea2f35c0STony Lindgren 		list_add_tail(&w->node, &musb->pending_list);
2272ea2f35c0STony Lindgren 		error = 0;
2273ea2f35c0STony Lindgren 	}
22740eaa1a37SPaul Cercueil 
22750eaa1a37SPaul Cercueil out_unlock:
2276ea2f35c0STony Lindgren 	spin_unlock_irqrestore(&musb->list_lock, flags);
2277ea2f35c0STony Lindgren 
22780eaa1a37SPaul Cercueil 	if (!is_suspended)
22790eaa1a37SPaul Cercueil 		error = callback(musb, data);
22800eaa1a37SPaul Cercueil 
2281ea2f35c0STony Lindgren 	return error;
2282ea2f35c0STony Lindgren }
2283ea2f35c0STony Lindgren EXPORT_SYMBOL_GPL(musb_queue_resume_work);
2284ea2f35c0STony Lindgren 
musb_deassert_reset(struct work_struct * work)22858ed1fb79SDaniel Mack static void musb_deassert_reset(struct work_struct *work)
22868ed1fb79SDaniel Mack {
22878ed1fb79SDaniel Mack 	struct musb *musb;
22888ed1fb79SDaniel Mack 	unsigned long flags;
22898ed1fb79SDaniel Mack 
22908ed1fb79SDaniel Mack 	musb = container_of(work, struct musb, deassert_reset_work.work);
22918ed1fb79SDaniel Mack 
22928ed1fb79SDaniel Mack 	spin_lock_irqsave(&musb->lock, flags);
22938ed1fb79SDaniel Mack 
22948ed1fb79SDaniel Mack 	if (musb->port1_status & USB_PORT_STAT_RESET)
22958ed1fb79SDaniel Mack 		musb_port_reset(musb, false);
22968ed1fb79SDaniel Mack 
22978ed1fb79SDaniel Mack 	spin_unlock_irqrestore(&musb->lock, flags);
22988ed1fb79SDaniel Mack }
22998ed1fb79SDaniel Mack 
2300550a7375SFelipe Balbi /*
2301550a7375SFelipe Balbi  * Perform generic per-controller initialization.
2302550a7375SFelipe Balbi  *
230328dd924aSSergei Shtylyov  * @dev: the controller (already clocked, etc)
230428dd924aSSergei Shtylyov  * @nIrq: IRQ number
230528dd924aSSergei Shtylyov  * @ctrl: virtual address of controller registers,
2306550a7375SFelipe Balbi  *	not yet corrected for platform-specific offsets
2307550a7375SFelipe Balbi  */
230841ac7b3aSBill Pemberton static int
musb_init_controller(struct device * dev,int nIrq,void __iomem * ctrl)2309550a7375SFelipe Balbi musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
2310550a7375SFelipe Balbi {
2311550a7375SFelipe Balbi 	int			status;
2312550a7375SFelipe Balbi 	struct musb		*musb;
2313c1a7d67cSJingoo Han 	struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
2314550a7375SFelipe Balbi 
2315550a7375SFelipe Balbi 	/* The driver might handle more features than the board; OK.
2316550a7375SFelipe Balbi 	 * Fail when the board needs a feature that's not enabled.
2317550a7375SFelipe Balbi 	 */
2318550a7375SFelipe Balbi 	if (!plat) {
2319b99d3659SBin Liu 		dev_err(dev, "no platform_data?\n");
232034e2beb2SSergei Shtylyov 		status = -ENODEV;
232134e2beb2SSergei Shtylyov 		goto fail0;
2322550a7375SFelipe Balbi 	}
232334e2beb2SSergei Shtylyov 
2324550a7375SFelipe Balbi 	/* allocate */
2325ca6d1b13SFelipe Balbi 	musb = allocate_instance(dev, plat->config, ctrl);
232634e2beb2SSergei Shtylyov 	if (!musb) {
232734e2beb2SSergei Shtylyov 		status = -ENOMEM;
232834e2beb2SSergei Shtylyov 		goto fail0;
232934e2beb2SSergei Shtylyov 	}
2330550a7375SFelipe Balbi 
2331550a7375SFelipe Balbi 	spin_lock_init(&musb->lock);
2332ea2f35c0STony Lindgren 	spin_lock_init(&musb->list_lock);
2333550a7375SFelipe Balbi 	musb->min_power = plat->min_power;
2334f7ec9437SFelipe Balbi 	musb->ops = plat->platform_ops;
23359ad96e69SDaniel Mack 	musb->port_mode = plat->mode;
2336550a7375SFelipe Balbi 
23371b40fc57STony Lindgren 	/*
23381b40fc57STony Lindgren 	 * Initialize the default IO functions. At least omap2430 needs
23391b40fc57STony Lindgren 	 * these early. We initialize the platform specific IO functions
23401b40fc57STony Lindgren 	 * later on.
23411b40fc57STony Lindgren 	 */
23421b40fc57STony Lindgren 	musb_readb = musb_default_readb;
23431b40fc57STony Lindgren 	musb_writeb = musb_default_writeb;
23441b40fc57STony Lindgren 	musb_readw = musb_default_readw;
23451b40fc57STony Lindgren 	musb_writew = musb_default_writew;
23461b40fc57STony Lindgren 
234784e250ffSDavid Brownell 	/* The musb_platform_init() call:
2348baef653aSPhilippe De Swert 	 *   - adjusts musb->mregs
2349baef653aSPhilippe De Swert 	 *   - sets the musb->isr
23505ae477b0SRahul Bedarkar 	 *   - may initialize an integrated transceiver
2351721002ecSKishon Vijay Abraham I 	 *   - initializes musb->xceiv, usually by otg_get_phy()
235284e250ffSDavid Brownell 	 *   - stops powering VBUS
235384e250ffSDavid Brownell 	 *
2354a9762b70SArnd Bergmann 	 * There are various transceiver configurations.
235584e250ffSDavid Brownell 	 * DaVinci, TUSB60x0, and others integrate them.  OMAP3 uses
235684e250ffSDavid Brownell 	 * external/discrete ones in various flavors (twl4030 family,
235784e250ffSDavid Brownell 	 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
2358550a7375SFelipe Balbi 	 */
2359ea65df57SHema Kalliguddi 	status = musb_platform_init(musb);
2360550a7375SFelipe Balbi 	if (status < 0)
236103491761SFelipe Balbi 		goto fail1;
236234e2beb2SSergei Shtylyov 
2363550a7375SFelipe Balbi 	if (!musb->isr) {
2364550a7375SFelipe Balbi 		status = -ENODEV;
2365c04352a5SGrazvydas Ignotas 		goto fail2;
2366550a7375SFelipe Balbi 	}
2367550a7375SFelipe Balbi 
23681b40fc57STony Lindgren 
2369da96cfc1SBen Hutchings 	/* Most devices use indexed offset or flat offset */
2370dc8fca6cSBin Liu 	if (musb->ops->quirks & MUSB_INDEXED_EP) {
2371d026e9c7STony Lindgren 		musb->io.ep_offset = musb_indexed_ep_offset;
2372d026e9c7STony Lindgren 		musb->io.ep_select = musb_indexed_ep_select;
2373d026e9c7STony Lindgren 	} else {
2374d026e9c7STony Lindgren 		musb->io.ep_offset = musb_flat_ep_offset;
2375d026e9c7STony Lindgren 		musb->io.ep_select = musb_flat_ep_select;
2376d026e9c7STony Lindgren 	}
2377d026e9c7STony Lindgren 
2378dc8fca6cSBin Liu 	if (musb->ops->quirks & MUSB_G_NO_SKB_RESERVE)
23791fa07c37SPeter Ujfalusi 		musb->g.quirk_avoids_skb_reserve = 1;
23801fa07c37SPeter Ujfalusi 
2381da96cfc1SBen Hutchings 	/* At least tusb6010 has its own offsets */
2382da96cfc1SBen Hutchings 	if (musb->ops->ep_offset)
2383da96cfc1SBen Hutchings 		musb->io.ep_offset = musb->ops->ep_offset;
2384da96cfc1SBen Hutchings 	if (musb->ops->ep_select)
2385da96cfc1SBen Hutchings 		musb->io.ep_select = musb->ops->ep_select;
2386da96cfc1SBen Hutchings 
23878a77f05aSTony Lindgren 	if (musb->ops->fifo_mode)
23888a77f05aSTony Lindgren 		fifo_mode = musb->ops->fifo_mode;
23898a77f05aSTony Lindgren 	else
23908a77f05aSTony Lindgren 		fifo_mode = 4;
23918a77f05aSTony Lindgren 
23921b40fc57STony Lindgren 	if (musb->ops->fifo_offset)
23931b40fc57STony Lindgren 		musb->io.fifo_offset = musb->ops->fifo_offset;
23941b40fc57STony Lindgren 	else
23951b40fc57STony Lindgren 		musb->io.fifo_offset = musb_default_fifo_offset;
23961b40fc57STony Lindgren 
23976cc2af6dSHans de Goede 	if (musb->ops->busctl_offset)
23986cc2af6dSHans de Goede 		musb->io.busctl_offset = musb->ops->busctl_offset;
23996cc2af6dSHans de Goede 	else
24006cc2af6dSHans de Goede 		musb->io.busctl_offset = musb_default_busctl_offset;
24016cc2af6dSHans de Goede 
24021b40fc57STony Lindgren 	if (musb->ops->readb)
24031b40fc57STony Lindgren 		musb_readb = musb->ops->readb;
24041b40fc57STony Lindgren 	if (musb->ops->writeb)
24051b40fc57STony Lindgren 		musb_writeb = musb->ops->writeb;
24069c93d7fdSMin Guo 	if (musb->ops->clearb)
24079c93d7fdSMin Guo 		musb_clearb = musb->ops->clearb;
24089c93d7fdSMin Guo 	else
24099c93d7fdSMin Guo 		musb_clearb = musb_readb;
24109c93d7fdSMin Guo 
24111b40fc57STony Lindgren 	if (musb->ops->readw)
24121b40fc57STony Lindgren 		musb_readw = musb->ops->readw;
24131b40fc57STony Lindgren 	if (musb->ops->writew)
24141b40fc57STony Lindgren 		musb_writew = musb->ops->writew;
24159c93d7fdSMin Guo 	if (musb->ops->clearw)
24169c93d7fdSMin Guo 		musb_clearw = musb->ops->clearw;
24179c93d7fdSMin Guo 	else
24189c93d7fdSMin Guo 		musb_clearw = musb_readw;
24191b40fc57STony Lindgren 
24207f6283edSTony Lindgren #ifndef CONFIG_MUSB_PIO_ONLY
24217f6283edSTony Lindgren 	if (!musb->ops->dma_init || !musb->ops->dma_exit) {
24227f6283edSTony Lindgren 		dev_err(dev, "DMA controller not set\n");
24237d32cdefSAaro Koskinen 		status = -ENODEV;
24247f6283edSTony Lindgren 		goto fail2;
24257f6283edSTony Lindgren 	}
24267f6283edSTony Lindgren 	musb_dma_controller_create = musb->ops->dma_init;
24277f6283edSTony Lindgren 	musb_dma_controller_destroy = musb->ops->dma_exit;
24287f6283edSTony Lindgren #endif
24297f6283edSTony Lindgren 
24301b40fc57STony Lindgren 	if (musb->ops->read_fifo)
24311b40fc57STony Lindgren 		musb->io.read_fifo = musb->ops->read_fifo;
24321b40fc57STony Lindgren 	else
24331b40fc57STony Lindgren 		musb->io.read_fifo = musb_default_read_fifo;
24341b40fc57STony Lindgren 
24351b40fc57STony Lindgren 	if (musb->ops->write_fifo)
24361b40fc57STony Lindgren 		musb->io.write_fifo = musb->ops->write_fifo;
24371b40fc57STony Lindgren 	else
24381b40fc57STony Lindgren 		musb->io.write_fifo = musb_default_write_fifo;
24391b40fc57STony Lindgren 
2440fe3bbd6bSMin Guo 	if (musb->ops->get_toggle)
2441fe3bbd6bSMin Guo 		musb->io.get_toggle = musb->ops->get_toggle;
2442fe3bbd6bSMin Guo 	else
2443fe3bbd6bSMin Guo 		musb->io.get_toggle = musb_default_get_toggle;
2444fe3bbd6bSMin Guo 
2445fe3bbd6bSMin Guo 	if (musb->ops->set_toggle)
2446fe3bbd6bSMin Guo 		musb->io.set_toggle = musb->ops->set_toggle;
2447fe3bbd6bSMin Guo 	else
2448fe3bbd6bSMin Guo 		musb->io.set_toggle = musb_default_set_toggle;
2449fe3bbd6bSMin Guo 
2450a6d45ea0SPaul Cercueil 	if (IS_ENABLED(CONFIG_USB_PHY) && musb->xceiv && !musb->xceiv->io_ops) {
2451bf070bc1SGrazvydas Ignotas 		musb->xceiv->io_dev = musb->controller;
2452ffb865b1SHeikki Krogerus 		musb->xceiv->io_priv = musb->mregs;
2453ffb865b1SHeikki Krogerus 		musb->xceiv->io_ops = &musb_ulpi_access;
2454ffb865b1SHeikki Krogerus 	}
2455ffb865b1SHeikki Krogerus 
24568055555fSTony Lindgren 	if (musb->ops->phy_callback)
24578055555fSTony Lindgren 		musb_phy_callback = musb->ops->phy_callback;
24588055555fSTony Lindgren 
2459f730f205STony Lindgren 	/*
2460f730f205STony Lindgren 	 * We need musb_read/write functions initialized for PM.
2461f730f205STony Lindgren 	 * Note that at least 2430 glue needs autosuspend delay
2462f730f205STony Lindgren 	 * somewhere above 300 ms for the hardware to idle properly
2463f730f205STony Lindgren 	 * after disconnecting the cable in host mode. Let's use
2464f730f205STony Lindgren 	 * 500 ms for some margin.
2465f730f205STony Lindgren 	 */
2466f730f205STony Lindgren 	pm_runtime_use_autosuspend(musb->controller);
2467f730f205STony Lindgren 	pm_runtime_set_autosuspend_delay(musb->controller, 500);
2468f730f205STony Lindgren 	pm_runtime_enable(musb->controller);
2469c04352a5SGrazvydas Ignotas 	pm_runtime_get_sync(musb->controller);
2470c04352a5SGrazvydas Ignotas 
247139cee200SUwe Kleine-König 	status = usb_phy_init(musb->xceiv);
247239cee200SUwe Kleine-König 	if (status < 0)
247339cee200SUwe Kleine-König 		goto err_usb_phy_init;
247439cee200SUwe Kleine-König 
247548054147SSebastian Andrzej Siewior 	if (use_dma && dev->dma_mask) {
24767f6283edSTony Lindgren 		musb->dma_controller =
24777f6283edSTony Lindgren 			musb_dma_controller_create(musb, musb->mregs);
247848054147SSebastian Andrzej Siewior 		if (IS_ERR(musb->dma_controller)) {
247948054147SSebastian Andrzej Siewior 			status = PTR_ERR(musb->dma_controller);
248048054147SSebastian Andrzej Siewior 			goto fail2_5;
248148054147SSebastian Andrzej Siewior 		}
248248054147SSebastian Andrzej Siewior 	}
2483550a7375SFelipe Balbi 
2484550a7375SFelipe Balbi 	/* be sure interrupts are disabled before connecting ISR */
2485550a7375SFelipe Balbi 	musb_platform_disable(musb);
2486e945953dSBin Liu 	musb_disable_interrupts(musb);
2487e945953dSBin Liu 	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
2488550a7375SFelipe Balbi 
248996a0c128SPaul Cercueil 	/* MUSB_POWER_SOFTCONN might be already set, JZ4740 does this. */
249096a0c128SPaul Cercueil 	musb_writeb(musb->mregs, MUSB_POWER, 0);
249196a0c128SPaul Cercueil 
249266fadea5SSebastian Andrzej Siewior 	/* Init IRQ workqueue before request_irq */
24932bff3916STony Lindgren 	INIT_DELAYED_WORK(&musb->irq_work, musb_irq_work);
24948ed1fb79SDaniel Mack 	INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
24958ed1fb79SDaniel Mack 	INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
249666fadea5SSebastian Andrzej Siewior 
2497550a7375SFelipe Balbi 	/* setup musb parts of the core (especially endpoints) */
2498ca6d1b13SFelipe Balbi 	status = musb_core_init(plat->config->multipoint
2499550a7375SFelipe Balbi 			? MUSB_CONTROLLER_MHDRC
2500550a7375SFelipe Balbi 			: MUSB_CONTROLLER_HDRC, musb);
2501550a7375SFelipe Balbi 	if (status < 0)
250234e2beb2SSergei Shtylyov 		goto fail3;
2503550a7375SFelipe Balbi 
250405678497SKees Cook 	timer_setup(&musb->otg_timer, musb_otg_timer_func, 0);
2505f7f9d63eSDavid Brownell 
2506550a7375SFelipe Balbi 	/* attach to the IRQ */
2507aa2fb886SAlexandre Bailon 	if (request_irq(nIrq, musb->isr, IRQF_SHARED, dev_name(dev), musb)) {
2508550a7375SFelipe Balbi 		dev_err(dev, "request_irq %d failed!\n", nIrq);
2509550a7375SFelipe Balbi 		status = -ENODEV;
251034e2beb2SSergei Shtylyov 		goto fail3;
2511550a7375SFelipe Balbi 	}
2512550a7375SFelipe Balbi 	musb->nIrq = nIrq;
2513550a7375SFelipe Balbi 	/* FIXME this handles wakeup irqs wrong */
2514c48a5155SFelipe Balbi 	if (enable_irq_wake(nIrq) == 0) {
2515c48a5155SFelipe Balbi 		musb->irq_wake = 1;
2516550a7375SFelipe Balbi 		device_init_wakeup(dev, 1);
2517c48a5155SFelipe Balbi 	} else {
2518c48a5155SFelipe Balbi 		musb->irq_wake = 0;
2519c48a5155SFelipe Balbi 	}
2520550a7375SFelipe Balbi 
25215fc4e779SAjay Kumar Gupta 	/* program PHY to use external vBus if required */
25225fc4e779SAjay Kumar Gupta 	if (plat->extvbus) {
2523113ad151SBin Liu 		u8 busctl = musb_readb(musb->mregs, MUSB_ULPI_BUSCONTROL);
25245fc4e779SAjay Kumar Gupta 		busctl |= MUSB_ULPI_USE_EXTVBUS;
2525113ad151SBin Liu 		musb_writeb(musb->mregs, MUSB_ULPI_BUSCONTROL, busctl);
25265fc4e779SAjay Kumar Gupta 	}
2527550a7375SFelipe Balbi 
2528550a7375SFelipe Balbi 	MUSB_DEV_MODE(musb);
252921acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_B_IDLE);
2530550a7375SFelipe Balbi 
25316c5f6a6fSDaniel Mack 	switch (musb->port_mode) {
25327ad76955SBin Liu 	case MUSB_HOST:
25336c5f6a6fSDaniel Mack 		status = musb_host_setup(musb, plat->power);
25342df6761eSFelipe Balbi 		if (status < 0)
25352df6761eSFelipe Balbi 			goto fail3;
25362df6761eSFelipe Balbi 		status = musb_platform_set_mode(musb, MUSB_HOST);
25376c5f6a6fSDaniel Mack 		break;
25387ad76955SBin Liu 	case MUSB_PERIPHERAL:
25396c5f6a6fSDaniel Mack 		status = musb_gadget_setup(musb);
25402df6761eSFelipe Balbi 		if (status < 0)
25412df6761eSFelipe Balbi 			goto fail3;
25422df6761eSFelipe Balbi 		status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
25436c5f6a6fSDaniel Mack 		break;
25447ad76955SBin Liu 	case MUSB_OTG:
25452cc65feaSDaniel Mack 		status = musb_host_setup(musb, plat->power);
25462cc65feaSDaniel Mack 		if (status < 0)
25472cc65feaSDaniel Mack 			goto fail3;
2548550a7375SFelipe Balbi 		status = musb_gadget_setup(musb);
25492df6761eSFelipe Balbi 		if (status) {
25500d2dd7eaSSebastian Andrzej Siewior 			musb_host_cleanup(musb);
25512df6761eSFelipe Balbi 			goto fail3;
25522df6761eSFelipe Balbi 		}
25532df6761eSFelipe Balbi 		status = musb_platform_set_mode(musb, MUSB_OTG);
25546c5f6a6fSDaniel Mack 		break;
25556c5f6a6fSDaniel Mack 	default:
25566c5f6a6fSDaniel Mack 		dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
25576c5f6a6fSDaniel Mack 		break;
25586c5f6a6fSDaniel Mack 	}
2559550a7375SFelipe Balbi 
2560461972d8SSergei Shtylyov 	if (status < 0)
256134e2beb2SSergei Shtylyov 		goto fail3;
2562550a7375SFelipe Balbi 
25638a1ef171SGreg Kroah-Hartman 	musb_init_debugfs(musb);
25647f7f9e2aSFelipe Balbi 
2565c723bd6eSTony Lindgren 	musb->is_initialized = 1;
25667099dbc5STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
25677099dbc5STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
2568c04352a5SGrazvydas Ignotas 
256928c2c51cSFelipe Balbi 	return 0;
257028c2c51cSFelipe Balbi 
257134e2beb2SSergei Shtylyov fail3:
25722bff3916STony Lindgren 	cancel_delayed_work_sync(&musb->irq_work);
25738ed1fb79SDaniel Mack 	cancel_delayed_work_sync(&musb->finish_resume_work);
25748ed1fb79SDaniel Mack 	cancel_delayed_work_sync(&musb->deassert_reset_work);
2575f3ce4d5bSSebastian Andrzej Siewior 	if (musb->dma_controller)
25767f6283edSTony Lindgren 		musb_dma_controller_destroy(musb->dma_controller);
257739cee200SUwe Kleine-König 
257848054147SSebastian Andrzej Siewior fail2_5:
257939cee200SUwe Kleine-König 	usb_phy_shutdown(musb->xceiv);
258039cee200SUwe Kleine-König 
258139cee200SUwe Kleine-König err_usb_phy_init:
25827099dbc5STony Lindgren 	pm_runtime_dont_use_autosuspend(musb->controller);
2583c04352a5SGrazvydas Ignotas 	pm_runtime_put_sync(musb->controller);
2584f730f205STony Lindgren 	pm_runtime_disable(musb->controller);
2585c04352a5SGrazvydas Ignotas 
2586c04352a5SGrazvydas Ignotas fail2:
258734e2beb2SSergei Shtylyov 	if (musb->irq_wake)
258834e2beb2SSergei Shtylyov 		device_init_wakeup(dev, 0);
258928c2c51cSFelipe Balbi 	musb_platform_exit(musb);
259034e2beb2SSergei Shtylyov 
259134e2beb2SSergei Shtylyov fail1:
25927be7231dSYang Yingliang 	dev_err_probe(musb->controller, status, "%s failed\n", __func__);
259328c2c51cSFelipe Balbi 
259428c2c51cSFelipe Balbi 	musb_free(musb);
2595550a7375SFelipe Balbi 
259634e2beb2SSergei Shtylyov fail0:
259734e2beb2SSergei Shtylyov 
2598550a7375SFelipe Balbi 	return status;
2599550a7375SFelipe Balbi 
2600550a7375SFelipe Balbi }
2601550a7375SFelipe Balbi 
2602550a7375SFelipe Balbi /*-------------------------------------------------------------------------*/
2603550a7375SFelipe Balbi 
2604550a7375SFelipe Balbi /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2605550a7375SFelipe Balbi  * bridge to a platform device; this driver then suffices.
2606550a7375SFelipe Balbi  */
musb_probe(struct platform_device * pdev)260741ac7b3aSBill Pemberton static int musb_probe(struct platform_device *pdev)
2608550a7375SFelipe Balbi {
2609550a7375SFelipe Balbi 	struct device	*dev = &pdev->dev;
2610fcf173e4SHema Kalliguddi 	int		irq = platform_get_irq_byname(pdev, "mc");
2611550a7375SFelipe Balbi 	void __iomem	*base;
2612550a7375SFelipe Balbi 
2613*adbe9720SZhu Wang 	if (irq < 0)
2614*adbe9720SZhu Wang 		return irq;
2615550a7375SFelipe Balbi 
2616f68341d1SYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
2617b42f7f30SFelipe Balbi 	if (IS_ERR(base))
2618b42f7f30SFelipe Balbi 		return PTR_ERR(base);
2619550a7375SFelipe Balbi 
2620b42f7f30SFelipe Balbi 	return musb_init_controller(dev, irq, base);
2621550a7375SFelipe Balbi }
2622550a7375SFelipe Balbi 
musb_remove(struct platform_device * pdev)2623aa846a29SUwe Kleine-König static void musb_remove(struct platform_device *pdev)
2624550a7375SFelipe Balbi {
26258d2421e6SAjay Kumar Gupta 	struct device	*dev = &pdev->dev;
26268d2421e6SAjay Kumar Gupta 	struct musb	*musb = dev_to_musb(dev);
2627302f6802STony Lindgren 	unsigned long	flags;
2628550a7375SFelipe Balbi 
2629550a7375SFelipe Balbi 	/* this gets called on rmmod.
2630550a7375SFelipe Balbi 	 *  - Host mode: host may still be active
2631550a7375SFelipe Balbi 	 *  - Peripheral mode: peripheral is deactivated (or never-activated)
2632550a7375SFelipe Balbi 	 *  - OTG mode: both roles are deactivated (or never-activated)
2633550a7375SFelipe Balbi 	 */
26347f7f9e2aSFelipe Balbi 	musb_exit_debugfs(musb);
2635302f6802STony Lindgren 
26362bff3916STony Lindgren 	cancel_delayed_work_sync(&musb->irq_work);
2637f730f205STony Lindgren 	cancel_delayed_work_sync(&musb->finish_resume_work);
2638f730f205STony Lindgren 	cancel_delayed_work_sync(&musb->deassert_reset_work);
2639302f6802STony Lindgren 	pm_runtime_get_sync(musb->controller);
2640302f6802STony Lindgren 	musb_host_cleanup(musb);
2641302f6802STony Lindgren 	musb_gadget_cleanup(musb);
2642e945953dSBin Liu 
2643302f6802STony Lindgren 	musb_platform_disable(musb);
2644bc1e2154SBin Liu 	spin_lock_irqsave(&musb->lock, flags);
2645e945953dSBin Liu 	musb_disable_interrupts(musb);
2646302f6802STony Lindgren 	musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
2647e945953dSBin Liu 	spin_unlock_irqrestore(&musb->lock, flags);
264894e46a4fSMerlijn Wajer 	musb_platform_exit(musb);
2649e945953dSBin Liu 
26507099dbc5STony Lindgren 	pm_runtime_dont_use_autosuspend(musb->controller);
26517099dbc5STony Lindgren 	pm_runtime_put_sync(musb->controller);
26527099dbc5STony Lindgren 	pm_runtime_disable(musb->controller);
2653f730f205STony Lindgren 	musb_phy_callback = NULL;
2654f730f205STony Lindgren 	if (musb->dma_controller)
2655f730f205STony Lindgren 		musb_dma_controller_destroy(musb->dma_controller);
2656f730f205STony Lindgren 	usb_phy_shutdown(musb->xceiv);
2657550a7375SFelipe Balbi 	musb_free(musb);
26588d2421e6SAjay Kumar Gupta 	device_init_wakeup(dev, 0);
2659550a7375SFelipe Balbi }
2660550a7375SFelipe Balbi 
2661550a7375SFelipe Balbi #ifdef	CONFIG_PM
2662550a7375SFelipe Balbi 
musb_save_context(struct musb * musb)26633c8a5fccSFelipe Balbi static void musb_save_context(struct musb *musb)
26644f712e01SAjay Kumar Gupta {
26654f712e01SAjay Kumar Gupta 	int i;
26664f712e01SAjay Kumar Gupta 	void __iomem *musb_base = musb->mregs;
2667ae9b2ad2SBob Liu 	void __iomem *epio;
26684f712e01SAjay Kumar Gupta 
26697421107bSFelipe Balbi 	musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
26707421107bSFelipe Balbi 	musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2671113ad151SBin Liu 	musb->context.busctl = musb_readb(musb_base, MUSB_ULPI_BUSCONTROL);
26727421107bSFelipe Balbi 	musb->context.power = musb_readb(musb_base, MUSB_POWER);
26737421107bSFelipe Balbi 	musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
26747421107bSFelipe Balbi 	musb->context.index = musb_readb(musb_base, MUSB_INDEX);
26757421107bSFelipe Balbi 	musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
26764f712e01SAjay Kumar Gupta 
2677ae9b2ad2SBob Liu 	for (i = 0; i < musb->config->num_eps; ++i) {
2678196a58bdSSergey Shtylyov 		epio = musb->endpoints[i].regs;
2679e4e5b136SFelipe Balbi 		if (!epio)
2680e4e5b136SFelipe Balbi 			continue;
2681e4e5b136SFelipe Balbi 
2682ea737554SVikram Pandita 		musb_writeb(musb_base, MUSB_INDEX, i);
26837421107bSFelipe Balbi 		musb->context.index_regs[i].txmaxp =
2684ae9b2ad2SBob Liu 			musb_readw(epio, MUSB_TXMAXP);
26857421107bSFelipe Balbi 		musb->context.index_regs[i].txcsr =
2686ae9b2ad2SBob Liu 			musb_readw(epio, MUSB_TXCSR);
26877421107bSFelipe Balbi 		musb->context.index_regs[i].rxmaxp =
2688ae9b2ad2SBob Liu 			musb_readw(epio, MUSB_RXMAXP);
26897421107bSFelipe Balbi 		musb->context.index_regs[i].rxcsr =
2690ae9b2ad2SBob Liu 			musb_readw(epio, MUSB_RXCSR);
26914f712e01SAjay Kumar Gupta 
26924f712e01SAjay Kumar Gupta 		if (musb->dyn_fifo) {
26937421107bSFelipe Balbi 			musb->context.index_regs[i].txfifoadd =
2694113ad151SBin Liu 					musb_readw(musb_base, MUSB_TXFIFOADD);
26957421107bSFelipe Balbi 			musb->context.index_regs[i].rxfifoadd =
2696113ad151SBin Liu 					musb_readw(musb_base, MUSB_RXFIFOADD);
26977421107bSFelipe Balbi 			musb->context.index_regs[i].txfifosz =
2698113ad151SBin Liu 					musb_readb(musb_base, MUSB_TXFIFOSZ);
26997421107bSFelipe Balbi 			musb->context.index_regs[i].rxfifosz =
2700113ad151SBin Liu 					musb_readb(musb_base, MUSB_RXFIFOSZ);
27014f712e01SAjay Kumar Gupta 		}
2702032ec49fSFelipe Balbi 
27037421107bSFelipe Balbi 		musb->context.index_regs[i].txtype =
2704ae9b2ad2SBob Liu 			musb_readb(epio, MUSB_TXTYPE);
27057421107bSFelipe Balbi 		musb->context.index_regs[i].txinterval =
2706ae9b2ad2SBob Liu 			musb_readb(epio, MUSB_TXINTERVAL);
27077421107bSFelipe Balbi 		musb->context.index_regs[i].rxtype =
2708ae9b2ad2SBob Liu 			musb_readb(epio, MUSB_RXTYPE);
27097421107bSFelipe Balbi 		musb->context.index_regs[i].rxinterval =
2710ae9b2ad2SBob Liu 			musb_readb(epio, MUSB_RXINTERVAL);
27114f712e01SAjay Kumar Gupta 
27127421107bSFelipe Balbi 		musb->context.index_regs[i].txfunaddr =
27136cc2af6dSHans de Goede 			musb_read_txfunaddr(musb, i);
27147421107bSFelipe Balbi 		musb->context.index_regs[i].txhubaddr =
27156cc2af6dSHans de Goede 			musb_read_txhubaddr(musb, i);
27167421107bSFelipe Balbi 		musb->context.index_regs[i].txhubport =
27176cc2af6dSHans de Goede 			musb_read_txhubport(musb, i);
27184f712e01SAjay Kumar Gupta 
27197421107bSFelipe Balbi 		musb->context.index_regs[i].rxfunaddr =
27206cc2af6dSHans de Goede 			musb_read_rxfunaddr(musb, i);
27217421107bSFelipe Balbi 		musb->context.index_regs[i].rxhubaddr =
27226cc2af6dSHans de Goede 			musb_read_rxhubaddr(musb, i);
27237421107bSFelipe Balbi 		musb->context.index_regs[i].rxhubport =
27246cc2af6dSHans de Goede 			musb_read_rxhubport(musb, i);
27254f712e01SAjay Kumar Gupta 	}
27264f712e01SAjay Kumar Gupta }
27274f712e01SAjay Kumar Gupta 
musb_restore_context(struct musb * musb)27283c8a5fccSFelipe Balbi static void musb_restore_context(struct musb *musb)
27294f712e01SAjay Kumar Gupta {
27304f712e01SAjay Kumar Gupta 	int i;
27314f712e01SAjay Kumar Gupta 	void __iomem *musb_base = musb->mregs;
2732ae9b2ad2SBob Liu 	void __iomem *epio;
273333f8d75fSRoger Quadros 	u8 power;
27344f712e01SAjay Kumar Gupta 
27357421107bSFelipe Balbi 	musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
27367421107bSFelipe Balbi 	musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2737113ad151SBin Liu 	musb_writeb(musb_base, MUSB_ULPI_BUSCONTROL, musb->context.busctl);
273833f8d75fSRoger Quadros 
273933f8d75fSRoger Quadros 	/* Don't affect SUSPENDM/RESUME bits in POWER reg */
274033f8d75fSRoger Quadros 	power = musb_readb(musb_base, MUSB_POWER);
274133f8d75fSRoger Quadros 	power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
274233f8d75fSRoger Quadros 	musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
274333f8d75fSRoger Quadros 	power |= musb->context.power;
274433f8d75fSRoger Quadros 	musb_writeb(musb_base, MUSB_POWER, power);
274533f8d75fSRoger Quadros 
2746b18d26f6SSebastian Andrzej Siewior 	musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
2747af5ec14dSSebastian Andrzej Siewior 	musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
27487421107bSFelipe Balbi 	musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
274984ac5d11SBin Liu 	if (musb->context.devctl & MUSB_DEVCTL_SESSION)
27507421107bSFelipe Balbi 		musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
27514f712e01SAjay Kumar Gupta 
2752ae9b2ad2SBob Liu 	for (i = 0; i < musb->config->num_eps; ++i) {
2753196a58bdSSergey Shtylyov 		epio = musb->endpoints[i].regs;
2754e4e5b136SFelipe Balbi 		if (!epio)
2755e4e5b136SFelipe Balbi 			continue;
2756e4e5b136SFelipe Balbi 
2757ea737554SVikram Pandita 		musb_writeb(musb_base, MUSB_INDEX, i);
2758ae9b2ad2SBob Liu 		musb_writew(epio, MUSB_TXMAXP,
27597421107bSFelipe Balbi 			musb->context.index_regs[i].txmaxp);
2760ae9b2ad2SBob Liu 		musb_writew(epio, MUSB_TXCSR,
27617421107bSFelipe Balbi 			musb->context.index_regs[i].txcsr);
2762ae9b2ad2SBob Liu 		musb_writew(epio, MUSB_RXMAXP,
27637421107bSFelipe Balbi 			musb->context.index_regs[i].rxmaxp);
2764ae9b2ad2SBob Liu 		musb_writew(epio, MUSB_RXCSR,
27657421107bSFelipe Balbi 			musb->context.index_regs[i].rxcsr);
27664f712e01SAjay Kumar Gupta 
27674f712e01SAjay Kumar Gupta 		if (musb->dyn_fifo) {
2768113ad151SBin Liu 			musb_writeb(musb_base, MUSB_TXFIFOSZ,
27697421107bSFelipe Balbi 				musb->context.index_regs[i].txfifosz);
2770113ad151SBin Liu 			musb_writeb(musb_base, MUSB_RXFIFOSZ,
27717421107bSFelipe Balbi 				musb->context.index_regs[i].rxfifosz);
2772113ad151SBin Liu 			musb_writew(musb_base, MUSB_TXFIFOADD,
27737421107bSFelipe Balbi 				musb->context.index_regs[i].txfifoadd);
2774113ad151SBin Liu 			musb_writew(musb_base, MUSB_RXFIFOADD,
27757421107bSFelipe Balbi 				musb->context.index_regs[i].rxfifoadd);
27764f712e01SAjay Kumar Gupta 		}
27774f712e01SAjay Kumar Gupta 
2778ae9b2ad2SBob Liu 		musb_writeb(epio, MUSB_TXTYPE,
27797421107bSFelipe Balbi 				musb->context.index_regs[i].txtype);
2780ae9b2ad2SBob Liu 		musb_writeb(epio, MUSB_TXINTERVAL,
27817421107bSFelipe Balbi 				musb->context.index_regs[i].txinterval);
2782ae9b2ad2SBob Liu 		musb_writeb(epio, MUSB_RXTYPE,
27837421107bSFelipe Balbi 				musb->context.index_regs[i].rxtype);
2784ae9b2ad2SBob Liu 		musb_writeb(epio, MUSB_RXINTERVAL,
27854f712e01SAjay Kumar Gupta 
27867421107bSFelipe Balbi 				musb->context.index_regs[i].rxinterval);
27876cc2af6dSHans de Goede 		musb_write_txfunaddr(musb, i,
27887421107bSFelipe Balbi 				musb->context.index_regs[i].txfunaddr);
27896cc2af6dSHans de Goede 		musb_write_txhubaddr(musb, i,
27907421107bSFelipe Balbi 				musb->context.index_regs[i].txhubaddr);
27916cc2af6dSHans de Goede 		musb_write_txhubport(musb, i,
27927421107bSFelipe Balbi 				musb->context.index_regs[i].txhubport);
27934f712e01SAjay Kumar Gupta 
27946cc2af6dSHans de Goede 		musb_write_rxfunaddr(musb, i,
27957421107bSFelipe Balbi 				musb->context.index_regs[i].rxfunaddr);
27966cc2af6dSHans de Goede 		musb_write_rxhubaddr(musb, i,
27977421107bSFelipe Balbi 				musb->context.index_regs[i].rxhubaddr);
27986cc2af6dSHans de Goede 		musb_write_rxhubport(musb, i,
27997421107bSFelipe Balbi 				musb->context.index_regs[i].rxhubport);
28004f712e01SAjay Kumar Gupta 	}
28013c5fec75SAjay Kumar Gupta 	musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
28024f712e01SAjay Kumar Gupta }
28034f712e01SAjay Kumar Gupta 
musb_suspend(struct device * dev)280448fea965SMagnus Damm static int musb_suspend(struct device *dev)
2805550a7375SFelipe Balbi {
28068220796dSFelipe Balbi 	struct musb	*musb = dev_to_musb(dev);
2807550a7375SFelipe Balbi 	unsigned long	flags;
2808082df8beSJohan Hovold 	int ret;
2809082df8beSJohan Hovold 
2810082df8beSJohan Hovold 	ret = pm_runtime_get_sync(dev);
2811082df8beSJohan Hovold 	if (ret < 0) {
2812082df8beSJohan Hovold 		pm_runtime_put_noidle(dev);
2813082df8beSJohan Hovold 		return ret;
2814082df8beSJohan Hovold 	}
2815550a7375SFelipe Balbi 
28166fc6f4b8SPascal Huerst 	musb_platform_disable(musb);
2817e945953dSBin Liu 	musb_disable_interrupts(musb);
28180c3aae9bSJohan Hovold 
28190c3aae9bSJohan Hovold 	musb->flush_irq_work = true;
28200c3aae9bSJohan Hovold 	while (flush_delayed_work(&musb->irq_work))
28210c3aae9bSJohan Hovold 		;
28220c3aae9bSJohan Hovold 	musb->flush_irq_work = false;
28230c3aae9bSJohan Hovold 
2824dc8fca6cSBin Liu 	if (!(musb->ops->quirks & MUSB_PRESERVE_SESSION))
2825e945953dSBin Liu 		musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
28260c3aae9bSJohan Hovold 
2827ea2f35c0STony Lindgren 	WARN_ON(!list_empty(&musb->pending_list));
28286fc6f4b8SPascal Huerst 
2829550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2830550a7375SFelipe Balbi 
2831550a7375SFelipe Balbi 	if (is_peripheral_active(musb)) {
2832550a7375SFelipe Balbi 		/* FIXME force disconnect unless we know USB will wake
2833550a7375SFelipe Balbi 		 * the system up quickly enough to respond ...
2834550a7375SFelipe Balbi 		 */
2835550a7375SFelipe Balbi 	} else if (is_host_active(musb)) {
2836550a7375SFelipe Balbi 		/* we know all the children are suspended; sometimes
2837550a7375SFelipe Balbi 		 * they will even be wakeup-enabled.
2838550a7375SFelipe Balbi 		 */
2839550a7375SFelipe Balbi 	}
2840550a7375SFelipe Balbi 
2841c338412bSDaniel Mack 	musb_save_context(musb);
2842c338412bSDaniel Mack 
2843550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2844550a7375SFelipe Balbi 	return 0;
2845550a7375SFelipe Balbi }
2846550a7375SFelipe Balbi 
musb_resume(struct device * dev)28473e87d9a3SSebastian Andrzej Siewior static int musb_resume(struct device *dev)
2848550a7375SFelipe Balbi {
2849c338412bSDaniel Mack 	struct musb *musb = dev_to_musb(dev);
2850ea2f35c0STony Lindgren 	unsigned long flags;
2851ea2f35c0STony Lindgren 	int error;
2852b87fd2f7SSebastian Andrzej Siewior 	u8 devctl;
2853b87fd2f7SSebastian Andrzej Siewior 	u8 mask;
2854c338412bSDaniel Mack 
2855c338412bSDaniel Mack 	/*
2856c338412bSDaniel Mack 	 * For static cmos like DaVinci, register values were preserved
28570ec8fd70SKim Kyuwon 	 * unless for some reason the whole soc powered down or the USB
28580ec8fd70SKim Kyuwon 	 * module got reset through the PSC (vs just being disabled).
2859c338412bSDaniel Mack 	 *
2860c338412bSDaniel Mack 	 * For the DSPS glue layer though, a full register restore has to
2861c338412bSDaniel Mack 	 * be done. As it shouldn't harm other platforms, we do it
2862c338412bSDaniel Mack 	 * unconditionally.
2863550a7375SFelipe Balbi 	 */
2864c338412bSDaniel Mack 
2865c338412bSDaniel Mack 	musb_restore_context(musb);
2866c338412bSDaniel Mack 
2867b87fd2f7SSebastian Andrzej Siewior 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2868b87fd2f7SSebastian Andrzej Siewior 	mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
2869b87fd2f7SSebastian Andrzej Siewior 	if ((devctl & mask) != (musb->context.devctl & mask))
2870b87fd2f7SSebastian Andrzej Siewior 		musb->port1_status = 0;
2871a1fc1920SSebastian Andrzej Siewior 
287217539f2fSAndreas Kemnade 	musb_enable_interrupts(musb);
287317539f2fSAndreas Kemnade 	musb_platform_enable(musb);
28746fc6f4b8SPascal Huerst 
28757f88a5acSBin Liu 	/* session might be disabled in suspend */
28767f88a5acSBin Liu 	if (musb->port_mode == MUSB_HOST &&
28777f88a5acSBin Liu 	    !(musb->ops->quirks & MUSB_PRESERVE_SESSION)) {
28787f88a5acSBin Liu 		devctl |= MUSB_DEVCTL_SESSION;
28797f88a5acSBin Liu 		musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
28807f88a5acSBin Liu 	}
28817f88a5acSBin Liu 
2882ea2f35c0STony Lindgren 	spin_lock_irqsave(&musb->lock, flags);
2883ea2f35c0STony Lindgren 	error = musb_run_resume_work(musb);
2884ea2f35c0STony Lindgren 	if (error)
2885ea2f35c0STony Lindgren 		dev_err(musb->controller, "resume work failed with %i\n",
2886ea2f35c0STony Lindgren 			error);
2887ea2f35c0STony Lindgren 	spin_unlock_irqrestore(&musb->lock, flags);
2888ea2f35c0STony Lindgren 
2889082df8beSJohan Hovold 	pm_runtime_mark_last_busy(dev);
2890082df8beSJohan Hovold 	pm_runtime_put_autosuspend(dev);
2891082df8beSJohan Hovold 
2892550a7375SFelipe Balbi 	return 0;
2893550a7375SFelipe Balbi }
2894550a7375SFelipe Balbi 
musb_runtime_suspend(struct device * dev)28957acc6197SHema HK static int musb_runtime_suspend(struct device *dev)
28967acc6197SHema HK {
28977acc6197SHema HK 	struct musb	*musb = dev_to_musb(dev);
28987acc6197SHema HK 
28997acc6197SHema HK 	musb_save_context(musb);
2900ea2f35c0STony Lindgren 	musb->is_runtime_suspended = 1;
29017acc6197SHema HK 
29027acc6197SHema HK 	return 0;
29037acc6197SHema HK }
29047acc6197SHema HK 
musb_runtime_resume(struct device * dev)29057acc6197SHema HK static int musb_runtime_resume(struct device *dev)
29067acc6197SHema HK {
29077acc6197SHema HK 	struct musb *musb = dev_to_musb(dev);
2908ea2f35c0STony Lindgren 	unsigned long flags;
2909ea2f35c0STony Lindgren 	int error;
29107acc6197SHema HK 
29117acc6197SHema HK 	/*
29127acc6197SHema HK 	 * When pm_runtime_get_sync called for the first time in driver
29137acc6197SHema HK 	 * init,  some of the structure is still not initialized which is
29147acc6197SHema HK 	 * used in restore function. But clock needs to be
29157acc6197SHema HK 	 * enabled before any register access, so
29167acc6197SHema HK 	 * pm_runtime_get_sync has to be called.
29177acc6197SHema HK 	 * Also context restore without save does not make
29187acc6197SHema HK 	 * any sense
29197acc6197SHema HK 	 */
2920c723bd6eSTony Lindgren 	if (!musb->is_initialized)
2921c723bd6eSTony Lindgren 		return 0;
2922c723bd6eSTony Lindgren 
29237acc6197SHema HK 	musb_restore_context(musb);
29247acc6197SHema HK 
2925ea2f35c0STony Lindgren 	spin_lock_irqsave(&musb->lock, flags);
2926ea2f35c0STony Lindgren 	error = musb_run_resume_work(musb);
2927ea2f35c0STony Lindgren 	if (error)
2928ea2f35c0STony Lindgren 		dev_err(musb->controller, "resume work failed with %i\n",
2929ea2f35c0STony Lindgren 			error);
2930ea2f35c0STony Lindgren 	musb->is_runtime_suspended = 0;
2931ea2f35c0STony Lindgren 	spin_unlock_irqrestore(&musb->lock, flags);
2932ea2f35c0STony Lindgren 
29337acc6197SHema HK 	return 0;
29347acc6197SHema HK }
29357acc6197SHema HK 
293647145210SAlexey Dobriyan static const struct dev_pm_ops musb_dev_pm_ops = {
293748fea965SMagnus Damm 	.suspend	= musb_suspend,
29383e87d9a3SSebastian Andrzej Siewior 	.resume		= musb_resume,
29397acc6197SHema HK 	.runtime_suspend = musb_runtime_suspend,
29407acc6197SHema HK 	.runtime_resume = musb_runtime_resume,
294148fea965SMagnus Damm };
294248fea965SMagnus Damm 
294348fea965SMagnus Damm #define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
2944550a7375SFelipe Balbi #else
294548fea965SMagnus Damm #define	MUSB_DEV_PM_OPS	NULL
2946550a7375SFelipe Balbi #endif
2947550a7375SFelipe Balbi 
2948550a7375SFelipe Balbi static struct platform_driver musb_driver = {
2949550a7375SFelipe Balbi 	.driver = {
29502f41c8a2SCorentin Labbe 		.name		= musb_driver_name,
2951550a7375SFelipe Balbi 		.bus		= &platform_bus_type,
295248fea965SMagnus Damm 		.pm		= MUSB_DEV_PM_OPS,
2953d3b5e319SGreg Kroah-Hartman 		.dev_groups	= musb_groups,
2954550a7375SFelipe Balbi 	},
2955e9e8c85eSFelipe Balbi 	.probe		= musb_probe,
2956aa846a29SUwe Kleine-König 	.remove_new	= musb_remove,
2957550a7375SFelipe Balbi };
2958550a7375SFelipe Balbi 
295989f836a8SEzequiel Garcia module_platform_driver(musb_driver);
2960