blob: 6e3988cb6e8a7e48424fa8aff376357bdc8dec4b [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
86 * - platform_data is mostly for board-specific informarion
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>
96#include <linux/init.h>
97#include <linux/list.h>
98#include <linux/kobject.h>
99#include <linux/platform_device.h>
100#include <linux/io.h>
101
Felipe Balbi550a7372008-07-24 12:27:36 +0300102#include "musb_core.h"
103
David Brownellf7f9d632009-03-31 12:32:12 -0700104#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +0300105
106
Felipe Balbi550a7372008-07-24 12:27:36 +0300107#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
108#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
109
Felipe Balbie8164f62008-08-10 21:22:35 +0300110#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300111
112#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
113
Felipe Balbi05ac10d2010-12-02 08:49:26 +0200114#define MUSB_DRIVER_NAME "musb-hdrc"
Felipe Balbi550a7372008-07-24 12:27:36 +0300115const char musb_driver_name[] = MUSB_DRIVER_NAME;
116
117MODULE_DESCRIPTION(DRIVER_INFO);
118MODULE_AUTHOR(DRIVER_AUTHOR);
119MODULE_LICENSE("GPL");
120MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
121
122
123/*-------------------------------------------------------------------------*/
124
125static inline struct musb *dev_to_musb(struct device *dev)
126{
Felipe Balbi550a7372008-07-24 12:27:36 +0300127 return dev_get_drvdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300128}
129
130/*-------------------------------------------------------------------------*/
131
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200132#ifndef CONFIG_BLACKFIN
133static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
134{
135 void __iomem *addr = otg->io_priv;
136 int i = 0;
137 u8 r;
138 u8 power;
139
140 /* Make sure the transceiver is not in low power mode */
141 power = musb_readb(addr, MUSB_POWER);
142 power &= ~MUSB_POWER_SUSPENDM;
143 musb_writeb(addr, MUSB_POWER, power);
144
145 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
146 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
147 */
148
149 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
150 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
151 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
152
153 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
154 & MUSB_ULPI_REG_CMPLT)) {
155 i++;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300156 if (i == 10000)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200157 return -ETIMEDOUT;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200158
159 }
160 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
161 r &= ~MUSB_ULPI_REG_CMPLT;
162 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
163
164 return musb_readb(addr, MUSB_ULPI_REG_DATA);
165}
166
167static int musb_ulpi_write(struct otg_transceiver *otg,
168 u32 offset, u32 data)
169{
170 void __iomem *addr = otg->io_priv;
171 int i = 0;
172 u8 r = 0;
173 u8 power;
174
175 /* Make sure the transceiver is not in low power mode */
176 power = musb_readb(addr, MUSB_POWER);
177 power &= ~MUSB_POWER_SUSPENDM;
178 musb_writeb(addr, MUSB_POWER, power);
179
180 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
181 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
182 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
183
184 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
185 & MUSB_ULPI_REG_CMPLT)) {
186 i++;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300187 if (i == 10000)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200188 return -ETIMEDOUT;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200189 }
190
191 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
192 r &= ~MUSB_ULPI_REG_CMPLT;
193 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
194
195 return 0;
196}
197#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530198#define musb_ulpi_read NULL
199#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200200#endif
201
202static struct otg_io_access_ops musb_ulpi_access = {
203 .read = musb_ulpi_read,
204 .write = musb_ulpi_write,
205};
206
207/*-------------------------------------------------------------------------*/
208
Felipe Balbi7c925542010-12-01 14:23:48 +0200209#if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200210
Felipe Balbi550a7372008-07-24 12:27:36 +0300211/*
212 * Load an endpoint's FIFO
213 */
214void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
215{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300216 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300217 void __iomem *fifo = hw_ep->fifo;
218
219 prefetch((u8 *)src);
220
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300221 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300222 'T', hw_ep->epnum, fifo, len, src);
223
224 /* we can't assume unaligned reads work */
225 if (likely((0x01 & (unsigned long) src) == 0)) {
226 u16 index = 0;
227
228 /* best case is 32bit-aligned source address */
229 if ((0x02 & (unsigned long) src) == 0) {
230 if (len >= 4) {
231 writesl(fifo, src + index, len >> 2);
232 index += len & ~0x03;
233 }
234 if (len & 0x02) {
235 musb_writew(fifo, 0, *(u16 *)&src[index]);
236 index += 2;
237 }
238 } else {
239 if (len >= 2) {
240 writesw(fifo, src + index, len >> 1);
241 index += len & ~0x01;
242 }
243 }
244 if (len & 0x01)
245 musb_writeb(fifo, 0, src[index]);
246 } else {
247 /* byte aligned */
248 writesb(fifo, src, len);
249 }
250}
251
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300252#if !defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +0300253/*
254 * Unload an endpoint's FIFO
255 */
256void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
257{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300258 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300259 void __iomem *fifo = hw_ep->fifo;
260
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300261 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300262 'R', hw_ep->epnum, fifo, len, dst);
263
264 /* we can't assume unaligned writes work */
265 if (likely((0x01 & (unsigned long) dst) == 0)) {
266 u16 index = 0;
267
268 /* best case is 32bit-aligned destination address */
269 if ((0x02 & (unsigned long) dst) == 0) {
270 if (len >= 4) {
271 readsl(fifo, dst, len >> 2);
272 index = len & ~0x03;
273 }
274 if (len & 0x02) {
275 *(u16 *)&dst[index] = musb_readw(fifo, 0);
276 index += 2;
277 }
278 } else {
279 if (len >= 2) {
280 readsw(fifo, dst, len >> 1);
281 index = len & ~0x01;
282 }
283 }
284 if (len & 0x01)
285 dst[index] = musb_readb(fifo, 0);
286 } else {
287 /* byte aligned */
288 readsb(fifo, dst, len);
289 }
290}
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300291#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300292
293#endif /* normal PIO */
294
295
296/*-------------------------------------------------------------------------*/
297
298/* for high speed test mode; see USB 2.0 spec 7.1.20 */
299static const u8 musb_test_packet[53] = {
300 /* implicit SYNC then DATA0 to start */
301
302 /* JKJKJKJK x9 */
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
304 /* JJKKJJKK x8 */
305 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
306 /* JJJJKKKK x8 */
307 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
308 /* JJJJJJJKKKKKKK x8 */
309 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
310 /* JJJJJJJK x8 */
311 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
312 /* JKKKKKKK x10, JK */
313 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
314
315 /* implicit CRC16 then EOP to end */
316};
317
318void musb_load_testpacket(struct musb *musb)
319{
320 void __iomem *regs = musb->endpoints[0].regs;
321
322 musb_ep_select(musb->mregs, 0);
323 musb_write_fifo(musb->control_ep,
324 sizeof(musb_test_packet), musb_test_packet);
325 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
326}
327
328/*-------------------------------------------------------------------------*/
329
Felipe Balbi550a7372008-07-24 12:27:36 +0300330#ifdef CONFIG_USB_MUSB_OTG
331
332/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300333 * Handles OTG hnp timeouts, such as b_ase0_brst
334 */
335void musb_otg_timer_func(unsigned long data)
336{
337 struct musb *musb = (struct musb *)data;
338 unsigned long flags;
339
340 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700341 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300342 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300343 dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300344 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700345 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300346 musb->is_active = 0;
347 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700348 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300349 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300350 dev_dbg(musb->controller, "HNP: %s timeout\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200351 otg_state_string(musb->xceiv->state));
Felipe Balbi743411b2010-12-01 13:22:05 +0200352 musb_platform_set_vbus(musb, 0);
David Brownellab983f2a2009-03-31 12:35:09 -0700353 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300354 break;
355 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300356 dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200357 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300358 }
359 musb->ignore_disconnect = 0;
360 spin_unlock_irqrestore(&musb->lock, flags);
361}
362
Felipe Balbi550a7372008-07-24 12:27:36 +0300363/*
David Brownellf7f9d632009-03-31 12:32:12 -0700364 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300365 */
366void musb_hnp_stop(struct musb *musb)
367{
368 struct usb_hcd *hcd = musb_to_hcd(musb);
369 void __iomem *mbase = musb->mregs;
370 u8 reg;
371
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300372 dev_dbg(musb->controller, "HNP: stop from %s\n", otg_state_string(musb->xceiv->state));
David Brownellab983f2a2009-03-31 12:35:09 -0700373
David Brownell84e250f2009-03-31 12:30:04 -0700374 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300375 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300376 musb_g_disconnect(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300377 dev_dbg(musb->controller, "HNP: back to %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200378 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300379 break;
380 case OTG_STATE_B_HOST:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300381 dev_dbg(musb->controller, "HNP: Disabling HR\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300382 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700383 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300384 MUSB_DEV_MODE(musb);
385 reg = musb_readb(mbase, MUSB_POWER);
386 reg |= MUSB_POWER_SUSPENDM;
387 musb_writeb(mbase, MUSB_POWER, reg);
388 /* REVISIT: Start SESSION_REQUEST here? */
389 break;
390 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300391 dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200392 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300393 }
394
395 /*
396 * When returning to A state after HNP, avoid hub_port_rebounce(),
397 * which cause occasional OPT A "Did not receive reset after connect"
398 * errors.
399 */
Alan Stern749da5f2010-03-04 17:05:08 -0500400 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300401}
402
403#endif
404
405/*
406 * Interrupt Service Routine to record USB "global" interrupts.
407 * Since these do not happen often and signify things of
408 * paramount importance, it seems OK to check them individually;
409 * the order of the tests is specified in the manual
410 *
411 * @param musb instance pointer
412 * @param int_usb register contents
413 * @param devctl
414 * @param power
415 */
416
Felipe Balbi550a7372008-07-24 12:27:36 +0300417static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
418 u8 devctl, u8 power)
419{
420 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300421
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300422 dev_dbg(musb->controller, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
Felipe Balbi550a7372008-07-24 12:27:36 +0300423 int_usb);
424
425 /* in host mode, the peripheral may issue remote wakeup.
426 * in peripheral mode, the host may resume the link.
427 * spurious RESUME irqs happen too, paired with SUSPEND.
428 */
429 if (int_usb & MUSB_INTR_RESUME) {
430 handled = IRQ_HANDLED;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300431 dev_dbg(musb->controller, "RESUME (%s)\n", otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300432
433 if (devctl & MUSB_DEVCTL_HM) {
434#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbiaa471452010-03-12 10:27:24 +0200435 void __iomem *mbase = musb->mregs;
436
David Brownell84e250f2009-03-31 12:30:04 -0700437 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300438 case OTG_STATE_A_SUSPEND:
439 /* remote wakeup? later, GetPortStatus
440 * will stop RESUME signaling
441 */
442
443 if (power & MUSB_POWER_SUSPENDM) {
444 /* spurious */
445 musb->int_usb &= ~MUSB_INTR_SUSPEND;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300446 dev_dbg(musb->controller, "Spurious SUSPENDM\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300447 break;
448 }
449
450 power &= ~MUSB_POWER_SUSPENDM;
451 musb_writeb(mbase, MUSB_POWER,
452 power | MUSB_POWER_RESUME);
453
454 musb->port1_status |=
455 (USB_PORT_STAT_C_SUSPEND << 16)
456 | MUSB_PORT_STAT_RESUME;
457 musb->rh_timer = jiffies
458 + msecs_to_jiffies(20);
459
David Brownell84e250f2009-03-31 12:30:04 -0700460 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300461 musb->is_active = 1;
462 usb_hcd_resume_root_hub(musb_to_hcd(musb));
463 break;
464 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700465 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300466 musb->is_active = 1;
467 MUSB_DEV_MODE(musb);
468 break;
469 default:
470 WARNING("bogus %s RESUME (%s)\n",
471 "host",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200472 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300473 }
474#endif
475 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700476 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300477#ifdef CONFIG_USB_MUSB_HDRC_HCD
478 case OTG_STATE_A_SUSPEND:
479 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700480 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300481 usb_hcd_resume_root_hub(musb_to_hcd(musb));
482 break;
483#endif
484#ifdef CONFIG_USB_GADGET_MUSB_HDRC
485 case OTG_STATE_B_WAIT_ACON:
486 case OTG_STATE_B_PERIPHERAL:
487 /* disconnect while suspended? we may
488 * not get a disconnect irq...
489 */
490 if ((devctl & MUSB_DEVCTL_VBUS)
491 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
492 ) {
493 musb->int_usb |= MUSB_INTR_DISCONNECT;
494 musb->int_usb &= ~MUSB_INTR_SUSPEND;
495 break;
496 }
497 musb_g_resume(musb);
498 break;
499 case OTG_STATE_B_IDLE:
500 musb->int_usb &= ~MUSB_INTR_SUSPEND;
501 break;
502#endif
503 default:
504 WARNING("bogus %s RESUME (%s)\n",
505 "peripheral",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200506 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300507 }
508 }
509 }
510
511#ifdef CONFIG_USB_MUSB_HDRC_HCD
512 /* see manual for the order of the tests */
513 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200514 void __iomem *mbase = musb->mregs;
515
Heikki Krogerus19aab562010-10-29 04:23:27 -0500516 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
517 && (devctl & MUSB_DEVCTL_BDEVICE)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300518 dev_dbg(musb->controller, "SessReq while on B state\n");
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300519 return IRQ_HANDLED;
520 }
521
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300522 dev_dbg(musb->controller, "SESSION_REQUEST (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200523 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300524
525 /* IRQ arrives from ID pin sense or (later, if VBUS power
526 * is removed) SRP. responses are time critical:
527 * - turn on VBUS (with silicon-specific mechanism)
528 * - go through A_WAIT_VRISE
529 * - ... to A_WAIT_BCON.
530 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
531 */
532 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
533 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700534 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300535 MUSB_HST_MODE(musb);
Felipe Balbi743411b2010-12-01 13:22:05 +0200536 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300537
538 handled = IRQ_HANDLED;
539 }
540
541 if (int_usb & MUSB_INTR_VBUSERROR) {
542 int ignore = 0;
543
544 /* During connection as an A-Device, we may see a short
545 * current spikes causing voltage drop, because of cable
546 * and peripheral capacitance combined with vbus draw.
547 * (So: less common with truly self-powered devices, where
548 * vbus doesn't act like a power supply.)
549 *
550 * Such spikes are short; usually less than ~500 usec, max
551 * of ~2 msec. That is, they're not sustained overcurrent
552 * errors, though they're reported using VBUSERROR irqs.
553 *
554 * Workarounds: (a) hardware: use self powered devices.
555 * (b) software: ignore non-repeated VBUS errors.
556 *
557 * REVISIT: do delays from lots of DEBUG_KERNEL checks
558 * make trouble here, keeping VBUS < 4.4V ?
559 */
David Brownell84e250f2009-03-31 12:30:04 -0700560 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300561 case OTG_STATE_A_HOST:
562 /* recovery is dicey once we've gotten past the
563 * initial stages of enumeration, but if VBUS
564 * stayed ok at the other end of the link, and
565 * another reset is due (at least for high speed,
566 * to redo the chirp etc), it might work OK...
567 */
568 case OTG_STATE_A_WAIT_BCON:
569 case OTG_STATE_A_WAIT_VRISE:
570 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200571 void __iomem *mbase = musb->mregs;
572
Felipe Balbi550a7372008-07-24 12:27:36 +0300573 musb->vbuserr_retry--;
574 ignore = 1;
575 devctl |= MUSB_DEVCTL_SESSION;
576 musb_writeb(mbase, MUSB_DEVCTL, devctl);
577 } else {
578 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500579 USB_PORT_STAT_OVERCURRENT
580 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300581 }
582 break;
583 default:
584 break;
585 }
586
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300587 dev_dbg(musb->controller, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200588 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300589 devctl,
590 ({ char *s;
591 switch (devctl & MUSB_DEVCTL_VBUS) {
592 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
593 s = "<SessEnd"; break;
594 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
595 s = "<AValid"; break;
596 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
597 s = "<VBusValid"; break;
598 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
599 default:
600 s = "VALID"; break;
601 }; s; }),
602 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
603 musb->port1_status);
604
605 /* go through A_WAIT_VFALL then start a new session */
606 if (!ignore)
Felipe Balbi743411b2010-12-01 13:22:05 +0200607 musb_platform_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300608 handled = IRQ_HANDLED;
609 }
610
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530611#endif
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200612 if (int_usb & MUSB_INTR_SUSPEND) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300613 dev_dbg(musb->controller, "SUSPEND (%s) devctl %02x power %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200614 otg_state_string(musb->xceiv->state), devctl, power);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200615 handled = IRQ_HANDLED;
616
617 switch (musb->xceiv->state) {
618#ifdef CONFIG_USB_MUSB_OTG
619 case OTG_STATE_A_PERIPHERAL:
620 /* We also come here if the cable is removed, since
621 * this silicon doesn't report ID-no-longer-grounded.
622 *
623 * We depend on T(a_wait_bcon) to shut us down, and
624 * hope users don't do anything dicey during this
625 * undesired detour through A_WAIT_BCON.
626 */
627 musb_hnp_stop(musb);
628 usb_hcd_resume_root_hub(musb_to_hcd(musb));
629 musb_root_disconnect(musb);
630 musb_platform_try_idle(musb, jiffies
631 + msecs_to_jiffies(musb->a_wait_bcon
632 ? : OTG_TIME_A_WAIT_BCON));
633
634 break;
635#endif
636 case OTG_STATE_B_IDLE:
637 if (!musb->is_active)
638 break;
639 case OTG_STATE_B_PERIPHERAL:
640 musb_g_suspend(musb);
641 musb->is_active = is_otg_enabled(musb)
642 && musb->xceiv->gadget->b_hnp_enable;
643 if (musb->is_active) {
644#ifdef CONFIG_USB_MUSB_OTG
645 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300646 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200647 mod_timer(&musb->otg_timer, jiffies
648 + msecs_to_jiffies(
649 OTG_TIME_B_ASE0_BRST));
650#endif
651 }
652 break;
653 case OTG_STATE_A_WAIT_BCON:
654 if (musb->a_wait_bcon != 0)
655 musb_platform_try_idle(musb, jiffies
656 + msecs_to_jiffies(musb->a_wait_bcon));
657 break;
658 case OTG_STATE_A_HOST:
659 musb->xceiv->state = OTG_STATE_A_SUSPEND;
660 musb->is_active = is_otg_enabled(musb)
661 && musb->xceiv->host->b_hnp_enable;
662 break;
663 case OTG_STATE_B_HOST:
664 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300665 dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200666 break;
667 default:
668 /* "should not happen" */
669 musb->is_active = 0;
670 break;
671 }
672 }
673
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530674#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +0300675 if (int_usb & MUSB_INTR_CONNECT) {
676 struct usb_hcd *hcd = musb_to_hcd(musb);
677
678 handled = IRQ_HANDLED;
679 musb->is_active = 1;
680 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
681
682 musb->ep0_stage = MUSB_EP0_START;
683
684#ifdef CONFIG_USB_MUSB_OTG
685 /* flush endpoints when transitioning from Device Mode */
686 if (is_peripheral_active(musb)) {
687 /* REVISIT HNP; just force disconnect */
688 }
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530689 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
690 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
691 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300692#endif
693 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
694 |USB_PORT_STAT_HIGH_SPEED
695 |USB_PORT_STAT_ENABLE
696 );
697 musb->port1_status |= USB_PORT_STAT_CONNECTION
698 |(USB_PORT_STAT_C_CONNECTION << 16);
699
700 /* high vs full speed is just a guess until after reset */
701 if (devctl & MUSB_DEVCTL_LSDEV)
702 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
703
Felipe Balbi550a7372008-07-24 12:27:36 +0300704 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700705 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300706 case OTG_STATE_B_PERIPHERAL:
707 if (int_usb & MUSB_INTR_SUSPEND) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300708 dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300709 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700710 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300711 } else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300712 dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300713 break;
714 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300715 dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
David Brownell1de00da2009-04-02 10:16:11 -0700716b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700717 musb->xceiv->state = OTG_STATE_B_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300718 hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700719 musb->ignore_disconnect = 0;
720 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300721 break;
722 default:
723 if ((devctl & MUSB_DEVCTL_VBUS)
724 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700725 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300726 hcd->self.is_b_host = 0;
727 }
728 break;
729 }
David Brownell1de00da2009-04-02 10:16:11 -0700730
731 /* poke the root hub */
732 MUSB_HST_MODE(musb);
733 if (hcd->status_urb)
734 usb_hcd_poll_rh_status(hcd);
735 else
736 usb_hcd_resume_root_hub(hcd);
737
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300738 dev_dbg(musb->controller, "CONNECT (%s) devctl %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200739 otg_state_string(musb->xceiv->state), devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300740 }
741#endif /* CONFIG_USB_MUSB_HDRC_HCD */
742
Felipe Balbi550a7372008-07-24 12:27:36 +0300743 if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300744 dev_dbg(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200745 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300746 MUSB_MODE(musb), devctl);
747 handled = IRQ_HANDLED;
748
David Brownell84e250f2009-03-31 12:30:04 -0700749 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300750#ifdef CONFIG_USB_MUSB_HDRC_HCD
751 case OTG_STATE_A_HOST:
752 case OTG_STATE_A_SUSPEND:
Anand Gadiyar5c23c902009-02-21 15:31:40 -0800753 usb_hcd_resume_root_hub(musb_to_hcd(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300754 musb_root_disconnect(musb);
Ajay Kumar Gupta74382172009-02-24 15:29:04 -0800755 if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +0300756 musb_platform_try_idle(musb, jiffies
757 + msecs_to_jiffies(musb->a_wait_bcon));
758 break;
759#endif /* HOST */
760#ifdef CONFIG_USB_MUSB_OTG
761 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700762 /* REVISIT this behaves for "real disconnect"
763 * cases; make sure the other transitions from
764 * from B_HOST act right too. The B_HOST code
765 * in hnp_stop() is currently not used...
766 */
767 musb_root_disconnect(musb);
768 musb_to_hcd(musb)->self.is_b_host = 0;
769 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
770 MUSB_DEV_MODE(musb);
771 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300772 break;
773 case OTG_STATE_A_PERIPHERAL:
774 musb_hnp_stop(musb);
775 musb_root_disconnect(musb);
776 /* FALLTHROUGH */
777 case OTG_STATE_B_WAIT_ACON:
778 /* FALLTHROUGH */
779#endif /* OTG */
780#ifdef CONFIG_USB_GADGET_MUSB_HDRC
781 case OTG_STATE_B_PERIPHERAL:
782 case OTG_STATE_B_IDLE:
783 musb_g_disconnect(musb);
784 break;
785#endif /* GADGET */
786 default:
787 WARNING("unhandled DISCONNECT transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200788 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300789 break;
790 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300791 }
792
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200793 /* mentor saves a bit: bus reset and babble share the same irq.
794 * only host sees babble; only peripheral sees bus reset.
795 */
796 if (int_usb & MUSB_INTR_RESET) {
797 handled = IRQ_HANDLED;
798 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
799 /*
800 * Looks like non-HS BABBLE can be ignored, but
801 * HS BABBLE is an error condition. For HS the solution
802 * is to avoid babble in the first place and fix what
803 * caused BABBLE. When HS BABBLE happens we can only
804 * stop the session.
805 */
806 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300807 dev_dbg(musb->controller, "BABBLE devctl: %02x\n", devctl);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200808 else {
809 ERR("Stopping host session -- babble\n");
810 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
811 }
812 } else if (is_peripheral_capable()) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300813 dev_dbg(musb->controller, "BUS RESET as %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200814 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200815 switch (musb->xceiv->state) {
816#ifdef CONFIG_USB_OTG
817 case OTG_STATE_A_SUSPEND:
818 /* We need to ignore disconnect on suspend
819 * otherwise tusb 2.0 won't reconnect after a
820 * power cycle, which breaks otg compliance.
821 */
822 musb->ignore_disconnect = 1;
823 musb_g_reset(musb);
824 /* FALLTHROUGH */
825 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
826 /* never use invalid T(a_wait_bcon) */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300827 dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200828 otg_state_string(musb->xceiv->state),
829 TA_WAIT_BCON(musb));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200830 mod_timer(&musb->otg_timer, jiffies
831 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
832 break;
833 case OTG_STATE_A_PERIPHERAL:
834 musb->ignore_disconnect = 0;
835 del_timer(&musb->otg_timer);
836 musb_g_reset(musb);
837 break;
838 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300839 dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200840 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200841 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
842 musb_g_reset(musb);
843 break;
844#endif
845 case OTG_STATE_B_IDLE:
846 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
847 /* FALLTHROUGH */
848 case OTG_STATE_B_PERIPHERAL:
849 musb_g_reset(musb);
850 break;
851 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300852 dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200853 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200854 }
855 }
856 }
857
858#if 0
859/* REVISIT ... this would be for multiplexing periodic endpoints, or
860 * supporting transfer phasing to prevent exceeding ISO bandwidth
861 * limits of a given frame or microframe.
862 *
863 * It's not needed for peripheral side, which dedicates endpoints;
864 * though it _might_ use SOF irqs for other purposes.
865 *
866 * And it's not currently needed for host side, which also dedicates
867 * endpoints, relies on TX/RX interval registers, and isn't claimed
868 * to support ISO transfers yet.
869 */
870 if (int_usb & MUSB_INTR_SOF) {
871 void __iomem *mbase = musb->mregs;
872 struct musb_hw_ep *ep;
873 u8 epnum;
874 u16 frame;
875
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300876 dev_dbg(musb->controller, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300877 handled = IRQ_HANDLED;
878
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200879 /* start any periodic Tx transfers waiting for current frame */
880 frame = musb_readw(mbase, MUSB_FRAME);
881 ep = musb->endpoints;
882 for (epnum = 1; (epnum < musb->nr_endpoints)
883 && (musb->epmask >= (1 << epnum));
884 epnum++, ep++) {
885 /*
886 * FIXME handle framecounter wraps (12 bits)
887 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300888 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200889 if (ep->dwWaitFrame >= frame) {
890 ep->dwWaitFrame = 0;
891 pr_debug("SOF --> periodic TX%s on %d\n",
892 ep->tx_channel ? " DMA" : "",
893 epnum);
894 if (!ep->tx_channel)
895 musb_h_tx_start(musb, epnum);
896 else
897 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300898 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200899 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300900 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200901#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300902
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200903 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300904
905 return handled;
906}
907
908/*-------------------------------------------------------------------------*/
909
910/*
911* Program the HDRC to start (enable interrupts, dma, etc.).
912*/
913void musb_start(struct musb *musb)
914{
915 void __iomem *regs = musb->mregs;
916 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
917
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300918 dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300919
920 /* Set INT enable registers, enable interrupts */
921 musb_writew(regs, MUSB_INTRTXE, musb->epmask);
922 musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
923 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
924
925 musb_writeb(regs, MUSB_TESTMODE, 0);
926
927 /* put into basic highspeed mode and start session */
928 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
929 | MUSB_POWER_SOFTCONN
930 | MUSB_POWER_HSENAB
931 /* ENSUSPEND wedges tusb */
932 /* | MUSB_POWER_ENSUSPEND */
933 );
934
935 musb->is_active = 0;
936 devctl = musb_readb(regs, MUSB_DEVCTL);
937 devctl &= ~MUSB_DEVCTL_SESSION;
938
939 if (is_otg_enabled(musb)) {
940 /* session started after:
941 * (a) ID-grounded irq, host mode;
942 * (b) vbus present/connect IRQ, peripheral mode;
943 * (c) peripheral initiates, using SRP
944 */
945 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
946 musb->is_active = 1;
947 else
948 devctl |= MUSB_DEVCTL_SESSION;
949
950 } else if (is_host_enabled(musb)) {
951 /* assume ID pin is hard-wired to ground */
952 devctl |= MUSB_DEVCTL_SESSION;
953
954 } else /* peripheral is enabled */ {
955 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
956 musb->is_active = 1;
957 }
958 musb_platform_enable(musb);
959 musb_writeb(regs, MUSB_DEVCTL, devctl);
960}
961
962
963static void musb_generic_disable(struct musb *musb)
964{
965 void __iomem *mbase = musb->mregs;
966 u16 temp;
967
968 /* disable interrupts */
969 musb_writeb(mbase, MUSB_INTRUSBE, 0);
970 musb_writew(mbase, MUSB_INTRTXE, 0);
971 musb_writew(mbase, MUSB_INTRRXE, 0);
972
973 /* off */
974 musb_writeb(mbase, MUSB_DEVCTL, 0);
975
976 /* flush pending interrupts */
977 temp = musb_readb(mbase, MUSB_INTRUSB);
978 temp = musb_readw(mbase, MUSB_INTRTX);
979 temp = musb_readw(mbase, MUSB_INTRRX);
980
981}
982
983/*
984 * Make the HDRC stop (disable interrupts, etc.);
985 * reversible by musb_start
986 * called on gadget driver unregister
987 * with controller locked, irqs blocked
988 * acts as a NOP unless some role activated the hardware
989 */
990void musb_stop(struct musb *musb)
991{
992 /* stop IRQs, timers, ... */
993 musb_platform_disable(musb);
994 musb_generic_disable(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300995 dev_dbg(musb->controller, "HDRC disabled\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300996
997 /* FIXME
998 * - mark host and/or peripheral drivers unusable/inactive
999 * - disable DMA (and enable it in HdrcStart)
1000 * - make sure we can musb_start() after musb_stop(); with
1001 * OTG mode, gadget driver module rmmod/modprobe cycles that
1002 * - ...
1003 */
1004 musb_platform_try_idle(musb, 0);
1005}
1006
1007static void musb_shutdown(struct platform_device *pdev)
1008{
1009 struct musb *musb = dev_to_musb(&pdev->dev);
1010 unsigned long flags;
1011
Hema HK4f9edd22011-03-22 16:02:12 +05301012 pm_runtime_get_sync(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001013 spin_lock_irqsave(&musb->lock, flags);
1014 musb_platform_disable(musb);
1015 musb_generic_disable(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001016 spin_unlock_irqrestore(&musb->lock, flags);
1017
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001018 if (!is_otg_enabled(musb) && is_host_enabled(musb))
1019 usb_remove_hcd(musb_to_hcd(musb));
1020 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1021 musb_platform_exit(musb);
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001022
Hema HK4f9edd22011-03-22 16:02:12 +05301023 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001024 /* FIXME power down */
1025}
1026
1027
1028/*-------------------------------------------------------------------------*/
1029
1030/*
1031 * The silicon either has hard-wired endpoint configurations, or else
1032 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001033 * writing only the dynamic sizing is very well tested. Since we switched
1034 * away from compile-time hardware parameters, we can no longer rely on
1035 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001036 *
1037 * We don't currently use dynamic fifo setup capability to do anything
1038 * more than selecting one of a bunch of predefined configurations.
1039 */
Felipe Balbi7c925542010-12-01 14:23:48 +02001040#if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
1041 || defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +03001042static ushort __initdata fifo_mode = 4;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001043#elif defined(CONFIG_USB_MUSB_UX500)
1044static ushort __initdata fifo_mode = 5;
Felipe Balbi550a7372008-07-24 12:27:36 +03001045#else
1046static ushort __initdata fifo_mode = 2;
1047#endif
1048
1049/* "modprobe ... fifo_mode=1" etc */
1050module_param(fifo_mode, ushort, 0);
1051MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1052
Felipe Balbi550a7372008-07-24 12:27:36 +03001053/*
1054 * tables defining fifo_mode values. define more if you like.
1055 * for host side, make sure both halves of ep1 are set up.
1056 */
1057
1058/* mode 0 - fits in 2KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001059static struct musb_fifo_cfg __initdata mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001060{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1061{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1062{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1063{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1064{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1065};
1066
1067/* mode 1 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001068static struct musb_fifo_cfg __initdata mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001069{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1070{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1071{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1072{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1073{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1074};
1075
1076/* mode 2 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001077static struct musb_fifo_cfg __initdata mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001078{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1079{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1080{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1081{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1082{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1083{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1084};
1085
1086/* mode 3 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001087static struct musb_fifo_cfg __initdata mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001088{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1089{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1090{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1091{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1092{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1093{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1094};
1095
1096/* mode 4 - fits in 16KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001097static struct musb_fifo_cfg __initdata mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001098{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1099{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1100{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1101{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1102{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1103{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1104{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1105{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1106{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1107{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1108{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1109{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1110{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1111{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1112{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1113{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1114{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1115{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001116{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1117{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1118{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1119{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1120{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1121{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1122{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001123{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1124{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1125};
1126
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001127/* mode 5 - fits in 8KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001128static struct musb_fifo_cfg __initdata mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001129{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1130{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1131{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1132{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1133{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1134{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1135{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1136{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1137{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1138{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1139{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1140{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1141{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1142{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1143{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1144{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1145{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1146{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1147{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1148{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1149{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1150{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1151{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1152{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1153{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1154{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1155{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1156};
Felipe Balbi550a7372008-07-24 12:27:36 +03001157
1158/*
1159 * configure a fifo; for non-shared endpoints, this may be called
1160 * once for a tx fifo and once for an rx fifo.
1161 *
1162 * returns negative errno or offset for next fifo.
1163 */
1164static int __init
1165fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001166 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001167{
1168 void __iomem *mbase = musb->mregs;
1169 int size = 0;
1170 u16 maxpacket = cfg->maxpacket;
1171 u16 c_off = offset >> 3;
1172 u8 c_size;
1173
1174 /* expect hw_ep has already been zero-initialized */
1175
1176 size = ffs(max(maxpacket, (u16) 8)) - 1;
1177 maxpacket = 1 << size;
1178
1179 c_size = size - 3;
1180 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001181 if ((offset + (maxpacket << 1)) >
1182 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001183 return -EMSGSIZE;
1184 c_size |= MUSB_FIFOSZ_DPB;
1185 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001186 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001187 return -EMSGSIZE;
1188 }
1189
1190 /* configure the FIFO */
1191 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1192
1193#ifdef CONFIG_USB_MUSB_HDRC_HCD
1194 /* EP0 reserved endpoint for control, bidirectional;
1195 * EP1 reserved for bulk, two unidirection halves.
1196 */
1197 if (hw_ep->epnum == 1)
1198 musb->bulk_ep = hw_ep;
1199 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1200#endif
1201 switch (cfg->style) {
1202 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001203 musb_write_txfifosz(mbase, c_size);
1204 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001205 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1206 hw_ep->max_packet_sz_tx = maxpacket;
1207 break;
1208 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001209 musb_write_rxfifosz(mbase, c_size);
1210 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001211 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1212 hw_ep->max_packet_sz_rx = maxpacket;
1213 break;
1214 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001215 musb_write_txfifosz(mbase, c_size);
1216 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001217 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1218 hw_ep->max_packet_sz_rx = maxpacket;
1219
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001220 musb_write_rxfifosz(mbase, c_size);
1221 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001222 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1223 hw_ep->max_packet_sz_tx = maxpacket;
1224
1225 hw_ep->is_shared_fifo = true;
1226 break;
1227 }
1228
1229 /* NOTE rx and tx endpoint irqs aren't managed separately,
1230 * which happens to be ok
1231 */
1232 musb->epmask |= (1 << hw_ep->epnum);
1233
1234 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1235}
1236
Felipe Balbie6c213b2010-03-12 10:29:06 +02001237static struct musb_fifo_cfg __initdata ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001238 .style = FIFO_RXTX, .maxpacket = 64,
1239};
1240
1241static int __init ep_config_from_table(struct musb *musb)
1242{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001243 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001244 unsigned i, n;
1245 int offset;
1246 struct musb_hw_ep *hw_ep = musb->endpoints;
1247
Felipe Balbie6c213b2010-03-12 10:29:06 +02001248 if (musb->config->fifo_cfg) {
1249 cfg = musb->config->fifo_cfg;
1250 n = musb->config->fifo_cfg_size;
1251 goto done;
1252 }
1253
Felipe Balbi550a7372008-07-24 12:27:36 +03001254 switch (fifo_mode) {
1255 default:
1256 fifo_mode = 0;
1257 /* FALLTHROUGH */
1258 case 0:
1259 cfg = mode_0_cfg;
1260 n = ARRAY_SIZE(mode_0_cfg);
1261 break;
1262 case 1:
1263 cfg = mode_1_cfg;
1264 n = ARRAY_SIZE(mode_1_cfg);
1265 break;
1266 case 2:
1267 cfg = mode_2_cfg;
1268 n = ARRAY_SIZE(mode_2_cfg);
1269 break;
1270 case 3:
1271 cfg = mode_3_cfg;
1272 n = ARRAY_SIZE(mode_3_cfg);
1273 break;
1274 case 4:
1275 cfg = mode_4_cfg;
1276 n = ARRAY_SIZE(mode_4_cfg);
1277 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001278 case 5:
1279 cfg = mode_5_cfg;
1280 n = ARRAY_SIZE(mode_5_cfg);
1281 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 }
1283
1284 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1285 musb_driver_name, fifo_mode);
1286
1287
Felipe Balbie6c213b2010-03-12 10:29:06 +02001288done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001289 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1290 /* assert(offset > 0) */
1291
1292 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001293 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001294 */
1295
1296 for (i = 0; i < n; i++) {
1297 u8 epn = cfg->hw_ep_num;
1298
Felipe Balbica6d1b12008-08-08 12:40:54 +03001299 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001300 pr_debug("%s: invalid ep %d\n",
1301 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001302 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001303 }
1304 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1305 if (offset < 0) {
1306 pr_debug("%s: mem overrun, ep %d\n",
1307 musb_driver_name, epn);
1308 return -EINVAL;
1309 }
1310 epn++;
1311 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1312 }
1313
1314 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1315 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001316 n + 1, musb->config->num_eps * 2 - 1,
1317 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001318
1319#ifdef CONFIG_USB_MUSB_HDRC_HCD
1320 if (!musb->bulk_ep) {
1321 pr_debug("%s: missing bulk\n", musb_driver_name);
1322 return -EINVAL;
1323 }
1324#endif
1325
1326 return 0;
1327}
1328
1329
1330/*
1331 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1332 * @param musb the controller
1333 */
1334static int __init ep_config_from_hw(struct musb *musb)
1335{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001336 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001337 struct musb_hw_ep *hw_ep;
1338 void *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001339 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001340
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001341 dev_dbg(musb->controller, "<== static silicon ep config\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001342
1343 /* FIXME pick up ep0 maxpacket size */
1344
Felipe Balbica6d1b12008-08-08 12:40:54 +03001345 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001346 musb_ep_select(mbase, epnum);
1347 hw_ep = musb->endpoints + epnum;
1348
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001349 ret = musb_read_fifosize(musb, hw_ep, epnum);
1350 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001351 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001352
1353 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1354
1355#ifdef CONFIG_USB_MUSB_HDRC_HCD
1356 /* pick an RX/TX endpoint for bulk */
1357 if (hw_ep->max_packet_sz_tx < 512
1358 || hw_ep->max_packet_sz_rx < 512)
1359 continue;
1360
1361 /* REVISIT: this algorithm is lazy, we should at least
1362 * try to pick a double buffered endpoint.
1363 */
1364 if (musb->bulk_ep)
1365 continue;
1366 musb->bulk_ep = hw_ep;
1367#endif
1368 }
1369
1370#ifdef CONFIG_USB_MUSB_HDRC_HCD
1371 if (!musb->bulk_ep) {
1372 pr_debug("%s: missing bulk\n", musb_driver_name);
1373 return -EINVAL;
1374 }
1375#endif
1376
1377 return 0;
1378}
1379
1380enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1381
1382/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1383 * configure endpoints, or take their config from silicon
1384 */
1385static int __init musb_core_init(u16 musb_type, struct musb *musb)
1386{
Felipe Balbi550a7372008-07-24 12:27:36 +03001387 u8 reg;
1388 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301389 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001390 void __iomem *mbase = musb->mregs;
1391 int status = 0;
1392 int i;
1393
1394 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001395 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001396
1397 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001398 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001399 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001400 musb->dyn_fifo = true;
1401 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001402 if (reg & MUSB_CONFIGDATA_MPRXE) {
1403 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001404 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001405 }
1406 if (reg & MUSB_CONFIGDATA_MPTXE) {
1407 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001408 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001409 }
1410 if (reg & MUSB_CONFIGDATA_HBRXE) {
1411 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001412 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001413 }
1414 if (reg & MUSB_CONFIGDATA_HBTXE) {
1415 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001416 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001417 }
1418 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1419 strcat(aInfo, ", SoftConn");
1420
1421 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1422 musb_driver_name, reg, aInfo);
1423
Felipe Balbi550a7372008-07-24 12:27:36 +03001424 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001425 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1426 musb->is_multipoint = 1;
1427 type = "M";
1428 } else {
1429 musb->is_multipoint = 0;
1430 type = "";
1431#ifdef CONFIG_USB_MUSB_HDRC_HCD
1432#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1433 printk(KERN_ERR
1434 "%s: kernel must blacklist external hubs\n",
1435 musb_driver_name);
1436#endif
1437#endif
1438 }
1439
1440 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301441 musb->hwvers = musb_read_hwvers(mbase);
1442 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1443 MUSB_HWVERS_MINOR(musb->hwvers),
1444 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001445 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1446 musb_driver_name, type, aRevision, aDate);
1447
1448 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001449 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001450
1451 /* discover endpoint configuration */
1452 musb->nr_endpoints = 1;
1453 musb->epmask = 1;
1454
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001455 if (musb->dyn_fifo)
1456 status = ep_config_from_table(musb);
1457 else
1458 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001459
1460 if (status < 0)
1461 return status;
1462
1463 /* finish init, and print endpoint config */
1464 for (i = 0; i < musb->nr_endpoints; i++) {
1465 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1466
1467 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
Felipe Balbi7c925542010-12-01 14:23:48 +02001468#ifdef CONFIG_USB_MUSB_TUSB6010
Felipe Balbi550a7372008-07-24 12:27:36 +03001469 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1470 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1471 hw_ep->fifo_sync_va =
1472 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1473
1474 if (i == 0)
1475 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1476 else
1477 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1478#endif
1479
1480 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1481#ifdef CONFIG_USB_MUSB_HDRC_HCD
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001482 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001483 hw_ep->rx_reinit = 1;
1484 hw_ep->tx_reinit = 1;
1485#endif
1486
1487 if (hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001488 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001489 "%s: hw_ep %d%s, %smax %d\n",
1490 musb_driver_name, i,
1491 hw_ep->is_shared_fifo ? "shared" : "tx",
1492 hw_ep->tx_double_buffered
1493 ? "doublebuffer, " : "",
1494 hw_ep->max_packet_sz_tx);
1495 }
1496 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001497 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001498 "%s: hw_ep %d%s, %smax %d\n",
1499 musb_driver_name, i,
1500 "rx",
1501 hw_ep->rx_double_buffered
1502 ? "doublebuffer, " : "",
1503 hw_ep->max_packet_sz_rx);
1504 }
1505 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001506 dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
Felipe Balbi550a7372008-07-24 12:27:36 +03001507 }
1508
1509 return 0;
1510}
1511
1512/*-------------------------------------------------------------------------*/
1513
Tony Lindgren59b479e2011-01-27 16:39:40 -08001514#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001515 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \
1516 defined(CONFIG_ARCH_U5500)
Felipe Balbi550a7372008-07-24 12:27:36 +03001517
1518static irqreturn_t generic_interrupt(int irq, void *__hci)
1519{
1520 unsigned long flags;
1521 irqreturn_t retval = IRQ_NONE;
1522 struct musb *musb = __hci;
1523
1524 spin_lock_irqsave(&musb->lock, flags);
1525
1526 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1527 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1528 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1529
1530 if (musb->int_usb || musb->int_tx || musb->int_rx)
1531 retval = musb_interrupt(musb);
1532
1533 spin_unlock_irqrestore(&musb->lock, flags);
1534
Sergei Shtylyova5073b52009-03-27 12:52:43 -07001535 return retval;
Felipe Balbi550a7372008-07-24 12:27:36 +03001536}
1537
1538#else
1539#define generic_interrupt NULL
1540#endif
1541
1542/*
1543 * handle all the irqs defined by the HDRC core. for now we expect: other
1544 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1545 * will be assigned, and the irq will already have been acked.
1546 *
1547 * called in irq context with spinlock held, irqs blocked
1548 */
1549irqreturn_t musb_interrupt(struct musb *musb)
1550{
1551 irqreturn_t retval = IRQ_NONE;
1552 u8 devctl, power;
1553 int ep_num;
1554 u32 reg;
1555
1556 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1557 power = musb_readb(musb->mregs, MUSB_POWER);
1558
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001559 dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001560 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1561 musb->int_usb, musb->int_tx, musb->int_rx);
1562
Felipe Balbicd42fef2009-12-15 13:47:30 +02001563#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1564 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
1565 if (!musb->gadget_driver) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001566 dev_dbg(musb->controller, "No gadget driver loaded\n");
Felipe Balbicd42fef2009-12-15 13:47:30 +02001567 return IRQ_HANDLED;
1568 }
1569#endif
1570
Felipe Balbi550a7372008-07-24 12:27:36 +03001571 /* the core can interrupt us for multiple reasons; docs have
1572 * a generic interrupt flowchart to follow
1573 */
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301574 if (musb->int_usb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001575 retval |= musb_stage0_irq(musb, musb->int_usb,
1576 devctl, power);
1577
1578 /* "stage 1" is handling endpoint irqs */
1579
1580 /* handle endpoint 0 first */
1581 if (musb->int_tx & 1) {
1582 if (devctl & MUSB_DEVCTL_HM)
1583 retval |= musb_h_ep0_irq(musb);
1584 else
1585 retval |= musb_g_ep0_irq(musb);
1586 }
1587
1588 /* RX on endpoints 1-15 */
1589 reg = musb->int_rx >> 1;
1590 ep_num = 1;
1591 while (reg) {
1592 if (reg & 1) {
1593 /* musb_ep_select(musb->mregs, ep_num); */
1594 /* REVISIT just retval = ep->rx_irq(...) */
1595 retval = IRQ_HANDLED;
1596 if (devctl & MUSB_DEVCTL_HM) {
1597 if (is_host_capable())
1598 musb_host_rx(musb, ep_num);
1599 } else {
1600 if (is_peripheral_capable())
1601 musb_g_rx(musb, ep_num);
1602 }
1603 }
1604
1605 reg >>= 1;
1606 ep_num++;
1607 }
1608
1609 /* TX on endpoints 1-15 */
1610 reg = musb->int_tx >> 1;
1611 ep_num = 1;
1612 while (reg) {
1613 if (reg & 1) {
1614 /* musb_ep_select(musb->mregs, ep_num); */
1615 /* REVISIT just retval |= ep->tx_irq(...) */
1616 retval = IRQ_HANDLED;
1617 if (devctl & MUSB_DEVCTL_HM) {
1618 if (is_host_capable())
1619 musb_host_tx(musb, ep_num);
1620 } else {
1621 if (is_peripheral_capable())
1622 musb_g_tx(musb, ep_num);
1623 }
1624 }
1625 reg >>= 1;
1626 ep_num++;
1627 }
1628
Felipe Balbi550a7372008-07-24 12:27:36 +03001629 return retval;
1630}
1631
1632
1633#ifndef CONFIG_MUSB_PIO_ONLY
1634static int __initdata use_dma = 1;
1635
1636/* "modprobe ... use_dma=0" etc */
1637module_param(use_dma, bool, 0);
1638MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1639
1640void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1641{
1642 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1643
1644 /* called with controller lock already held */
1645
1646 if (!epnum) {
1647#ifndef CONFIG_USB_TUSB_OMAP_DMA
1648 if (!is_cppi_enabled()) {
1649 /* endpoint 0 */
1650 if (devctl & MUSB_DEVCTL_HM)
1651 musb_h_ep0_irq(musb);
1652 else
1653 musb_g_ep0_irq(musb);
1654 }
1655#endif
1656 } else {
1657 /* endpoints 1..15 */
1658 if (transmit) {
1659 if (devctl & MUSB_DEVCTL_HM) {
1660 if (is_host_capable())
1661 musb_host_tx(musb, epnum);
1662 } else {
1663 if (is_peripheral_capable())
1664 musb_g_tx(musb, epnum);
1665 }
1666 } else {
1667 /* receive */
1668 if (devctl & MUSB_DEVCTL_HM) {
1669 if (is_host_capable())
1670 musb_host_rx(musb, epnum);
1671 } else {
1672 if (is_peripheral_capable())
1673 musb_g_rx(musb, epnum);
1674 }
1675 }
1676 }
1677}
1678
1679#else
1680#define use_dma 0
1681#endif
1682
1683/*-------------------------------------------------------------------------*/
1684
1685#ifdef CONFIG_SYSFS
1686
1687static ssize_t
1688musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1689{
1690 struct musb *musb = dev_to_musb(dev);
1691 unsigned long flags;
1692 int ret = -EINVAL;
1693
1694 spin_lock_irqsave(&musb->lock, flags);
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001695 ret = sprintf(buf, "%s\n", otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001696 spin_unlock_irqrestore(&musb->lock, flags);
1697
1698 return ret;
1699}
1700
1701static ssize_t
1702musb_mode_store(struct device *dev, struct device_attribute *attr,
1703 const char *buf, size_t n)
1704{
1705 struct musb *musb = dev_to_musb(dev);
1706 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001707 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001708
1709 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001710 if (sysfs_streq(buf, "host"))
1711 status = musb_platform_set_mode(musb, MUSB_HOST);
1712 else if (sysfs_streq(buf, "peripheral"))
1713 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1714 else if (sysfs_streq(buf, "otg"))
1715 status = musb_platform_set_mode(musb, MUSB_OTG);
1716 else
1717 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001718 spin_unlock_irqrestore(&musb->lock, flags);
1719
David Brownell96a274d2008-11-24 13:06:47 +02001720 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001721}
1722static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1723
1724static ssize_t
1725musb_vbus_store(struct device *dev, struct device_attribute *attr,
1726 const char *buf, size_t n)
1727{
1728 struct musb *musb = dev_to_musb(dev);
1729 unsigned long flags;
1730 unsigned long val;
1731
1732 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001733 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001734 return -EINVAL;
1735 }
1736
1737 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001738 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1739 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001740 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001741 musb->is_active = 0;
1742 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1743 spin_unlock_irqrestore(&musb->lock, flags);
1744
1745 return n;
1746}
1747
1748static ssize_t
1749musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1750{
1751 struct musb *musb = dev_to_musb(dev);
1752 unsigned long flags;
1753 unsigned long val;
1754 int vbus;
1755
1756 spin_lock_irqsave(&musb->lock, flags);
1757 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001758 /* FIXME get_vbus_status() is normally #defined as false...
1759 * and is effectively TUSB-specific.
1760 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001761 vbus = musb_platform_get_vbus_status(musb);
1762 spin_unlock_irqrestore(&musb->lock, flags);
1763
David Brownellf7f9d632009-03-31 12:32:12 -07001764 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001765 vbus ? "on" : "off", val);
1766}
1767static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1768
1769#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1770
1771/* Gadget drivers can't know that a host is connected so they might want
1772 * to start SRP, but users can. This allows userspace to trigger SRP.
1773 */
1774static ssize_t
1775musb_srp_store(struct device *dev, struct device_attribute *attr,
1776 const char *buf, size_t n)
1777{
1778 struct musb *musb = dev_to_musb(dev);
1779 unsigned short srp;
1780
1781 if (sscanf(buf, "%hu", &srp) != 1
1782 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001783 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001784 return -EINVAL;
1785 }
1786
1787 if (srp == 1)
1788 musb_g_wakeup(musb);
1789
1790 return n;
1791}
1792static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1793
1794#endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1795
Felipe Balbi94375752009-12-15 11:08:38 +02001796static struct attribute *musb_attributes[] = {
1797 &dev_attr_mode.attr,
1798 &dev_attr_vbus.attr,
1799#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1800 &dev_attr_srp.attr,
1801#endif
1802 NULL
1803};
1804
1805static const struct attribute_group musb_attr_group = {
1806 .attrs = musb_attributes,
1807};
1808
Felipe Balbi550a7372008-07-24 12:27:36 +03001809#endif /* sysfs */
1810
1811/* Only used to provide driver mode change events */
1812static void musb_irq_work(struct work_struct *data)
1813{
1814 struct musb *musb = container_of(data, struct musb, irq_work);
1815 static int old_state;
1816
David Brownell84e250f2009-03-31 12:30:04 -07001817 if (musb->xceiv->state != old_state) {
1818 old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001819 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1820 }
1821}
1822
1823/* --------------------------------------------------------------------------
1824 * Init support
1825 */
1826
1827static struct musb *__init
Felipe Balbica6d1b12008-08-08 12:40:54 +03001828allocate_instance(struct device *dev,
1829 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001830{
1831 struct musb *musb;
1832 struct musb_hw_ep *ep;
1833 int epnum;
1834#ifdef CONFIG_USB_MUSB_HDRC_HCD
1835 struct usb_hcd *hcd;
1836
Kay Sievers427c4f32008-11-07 01:52:53 +01001837 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
Felipe Balbi550a7372008-07-24 12:27:36 +03001838 if (!hcd)
1839 return NULL;
1840 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1841
1842 musb = hcd_to_musb(hcd);
1843 INIT_LIST_HEAD(&musb->control);
1844 INIT_LIST_HEAD(&musb->in_bulk);
1845 INIT_LIST_HEAD(&musb->out_bulk);
1846
1847 hcd->uses_new_polling = 1;
Felipe Balbiec95d352011-02-24 10:36:53 +02001848 hcd->has_tt = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001849
1850 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001851 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001852#else
1853 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1854 if (!musb)
1855 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001856
1857#endif
Ming Lei456bb162010-12-21 21:16:11 +08001858 dev_set_drvdata(dev, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001859 musb->mregs = mbase;
1860 musb->ctrl_base = mbase;
1861 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001862 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001863 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001864 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001865 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001866 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001867 ep->musb = musb;
1868 ep->epnum = epnum;
1869 }
1870
1871 musb->controller = dev;
Felipe Balbi743411b2010-12-01 13:22:05 +02001872
Felipe Balbi550a7372008-07-24 12:27:36 +03001873 return musb;
1874}
1875
1876static void musb_free(struct musb *musb)
1877{
1878 /* this has multiple entry modes. it handles fault cleanup after
1879 * probe(), where things may be partially set up, as well as rmmod
1880 * cleanup after everything's been de-activated.
1881 */
1882
1883#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001884 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001885#endif
1886
1887#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1888 musb_gadget_cleanup(musb);
1889#endif
1890
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001891 if (musb->nIrq >= 0) {
1892 if (musb->irq_wake)
1893 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001894 free_irq(musb->nIrq, musb);
1895 }
1896 if (is_dma_capable() && musb->dma_controller) {
1897 struct dma_controller *c = musb->dma_controller;
1898
1899 (void) c->stop(c);
1900 dma_controller_destroy(c);
1901 }
1902
Felipe Balbi550a7372008-07-24 12:27:36 +03001903#ifdef CONFIG_USB_MUSB_HDRC_HCD
1904 usb_put_hcd(musb_to_hcd(musb));
1905#else
1906 kfree(musb);
1907#endif
1908}
1909
1910/*
1911 * Perform generic per-controller initialization.
1912 *
1913 * @pDevice: the controller (already clocked, etc)
1914 * @nIrq: irq
1915 * @mregs: virtual address of controller registers,
1916 * not yet corrected for platform-specific offsets
1917 */
1918static int __init
1919musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1920{
1921 int status;
1922 struct musb *musb;
1923 struct musb_hdrc_platform_data *plat = dev->platform_data;
1924
1925 /* The driver might handle more features than the board; OK.
1926 * Fail when the board needs a feature that's not enabled.
1927 */
1928 if (!plat) {
1929 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001930 status = -ENODEV;
1931 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001932 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001933
Felipe Balbi550a7372008-07-24 12:27:36 +03001934 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001935 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001936 if (!musb) {
1937 status = -ENOMEM;
1938 goto fail0;
1939 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001940
Hema HK7acc6192011-02-28 14:19:34 +05301941 pm_runtime_use_autosuspend(musb->controller);
1942 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1943 pm_runtime_enable(musb->controller);
1944
Felipe Balbi550a7372008-07-24 12:27:36 +03001945 spin_lock_init(&musb->lock);
1946 musb->board_mode = plat->mode;
1947 musb->board_set_power = plat->set_power;
Felipe Balbi550a7372008-07-24 12:27:36 +03001948 musb->min_power = plat->min_power;
Felipe Balbif7ec9432010-12-02 09:48:58 +02001949 musb->ops = plat->platform_ops;
Felipe Balbi550a7372008-07-24 12:27:36 +03001950
David Brownell84e250f2009-03-31 12:30:04 -07001951 /* The musb_platform_init() call:
1952 * - adjusts musb->mregs and musb->isr if needed,
1953 * - may initialize an integrated tranceiver
1954 * - initializes musb->xceiv, usually by otg_get_transceiver()
David Brownell84e250f2009-03-31 12:30:04 -07001955 * - stops powering VBUS
David Brownell84e250f2009-03-31 12:30:04 -07001956 *
1957 * There are various transciever configurations. Blackfin,
1958 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1959 * external/discrete ones in various flavors (twl4030 family,
1960 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03001961 */
1962 musb->isr = generic_interrupt;
Hema Kalliguddiea65df52010-09-22 19:27:40 -05001963 status = musb_platform_init(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001964 if (status < 0)
Felipe Balbi03491762010-12-02 09:57:08 +02001965 goto fail1;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001966
Felipe Balbi550a7372008-07-24 12:27:36 +03001967 if (!musb->isr) {
1968 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001969 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001970 }
1971
Heikki Krogerusffb865b2010-03-25 13:25:28 +02001972 if (!musb->xceiv->io_ops) {
1973 musb->xceiv->io_priv = musb->mregs;
1974 musb->xceiv->io_ops = &musb_ulpi_access;
1975 }
1976
Felipe Balbi550a7372008-07-24 12:27:36 +03001977#ifndef CONFIG_MUSB_PIO_ONLY
1978 if (use_dma && dev->dma_mask) {
1979 struct dma_controller *c;
1980
1981 c = dma_controller_create(musb, musb->mregs);
1982 musb->dma_controller = c;
1983 if (c)
1984 (void) c->start(c);
1985 }
1986#endif
1987 /* ideally this would be abstracted in platform setup */
1988 if (!is_dma_capable() || !musb->dma_controller)
1989 dev->dma_mask = NULL;
1990
1991 /* be sure interrupts are disabled before connecting ISR */
1992 musb_platform_disable(musb);
1993 musb_generic_disable(musb);
1994
1995 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001996 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03001997 ? MUSB_CONTROLLER_MHDRC
1998 : MUSB_CONTROLLER_HDRC, musb);
1999 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002000 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002001
Amit Kucheria3a9f5bd2009-07-27 12:03:19 +03002002#ifdef CONFIG_USB_MUSB_OTG
David Brownellf7f9d632009-03-31 12:32:12 -07002003 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2004#endif
2005
Felipe Balbi550a7372008-07-24 12:27:36 +03002006 /* Init IRQ workqueue before request_irq */
2007 INIT_WORK(&musb->irq_work, musb_irq_work);
2008
2009 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002010 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002011 dev_err(dev, "request_irq %d failed!\n", nIrq);
2012 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002013 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002014 }
2015 musb->nIrq = nIrq;
2016/* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002017 if (enable_irq_wake(nIrq) == 0) {
2018 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002019 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002020 } else {
2021 musb->irq_wake = 0;
2022 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002023
David Brownell84e250f2009-03-31 12:30:04 -07002024 /* host side needs more setup */
2025 if (is_host_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002026 struct usb_hcd *hcd = musb_to_hcd(musb);
2027
David Brownell84e250f2009-03-31 12:30:04 -07002028 otg_set_host(musb->xceiv, &hcd->self);
2029
2030 if (is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002031 hcd->self.otg_port = 1;
David Brownell84e250f2009-03-31 12:30:04 -07002032 musb->xceiv->host = &hcd->self;
Felipe Balbi550a7372008-07-24 12:27:36 +03002033 hcd->power_budget = 2 * (plat->power ? : 250);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002034
2035 /* program PHY to use external vBus if required */
2036 if (plat->extvbus) {
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002037 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002038 busctl |= MUSB_ULPI_USE_EXTVBUS;
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002039 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002040 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002041 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002042
2043 /* For the host-only role, we can activate right away.
2044 * (We expect the ID pin to be forcibly grounded!!)
2045 * Otherwise, wait till the gadget driver hooks up.
2046 */
2047 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302048 struct usb_hcd *hcd = musb_to_hcd(musb);
2049
Felipe Balbi550a7372008-07-24 12:27:36 +03002050 MUSB_HST_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002051 musb->xceiv->default_a = 1;
2052 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002053
2054 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2055
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302056 hcd->self.uses_pio_for_control = 1;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002057 dev_dbg(musb->controller, "%s mode, status %d, devctl %02x %c\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002058 "HOST", status,
2059 musb_readb(musb->mregs, MUSB_DEVCTL),
2060 (musb_readb(musb->mregs, MUSB_DEVCTL)
2061 & MUSB_DEVCTL_BDEVICE
2062 ? 'B' : 'A'));
2063
2064 } else /* peripheral is enabled */ {
2065 MUSB_DEV_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002066 musb->xceiv->default_a = 0;
2067 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002068
2069 status = musb_gadget_setup(musb);
2070
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002071 dev_dbg(musb->controller, "%s mode, status %d, dev%02x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002072 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2073 status,
2074 musb_readb(musb->mregs, MUSB_DEVCTL));
2075
2076 }
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002077 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002078 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002079
Hema HK7acc6192011-02-28 14:19:34 +05302080 pm_runtime_put(musb->controller);
2081
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002082 status = musb_init_debugfs(musb);
2083 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002084 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002085
Felipe Balbi550a7372008-07-24 12:27:36 +03002086#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02002087 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002088 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002089 goto fail5;
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002090#endif
Felipe Balbi28c2c512008-09-11 11:53:25 +03002091
Felipe Balbiab3bbfa2010-01-21 15:33:58 +02002092 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
2093 ({char *s;
2094 switch (musb->board_mode) {
2095 case MUSB_HOST: s = "Host"; break;
2096 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2097 default: s = "OTG"; break;
2098 }; s; }),
2099 ctrl,
2100 (is_dma_capable() && musb->dma_controller)
2101 ? "DMA" : "PIO",
2102 musb->nIrq);
2103
Felipe Balbi28c2c512008-09-11 11:53:25 +03002104 return 0;
2105
Felipe Balbib0f9da72010-03-25 13:25:18 +02002106fail5:
2107 musb_exit_debugfs(musb);
2108
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002109fail4:
2110 if (!is_otg_enabled(musb) && is_host_enabled(musb))
2111 usb_remove_hcd(musb_to_hcd(musb));
2112 else
2113 musb_gadget_cleanup(musb);
2114
2115fail3:
2116 if (musb->irq_wake)
2117 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002118 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002119
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002120fail1:
Felipe Balbi28c2c512008-09-11 11:53:25 +03002121 dev_err(musb->controller,
2122 "musb_init_controller failed with status %d\n", status);
2123
Felipe Balbi28c2c512008-09-11 11:53:25 +03002124 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002125
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002126fail0:
2127
Felipe Balbi550a7372008-07-24 12:27:36 +03002128 return status;
2129
Felipe Balbi550a7372008-07-24 12:27:36 +03002130}
2131
2132/*-------------------------------------------------------------------------*/
2133
2134/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2135 * bridge to a platform device; this driver then suffices.
2136 */
2137
2138#ifndef CONFIG_MUSB_PIO_ONLY
2139static u64 *orig_dma_mask;
2140#endif
2141
2142static int __init musb_probe(struct platform_device *pdev)
2143{
2144 struct device *dev = &pdev->dev;
Hema Kalliguddifcf173e2010-09-29 11:26:39 -05002145 int irq = platform_get_irq_byname(pdev, "mc");
Felipe Balbida5108e2010-01-21 15:33:57 +02002146 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002147 struct resource *iomem;
2148 void __iomem *base;
2149
2150 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sergei Shtylyov541079d2010-12-10 21:03:29 +03002151 if (!iomem || irq <= 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03002152 return -ENODEV;
2153
Felipe Balbi195e9e42009-12-15 11:08:42 +02002154 base = ioremap(iomem->start, resource_size(iomem));
Felipe Balbi550a7372008-07-24 12:27:36 +03002155 if (!base) {
2156 dev_err(dev, "ioremap failed\n");
2157 return -ENOMEM;
2158 }
2159
2160#ifndef CONFIG_MUSB_PIO_ONLY
2161 /* clobbered by use_dma=n */
2162 orig_dma_mask = dev->dma_mask;
2163#endif
Felipe Balbida5108e2010-01-21 15:33:57 +02002164 status = musb_init_controller(dev, irq, base);
2165 if (status < 0)
2166 iounmap(base);
2167
2168 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002169}
2170
Felipe Balbie3060b12009-12-15 11:08:41 +02002171static int __exit musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002172{
2173 struct musb *musb = dev_to_musb(&pdev->dev);
2174 void __iomem *ctrl_base = musb->ctrl_base;
2175
2176 /* this gets called on rmmod.
2177 * - Host mode: host may still be active
2178 * - Peripheral mode: peripheral is deactivated (or never-activated)
2179 * - OTG mode: both roles are deactivated (or never-activated)
2180 */
Hema HK7acc6192011-02-28 14:19:34 +05302181 pm_runtime_get_sync(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002182 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002183 musb_shutdown(pdev);
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002184
Hema HK7acc6192011-02-28 14:19:34 +05302185 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03002186 musb_free(musb);
2187 iounmap(ctrl_base);
2188 device_init_wakeup(&pdev->dev, 0);
2189#ifndef CONFIG_MUSB_PIO_ONLY
2190 pdev->dev.dma_mask = orig_dma_mask;
2191#endif
2192 return 0;
2193}
2194
2195#ifdef CONFIG_PM
2196
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002197static void musb_save_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002198{
2199 int i;
2200 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002201 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002202
2203 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002204 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2205 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2206 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002207 }
Felipe Balbi74211072010-12-01 13:53:27 +02002208 musb->context.power = musb_readb(musb_base, MUSB_POWER);
2209 musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
2210 musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
2211 musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2212 musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2213 musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002214
Bob Liuae9b2ad2010-09-24 13:44:07 +03002215 for (i = 0; i < musb->config->num_eps; ++i) {
2216 epio = musb->endpoints[i].regs;
Felipe Balbi74211072010-12-01 13:53:27 +02002217 musb->context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002218 musb_readw(epio, MUSB_TXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002219 musb->context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002220 musb_readw(epio, MUSB_TXCSR);
Felipe Balbi74211072010-12-01 13:53:27 +02002221 musb->context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002222 musb_readw(epio, MUSB_RXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002223 musb->context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002224 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002225
2226 if (musb->dyn_fifo) {
Felipe Balbi74211072010-12-01 13:53:27 +02002227 musb->context.index_regs[i].txfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002228 musb_read_txfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002229 musb->context.index_regs[i].rxfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002230 musb_read_rxfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002231 musb->context.index_regs[i].txfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002232 musb_read_txfifosz(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002233 musb->context.index_regs[i].rxfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002234 musb_read_rxfifosz(musb_base);
2235 }
2236 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002237 musb->context.index_regs[i].txtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002238 musb_readb(epio, MUSB_TXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002239 musb->context.index_regs[i].txinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002240 musb_readb(epio, MUSB_TXINTERVAL);
Felipe Balbi74211072010-12-01 13:53:27 +02002241 musb->context.index_regs[i].rxtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002242 musb_readb(epio, MUSB_RXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002243 musb->context.index_regs[i].rxinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002244 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002245
Felipe Balbi74211072010-12-01 13:53:27 +02002246 musb->context.index_regs[i].txfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002247 musb_read_txfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002248 musb->context.index_regs[i].txhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002249 musb_read_txhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002250 musb->context.index_regs[i].txhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002251 musb_read_txhubport(musb_base, i);
2252
Felipe Balbi74211072010-12-01 13:53:27 +02002253 musb->context.index_regs[i].rxfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002254 musb_read_rxfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002255 musb->context.index_regs[i].rxhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002256 musb_read_rxhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002257 musb->context.index_regs[i].rxhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002258 musb_read_rxhubport(musb_base, i);
2259 }
2260 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002261}
2262
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002263static void musb_restore_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002264{
2265 int i;
2266 void __iomem *musb_base = musb->mregs;
2267 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002268 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002269
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002270 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002271 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2272 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2273 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002274 }
Felipe Balbi74211072010-12-01 13:53:27 +02002275 musb_writeb(musb_base, MUSB_POWER, musb->context.power);
2276 musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
2277 musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
2278 musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2279 musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002280
Bob Liuae9b2ad2010-09-24 13:44:07 +03002281 for (i = 0; i < musb->config->num_eps; ++i) {
2282 epio = musb->endpoints[i].regs;
2283 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002284 musb->context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002285 musb_writew(epio, MUSB_TXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002286 musb->context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002287 musb_writew(epio, MUSB_RXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002288 musb->context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002289 musb_writew(epio, MUSB_RXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002290 musb->context.index_regs[i].rxcsr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002291
2292 if (musb->dyn_fifo) {
2293 musb_write_txfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002294 musb->context.index_regs[i].txfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002295 musb_write_rxfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002296 musb->context.index_regs[i].rxfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002297 musb_write_txfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002298 musb->context.index_regs[i].txfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002299 musb_write_rxfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002300 musb->context.index_regs[i].rxfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002301 }
2302
2303 if (is_host_enabled(musb)) {
Bob Liuae9b2ad2010-09-24 13:44:07 +03002304 musb_writeb(epio, MUSB_TXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002305 musb->context.index_regs[i].txtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002306 musb_writeb(epio, MUSB_TXINTERVAL,
Felipe Balbi74211072010-12-01 13:53:27 +02002307 musb->context.index_regs[i].txinterval);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002308 musb_writeb(epio, MUSB_RXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002309 musb->context.index_regs[i].rxtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002310 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002311
Felipe Balbi74211072010-12-01 13:53:27 +02002312 musb->context.index_regs[i].rxinterval);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002313 musb_write_txfunaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002314 musb->context.index_regs[i].txfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002315 musb_write_txhubaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002316 musb->context.index_regs[i].txhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002317 musb_write_txhubport(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002318 musb->context.index_regs[i].txhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002319
2320 ep_target_regs =
2321 musb_read_target_reg_base(i, musb_base);
2322
2323 musb_write_rxfunaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002324 musb->context.index_regs[i].rxfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002325 musb_write_rxhubaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002326 musb->context.index_regs[i].rxhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002327 musb_write_rxhubport(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002328 musb->context.index_regs[i].rxhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002329 }
2330 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002331}
2332
Magnus Damm48fea962009-07-08 13:22:56 +02002333static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002334{
Magnus Damm48fea962009-07-08 13:22:56 +02002335 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002336 unsigned long flags;
2337 struct musb *musb = dev_to_musb(&pdev->dev);
2338
Felipe Balbi550a7372008-07-24 12:27:36 +03002339 spin_lock_irqsave(&musb->lock, flags);
2340
2341 if (is_peripheral_active(musb)) {
2342 /* FIXME force disconnect unless we know USB will wake
2343 * the system up quickly enough to respond ...
2344 */
2345 } else if (is_host_active(musb)) {
2346 /* we know all the children are suspended; sometimes
2347 * they will even be wakeup-enabled.
2348 */
2349 }
2350
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002351 musb_save_context(musb);
2352
Felipe Balbi550a7372008-07-24 12:27:36 +03002353 spin_unlock_irqrestore(&musb->lock, flags);
2354 return 0;
2355}
2356
Magnus Damm48fea962009-07-08 13:22:56 +02002357static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002358{
Magnus Damm48fea962009-07-08 13:22:56 +02002359 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002360 struct musb *musb = dev_to_musb(&pdev->dev);
2361
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002362 musb_restore_context(musb);
2363
Felipe Balbi550a7372008-07-24 12:27:36 +03002364 /* for static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002365 * unless for some reason the whole soc powered down or the USB
2366 * module got reset through the PSC (vs just being disabled).
Felipe Balbi550a7372008-07-24 12:27:36 +03002367 */
Felipe Balbi550a7372008-07-24 12:27:36 +03002368 return 0;
2369}
2370
Hema HK7acc6192011-02-28 14:19:34 +05302371static int musb_runtime_suspend(struct device *dev)
2372{
2373 struct musb *musb = dev_to_musb(dev);
2374
2375 musb_save_context(musb);
2376
2377 return 0;
2378}
2379
2380static int musb_runtime_resume(struct device *dev)
2381{
2382 struct musb *musb = dev_to_musb(dev);
2383 static int first = 1;
2384
2385 /*
2386 * When pm_runtime_get_sync called for the first time in driver
2387 * init, some of the structure is still not initialized which is
2388 * used in restore function. But clock needs to be
2389 * enabled before any register access, so
2390 * pm_runtime_get_sync has to be called.
2391 * Also context restore without save does not make
2392 * any sense
2393 */
2394 if (!first)
2395 musb_restore_context(musb);
2396 first = 0;
2397
2398 return 0;
2399}
2400
Alexey Dobriyan47145212009-12-14 18:00:08 -08002401static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002402 .suspend = musb_suspend,
2403 .resume_noirq = musb_resume_noirq,
Hema HK7acc6192011-02-28 14:19:34 +05302404 .runtime_suspend = musb_runtime_suspend,
2405 .runtime_resume = musb_runtime_resume,
Magnus Damm48fea962009-07-08 13:22:56 +02002406};
2407
2408#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002409#else
Magnus Damm48fea962009-07-08 13:22:56 +02002410#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002411#endif
2412
2413static struct platform_driver musb_driver = {
2414 .driver = {
2415 .name = (char *)musb_driver_name,
2416 .bus = &platform_bus_type,
2417 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002418 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002419 },
Felipe Balbie3060b12009-12-15 11:08:41 +02002420 .remove = __exit_p(musb_remove),
Felipe Balbi550a7372008-07-24 12:27:36 +03002421 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002422};
2423
2424/*-------------------------------------------------------------------------*/
2425
2426static int __init musb_init(void)
2427{
2428#ifdef CONFIG_USB_MUSB_HDRC_HCD
2429 if (usb_disabled())
2430 return 0;
2431#endif
2432
2433 pr_info("%s: version " MUSB_VERSION ", "
2434#ifdef CONFIG_MUSB_PIO_ONLY
2435 "pio"
2436#elif defined(CONFIG_USB_TI_CPPI_DMA)
2437 "cppi-dma"
2438#elif defined(CONFIG_USB_INVENTRA_DMA)
2439 "musb-dma"
2440#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2441 "tusb-omap-dma"
2442#else
2443 "?dma?"
2444#endif
2445 ", "
2446#ifdef CONFIG_USB_MUSB_OTG
2447 "otg (peripheral+host)"
2448#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2449 "peripheral"
2450#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2451 "host"
2452#endif
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002453 ,
2454 musb_driver_name);
Felipe Balbi550a7372008-07-24 12:27:36 +03002455 return platform_driver_probe(&musb_driver, musb_probe);
2456}
2457
David Brownell34f32c92009-02-20 13:45:17 -08002458/* make us init after usbcore and i2c (transceivers, regulators, etc)
2459 * and before usb gadget and host-side drivers start to register
Felipe Balbi550a7372008-07-24 12:27:36 +03002460 */
David Brownell34f32c92009-02-20 13:45:17 -08002461fs_initcall(musb_init);
Felipe Balbi550a7372008-07-24 12:27:36 +03002462
2463static void __exit musb_cleanup(void)
2464{
2465 platform_driver_unregister(&musb_driver);
2466}
2467module_exit(musb_cleanup);