blob: 239ad0b1ceb6d15ddff5fa3741ca97796e70e067 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * MUSB OTG driver core code
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35/*
36 * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
37 *
38 * This consists of a Host Controller Driver (HCD) and a peripheral
39 * controller driver implementing the "Gadget" API; OTG support is
40 * in the works. These are normal Linux-USB controller drivers which
41 * use IRQs and have no dedicated thread.
42 *
43 * This version of the driver has only been used with products from
44 * Texas Instruments. Those products integrate the Inventra logic
45 * with other DMA, IRQ, and bus modules, as well as other logic that
46 * needs to be reflected in this driver.
47 *
48 *
49 * NOTE: the original Mentor code here was pretty much a collection
50 * of mechanisms that don't seem to have been fully integrated/working
51 * for *any* Linux kernel version. This version aims at Linux 2.6.now,
52 * Key open issues include:
53 *
54 * - Lack of host-side transaction scheduling, for all transfer types.
55 * The hardware doesn't do it; instead, software must.
56 *
57 * This is not an issue for OTG devices that don't support external
58 * hubs, but for more "normal" USB hosts it's a user issue that the
59 * "multipoint" support doesn't scale in the expected ways. That
60 * includes DaVinci EVM in a common non-OTG mode.
61 *
62 * * Control and bulk use dedicated endpoints, and there's as
63 * yet no mechanism to either (a) reclaim the hardware when
64 * peripherals are NAKing, which gets complicated with bulk
65 * endpoints, or (b) use more than a single bulk endpoint in
66 * each direction.
67 *
68 * RESULT: one device may be perceived as blocking another one.
69 *
70 * * Interrupt and isochronous will dynamically allocate endpoint
71 * hardware, but (a) there's no record keeping for bandwidth;
72 * (b) in the common case that few endpoints are available, there
73 * is no mechanism to reuse endpoints to talk to multiple devices.
74 *
75 * RESULT: At one extreme, bandwidth can be overcommitted in
76 * some hardware configurations, no faults will be reported.
77 * At the other extreme, the bandwidth capabilities which do
78 * exist tend to be severely undercommitted. You can't yet hook
79 * up both a keyboard and a mouse to an external USB hub.
80 */
81
82/*
83 * This gets many kinds of configuration information:
84 * - Kconfig for everything user-configurable
Felipe Balbi550a7372008-07-24 12:27:36 +030085 * - platform_device for addressing, irq, and platform_data
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +053086 * - platform_data is mostly for board-specific information
David Brownellc767c1c2008-09-11 11:53:23 +030087 * (plus recentrly, SOC or family details)
Felipe Balbi550a7372008-07-24 12:27:36 +030088 *
89 * Most of the conditional compilation will (someday) vanish.
90 */
91
92#include <linux/module.h>
93#include <linux/kernel.h>
94#include <linux/sched.h>
95#include <linux/slab.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030096#include <linux/list.h>
97#include <linux/kobject.h>
Mike Frysinger93039612011-05-25 08:13:24 -040098#include <linux/prefetch.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030099#include <linux/platform_device.h>
100#include <linux/io.h>
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +0000101#include <linux/dma-mapping.h>
Felipe Balbi550a7372008-07-24 12:27:36 +0300102
Felipe Balbi550a7372008-07-24 12:27:36 +0300103#include "musb_core.h"
104
David Brownellf7f9d632009-03-31 12:32:12 -0700105#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +0300106
107
Felipe Balbi550a7372008-07-24 12:27:36 +0300108#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
109#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
110
Felipe Balbie8164f62008-08-10 21:22:35 +0300111#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300112
113#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
114
Felipe Balbi05ac10d2010-12-02 08:49:26 +0200115#define MUSB_DRIVER_NAME "musb-hdrc"
Felipe Balbi550a7372008-07-24 12:27:36 +0300116const char musb_driver_name[] = MUSB_DRIVER_NAME;
117
118MODULE_DESCRIPTION(DRIVER_INFO);
119MODULE_AUTHOR(DRIVER_AUTHOR);
120MODULE_LICENSE("GPL");
121MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
122
123
124/*-------------------------------------------------------------------------*/
125
126static inline struct musb *dev_to_musb(struct device *dev)
127{
Felipe Balbi550a7372008-07-24 12:27:36 +0300128 return dev_get_drvdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300129}
130
131/*-------------------------------------------------------------------------*/
132
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200133#ifndef CONFIG_BLACKFIN
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200134static int musb_ulpi_read(struct usb_phy *phy, u32 offset)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200135{
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200136 void __iomem *addr = phy->io_priv;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200137 int i = 0;
138 u8 r;
139 u8 power;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200140 int ret;
141
142 pm_runtime_get_sync(phy->io_dev);
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200143
144 /* Make sure the transceiver is not in low power mode */
145 power = musb_readb(addr, MUSB_POWER);
146 power &= ~MUSB_POWER_SUSPENDM;
147 musb_writeb(addr, MUSB_POWER, power);
148
149 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
150 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
151 */
152
153 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
154 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
155 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
156
157 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
158 & MUSB_ULPI_REG_CMPLT)) {
159 i++;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200160 if (i == 10000) {
161 ret = -ETIMEDOUT;
162 goto out;
163 }
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200164
165 }
166 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
167 r &= ~MUSB_ULPI_REG_CMPLT;
168 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
169
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200170 ret = musb_readb(addr, MUSB_ULPI_REG_DATA);
171
172out:
173 pm_runtime_put(phy->io_dev);
174
175 return ret;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200176}
177
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200178static int musb_ulpi_write(struct usb_phy *phy, u32 offset, u32 data)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200179{
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200180 void __iomem *addr = phy->io_priv;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200181 int i = 0;
182 u8 r = 0;
183 u8 power;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200184 int ret = 0;
185
186 pm_runtime_get_sync(phy->io_dev);
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200187
188 /* Make sure the transceiver is not in low power mode */
189 power = musb_readb(addr, MUSB_POWER);
190 power &= ~MUSB_POWER_SUSPENDM;
191 musb_writeb(addr, MUSB_POWER, power);
192
193 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
194 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
195 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
196
197 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
198 & MUSB_ULPI_REG_CMPLT)) {
199 i++;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200200 if (i == 10000) {
201 ret = -ETIMEDOUT;
202 goto out;
203 }
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200204 }
205
206 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
207 r &= ~MUSB_ULPI_REG_CMPLT;
208 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
209
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200210out:
211 pm_runtime_put(phy->io_dev);
212
213 return ret;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200214}
215#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530216#define musb_ulpi_read NULL
217#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200218#endif
219
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200220static struct usb_phy_io_ops musb_ulpi_access = {
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200221 .read = musb_ulpi_read,
222 .write = musb_ulpi_write,
223};
224
225/*-------------------------------------------------------------------------*/
226
Felipe Balbi7c925542010-12-01 14:23:48 +0200227#if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200228
Felipe Balbi550a7372008-07-24 12:27:36 +0300229/*
230 * Load an endpoint's FIFO
231 */
232void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
233{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300234 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300235 void __iomem *fifo = hw_ep->fifo;
236
Ajay Kumar Gupta603fe2b2012-07-20 11:07:24 +0530237 if (unlikely(len == 0))
238 return;
239
Felipe Balbi550a7372008-07-24 12:27:36 +0300240 prefetch((u8 *)src);
241
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300242 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300243 'T', hw_ep->epnum, fifo, len, src);
244
245 /* we can't assume unaligned reads work */
246 if (likely((0x01 & (unsigned long) src) == 0)) {
247 u16 index = 0;
248
249 /* best case is 32bit-aligned source address */
250 if ((0x02 & (unsigned long) src) == 0) {
251 if (len >= 4) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800252 iowrite32_rep(fifo, src + index, len >> 2);
Felipe Balbi550a7372008-07-24 12:27:36 +0300253 index += len & ~0x03;
254 }
255 if (len & 0x02) {
256 musb_writew(fifo, 0, *(u16 *)&src[index]);
257 index += 2;
258 }
259 } else {
260 if (len >= 2) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800261 iowrite16_rep(fifo, src + index, len >> 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300262 index += len & ~0x01;
263 }
264 }
265 if (len & 0x01)
266 musb_writeb(fifo, 0, src[index]);
267 } else {
268 /* byte aligned */
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800269 iowrite8_rep(fifo, src, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300270 }
271}
272
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300273#if !defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +0300274/*
275 * Unload an endpoint's FIFO
276 */
277void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
278{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300279 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300280 void __iomem *fifo = hw_ep->fifo;
281
Ajay Kumar Gupta603fe2b2012-07-20 11:07:24 +0530282 if (unlikely(len == 0))
283 return;
284
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300285 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300286 'R', hw_ep->epnum, fifo, len, dst);
287
288 /* we can't assume unaligned writes work */
289 if (likely((0x01 & (unsigned long) dst) == 0)) {
290 u16 index = 0;
291
292 /* best case is 32bit-aligned destination address */
293 if ((0x02 & (unsigned long) dst) == 0) {
294 if (len >= 4) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800295 ioread32_rep(fifo, dst, len >> 2);
Felipe Balbi550a7372008-07-24 12:27:36 +0300296 index = len & ~0x03;
297 }
298 if (len & 0x02) {
299 *(u16 *)&dst[index] = musb_readw(fifo, 0);
300 index += 2;
301 }
302 } else {
303 if (len >= 2) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800304 ioread16_rep(fifo, dst, len >> 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300305 index = len & ~0x01;
306 }
307 }
308 if (len & 0x01)
309 dst[index] = musb_readb(fifo, 0);
310 } else {
311 /* byte aligned */
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800312 ioread8_rep(fifo, dst, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300313 }
314}
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300315#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300316
317#endif /* normal PIO */
318
319
320/*-------------------------------------------------------------------------*/
321
322/* for high speed test mode; see USB 2.0 spec 7.1.20 */
323static const u8 musb_test_packet[53] = {
324 /* implicit SYNC then DATA0 to start */
325
326 /* JKJKJKJK x9 */
327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
328 /* JJKKJJKK x8 */
329 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
330 /* JJJJKKKK x8 */
331 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
332 /* JJJJJJJKKKKKKK x8 */
333 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
334 /* JJJJJJJK x8 */
335 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
336 /* JKKKKKKK x10, JK */
337 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
338
339 /* implicit CRC16 then EOP to end */
340};
341
342void musb_load_testpacket(struct musb *musb)
343{
344 void __iomem *regs = musb->endpoints[0].regs;
345
346 musb_ep_select(musb->mregs, 0);
347 musb_write_fifo(musb->control_ep,
348 sizeof(musb_test_packet), musb_test_packet);
349 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
350}
351
352/*-------------------------------------------------------------------------*/
353
Felipe Balbi550a7372008-07-24 12:27:36 +0300354/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300355 * Handles OTG hnp timeouts, such as b_ase0_brst
356 */
Felipe Balbia1565442012-08-07 14:00:50 +0300357static void musb_otg_timer_func(unsigned long data)
Felipe Balbi550a7372008-07-24 12:27:36 +0300358{
359 struct musb *musb = (struct musb *)data;
360 unsigned long flags;
361
362 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700363 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300365 dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300366 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700367 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300368 musb->is_active = 0;
369 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700370 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300371 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300372 dev_dbg(musb->controller, "HNP: %s timeout\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200373 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi743411b2010-12-01 13:22:05 +0200374 musb_platform_set_vbus(musb, 0);
David Brownellab983f2a2009-03-31 12:35:09 -0700375 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300376 break;
377 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300378 dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200379 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300380 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300381 spin_unlock_irqrestore(&musb->lock, flags);
382}
383
Felipe Balbi550a7372008-07-24 12:27:36 +0300384/*
David Brownellf7f9d632009-03-31 12:32:12 -0700385 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 */
387void musb_hnp_stop(struct musb *musb)
388{
Daniel Mack8b125df2013-04-10 21:55:50 +0200389 struct usb_hcd *hcd = musb->hcd;
Felipe Balbi550a7372008-07-24 12:27:36 +0300390 void __iomem *mbase = musb->mregs;
391 u8 reg;
392
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200393 dev_dbg(musb->controller, "HNP: stop from %s\n",
394 usb_otg_state_string(musb->xceiv->state));
David Brownellab983f2a2009-03-31 12:35:09 -0700395
David Brownell84e250f2009-03-31 12:30:04 -0700396 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300397 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300398 musb_g_disconnect(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300399 dev_dbg(musb->controller, "HNP: back to %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200400 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300401 break;
402 case OTG_STATE_B_HOST:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300403 dev_dbg(musb->controller, "HNP: Disabling HR\n");
Daniel Mack74c2e932013-04-10 21:55:45 +0200404 if (hcd)
405 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700406 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300407 MUSB_DEV_MODE(musb);
408 reg = musb_readb(mbase, MUSB_POWER);
409 reg |= MUSB_POWER_SUSPENDM;
410 musb_writeb(mbase, MUSB_POWER, reg);
411 /* REVISIT: Start SESSION_REQUEST here? */
412 break;
413 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300414 dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200415 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300416 }
417
418 /*
419 * When returning to A state after HNP, avoid hub_port_rebounce(),
420 * which cause occasional OPT A "Did not receive reset after connect"
421 * errors.
422 */
Alan Stern749da5f2010-03-04 17:05:08 -0500423 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300424}
425
Felipe Balbi550a7372008-07-24 12:27:36 +0300426/*
427 * Interrupt Service Routine to record USB "global" interrupts.
428 * Since these do not happen often and signify things of
429 * paramount importance, it seems OK to check them individually;
430 * the order of the tests is specified in the manual
431 *
432 * @param musb instance pointer
433 * @param int_usb register contents
434 * @param devctl
435 * @param power
436 */
437
Felipe Balbi550a7372008-07-24 12:27:36 +0300438static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100439 u8 devctl)
Felipe Balbi550a7372008-07-24 12:27:36 +0300440{
Heikki Krogerusd445b6d2012-02-13 13:24:15 +0200441 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi550a7372008-07-24 12:27:36 +0300442 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300443
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100444 dev_dbg(musb->controller, "<== DevCtl=%02x, int_usb=0x%x\n", devctl,
Felipe Balbi550a7372008-07-24 12:27:36 +0300445 int_usb);
446
447 /* in host mode, the peripheral may issue remote wakeup.
448 * in peripheral mode, the host may resume the link.
449 * spurious RESUME irqs happen too, paired with SUSPEND.
450 */
451 if (int_usb & MUSB_INTR_RESUME) {
452 handled = IRQ_HANDLED;
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200453 dev_dbg(musb->controller, "RESUME (%s)\n", usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300454
455 if (devctl & MUSB_DEVCTL_HM) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200456 void __iomem *mbase = musb->mregs;
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100457 u8 power;
Felipe Balbiaa471452010-03-12 10:27:24 +0200458
David Brownell84e250f2009-03-31 12:30:04 -0700459 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300460 case OTG_STATE_A_SUSPEND:
461 /* remote wakeup? later, GetPortStatus
462 * will stop RESUME signaling
463 */
464
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100465 power = musb_readb(musb->mregs, MUSB_POWER);
Felipe Balbi550a7372008-07-24 12:27:36 +0300466 if (power & MUSB_POWER_SUSPENDM) {
467 /* spurious */
468 musb->int_usb &= ~MUSB_INTR_SUSPEND;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300469 dev_dbg(musb->controller, "Spurious SUSPENDM\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300470 break;
471 }
472
473 power &= ~MUSB_POWER_SUSPENDM;
474 musb_writeb(mbase, MUSB_POWER,
475 power | MUSB_POWER_RESUME);
476
477 musb->port1_status |=
478 (USB_PORT_STAT_C_SUSPEND << 16)
479 | MUSB_PORT_STAT_RESUME;
Daniel Mack30d361b2014-01-15 14:09:49 +0100480 musb->rh_timer = jiffies
481 + msecs_to_jiffies(20);
Daniel Mack8ed1fb72013-12-18 20:23:46 +0100482 schedule_delayed_work(
Daniel Mack9ccfaf72014-02-05 15:34:18 +0100483 &musb->finish_resume_work,
484 msecs_to_jiffies(20));
Felipe Balbi550a7372008-07-24 12:27:36 +0300485
David Brownell84e250f2009-03-31 12:30:04 -0700486 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300487 musb->is_active = 1;
Daniel Mack0b3eba42013-04-10 21:55:42 +0200488 musb_host_resume_root_hub(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300489 break;
490 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700491 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300492 musb->is_active = 1;
493 MUSB_DEV_MODE(musb);
494 break;
495 default:
496 WARNING("bogus %s RESUME (%s)\n",
497 "host",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200498 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300499 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300500 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700501 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300502 case OTG_STATE_A_SUSPEND:
503 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700504 musb->xceiv->state = OTG_STATE_A_HOST;
Daniel Mack0b3eba42013-04-10 21:55:42 +0200505 musb_host_resume_root_hub(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300506 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300507 case OTG_STATE_B_WAIT_ACON:
508 case OTG_STATE_B_PERIPHERAL:
509 /* disconnect while suspended? we may
510 * not get a disconnect irq...
511 */
512 if ((devctl & MUSB_DEVCTL_VBUS)
513 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
514 ) {
515 musb->int_usb |= MUSB_INTR_DISCONNECT;
516 musb->int_usb &= ~MUSB_INTR_SUSPEND;
517 break;
518 }
519 musb_g_resume(musb);
520 break;
521 case OTG_STATE_B_IDLE:
522 musb->int_usb &= ~MUSB_INTR_SUSPEND;
523 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300524 default:
525 WARNING("bogus %s RESUME (%s)\n",
526 "peripheral",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200527 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300528 }
529 }
530 }
531
Felipe Balbi550a7372008-07-24 12:27:36 +0300532 /* see manual for the order of the tests */
533 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200534 void __iomem *mbase = musb->mregs;
535
Heikki Krogerus19aab562010-10-29 04:23:27 -0500536 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
537 && (devctl & MUSB_DEVCTL_BDEVICE)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300538 dev_dbg(musb->controller, "SessReq while on B state\n");
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300539 return IRQ_HANDLED;
540 }
541
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300542 dev_dbg(musb->controller, "SESSION_REQUEST (%s)\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200543 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300544
545 /* IRQ arrives from ID pin sense or (later, if VBUS power
546 * is removed) SRP. responses are time critical:
547 * - turn on VBUS (with silicon-specific mechanism)
548 * - go through A_WAIT_VRISE
549 * - ... to A_WAIT_BCON.
550 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
551 */
552 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
553 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700554 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300555 MUSB_HST_MODE(musb);
Felipe Balbi743411b2010-12-01 13:22:05 +0200556 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300557
558 handled = IRQ_HANDLED;
559 }
560
561 if (int_usb & MUSB_INTR_VBUSERROR) {
562 int ignore = 0;
563
564 /* During connection as an A-Device, we may see a short
565 * current spikes causing voltage drop, because of cable
566 * and peripheral capacitance combined with vbus draw.
567 * (So: less common with truly self-powered devices, where
568 * vbus doesn't act like a power supply.)
569 *
570 * Such spikes are short; usually less than ~500 usec, max
571 * of ~2 msec. That is, they're not sustained overcurrent
572 * errors, though they're reported using VBUSERROR irqs.
573 *
574 * Workarounds: (a) hardware: use self powered devices.
575 * (b) software: ignore non-repeated VBUS errors.
576 *
577 * REVISIT: do delays from lots of DEBUG_KERNEL checks
578 * make trouble here, keeping VBUS < 4.4V ?
579 */
David Brownell84e250f2009-03-31 12:30:04 -0700580 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300581 case OTG_STATE_A_HOST:
582 /* recovery is dicey once we've gotten past the
583 * initial stages of enumeration, but if VBUS
584 * stayed ok at the other end of the link, and
585 * another reset is due (at least for high speed,
586 * to redo the chirp etc), it might work OK...
587 */
588 case OTG_STATE_A_WAIT_BCON:
589 case OTG_STATE_A_WAIT_VRISE:
590 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200591 void __iomem *mbase = musb->mregs;
592
Felipe Balbi550a7372008-07-24 12:27:36 +0300593 musb->vbuserr_retry--;
594 ignore = 1;
595 devctl |= MUSB_DEVCTL_SESSION;
596 musb_writeb(mbase, MUSB_DEVCTL, devctl);
597 } else {
598 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500599 USB_PORT_STAT_OVERCURRENT
600 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300601 }
602 break;
603 default:
604 break;
605 }
606
Grazvydas Ignotas54485112013-03-10 02:49:28 +0200607 dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
608 "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200609 usb_otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300610 devctl,
611 ({ char *s;
612 switch (devctl & MUSB_DEVCTL_VBUS) {
613 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
614 s = "<SessEnd"; break;
615 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
616 s = "<AValid"; break;
617 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
618 s = "<VBusValid"; break;
619 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
620 default:
621 s = "VALID"; break;
Joe Perches2b84f922013-10-08 16:01:37 -0700622 } s; }),
Felipe Balbi550a7372008-07-24 12:27:36 +0300623 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
624 musb->port1_status);
625
626 /* go through A_WAIT_VFALL then start a new session */
627 if (!ignore)
Felipe Balbi743411b2010-12-01 13:22:05 +0200628 musb_platform_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300629 handled = IRQ_HANDLED;
630 }
631
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200632 if (int_usb & MUSB_INTR_SUSPEND) {
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100633 dev_dbg(musb->controller, "SUSPEND (%s) devctl %02x\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200634 usb_otg_state_string(musb->xceiv->state), devctl);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200635 handled = IRQ_HANDLED;
636
637 switch (musb->xceiv->state) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200638 case OTG_STATE_A_PERIPHERAL:
639 /* We also come here if the cable is removed, since
640 * this silicon doesn't report ID-no-longer-grounded.
641 *
642 * We depend on T(a_wait_bcon) to shut us down, and
643 * hope users don't do anything dicey during this
644 * undesired detour through A_WAIT_BCON.
645 */
646 musb_hnp_stop(musb);
Daniel Mack0b3eba42013-04-10 21:55:42 +0200647 musb_host_resume_root_hub(musb);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200648 musb_root_disconnect(musb);
649 musb_platform_try_idle(musb, jiffies
650 + msecs_to_jiffies(musb->a_wait_bcon
651 ? : OTG_TIME_A_WAIT_BCON));
652
653 break;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200654 case OTG_STATE_B_IDLE:
655 if (!musb->is_active)
656 break;
657 case OTG_STATE_B_PERIPHERAL:
658 musb_g_suspend(musb);
Felipe Balbi032ec492011-11-24 15:46:26 +0200659 musb->is_active = otg->gadget->b_hnp_enable;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200660 if (musb->is_active) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200661 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300662 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200663 mod_timer(&musb->otg_timer, jiffies
664 + msecs_to_jiffies(
665 OTG_TIME_B_ASE0_BRST));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200666 }
667 break;
668 case OTG_STATE_A_WAIT_BCON:
669 if (musb->a_wait_bcon != 0)
670 musb_platform_try_idle(musb, jiffies
671 + msecs_to_jiffies(musb->a_wait_bcon));
672 break;
673 case OTG_STATE_A_HOST:
674 musb->xceiv->state = OTG_STATE_A_SUSPEND;
Felipe Balbi032ec492011-11-24 15:46:26 +0200675 musb->is_active = otg->host->b_hnp_enable;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200676 break;
677 case OTG_STATE_B_HOST:
678 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300679 dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200680 break;
681 default:
682 /* "should not happen" */
683 musb->is_active = 0;
684 break;
685 }
686 }
687
Felipe Balbi550a7372008-07-24 12:27:36 +0300688 if (int_usb & MUSB_INTR_CONNECT) {
Daniel Mack8b125df2013-04-10 21:55:50 +0200689 struct usb_hcd *hcd = musb->hcd;
Felipe Balbi550a7372008-07-24 12:27:36 +0300690
691 handled = IRQ_HANDLED;
692 musb->is_active = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300693
694 musb->ep0_stage = MUSB_EP0_START;
695
Felipe Balbi550a7372008-07-24 12:27:36 +0300696 /* flush endpoints when transitioning from Device Mode */
697 if (is_peripheral_active(musb)) {
698 /* REVISIT HNP; just force disconnect */
699 }
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100700 musb->intrtxe = musb->epmask;
701 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +0100702 musb->intrrxe = musb->epmask & 0xfffe;
703 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530704 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300705 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
706 |USB_PORT_STAT_HIGH_SPEED
707 |USB_PORT_STAT_ENABLE
708 );
709 musb->port1_status |= USB_PORT_STAT_CONNECTION
710 |(USB_PORT_STAT_C_CONNECTION << 16);
711
712 /* high vs full speed is just a guess until after reset */
713 if (devctl & MUSB_DEVCTL_LSDEV)
714 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
715
Felipe Balbi550a7372008-07-24 12:27:36 +0300716 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700717 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300718 case OTG_STATE_B_PERIPHERAL:
719 if (int_usb & MUSB_INTR_SUSPEND) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300720 dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300721 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700722 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300723 } else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300724 dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300725 break;
726 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300727 dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
David Brownell1de00da2009-04-02 10:16:11 -0700728b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700729 musb->xceiv->state = OTG_STATE_B_HOST;
Daniel Mack74c2e932013-04-10 21:55:45 +0200730 if (musb->hcd)
731 musb->hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700732 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300733 break;
734 default:
735 if ((devctl & MUSB_DEVCTL_VBUS)
736 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700737 musb->xceiv->state = OTG_STATE_A_HOST;
Daniel Mack0b3eba42013-04-10 21:55:42 +0200738 if (hcd)
739 hcd->self.is_b_host = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300740 }
741 break;
742 }
David Brownell1de00da2009-04-02 10:16:11 -0700743
Daniel Mack0b3eba42013-04-10 21:55:42 +0200744 musb_host_poke_root_hub(musb);
David Brownell1de00da2009-04-02 10:16:11 -0700745
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300746 dev_dbg(musb->controller, "CONNECT (%s) devctl %02x\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200747 usb_otg_state_string(musb->xceiv->state), devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300748 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300749
Felipe Balbi6d349672013-04-29 12:02:24 +0300750 if (int_usb & MUSB_INTR_DISCONNECT) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300751 dev_dbg(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200752 usb_otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300753 MUSB_MODE(musb), devctl);
754 handled = IRQ_HANDLED;
755
David Brownell84e250f2009-03-31 12:30:04 -0700756 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300757 case OTG_STATE_A_HOST:
758 case OTG_STATE_A_SUSPEND:
Daniel Mack0b3eba42013-04-10 21:55:42 +0200759 musb_host_resume_root_hub(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300760 musb_root_disconnect(musb);
Felipe Balbi032ec492011-11-24 15:46:26 +0200761 if (musb->a_wait_bcon != 0)
Felipe Balbi550a7372008-07-24 12:27:36 +0300762 musb_platform_try_idle(musb, jiffies
763 + msecs_to_jiffies(musb->a_wait_bcon));
764 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300765 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700766 /* REVISIT this behaves for "real disconnect"
767 * cases; make sure the other transitions from
768 * from B_HOST act right too. The B_HOST code
769 * in hnp_stop() is currently not used...
770 */
771 musb_root_disconnect(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +0200772 if (musb->hcd)
773 musb->hcd->self.is_b_host = 0;
David Brownellab983f2a2009-03-31 12:35:09 -0700774 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
775 MUSB_DEV_MODE(musb);
776 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300777 break;
778 case OTG_STATE_A_PERIPHERAL:
779 musb_hnp_stop(musb);
780 musb_root_disconnect(musb);
781 /* FALLTHROUGH */
782 case OTG_STATE_B_WAIT_ACON:
783 /* FALLTHROUGH */
Felipe Balbi550a7372008-07-24 12:27:36 +0300784 case OTG_STATE_B_PERIPHERAL:
785 case OTG_STATE_B_IDLE:
786 musb_g_disconnect(musb);
787 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300788 default:
789 WARNING("unhandled DISCONNECT transition (%s)\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200790 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300791 break;
792 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300793 }
794
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200795 /* mentor saves a bit: bus reset and babble share the same irq.
796 * only host sees babble; only peripheral sees bus reset.
797 */
798 if (int_usb & MUSB_INTR_RESET) {
799 handled = IRQ_HANDLED;
Felipe Balbia04d46d2011-11-24 15:46:27 +0200800 if ((devctl & MUSB_DEVCTL_HM) != 0) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200801 /*
802 * Looks like non-HS BABBLE can be ignored, but
803 * HS BABBLE is an error condition. For HS the solution
804 * is to avoid babble in the first place and fix what
805 * caused BABBLE. When HS BABBLE happens we can only
806 * stop the session.
807 */
808 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300809 dev_dbg(musb->controller, "BABBLE devctl: %02x\n", devctl);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200810 else {
811 ERR("Stopping host session -- babble\n");
812 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
813 }
Felipe Balbia04d46d2011-11-24 15:46:27 +0200814 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300815 dev_dbg(musb->controller, "BUS RESET as %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200816 usb_otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200817 switch (musb->xceiv->state) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200818 case OTG_STATE_A_SUSPEND:
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200819 musb_g_reset(musb);
820 /* FALLTHROUGH */
821 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
822 /* never use invalid T(a_wait_bcon) */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300823 dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200824 usb_otg_state_string(musb->xceiv->state),
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200825 TA_WAIT_BCON(musb));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200826 mod_timer(&musb->otg_timer, jiffies
827 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
828 break;
829 case OTG_STATE_A_PERIPHERAL:
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200830 del_timer(&musb->otg_timer);
831 musb_g_reset(musb);
832 break;
833 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300834 dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200835 usb_otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200836 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
837 musb_g_reset(musb);
838 break;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200839 case OTG_STATE_B_IDLE:
840 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
841 /* FALLTHROUGH */
842 case OTG_STATE_B_PERIPHERAL:
843 musb_g_reset(musb);
844 break;
845 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300846 dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200847 usb_otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200848 }
849 }
850 }
851
852#if 0
853/* REVISIT ... this would be for multiplexing periodic endpoints, or
854 * supporting transfer phasing to prevent exceeding ISO bandwidth
855 * limits of a given frame or microframe.
856 *
857 * It's not needed for peripheral side, which dedicates endpoints;
858 * though it _might_ use SOF irqs for other purposes.
859 *
860 * And it's not currently needed for host side, which also dedicates
861 * endpoints, relies on TX/RX interval registers, and isn't claimed
862 * to support ISO transfers yet.
863 */
864 if (int_usb & MUSB_INTR_SOF) {
865 void __iomem *mbase = musb->mregs;
866 struct musb_hw_ep *ep;
867 u8 epnum;
868 u16 frame;
869
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300870 dev_dbg(musb->controller, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300871 handled = IRQ_HANDLED;
872
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200873 /* start any periodic Tx transfers waiting for current frame */
874 frame = musb_readw(mbase, MUSB_FRAME);
875 ep = musb->endpoints;
876 for (epnum = 1; (epnum < musb->nr_endpoints)
877 && (musb->epmask >= (1 << epnum));
878 epnum++, ep++) {
879 /*
880 * FIXME handle framecounter wraps (12 bits)
881 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300882 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200883 if (ep->dwWaitFrame >= frame) {
884 ep->dwWaitFrame = 0;
885 pr_debug("SOF --> periodic TX%s on %d\n",
886 ep->tx_channel ? " DMA" : "",
887 epnum);
888 if (!ep->tx_channel)
889 musb_h_tx_start(musb, epnum);
890 else
891 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300892 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200893 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300894 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200895#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300896
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200897 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300898
899 return handled;
900}
901
902/*-------------------------------------------------------------------------*/
903
Felipe Balbi550a7372008-07-24 12:27:36 +0300904static void musb_generic_disable(struct musb *musb)
905{
906 void __iomem *mbase = musb->mregs;
907 u16 temp;
908
909 /* disable interrupts */
910 musb_writeb(mbase, MUSB_INTRUSBE, 0);
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100911 musb->intrtxe = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300912 musb_writew(mbase, MUSB_INTRTXE, 0);
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +0100913 musb->intrrxe = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300914 musb_writew(mbase, MUSB_INTRRXE, 0);
915
916 /* off */
917 musb_writeb(mbase, MUSB_DEVCTL, 0);
918
919 /* flush pending interrupts */
920 temp = musb_readb(mbase, MUSB_INTRUSB);
921 temp = musb_readw(mbase, MUSB_INTRTX);
922 temp = musb_readw(mbase, MUSB_INTRRX);
923
924}
925
926/*
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +0200927 * Program the HDRC to start (enable interrupts, dma, etc.).
928 */
929void musb_start(struct musb *musb)
930{
931 void __iomem *regs = musb->mregs;
932 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
933
934 dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
935
936 /* Set INT enable registers, enable interrupts */
937 musb->intrtxe = musb->epmask;
938 musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
939 musb->intrrxe = musb->epmask & 0xfffe;
940 musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
941 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
942
943 musb_writeb(regs, MUSB_TESTMODE, 0);
944
945 /* put into basic highspeed mode and start session */
946 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
947 | MUSB_POWER_HSENAB
948 /* ENSUSPEND wedges tusb */
949 /* | MUSB_POWER_ENSUSPEND */
950 );
951
952 musb->is_active = 0;
953 devctl = musb_readb(regs, MUSB_DEVCTL);
954 devctl &= ~MUSB_DEVCTL_SESSION;
955
956 /* session started after:
957 * (a) ID-grounded irq, host mode;
958 * (b) vbus present/connect IRQ, peripheral mode;
959 * (c) peripheral initiates, using SRP
960 */
961 if (musb->port_mode != MUSB_PORT_MODE_HOST &&
962 (devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
963 musb->is_active = 1;
964 } else {
965 devctl |= MUSB_DEVCTL_SESSION;
966 }
967
968 musb_platform_enable(musb);
969 musb_writeb(regs, MUSB_DEVCTL, devctl);
970}
971
972/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300973 * Make the HDRC stop (disable interrupts, etc.);
974 * reversible by musb_start
975 * called on gadget driver unregister
976 * with controller locked, irqs blocked
977 * acts as a NOP unless some role activated the hardware
978 */
979void musb_stop(struct musb *musb)
980{
981 /* stop IRQs, timers, ... */
982 musb_platform_disable(musb);
983 musb_generic_disable(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300984 dev_dbg(musb->controller, "HDRC disabled\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300985
986 /* FIXME
987 * - mark host and/or peripheral drivers unusable/inactive
988 * - disable DMA (and enable it in HdrcStart)
989 * - make sure we can musb_start() after musb_stop(); with
990 * OTG mode, gadget driver module rmmod/modprobe cycles that
991 * - ...
992 */
993 musb_platform_try_idle(musb, 0);
994}
995
996static void musb_shutdown(struct platform_device *pdev)
997{
998 struct musb *musb = dev_to_musb(&pdev->dev);
999 unsigned long flags;
1000
Hema HK4f9edd22011-03-22 16:02:12 +05301001 pm_runtime_get_sync(musb->controller);
Grazvydas Ignotas24307ca2012-01-12 15:22:45 +02001002
Daniel Mack2cc65fe2013-04-10 21:55:47 +02001003 musb_host_cleanup(musb);
Grazvydas Ignotas24307ca2012-01-12 15:22:45 +02001004 musb_gadget_cleanup(musb);
1005
Felipe Balbi550a7372008-07-24 12:27:36 +03001006 spin_lock_irqsave(&musb->lock, flags);
1007 musb_platform_disable(musb);
1008 musb_generic_disable(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001009 spin_unlock_irqrestore(&musb->lock, flags);
1010
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001011 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1012 musb_platform_exit(musb);
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001013
Hema HK4f9edd22011-03-22 16:02:12 +05301014 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001015 /* FIXME power down */
1016}
1017
1018
1019/*-------------------------------------------------------------------------*/
1020
1021/*
1022 * The silicon either has hard-wired endpoint configurations, or else
1023 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001024 * writing only the dynamic sizing is very well tested. Since we switched
1025 * away from compile-time hardware parameters, we can no longer rely on
1026 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001027 *
1028 * We don't currently use dynamic fifo setup capability to do anything
1029 * more than selecting one of a bunch of predefined configurations.
1030 */
Felipe Balbiee34e512011-06-29 12:45:03 +03001031#if defined(CONFIG_USB_MUSB_TUSB6010) \
1032 || defined(CONFIG_USB_MUSB_TUSB6010_MODULE) \
1033 || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
1034 || defined(CONFIG_USB_MUSB_OMAP2PLUS_MODULE) \
1035 || defined(CONFIG_USB_MUSB_AM35X) \
Ajay Kumar Gupta9ecb8872012-03-12 19:30:22 +05301036 || defined(CONFIG_USB_MUSB_AM35X_MODULE) \
1037 || defined(CONFIG_USB_MUSB_DSPS) \
1038 || defined(CONFIG_USB_MUSB_DSPS_MODULE)
Bill Pembertond3608b62012-11-19 13:24:34 -05001039static ushort fifo_mode = 4;
Felipe Balbiee34e512011-06-29 12:45:03 +03001040#elif defined(CONFIG_USB_MUSB_UX500) \
1041 || defined(CONFIG_USB_MUSB_UX500_MODULE)
Bill Pembertond3608b62012-11-19 13:24:34 -05001042static ushort fifo_mode = 5;
Felipe Balbi550a7372008-07-24 12:27:36 +03001043#else
Bill Pembertond3608b62012-11-19 13:24:34 -05001044static ushort fifo_mode = 2;
Felipe Balbi550a7372008-07-24 12:27:36 +03001045#endif
1046
1047/* "modprobe ... fifo_mode=1" etc */
1048module_param(fifo_mode, ushort, 0);
1049MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1050
Felipe Balbi550a7372008-07-24 12:27:36 +03001051/*
1052 * tables defining fifo_mode values. define more if you like.
1053 * for host side, make sure both halves of ep1 are set up.
1054 */
1055
1056/* mode 0 - fits in 2KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001057static struct musb_fifo_cfg mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001058{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1059{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1060{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1061{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1062{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1063};
1064
1065/* mode 1 - fits in 4KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001066static struct musb_fifo_cfg mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001067{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1068{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1069{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1070{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1071{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1072};
1073
1074/* mode 2 - fits in 4KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001075static struct musb_fifo_cfg mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001076{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1077{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1078{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1079{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1080{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1081{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1082};
1083
1084/* mode 3 - fits in 4KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001085static struct musb_fifo_cfg mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001086{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1087{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1088{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1089{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1090{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1091{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1092};
1093
1094/* mode 4 - fits in 16KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001095static struct musb_fifo_cfg mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001096{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1097{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1098{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1099{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1100{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1101{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1102{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1103{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1104{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1105{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1106{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1107{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1108{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1109{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1110{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1111{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1112{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1113{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001114{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1115{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1116{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1117{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1118{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1119{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1120{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001121{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1122{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1123};
1124
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001125/* mode 5 - fits in 8KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001126static struct musb_fifo_cfg mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001127{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1128{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1129{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1130{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1131{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1132{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1133{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1134{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1135{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1136{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1137{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1138{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1139{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1140{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1141{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1142{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1143{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1144{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1145{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1146{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1147{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1148{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1149{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1150{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1151{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1152{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1153{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1154};
Felipe Balbi550a7372008-07-24 12:27:36 +03001155
1156/*
1157 * configure a fifo; for non-shared endpoints, this may be called
1158 * once for a tx fifo and once for an rx fifo.
1159 *
1160 * returns negative errno or offset for next fifo.
1161 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001162static int
Felipe Balbi550a7372008-07-24 12:27:36 +03001163fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001164 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001165{
1166 void __iomem *mbase = musb->mregs;
1167 int size = 0;
1168 u16 maxpacket = cfg->maxpacket;
1169 u16 c_off = offset >> 3;
1170 u8 c_size;
1171
1172 /* expect hw_ep has already been zero-initialized */
1173
1174 size = ffs(max(maxpacket, (u16) 8)) - 1;
1175 maxpacket = 1 << size;
1176
1177 c_size = size - 3;
1178 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001179 if ((offset + (maxpacket << 1)) >
1180 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001181 return -EMSGSIZE;
1182 c_size |= MUSB_FIFOSZ_DPB;
1183 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001184 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001185 return -EMSGSIZE;
1186 }
1187
1188 /* configure the FIFO */
1189 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1190
Felipe Balbi550a7372008-07-24 12:27:36 +03001191 /* EP0 reserved endpoint for control, bidirectional;
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05301192 * EP1 reserved for bulk, two unidirectional halves.
Felipe Balbi550a7372008-07-24 12:27:36 +03001193 */
1194 if (hw_ep->epnum == 1)
1195 musb->bulk_ep = hw_ep;
1196 /* REVISIT error check: be sure ep0 can both rx and tx ... */
Felipe Balbi550a7372008-07-24 12:27:36 +03001197 switch (cfg->style) {
1198 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001199 musb_write_txfifosz(mbase, c_size);
1200 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001201 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1202 hw_ep->max_packet_sz_tx = maxpacket;
1203 break;
1204 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001205 musb_write_rxfifosz(mbase, c_size);
1206 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001207 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1208 hw_ep->max_packet_sz_rx = maxpacket;
1209 break;
1210 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001211 musb_write_txfifosz(mbase, c_size);
1212 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001213 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1214 hw_ep->max_packet_sz_rx = maxpacket;
1215
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001216 musb_write_rxfifosz(mbase, c_size);
1217 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001218 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1219 hw_ep->max_packet_sz_tx = maxpacket;
1220
1221 hw_ep->is_shared_fifo = true;
1222 break;
1223 }
1224
1225 /* NOTE rx and tx endpoint irqs aren't managed separately,
1226 * which happens to be ok
1227 */
1228 musb->epmask |= (1 << hw_ep->epnum);
1229
1230 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1231}
1232
Bill Pembertond3608b62012-11-19 13:24:34 -05001233static struct musb_fifo_cfg ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001234 .style = FIFO_RXTX, .maxpacket = 64,
1235};
1236
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001237static int ep_config_from_table(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001238{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001239 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001240 unsigned i, n;
1241 int offset;
1242 struct musb_hw_ep *hw_ep = musb->endpoints;
1243
Felipe Balbie6c213b2010-03-12 10:29:06 +02001244 if (musb->config->fifo_cfg) {
1245 cfg = musb->config->fifo_cfg;
1246 n = musb->config->fifo_cfg_size;
1247 goto done;
1248 }
1249
Felipe Balbi550a7372008-07-24 12:27:36 +03001250 switch (fifo_mode) {
1251 default:
1252 fifo_mode = 0;
1253 /* FALLTHROUGH */
1254 case 0:
1255 cfg = mode_0_cfg;
1256 n = ARRAY_SIZE(mode_0_cfg);
1257 break;
1258 case 1:
1259 cfg = mode_1_cfg;
1260 n = ARRAY_SIZE(mode_1_cfg);
1261 break;
1262 case 2:
1263 cfg = mode_2_cfg;
1264 n = ARRAY_SIZE(mode_2_cfg);
1265 break;
1266 case 3:
1267 cfg = mode_3_cfg;
1268 n = ARRAY_SIZE(mode_3_cfg);
1269 break;
1270 case 4:
1271 cfg = mode_4_cfg;
1272 n = ARRAY_SIZE(mode_4_cfg);
1273 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001274 case 5:
1275 cfg = mode_5_cfg;
1276 n = ARRAY_SIZE(mode_5_cfg);
1277 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001278 }
1279
1280 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1281 musb_driver_name, fifo_mode);
1282
1283
Felipe Balbie6c213b2010-03-12 10:29:06 +02001284done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001285 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1286 /* assert(offset > 0) */
1287
1288 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001289 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001290 */
1291
1292 for (i = 0; i < n; i++) {
1293 u8 epn = cfg->hw_ep_num;
1294
Felipe Balbica6d1b12008-08-08 12:40:54 +03001295 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001296 pr_debug("%s: invalid ep %d\n",
1297 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001298 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001299 }
1300 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1301 if (offset < 0) {
1302 pr_debug("%s: mem overrun, ep %d\n",
1303 musb_driver_name, epn);
Shubhrajyoti Df69dfa12012-08-07 19:56:31 +05301304 return offset;
Felipe Balbi550a7372008-07-24 12:27:36 +03001305 }
1306 epn++;
1307 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1308 }
1309
1310 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1311 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001312 n + 1, musb->config->num_eps * 2 - 1,
1313 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001314
Felipe Balbi550a7372008-07-24 12:27:36 +03001315 if (!musb->bulk_ep) {
1316 pr_debug("%s: missing bulk\n", musb_driver_name);
1317 return -EINVAL;
1318 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001319
1320 return 0;
1321}
1322
1323
1324/*
1325 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1326 * @param musb the controller
1327 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001328static int ep_config_from_hw(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001329{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001330 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001331 struct musb_hw_ep *hw_ep;
Felipe Balbia1565442012-08-07 14:00:50 +03001332 void __iomem *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001333 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001334
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001335 dev_dbg(musb->controller, "<== static silicon ep config\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001336
1337 /* FIXME pick up ep0 maxpacket size */
1338
Felipe Balbica6d1b12008-08-08 12:40:54 +03001339 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001340 musb_ep_select(mbase, epnum);
1341 hw_ep = musb->endpoints + epnum;
1342
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001343 ret = musb_read_fifosize(musb, hw_ep, epnum);
1344 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001345 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001346
1347 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1348
Felipe Balbi550a7372008-07-24 12:27:36 +03001349 /* pick an RX/TX endpoint for bulk */
1350 if (hw_ep->max_packet_sz_tx < 512
1351 || hw_ep->max_packet_sz_rx < 512)
1352 continue;
1353
1354 /* REVISIT: this algorithm is lazy, we should at least
1355 * try to pick a double buffered endpoint.
1356 */
1357 if (musb->bulk_ep)
1358 continue;
1359 musb->bulk_ep = hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03001360 }
1361
Felipe Balbi550a7372008-07-24 12:27:36 +03001362 if (!musb->bulk_ep) {
1363 pr_debug("%s: missing bulk\n", musb_driver_name);
1364 return -EINVAL;
1365 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001366
1367 return 0;
1368}
1369
1370enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1371
1372/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1373 * configure endpoints, or take their config from silicon
1374 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001375static int musb_core_init(u16 musb_type, struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001376{
Felipe Balbi550a7372008-07-24 12:27:36 +03001377 u8 reg;
1378 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301379 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001380 void __iomem *mbase = musb->mregs;
1381 int status = 0;
1382 int i;
1383
1384 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001385 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001386
1387 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001388 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001389 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001390 musb->dyn_fifo = true;
1391 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001392 if (reg & MUSB_CONFIGDATA_MPRXE) {
1393 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001394 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001395 }
1396 if (reg & MUSB_CONFIGDATA_MPTXE) {
1397 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001398 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001399 }
1400 if (reg & MUSB_CONFIGDATA_HBRXE) {
1401 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001402 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001403 }
1404 if (reg & MUSB_CONFIGDATA_HBTXE) {
1405 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001406 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001407 }
1408 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1409 strcat(aInfo, ", SoftConn");
1410
1411 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1412 musb_driver_name, reg, aInfo);
1413
Felipe Balbi550a7372008-07-24 12:27:36 +03001414 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001415 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1416 musb->is_multipoint = 1;
1417 type = "M";
1418 } else {
1419 musb->is_multipoint = 0;
1420 type = "";
Felipe Balbi550a7372008-07-24 12:27:36 +03001421#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1422 printk(KERN_ERR
1423 "%s: kernel must blacklist external hubs\n",
1424 musb_driver_name);
1425#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001426 }
1427
1428 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301429 musb->hwvers = musb_read_hwvers(mbase);
1430 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1431 MUSB_HWVERS_MINOR(musb->hwvers),
1432 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001433 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1434 musb_driver_name, type, aRevision, aDate);
1435
1436 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001437 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001438
1439 /* discover endpoint configuration */
1440 musb->nr_endpoints = 1;
1441 musb->epmask = 1;
1442
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001443 if (musb->dyn_fifo)
1444 status = ep_config_from_table(musb);
1445 else
1446 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001447
1448 if (status < 0)
1449 return status;
1450
1451 /* finish init, and print endpoint config */
1452 for (i = 0; i < musb->nr_endpoints; i++) {
1453 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1454
1455 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
Arnd Bergmann9a35f872011-10-02 16:45:47 +02001456#if defined(CONFIG_USB_MUSB_TUSB6010) || defined (CONFIG_USB_MUSB_TUSB6010_MODULE)
Felipe Balbi550a7372008-07-24 12:27:36 +03001457 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1458 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1459 hw_ep->fifo_sync_va =
1460 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1461
1462 if (i == 0)
1463 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1464 else
1465 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1466#endif
1467
1468 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001469 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001470 hw_ep->rx_reinit = 1;
1471 hw_ep->tx_reinit = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001472
1473 if (hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001474 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001475 "%s: hw_ep %d%s, %smax %d\n",
1476 musb_driver_name, i,
1477 hw_ep->is_shared_fifo ? "shared" : "tx",
1478 hw_ep->tx_double_buffered
1479 ? "doublebuffer, " : "",
1480 hw_ep->max_packet_sz_tx);
1481 }
1482 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001483 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001484 "%s: hw_ep %d%s, %smax %d\n",
1485 musb_driver_name, i,
1486 "rx",
1487 hw_ep->rx_double_buffered
1488 ? "doublebuffer, " : "",
1489 hw_ep->max_packet_sz_rx);
1490 }
1491 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001492 dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
Felipe Balbi550a7372008-07-24 12:27:36 +03001493 }
1494
1495 return 0;
1496}
1497
1498/*-------------------------------------------------------------------------*/
1499
Felipe Balbi550a7372008-07-24 12:27:36 +03001500/*
1501 * handle all the irqs defined by the HDRC core. for now we expect: other
1502 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1503 * will be assigned, and the irq will already have been acked.
1504 *
1505 * called in irq context with spinlock held, irqs blocked
1506 */
1507irqreturn_t musb_interrupt(struct musb *musb)
1508{
1509 irqreturn_t retval = IRQ_NONE;
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +01001510 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03001511 int ep_num;
1512 u32 reg;
1513
1514 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001515
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001516 dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001517 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1518 musb->int_usb, musb->int_tx, musb->int_rx);
1519
1520 /* the core can interrupt us for multiple reasons; docs have
1521 * a generic interrupt flowchart to follow
1522 */
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301523 if (musb->int_usb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001524 retval |= musb_stage0_irq(musb, musb->int_usb,
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +01001525 devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001526
1527 /* "stage 1" is handling endpoint irqs */
1528
1529 /* handle endpoint 0 first */
1530 if (musb->int_tx & 1) {
1531 if (devctl & MUSB_DEVCTL_HM)
1532 retval |= musb_h_ep0_irq(musb);
1533 else
1534 retval |= musb_g_ep0_irq(musb);
1535 }
1536
1537 /* RX on endpoints 1-15 */
1538 reg = musb->int_rx >> 1;
1539 ep_num = 1;
1540 while (reg) {
1541 if (reg & 1) {
1542 /* musb_ep_select(musb->mregs, ep_num); */
1543 /* REVISIT just retval = ep->rx_irq(...) */
1544 retval = IRQ_HANDLED;
Felipe Balbia04d46d2011-11-24 15:46:27 +02001545 if (devctl & MUSB_DEVCTL_HM)
1546 musb_host_rx(musb, ep_num);
1547 else
1548 musb_g_rx(musb, ep_num);
Felipe Balbi550a7372008-07-24 12:27:36 +03001549 }
1550
1551 reg >>= 1;
1552 ep_num++;
1553 }
1554
1555 /* TX on endpoints 1-15 */
1556 reg = musb->int_tx >> 1;
1557 ep_num = 1;
1558 while (reg) {
1559 if (reg & 1) {
1560 /* musb_ep_select(musb->mregs, ep_num); */
1561 /* REVISIT just retval |= ep->tx_irq(...) */
1562 retval = IRQ_HANDLED;
Felipe Balbia04d46d2011-11-24 15:46:27 +02001563 if (devctl & MUSB_DEVCTL_HM)
1564 musb_host_tx(musb, ep_num);
1565 else
1566 musb_g_tx(musb, ep_num);
Felipe Balbi550a7372008-07-24 12:27:36 +03001567 }
1568 reg >>= 1;
1569 ep_num++;
1570 }
1571
Felipe Balbi550a7372008-07-24 12:27:36 +03001572 return retval;
1573}
Felipe Balbi981430a2011-05-11 13:02:23 +03001574EXPORT_SYMBOL_GPL(musb_interrupt);
Felipe Balbi550a7372008-07-24 12:27:36 +03001575
1576#ifndef CONFIG_MUSB_PIO_ONLY
Bill Pembertond3608b62012-11-19 13:24:34 -05001577static bool use_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001578
1579/* "modprobe ... use_dma=0" etc */
1580module_param(use_dma, bool, 0);
1581MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1582
1583void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1584{
1585 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1586
1587 /* called with controller lock already held */
1588
1589 if (!epnum) {
1590#ifndef CONFIG_USB_TUSB_OMAP_DMA
1591 if (!is_cppi_enabled()) {
1592 /* endpoint 0 */
1593 if (devctl & MUSB_DEVCTL_HM)
1594 musb_h_ep0_irq(musb);
1595 else
1596 musb_g_ep0_irq(musb);
1597 }
1598#endif
1599 } else {
1600 /* endpoints 1..15 */
1601 if (transmit) {
Felipe Balbia04d46d2011-11-24 15:46:27 +02001602 if (devctl & MUSB_DEVCTL_HM)
1603 musb_host_tx(musb, epnum);
1604 else
1605 musb_g_tx(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001606 } else {
1607 /* receive */
Felipe Balbia04d46d2011-11-24 15:46:27 +02001608 if (devctl & MUSB_DEVCTL_HM)
1609 musb_host_rx(musb, epnum);
1610 else
1611 musb_g_rx(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001612 }
1613 }
1614}
Arnd Bergmann9a35f872011-10-02 16:45:47 +02001615EXPORT_SYMBOL_GPL(musb_dma_completion);
Felipe Balbi550a7372008-07-24 12:27:36 +03001616
1617#else
1618#define use_dma 0
1619#endif
1620
1621/*-------------------------------------------------------------------------*/
1622
Felipe Balbi550a7372008-07-24 12:27:36 +03001623static ssize_t
1624musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1625{
1626 struct musb *musb = dev_to_musb(dev);
1627 unsigned long flags;
1628 int ret = -EINVAL;
1629
1630 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi42c0bf12013-03-07 10:39:57 +02001631 ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001632 spin_unlock_irqrestore(&musb->lock, flags);
1633
1634 return ret;
1635}
1636
1637static ssize_t
1638musb_mode_store(struct device *dev, struct device_attribute *attr,
1639 const char *buf, size_t n)
1640{
1641 struct musb *musb = dev_to_musb(dev);
1642 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001643 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001644
1645 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001646 if (sysfs_streq(buf, "host"))
1647 status = musb_platform_set_mode(musb, MUSB_HOST);
1648 else if (sysfs_streq(buf, "peripheral"))
1649 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1650 else if (sysfs_streq(buf, "otg"))
1651 status = musb_platform_set_mode(musb, MUSB_OTG);
1652 else
1653 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001654 spin_unlock_irqrestore(&musb->lock, flags);
1655
David Brownell96a274d2008-11-24 13:06:47 +02001656 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001657}
1658static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1659
1660static ssize_t
1661musb_vbus_store(struct device *dev, struct device_attribute *attr,
1662 const char *buf, size_t n)
1663{
1664 struct musb *musb = dev_to_musb(dev);
1665 unsigned long flags;
1666 unsigned long val;
1667
1668 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001669 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001670 return -EINVAL;
1671 }
1672
1673 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001674 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1675 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001676 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001677 musb->is_active = 0;
1678 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1679 spin_unlock_irqrestore(&musb->lock, flags);
1680
1681 return n;
1682}
1683
1684static ssize_t
1685musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1686{
1687 struct musb *musb = dev_to_musb(dev);
1688 unsigned long flags;
1689 unsigned long val;
1690 int vbus;
1691
1692 spin_lock_irqsave(&musb->lock, flags);
1693 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001694 /* FIXME get_vbus_status() is normally #defined as false...
1695 * and is effectively TUSB-specific.
1696 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001697 vbus = musb_platform_get_vbus_status(musb);
1698 spin_unlock_irqrestore(&musb->lock, flags);
1699
David Brownellf7f9d632009-03-31 12:32:12 -07001700 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001701 vbus ? "on" : "off", val);
1702}
1703static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1704
Felipe Balbi550a7372008-07-24 12:27:36 +03001705/* Gadget drivers can't know that a host is connected so they might want
1706 * to start SRP, but users can. This allows userspace to trigger SRP.
1707 */
1708static ssize_t
1709musb_srp_store(struct device *dev, struct device_attribute *attr,
1710 const char *buf, size_t n)
1711{
1712 struct musb *musb = dev_to_musb(dev);
1713 unsigned short srp;
1714
1715 if (sscanf(buf, "%hu", &srp) != 1
1716 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001717 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001718 return -EINVAL;
1719 }
1720
1721 if (srp == 1)
1722 musb_g_wakeup(musb);
1723
1724 return n;
1725}
1726static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1727
Felipe Balbi94375752009-12-15 11:08:38 +02001728static struct attribute *musb_attributes[] = {
1729 &dev_attr_mode.attr,
1730 &dev_attr_vbus.attr,
Felipe Balbi94375752009-12-15 11:08:38 +02001731 &dev_attr_srp.attr,
Felipe Balbi94375752009-12-15 11:08:38 +02001732 NULL
1733};
1734
1735static const struct attribute_group musb_attr_group = {
1736 .attrs = musb_attributes,
1737};
1738
Felipe Balbi550a7372008-07-24 12:27:36 +03001739/* Only used to provide driver mode change events */
1740static void musb_irq_work(struct work_struct *data)
1741{
1742 struct musb *musb = container_of(data, struct musb, irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03001743
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +00001744 if (musb->xceiv->state != musb->xceiv_old_state) {
1745 musb->xceiv_old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001746 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1747 }
1748}
1749
1750/* --------------------------------------------------------------------------
1751 * Init support
1752 */
1753
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001754static struct musb *allocate_instance(struct device *dev,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001755 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001756{
1757 struct musb *musb;
1758 struct musb_hw_ep *ep;
1759 int epnum;
Daniel Mack74c2e932013-04-10 21:55:45 +02001760 int ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03001761
Daniel Mack74c2e932013-04-10 21:55:45 +02001762 musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
1763 if (!musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001764 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001765
Felipe Balbi550a7372008-07-24 12:27:36 +03001766 INIT_LIST_HEAD(&musb->control);
1767 INIT_LIST_HEAD(&musb->in_bulk);
1768 INIT_LIST_HEAD(&musb->out_bulk);
1769
Felipe Balbi550a7372008-07-24 12:27:36 +03001770 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001771 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001772 musb->mregs = mbase;
1773 musb->ctrl_base = mbase;
1774 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001775 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001776 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001777 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001778 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001779 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001780 ep->musb = musb;
1781 ep->epnum = epnum;
1782 }
1783
1784 musb->controller = dev;
Felipe Balbi743411b2010-12-01 13:22:05 +02001785
Daniel Mack74c2e932013-04-10 21:55:45 +02001786 ret = musb_host_alloc(musb);
1787 if (ret < 0)
1788 goto err_free;
1789
1790 dev_set_drvdata(dev, musb);
1791
Felipe Balbi550a7372008-07-24 12:27:36 +03001792 return musb;
Daniel Mack74c2e932013-04-10 21:55:45 +02001793
1794err_free:
1795 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001796}
1797
1798static void musb_free(struct musb *musb)
1799{
1800 /* this has multiple entry modes. it handles fault cleanup after
1801 * probe(), where things may be partially set up, as well as rmmod
1802 * cleanup after everything's been de-activated.
1803 */
1804
1805#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001806 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001807#endif
1808
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001809 if (musb->nIrq >= 0) {
1810 if (musb->irq_wake)
1811 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001812 free_irq(musb->nIrq, musb);
1813 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001814
Daniel Mack74c2e932013-04-10 21:55:45 +02001815 musb_host_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001816}
1817
Daniel Mack8ed1fb72013-12-18 20:23:46 +01001818static void musb_deassert_reset(struct work_struct *work)
1819{
1820 struct musb *musb;
1821 unsigned long flags;
1822
1823 musb = container_of(work, struct musb, deassert_reset_work.work);
1824
1825 spin_lock_irqsave(&musb->lock, flags);
1826
1827 if (musb->port1_status & USB_PORT_STAT_RESET)
1828 musb_port_reset(musb, false);
1829
1830 spin_unlock_irqrestore(&musb->lock, flags);
1831}
1832
Felipe Balbi550a7372008-07-24 12:27:36 +03001833/*
1834 * Perform generic per-controller initialization.
1835 *
Sergei Shtylyov28dd9242012-08-21 21:22:45 +04001836 * @dev: the controller (already clocked, etc)
1837 * @nIrq: IRQ number
1838 * @ctrl: virtual address of controller registers,
Felipe Balbi550a7372008-07-24 12:27:36 +03001839 * not yet corrected for platform-specific offsets
1840 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001841static int
Felipe Balbi550a7372008-07-24 12:27:36 +03001842musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1843{
1844 int status;
1845 struct musb *musb;
Jingoo Hanc1a7d672013-07-30 17:03:12 +09001846 struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001847
1848 /* The driver might handle more features than the board; OK.
1849 * Fail when the board needs a feature that's not enabled.
1850 */
1851 if (!plat) {
1852 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001853 status = -ENODEV;
1854 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001855 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001856
Felipe Balbi550a7372008-07-24 12:27:36 +03001857 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001858 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001859 if (!musb) {
1860 status = -ENOMEM;
1861 goto fail0;
1862 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001863
Hema HK7acc6192011-02-28 14:19:34 +05301864 pm_runtime_use_autosuspend(musb->controller);
1865 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1866 pm_runtime_enable(musb->controller);
1867
Felipe Balbi550a7372008-07-24 12:27:36 +03001868 spin_lock_init(&musb->lock);
Felipe Balbi550a7372008-07-24 12:27:36 +03001869 musb->board_set_power = plat->set_power;
Felipe Balbi550a7372008-07-24 12:27:36 +03001870 musb->min_power = plat->min_power;
Felipe Balbif7ec9432010-12-02 09:48:58 +02001871 musb->ops = plat->platform_ops;
Daniel Mack9ad96e62013-04-10 21:55:48 +02001872 musb->port_mode = plat->mode;
Felipe Balbi550a7372008-07-24 12:27:36 +03001873
David Brownell84e250f2009-03-31 12:30:04 -07001874 /* The musb_platform_init() call:
Philippe De Swertbaef6532012-11-06 15:32:13 +02001875 * - adjusts musb->mregs
1876 * - sets the musb->isr
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05301877 * - may initialize an integrated transceiver
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05301878 * - initializes musb->xceiv, usually by otg_get_phy()
David Brownell84e250f2009-03-31 12:30:04 -07001879 * - stops powering VBUS
David Brownell84e250f2009-03-31 12:30:04 -07001880 *
Joe Perches7c9d4402011-06-23 11:39:20 -07001881 * There are various transceiver configurations. Blackfin,
David Brownell84e250f2009-03-31 12:30:04 -07001882 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1883 * external/discrete ones in various flavors (twl4030 family,
1884 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03001885 */
Hema Kalliguddiea65df52010-09-22 19:27:40 -05001886 status = musb_platform_init(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001887 if (status < 0)
Felipe Balbi03491762010-12-02 09:57:08 +02001888 goto fail1;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001889
Felipe Balbi550a7372008-07-24 12:27:36 +03001890 if (!musb->isr) {
1891 status = -ENODEV;
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02001892 goto fail2;
Felipe Balbi550a7372008-07-24 12:27:36 +03001893 }
1894
Heikki Krogerusffb865b2010-03-25 13:25:28 +02001895 if (!musb->xceiv->io_ops) {
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +02001896 musb->xceiv->io_dev = musb->controller;
Heikki Krogerusffb865b2010-03-25 13:25:28 +02001897 musb->xceiv->io_priv = musb->mregs;
1898 musb->xceiv->io_ops = &musb_ulpi_access;
1899 }
1900
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02001901 pm_runtime_get_sync(musb->controller);
1902
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +02001903 if (use_dma && dev->dma_mask) {
Sebastian Andrzej Siewior66c01882013-06-19 17:38:11 +02001904 musb->dma_controller = dma_controller_create(musb, musb->mregs);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +02001905 if (IS_ERR(musb->dma_controller)) {
1906 status = PTR_ERR(musb->dma_controller);
1907 goto fail2_5;
1908 }
1909 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001910
1911 /* be sure interrupts are disabled before connecting ISR */
1912 musb_platform_disable(musb);
1913 musb_generic_disable(musb);
1914
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01001915 /* Init IRQ workqueue before request_irq */
1916 INIT_WORK(&musb->irq_work, musb_irq_work);
Daniel Mack8ed1fb72013-12-18 20:23:46 +01001917 INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
1918 INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01001919
Felipe Balbi550a7372008-07-24 12:27:36 +03001920 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001921 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03001922 ? MUSB_CONTROLLER_MHDRC
1923 : MUSB_CONTROLLER_HDRC, musb);
1924 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001925 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001926
David Brownellf7f9d632009-03-31 12:32:12 -07001927 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
David Brownellf7f9d632009-03-31 12:32:12 -07001928
Felipe Balbi550a7372008-07-24 12:27:36 +03001929 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01001930 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001931 dev_err(dev, "request_irq %d failed!\n", nIrq);
1932 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001933 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001934 }
1935 musb->nIrq = nIrq;
Felipe Balbi032ec492011-11-24 15:46:26 +02001936 /* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02001937 if (enable_irq_wake(nIrq) == 0) {
1938 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001939 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02001940 } else {
1941 musb->irq_wake = 0;
1942 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001943
Felipe Balbi032ec492011-11-24 15:46:26 +02001944 /* program PHY to use external vBus if required */
1945 if (plat->extvbus) {
1946 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
1947 busctl |= MUSB_ULPI_USE_EXTVBUS;
1948 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001949 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001950
Grazvydas Ignotase5615112013-03-10 02:48:55 +02001951 if (musb->xceiv->otg->default_a) {
1952 MUSB_HST_MODE(musb);
1953 musb->xceiv->state = OTG_STATE_A_IDLE;
1954 } else {
1955 MUSB_DEV_MODE(musb);
1956 musb->xceiv->state = OTG_STATE_B_IDLE;
1957 }
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05301958
Daniel Mack6c5f6a62013-04-10 21:55:49 +02001959 switch (musb->port_mode) {
1960 case MUSB_PORT_MODE_HOST:
1961 status = musb_host_setup(musb, plat->power);
Felipe Balbi2df67612013-10-29 12:17:17 -05001962 if (status < 0)
1963 goto fail3;
1964 status = musb_platform_set_mode(musb, MUSB_HOST);
Daniel Mack6c5f6a62013-04-10 21:55:49 +02001965 break;
1966 case MUSB_PORT_MODE_GADGET:
1967 status = musb_gadget_setup(musb);
Felipe Balbi2df67612013-10-29 12:17:17 -05001968 if (status < 0)
1969 goto fail3;
1970 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
Daniel Mack6c5f6a62013-04-10 21:55:49 +02001971 break;
1972 case MUSB_PORT_MODE_DUAL_ROLE:
1973 status = musb_host_setup(musb, plat->power);
1974 if (status < 0)
1975 goto fail3;
1976 status = musb_gadget_setup(musb);
Felipe Balbi2df67612013-10-29 12:17:17 -05001977 if (status) {
Sebastian Andrzej Siewior0d2dd7e2013-10-16 12:50:06 +02001978 musb_host_cleanup(musb);
Felipe Balbi2df67612013-10-29 12:17:17 -05001979 goto fail3;
1980 }
1981 status = musb_platform_set_mode(musb, MUSB_OTG);
Daniel Mack6c5f6a62013-04-10 21:55:49 +02001982 break;
1983 default:
1984 dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
1985 break;
1986 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001987
Sergei Shtylyov461972d2010-03-25 13:14:32 +02001988 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001989 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001990
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02001991 status = musb_init_debugfs(musb);
1992 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02001993 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02001994
Felipe Balbi94375752009-12-15 11:08:38 +02001995 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03001996 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02001997 goto fail5;
Felipe Balbi28c2c512008-09-11 11:53:25 +03001998
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02001999 pm_runtime_put(musb->controller);
2000
Felipe Balbi28c2c512008-09-11 11:53:25 +03002001 return 0;
2002
Felipe Balbib0f9da72010-03-25 13:25:18 +02002003fail5:
2004 musb_exit_debugfs(musb);
2005
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002006fail4:
Felipe Balbi032ec492011-11-24 15:46:26 +02002007 musb_gadget_cleanup(musb);
Sebastian Andrzej Siewior0d2dd7e2013-10-16 12:50:06 +02002008 musb_host_cleanup(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002009
2010fail3:
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01002011 cancel_work_sync(&musb->irq_work);
Daniel Mack8ed1fb72013-12-18 20:23:46 +01002012 cancel_delayed_work_sync(&musb->finish_resume_work);
2013 cancel_delayed_work_sync(&musb->deassert_reset_work);
Sebastian Andrzej Siewiorf3ce4d52013-06-19 17:38:14 +02002014 if (musb->dma_controller)
2015 dma_controller_destroy(musb->dma_controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +02002016fail2_5:
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02002017 pm_runtime_put_sync(musb->controller);
2018
2019fail2:
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002020 if (musb->irq_wake)
2021 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002022 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002023
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002024fail1:
Ming Lei681d1e82013-01-04 23:13:06 +08002025 pm_runtime_disable(musb->controller);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002026 dev_err(musb->controller,
2027 "musb_init_controller failed with status %d\n", status);
2028
Felipe Balbi28c2c512008-09-11 11:53:25 +03002029 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002030
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002031fail0:
2032
Felipe Balbi550a7372008-07-24 12:27:36 +03002033 return status;
2034
Felipe Balbi550a7372008-07-24 12:27:36 +03002035}
2036
2037/*-------------------------------------------------------------------------*/
2038
2039/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2040 * bridge to a platform device; this driver then suffices.
2041 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002042static int musb_probe(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002043{
2044 struct device *dev = &pdev->dev;
Hema Kalliguddifcf173e2010-09-29 11:26:39 -05002045 int irq = platform_get_irq_byname(pdev, "mc");
Felipe Balbi550a7372008-07-24 12:27:36 +03002046 struct resource *iomem;
2047 void __iomem *base;
2048
2049 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sergei Shtylyov541079d2010-12-10 21:03:29 +03002050 if (!iomem || irq <= 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03002051 return -ENODEV;
2052
Felipe Balbib42f7f32013-02-04 19:04:45 +02002053 base = devm_ioremap_resource(dev, iomem);
2054 if (IS_ERR(base))
2055 return PTR_ERR(base);
Felipe Balbi550a7372008-07-24 12:27:36 +03002056
Felipe Balbib42f7f32013-02-04 19:04:45 +02002057 return musb_init_controller(dev, irq, base);
Felipe Balbi550a7372008-07-24 12:27:36 +03002058}
2059
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002060static int musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002061{
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +00002062 struct device *dev = &pdev->dev;
2063 struct musb *musb = dev_to_musb(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002064
2065 /* this gets called on rmmod.
2066 * - Host mode: host may still be active
2067 * - Peripheral mode: peripheral is deactivated (or never-activated)
2068 * - OTG mode: both roles are deactivated (or never-activated)
2069 */
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002070 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002071 musb_shutdown(pdev);
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002072
Sebastian Andrzej Siewior8d1aad72013-10-16 12:50:07 +02002073 if (musb->dma_controller)
2074 dma_controller_destroy(musb->dma_controller);
2075
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01002076 cancel_work_sync(&musb->irq_work);
Daniel Mack8ed1fb72013-12-18 20:23:46 +01002077 cancel_delayed_work_sync(&musb->finish_resume_work);
2078 cancel_delayed_work_sync(&musb->deassert_reset_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03002079 musb_free(musb);
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +00002080 device_init_wakeup(dev, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002081 return 0;
2082}
2083
2084#ifdef CONFIG_PM
2085
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002086static void musb_save_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002087{
2088 int i;
2089 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002090 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002091
Felipe Balbi032ec492011-11-24 15:46:26 +02002092 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2093 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2094 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Felipe Balbi74211072010-12-01 13:53:27 +02002095 musb->context.power = musb_readb(musb_base, MUSB_POWER);
Felipe Balbi74211072010-12-01 13:53:27 +02002096 musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2097 musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2098 musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002099
Bob Liuae9b2ad2010-09-24 13:44:07 +03002100 for (i = 0; i < musb->config->num_eps; ++i) {
Felipe Balbie4e5b1362011-06-27 15:57:46 +03002101 struct musb_hw_ep *hw_ep;
2102
2103 hw_ep = &musb->endpoints[i];
2104 if (!hw_ep)
2105 continue;
2106
2107 epio = hw_ep->regs;
2108 if (!epio)
2109 continue;
2110
Vikram Panditaea737552011-09-07 09:19:23 -07002111 musb_writeb(musb_base, MUSB_INDEX, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002112 musb->context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002113 musb_readw(epio, MUSB_TXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002114 musb->context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002115 musb_readw(epio, MUSB_TXCSR);
Felipe Balbi74211072010-12-01 13:53:27 +02002116 musb->context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002117 musb_readw(epio, MUSB_RXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002118 musb->context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002119 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002120
2121 if (musb->dyn_fifo) {
Felipe Balbi74211072010-12-01 13:53:27 +02002122 musb->context.index_regs[i].txfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002123 musb_read_txfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002124 musb->context.index_regs[i].rxfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002125 musb_read_rxfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002126 musb->context.index_regs[i].txfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002127 musb_read_txfifosz(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002128 musb->context.index_regs[i].rxfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002129 musb_read_rxfifosz(musb_base);
2130 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002131
Felipe Balbi032ec492011-11-24 15:46:26 +02002132 musb->context.index_regs[i].txtype =
2133 musb_readb(epio, MUSB_TXTYPE);
2134 musb->context.index_regs[i].txinterval =
2135 musb_readb(epio, MUSB_TXINTERVAL);
2136 musb->context.index_regs[i].rxtype =
2137 musb_readb(epio, MUSB_RXTYPE);
2138 musb->context.index_regs[i].rxinterval =
2139 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002140
Felipe Balbi032ec492011-11-24 15:46:26 +02002141 musb->context.index_regs[i].txfunaddr =
2142 musb_read_txfunaddr(musb_base, i);
2143 musb->context.index_regs[i].txhubaddr =
2144 musb_read_txhubaddr(musb_base, i);
2145 musb->context.index_regs[i].txhubport =
2146 musb_read_txhubport(musb_base, i);
2147
2148 musb->context.index_regs[i].rxfunaddr =
2149 musb_read_rxfunaddr(musb_base, i);
2150 musb->context.index_regs[i].rxhubaddr =
2151 musb_read_rxhubaddr(musb_base, i);
2152 musb->context.index_regs[i].rxhubport =
2153 musb_read_rxhubport(musb_base, i);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002154 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002155}
2156
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002157static void musb_restore_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002158{
2159 int i;
2160 void __iomem *musb_base = musb->mregs;
2161 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002162 void __iomem *epio;
Roger Quadros33f8d752014-02-04 15:29:33 +02002163 u8 power;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002164
Felipe Balbi032ec492011-11-24 15:46:26 +02002165 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2166 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2167 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
Roger Quadros33f8d752014-02-04 15:29:33 +02002168
2169 /* Don't affect SUSPENDM/RESUME bits in POWER reg */
2170 power = musb_readb(musb_base, MUSB_POWER);
2171 power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
2172 musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
2173 power |= musb->context.power;
2174 musb_writeb(musb_base, MUSB_POWER, power);
2175
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01002176 musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01002177 musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi74211072010-12-01 13:53:27 +02002178 musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2179 musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002180
Bob Liuae9b2ad2010-09-24 13:44:07 +03002181 for (i = 0; i < musb->config->num_eps; ++i) {
Felipe Balbie4e5b1362011-06-27 15:57:46 +03002182 struct musb_hw_ep *hw_ep;
2183
2184 hw_ep = &musb->endpoints[i];
2185 if (!hw_ep)
2186 continue;
2187
2188 epio = hw_ep->regs;
2189 if (!epio)
2190 continue;
2191
Vikram Panditaea737552011-09-07 09:19:23 -07002192 musb_writeb(musb_base, MUSB_INDEX, i);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002193 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002194 musb->context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002195 musb_writew(epio, MUSB_TXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002196 musb->context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002197 musb_writew(epio, MUSB_RXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002198 musb->context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002199 musb_writew(epio, MUSB_RXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002200 musb->context.index_regs[i].rxcsr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002201
2202 if (musb->dyn_fifo) {
2203 musb_write_txfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002204 musb->context.index_regs[i].txfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002205 musb_write_rxfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002206 musb->context.index_regs[i].rxfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002207 musb_write_txfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002208 musb->context.index_regs[i].txfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002209 musb_write_rxfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002210 musb->context.index_regs[i].rxfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002211 }
2212
Felipe Balbi032ec492011-11-24 15:46:26 +02002213 musb_writeb(epio, MUSB_TXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002214 musb->context.index_regs[i].txtype);
Felipe Balbi032ec492011-11-24 15:46:26 +02002215 musb_writeb(epio, MUSB_TXINTERVAL,
Felipe Balbi74211072010-12-01 13:53:27 +02002216 musb->context.index_regs[i].txinterval);
Felipe Balbi032ec492011-11-24 15:46:26 +02002217 musb_writeb(epio, MUSB_RXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002218 musb->context.index_regs[i].rxtype);
Felipe Balbi032ec492011-11-24 15:46:26 +02002219 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002220
Felipe Balbi032ec492011-11-24 15:46:26 +02002221 musb->context.index_regs[i].rxinterval);
2222 musb_write_txfunaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002223 musb->context.index_regs[i].txfunaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002224 musb_write_txhubaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002225 musb->context.index_regs[i].txhubaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002226 musb_write_txhubport(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002227 musb->context.index_regs[i].txhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002228
Felipe Balbi032ec492011-11-24 15:46:26 +02002229 ep_target_regs =
2230 musb_read_target_reg_base(i, musb_base);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002231
Felipe Balbi032ec492011-11-24 15:46:26 +02002232 musb_write_rxfunaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002233 musb->context.index_regs[i].rxfunaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002234 musb_write_rxhubaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002235 musb->context.index_regs[i].rxhubaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002236 musb_write_rxhubport(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002237 musb->context.index_regs[i].rxhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002238 }
Ajay Kumar Gupta3c5fec72011-07-08 15:06:13 +05302239 musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002240}
2241
Magnus Damm48fea962009-07-08 13:22:56 +02002242static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002243{
Felipe Balbi82207962011-06-27 15:57:12 +03002244 struct musb *musb = dev_to_musb(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002245 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03002246
Felipe Balbi550a7372008-07-24 12:27:36 +03002247 spin_lock_irqsave(&musb->lock, flags);
2248
2249 if (is_peripheral_active(musb)) {
2250 /* FIXME force disconnect unless we know USB will wake
2251 * the system up quickly enough to respond ...
2252 */
2253 } else if (is_host_active(musb)) {
2254 /* we know all the children are suspended; sometimes
2255 * they will even be wakeup-enabled.
2256 */
2257 }
2258
Daniel Mackc3384122013-11-25 22:26:40 +01002259 musb_save_context(musb);
2260
Felipe Balbi550a7372008-07-24 12:27:36 +03002261 spin_unlock_irqrestore(&musb->lock, flags);
2262 return 0;
2263}
2264
Magnus Damm48fea962009-07-08 13:22:56 +02002265static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002266{
Daniel Mackc3384122013-11-25 22:26:40 +01002267 struct musb *musb = dev_to_musb(dev);
2268
2269 /*
2270 * For static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002271 * unless for some reason the whole soc powered down or the USB
2272 * module got reset through the PSC (vs just being disabled).
Daniel Mackc3384122013-11-25 22:26:40 +01002273 *
2274 * For the DSPS glue layer though, a full register restore has to
2275 * be done. As it shouldn't harm other platforms, we do it
2276 * unconditionally.
Felipe Balbi550a7372008-07-24 12:27:36 +03002277 */
Daniel Mackc3384122013-11-25 22:26:40 +01002278
2279 musb_restore_context(musb);
2280
Felipe Balbi550a7372008-07-24 12:27:36 +03002281 return 0;
2282}
2283
Hema HK7acc6192011-02-28 14:19:34 +05302284static int musb_runtime_suspend(struct device *dev)
2285{
2286 struct musb *musb = dev_to_musb(dev);
2287
2288 musb_save_context(musb);
2289
2290 return 0;
2291}
2292
2293static int musb_runtime_resume(struct device *dev)
2294{
2295 struct musb *musb = dev_to_musb(dev);
2296 static int first = 1;
2297
2298 /*
2299 * When pm_runtime_get_sync called for the first time in driver
2300 * init, some of the structure is still not initialized which is
2301 * used in restore function. But clock needs to be
2302 * enabled before any register access, so
2303 * pm_runtime_get_sync has to be called.
2304 * Also context restore without save does not make
2305 * any sense
2306 */
2307 if (!first)
2308 musb_restore_context(musb);
2309 first = 0;
2310
2311 return 0;
2312}
2313
Alexey Dobriyan47145212009-12-14 18:00:08 -08002314static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002315 .suspend = musb_suspend,
2316 .resume_noirq = musb_resume_noirq,
Hema HK7acc6192011-02-28 14:19:34 +05302317 .runtime_suspend = musb_runtime_suspend,
2318 .runtime_resume = musb_runtime_resume,
Magnus Damm48fea962009-07-08 13:22:56 +02002319};
2320
2321#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002322#else
Magnus Damm48fea962009-07-08 13:22:56 +02002323#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002324#endif
2325
2326static struct platform_driver musb_driver = {
2327 .driver = {
2328 .name = (char *)musb_driver_name,
2329 .bus = &platform_bus_type,
2330 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002331 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002332 },
Felipe Balbie9e8c852012-01-26 12:40:23 +02002333 .probe = musb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05002334 .remove = musb_remove,
Felipe Balbi550a7372008-07-24 12:27:36 +03002335 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002336};
2337
Ezequiel Garcia89f836a2013-12-26 09:24:52 -03002338module_platform_driver(musb_driver);