blob: f10ff00ca09ea377bbd7dd6167e3723260bd78f9 [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 Balbib60c72a2008-10-29 15:10:39 +0200107unsigned musb_debug;
David Brownell34f32c92009-02-20 13:45:17 -0800108module_param_named(debug, musb_debug, uint, S_IRUGO | S_IWUSR);
Felipe Balbie8164f62008-08-10 21:22:35 +0300109MODULE_PARM_DESC(debug, "Debug message level. Default = 0");
Felipe Balbi550a7372008-07-24 12:27:36 +0300110
111#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
112#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
113
Felipe Balbie8164f62008-08-10 21:22:35 +0300114#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300115
116#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
117
Felipe Balbi05ac10d2010-12-02 08:49:26 +0200118#define MUSB_DRIVER_NAME "musb-hdrc"
Felipe Balbi550a7372008-07-24 12:27:36 +0300119const char musb_driver_name[] = MUSB_DRIVER_NAME;
120
121MODULE_DESCRIPTION(DRIVER_INFO);
122MODULE_AUTHOR(DRIVER_AUTHOR);
123MODULE_LICENSE("GPL");
124MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
125
126
127/*-------------------------------------------------------------------------*/
128
129static inline struct musb *dev_to_musb(struct device *dev)
130{
Felipe Balbi550a7372008-07-24 12:27:36 +0300131 return dev_get_drvdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300132}
133
134/*-------------------------------------------------------------------------*/
135
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200136#ifndef CONFIG_BLACKFIN
137static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
138{
139 void __iomem *addr = otg->io_priv;
140 int i = 0;
141 u8 r;
142 u8 power;
143
144 /* Make sure the transceiver is not in low power mode */
145 power = musb_readb(addr, MUSB_POWER);
146 power &= ~MUSB_POWER_SUSPENDM;
147 musb_writeb(addr, MUSB_POWER, power);
148
149 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
150 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
151 */
152
153 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
154 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
155 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
156
157 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
158 & MUSB_ULPI_REG_CMPLT)) {
159 i++;
160 if (i == 10000) {
161 DBG(3, "ULPI read timed out\n");
162 return -ETIMEDOUT;
163 }
164
165 }
166 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
167 r &= ~MUSB_ULPI_REG_CMPLT;
168 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
169
170 return musb_readb(addr, MUSB_ULPI_REG_DATA);
171}
172
173static int musb_ulpi_write(struct otg_transceiver *otg,
174 u32 offset, u32 data)
175{
176 void __iomem *addr = otg->io_priv;
177 int i = 0;
178 u8 r = 0;
179 u8 power;
180
181 /* Make sure the transceiver is not in low power mode */
182 power = musb_readb(addr, MUSB_POWER);
183 power &= ~MUSB_POWER_SUSPENDM;
184 musb_writeb(addr, MUSB_POWER, power);
185
186 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
187 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
188 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
189
190 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
191 & MUSB_ULPI_REG_CMPLT)) {
192 i++;
193 if (i == 10000) {
194 DBG(3, "ULPI write timed out\n");
195 return -ETIMEDOUT;
196 }
197 }
198
199 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
200 r &= ~MUSB_ULPI_REG_CMPLT;
201 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
202
203 return 0;
204}
205#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530206#define musb_ulpi_read NULL
207#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200208#endif
209
210static struct otg_io_access_ops musb_ulpi_access = {
211 .read = musb_ulpi_read,
212 .write = musb_ulpi_write,
213};
214
215/*-------------------------------------------------------------------------*/
216
Felipe Balbi7c925542010-12-01 14:23:48 +0200217#if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200218
Felipe Balbi550a7372008-07-24 12:27:36 +0300219/*
220 * Load an endpoint's FIFO
221 */
222void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
223{
224 void __iomem *fifo = hw_ep->fifo;
225
226 prefetch((u8 *)src);
227
228 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
229 'T', hw_ep->epnum, fifo, len, src);
230
231 /* we can't assume unaligned reads work */
232 if (likely((0x01 & (unsigned long) src) == 0)) {
233 u16 index = 0;
234
235 /* best case is 32bit-aligned source address */
236 if ((0x02 & (unsigned long) src) == 0) {
237 if (len >= 4) {
238 writesl(fifo, src + index, len >> 2);
239 index += len & ~0x03;
240 }
241 if (len & 0x02) {
242 musb_writew(fifo, 0, *(u16 *)&src[index]);
243 index += 2;
244 }
245 } else {
246 if (len >= 2) {
247 writesw(fifo, src + index, len >> 1);
248 index += len & ~0x01;
249 }
250 }
251 if (len & 0x01)
252 musb_writeb(fifo, 0, src[index]);
253 } else {
254 /* byte aligned */
255 writesb(fifo, src, len);
256 }
257}
258
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300259#if !defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +0300260/*
261 * Unload an endpoint's FIFO
262 */
263void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
264{
265 void __iomem *fifo = hw_ep->fifo;
266
267 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
268 'R', hw_ep->epnum, fifo, len, dst);
269
270 /* we can't assume unaligned writes work */
271 if (likely((0x01 & (unsigned long) dst) == 0)) {
272 u16 index = 0;
273
274 /* best case is 32bit-aligned destination address */
275 if ((0x02 & (unsigned long) dst) == 0) {
276 if (len >= 4) {
277 readsl(fifo, dst, len >> 2);
278 index = len & ~0x03;
279 }
280 if (len & 0x02) {
281 *(u16 *)&dst[index] = musb_readw(fifo, 0);
282 index += 2;
283 }
284 } else {
285 if (len >= 2) {
286 readsw(fifo, dst, len >> 1);
287 index = len & ~0x01;
288 }
289 }
290 if (len & 0x01)
291 dst[index] = musb_readb(fifo, 0);
292 } else {
293 /* byte aligned */
294 readsb(fifo, dst, len);
295 }
296}
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300297#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300298
299#endif /* normal PIO */
300
301
302/*-------------------------------------------------------------------------*/
303
304/* for high speed test mode; see USB 2.0 spec 7.1.20 */
305static const u8 musb_test_packet[53] = {
306 /* implicit SYNC then DATA0 to start */
307
308 /* JKJKJKJK x9 */
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 /* JJKKJJKK x8 */
311 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
312 /* JJJJKKKK x8 */
313 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
314 /* JJJJJJJKKKKKKK x8 */
315 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
316 /* JJJJJJJK x8 */
317 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
318 /* JKKKKKKK x10, JK */
319 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
320
321 /* implicit CRC16 then EOP to end */
322};
323
324void musb_load_testpacket(struct musb *musb)
325{
326 void __iomem *regs = musb->endpoints[0].regs;
327
328 musb_ep_select(musb->mregs, 0);
329 musb_write_fifo(musb->control_ep,
330 sizeof(musb_test_packet), musb_test_packet);
331 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
332}
333
334/*-------------------------------------------------------------------------*/
335
336const char *otg_state_string(struct musb *musb)
337{
David Brownell84e250f2009-03-31 12:30:04 -0700338 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300339 case OTG_STATE_A_IDLE: return "a_idle";
340 case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise";
341 case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon";
342 case OTG_STATE_A_HOST: return "a_host";
343 case OTG_STATE_A_SUSPEND: return "a_suspend";
344 case OTG_STATE_A_PERIPHERAL: return "a_peripheral";
345 case OTG_STATE_A_WAIT_VFALL: return "a_wait_vfall";
346 case OTG_STATE_A_VBUS_ERR: return "a_vbus_err";
347 case OTG_STATE_B_IDLE: return "b_idle";
348 case OTG_STATE_B_SRP_INIT: return "b_srp_init";
349 case OTG_STATE_B_PERIPHERAL: return "b_peripheral";
350 case OTG_STATE_B_WAIT_ACON: return "b_wait_acon";
351 case OTG_STATE_B_HOST: return "b_host";
352 default: return "UNDEFINED";
353 }
354}
355
356#ifdef CONFIG_USB_MUSB_OTG
357
358/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300359 * Handles OTG hnp timeouts, such as b_ase0_brst
360 */
361void musb_otg_timer_func(unsigned long data)
362{
363 struct musb *musb = (struct musb *)data;
364 unsigned long flags;
365
366 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700367 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300368 case OTG_STATE_B_WAIT_ACON:
369 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
370 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700371 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300372 musb->is_active = 0;
373 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700374 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300375 case OTG_STATE_A_WAIT_BCON:
David Brownellab983f2a2009-03-31 12:35:09 -0700376 DBG(1, "HNP: %s timeout\n", otg_state_string(musb));
Felipe Balbi743411b2010-12-01 13:22:05 +0200377 musb_platform_set_vbus(musb, 0);
David Brownellab983f2a2009-03-31 12:35:09 -0700378 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300379 break;
380 default:
381 DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb));
382 }
383 musb->ignore_disconnect = 0;
384 spin_unlock_irqrestore(&musb->lock, flags);
385}
386
Felipe Balbi550a7372008-07-24 12:27:36 +0300387/*
David Brownellf7f9d632009-03-31 12:32:12 -0700388 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300389 */
390void musb_hnp_stop(struct musb *musb)
391{
392 struct usb_hcd *hcd = musb_to_hcd(musb);
393 void __iomem *mbase = musb->mregs;
394 u8 reg;
395
David Brownellab983f2a2009-03-31 12:35:09 -0700396 DBG(1, "HNP: stop from %s\n", otg_state_string(musb));
397
David Brownell84e250f2009-03-31 12:30:04 -0700398 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300399 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300400 musb_g_disconnect(musb);
David Brownellab983f2a2009-03-31 12:35:09 -0700401 DBG(1, "HNP: back to %s\n", otg_state_string(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300402 break;
403 case OTG_STATE_B_HOST:
404 DBG(1, "HNP: Disabling HR\n");
405 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700406 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300407 MUSB_DEV_MODE(musb);
408 reg = musb_readb(mbase, MUSB_POWER);
409 reg |= MUSB_POWER_SUSPENDM;
410 musb_writeb(mbase, MUSB_POWER, reg);
411 /* REVISIT: Start SESSION_REQUEST here? */
412 break;
413 default:
414 DBG(1, "HNP: Stopping in unknown state %s\n",
415 otg_state_string(musb));
416 }
417
418 /*
419 * When returning to A state after HNP, avoid hub_port_rebounce(),
420 * which cause occasional OPT A "Did not receive reset after connect"
421 * errors.
422 */
Alan Stern749da5f2010-03-04 17:05:08 -0500423 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300424}
425
426#endif
427
428/*
429 * Interrupt Service Routine to record USB "global" interrupts.
430 * Since these do not happen often and signify things of
431 * paramount importance, it seems OK to check them individually;
432 * the order of the tests is specified in the manual
433 *
434 * @param musb instance pointer
435 * @param int_usb register contents
436 * @param devctl
437 * @param power
438 */
439
Felipe Balbi550a7372008-07-24 12:27:36 +0300440static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
441 u8 devctl, u8 power)
442{
443 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300444
445 DBG(3, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
446 int_usb);
447
448 /* in host mode, the peripheral may issue remote wakeup.
449 * in peripheral mode, the host may resume the link.
450 * spurious RESUME irqs happen too, paired with SUSPEND.
451 */
452 if (int_usb & MUSB_INTR_RESUME) {
453 handled = IRQ_HANDLED;
454 DBG(3, "RESUME (%s)\n", otg_state_string(musb));
455
456 if (devctl & MUSB_DEVCTL_HM) {
457#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbiaa471452010-03-12 10:27:24 +0200458 void __iomem *mbase = musb->mregs;
459
David Brownell84e250f2009-03-31 12:30:04 -0700460 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300461 case OTG_STATE_A_SUSPEND:
462 /* remote wakeup? later, GetPortStatus
463 * will stop RESUME signaling
464 */
465
466 if (power & MUSB_POWER_SUSPENDM) {
467 /* spurious */
468 musb->int_usb &= ~MUSB_INTR_SUSPEND;
469 DBG(2, "Spurious SUSPENDM\n");
470 break;
471 }
472
473 power &= ~MUSB_POWER_SUSPENDM;
474 musb_writeb(mbase, MUSB_POWER,
475 power | MUSB_POWER_RESUME);
476
477 musb->port1_status |=
478 (USB_PORT_STAT_C_SUSPEND << 16)
479 | MUSB_PORT_STAT_RESUME;
480 musb->rh_timer = jiffies
481 + msecs_to_jiffies(20);
482
David Brownell84e250f2009-03-31 12:30:04 -0700483 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300484 musb->is_active = 1;
485 usb_hcd_resume_root_hub(musb_to_hcd(musb));
486 break;
487 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700488 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300489 musb->is_active = 1;
490 MUSB_DEV_MODE(musb);
491 break;
492 default:
493 WARNING("bogus %s RESUME (%s)\n",
494 "host",
495 otg_state_string(musb));
496 }
497#endif
498 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700499 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300500#ifdef CONFIG_USB_MUSB_HDRC_HCD
501 case OTG_STATE_A_SUSPEND:
502 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700503 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300504 usb_hcd_resume_root_hub(musb_to_hcd(musb));
505 break;
506#endif
507#ifdef CONFIG_USB_GADGET_MUSB_HDRC
508 case OTG_STATE_B_WAIT_ACON:
509 case OTG_STATE_B_PERIPHERAL:
510 /* disconnect while suspended? we may
511 * not get a disconnect irq...
512 */
513 if ((devctl & MUSB_DEVCTL_VBUS)
514 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
515 ) {
516 musb->int_usb |= MUSB_INTR_DISCONNECT;
517 musb->int_usb &= ~MUSB_INTR_SUSPEND;
518 break;
519 }
520 musb_g_resume(musb);
521 break;
522 case OTG_STATE_B_IDLE:
523 musb->int_usb &= ~MUSB_INTR_SUSPEND;
524 break;
525#endif
526 default:
527 WARNING("bogus %s RESUME (%s)\n",
528 "peripheral",
529 otg_state_string(musb));
530 }
531 }
532 }
533
534#ifdef CONFIG_USB_MUSB_HDRC_HCD
535 /* see manual for the order of the tests */
536 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200537 void __iomem *mbase = musb->mregs;
538
Heikki Krogerus19aab562010-10-29 04:23:27 -0500539 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
540 && (devctl & MUSB_DEVCTL_BDEVICE)) {
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300541 DBG(3, "SessReq while on B state\n");
542 return IRQ_HANDLED;
543 }
544
Felipe Balbi550a7372008-07-24 12:27:36 +0300545 DBG(1, "SESSION_REQUEST (%s)\n", otg_state_string(musb));
546
547 /* IRQ arrives from ID pin sense or (later, if VBUS power
548 * is removed) SRP. responses are time critical:
549 * - turn on VBUS (with silicon-specific mechanism)
550 * - go through A_WAIT_VRISE
551 * - ... to A_WAIT_BCON.
552 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
553 */
554 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
555 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700556 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300557 MUSB_HST_MODE(musb);
Felipe Balbi743411b2010-12-01 13:22:05 +0200558 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300559
560 handled = IRQ_HANDLED;
561 }
562
563 if (int_usb & MUSB_INTR_VBUSERROR) {
564 int ignore = 0;
565
566 /* During connection as an A-Device, we may see a short
567 * current spikes causing voltage drop, because of cable
568 * and peripheral capacitance combined with vbus draw.
569 * (So: less common with truly self-powered devices, where
570 * vbus doesn't act like a power supply.)
571 *
572 * Such spikes are short; usually less than ~500 usec, max
573 * of ~2 msec. That is, they're not sustained overcurrent
574 * errors, though they're reported using VBUSERROR irqs.
575 *
576 * Workarounds: (a) hardware: use self powered devices.
577 * (b) software: ignore non-repeated VBUS errors.
578 *
579 * REVISIT: do delays from lots of DEBUG_KERNEL checks
580 * make trouble here, keeping VBUS < 4.4V ?
581 */
David Brownell84e250f2009-03-31 12:30:04 -0700582 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300583 case OTG_STATE_A_HOST:
584 /* recovery is dicey once we've gotten past the
585 * initial stages of enumeration, but if VBUS
586 * stayed ok at the other end of the link, and
587 * another reset is due (at least for high speed,
588 * to redo the chirp etc), it might work OK...
589 */
590 case OTG_STATE_A_WAIT_BCON:
591 case OTG_STATE_A_WAIT_VRISE:
592 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200593 void __iomem *mbase = musb->mregs;
594
Felipe Balbi550a7372008-07-24 12:27:36 +0300595 musb->vbuserr_retry--;
596 ignore = 1;
597 devctl |= MUSB_DEVCTL_SESSION;
598 musb_writeb(mbase, MUSB_DEVCTL, devctl);
599 } else {
600 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500601 USB_PORT_STAT_OVERCURRENT
602 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300603 }
604 break;
605 default:
606 break;
607 }
608
609 DBG(1, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
610 otg_state_string(musb),
611 devctl,
612 ({ char *s;
613 switch (devctl & MUSB_DEVCTL_VBUS) {
614 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
615 s = "<SessEnd"; break;
616 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
617 s = "<AValid"; break;
618 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
619 s = "<VBusValid"; break;
620 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
621 default:
622 s = "VALID"; break;
623 }; s; }),
624 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
625 musb->port1_status);
626
627 /* go through A_WAIT_VFALL then start a new session */
628 if (!ignore)
Felipe Balbi743411b2010-12-01 13:22:05 +0200629 musb_platform_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300630 handled = IRQ_HANDLED;
631 }
632
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530633#endif
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200634 if (int_usb & MUSB_INTR_SUSPEND) {
635 DBG(1, "SUSPEND (%s) devctl %02x power %02x\n",
636 otg_state_string(musb), devctl, power);
637 handled = IRQ_HANDLED;
638
639 switch (musb->xceiv->state) {
640#ifdef CONFIG_USB_MUSB_OTG
641 case OTG_STATE_A_PERIPHERAL:
642 /* We also come here if the cable is removed, since
643 * this silicon doesn't report ID-no-longer-grounded.
644 *
645 * We depend on T(a_wait_bcon) to shut us down, and
646 * hope users don't do anything dicey during this
647 * undesired detour through A_WAIT_BCON.
648 */
649 musb_hnp_stop(musb);
650 usb_hcd_resume_root_hub(musb_to_hcd(musb));
651 musb_root_disconnect(musb);
652 musb_platform_try_idle(musb, jiffies
653 + msecs_to_jiffies(musb->a_wait_bcon
654 ? : OTG_TIME_A_WAIT_BCON));
655
656 break;
657#endif
658 case OTG_STATE_B_IDLE:
659 if (!musb->is_active)
660 break;
661 case OTG_STATE_B_PERIPHERAL:
662 musb_g_suspend(musb);
663 musb->is_active = is_otg_enabled(musb)
664 && musb->xceiv->gadget->b_hnp_enable;
665 if (musb->is_active) {
666#ifdef CONFIG_USB_MUSB_OTG
667 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
668 DBG(1, "HNP: Setting timer for b_ase0_brst\n");
669 mod_timer(&musb->otg_timer, jiffies
670 + msecs_to_jiffies(
671 OTG_TIME_B_ASE0_BRST));
672#endif
673 }
674 break;
675 case OTG_STATE_A_WAIT_BCON:
676 if (musb->a_wait_bcon != 0)
677 musb_platform_try_idle(musb, jiffies
678 + msecs_to_jiffies(musb->a_wait_bcon));
679 break;
680 case OTG_STATE_A_HOST:
681 musb->xceiv->state = OTG_STATE_A_SUSPEND;
682 musb->is_active = is_otg_enabled(musb)
683 && musb->xceiv->host->b_hnp_enable;
684 break;
685 case OTG_STATE_B_HOST:
686 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
687 DBG(1, "REVISIT: SUSPEND as B_HOST\n");
688 break;
689 default:
690 /* "should not happen" */
691 musb->is_active = 0;
692 break;
693 }
694 }
695
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530696#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +0300697 if (int_usb & MUSB_INTR_CONNECT) {
698 struct usb_hcd *hcd = musb_to_hcd(musb);
699
700 handled = IRQ_HANDLED;
701 musb->is_active = 1;
702 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
703
704 musb->ep0_stage = MUSB_EP0_START;
705
706#ifdef CONFIG_USB_MUSB_OTG
707 /* flush endpoints when transitioning from Device Mode */
708 if (is_peripheral_active(musb)) {
709 /* REVISIT HNP; just force disconnect */
710 }
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530711 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
712 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
713 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300714#endif
715 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
716 |USB_PORT_STAT_HIGH_SPEED
717 |USB_PORT_STAT_ENABLE
718 );
719 musb->port1_status |= USB_PORT_STAT_CONNECTION
720 |(USB_PORT_STAT_C_CONNECTION << 16);
721
722 /* high vs full speed is just a guess until after reset */
723 if (devctl & MUSB_DEVCTL_LSDEV)
724 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
725
Felipe Balbi550a7372008-07-24 12:27:36 +0300726 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700727 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300728 case OTG_STATE_B_PERIPHERAL:
729 if (int_usb & MUSB_INTR_SUSPEND) {
730 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300731 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700732 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300733 } else
734 DBG(1, "CONNECT as b_peripheral???\n");
735 break;
736 case OTG_STATE_B_WAIT_ACON:
David Brownell1de00da2009-04-02 10:16:11 -0700737 DBG(1, "HNP: CONNECT, now b_host\n");
738b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700739 musb->xceiv->state = OTG_STATE_B_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300740 hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700741 musb->ignore_disconnect = 0;
742 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300743 break;
744 default:
745 if ((devctl & MUSB_DEVCTL_VBUS)
746 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700747 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300748 hcd->self.is_b_host = 0;
749 }
750 break;
751 }
David Brownell1de00da2009-04-02 10:16:11 -0700752
753 /* poke the root hub */
754 MUSB_HST_MODE(musb);
755 if (hcd->status_urb)
756 usb_hcd_poll_rh_status(hcd);
757 else
758 usb_hcd_resume_root_hub(hcd);
759
Felipe Balbi550a7372008-07-24 12:27:36 +0300760 DBG(1, "CONNECT (%s) devctl %02x\n",
761 otg_state_string(musb), devctl);
762 }
763#endif /* CONFIG_USB_MUSB_HDRC_HCD */
764
Felipe Balbi550a7372008-07-24 12:27:36 +0300765 if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
766 DBG(1, "DISCONNECT (%s) as %s, devctl %02x\n",
767 otg_state_string(musb),
768 MUSB_MODE(musb), devctl);
769 handled = IRQ_HANDLED;
770
David Brownell84e250f2009-03-31 12:30:04 -0700771 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300772#ifdef CONFIG_USB_MUSB_HDRC_HCD
773 case OTG_STATE_A_HOST:
774 case OTG_STATE_A_SUSPEND:
Anand Gadiyar5c23c902009-02-21 15:31:40 -0800775 usb_hcd_resume_root_hub(musb_to_hcd(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300776 musb_root_disconnect(musb);
Ajay Kumar Gupta74382172009-02-24 15:29:04 -0800777 if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +0300778 musb_platform_try_idle(musb, jiffies
779 + msecs_to_jiffies(musb->a_wait_bcon));
780 break;
781#endif /* HOST */
782#ifdef CONFIG_USB_MUSB_OTG
783 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700784 /* REVISIT this behaves for "real disconnect"
785 * cases; make sure the other transitions from
786 * from B_HOST act right too. The B_HOST code
787 * in hnp_stop() is currently not used...
788 */
789 musb_root_disconnect(musb);
790 musb_to_hcd(musb)->self.is_b_host = 0;
791 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
792 MUSB_DEV_MODE(musb);
793 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300794 break;
795 case OTG_STATE_A_PERIPHERAL:
796 musb_hnp_stop(musb);
797 musb_root_disconnect(musb);
798 /* FALLTHROUGH */
799 case OTG_STATE_B_WAIT_ACON:
800 /* FALLTHROUGH */
801#endif /* OTG */
802#ifdef CONFIG_USB_GADGET_MUSB_HDRC
803 case OTG_STATE_B_PERIPHERAL:
804 case OTG_STATE_B_IDLE:
805 musb_g_disconnect(musb);
806 break;
807#endif /* GADGET */
808 default:
809 WARNING("unhandled DISCONNECT transition (%s)\n",
810 otg_state_string(musb));
811 break;
812 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300813 }
814
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200815 /* mentor saves a bit: bus reset and babble share the same irq.
816 * only host sees babble; only peripheral sees bus reset.
817 */
818 if (int_usb & MUSB_INTR_RESET) {
819 handled = IRQ_HANDLED;
820 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
821 /*
822 * Looks like non-HS BABBLE can be ignored, but
823 * HS BABBLE is an error condition. For HS the solution
824 * is to avoid babble in the first place and fix what
825 * caused BABBLE. When HS BABBLE happens we can only
826 * stop the session.
827 */
828 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
829 DBG(1, "BABBLE devctl: %02x\n", devctl);
830 else {
831 ERR("Stopping host session -- babble\n");
832 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
833 }
834 } else if (is_peripheral_capable()) {
835 DBG(1, "BUS RESET as %s\n", otg_state_string(musb));
836 switch (musb->xceiv->state) {
837#ifdef CONFIG_USB_OTG
838 case OTG_STATE_A_SUSPEND:
839 /* We need to ignore disconnect on suspend
840 * otherwise tusb 2.0 won't reconnect after a
841 * power cycle, which breaks otg compliance.
842 */
843 musb->ignore_disconnect = 1;
844 musb_g_reset(musb);
845 /* FALLTHROUGH */
846 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
847 /* never use invalid T(a_wait_bcon) */
848 DBG(1, "HNP: in %s, %d msec timeout\n",
849 otg_state_string(musb),
850 TA_WAIT_BCON(musb));
851 mod_timer(&musb->otg_timer, jiffies
852 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
853 break;
854 case OTG_STATE_A_PERIPHERAL:
855 musb->ignore_disconnect = 0;
856 del_timer(&musb->otg_timer);
857 musb_g_reset(musb);
858 break;
859 case OTG_STATE_B_WAIT_ACON:
860 DBG(1, "HNP: RESET (%s), to b_peripheral\n",
861 otg_state_string(musb));
862 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
863 musb_g_reset(musb);
864 break;
865#endif
866 case OTG_STATE_B_IDLE:
867 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
868 /* FALLTHROUGH */
869 case OTG_STATE_B_PERIPHERAL:
870 musb_g_reset(musb);
871 break;
872 default:
873 DBG(1, "Unhandled BUS RESET as %s\n",
874 otg_state_string(musb));
875 }
876 }
877 }
878
879#if 0
880/* REVISIT ... this would be for multiplexing periodic endpoints, or
881 * supporting transfer phasing to prevent exceeding ISO bandwidth
882 * limits of a given frame or microframe.
883 *
884 * It's not needed for peripheral side, which dedicates endpoints;
885 * though it _might_ use SOF irqs for other purposes.
886 *
887 * And it's not currently needed for host side, which also dedicates
888 * endpoints, relies on TX/RX interval registers, and isn't claimed
889 * to support ISO transfers yet.
890 */
891 if (int_usb & MUSB_INTR_SOF) {
892 void __iomem *mbase = musb->mregs;
893 struct musb_hw_ep *ep;
894 u8 epnum;
895 u16 frame;
896
897 DBG(6, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300898 handled = IRQ_HANDLED;
899
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200900 /* start any periodic Tx transfers waiting for current frame */
901 frame = musb_readw(mbase, MUSB_FRAME);
902 ep = musb->endpoints;
903 for (epnum = 1; (epnum < musb->nr_endpoints)
904 && (musb->epmask >= (1 << epnum));
905 epnum++, ep++) {
906 /*
907 * FIXME handle framecounter wraps (12 bits)
908 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300909 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200910 if (ep->dwWaitFrame >= frame) {
911 ep->dwWaitFrame = 0;
912 pr_debug("SOF --> periodic TX%s on %d\n",
913 ep->tx_channel ? " DMA" : "",
914 epnum);
915 if (!ep->tx_channel)
916 musb_h_tx_start(musb, epnum);
917 else
918 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300919 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200920 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300921 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200922#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300923
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200924 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300925
926 return handled;
927}
928
929/*-------------------------------------------------------------------------*/
930
931/*
932* Program the HDRC to start (enable interrupts, dma, etc.).
933*/
934void musb_start(struct musb *musb)
935{
936 void __iomem *regs = musb->mregs;
937 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
938
939 DBG(2, "<== devctl %02x\n", devctl);
940
941 /* Set INT enable registers, enable interrupts */
942 musb_writew(regs, MUSB_INTRTXE, musb->epmask);
943 musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
944 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
945
946 musb_writeb(regs, MUSB_TESTMODE, 0);
947
948 /* put into basic highspeed mode and start session */
949 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
950 | MUSB_POWER_SOFTCONN
951 | MUSB_POWER_HSENAB
952 /* ENSUSPEND wedges tusb */
953 /* | MUSB_POWER_ENSUSPEND */
954 );
955
956 musb->is_active = 0;
957 devctl = musb_readb(regs, MUSB_DEVCTL);
958 devctl &= ~MUSB_DEVCTL_SESSION;
959
960 if (is_otg_enabled(musb)) {
961 /* session started after:
962 * (a) ID-grounded irq, host mode;
963 * (b) vbus present/connect IRQ, peripheral mode;
964 * (c) peripheral initiates, using SRP
965 */
966 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
967 musb->is_active = 1;
968 else
969 devctl |= MUSB_DEVCTL_SESSION;
970
971 } else if (is_host_enabled(musb)) {
972 /* assume ID pin is hard-wired to ground */
973 devctl |= MUSB_DEVCTL_SESSION;
974
975 } else /* peripheral is enabled */ {
976 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
977 musb->is_active = 1;
978 }
979 musb_platform_enable(musb);
980 musb_writeb(regs, MUSB_DEVCTL, devctl);
981}
982
983
984static void musb_generic_disable(struct musb *musb)
985{
986 void __iomem *mbase = musb->mregs;
987 u16 temp;
988
989 /* disable interrupts */
990 musb_writeb(mbase, MUSB_INTRUSBE, 0);
991 musb_writew(mbase, MUSB_INTRTXE, 0);
992 musb_writew(mbase, MUSB_INTRRXE, 0);
993
994 /* off */
995 musb_writeb(mbase, MUSB_DEVCTL, 0);
996
997 /* flush pending interrupts */
998 temp = musb_readb(mbase, MUSB_INTRUSB);
999 temp = musb_readw(mbase, MUSB_INTRTX);
1000 temp = musb_readw(mbase, MUSB_INTRRX);
1001
1002}
1003
1004/*
1005 * Make the HDRC stop (disable interrupts, etc.);
1006 * reversible by musb_start
1007 * called on gadget driver unregister
1008 * with controller locked, irqs blocked
1009 * acts as a NOP unless some role activated the hardware
1010 */
1011void musb_stop(struct musb *musb)
1012{
1013 /* stop IRQs, timers, ... */
1014 musb_platform_disable(musb);
1015 musb_generic_disable(musb);
1016 DBG(3, "HDRC disabled\n");
1017
1018 /* FIXME
1019 * - mark host and/or peripheral drivers unusable/inactive
1020 * - disable DMA (and enable it in HdrcStart)
1021 * - make sure we can musb_start() after musb_stop(); with
1022 * OTG mode, gadget driver module rmmod/modprobe cycles that
1023 * - ...
1024 */
1025 musb_platform_try_idle(musb, 0);
1026}
1027
1028static void musb_shutdown(struct platform_device *pdev)
1029{
1030 struct musb *musb = dev_to_musb(&pdev->dev);
1031 unsigned long flags;
1032
Hema HK4f9edd22011-03-22 16:02:12 +05301033 pm_runtime_get_sync(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001034 spin_lock_irqsave(&musb->lock, flags);
1035 musb_platform_disable(musb);
1036 musb_generic_disable(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001037 spin_unlock_irqrestore(&musb->lock, flags);
1038
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001039 if (!is_otg_enabled(musb) && is_host_enabled(musb))
1040 usb_remove_hcd(musb_to_hcd(musb));
1041 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1042 musb_platform_exit(musb);
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001043
Hema HK4f9edd22011-03-22 16:02:12 +05301044 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001045 /* FIXME power down */
1046}
1047
1048
1049/*-------------------------------------------------------------------------*/
1050
1051/*
1052 * The silicon either has hard-wired endpoint configurations, or else
1053 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001054 * writing only the dynamic sizing is very well tested. Since we switched
1055 * away from compile-time hardware parameters, we can no longer rely on
1056 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001057 *
1058 * We don't currently use dynamic fifo setup capability to do anything
1059 * more than selecting one of a bunch of predefined configurations.
1060 */
Felipe Balbi7c925542010-12-01 14:23:48 +02001061#if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
1062 || defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +03001063static ushort __initdata fifo_mode = 4;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001064#elif defined(CONFIG_USB_MUSB_UX500)
1065static ushort __initdata fifo_mode = 5;
Felipe Balbi550a7372008-07-24 12:27:36 +03001066#else
1067static ushort __initdata fifo_mode = 2;
1068#endif
1069
1070/* "modprobe ... fifo_mode=1" etc */
1071module_param(fifo_mode, ushort, 0);
1072MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1073
Felipe Balbi550a7372008-07-24 12:27:36 +03001074/*
1075 * tables defining fifo_mode values. define more if you like.
1076 * for host side, make sure both halves of ep1 are set up.
1077 */
1078
1079/* mode 0 - fits in 2KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001080static struct musb_fifo_cfg __initdata mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001081{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1082{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1083{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1084{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1085{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1086};
1087
1088/* mode 1 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001089static struct musb_fifo_cfg __initdata mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001090{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1091{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1092{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1093{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1094{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1095};
1096
1097/* mode 2 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001098static struct musb_fifo_cfg __initdata mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001099{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1100{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1101{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1102{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1103{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1104{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1105};
1106
1107/* mode 3 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001108static struct musb_fifo_cfg __initdata mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001109{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1110{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1111{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1112{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1113{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1114{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1115};
1116
1117/* mode 4 - fits in 16KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001118static struct musb_fifo_cfg __initdata mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001119{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1120{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1121{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1122{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1123{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1124{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1125{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1126{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1127{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1128{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1129{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1130{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1131{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1132{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1133{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1134{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1135{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1136{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001137{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1138{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1139{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1140{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1141{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1142{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1143{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001144{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1145{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1146};
1147
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001148/* mode 5 - fits in 8KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001149static struct musb_fifo_cfg __initdata mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001150{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1151{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1152{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1153{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1154{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1155{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1156{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1157{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1158{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1159{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1160{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1161{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1162{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1163{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1164{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1165{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1166{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1167{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1168{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1169{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1170{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1171{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1172{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1173{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1174{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1175{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1176{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1177};
Felipe Balbi550a7372008-07-24 12:27:36 +03001178
1179/*
1180 * configure a fifo; for non-shared endpoints, this may be called
1181 * once for a tx fifo and once for an rx fifo.
1182 *
1183 * returns negative errno or offset for next fifo.
1184 */
1185static int __init
1186fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001187 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001188{
1189 void __iomem *mbase = musb->mregs;
1190 int size = 0;
1191 u16 maxpacket = cfg->maxpacket;
1192 u16 c_off = offset >> 3;
1193 u8 c_size;
1194
1195 /* expect hw_ep has already been zero-initialized */
1196
1197 size = ffs(max(maxpacket, (u16) 8)) - 1;
1198 maxpacket = 1 << size;
1199
1200 c_size = size - 3;
1201 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001202 if ((offset + (maxpacket << 1)) >
1203 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001204 return -EMSGSIZE;
1205 c_size |= MUSB_FIFOSZ_DPB;
1206 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001207 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001208 return -EMSGSIZE;
1209 }
1210
1211 /* configure the FIFO */
1212 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1213
1214#ifdef CONFIG_USB_MUSB_HDRC_HCD
1215 /* EP0 reserved endpoint for control, bidirectional;
1216 * EP1 reserved for bulk, two unidirection halves.
1217 */
1218 if (hw_ep->epnum == 1)
1219 musb->bulk_ep = hw_ep;
1220 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1221#endif
1222 switch (cfg->style) {
1223 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001224 musb_write_txfifosz(mbase, c_size);
1225 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001226 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1227 hw_ep->max_packet_sz_tx = maxpacket;
1228 break;
1229 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001230 musb_write_rxfifosz(mbase, c_size);
1231 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001232 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1233 hw_ep->max_packet_sz_rx = maxpacket;
1234 break;
1235 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001236 musb_write_txfifosz(mbase, c_size);
1237 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001238 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1239 hw_ep->max_packet_sz_rx = maxpacket;
1240
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001241 musb_write_rxfifosz(mbase, c_size);
1242 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001243 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1244 hw_ep->max_packet_sz_tx = maxpacket;
1245
1246 hw_ep->is_shared_fifo = true;
1247 break;
1248 }
1249
1250 /* NOTE rx and tx endpoint irqs aren't managed separately,
1251 * which happens to be ok
1252 */
1253 musb->epmask |= (1 << hw_ep->epnum);
1254
1255 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1256}
1257
Felipe Balbie6c213b2010-03-12 10:29:06 +02001258static struct musb_fifo_cfg __initdata ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001259 .style = FIFO_RXTX, .maxpacket = 64,
1260};
1261
1262static int __init ep_config_from_table(struct musb *musb)
1263{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001264 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001265 unsigned i, n;
1266 int offset;
1267 struct musb_hw_ep *hw_ep = musb->endpoints;
1268
Felipe Balbie6c213b2010-03-12 10:29:06 +02001269 if (musb->config->fifo_cfg) {
1270 cfg = musb->config->fifo_cfg;
1271 n = musb->config->fifo_cfg_size;
1272 goto done;
1273 }
1274
Felipe Balbi550a7372008-07-24 12:27:36 +03001275 switch (fifo_mode) {
1276 default:
1277 fifo_mode = 0;
1278 /* FALLTHROUGH */
1279 case 0:
1280 cfg = mode_0_cfg;
1281 n = ARRAY_SIZE(mode_0_cfg);
1282 break;
1283 case 1:
1284 cfg = mode_1_cfg;
1285 n = ARRAY_SIZE(mode_1_cfg);
1286 break;
1287 case 2:
1288 cfg = mode_2_cfg;
1289 n = ARRAY_SIZE(mode_2_cfg);
1290 break;
1291 case 3:
1292 cfg = mode_3_cfg;
1293 n = ARRAY_SIZE(mode_3_cfg);
1294 break;
1295 case 4:
1296 cfg = mode_4_cfg;
1297 n = ARRAY_SIZE(mode_4_cfg);
1298 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001299 case 5:
1300 cfg = mode_5_cfg;
1301 n = ARRAY_SIZE(mode_5_cfg);
1302 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001303 }
1304
1305 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1306 musb_driver_name, fifo_mode);
1307
1308
Felipe Balbie6c213b2010-03-12 10:29:06 +02001309done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001310 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1311 /* assert(offset > 0) */
1312
1313 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001314 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001315 */
1316
1317 for (i = 0; i < n; i++) {
1318 u8 epn = cfg->hw_ep_num;
1319
Felipe Balbica6d1b12008-08-08 12:40:54 +03001320 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001321 pr_debug("%s: invalid ep %d\n",
1322 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001323 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001324 }
1325 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1326 if (offset < 0) {
1327 pr_debug("%s: mem overrun, ep %d\n",
1328 musb_driver_name, epn);
1329 return -EINVAL;
1330 }
1331 epn++;
1332 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1333 }
1334
1335 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1336 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001337 n + 1, musb->config->num_eps * 2 - 1,
1338 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001339
1340#ifdef CONFIG_USB_MUSB_HDRC_HCD
1341 if (!musb->bulk_ep) {
1342 pr_debug("%s: missing bulk\n", musb_driver_name);
1343 return -EINVAL;
1344 }
1345#endif
1346
1347 return 0;
1348}
1349
1350
1351/*
1352 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1353 * @param musb the controller
1354 */
1355static int __init ep_config_from_hw(struct musb *musb)
1356{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001357 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001358 struct musb_hw_ep *hw_ep;
1359 void *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001360 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001361
1362 DBG(2, "<== static silicon ep config\n");
1363
1364 /* FIXME pick up ep0 maxpacket size */
1365
Felipe Balbica6d1b12008-08-08 12:40:54 +03001366 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001367 musb_ep_select(mbase, epnum);
1368 hw_ep = musb->endpoints + epnum;
1369
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001370 ret = musb_read_fifosize(musb, hw_ep, epnum);
1371 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001372 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001373
1374 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1375
1376#ifdef CONFIG_USB_MUSB_HDRC_HCD
1377 /* pick an RX/TX endpoint for bulk */
1378 if (hw_ep->max_packet_sz_tx < 512
1379 || hw_ep->max_packet_sz_rx < 512)
1380 continue;
1381
1382 /* REVISIT: this algorithm is lazy, we should at least
1383 * try to pick a double buffered endpoint.
1384 */
1385 if (musb->bulk_ep)
1386 continue;
1387 musb->bulk_ep = hw_ep;
1388#endif
1389 }
1390
1391#ifdef CONFIG_USB_MUSB_HDRC_HCD
1392 if (!musb->bulk_ep) {
1393 pr_debug("%s: missing bulk\n", musb_driver_name);
1394 return -EINVAL;
1395 }
1396#endif
1397
1398 return 0;
1399}
1400
1401enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1402
1403/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1404 * configure endpoints, or take their config from silicon
1405 */
1406static int __init musb_core_init(u16 musb_type, struct musb *musb)
1407{
Felipe Balbi550a7372008-07-24 12:27:36 +03001408 u8 reg;
1409 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301410 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001411 void __iomem *mbase = musb->mregs;
1412 int status = 0;
1413 int i;
1414
1415 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001416 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001417
1418 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001419 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001420 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001421 musb->dyn_fifo = true;
1422 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001423 if (reg & MUSB_CONFIGDATA_MPRXE) {
1424 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001425 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001426 }
1427 if (reg & MUSB_CONFIGDATA_MPTXE) {
1428 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001429 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001430 }
1431 if (reg & MUSB_CONFIGDATA_HBRXE) {
1432 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001433 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001434 }
1435 if (reg & MUSB_CONFIGDATA_HBTXE) {
1436 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001437 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001438 }
1439 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1440 strcat(aInfo, ", SoftConn");
1441
1442 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1443 musb_driver_name, reg, aInfo);
1444
Felipe Balbi550a7372008-07-24 12:27:36 +03001445 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001446 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1447 musb->is_multipoint = 1;
1448 type = "M";
1449 } else {
1450 musb->is_multipoint = 0;
1451 type = "";
1452#ifdef CONFIG_USB_MUSB_HDRC_HCD
1453#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1454 printk(KERN_ERR
1455 "%s: kernel must blacklist external hubs\n",
1456 musb_driver_name);
1457#endif
1458#endif
1459 }
1460
1461 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301462 musb->hwvers = musb_read_hwvers(mbase);
1463 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1464 MUSB_HWVERS_MINOR(musb->hwvers),
1465 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001466 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1467 musb_driver_name, type, aRevision, aDate);
1468
1469 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001470 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001471
1472 /* discover endpoint configuration */
1473 musb->nr_endpoints = 1;
1474 musb->epmask = 1;
1475
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001476 if (musb->dyn_fifo)
1477 status = ep_config_from_table(musb);
1478 else
1479 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001480
1481 if (status < 0)
1482 return status;
1483
1484 /* finish init, and print endpoint config */
1485 for (i = 0; i < musb->nr_endpoints; i++) {
1486 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1487
1488 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
Felipe Balbi7c925542010-12-01 14:23:48 +02001489#ifdef CONFIG_USB_MUSB_TUSB6010
Felipe Balbi550a7372008-07-24 12:27:36 +03001490 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1491 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1492 hw_ep->fifo_sync_va =
1493 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1494
1495 if (i == 0)
1496 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1497 else
1498 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1499#endif
1500
1501 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1502#ifdef CONFIG_USB_MUSB_HDRC_HCD
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001503 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001504 hw_ep->rx_reinit = 1;
1505 hw_ep->tx_reinit = 1;
1506#endif
1507
1508 if (hw_ep->max_packet_sz_tx) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301509 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001510 "%s: hw_ep %d%s, %smax %d\n",
1511 musb_driver_name, i,
1512 hw_ep->is_shared_fifo ? "shared" : "tx",
1513 hw_ep->tx_double_buffered
1514 ? "doublebuffer, " : "",
1515 hw_ep->max_packet_sz_tx);
1516 }
1517 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301518 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001519 "%s: hw_ep %d%s, %smax %d\n",
1520 musb_driver_name, i,
1521 "rx",
1522 hw_ep->rx_double_buffered
1523 ? "doublebuffer, " : "",
1524 hw_ep->max_packet_sz_rx);
1525 }
1526 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1527 DBG(1, "hw_ep %d not configured\n", i);
1528 }
1529
1530 return 0;
1531}
1532
1533/*-------------------------------------------------------------------------*/
1534
Tony Lindgren59b479e2011-01-27 16:39:40 -08001535#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001536 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \
1537 defined(CONFIG_ARCH_U5500)
Felipe Balbi550a7372008-07-24 12:27:36 +03001538
1539static irqreturn_t generic_interrupt(int irq, void *__hci)
1540{
1541 unsigned long flags;
1542 irqreturn_t retval = IRQ_NONE;
1543 struct musb *musb = __hci;
1544
1545 spin_lock_irqsave(&musb->lock, flags);
1546
1547 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1548 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1549 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1550
1551 if (musb->int_usb || musb->int_tx || musb->int_rx)
1552 retval = musb_interrupt(musb);
1553
1554 spin_unlock_irqrestore(&musb->lock, flags);
1555
Sergei Shtylyova5073b52009-03-27 12:52:43 -07001556 return retval;
Felipe Balbi550a7372008-07-24 12:27:36 +03001557}
1558
1559#else
1560#define generic_interrupt NULL
1561#endif
1562
1563/*
1564 * handle all the irqs defined by the HDRC core. for now we expect: other
1565 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1566 * will be assigned, and the irq will already have been acked.
1567 *
1568 * called in irq context with spinlock held, irqs blocked
1569 */
1570irqreturn_t musb_interrupt(struct musb *musb)
1571{
1572 irqreturn_t retval = IRQ_NONE;
1573 u8 devctl, power;
1574 int ep_num;
1575 u32 reg;
1576
1577 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1578 power = musb_readb(musb->mregs, MUSB_POWER);
1579
1580 DBG(4, "** IRQ %s usb%04x tx%04x rx%04x\n",
1581 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1582 musb->int_usb, musb->int_tx, musb->int_rx);
1583
Felipe Balbicd42fef2009-12-15 13:47:30 +02001584#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1585 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
1586 if (!musb->gadget_driver) {
1587 DBG(5, "No gadget driver loaded\n");
1588 return IRQ_HANDLED;
1589 }
1590#endif
1591
Felipe Balbi550a7372008-07-24 12:27:36 +03001592 /* the core can interrupt us for multiple reasons; docs have
1593 * a generic interrupt flowchart to follow
1594 */
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301595 if (musb->int_usb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001596 retval |= musb_stage0_irq(musb, musb->int_usb,
1597 devctl, power);
1598
1599 /* "stage 1" is handling endpoint irqs */
1600
1601 /* handle endpoint 0 first */
1602 if (musb->int_tx & 1) {
1603 if (devctl & MUSB_DEVCTL_HM)
1604 retval |= musb_h_ep0_irq(musb);
1605 else
1606 retval |= musb_g_ep0_irq(musb);
1607 }
1608
1609 /* RX on endpoints 1-15 */
1610 reg = musb->int_rx >> 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->rx_irq(...) */
1616 retval = IRQ_HANDLED;
1617 if (devctl & MUSB_DEVCTL_HM) {
1618 if (is_host_capable())
1619 musb_host_rx(musb, ep_num);
1620 } else {
1621 if (is_peripheral_capable())
1622 musb_g_rx(musb, ep_num);
1623 }
1624 }
1625
1626 reg >>= 1;
1627 ep_num++;
1628 }
1629
1630 /* TX on endpoints 1-15 */
1631 reg = musb->int_tx >> 1;
1632 ep_num = 1;
1633 while (reg) {
1634 if (reg & 1) {
1635 /* musb_ep_select(musb->mregs, ep_num); */
1636 /* REVISIT just retval |= ep->tx_irq(...) */
1637 retval = IRQ_HANDLED;
1638 if (devctl & MUSB_DEVCTL_HM) {
1639 if (is_host_capable())
1640 musb_host_tx(musb, ep_num);
1641 } else {
1642 if (is_peripheral_capable())
1643 musb_g_tx(musb, ep_num);
1644 }
1645 }
1646 reg >>= 1;
1647 ep_num++;
1648 }
1649
Felipe Balbi550a7372008-07-24 12:27:36 +03001650 return retval;
1651}
1652
1653
1654#ifndef CONFIG_MUSB_PIO_ONLY
1655static int __initdata use_dma = 1;
1656
1657/* "modprobe ... use_dma=0" etc */
1658module_param(use_dma, bool, 0);
1659MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1660
1661void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1662{
1663 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1664
1665 /* called with controller lock already held */
1666
1667 if (!epnum) {
1668#ifndef CONFIG_USB_TUSB_OMAP_DMA
1669 if (!is_cppi_enabled()) {
1670 /* endpoint 0 */
1671 if (devctl & MUSB_DEVCTL_HM)
1672 musb_h_ep0_irq(musb);
1673 else
1674 musb_g_ep0_irq(musb);
1675 }
1676#endif
1677 } else {
1678 /* endpoints 1..15 */
1679 if (transmit) {
1680 if (devctl & MUSB_DEVCTL_HM) {
1681 if (is_host_capable())
1682 musb_host_tx(musb, epnum);
1683 } else {
1684 if (is_peripheral_capable())
1685 musb_g_tx(musb, epnum);
1686 }
1687 } else {
1688 /* receive */
1689 if (devctl & MUSB_DEVCTL_HM) {
1690 if (is_host_capable())
1691 musb_host_rx(musb, epnum);
1692 } else {
1693 if (is_peripheral_capable())
1694 musb_g_rx(musb, epnum);
1695 }
1696 }
1697 }
1698}
1699
1700#else
1701#define use_dma 0
1702#endif
1703
1704/*-------------------------------------------------------------------------*/
1705
1706#ifdef CONFIG_SYSFS
1707
1708static ssize_t
1709musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1710{
1711 struct musb *musb = dev_to_musb(dev);
1712 unsigned long flags;
1713 int ret = -EINVAL;
1714
1715 spin_lock_irqsave(&musb->lock, flags);
1716 ret = sprintf(buf, "%s\n", otg_state_string(musb));
1717 spin_unlock_irqrestore(&musb->lock, flags);
1718
1719 return ret;
1720}
1721
1722static ssize_t
1723musb_mode_store(struct device *dev, struct device_attribute *attr,
1724 const char *buf, size_t n)
1725{
1726 struct musb *musb = dev_to_musb(dev);
1727 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001728 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001729
1730 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001731 if (sysfs_streq(buf, "host"))
1732 status = musb_platform_set_mode(musb, MUSB_HOST);
1733 else if (sysfs_streq(buf, "peripheral"))
1734 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1735 else if (sysfs_streq(buf, "otg"))
1736 status = musb_platform_set_mode(musb, MUSB_OTG);
1737 else
1738 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001739 spin_unlock_irqrestore(&musb->lock, flags);
1740
David Brownell96a274d2008-11-24 13:06:47 +02001741 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001742}
1743static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1744
1745static ssize_t
1746musb_vbus_store(struct device *dev, struct device_attribute *attr,
1747 const char *buf, size_t n)
1748{
1749 struct musb *musb = dev_to_musb(dev);
1750 unsigned long flags;
1751 unsigned long val;
1752
1753 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001754 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001755 return -EINVAL;
1756 }
1757
1758 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001759 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1760 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001761 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001762 musb->is_active = 0;
1763 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1764 spin_unlock_irqrestore(&musb->lock, flags);
1765
1766 return n;
1767}
1768
1769static ssize_t
1770musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1771{
1772 struct musb *musb = dev_to_musb(dev);
1773 unsigned long flags;
1774 unsigned long val;
1775 int vbus;
1776
1777 spin_lock_irqsave(&musb->lock, flags);
1778 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001779 /* FIXME get_vbus_status() is normally #defined as false...
1780 * and is effectively TUSB-specific.
1781 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001782 vbus = musb_platform_get_vbus_status(musb);
1783 spin_unlock_irqrestore(&musb->lock, flags);
1784
David Brownellf7f9d632009-03-31 12:32:12 -07001785 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001786 vbus ? "on" : "off", val);
1787}
1788static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1789
1790#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1791
1792/* Gadget drivers can't know that a host is connected so they might want
1793 * to start SRP, but users can. This allows userspace to trigger SRP.
1794 */
1795static ssize_t
1796musb_srp_store(struct device *dev, struct device_attribute *attr,
1797 const char *buf, size_t n)
1798{
1799 struct musb *musb = dev_to_musb(dev);
1800 unsigned short srp;
1801
1802 if (sscanf(buf, "%hu", &srp) != 1
1803 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001804 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001805 return -EINVAL;
1806 }
1807
1808 if (srp == 1)
1809 musb_g_wakeup(musb);
1810
1811 return n;
1812}
1813static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1814
1815#endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1816
Felipe Balbi94375752009-12-15 11:08:38 +02001817static struct attribute *musb_attributes[] = {
1818 &dev_attr_mode.attr,
1819 &dev_attr_vbus.attr,
1820#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1821 &dev_attr_srp.attr,
1822#endif
1823 NULL
1824};
1825
1826static const struct attribute_group musb_attr_group = {
1827 .attrs = musb_attributes,
1828};
1829
Felipe Balbi550a7372008-07-24 12:27:36 +03001830#endif /* sysfs */
1831
1832/* Only used to provide driver mode change events */
1833static void musb_irq_work(struct work_struct *data)
1834{
1835 struct musb *musb = container_of(data, struct musb, irq_work);
1836 static int old_state;
1837
David Brownell84e250f2009-03-31 12:30:04 -07001838 if (musb->xceiv->state != old_state) {
1839 old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001840 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1841 }
1842}
1843
1844/* --------------------------------------------------------------------------
1845 * Init support
1846 */
1847
1848static struct musb *__init
Felipe Balbica6d1b12008-08-08 12:40:54 +03001849allocate_instance(struct device *dev,
1850 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001851{
1852 struct musb *musb;
1853 struct musb_hw_ep *ep;
1854 int epnum;
1855#ifdef CONFIG_USB_MUSB_HDRC_HCD
1856 struct usb_hcd *hcd;
1857
Kay Sievers427c4f32008-11-07 01:52:53 +01001858 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
Felipe Balbi550a7372008-07-24 12:27:36 +03001859 if (!hcd)
1860 return NULL;
1861 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1862
1863 musb = hcd_to_musb(hcd);
1864 INIT_LIST_HEAD(&musb->control);
1865 INIT_LIST_HEAD(&musb->in_bulk);
1866 INIT_LIST_HEAD(&musb->out_bulk);
1867
1868 hcd->uses_new_polling = 1;
Felipe Balbiec95d352011-02-24 10:36:53 +02001869 hcd->has_tt = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001870
1871 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001872 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001873#else
1874 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1875 if (!musb)
1876 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001877
1878#endif
Ming Lei456bb162010-12-21 21:16:11 +08001879 dev_set_drvdata(dev, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001880 musb->mregs = mbase;
1881 musb->ctrl_base = mbase;
1882 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001883 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001884 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001885 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001886 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001887 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001888 ep->musb = musb;
1889 ep->epnum = epnum;
1890 }
1891
1892 musb->controller = dev;
Felipe Balbi743411b2010-12-01 13:22:05 +02001893
Felipe Balbi550a7372008-07-24 12:27:36 +03001894 return musb;
1895}
1896
1897static void musb_free(struct musb *musb)
1898{
1899 /* this has multiple entry modes. it handles fault cleanup after
1900 * probe(), where things may be partially set up, as well as rmmod
1901 * cleanup after everything's been de-activated.
1902 */
1903
1904#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001905 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001906#endif
1907
1908#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1909 musb_gadget_cleanup(musb);
1910#endif
1911
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001912 if (musb->nIrq >= 0) {
1913 if (musb->irq_wake)
1914 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001915 free_irq(musb->nIrq, musb);
1916 }
1917 if (is_dma_capable() && musb->dma_controller) {
1918 struct dma_controller *c = musb->dma_controller;
1919
1920 (void) c->stop(c);
1921 dma_controller_destroy(c);
1922 }
1923
Felipe Balbi550a7372008-07-24 12:27:36 +03001924#ifdef CONFIG_USB_MUSB_HDRC_HCD
1925 usb_put_hcd(musb_to_hcd(musb));
1926#else
1927 kfree(musb);
1928#endif
1929}
1930
1931/*
1932 * Perform generic per-controller initialization.
1933 *
1934 * @pDevice: the controller (already clocked, etc)
1935 * @nIrq: irq
1936 * @mregs: virtual address of controller registers,
1937 * not yet corrected for platform-specific offsets
1938 */
1939static int __init
1940musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1941{
1942 int status;
1943 struct musb *musb;
1944 struct musb_hdrc_platform_data *plat = dev->platform_data;
1945
1946 /* The driver might handle more features than the board; OK.
1947 * Fail when the board needs a feature that's not enabled.
1948 */
1949 if (!plat) {
1950 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001951 status = -ENODEV;
1952 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001953 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001954
Felipe Balbi550a7372008-07-24 12:27:36 +03001955 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001956 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001957 if (!musb) {
1958 status = -ENOMEM;
1959 goto fail0;
1960 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001961
Hema HK7acc6192011-02-28 14:19:34 +05301962 pm_runtime_use_autosuspend(musb->controller);
1963 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1964 pm_runtime_enable(musb->controller);
1965
Felipe Balbi550a7372008-07-24 12:27:36 +03001966 spin_lock_init(&musb->lock);
1967 musb->board_mode = plat->mode;
1968 musb->board_set_power = plat->set_power;
Felipe Balbi550a7372008-07-24 12:27:36 +03001969 musb->min_power = plat->min_power;
Felipe Balbif7ec9432010-12-02 09:48:58 +02001970 musb->ops = plat->platform_ops;
Felipe Balbi550a7372008-07-24 12:27:36 +03001971
David Brownell84e250f2009-03-31 12:30:04 -07001972 /* The musb_platform_init() call:
1973 * - adjusts musb->mregs and musb->isr if needed,
1974 * - may initialize an integrated tranceiver
1975 * - initializes musb->xceiv, usually by otg_get_transceiver()
David Brownell84e250f2009-03-31 12:30:04 -07001976 * - stops powering VBUS
David Brownell84e250f2009-03-31 12:30:04 -07001977 *
1978 * There are various transciever configurations. Blackfin,
1979 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1980 * external/discrete ones in various flavors (twl4030 family,
1981 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03001982 */
1983 musb->isr = generic_interrupt;
Hema Kalliguddiea65df52010-09-22 19:27:40 -05001984 status = musb_platform_init(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001985 if (status < 0)
Felipe Balbi03491762010-12-02 09:57:08 +02001986 goto fail1;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001987
Felipe Balbi550a7372008-07-24 12:27:36 +03001988 if (!musb->isr) {
1989 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001990 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001991 }
1992
Heikki Krogerusffb865b2010-03-25 13:25:28 +02001993 if (!musb->xceiv->io_ops) {
1994 musb->xceiv->io_priv = musb->mregs;
1995 musb->xceiv->io_ops = &musb_ulpi_access;
1996 }
1997
Felipe Balbi550a7372008-07-24 12:27:36 +03001998#ifndef CONFIG_MUSB_PIO_ONLY
1999 if (use_dma && dev->dma_mask) {
2000 struct dma_controller *c;
2001
2002 c = dma_controller_create(musb, musb->mregs);
2003 musb->dma_controller = c;
2004 if (c)
2005 (void) c->start(c);
2006 }
2007#endif
2008 /* ideally this would be abstracted in platform setup */
2009 if (!is_dma_capable() || !musb->dma_controller)
2010 dev->dma_mask = NULL;
2011
2012 /* be sure interrupts are disabled before connecting ISR */
2013 musb_platform_disable(musb);
2014 musb_generic_disable(musb);
2015
2016 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03002017 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03002018 ? MUSB_CONTROLLER_MHDRC
2019 : MUSB_CONTROLLER_HDRC, musb);
2020 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002021 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002022
Amit Kucheria3a9f5bd2009-07-27 12:03:19 +03002023#ifdef CONFIG_USB_MUSB_OTG
David Brownellf7f9d632009-03-31 12:32:12 -07002024 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2025#endif
2026
Felipe Balbi550a7372008-07-24 12:27:36 +03002027 /* Init IRQ workqueue before request_irq */
2028 INIT_WORK(&musb->irq_work, musb_irq_work);
2029
2030 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002031 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002032 dev_err(dev, "request_irq %d failed!\n", nIrq);
2033 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002034 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002035 }
2036 musb->nIrq = nIrq;
2037/* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002038 if (enable_irq_wake(nIrq) == 0) {
2039 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002040 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002041 } else {
2042 musb->irq_wake = 0;
2043 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002044
David Brownell84e250f2009-03-31 12:30:04 -07002045 /* host side needs more setup */
2046 if (is_host_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002047 struct usb_hcd *hcd = musb_to_hcd(musb);
2048
David Brownell84e250f2009-03-31 12:30:04 -07002049 otg_set_host(musb->xceiv, &hcd->self);
2050
2051 if (is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002052 hcd->self.otg_port = 1;
David Brownell84e250f2009-03-31 12:30:04 -07002053 musb->xceiv->host = &hcd->self;
Felipe Balbi550a7372008-07-24 12:27:36 +03002054 hcd->power_budget = 2 * (plat->power ? : 250);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002055
2056 /* program PHY to use external vBus if required */
2057 if (plat->extvbus) {
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002058 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002059 busctl |= MUSB_ULPI_USE_EXTVBUS;
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002060 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002061 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002062 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002063
2064 /* For the host-only role, we can activate right away.
2065 * (We expect the ID pin to be forcibly grounded!!)
2066 * Otherwise, wait till the gadget driver hooks up.
2067 */
2068 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302069 struct usb_hcd *hcd = musb_to_hcd(musb);
2070
Felipe Balbi550a7372008-07-24 12:27:36 +03002071 MUSB_HST_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002072 musb->xceiv->default_a = 1;
2073 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002074
2075 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2076
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302077 hcd->self.uses_pio_for_control = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002078 DBG(1, "%s mode, status %d, devctl %02x %c\n",
2079 "HOST", status,
2080 musb_readb(musb->mregs, MUSB_DEVCTL),
2081 (musb_readb(musb->mregs, MUSB_DEVCTL)
2082 & MUSB_DEVCTL_BDEVICE
2083 ? 'B' : 'A'));
2084
2085 } else /* peripheral is enabled */ {
2086 MUSB_DEV_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002087 musb->xceiv->default_a = 0;
2088 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002089
2090 status = musb_gadget_setup(musb);
2091
2092 DBG(1, "%s mode, status %d, dev%02x\n",
2093 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2094 status,
2095 musb_readb(musb->mregs, MUSB_DEVCTL));
2096
2097 }
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002098 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002099 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002100
Hema HK7acc6192011-02-28 14:19:34 +05302101 pm_runtime_put(musb->controller);
2102
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002103 status = musb_init_debugfs(musb);
2104 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002105 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002106
Felipe Balbi550a7372008-07-24 12:27:36 +03002107#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02002108 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002109 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002110 goto fail5;
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002111#endif
Felipe Balbi28c2c512008-09-11 11:53:25 +03002112
Felipe Balbiab3bbfa2010-01-21 15:33:58 +02002113 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
2114 ({char *s;
2115 switch (musb->board_mode) {
2116 case MUSB_HOST: s = "Host"; break;
2117 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2118 default: s = "OTG"; break;
2119 }; s; }),
2120 ctrl,
2121 (is_dma_capable() && musb->dma_controller)
2122 ? "DMA" : "PIO",
2123 musb->nIrq);
2124
Felipe Balbi28c2c512008-09-11 11:53:25 +03002125 return 0;
2126
Felipe Balbib0f9da72010-03-25 13:25:18 +02002127fail5:
2128 musb_exit_debugfs(musb);
2129
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002130fail4:
2131 if (!is_otg_enabled(musb) && is_host_enabled(musb))
2132 usb_remove_hcd(musb_to_hcd(musb));
2133 else
2134 musb_gadget_cleanup(musb);
2135
2136fail3:
2137 if (musb->irq_wake)
2138 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002139 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002140
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002141fail1:
Felipe Balbi28c2c512008-09-11 11:53:25 +03002142 dev_err(musb->controller,
2143 "musb_init_controller failed with status %d\n", status);
2144
Felipe Balbi28c2c512008-09-11 11:53:25 +03002145 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002146
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002147fail0:
2148
Felipe Balbi550a7372008-07-24 12:27:36 +03002149 return status;
2150
Felipe Balbi550a7372008-07-24 12:27:36 +03002151}
2152
2153/*-------------------------------------------------------------------------*/
2154
2155/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2156 * bridge to a platform device; this driver then suffices.
2157 */
2158
2159#ifndef CONFIG_MUSB_PIO_ONLY
2160static u64 *orig_dma_mask;
2161#endif
2162
2163static int __init musb_probe(struct platform_device *pdev)
2164{
2165 struct device *dev = &pdev->dev;
Hema Kalliguddifcf173e2010-09-29 11:26:39 -05002166 int irq = platform_get_irq_byname(pdev, "mc");
Felipe Balbida5108e2010-01-21 15:33:57 +02002167 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002168 struct resource *iomem;
2169 void __iomem *base;
2170
2171 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sergei Shtylyov541079d2010-12-10 21:03:29 +03002172 if (!iomem || irq <= 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03002173 return -ENODEV;
2174
Felipe Balbi195e9e42009-12-15 11:08:42 +02002175 base = ioremap(iomem->start, resource_size(iomem));
Felipe Balbi550a7372008-07-24 12:27:36 +03002176 if (!base) {
2177 dev_err(dev, "ioremap failed\n");
2178 return -ENOMEM;
2179 }
2180
2181#ifndef CONFIG_MUSB_PIO_ONLY
2182 /* clobbered by use_dma=n */
2183 orig_dma_mask = dev->dma_mask;
2184#endif
Felipe Balbida5108e2010-01-21 15:33:57 +02002185 status = musb_init_controller(dev, irq, base);
2186 if (status < 0)
2187 iounmap(base);
2188
2189 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002190}
2191
Felipe Balbie3060b12009-12-15 11:08:41 +02002192static int __exit musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002193{
2194 struct musb *musb = dev_to_musb(&pdev->dev);
2195 void __iomem *ctrl_base = musb->ctrl_base;
2196
2197 /* this gets called on rmmod.
2198 * - Host mode: host may still be active
2199 * - Peripheral mode: peripheral is deactivated (or never-activated)
2200 * - OTG mode: both roles are deactivated (or never-activated)
2201 */
Hema HK7acc6192011-02-28 14:19:34 +05302202 pm_runtime_get_sync(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002203 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002204 musb_shutdown(pdev);
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002205
Hema HK7acc6192011-02-28 14:19:34 +05302206 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03002207 musb_free(musb);
2208 iounmap(ctrl_base);
2209 device_init_wakeup(&pdev->dev, 0);
2210#ifndef CONFIG_MUSB_PIO_ONLY
2211 pdev->dev.dma_mask = orig_dma_mask;
2212#endif
2213 return 0;
2214}
2215
2216#ifdef CONFIG_PM
2217
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002218static void musb_save_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002219{
2220 int i;
2221 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002222 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002223
2224 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002225 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2226 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2227 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002228 }
Felipe Balbi74211072010-12-01 13:53:27 +02002229 musb->context.power = musb_readb(musb_base, MUSB_POWER);
2230 musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
2231 musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
2232 musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2233 musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2234 musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002235
Bob Liuae9b2ad2010-09-24 13:44:07 +03002236 for (i = 0; i < musb->config->num_eps; ++i) {
2237 epio = musb->endpoints[i].regs;
Felipe Balbi74211072010-12-01 13:53:27 +02002238 musb->context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002239 musb_readw(epio, MUSB_TXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002240 musb->context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002241 musb_readw(epio, MUSB_TXCSR);
Felipe Balbi74211072010-12-01 13:53:27 +02002242 musb->context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002243 musb_readw(epio, MUSB_RXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002244 musb->context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002245 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002246
2247 if (musb->dyn_fifo) {
Felipe Balbi74211072010-12-01 13:53:27 +02002248 musb->context.index_regs[i].txfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002249 musb_read_txfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002250 musb->context.index_regs[i].rxfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002251 musb_read_rxfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002252 musb->context.index_regs[i].txfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002253 musb_read_txfifosz(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002254 musb->context.index_regs[i].rxfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002255 musb_read_rxfifosz(musb_base);
2256 }
2257 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002258 musb->context.index_regs[i].txtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002259 musb_readb(epio, MUSB_TXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002260 musb->context.index_regs[i].txinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002261 musb_readb(epio, MUSB_TXINTERVAL);
Felipe Balbi74211072010-12-01 13:53:27 +02002262 musb->context.index_regs[i].rxtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002263 musb_readb(epio, MUSB_RXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002264 musb->context.index_regs[i].rxinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002265 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002266
Felipe Balbi74211072010-12-01 13:53:27 +02002267 musb->context.index_regs[i].txfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002268 musb_read_txfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002269 musb->context.index_regs[i].txhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002270 musb_read_txhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002271 musb->context.index_regs[i].txhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002272 musb_read_txhubport(musb_base, i);
2273
Felipe Balbi74211072010-12-01 13:53:27 +02002274 musb->context.index_regs[i].rxfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002275 musb_read_rxfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002276 musb->context.index_regs[i].rxhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002277 musb_read_rxhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002278 musb->context.index_regs[i].rxhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002279 musb_read_rxhubport(musb_base, i);
2280 }
2281 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002282}
2283
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002284static void musb_restore_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002285{
2286 int i;
2287 void __iomem *musb_base = musb->mregs;
2288 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002289 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002290
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002291 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002292 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2293 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2294 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002295 }
Felipe Balbi74211072010-12-01 13:53:27 +02002296 musb_writeb(musb_base, MUSB_POWER, musb->context.power);
2297 musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
2298 musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
2299 musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2300 musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002301
Bob Liuae9b2ad2010-09-24 13:44:07 +03002302 for (i = 0; i < musb->config->num_eps; ++i) {
2303 epio = musb->endpoints[i].regs;
2304 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002305 musb->context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002306 musb_writew(epio, MUSB_TXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002307 musb->context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002308 musb_writew(epio, MUSB_RXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002309 musb->context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002310 musb_writew(epio, MUSB_RXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002311 musb->context.index_regs[i].rxcsr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002312
2313 if (musb->dyn_fifo) {
2314 musb_write_txfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002315 musb->context.index_regs[i].txfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002316 musb_write_rxfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002317 musb->context.index_regs[i].rxfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002318 musb_write_txfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002319 musb->context.index_regs[i].txfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002320 musb_write_rxfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002321 musb->context.index_regs[i].rxfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002322 }
2323
2324 if (is_host_enabled(musb)) {
Bob Liuae9b2ad2010-09-24 13:44:07 +03002325 musb_writeb(epio, MUSB_TXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002326 musb->context.index_regs[i].txtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002327 musb_writeb(epio, MUSB_TXINTERVAL,
Felipe Balbi74211072010-12-01 13:53:27 +02002328 musb->context.index_regs[i].txinterval);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002329 musb_writeb(epio, MUSB_RXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002330 musb->context.index_regs[i].rxtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002331 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002332
Felipe Balbi74211072010-12-01 13:53:27 +02002333 musb->context.index_regs[i].rxinterval);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002334 musb_write_txfunaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002335 musb->context.index_regs[i].txfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002336 musb_write_txhubaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002337 musb->context.index_regs[i].txhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002338 musb_write_txhubport(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002339 musb->context.index_regs[i].txhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002340
2341 ep_target_regs =
2342 musb_read_target_reg_base(i, musb_base);
2343
2344 musb_write_rxfunaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002345 musb->context.index_regs[i].rxfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002346 musb_write_rxhubaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002347 musb->context.index_regs[i].rxhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002348 musb_write_rxhubport(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002349 musb->context.index_regs[i].rxhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002350 }
2351 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002352}
2353
Magnus Damm48fea962009-07-08 13:22:56 +02002354static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002355{
Magnus Damm48fea962009-07-08 13:22:56 +02002356 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002357 unsigned long flags;
2358 struct musb *musb = dev_to_musb(&pdev->dev);
2359
Felipe Balbi550a7372008-07-24 12:27:36 +03002360 spin_lock_irqsave(&musb->lock, flags);
2361
2362 if (is_peripheral_active(musb)) {
2363 /* FIXME force disconnect unless we know USB will wake
2364 * the system up quickly enough to respond ...
2365 */
2366 } else if (is_host_active(musb)) {
2367 /* we know all the children are suspended; sometimes
2368 * they will even be wakeup-enabled.
2369 */
2370 }
2371
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002372 musb_save_context(musb);
2373
Felipe Balbi550a7372008-07-24 12:27:36 +03002374 spin_unlock_irqrestore(&musb->lock, flags);
2375 return 0;
2376}
2377
Magnus Damm48fea962009-07-08 13:22:56 +02002378static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002379{
Magnus Damm48fea962009-07-08 13:22:56 +02002380 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002381 struct musb *musb = dev_to_musb(&pdev->dev);
2382
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002383 musb_restore_context(musb);
2384
Felipe Balbi550a7372008-07-24 12:27:36 +03002385 /* for static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002386 * unless for some reason the whole soc powered down or the USB
2387 * module got reset through the PSC (vs just being disabled).
Felipe Balbi550a7372008-07-24 12:27:36 +03002388 */
Felipe Balbi550a7372008-07-24 12:27:36 +03002389 return 0;
2390}
2391
Hema HK7acc6192011-02-28 14:19:34 +05302392static int musb_runtime_suspend(struct device *dev)
2393{
2394 struct musb *musb = dev_to_musb(dev);
2395
2396 musb_save_context(musb);
2397
2398 return 0;
2399}
2400
2401static int musb_runtime_resume(struct device *dev)
2402{
2403 struct musb *musb = dev_to_musb(dev);
2404 static int first = 1;
2405
2406 /*
2407 * When pm_runtime_get_sync called for the first time in driver
2408 * init, some of the structure is still not initialized which is
2409 * used in restore function. But clock needs to be
2410 * enabled before any register access, so
2411 * pm_runtime_get_sync has to be called.
2412 * Also context restore without save does not make
2413 * any sense
2414 */
2415 if (!first)
2416 musb_restore_context(musb);
2417 first = 0;
2418
2419 return 0;
2420}
2421
Alexey Dobriyan47145212009-12-14 18:00:08 -08002422static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002423 .suspend = musb_suspend,
2424 .resume_noirq = musb_resume_noirq,
Hema HK7acc6192011-02-28 14:19:34 +05302425 .runtime_suspend = musb_runtime_suspend,
2426 .runtime_resume = musb_runtime_resume,
Magnus Damm48fea962009-07-08 13:22:56 +02002427};
2428
2429#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002430#else
Magnus Damm48fea962009-07-08 13:22:56 +02002431#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002432#endif
2433
2434static struct platform_driver musb_driver = {
2435 .driver = {
2436 .name = (char *)musb_driver_name,
2437 .bus = &platform_bus_type,
2438 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002439 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002440 },
Felipe Balbie3060b12009-12-15 11:08:41 +02002441 .remove = __exit_p(musb_remove),
Felipe Balbi550a7372008-07-24 12:27:36 +03002442 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002443};
2444
2445/*-------------------------------------------------------------------------*/
2446
2447static int __init musb_init(void)
2448{
2449#ifdef CONFIG_USB_MUSB_HDRC_HCD
2450 if (usb_disabled())
2451 return 0;
2452#endif
2453
2454 pr_info("%s: version " MUSB_VERSION ", "
2455#ifdef CONFIG_MUSB_PIO_ONLY
2456 "pio"
2457#elif defined(CONFIG_USB_TI_CPPI_DMA)
2458 "cppi-dma"
2459#elif defined(CONFIG_USB_INVENTRA_DMA)
2460 "musb-dma"
2461#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2462 "tusb-omap-dma"
2463#else
2464 "?dma?"
2465#endif
2466 ", "
2467#ifdef CONFIG_USB_MUSB_OTG
2468 "otg (peripheral+host)"
2469#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2470 "peripheral"
2471#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2472 "host"
2473#endif
2474 ", debug=%d\n",
Felipe Balbib60c72a2008-10-29 15:10:39 +02002475 musb_driver_name, musb_debug);
Felipe Balbi550a7372008-07-24 12:27:36 +03002476 return platform_driver_probe(&musb_driver, musb_probe);
2477}
2478
David Brownell34f32c92009-02-20 13:45:17 -08002479/* make us init after usbcore and i2c (transceivers, regulators, etc)
2480 * and before usb gadget and host-side drivers start to register
Felipe Balbi550a7372008-07-24 12:27:36 +03002481 */
David Brownell34f32c92009-02-20 13:45:17 -08002482fs_initcall(musb_init);
Felipe Balbi550a7372008-07-24 12:27:36 +03002483
2484static void __exit musb_cleanup(void)
2485{
2486 platform_driver_unregister(&musb_driver);
2487}
2488module_exit(musb_cleanup);