blob: 6dca3d794ced6e1948dd5cbb180e708893f7ba83 [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 Balbi309be232015-02-13 14:46:27 -0600102#include <linux/usb.h>
Felipe Balbi550a7372008-07-24 12:27:36 +0300103
Felipe Balbi550a7372008-07-24 12:27:36 +0300104#include "musb_core.h"
105
David Brownellf7f9d632009-03-31 12:32:12 -0700106#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +0300107
108
Felipe Balbi550a7372008-07-24 12:27:36 +0300109#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
110#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
111
Felipe Balbie8164f62008-08-10 21:22:35 +0300112#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300113
114#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
115
Felipe Balbi05ac10d2010-12-02 08:49:26 +0200116#define MUSB_DRIVER_NAME "musb-hdrc"
Felipe Balbi550a7372008-07-24 12:27:36 +0300117const char musb_driver_name[] = MUSB_DRIVER_NAME;
118
119MODULE_DESCRIPTION(DRIVER_INFO);
120MODULE_AUTHOR(DRIVER_AUTHOR);
121MODULE_LICENSE("GPL");
122MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
123
124
125/*-------------------------------------------------------------------------*/
126
127static inline struct musb *dev_to_musb(struct device *dev)
128{
Felipe Balbi550a7372008-07-24 12:27:36 +0300129 return dev_get_drvdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300130}
131
132/*-------------------------------------------------------------------------*/
133
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200134#ifndef CONFIG_BLACKFIN
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200135static int musb_ulpi_read(struct usb_phy *phy, u32 offset)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200136{
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200137 void __iomem *addr = phy->io_priv;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200138 int i = 0;
139 u8 r;
140 u8 power;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200141 int ret;
142
143 pm_runtime_get_sync(phy->io_dev);
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200144
145 /* Make sure the transceiver is not in low power mode */
146 power = musb_readb(addr, MUSB_POWER);
147 power &= ~MUSB_POWER_SUSPENDM;
148 musb_writeb(addr, MUSB_POWER, power);
149
150 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
151 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
152 */
153
154 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
155 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
156 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
157
158 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
159 & MUSB_ULPI_REG_CMPLT)) {
160 i++;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200161 if (i == 10000) {
162 ret = -ETIMEDOUT;
163 goto out;
164 }
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200165
166 }
167 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
168 r &= ~MUSB_ULPI_REG_CMPLT;
169 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
170
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200171 ret = musb_readb(addr, MUSB_ULPI_REG_DATA);
172
173out:
174 pm_runtime_put(phy->io_dev);
175
176 return ret;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200177}
178
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200179static int musb_ulpi_write(struct usb_phy *phy, u32 offset, u32 data)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200180{
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200181 void __iomem *addr = phy->io_priv;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200182 int i = 0;
183 u8 r = 0;
184 u8 power;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200185 int ret = 0;
186
187 pm_runtime_get_sync(phy->io_dev);
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200188
189 /* Make sure the transceiver is not in low power mode */
190 power = musb_readb(addr, MUSB_POWER);
191 power &= ~MUSB_POWER_SUSPENDM;
192 musb_writeb(addr, MUSB_POWER, power);
193
194 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
195 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
196 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
197
198 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
199 & MUSB_ULPI_REG_CMPLT)) {
200 i++;
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200201 if (i == 10000) {
202 ret = -ETIMEDOUT;
203 goto out;
204 }
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200205 }
206
207 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
208 r &= ~MUSB_ULPI_REG_CMPLT;
209 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
210
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +0200211out:
212 pm_runtime_put(phy->io_dev);
213
214 return ret;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200215}
216#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530217#define musb_ulpi_read NULL
218#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200219#endif
220
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200221static struct usb_phy_io_ops musb_ulpi_access = {
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200222 .read = musb_ulpi_read,
223 .write = musb_ulpi_write,
224};
225
226/*-------------------------------------------------------------------------*/
227
Tony Lindgren1b40fc52014-11-24 11:05:02 -0800228static u32 musb_default_fifo_offset(u8 epnum)
229{
230 return 0x20 + (epnum * 4);
231}
232
Tony Lindgrend026e9c2014-11-24 11:05:03 -0800233/* "flat" mapping: each endpoint has its own i/o address */
234static void musb_flat_ep_select(void __iomem *mbase, u8 epnum)
235{
236}
237
238static u32 musb_flat_ep_offset(u8 epnum, u16 offset)
239{
240 return 0x100 + (0x10 * epnum) + offset;
241}
242
243/* "indexed" mapping: INDEX register controls register bank select */
244static void musb_indexed_ep_select(void __iomem *mbase, u8 epnum)
245{
246 musb_writeb(mbase, MUSB_INDEX, epnum);
247}
248
249static u32 musb_indexed_ep_offset(u8 epnum, u16 offset)
250{
251 return 0x10 + offset;
252}
253
Tony Lindgren1b40fc52014-11-24 11:05:02 -0800254static u8 musb_default_readb(const void __iomem *addr, unsigned offset)
255{
256 return __raw_readb(addr + offset);
257}
258
259static void musb_default_writeb(void __iomem *addr, unsigned offset, u8 data)
260{
261 __raw_writeb(data, addr + offset);
262}
263
264static u16 musb_default_readw(const void __iomem *addr, unsigned offset)
265{
266 return __raw_readw(addr + offset);
267}
268
269static void musb_default_writew(void __iomem *addr, unsigned offset, u16 data)
270{
271 __raw_writew(data, addr + offset);
272}
273
274static u32 musb_default_readl(const void __iomem *addr, unsigned offset)
275{
276 return __raw_readl(addr + offset);
277}
278
279static void musb_default_writel(void __iomem *addr, unsigned offset, u32 data)
280{
281 __raw_writel(data, addr + offset);
282}
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200283
Felipe Balbi550a7372008-07-24 12:27:36 +0300284/*
285 * Load an endpoint's FIFO
286 */
Tony Lindgren1b40fc52014-11-24 11:05:02 -0800287static void musb_default_write_fifo(struct musb_hw_ep *hw_ep, u16 len,
288 const u8 *src)
Felipe Balbi550a7372008-07-24 12:27:36 +0300289{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300290 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300291 void __iomem *fifo = hw_ep->fifo;
292
Ajay Kumar Gupta603fe2b2012-07-20 11:07:24 +0530293 if (unlikely(len == 0))
294 return;
295
Felipe Balbi550a7372008-07-24 12:27:36 +0300296 prefetch((u8 *)src);
297
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300298 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300299 'T', hw_ep->epnum, fifo, len, src);
300
301 /* we can't assume unaligned reads work */
302 if (likely((0x01 & (unsigned long) src) == 0)) {
303 u16 index = 0;
304
305 /* best case is 32bit-aligned source address */
306 if ((0x02 & (unsigned long) src) == 0) {
307 if (len >= 4) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800308 iowrite32_rep(fifo, src + index, len >> 2);
Felipe Balbi550a7372008-07-24 12:27:36 +0300309 index += len & ~0x03;
310 }
311 if (len & 0x02) {
312 musb_writew(fifo, 0, *(u16 *)&src[index]);
313 index += 2;
314 }
315 } else {
316 if (len >= 2) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800317 iowrite16_rep(fifo, src + index, len >> 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300318 index += len & ~0x01;
319 }
320 }
321 if (len & 0x01)
322 musb_writeb(fifo, 0, src[index]);
323 } else {
324 /* byte aligned */
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800325 iowrite8_rep(fifo, src, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300326 }
327}
328
329/*
330 * Unload an endpoint's FIFO
331 */
Tony Lindgren1b40fc52014-11-24 11:05:02 -0800332static void musb_default_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
Felipe Balbi550a7372008-07-24 12:27:36 +0300333{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300334 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300335 void __iomem *fifo = hw_ep->fifo;
336
Ajay Kumar Gupta603fe2b2012-07-20 11:07:24 +0530337 if (unlikely(len == 0))
338 return;
339
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300340 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300341 'R', hw_ep->epnum, fifo, len, dst);
342
343 /* we can't assume unaligned writes work */
344 if (likely((0x01 & (unsigned long) dst) == 0)) {
345 u16 index = 0;
346
347 /* best case is 32bit-aligned destination address */
348 if ((0x02 & (unsigned long) dst) == 0) {
349 if (len >= 4) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800350 ioread32_rep(fifo, dst, len >> 2);
Felipe Balbi550a7372008-07-24 12:27:36 +0300351 index = len & ~0x03;
352 }
353 if (len & 0x02) {
354 *(u16 *)&dst[index] = musb_readw(fifo, 0);
355 index += 2;
356 }
357 } else {
358 if (len >= 2) {
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800359 ioread16_rep(fifo, dst, len >> 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300360 index = len & ~0x01;
361 }
362 }
363 if (len & 0x01)
364 dst[index] = musb_readb(fifo, 0);
365 } else {
366 /* byte aligned */
Matthew Leach2bf0a8f2012-12-17 15:59:48 -0800367 ioread8_rep(fifo, dst, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300368 }
369}
370
Tony Lindgren1b40fc52014-11-24 11:05:02 -0800371/*
372 * Old style IO functions
373 */
374u8 (*musb_readb)(const void __iomem *addr, unsigned offset);
375EXPORT_SYMBOL_GPL(musb_readb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300376
Tony Lindgren1b40fc52014-11-24 11:05:02 -0800377void (*musb_writeb)(void __iomem *addr, unsigned offset, u8 data);
378EXPORT_SYMBOL_GPL(musb_writeb);
379
380u16 (*musb_readw)(const void __iomem *addr, unsigned offset);
381EXPORT_SYMBOL_GPL(musb_readw);
382
383void (*musb_writew)(void __iomem *addr, unsigned offset, u16 data);
384EXPORT_SYMBOL_GPL(musb_writew);
385
386u32 (*musb_readl)(const void __iomem *addr, unsigned offset);
387EXPORT_SYMBOL_GPL(musb_readl);
388
389void (*musb_writel)(void __iomem *addr, unsigned offset, u32 data);
390EXPORT_SYMBOL_GPL(musb_writel);
391
392/*
393 * New style IO functions
394 */
395void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
396{
397 return hw_ep->musb->io.read_fifo(hw_ep, len, dst);
398}
399
400void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
401{
402 return hw_ep->musb->io.write_fifo(hw_ep, len, src);
403}
Felipe Balbi550a7372008-07-24 12:27:36 +0300404
405/*-------------------------------------------------------------------------*/
406
407/* for high speed test mode; see USB 2.0 spec 7.1.20 */
408static const u8 musb_test_packet[53] = {
409 /* implicit SYNC then DATA0 to start */
410
411 /* JKJKJKJK x9 */
412 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
413 /* JJKKJJKK x8 */
414 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
415 /* JJJJKKKK x8 */
416 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
417 /* JJJJJJJKKKKKKK x8 */
418 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
419 /* JJJJJJJK x8 */
420 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
421 /* JKKKKKKK x10, JK */
422 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
423
424 /* implicit CRC16 then EOP to end */
425};
426
427void musb_load_testpacket(struct musb *musb)
428{
429 void __iomem *regs = musb->endpoints[0].regs;
430
431 musb_ep_select(musb->mregs, 0);
432 musb_write_fifo(musb->control_ep,
433 sizeof(musb_test_packet), musb_test_packet);
434 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
435}
436
437/*-------------------------------------------------------------------------*/
438
Felipe Balbi550a7372008-07-24 12:27:36 +0300439/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300440 * Handles OTG hnp timeouts, such as b_ase0_brst
441 */
Felipe Balbia1565442012-08-07 14:00:50 +0300442static void musb_otg_timer_func(unsigned long data)
Felipe Balbi550a7372008-07-24 12:27:36 +0300443{
444 struct musb *musb = (struct musb *)data;
445 unsigned long flags;
446
447 spin_lock_irqsave(&musb->lock, flags);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100448 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300449 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300450 dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300451 musb_g_disconnect(musb);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100452 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300453 musb->is_active = 0;
454 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700455 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300456 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300457 dev_dbg(musb->controller, "HNP: %s timeout\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100458 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi743411b2010-12-01 13:22:05 +0200459 musb_platform_set_vbus(musb, 0);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100460 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300461 break;
462 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300463 dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100464 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300465 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300466 spin_unlock_irqrestore(&musb->lock, flags);
467}
468
Felipe Balbi550a7372008-07-24 12:27:36 +0300469/*
David Brownellf7f9d632009-03-31 12:32:12 -0700470 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300471 */
472void musb_hnp_stop(struct musb *musb)
473{
Daniel Mack8b125df2013-04-10 21:55:50 +0200474 struct usb_hcd *hcd = musb->hcd;
Felipe Balbi550a7372008-07-24 12:27:36 +0300475 void __iomem *mbase = musb->mregs;
476 u8 reg;
477
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200478 dev_dbg(musb->controller, "HNP: stop from %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100479 usb_otg_state_string(musb->xceiv->otg->state));
David Brownellab983f2a2009-03-31 12:35:09 -0700480
Antoine Tenarte47d9252014-10-30 18:41:13 +0100481 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300482 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300483 musb_g_disconnect(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300484 dev_dbg(musb->controller, "HNP: back to %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100485 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300486 break;
487 case OTG_STATE_B_HOST:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300488 dev_dbg(musb->controller, "HNP: Disabling HR\n");
Daniel Mack74c2e932013-04-10 21:55:45 +0200489 if (hcd)
490 hcd->self.is_b_host = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100491 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300492 MUSB_DEV_MODE(musb);
493 reg = musb_readb(mbase, MUSB_POWER);
494 reg |= MUSB_POWER_SUSPENDM;
495 musb_writeb(mbase, MUSB_POWER, reg);
496 /* REVISIT: Start SESSION_REQUEST here? */
497 break;
498 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300499 dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100500 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300501 }
502
503 /*
504 * When returning to A state after HNP, avoid hub_port_rebounce(),
505 * which cause occasional OPT A "Did not receive reset after connect"
506 * errors.
507 */
Alan Stern749da5f2010-03-04 17:05:08 -0500508 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300509}
510
Felipe Balbi83b8f5b2015-02-26 14:27:12 -0600511static void musb_recover_from_babble(struct musb *musb);
Felipe Balbie1eb3eb2015-02-26 11:26:09 -0600512
Felipe Balbi550a7372008-07-24 12:27:36 +0300513/*
514 * Interrupt Service Routine to record USB "global" interrupts.
515 * Since these do not happen often and signify things of
516 * paramount importance, it seems OK to check them individually;
517 * the order of the tests is specified in the manual
518 *
519 * @param musb instance pointer
520 * @param int_usb register contents
521 * @param devctl
522 * @param power
523 */
524
Felipe Balbi550a7372008-07-24 12:27:36 +0300525static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100526 u8 devctl)
Felipe Balbi550a7372008-07-24 12:27:36 +0300527{
528 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300529
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100530 dev_dbg(musb->controller, "<== DevCtl=%02x, int_usb=0x%x\n", devctl,
Felipe Balbi550a7372008-07-24 12:27:36 +0300531 int_usb);
532
533 /* in host mode, the peripheral may issue remote wakeup.
534 * in peripheral mode, the host may resume the link.
535 * spurious RESUME irqs happen too, paired with SUSPEND.
536 */
537 if (int_usb & MUSB_INTR_RESUME) {
538 handled = IRQ_HANDLED;
Felipe Balbi0acff6b2015-02-25 14:14:15 -0600539 dev_dbg(musb->controller, "RESUME (%s)\n",
540 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300541
542 if (devctl & MUSB_DEVCTL_HM) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100543 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300544 case OTG_STATE_A_SUSPEND:
545 /* remote wakeup? later, GetPortStatus
546 * will stop RESUME signaling
547 */
548
Felipe Balbi550a7372008-07-24 12:27:36 +0300549 musb->port1_status |=
550 (USB_PORT_STAT_C_SUSPEND << 16)
551 | MUSB_PORT_STAT_RESUME;
Daniel Mack30d361b2014-01-15 14:09:49 +0100552 musb->rh_timer = jiffies
Felipe Balbi309be232015-02-13 14:46:27 -0600553 + msecs_to_jiffies(USB_RESUME_TIMEOUT);
Sebastian Andrzej Siewiorbaadd522014-10-27 19:06:19 +0100554 musb->need_finish_resume = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300555
Antoine Tenarte47d9252014-10-30 18:41:13 +0100556 musb->xceiv->otg->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300557 musb->is_active = 1;
Bin Liu9298b4a2015-02-03 11:02:10 -0600558 musb_host_resume_root_hub(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300559 break;
560 case OTG_STATE_B_WAIT_ACON:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100561 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300562 musb->is_active = 1;
563 MUSB_DEV_MODE(musb);
564 break;
565 default:
566 WARNING("bogus %s RESUME (%s)\n",
567 "host",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100568 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300569 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300570 } else {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100571 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300572 case OTG_STATE_A_SUSPEND:
573 /* possibly DISCONNECT is upcoming */
Antoine Tenarte47d9252014-10-30 18:41:13 +0100574 musb->xceiv->otg->state = OTG_STATE_A_HOST;
Daniel Mack0b3eba42013-04-10 21:55:42 +0200575 musb_host_resume_root_hub(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300576 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300577 case OTG_STATE_B_WAIT_ACON:
578 case OTG_STATE_B_PERIPHERAL:
579 /* disconnect while suspended? we may
580 * not get a disconnect irq...
581 */
582 if ((devctl & MUSB_DEVCTL_VBUS)
583 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
584 ) {
585 musb->int_usb |= MUSB_INTR_DISCONNECT;
586 musb->int_usb &= ~MUSB_INTR_SUSPEND;
587 break;
588 }
589 musb_g_resume(musb);
590 break;
591 case OTG_STATE_B_IDLE:
592 musb->int_usb &= ~MUSB_INTR_SUSPEND;
593 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300594 default:
595 WARNING("bogus %s RESUME (%s)\n",
596 "peripheral",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100597 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300598 }
599 }
600 }
601
Felipe Balbi550a7372008-07-24 12:27:36 +0300602 /* see manual for the order of the tests */
603 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200604 void __iomem *mbase = musb->mregs;
605
Heikki Krogerus19aab562010-10-29 04:23:27 -0500606 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
607 && (devctl & MUSB_DEVCTL_BDEVICE)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300608 dev_dbg(musb->controller, "SessReq while on B state\n");
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300609 return IRQ_HANDLED;
610 }
611
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300612 dev_dbg(musb->controller, "SESSION_REQUEST (%s)\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100613 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300614
615 /* IRQ arrives from ID pin sense or (later, if VBUS power
616 * is removed) SRP. responses are time critical:
617 * - turn on VBUS (with silicon-specific mechanism)
618 * - go through A_WAIT_VRISE
619 * - ... to A_WAIT_BCON.
620 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
621 */
622 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
623 musb->ep0_stage = MUSB_EP0_START;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100624 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300625 MUSB_HST_MODE(musb);
Felipe Balbi743411b2010-12-01 13:22:05 +0200626 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300627
628 handled = IRQ_HANDLED;
629 }
630
631 if (int_usb & MUSB_INTR_VBUSERROR) {
632 int ignore = 0;
633
634 /* During connection as an A-Device, we may see a short
635 * current spikes causing voltage drop, because of cable
636 * and peripheral capacitance combined with vbus draw.
637 * (So: less common with truly self-powered devices, where
638 * vbus doesn't act like a power supply.)
639 *
640 * Such spikes are short; usually less than ~500 usec, max
641 * of ~2 msec. That is, they're not sustained overcurrent
642 * errors, though they're reported using VBUSERROR irqs.
643 *
644 * Workarounds: (a) hardware: use self powered devices.
645 * (b) software: ignore non-repeated VBUS errors.
646 *
647 * REVISIT: do delays from lots of DEBUG_KERNEL checks
648 * make trouble here, keeping VBUS < 4.4V ?
649 */
Antoine Tenarte47d9252014-10-30 18:41:13 +0100650 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300651 case OTG_STATE_A_HOST:
652 /* recovery is dicey once we've gotten past the
653 * initial stages of enumeration, but if VBUS
654 * stayed ok at the other end of the link, and
655 * another reset is due (at least for high speed,
656 * to redo the chirp etc), it might work OK...
657 */
658 case OTG_STATE_A_WAIT_BCON:
659 case OTG_STATE_A_WAIT_VRISE:
660 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200661 void __iomem *mbase = musb->mregs;
662
Felipe Balbi550a7372008-07-24 12:27:36 +0300663 musb->vbuserr_retry--;
664 ignore = 1;
665 devctl |= MUSB_DEVCTL_SESSION;
666 musb_writeb(mbase, MUSB_DEVCTL, devctl);
667 } else {
668 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500669 USB_PORT_STAT_OVERCURRENT
670 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300671 }
672 break;
673 default:
674 break;
675 }
676
Grazvydas Ignotas54485112013-03-10 02:49:28 +0200677 dev_printk(ignore ? KERN_DEBUG : KERN_ERR, musb->controller,
678 "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100679 usb_otg_state_string(musb->xceiv->otg->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300680 devctl,
681 ({ char *s;
682 switch (devctl & MUSB_DEVCTL_VBUS) {
683 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
684 s = "<SessEnd"; break;
685 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
686 s = "<AValid"; break;
687 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
688 s = "<VBusValid"; break;
689 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
690 default:
691 s = "VALID"; break;
Joe Perches2b84f922013-10-08 16:01:37 -0700692 } s; }),
Felipe Balbi550a7372008-07-24 12:27:36 +0300693 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
694 musb->port1_status);
695
696 /* go through A_WAIT_VFALL then start a new session */
697 if (!ignore)
Felipe Balbi743411b2010-12-01 13:22:05 +0200698 musb_platform_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300699 handled = IRQ_HANDLED;
700 }
701
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200702 if (int_usb & MUSB_INTR_SUSPEND) {
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +0100703 dev_dbg(musb->controller, "SUSPEND (%s) devctl %02x\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100704 usb_otg_state_string(musb->xceiv->otg->state), devctl);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200705 handled = IRQ_HANDLED;
706
Antoine Tenarte47d9252014-10-30 18:41:13 +0100707 switch (musb->xceiv->otg->state) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200708 case OTG_STATE_A_PERIPHERAL:
709 /* We also come here if the cable is removed, since
710 * this silicon doesn't report ID-no-longer-grounded.
711 *
712 * We depend on T(a_wait_bcon) to shut us down, and
713 * hope users don't do anything dicey during this
714 * undesired detour through A_WAIT_BCON.
715 */
716 musb_hnp_stop(musb);
Daniel Mack0b3eba42013-04-10 21:55:42 +0200717 musb_host_resume_root_hub(musb);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200718 musb_root_disconnect(musb);
719 musb_platform_try_idle(musb, jiffies
720 + msecs_to_jiffies(musb->a_wait_bcon
721 ? : OTG_TIME_A_WAIT_BCON));
722
723 break;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200724 case OTG_STATE_B_IDLE:
725 if (!musb->is_active)
726 break;
727 case OTG_STATE_B_PERIPHERAL:
728 musb_g_suspend(musb);
Felipe Balbieee3f152014-02-25 10:58:43 -0600729 musb->is_active = musb->g.b_hnp_enable;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200730 if (musb->is_active) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100731 musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300732 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200733 mod_timer(&musb->otg_timer, jiffies
734 + msecs_to_jiffies(
735 OTG_TIME_B_ASE0_BRST));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200736 }
737 break;
738 case OTG_STATE_A_WAIT_BCON:
739 if (musb->a_wait_bcon != 0)
740 musb_platform_try_idle(musb, jiffies
741 + msecs_to_jiffies(musb->a_wait_bcon));
742 break;
743 case OTG_STATE_A_HOST:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100744 musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
Felipe Balbieee3f152014-02-25 10:58:43 -0600745 musb->is_active = musb->hcd->self.b_hnp_enable;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200746 break;
747 case OTG_STATE_B_HOST:
748 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300749 dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200750 break;
751 default:
752 /* "should not happen" */
753 musb->is_active = 0;
754 break;
755 }
756 }
757
Felipe Balbi550a7372008-07-24 12:27:36 +0300758 if (int_usb & MUSB_INTR_CONNECT) {
Daniel Mack8b125df2013-04-10 21:55:50 +0200759 struct usb_hcd *hcd = musb->hcd;
Felipe Balbi550a7372008-07-24 12:27:36 +0300760
761 handled = IRQ_HANDLED;
762 musb->is_active = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300763
764 musb->ep0_stage = MUSB_EP0_START;
765
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100766 musb->intrtxe = musb->epmask;
767 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +0100768 musb->intrrxe = musb->epmask & 0xfffe;
769 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530770 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300771 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
772 |USB_PORT_STAT_HIGH_SPEED
773 |USB_PORT_STAT_ENABLE
774 );
775 musb->port1_status |= USB_PORT_STAT_CONNECTION
776 |(USB_PORT_STAT_C_CONNECTION << 16);
777
778 /* high vs full speed is just a guess until after reset */
779 if (devctl & MUSB_DEVCTL_LSDEV)
780 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
781
Felipe Balbi550a7372008-07-24 12:27:36 +0300782 /* indicate new connection to OTG machine */
Antoine Tenarte47d9252014-10-30 18:41:13 +0100783 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300784 case OTG_STATE_B_PERIPHERAL:
785 if (int_usb & MUSB_INTR_SUSPEND) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300786 dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300787 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700788 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300789 } else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300790 dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300791 break;
792 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300793 dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
David Brownell1de00da2009-04-02 10:16:11 -0700794b_host:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100795 musb->xceiv->otg->state = OTG_STATE_B_HOST;
Daniel Mack74c2e932013-04-10 21:55:45 +0200796 if (musb->hcd)
797 musb->hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700798 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300799 break;
800 default:
801 if ((devctl & MUSB_DEVCTL_VBUS)
802 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100803 musb->xceiv->otg->state = OTG_STATE_A_HOST;
Daniel Mack0b3eba42013-04-10 21:55:42 +0200804 if (hcd)
805 hcd->self.is_b_host = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300806 }
807 break;
808 }
David Brownell1de00da2009-04-02 10:16:11 -0700809
Daniel Mack0b3eba42013-04-10 21:55:42 +0200810 musb_host_poke_root_hub(musb);
David Brownell1de00da2009-04-02 10:16:11 -0700811
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300812 dev_dbg(musb->controller, "CONNECT (%s) devctl %02x\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100813 usb_otg_state_string(musb->xceiv->otg->state), devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300814 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300815
Felipe Balbi6d349672013-04-29 12:02:24 +0300816 if (int_usb & MUSB_INTR_DISCONNECT) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300817 dev_dbg(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100818 usb_otg_state_string(musb->xceiv->otg->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300819 MUSB_MODE(musb), devctl);
820 handled = IRQ_HANDLED;
821
Antoine Tenarte47d9252014-10-30 18:41:13 +0100822 switch (musb->xceiv->otg->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300823 case OTG_STATE_A_HOST:
824 case OTG_STATE_A_SUSPEND:
Daniel Mack0b3eba42013-04-10 21:55:42 +0200825 musb_host_resume_root_hub(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300826 musb_root_disconnect(musb);
Felipe Balbi032ec492011-11-24 15:46:26 +0200827 if (musb->a_wait_bcon != 0)
Felipe Balbi550a7372008-07-24 12:27:36 +0300828 musb_platform_try_idle(musb, jiffies
829 + msecs_to_jiffies(musb->a_wait_bcon));
830 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300831 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700832 /* REVISIT this behaves for "real disconnect"
833 * cases; make sure the other transitions from
834 * from B_HOST act right too. The B_HOST code
835 * in hnp_stop() is currently not used...
836 */
837 musb_root_disconnect(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +0200838 if (musb->hcd)
839 musb->hcd->self.is_b_host = 0;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100840 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
David Brownellab983f2a2009-03-31 12:35:09 -0700841 MUSB_DEV_MODE(musb);
842 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 break;
844 case OTG_STATE_A_PERIPHERAL:
845 musb_hnp_stop(musb);
846 musb_root_disconnect(musb);
847 /* FALLTHROUGH */
848 case OTG_STATE_B_WAIT_ACON:
849 /* FALLTHROUGH */
Felipe Balbi550a7372008-07-24 12:27:36 +0300850 case OTG_STATE_B_PERIPHERAL:
851 case OTG_STATE_B_IDLE:
852 musb_g_disconnect(musb);
853 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300854 default:
855 WARNING("unhandled DISCONNECT transition (%s)\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100856 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300857 break;
858 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300859 }
860
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200861 /* mentor saves a bit: bus reset and babble share the same irq.
862 * only host sees babble; only peripheral sees bus reset.
863 */
864 if (int_usb & MUSB_INTR_RESET) {
865 handled = IRQ_HANDLED;
Felipe Balbi896f7ea2015-02-25 14:03:23 -0600866 if (devctl & MUSB_DEVCTL_HM) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200867 /*
Felipe Balbi34754de2015-02-26 14:43:57 -0600868 * When BABBLE happens what we can depends on which
Felipe Balbi28378d52015-02-26 10:54:27 -0600869 * platform MUSB is running, because some platforms
870 * implemented proprietary means for 'recovering' from
871 * Babble conditions. One such platform is AM335x. In
Felipe Balbi34754de2015-02-26 14:43:57 -0600872 * most cases, however, the only thing we can do is
873 * drop the session.
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200874 */
Felipe Balbi34754de2015-02-26 14:43:57 -0600875 dev_err(musb->controller, "Babble\n");
Felipe Balbid0fc0a22015-02-25 14:07:52 -0600876
Felipe Balbi34754de2015-02-26 14:43:57 -0600877 if (is_host_active(musb))
878 musb_recover_from_babble(musb);
Felipe Balbia04d46d2011-11-24 15:46:27 +0200879 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300880 dev_dbg(musb->controller, "BUS RESET as %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100881 usb_otg_state_string(musb->xceiv->otg->state));
882 switch (musb->xceiv->otg->state) {
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200883 case OTG_STATE_A_SUSPEND:
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200884 musb_g_reset(musb);
885 /* FALLTHROUGH */
886 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
887 /* never use invalid T(a_wait_bcon) */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300888 dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100889 usb_otg_state_string(musb->xceiv->otg->state),
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200890 TA_WAIT_BCON(musb));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200891 mod_timer(&musb->otg_timer, jiffies
892 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
893 break;
894 case OTG_STATE_A_PERIPHERAL:
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200895 del_timer(&musb->otg_timer);
896 musb_g_reset(musb);
897 break;
898 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300899 dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100900 usb_otg_state_string(musb->xceiv->otg->state));
901 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200902 musb_g_reset(musb);
903 break;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200904 case OTG_STATE_B_IDLE:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100905 musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200906 /* FALLTHROUGH */
907 case OTG_STATE_B_PERIPHERAL:
908 musb_g_reset(musb);
909 break;
910 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300911 dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +0100912 usb_otg_state_string(musb->xceiv->otg->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200913 }
914 }
915 }
916
917#if 0
918/* REVISIT ... this would be for multiplexing periodic endpoints, or
919 * supporting transfer phasing to prevent exceeding ISO bandwidth
920 * limits of a given frame or microframe.
921 *
922 * It's not needed for peripheral side, which dedicates endpoints;
923 * though it _might_ use SOF irqs for other purposes.
924 *
925 * And it's not currently needed for host side, which also dedicates
926 * endpoints, relies on TX/RX interval registers, and isn't claimed
927 * to support ISO transfers yet.
928 */
929 if (int_usb & MUSB_INTR_SOF) {
930 void __iomem *mbase = musb->mregs;
931 struct musb_hw_ep *ep;
932 u8 epnum;
933 u16 frame;
934
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300935 dev_dbg(musb->controller, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300936 handled = IRQ_HANDLED;
937
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200938 /* start any periodic Tx transfers waiting for current frame */
939 frame = musb_readw(mbase, MUSB_FRAME);
940 ep = musb->endpoints;
941 for (epnum = 1; (epnum < musb->nr_endpoints)
942 && (musb->epmask >= (1 << epnum));
943 epnum++, ep++) {
944 /*
945 * FIXME handle framecounter wraps (12 bits)
946 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300947 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200948 if (ep->dwWaitFrame >= frame) {
949 ep->dwWaitFrame = 0;
950 pr_debug("SOF --> periodic TX%s on %d\n",
951 ep->tx_channel ? " DMA" : "",
952 epnum);
953 if (!ep->tx_channel)
954 musb_h_tx_start(musb, epnum);
955 else
956 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300957 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200958 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300959 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200960#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300961
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200962 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300963
964 return handled;
965}
966
967/*-------------------------------------------------------------------------*/
968
Felipe Balbie1eb3eb2015-02-26 11:26:09 -0600969static void musb_disable_interrupts(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300970{
971 void __iomem *mbase = musb->mregs;
972 u16 temp;
973
974 /* disable interrupts */
975 musb_writeb(mbase, MUSB_INTRUSBE, 0);
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100976 musb->intrtxe = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300977 musb_writew(mbase, MUSB_INTRTXE, 0);
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +0100978 musb->intrrxe = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300979 musb_writew(mbase, MUSB_INTRRXE, 0);
980
Felipe Balbi550a7372008-07-24 12:27:36 +0300981 /* flush pending interrupts */
982 temp = musb_readb(mbase, MUSB_INTRUSB);
983 temp = musb_readw(mbase, MUSB_INTRTX);
984 temp = musb_readw(mbase, MUSB_INTRRX);
Felipe Balbie1eb3eb2015-02-26 11:26:09 -0600985}
Felipe Balbi550a7372008-07-24 12:27:36 +0300986
Felipe Balbie1eb3eb2015-02-26 11:26:09 -0600987static void musb_enable_interrupts(struct musb *musb)
988{
989 void __iomem *regs = musb->mregs;
990
991 /* Set INT enable registers, enable interrupts */
992 musb->intrtxe = musb->epmask;
993 musb_writew(regs, MUSB_INTRTXE, musb->intrtxe);
994 musb->intrrxe = musb->epmask & 0xfffe;
995 musb_writew(regs, MUSB_INTRRXE, musb->intrrxe);
996 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
997
998}
999
1000static void musb_generic_disable(struct musb *musb)
1001{
1002 void __iomem *mbase = musb->mregs;
1003
1004 musb_disable_interrupts(musb);
1005
1006 /* off */
1007 musb_writeb(mbase, MUSB_DEVCTL, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001008}
1009
1010/*
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +02001011 * Program the HDRC to start (enable interrupts, dma, etc.).
1012 */
1013void musb_start(struct musb *musb)
1014{
1015 void __iomem *regs = musb->mregs;
1016 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
1017
1018 dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
1019
Felipe Balbie1eb3eb2015-02-26 11:26:09 -06001020 musb_enable_interrupts(musb);
Sebastian Andrzej Siewior001dd842013-10-11 10:38:13 +02001021 musb_writeb(regs, MUSB_TESTMODE, 0);
1022
1023 /* put into basic highspeed mode and start session */
1024 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
1025 | MUSB_POWER_HSENAB
1026 /* ENSUSPEND wedges tusb */
1027 /* | MUSB_POWER_ENSUSPEND */
1028 );
1029
1030 musb->is_active = 0;
1031 devctl = musb_readb(regs, MUSB_DEVCTL);
1032 devctl &= ~MUSB_DEVCTL_SESSION;
1033
1034 /* session started after:
1035 * (a) ID-grounded irq, host mode;
1036 * (b) vbus present/connect IRQ, peripheral mode;
1037 * (c) peripheral initiates, using SRP
1038 */
1039 if (musb->port_mode != MUSB_PORT_MODE_HOST &&
1040 (devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS) {
1041 musb->is_active = 1;
1042 } else {
1043 devctl |= MUSB_DEVCTL_SESSION;
1044 }
1045
1046 musb_platform_enable(musb);
1047 musb_writeb(regs, MUSB_DEVCTL, devctl);
1048}
1049
1050/*
Felipe Balbi550a7372008-07-24 12:27:36 +03001051 * Make the HDRC stop (disable interrupts, etc.);
1052 * reversible by musb_start
1053 * called on gadget driver unregister
1054 * with controller locked, irqs blocked
1055 * acts as a NOP unless some role activated the hardware
1056 */
1057void musb_stop(struct musb *musb)
1058{
1059 /* stop IRQs, timers, ... */
1060 musb_platform_disable(musb);
1061 musb_generic_disable(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001062 dev_dbg(musb->controller, "HDRC disabled\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001063
1064 /* FIXME
1065 * - mark host and/or peripheral drivers unusable/inactive
1066 * - disable DMA (and enable it in HdrcStart)
1067 * - make sure we can musb_start() after musb_stop(); with
1068 * OTG mode, gadget driver module rmmod/modprobe cycles that
1069 * - ...
1070 */
1071 musb_platform_try_idle(musb, 0);
1072}
1073
1074static void musb_shutdown(struct platform_device *pdev)
1075{
1076 struct musb *musb = dev_to_musb(&pdev->dev);
1077 unsigned long flags;
1078
Hema HK4f9edd22011-03-22 16:02:12 +05301079 pm_runtime_get_sync(musb->controller);
Grazvydas Ignotas24307ca2012-01-12 15:22:45 +02001080
Daniel Mack2cc65fe2013-04-10 21:55:47 +02001081 musb_host_cleanup(musb);
Grazvydas Ignotas24307ca2012-01-12 15:22:45 +02001082 musb_gadget_cleanup(musb);
1083
Felipe Balbi550a7372008-07-24 12:27:36 +03001084 spin_lock_irqsave(&musb->lock, flags);
1085 musb_platform_disable(musb);
1086 musb_generic_disable(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001087 spin_unlock_irqrestore(&musb->lock, flags);
1088
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001089 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1090 musb_platform_exit(musb);
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001091
Hema HK4f9edd22011-03-22 16:02:12 +05301092 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001093 /* FIXME power down */
1094}
1095
1096
1097/*-------------------------------------------------------------------------*/
1098
1099/*
1100 * The silicon either has hard-wired endpoint configurations, or else
1101 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001102 * writing only the dynamic sizing is very well tested. Since we switched
1103 * away from compile-time hardware parameters, we can no longer rely on
1104 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001105 *
1106 * We don't currently use dynamic fifo setup capability to do anything
1107 * more than selecting one of a bunch of predefined configurations.
1108 */
Tony Lindgren8a77f052014-11-24 11:05:04 -08001109static ushort fifo_mode;
Felipe Balbi550a7372008-07-24 12:27:36 +03001110
1111/* "modprobe ... fifo_mode=1" etc */
1112module_param(fifo_mode, ushort, 0);
1113MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1114
Felipe Balbi550a7372008-07-24 12:27:36 +03001115/*
1116 * tables defining fifo_mode values. define more if you like.
1117 * for host side, make sure both halves of ep1 are set up.
1118 */
1119
1120/* mode 0 - fits in 2KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001121static struct musb_fifo_cfg mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001122{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1123{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1124{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1125{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1126{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1127};
1128
1129/* mode 1 - fits in 4KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001130static struct musb_fifo_cfg mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001131{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1132{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1133{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1134{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1135{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1136};
1137
1138/* mode 2 - fits in 4KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001139static struct musb_fifo_cfg mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001140{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1141{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1142{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1143{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1144{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1145{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1146};
1147
1148/* mode 3 - fits in 4KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001149static struct musb_fifo_cfg mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001150{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1151{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1152{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1153{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1154{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1155{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1156};
1157
1158/* mode 4 - fits in 16KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001159static struct musb_fifo_cfg mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001160{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1161{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1162{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1163{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1164{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1165{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1166{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1167{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1168{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1169{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1170{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1171{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1172{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1173{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1174{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1175{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1176{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1177{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001178{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1179{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1180{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1181{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1182{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1183{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1184{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001185{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1186{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1187};
1188
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001189/* mode 5 - fits in 8KB */
Bill Pembertond3608b62012-11-19 13:24:34 -05001190static struct musb_fifo_cfg mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001191{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1192{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1193{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1194{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1195{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1196{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1197{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1198{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1199{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1200{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1201{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1202{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1203{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1204{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1205{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1206{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1207{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1208{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1209{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1210{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1211{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1212{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1213{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1214{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1215{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1216{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1217{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1218};
Felipe Balbi550a7372008-07-24 12:27:36 +03001219
1220/*
1221 * configure a fifo; for non-shared endpoints, this may be called
1222 * once for a tx fifo and once for an rx fifo.
1223 *
1224 * returns negative errno or offset for next fifo.
1225 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001226static int
Felipe Balbi550a7372008-07-24 12:27:36 +03001227fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001228 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001229{
1230 void __iomem *mbase = musb->mregs;
1231 int size = 0;
1232 u16 maxpacket = cfg->maxpacket;
1233 u16 c_off = offset >> 3;
1234 u8 c_size;
1235
1236 /* expect hw_ep has already been zero-initialized */
1237
1238 size = ffs(max(maxpacket, (u16) 8)) - 1;
1239 maxpacket = 1 << size;
1240
1241 c_size = size - 3;
1242 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001243 if ((offset + (maxpacket << 1)) >
1244 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001245 return -EMSGSIZE;
1246 c_size |= MUSB_FIFOSZ_DPB;
1247 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001248 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001249 return -EMSGSIZE;
1250 }
1251
1252 /* configure the FIFO */
1253 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1254
Felipe Balbi550a7372008-07-24 12:27:36 +03001255 /* EP0 reserved endpoint for control, bidirectional;
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05301256 * EP1 reserved for bulk, two unidirectional halves.
Felipe Balbi550a7372008-07-24 12:27:36 +03001257 */
1258 if (hw_ep->epnum == 1)
1259 musb->bulk_ep = hw_ep;
1260 /* REVISIT error check: be sure ep0 can both rx and tx ... */
Felipe Balbi550a7372008-07-24 12:27:36 +03001261 switch (cfg->style) {
1262 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001263 musb_write_txfifosz(mbase, c_size);
1264 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001265 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1266 hw_ep->max_packet_sz_tx = maxpacket;
1267 break;
1268 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001269 musb_write_rxfifosz(mbase, c_size);
1270 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001271 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1272 hw_ep->max_packet_sz_rx = maxpacket;
1273 break;
1274 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001275 musb_write_txfifosz(mbase, c_size);
1276 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001277 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1278 hw_ep->max_packet_sz_rx = maxpacket;
1279
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001280 musb_write_rxfifosz(mbase, c_size);
1281 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1283 hw_ep->max_packet_sz_tx = maxpacket;
1284
1285 hw_ep->is_shared_fifo = true;
1286 break;
1287 }
1288
1289 /* NOTE rx and tx endpoint irqs aren't managed separately,
1290 * which happens to be ok
1291 */
1292 musb->epmask |= (1 << hw_ep->epnum);
1293
1294 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1295}
1296
Bill Pembertond3608b62012-11-19 13:24:34 -05001297static struct musb_fifo_cfg ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001298 .style = FIFO_RXTX, .maxpacket = 64,
1299};
1300
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001301static int ep_config_from_table(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001302{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001303 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001304 unsigned i, n;
1305 int offset;
1306 struct musb_hw_ep *hw_ep = musb->endpoints;
1307
Felipe Balbie6c213b2010-03-12 10:29:06 +02001308 if (musb->config->fifo_cfg) {
1309 cfg = musb->config->fifo_cfg;
1310 n = musb->config->fifo_cfg_size;
1311 goto done;
1312 }
1313
Felipe Balbi550a7372008-07-24 12:27:36 +03001314 switch (fifo_mode) {
1315 default:
1316 fifo_mode = 0;
1317 /* FALLTHROUGH */
1318 case 0:
1319 cfg = mode_0_cfg;
1320 n = ARRAY_SIZE(mode_0_cfg);
1321 break;
1322 case 1:
1323 cfg = mode_1_cfg;
1324 n = ARRAY_SIZE(mode_1_cfg);
1325 break;
1326 case 2:
1327 cfg = mode_2_cfg;
1328 n = ARRAY_SIZE(mode_2_cfg);
1329 break;
1330 case 3:
1331 cfg = mode_3_cfg;
1332 n = ARRAY_SIZE(mode_3_cfg);
1333 break;
1334 case 4:
1335 cfg = mode_4_cfg;
1336 n = ARRAY_SIZE(mode_4_cfg);
1337 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001338 case 5:
1339 cfg = mode_5_cfg;
1340 n = ARRAY_SIZE(mode_5_cfg);
1341 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001342 }
1343
1344 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1345 musb_driver_name, fifo_mode);
1346
1347
Felipe Balbie6c213b2010-03-12 10:29:06 +02001348done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001349 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1350 /* assert(offset > 0) */
1351
1352 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001353 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001354 */
1355
1356 for (i = 0; i < n; i++) {
1357 u8 epn = cfg->hw_ep_num;
1358
Felipe Balbica6d1b12008-08-08 12:40:54 +03001359 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001360 pr_debug("%s: invalid ep %d\n",
1361 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001362 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001363 }
1364 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1365 if (offset < 0) {
1366 pr_debug("%s: mem overrun, ep %d\n",
1367 musb_driver_name, epn);
Shubhrajyoti Df69dfa12012-08-07 19:56:31 +05301368 return offset;
Felipe Balbi550a7372008-07-24 12:27:36 +03001369 }
1370 epn++;
1371 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1372 }
1373
1374 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1375 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001376 n + 1, musb->config->num_eps * 2 - 1,
1377 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001378
Felipe Balbi550a7372008-07-24 12:27:36 +03001379 if (!musb->bulk_ep) {
1380 pr_debug("%s: missing bulk\n", musb_driver_name);
1381 return -EINVAL;
1382 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001383
1384 return 0;
1385}
1386
1387
1388/*
1389 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1390 * @param musb the controller
1391 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001392static int ep_config_from_hw(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001393{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001394 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001395 struct musb_hw_ep *hw_ep;
Felipe Balbia1565442012-08-07 14:00:50 +03001396 void __iomem *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001397 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001398
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001399 dev_dbg(musb->controller, "<== static silicon ep config\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001400
1401 /* FIXME pick up ep0 maxpacket size */
1402
Felipe Balbica6d1b12008-08-08 12:40:54 +03001403 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001404 musb_ep_select(mbase, epnum);
1405 hw_ep = musb->endpoints + epnum;
1406
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001407 ret = musb_read_fifosize(musb, hw_ep, epnum);
1408 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001409 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001410
1411 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1412
Felipe Balbi550a7372008-07-24 12:27:36 +03001413 /* pick an RX/TX endpoint for bulk */
1414 if (hw_ep->max_packet_sz_tx < 512
1415 || hw_ep->max_packet_sz_rx < 512)
1416 continue;
1417
1418 /* REVISIT: this algorithm is lazy, we should at least
1419 * try to pick a double buffered endpoint.
1420 */
1421 if (musb->bulk_ep)
1422 continue;
1423 musb->bulk_ep = hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03001424 }
1425
Felipe Balbi550a7372008-07-24 12:27:36 +03001426 if (!musb->bulk_ep) {
1427 pr_debug("%s: missing bulk\n", musb_driver_name);
1428 return -EINVAL;
1429 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001430
1431 return 0;
1432}
1433
1434enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1435
1436/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1437 * configure endpoints, or take their config from silicon
1438 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001439static int musb_core_init(u16 musb_type, struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001440{
Felipe Balbi550a7372008-07-24 12:27:36 +03001441 u8 reg;
1442 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301443 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001444 void __iomem *mbase = musb->mregs;
1445 int status = 0;
1446 int i;
1447
1448 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001449 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001450
1451 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001452 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001453 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001454 musb->dyn_fifo = true;
1455 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001456 if (reg & MUSB_CONFIGDATA_MPRXE) {
1457 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001458 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001459 }
1460 if (reg & MUSB_CONFIGDATA_MPTXE) {
1461 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001462 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001463 }
1464 if (reg & MUSB_CONFIGDATA_HBRXE) {
1465 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001466 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001467 }
1468 if (reg & MUSB_CONFIGDATA_HBTXE) {
1469 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001470 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001471 }
1472 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1473 strcat(aInfo, ", SoftConn");
1474
1475 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1476 musb_driver_name, reg, aInfo);
1477
Felipe Balbi550a7372008-07-24 12:27:36 +03001478 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001479 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1480 musb->is_multipoint = 1;
1481 type = "M";
1482 } else {
1483 musb->is_multipoint = 0;
1484 type = "";
Felipe Balbi550a7372008-07-24 12:27:36 +03001485#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1486 printk(KERN_ERR
1487 "%s: kernel must blacklist external hubs\n",
1488 musb_driver_name);
1489#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001490 }
1491
1492 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301493 musb->hwvers = musb_read_hwvers(mbase);
1494 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1495 MUSB_HWVERS_MINOR(musb->hwvers),
1496 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001497 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1498 musb_driver_name, type, aRevision, aDate);
1499
1500 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001501 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001502
1503 /* discover endpoint configuration */
1504 musb->nr_endpoints = 1;
1505 musb->epmask = 1;
1506
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001507 if (musb->dyn_fifo)
1508 status = ep_config_from_table(musb);
1509 else
1510 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001511
1512 if (status < 0)
1513 return status;
1514
1515 /* finish init, and print endpoint config */
1516 for (i = 0; i < musb->nr_endpoints; i++) {
1517 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1518
Tony Lindgren1b40fc52014-11-24 11:05:02 -08001519 hw_ep->fifo = musb->io.fifo_offset(i) + mbase;
Tony Lindgrenebf39922014-11-24 11:05:06 -08001520#if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
Tony Lindgren1b40fc52014-11-24 11:05:02 -08001521 if (musb->io.quirks & MUSB_IN_TUSB) {
1522 hw_ep->fifo_async = musb->async + 0x400 +
1523 musb->io.fifo_offset(i);
1524 hw_ep->fifo_sync = musb->sync + 0x400 +
1525 musb->io.fifo_offset(i);
1526 hw_ep->fifo_sync_va =
1527 musb->sync_va + 0x400 + musb->io.fifo_offset(i);
Felipe Balbi550a7372008-07-24 12:27:36 +03001528
Tony Lindgren1b40fc52014-11-24 11:05:02 -08001529 if (i == 0)
1530 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1531 else
1532 hw_ep->conf = mbase + 0x400 +
1533 (((i - 1) & 0xf) << 2);
1534 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001535#endif
1536
Tony Lindgrend026e9c2014-11-24 11:05:03 -08001537 hw_ep->regs = musb->io.ep_offset(i, 0) + mbase;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001538 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001539 hw_ep->rx_reinit = 1;
1540 hw_ep->tx_reinit = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001541
1542 if (hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001543 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001544 "%s: hw_ep %d%s, %smax %d\n",
1545 musb_driver_name, i,
1546 hw_ep->is_shared_fifo ? "shared" : "tx",
1547 hw_ep->tx_double_buffered
1548 ? "doublebuffer, " : "",
1549 hw_ep->max_packet_sz_tx);
1550 }
1551 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001552 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001553 "%s: hw_ep %d%s, %smax %d\n",
1554 musb_driver_name, i,
1555 "rx",
1556 hw_ep->rx_double_buffered
1557 ? "doublebuffer, " : "",
1558 hw_ep->max_packet_sz_rx);
1559 }
1560 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001561 dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
Felipe Balbi550a7372008-07-24 12:27:36 +03001562 }
1563
1564 return 0;
1565}
1566
1567/*-------------------------------------------------------------------------*/
1568
Felipe Balbi550a7372008-07-24 12:27:36 +03001569/*
1570 * handle all the irqs defined by the HDRC core. for now we expect: other
1571 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1572 * will be assigned, and the irq will already have been acked.
1573 *
1574 * called in irq context with spinlock held, irqs blocked
1575 */
1576irqreturn_t musb_interrupt(struct musb *musb)
1577{
1578 irqreturn_t retval = IRQ_NONE;
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001579 unsigned long status;
1580 unsigned long epnum;
Sebastian Andrzej Siewiorb11e94d2012-10-30 19:52:23 +01001581 u8 devctl;
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001582
1583 if (!musb->int_usb && !musb->int_tx && !musb->int_rx)
1584 return IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001585
1586 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi550a7372008-07-24 12:27:36 +03001587
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001588 dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
Daniel Mackc03da382014-05-26 14:52:36 +02001589 is_host_active(musb) ? "host" : "peripheral",
Felipe Balbi550a7372008-07-24 12:27:36 +03001590 musb->int_usb, musb->int_tx, musb->int_rx);
1591
Felipe Balbie3c93e12013-12-30 12:33:53 -06001592 /**
1593 * According to Mentor Graphics' documentation, flowchart on page 98,
1594 * IRQ should be handled as follows:
1595 *
1596 * . Resume IRQ
1597 * . Session Request IRQ
1598 * . VBUS Error IRQ
1599 * . Suspend IRQ
1600 * . Connect IRQ
1601 * . Disconnect IRQ
1602 * . Reset/Babble IRQ
1603 * . SOF IRQ (we're not using this one)
1604 * . Endpoint 0 IRQ
1605 * . TX Endpoints
1606 * . RX Endpoints
1607 *
1608 * We will be following that flowchart in order to avoid any problems
1609 * that might arise with internal Finite State Machine.
Felipe Balbi550a7372008-07-24 12:27:36 +03001610 */
Felipe Balbie3c93e12013-12-30 12:33:53 -06001611
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301612 if (musb->int_usb)
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001613 retval |= musb_stage0_irq(musb, musb->int_usb, devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001614
Felipe Balbi550a7372008-07-24 12:27:36 +03001615 if (musb->int_tx & 1) {
Daniel Mackc03da382014-05-26 14:52:36 +02001616 if (is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03001617 retval |= musb_h_ep0_irq(musb);
1618 else
1619 retval |= musb_g_ep0_irq(musb);
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001620
1621 /* we have just handled endpoint 0 IRQ, clear it */
1622 musb->int_tx &= ~BIT(0);
Felipe Balbi550a7372008-07-24 12:27:36 +03001623 }
1624
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001625 status = musb->int_tx;
1626
1627 for_each_set_bit(epnum, &status, 16) {
1628 retval = IRQ_HANDLED;
1629 if (is_host_active(musb))
1630 musb_host_tx(musb, epnum);
1631 else
1632 musb_g_tx(musb, epnum);
Felipe Balbie3c93e12013-12-30 12:33:53 -06001633 }
1634
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001635 status = musb->int_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03001636
Felipe Balbi31a0ede2013-12-30 12:42:38 -06001637 for_each_set_bit(epnum, &status, 16) {
1638 retval = IRQ_HANDLED;
1639 if (is_host_active(musb))
1640 musb_host_rx(musb, epnum);
1641 else
1642 musb_g_rx(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001643 }
1644
Felipe Balbi550a7372008-07-24 12:27:36 +03001645 return retval;
1646}
Felipe Balbi981430a2011-05-11 13:02:23 +03001647EXPORT_SYMBOL_GPL(musb_interrupt);
Felipe Balbi550a7372008-07-24 12:27:36 +03001648
1649#ifndef CONFIG_MUSB_PIO_ONLY
Bill Pembertond3608b62012-11-19 13:24:34 -05001650static bool use_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001651
1652/* "modprobe ... use_dma=0" etc */
1653module_param(use_dma, bool, 0);
1654MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1655
1656void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1657{
Felipe Balbi550a7372008-07-24 12:27:36 +03001658 /* called with controller lock already held */
1659
1660 if (!epnum) {
1661#ifndef CONFIG_USB_TUSB_OMAP_DMA
1662 if (!is_cppi_enabled()) {
1663 /* endpoint 0 */
Daniel Mackc03da382014-05-26 14:52:36 +02001664 if (is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03001665 musb_h_ep0_irq(musb);
1666 else
1667 musb_g_ep0_irq(musb);
1668 }
1669#endif
1670 } else {
1671 /* endpoints 1..15 */
1672 if (transmit) {
Daniel Mackc03da382014-05-26 14:52:36 +02001673 if (is_host_active(musb))
Felipe Balbia04d46d2011-11-24 15:46:27 +02001674 musb_host_tx(musb, epnum);
1675 else
1676 musb_g_tx(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001677 } else {
1678 /* receive */
Daniel Mackc03da382014-05-26 14:52:36 +02001679 if (is_host_active(musb))
Felipe Balbia04d46d2011-11-24 15:46:27 +02001680 musb_host_rx(musb, epnum);
1681 else
1682 musb_g_rx(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001683 }
1684 }
1685}
Arnd Bergmann9a35f872011-10-02 16:45:47 +02001686EXPORT_SYMBOL_GPL(musb_dma_completion);
Felipe Balbi550a7372008-07-24 12:27:36 +03001687
1688#else
1689#define use_dma 0
1690#endif
1691
1692/*-------------------------------------------------------------------------*/
1693
Felipe Balbi550a7372008-07-24 12:27:36 +03001694static ssize_t
1695musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1696{
1697 struct musb *musb = dev_to_musb(dev);
1698 unsigned long flags;
1699 int ret = -EINVAL;
1700
1701 spin_lock_irqsave(&musb->lock, flags);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001702 ret = sprintf(buf, "%s\n", usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001703 spin_unlock_irqrestore(&musb->lock, flags);
1704
1705 return ret;
1706}
1707
1708static ssize_t
1709musb_mode_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 long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001714 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001715
1716 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001717 if (sysfs_streq(buf, "host"))
1718 status = musb_platform_set_mode(musb, MUSB_HOST);
1719 else if (sysfs_streq(buf, "peripheral"))
1720 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1721 else if (sysfs_streq(buf, "otg"))
1722 status = musb_platform_set_mode(musb, MUSB_OTG);
1723 else
1724 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001725 spin_unlock_irqrestore(&musb->lock, flags);
1726
David Brownell96a274d2008-11-24 13:06:47 +02001727 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001728}
1729static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1730
1731static ssize_t
1732musb_vbus_store(struct device *dev, struct device_attribute *attr,
1733 const char *buf, size_t n)
1734{
1735 struct musb *musb = dev_to_musb(dev);
1736 unsigned long flags;
1737 unsigned long val;
1738
1739 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001740 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001741 return -EINVAL;
1742 }
1743
1744 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001745 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1746 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001747 if (musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001748 musb->is_active = 0;
1749 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1750 spin_unlock_irqrestore(&musb->lock, flags);
1751
1752 return n;
1753}
1754
1755static ssize_t
1756musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1757{
1758 struct musb *musb = dev_to_musb(dev);
1759 unsigned long flags;
1760 unsigned long val;
1761 int vbus;
1762
1763 spin_lock_irqsave(&musb->lock, flags);
1764 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001765 /* FIXME get_vbus_status() is normally #defined as false...
1766 * and is effectively TUSB-specific.
1767 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001768 vbus = musb_platform_get_vbus_status(musb);
1769 spin_unlock_irqrestore(&musb->lock, flags);
1770
David Brownellf7f9d632009-03-31 12:32:12 -07001771 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001772 vbus ? "on" : "off", val);
1773}
1774static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1775
Felipe Balbi550a7372008-07-24 12:27:36 +03001776/* Gadget drivers can't know that a host is connected so they might want
1777 * to start SRP, but users can. This allows userspace to trigger SRP.
1778 */
1779static ssize_t
1780musb_srp_store(struct device *dev, struct device_attribute *attr,
1781 const char *buf, size_t n)
1782{
1783 struct musb *musb = dev_to_musb(dev);
1784 unsigned short srp;
1785
1786 if (sscanf(buf, "%hu", &srp) != 1
1787 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001788 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001789 return -EINVAL;
1790 }
1791
1792 if (srp == 1)
1793 musb_g_wakeup(musb);
1794
1795 return n;
1796}
1797static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1798
Felipe Balbi94375752009-12-15 11:08:38 +02001799static struct attribute *musb_attributes[] = {
1800 &dev_attr_mode.attr,
1801 &dev_attr_vbus.attr,
Felipe Balbi94375752009-12-15 11:08:38 +02001802 &dev_attr_srp.attr,
Felipe Balbi94375752009-12-15 11:08:38 +02001803 NULL
1804};
1805
1806static const struct attribute_group musb_attr_group = {
1807 .attrs = musb_attributes,
1808};
1809
Felipe Balbi550a7372008-07-24 12:27:36 +03001810/* Only used to provide driver mode change events */
1811static void musb_irq_work(struct work_struct *data)
1812{
1813 struct musb *musb = container_of(data, struct musb, irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03001814
Antoine Tenarte47d9252014-10-30 18:41:13 +01001815 if (musb->xceiv->otg->state != musb->xceiv_old_state) {
1816 musb->xceiv_old_state = musb->xceiv->otg->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001817 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1818 }
1819}
1820
Felipe Balbi83b8f5b2015-02-26 14:27:12 -06001821static void musb_recover_from_babble(struct musb *musb)
Daniel Mackca88fc22014-04-02 13:58:28 +02001822{
Felipe Balbib4dc38f2015-02-26 14:02:35 -06001823 int ret;
1824 u8 devctl;
Daniel Mackca88fc22014-04-02 13:58:28 +02001825
Felipe Balbi02443362015-02-26 14:42:19 -06001826 musb_disable_interrupts(musb);
1827
Felipe Balbi83b8f5b2015-02-26 14:27:12 -06001828 /*
1829 * wait at least 320 cycles of 60MHz clock. That's 5.3us, we will give
1830 * it some slack and wait for 10us.
1831 */
1832 udelay(10);
1833
Felipe Balbib28a6432015-02-26 14:20:58 -06001834 ret = musb_platform_recover(musb);
Felipe Balbiba7ee8b2015-02-26 11:31:49 -06001835 if (ret) {
1836 musb_enable_interrupts(musb);
George Cheriand871c622014-07-16 18:22:11 +05301837 return;
Felipe Balbiba7ee8b2015-02-26 11:31:49 -06001838 }
Daniel Mackca88fc22014-04-02 13:58:28 +02001839
Felipe Balbib4dc38f2015-02-26 14:02:35 -06001840 /* drop session bit */
1841 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1842 devctl &= ~MUSB_DEVCTL_SESSION;
1843 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
Daniel Mackca88fc22014-04-02 13:58:28 +02001844
Felipe Balbib4dc38f2015-02-26 14:02:35 -06001845 /* tell usbcore about it */
1846 musb_root_disconnect(musb);
Daniel Mackca88fc22014-04-02 13:58:28 +02001847
1848 /*
George Cheriand871c622014-07-16 18:22:11 +05301849 * When a babble condition occurs, the musb controller
1850 * removes the session bit and the endpoint config is lost.
Daniel Mackca88fc22014-04-02 13:58:28 +02001851 */
1852 if (musb->dyn_fifo)
Felipe Balbib4dc38f2015-02-26 14:02:35 -06001853 ret = ep_config_from_table(musb);
Daniel Mackca88fc22014-04-02 13:58:28 +02001854 else
Felipe Balbib4dc38f2015-02-26 14:02:35 -06001855 ret = ep_config_from_hw(musb);
Daniel Mackca88fc22014-04-02 13:58:28 +02001856
Felipe Balbib4dc38f2015-02-26 14:02:35 -06001857 /* restart session */
1858 if (ret == 0)
Daniel Mackca88fc22014-04-02 13:58:28 +02001859 musb_start(musb);
1860}
1861
Felipe Balbi550a7372008-07-24 12:27:36 +03001862/* --------------------------------------------------------------------------
1863 * Init support
1864 */
1865
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001866static struct musb *allocate_instance(struct device *dev,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001867 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001868{
1869 struct musb *musb;
1870 struct musb_hw_ep *ep;
1871 int epnum;
Daniel Mack74c2e932013-04-10 21:55:45 +02001872 int ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03001873
Daniel Mack74c2e932013-04-10 21:55:45 +02001874 musb = devm_kzalloc(dev, sizeof(*musb), GFP_KERNEL);
1875 if (!musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001876 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001877
Felipe Balbi550a7372008-07-24 12:27:36 +03001878 INIT_LIST_HEAD(&musb->control);
1879 INIT_LIST_HEAD(&musb->in_bulk);
1880 INIT_LIST_HEAD(&musb->out_bulk);
1881
Felipe Balbi550a7372008-07-24 12:27:36 +03001882 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001883 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001884 musb->mregs = mbase;
1885 musb->ctrl_base = mbase;
1886 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001887 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001888 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001889 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001890 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001891 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001892 ep->musb = musb;
1893 ep->epnum = epnum;
1894 }
1895
1896 musb->controller = dev;
Felipe Balbi743411b2010-12-01 13:22:05 +02001897
Daniel Mack74c2e932013-04-10 21:55:45 +02001898 ret = musb_host_alloc(musb);
1899 if (ret < 0)
1900 goto err_free;
1901
1902 dev_set_drvdata(dev, musb);
1903
Felipe Balbi550a7372008-07-24 12:27:36 +03001904 return musb;
Daniel Mack74c2e932013-04-10 21:55:45 +02001905
1906err_free:
1907 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001908}
1909
1910static void musb_free(struct musb *musb)
1911{
1912 /* this has multiple entry modes. it handles fault cleanup after
1913 * probe(), where things may be partially set up, as well as rmmod
1914 * cleanup after everything's been de-activated.
1915 */
1916
1917#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001918 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001919#endif
1920
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001921 if (musb->nIrq >= 0) {
1922 if (musb->irq_wake)
1923 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001924 free_irq(musb->nIrq, musb);
1925 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001926
Daniel Mack74c2e932013-04-10 21:55:45 +02001927 musb_host_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001928}
1929
Daniel Mack8ed1fb72013-12-18 20:23:46 +01001930static void musb_deassert_reset(struct work_struct *work)
1931{
1932 struct musb *musb;
1933 unsigned long flags;
1934
1935 musb = container_of(work, struct musb, deassert_reset_work.work);
1936
1937 spin_lock_irqsave(&musb->lock, flags);
1938
1939 if (musb->port1_status & USB_PORT_STAT_RESET)
1940 musb_port_reset(musb, false);
1941
1942 spin_unlock_irqrestore(&musb->lock, flags);
1943}
1944
Felipe Balbi550a7372008-07-24 12:27:36 +03001945/*
1946 * Perform generic per-controller initialization.
1947 *
Sergei Shtylyov28dd9242012-08-21 21:22:45 +04001948 * @dev: the controller (already clocked, etc)
1949 * @nIrq: IRQ number
1950 * @ctrl: virtual address of controller registers,
Felipe Balbi550a7372008-07-24 12:27:36 +03001951 * not yet corrected for platform-specific offsets
1952 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001953static int
Felipe Balbi550a7372008-07-24 12:27:36 +03001954musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1955{
1956 int status;
1957 struct musb *musb;
Jingoo Hanc1a7d672013-07-30 17:03:12 +09001958 struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001959
1960 /* The driver might handle more features than the board; OK.
1961 * Fail when the board needs a feature that's not enabled.
1962 */
1963 if (!plat) {
1964 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001965 status = -ENODEV;
1966 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001967 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001968
Felipe Balbi550a7372008-07-24 12:27:36 +03001969 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001970 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001971 if (!musb) {
1972 status = -ENOMEM;
1973 goto fail0;
1974 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001975
1976 spin_lock_init(&musb->lock);
Felipe Balbi550a7372008-07-24 12:27:36 +03001977 musb->board_set_power = plat->set_power;
Felipe Balbi550a7372008-07-24 12:27:36 +03001978 musb->min_power = plat->min_power;
Felipe Balbif7ec9432010-12-02 09:48:58 +02001979 musb->ops = plat->platform_ops;
Daniel Mack9ad96e62013-04-10 21:55:48 +02001980 musb->port_mode = plat->mode;
Felipe Balbi550a7372008-07-24 12:27:36 +03001981
Tony Lindgren1b40fc52014-11-24 11:05:02 -08001982 /*
1983 * Initialize the default IO functions. At least omap2430 needs
1984 * these early. We initialize the platform specific IO functions
1985 * later on.
1986 */
1987 musb_readb = musb_default_readb;
1988 musb_writeb = musb_default_writeb;
1989 musb_readw = musb_default_readw;
1990 musb_writew = musb_default_writew;
1991 musb_readl = musb_default_readl;
1992 musb_writel = musb_default_writel;
1993
Felipe Balbi3e43a072015-02-02 17:12:00 -06001994 /* We need musb_read/write functions initialized for PM */
1995 pm_runtime_use_autosuspend(musb->controller);
1996 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1997 pm_runtime_irq_safe(musb->controller);
1998 pm_runtime_enable(musb->controller);
1999
David Brownell84e250f2009-03-31 12:30:04 -07002000 /* The musb_platform_init() call:
Philippe De Swertbaef6532012-11-06 15:32:13 +02002001 * - adjusts musb->mregs
2002 * - sets the musb->isr
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302003 * - may initialize an integrated transceiver
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05302004 * - initializes musb->xceiv, usually by otg_get_phy()
David Brownell84e250f2009-03-31 12:30:04 -07002005 * - stops powering VBUS
David Brownell84e250f2009-03-31 12:30:04 -07002006 *
Joe Perches7c9d4402011-06-23 11:39:20 -07002007 * There are various transceiver configurations. Blackfin,
David Brownell84e250f2009-03-31 12:30:04 -07002008 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
2009 * external/discrete ones in various flavors (twl4030 family,
2010 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03002011 */
Hema Kalliguddiea65df52010-09-22 19:27:40 -05002012 status = musb_platform_init(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002013 if (status < 0)
Felipe Balbi03491762010-12-02 09:57:08 +02002014 goto fail1;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002015
Felipe Balbi550a7372008-07-24 12:27:36 +03002016 if (!musb->isr) {
2017 status = -ENODEV;
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02002018 goto fail2;
Felipe Balbi550a7372008-07-24 12:27:36 +03002019 }
2020
Tony Lindgren1b40fc52014-11-24 11:05:02 -08002021 if (musb->ops->quirks)
2022 musb->io.quirks = musb->ops->quirks;
2023
Ben Hutchingsda96cfc2015-05-24 04:27:32 +01002024 /* Most devices use indexed offset or flat offset */
Tony Lindgrend026e9c2014-11-24 11:05:03 -08002025 if (musb->io.quirks & MUSB_INDEXED_EP) {
2026 musb->io.ep_offset = musb_indexed_ep_offset;
2027 musb->io.ep_select = musb_indexed_ep_select;
2028 } else {
2029 musb->io.ep_offset = musb_flat_ep_offset;
2030 musb->io.ep_select = musb_flat_ep_select;
2031 }
2032
Ben Hutchingsda96cfc2015-05-24 04:27:32 +01002033 /* At least tusb6010 has its own offsets */
2034 if (musb->ops->ep_offset)
2035 musb->io.ep_offset = musb->ops->ep_offset;
2036 if (musb->ops->ep_select)
2037 musb->io.ep_select = musb->ops->ep_select;
2038
Tony Lindgren8a77f052014-11-24 11:05:04 -08002039 if (musb->ops->fifo_mode)
2040 fifo_mode = musb->ops->fifo_mode;
2041 else
2042 fifo_mode = 4;
2043
Tony Lindgren1b40fc52014-11-24 11:05:02 -08002044 if (musb->ops->fifo_offset)
2045 musb->io.fifo_offset = musb->ops->fifo_offset;
2046 else
2047 musb->io.fifo_offset = musb_default_fifo_offset;
2048
2049 if (musb->ops->readb)
2050 musb_readb = musb->ops->readb;
2051 if (musb->ops->writeb)
2052 musb_writeb = musb->ops->writeb;
2053 if (musb->ops->readw)
2054 musb_readw = musb->ops->readw;
2055 if (musb->ops->writew)
2056 musb_writew = musb->ops->writew;
2057 if (musb->ops->readl)
2058 musb_readl = musb->ops->readl;
2059 if (musb->ops->writel)
2060 musb_writel = musb->ops->writel;
2061
2062 if (musb->ops->read_fifo)
2063 musb->io.read_fifo = musb->ops->read_fifo;
2064 else
2065 musb->io.read_fifo = musb_default_read_fifo;
2066
2067 if (musb->ops->write_fifo)
2068 musb->io.write_fifo = musb->ops->write_fifo;
2069 else
2070 musb->io.write_fifo = musb_default_write_fifo;
2071
Heikki Krogerusffb865b2010-03-25 13:25:28 +02002072 if (!musb->xceiv->io_ops) {
Grazvydas Ignotasbf070bc2012-03-21 16:35:52 +02002073 musb->xceiv->io_dev = musb->controller;
Heikki Krogerusffb865b2010-03-25 13:25:28 +02002074 musb->xceiv->io_priv = musb->mregs;
2075 musb->xceiv->io_ops = &musb_ulpi_access;
2076 }
2077
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02002078 pm_runtime_get_sync(musb->controller);
2079
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +02002080 if (use_dma && dev->dma_mask) {
Sebastian Andrzej Siewior66c01882013-06-19 17:38:11 +02002081 musb->dma_controller = dma_controller_create(musb, musb->mregs);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +02002082 if (IS_ERR(musb->dma_controller)) {
2083 status = PTR_ERR(musb->dma_controller);
2084 goto fail2_5;
2085 }
2086 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002087
2088 /* be sure interrupts are disabled before connecting ISR */
2089 musb_platform_disable(musb);
2090 musb_generic_disable(musb);
2091
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01002092 /* Init IRQ workqueue before request_irq */
2093 INIT_WORK(&musb->irq_work, musb_irq_work);
Daniel Mack8ed1fb72013-12-18 20:23:46 +01002094 INIT_DELAYED_WORK(&musb->deassert_reset_work, musb_deassert_reset);
2095 INIT_DELAYED_WORK(&musb->finish_resume_work, musb_host_finish_resume);
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01002096
Felipe Balbi550a7372008-07-24 12:27:36 +03002097 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03002098 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03002099 ? MUSB_CONTROLLER_MHDRC
2100 : MUSB_CONTROLLER_HDRC, musb);
2101 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002102 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002103
David Brownellf7f9d632009-03-31 12:32:12 -07002104 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
David Brownellf7f9d632009-03-31 12:32:12 -07002105
Felipe Balbi550a7372008-07-24 12:27:36 +03002106 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002107 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002108 dev_err(dev, "request_irq %d failed!\n", nIrq);
2109 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002110 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002111 }
2112 musb->nIrq = nIrq;
Felipe Balbi032ec492011-11-24 15:46:26 +02002113 /* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002114 if (enable_irq_wake(nIrq) == 0) {
2115 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002116 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002117 } else {
2118 musb->irq_wake = 0;
2119 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002120
Felipe Balbi032ec492011-11-24 15:46:26 +02002121 /* program PHY to use external vBus if required */
2122 if (plat->extvbus) {
2123 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
2124 busctl |= MUSB_ULPI_USE_EXTVBUS;
2125 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002126 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002127
Grazvydas Ignotase5615112013-03-10 02:48:55 +02002128 if (musb->xceiv->otg->default_a) {
2129 MUSB_HST_MODE(musb);
Antoine Tenarte47d9252014-10-30 18:41:13 +01002130 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
Grazvydas Ignotase5615112013-03-10 02:48:55 +02002131 } else {
2132 MUSB_DEV_MODE(musb);
Antoine Tenarte47d9252014-10-30 18:41:13 +01002133 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Grazvydas Ignotase5615112013-03-10 02:48:55 +02002134 }
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302135
Daniel Mack6c5f6a62013-04-10 21:55:49 +02002136 switch (musb->port_mode) {
2137 case MUSB_PORT_MODE_HOST:
2138 status = musb_host_setup(musb, plat->power);
Felipe Balbi2df67612013-10-29 12:17:17 -05002139 if (status < 0)
2140 goto fail3;
2141 status = musb_platform_set_mode(musb, MUSB_HOST);
Daniel Mack6c5f6a62013-04-10 21:55:49 +02002142 break;
2143 case MUSB_PORT_MODE_GADGET:
2144 status = musb_gadget_setup(musb);
Felipe Balbi2df67612013-10-29 12:17:17 -05002145 if (status < 0)
2146 goto fail3;
2147 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
Daniel Mack6c5f6a62013-04-10 21:55:49 +02002148 break;
2149 case MUSB_PORT_MODE_DUAL_ROLE:
2150 status = musb_host_setup(musb, plat->power);
2151 if (status < 0)
2152 goto fail3;
2153 status = musb_gadget_setup(musb);
Felipe Balbi2df67612013-10-29 12:17:17 -05002154 if (status) {
Sebastian Andrzej Siewior0d2dd7e2013-10-16 12:50:06 +02002155 musb_host_cleanup(musb);
Felipe Balbi2df67612013-10-29 12:17:17 -05002156 goto fail3;
2157 }
2158 status = musb_platform_set_mode(musb, MUSB_OTG);
Daniel Mack6c5f6a62013-04-10 21:55:49 +02002159 break;
2160 default:
2161 dev_err(dev, "unsupported port mode %d\n", musb->port_mode);
2162 break;
2163 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002164
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002165 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002166 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002167
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002168 status = musb_init_debugfs(musb);
2169 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002170 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002171
Felipe Balbi94375752009-12-15 11:08:38 +02002172 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002173 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002174 goto fail5;
Felipe Balbi28c2c512008-09-11 11:53:25 +03002175
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02002176 pm_runtime_put(musb->controller);
2177
Felipe Balbi28c2c512008-09-11 11:53:25 +03002178 return 0;
2179
Felipe Balbib0f9da72010-03-25 13:25:18 +02002180fail5:
2181 musb_exit_debugfs(musb);
2182
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002183fail4:
Felipe Balbi032ec492011-11-24 15:46:26 +02002184 musb_gadget_cleanup(musb);
Sebastian Andrzej Siewior0d2dd7e2013-10-16 12:50:06 +02002185 musb_host_cleanup(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002186
2187fail3:
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01002188 cancel_work_sync(&musb->irq_work);
Daniel Mack8ed1fb72013-12-18 20:23:46 +01002189 cancel_delayed_work_sync(&musb->finish_resume_work);
2190 cancel_delayed_work_sync(&musb->deassert_reset_work);
Sebastian Andrzej Siewiorf3ce4d52013-06-19 17:38:14 +02002191 if (musb->dma_controller)
2192 dma_controller_destroy(musb->dma_controller);
Sebastian Andrzej Siewior48054142013-10-16 12:50:08 +02002193fail2_5:
Grazvydas Ignotasc04352a2012-02-04 19:43:51 +02002194 pm_runtime_put_sync(musb->controller);
2195
2196fail2:
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002197 if (musb->irq_wake)
2198 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002199 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002200
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002201fail1:
Ming Lei681d1e82013-01-04 23:13:06 +08002202 pm_runtime_disable(musb->controller);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002203 dev_err(musb->controller,
2204 "musb_init_controller failed with status %d\n", status);
2205
Felipe Balbi28c2c512008-09-11 11:53:25 +03002206 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002207
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002208fail0:
2209
Felipe Balbi550a7372008-07-24 12:27:36 +03002210 return status;
2211
Felipe Balbi550a7372008-07-24 12:27:36 +03002212}
2213
2214/*-------------------------------------------------------------------------*/
2215
2216/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2217 * bridge to a platform device; this driver then suffices.
2218 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002219static int musb_probe(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002220{
2221 struct device *dev = &pdev->dev;
Hema Kalliguddifcf173e2010-09-29 11:26:39 -05002222 int irq = platform_get_irq_byname(pdev, "mc");
Felipe Balbi550a7372008-07-24 12:27:36 +03002223 struct resource *iomem;
2224 void __iomem *base;
2225
Varka Bhadram1f79b262014-10-29 21:30:19 +05302226 if (irq <= 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03002227 return -ENODEV;
2228
Varka Bhadram1f79b262014-10-29 21:30:19 +05302229 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Felipe Balbib42f7f32013-02-04 19:04:45 +02002230 base = devm_ioremap_resource(dev, iomem);
2231 if (IS_ERR(base))
2232 return PTR_ERR(base);
Felipe Balbi550a7372008-07-24 12:27:36 +03002233
Felipe Balbib42f7f32013-02-04 19:04:45 +02002234 return musb_init_controller(dev, irq, base);
Felipe Balbi550a7372008-07-24 12:27:36 +03002235}
2236
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002237static int musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002238{
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +00002239 struct device *dev = &pdev->dev;
2240 struct musb *musb = dev_to_musb(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002241
2242 /* this gets called on rmmod.
2243 * - Host mode: host may still be active
2244 * - Peripheral mode: peripheral is deactivated (or never-activated)
2245 * - OTG mode: both roles are deactivated (or never-activated)
2246 */
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002247 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002248 musb_shutdown(pdev);
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002249
Sebastian Andrzej Siewior8d1aad72013-10-16 12:50:07 +02002250 if (musb->dma_controller)
2251 dma_controller_destroy(musb->dma_controller);
2252
Sebastian Andrzej Siewior66fadea2013-11-06 09:25:27 +01002253 cancel_work_sync(&musb->irq_work);
Daniel Mack8ed1fb72013-12-18 20:23:46 +01002254 cancel_delayed_work_sync(&musb->finish_resume_work);
2255 cancel_delayed_work_sync(&musb->deassert_reset_work);
Felipe Balbi550a7372008-07-24 12:27:36 +03002256 musb_free(musb);
Ajay Kumar Gupta8d2421e2012-08-31 11:09:50 +00002257 device_init_wakeup(dev, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002258 return 0;
2259}
2260
2261#ifdef CONFIG_PM
2262
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002263static void musb_save_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002264{
2265 int i;
2266 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002267 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002268
Felipe Balbi032ec492011-11-24 15:46:26 +02002269 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2270 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2271 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Felipe Balbi74211072010-12-01 13:53:27 +02002272 musb->context.power = musb_readb(musb_base, MUSB_POWER);
Felipe Balbi74211072010-12-01 13:53:27 +02002273 musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2274 musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2275 musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002276
Bob Liuae9b2ad2010-09-24 13:44:07 +03002277 for (i = 0; i < musb->config->num_eps; ++i) {
Felipe Balbie4e5b1362011-06-27 15:57:46 +03002278 struct musb_hw_ep *hw_ep;
2279
2280 hw_ep = &musb->endpoints[i];
2281 if (!hw_ep)
2282 continue;
2283
2284 epio = hw_ep->regs;
2285 if (!epio)
2286 continue;
2287
Vikram Panditaea737552011-09-07 09:19:23 -07002288 musb_writeb(musb_base, MUSB_INDEX, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002289 musb->context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002290 musb_readw(epio, MUSB_TXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002291 musb->context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002292 musb_readw(epio, MUSB_TXCSR);
Felipe Balbi74211072010-12-01 13:53:27 +02002293 musb->context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002294 musb_readw(epio, MUSB_RXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002295 musb->context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002296 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002297
2298 if (musb->dyn_fifo) {
Felipe Balbi74211072010-12-01 13:53:27 +02002299 musb->context.index_regs[i].txfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002300 musb_read_txfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002301 musb->context.index_regs[i].rxfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002302 musb_read_rxfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002303 musb->context.index_regs[i].txfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002304 musb_read_txfifosz(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002305 musb->context.index_regs[i].rxfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002306 musb_read_rxfifosz(musb_base);
2307 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002308
Felipe Balbi032ec492011-11-24 15:46:26 +02002309 musb->context.index_regs[i].txtype =
2310 musb_readb(epio, MUSB_TXTYPE);
2311 musb->context.index_regs[i].txinterval =
2312 musb_readb(epio, MUSB_TXINTERVAL);
2313 musb->context.index_regs[i].rxtype =
2314 musb_readb(epio, MUSB_RXTYPE);
2315 musb->context.index_regs[i].rxinterval =
2316 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002317
Felipe Balbi032ec492011-11-24 15:46:26 +02002318 musb->context.index_regs[i].txfunaddr =
2319 musb_read_txfunaddr(musb_base, i);
2320 musb->context.index_regs[i].txhubaddr =
2321 musb_read_txhubaddr(musb_base, i);
2322 musb->context.index_regs[i].txhubport =
2323 musb_read_txhubport(musb_base, i);
2324
2325 musb->context.index_regs[i].rxfunaddr =
2326 musb_read_rxfunaddr(musb_base, i);
2327 musb->context.index_regs[i].rxhubaddr =
2328 musb_read_rxhubaddr(musb_base, i);
2329 musb->context.index_regs[i].rxhubport =
2330 musb_read_rxhubport(musb_base, i);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002331 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002332}
2333
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002334static void musb_restore_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002335{
2336 int i;
2337 void __iomem *musb_base = musb->mregs;
2338 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002339 void __iomem *epio;
Roger Quadros33f8d752014-02-04 15:29:33 +02002340 u8 power;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002341
Felipe Balbi032ec492011-11-24 15:46:26 +02002342 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2343 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2344 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
Roger Quadros33f8d752014-02-04 15:29:33 +02002345
2346 /* Don't affect SUSPENDM/RESUME bits in POWER reg */
2347 power = musb_readb(musb_base, MUSB_POWER);
2348 power &= MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME;
2349 musb->context.power &= ~(MUSB_POWER_SUSPENDM | MUSB_POWER_RESUME);
2350 power |= musb->context.power;
2351 musb_writeb(musb_base, MUSB_POWER, power);
2352
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01002353 musb_writew(musb_base, MUSB_INTRTXE, musb->intrtxe);
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01002354 musb_writew(musb_base, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi74211072010-12-01 13:53:27 +02002355 musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2356 musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002357
Bob Liuae9b2ad2010-09-24 13:44:07 +03002358 for (i = 0; i < musb->config->num_eps; ++i) {
Felipe Balbie4e5b1362011-06-27 15:57:46 +03002359 struct musb_hw_ep *hw_ep;
2360
2361 hw_ep = &musb->endpoints[i];
2362 if (!hw_ep)
2363 continue;
2364
2365 epio = hw_ep->regs;
2366 if (!epio)
2367 continue;
2368
Vikram Panditaea737552011-09-07 09:19:23 -07002369 musb_writeb(musb_base, MUSB_INDEX, i);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002370 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002371 musb->context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002372 musb_writew(epio, MUSB_TXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002373 musb->context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002374 musb_writew(epio, MUSB_RXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002375 musb->context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002376 musb_writew(epio, MUSB_RXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002377 musb->context.index_regs[i].rxcsr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002378
2379 if (musb->dyn_fifo) {
2380 musb_write_txfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002381 musb->context.index_regs[i].txfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002382 musb_write_rxfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002383 musb->context.index_regs[i].rxfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002384 musb_write_txfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002385 musb->context.index_regs[i].txfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002386 musb_write_rxfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002387 musb->context.index_regs[i].rxfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002388 }
2389
Felipe Balbi032ec492011-11-24 15:46:26 +02002390 musb_writeb(epio, MUSB_TXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002391 musb->context.index_regs[i].txtype);
Felipe Balbi032ec492011-11-24 15:46:26 +02002392 musb_writeb(epio, MUSB_TXINTERVAL,
Felipe Balbi74211072010-12-01 13:53:27 +02002393 musb->context.index_regs[i].txinterval);
Felipe Balbi032ec492011-11-24 15:46:26 +02002394 musb_writeb(epio, MUSB_RXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002395 musb->context.index_regs[i].rxtype);
Felipe Balbi032ec492011-11-24 15:46:26 +02002396 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002397
Felipe Balbi032ec492011-11-24 15:46:26 +02002398 musb->context.index_regs[i].rxinterval);
2399 musb_write_txfunaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002400 musb->context.index_regs[i].txfunaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002401 musb_write_txhubaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002402 musb->context.index_regs[i].txhubaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002403 musb_write_txhubport(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002404 musb->context.index_regs[i].txhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002405
Felipe Balbi032ec492011-11-24 15:46:26 +02002406 ep_target_regs =
2407 musb_read_target_reg_base(i, musb_base);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002408
Felipe Balbi032ec492011-11-24 15:46:26 +02002409 musb_write_rxfunaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002410 musb->context.index_regs[i].rxfunaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002411 musb_write_rxhubaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002412 musb->context.index_regs[i].rxhubaddr);
Felipe Balbi032ec492011-11-24 15:46:26 +02002413 musb_write_rxhubport(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002414 musb->context.index_regs[i].rxhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002415 }
Ajay Kumar Gupta3c5fec72011-07-08 15:06:13 +05302416 musb_writeb(musb_base, MUSB_INDEX, musb->context.index);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002417}
2418
Magnus Damm48fea962009-07-08 13:22:56 +02002419static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002420{
Felipe Balbi82207962011-06-27 15:57:12 +03002421 struct musb *musb = dev_to_musb(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002422 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03002423
Felipe Balbi550a7372008-07-24 12:27:36 +03002424 spin_lock_irqsave(&musb->lock, flags);
2425
2426 if (is_peripheral_active(musb)) {
2427 /* FIXME force disconnect unless we know USB will wake
2428 * the system up quickly enough to respond ...
2429 */
2430 } else if (is_host_active(musb)) {
2431 /* we know all the children are suspended; sometimes
2432 * they will even be wakeup-enabled.
2433 */
2434 }
2435
Daniel Mackc3384122013-11-25 22:26:40 +01002436 musb_save_context(musb);
2437
Felipe Balbi550a7372008-07-24 12:27:36 +03002438 spin_unlock_irqrestore(&musb->lock, flags);
2439 return 0;
2440}
2441
Sebastian Andrzej Siewior3e87d9a2014-10-27 10:49:42 +01002442static int musb_resume(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002443{
Daniel Mackc3384122013-11-25 22:26:40 +01002444 struct musb *musb = dev_to_musb(dev);
Sebastian Andrzej Siewiorb87fd2f2014-10-27 19:06:18 +01002445 u8 devctl;
2446 u8 mask;
Daniel Mackc3384122013-11-25 22:26:40 +01002447
2448 /*
2449 * For static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002450 * unless for some reason the whole soc powered down or the USB
2451 * module got reset through the PSC (vs just being disabled).
Daniel Mackc3384122013-11-25 22:26:40 +01002452 *
2453 * For the DSPS glue layer though, a full register restore has to
2454 * be done. As it shouldn't harm other platforms, we do it
2455 * unconditionally.
Felipe Balbi550a7372008-07-24 12:27:36 +03002456 */
Daniel Mackc3384122013-11-25 22:26:40 +01002457
2458 musb_restore_context(musb);
2459
Sebastian Andrzej Siewiorb87fd2f2014-10-27 19:06:18 +01002460 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2461 mask = MUSB_DEVCTL_BDEVICE | MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV;
2462 if ((devctl & mask) != (musb->context.devctl & mask))
2463 musb->port1_status = 0;
Sebastian Andrzej Siewiorbaadd522014-10-27 19:06:19 +01002464 if (musb->need_finish_resume) {
2465 musb->need_finish_resume = 0;
2466 schedule_delayed_work(&musb->finish_resume_work,
Felipe Balbi309be232015-02-13 14:46:27 -06002467 msecs_to_jiffies(USB_RESUME_TIMEOUT));
Sebastian Andrzej Siewiorbaadd522014-10-27 19:06:19 +01002468 }
Sebastian Andrzej Siewiora1fc1922014-11-13 18:33:08 +01002469
2470 /*
2471 * The USB HUB code expects the device to be in RPM_ACTIVE once it came
2472 * out of suspend
2473 */
2474 pm_runtime_disable(dev);
2475 pm_runtime_set_active(dev);
2476 pm_runtime_enable(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002477 return 0;
2478}
2479
Hema HK7acc6192011-02-28 14:19:34 +05302480static int musb_runtime_suspend(struct device *dev)
2481{
2482 struct musb *musb = dev_to_musb(dev);
2483
2484 musb_save_context(musb);
2485
2486 return 0;
2487}
2488
2489static int musb_runtime_resume(struct device *dev)
2490{
2491 struct musb *musb = dev_to_musb(dev);
2492 static int first = 1;
2493
2494 /*
2495 * When pm_runtime_get_sync called for the first time in driver
2496 * init, some of the structure is still not initialized which is
2497 * used in restore function. But clock needs to be
2498 * enabled before any register access, so
2499 * pm_runtime_get_sync has to be called.
2500 * Also context restore without save does not make
2501 * any sense
2502 */
2503 if (!first)
2504 musb_restore_context(musb);
2505 first = 0;
2506
Bin Liu9298b4a2015-02-03 11:02:10 -06002507 if (musb->need_finish_resume) {
2508 musb->need_finish_resume = 0;
2509 schedule_delayed_work(&musb->finish_resume_work,
Felipe Balbi309be232015-02-13 14:46:27 -06002510 msecs_to_jiffies(USB_RESUME_TIMEOUT));
Bin Liu9298b4a2015-02-03 11:02:10 -06002511 }
2512
Hema HK7acc6192011-02-28 14:19:34 +05302513 return 0;
2514}
2515
Alexey Dobriyan47145212009-12-14 18:00:08 -08002516static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002517 .suspend = musb_suspend,
Sebastian Andrzej Siewior3e87d9a2014-10-27 10:49:42 +01002518 .resume = musb_resume,
Hema HK7acc6192011-02-28 14:19:34 +05302519 .runtime_suspend = musb_runtime_suspend,
2520 .runtime_resume = musb_runtime_resume,
Magnus Damm48fea962009-07-08 13:22:56 +02002521};
2522
2523#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002524#else
Magnus Damm48fea962009-07-08 13:22:56 +02002525#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002526#endif
2527
2528static struct platform_driver musb_driver = {
2529 .driver = {
2530 .name = (char *)musb_driver_name,
2531 .bus = &platform_bus_type,
Magnus Damm48fea962009-07-08 13:22:56 +02002532 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002533 },
Felipe Balbie9e8c852012-01-26 12:40:23 +02002534 .probe = musb_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05002535 .remove = musb_remove,
Felipe Balbi550a7372008-07-24 12:27:36 +03002536 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002537};
2538
Ezequiel Garcia89f836a2013-12-26 09:24:52 -03002539module_platform_driver(musb_driver);