blob: 472b2a7e78960f6be2d7e1e9dae047503ceb35c2 [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
102#ifdef CONFIG_ARM
Felipe Balbi0590d582008-08-30 19:42:02 +0300103#include <mach/hardware.h>
104#include <mach/memory.h>
Felipe Balbi550a7372008-07-24 12:27:36 +0300105#include <asm/mach-types.h>
106#endif
107
108#include "musb_core.h"
109
110
111#ifdef CONFIG_ARCH_DAVINCI
112#include "davinci.h"
113#endif
114
David Brownellf7f9d632009-03-31 12:32:12 -0700115#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +0300116
117
Felipe Balbib60c72a2008-10-29 15:10:39 +0200118unsigned musb_debug;
David Brownell34f32c92009-02-20 13:45:17 -0800119module_param_named(debug, musb_debug, uint, S_IRUGO | S_IWUSR);
Felipe Balbie8164f62008-08-10 21:22:35 +0300120MODULE_PARM_DESC(debug, "Debug message level. Default = 0");
Felipe Balbi550a7372008-07-24 12:27:36 +0300121
122#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
123#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
124
Felipe Balbie8164f62008-08-10 21:22:35 +0300125#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300126
127#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
128
129#define MUSB_DRIVER_NAME "musb_hdrc"
130const char musb_driver_name[] = MUSB_DRIVER_NAME;
131
132MODULE_DESCRIPTION(DRIVER_INFO);
133MODULE_AUTHOR(DRIVER_AUTHOR);
134MODULE_LICENSE("GPL");
135MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
136
137
138/*-------------------------------------------------------------------------*/
139
140static inline struct musb *dev_to_musb(struct device *dev)
141{
142#ifdef CONFIG_USB_MUSB_HDRC_HCD
143 /* usbcore insists dev->driver_data is a "struct hcd *" */
144 return hcd_to_musb(dev_get_drvdata(dev));
145#else
146 return dev_get_drvdata(dev);
147#endif
148}
149
150/*-------------------------------------------------------------------------*/
151
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200152#ifndef CONFIG_BLACKFIN
153static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
154{
155 void __iomem *addr = otg->io_priv;
156 int i = 0;
157 u8 r;
158 u8 power;
159
160 /* Make sure the transceiver is not in low power mode */
161 power = musb_readb(addr, MUSB_POWER);
162 power &= ~MUSB_POWER_SUSPENDM;
163 musb_writeb(addr, MUSB_POWER, power);
164
165 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
166 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
167 */
168
169 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
170 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
171 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
172
173 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
174 & MUSB_ULPI_REG_CMPLT)) {
175 i++;
176 if (i == 10000) {
177 DBG(3, "ULPI read timed out\n");
178 return -ETIMEDOUT;
179 }
180
181 }
182 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
183 r &= ~MUSB_ULPI_REG_CMPLT;
184 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
185
186 return musb_readb(addr, MUSB_ULPI_REG_DATA);
187}
188
189static int musb_ulpi_write(struct otg_transceiver *otg,
190 u32 offset, u32 data)
191{
192 void __iomem *addr = otg->io_priv;
193 int i = 0;
194 u8 r = 0;
195 u8 power;
196
197 /* Make sure the transceiver is not in low power mode */
198 power = musb_readb(addr, MUSB_POWER);
199 power &= ~MUSB_POWER_SUSPENDM;
200 musb_writeb(addr, MUSB_POWER, power);
201
202 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
203 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
204 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
205
206 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
207 & MUSB_ULPI_REG_CMPLT)) {
208 i++;
209 if (i == 10000) {
210 DBG(3, "ULPI write timed out\n");
211 return -ETIMEDOUT;
212 }
213 }
214
215 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
216 r &= ~MUSB_ULPI_REG_CMPLT;
217 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
218
219 return 0;
220}
221#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530222#define musb_ulpi_read NULL
223#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200224#endif
225
226static struct otg_io_access_ops musb_ulpi_access = {
227 .read = musb_ulpi_read,
228 .write = musb_ulpi_write,
229};
230
231/*-------------------------------------------------------------------------*/
232
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200233#if !defined(CONFIG_USB_TUSB6010) && !defined(CONFIG_BLACKFIN)
234
Felipe Balbi550a7372008-07-24 12:27:36 +0300235/*
236 * Load an endpoint's FIFO
237 */
238void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
239{
240 void __iomem *fifo = hw_ep->fifo;
241
242 prefetch((u8 *)src);
243
244 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
245 'T', hw_ep->epnum, fifo, len, src);
246
247 /* we can't assume unaligned reads work */
248 if (likely((0x01 & (unsigned long) src) == 0)) {
249 u16 index = 0;
250
251 /* best case is 32bit-aligned source address */
252 if ((0x02 & (unsigned long) src) == 0) {
253 if (len >= 4) {
254 writesl(fifo, src + index, len >> 2);
255 index += len & ~0x03;
256 }
257 if (len & 0x02) {
258 musb_writew(fifo, 0, *(u16 *)&src[index]);
259 index += 2;
260 }
261 } else {
262 if (len >= 2) {
263 writesw(fifo, src + index, len >> 1);
264 index += len & ~0x01;
265 }
266 }
267 if (len & 0x01)
268 musb_writeb(fifo, 0, src[index]);
269 } else {
270 /* byte aligned */
271 writesb(fifo, src, len);
272 }
273}
274
275/*
276 * Unload an endpoint's FIFO
277 */
278void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
279{
280 void __iomem *fifo = hw_ep->fifo;
281
282 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
283 'R', hw_ep->epnum, fifo, len, dst);
284
285 /* we can't assume unaligned writes work */
286 if (likely((0x01 & (unsigned long) dst) == 0)) {
287 u16 index = 0;
288
289 /* best case is 32bit-aligned destination address */
290 if ((0x02 & (unsigned long) dst) == 0) {
291 if (len >= 4) {
292 readsl(fifo, dst, len >> 2);
293 index = len & ~0x03;
294 }
295 if (len & 0x02) {
296 *(u16 *)&dst[index] = musb_readw(fifo, 0);
297 index += 2;
298 }
299 } else {
300 if (len >= 2) {
301 readsw(fifo, dst, len >> 1);
302 index = len & ~0x01;
303 }
304 }
305 if (len & 0x01)
306 dst[index] = musb_readb(fifo, 0);
307 } else {
308 /* byte aligned */
309 readsb(fifo, dst, len);
310 }
311}
312
313#endif /* normal PIO */
314
315
316/*-------------------------------------------------------------------------*/
317
318/* for high speed test mode; see USB 2.0 spec 7.1.20 */
319static const u8 musb_test_packet[53] = {
320 /* implicit SYNC then DATA0 to start */
321
322 /* JKJKJKJK x9 */
323 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
324 /* JJKKJJKK x8 */
325 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
326 /* JJJJKKKK x8 */
327 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
328 /* JJJJJJJKKKKKKK x8 */
329 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
330 /* JJJJJJJK x8 */
331 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
332 /* JKKKKKKK x10, JK */
333 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
334
335 /* implicit CRC16 then EOP to end */
336};
337
338void musb_load_testpacket(struct musb *musb)
339{
340 void __iomem *regs = musb->endpoints[0].regs;
341
342 musb_ep_select(musb->mregs, 0);
343 musb_write_fifo(musb->control_ep,
344 sizeof(musb_test_packet), musb_test_packet);
345 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
346}
347
348/*-------------------------------------------------------------------------*/
349
350const char *otg_state_string(struct musb *musb)
351{
David Brownell84e250f2009-03-31 12:30:04 -0700352 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300353 case OTG_STATE_A_IDLE: return "a_idle";
354 case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise";
355 case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon";
356 case OTG_STATE_A_HOST: return "a_host";
357 case OTG_STATE_A_SUSPEND: return "a_suspend";
358 case OTG_STATE_A_PERIPHERAL: return "a_peripheral";
359 case OTG_STATE_A_WAIT_VFALL: return "a_wait_vfall";
360 case OTG_STATE_A_VBUS_ERR: return "a_vbus_err";
361 case OTG_STATE_B_IDLE: return "b_idle";
362 case OTG_STATE_B_SRP_INIT: return "b_srp_init";
363 case OTG_STATE_B_PERIPHERAL: return "b_peripheral";
364 case OTG_STATE_B_WAIT_ACON: return "b_wait_acon";
365 case OTG_STATE_B_HOST: return "b_host";
366 default: return "UNDEFINED";
367 }
368}
369
370#ifdef CONFIG_USB_MUSB_OTG
371
372/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300373 * Handles OTG hnp timeouts, such as b_ase0_brst
374 */
375void musb_otg_timer_func(unsigned long data)
376{
377 struct musb *musb = (struct musb *)data;
378 unsigned long flags;
379
380 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700381 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300382 case OTG_STATE_B_WAIT_ACON:
383 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
384 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700385 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 musb->is_active = 0;
387 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700388 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300389 case OTG_STATE_A_WAIT_BCON:
David Brownellab983f2a2009-03-31 12:35:09 -0700390 DBG(1, "HNP: %s timeout\n", otg_state_string(musb));
391 musb_set_vbus(musb, 0);
392 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300393 break;
394 default:
395 DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb));
396 }
397 musb->ignore_disconnect = 0;
398 spin_unlock_irqrestore(&musb->lock, flags);
399}
400
Felipe Balbi550a7372008-07-24 12:27:36 +0300401/*
David Brownellf7f9d632009-03-31 12:32:12 -0700402 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300403 */
404void musb_hnp_stop(struct musb *musb)
405{
406 struct usb_hcd *hcd = musb_to_hcd(musb);
407 void __iomem *mbase = musb->mregs;
408 u8 reg;
409
David Brownellab983f2a2009-03-31 12:35:09 -0700410 DBG(1, "HNP: stop from %s\n", otg_state_string(musb));
411
David Brownell84e250f2009-03-31 12:30:04 -0700412 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300413 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300414 musb_g_disconnect(musb);
David Brownellab983f2a2009-03-31 12:35:09 -0700415 DBG(1, "HNP: back to %s\n", otg_state_string(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300416 break;
417 case OTG_STATE_B_HOST:
418 DBG(1, "HNP: Disabling HR\n");
419 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700420 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300421 MUSB_DEV_MODE(musb);
422 reg = musb_readb(mbase, MUSB_POWER);
423 reg |= MUSB_POWER_SUSPENDM;
424 musb_writeb(mbase, MUSB_POWER, reg);
425 /* REVISIT: Start SESSION_REQUEST here? */
426 break;
427 default:
428 DBG(1, "HNP: Stopping in unknown state %s\n",
429 otg_state_string(musb));
430 }
431
432 /*
433 * When returning to A state after HNP, avoid hub_port_rebounce(),
434 * which cause occasional OPT A "Did not receive reset after connect"
435 * errors.
436 */
Alan Stern749da5f2010-03-04 17:05:08 -0500437 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300438}
439
440#endif
441
442/*
443 * Interrupt Service Routine to record USB "global" interrupts.
444 * Since these do not happen often and signify things of
445 * paramount importance, it seems OK to check them individually;
446 * the order of the tests is specified in the manual
447 *
448 * @param musb instance pointer
449 * @param int_usb register contents
450 * @param devctl
451 * @param power
452 */
453
Felipe Balbi550a7372008-07-24 12:27:36 +0300454static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
455 u8 devctl, u8 power)
456{
457 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300458
459 DBG(3, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
460 int_usb);
461
462 /* in host mode, the peripheral may issue remote wakeup.
463 * in peripheral mode, the host may resume the link.
464 * spurious RESUME irqs happen too, paired with SUSPEND.
465 */
466 if (int_usb & MUSB_INTR_RESUME) {
467 handled = IRQ_HANDLED;
468 DBG(3, "RESUME (%s)\n", otg_state_string(musb));
469
470 if (devctl & MUSB_DEVCTL_HM) {
471#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbiaa471452010-03-12 10:27:24 +0200472 void __iomem *mbase = musb->mregs;
473
David Brownell84e250f2009-03-31 12:30:04 -0700474 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300475 case OTG_STATE_A_SUSPEND:
476 /* remote wakeup? later, GetPortStatus
477 * will stop RESUME signaling
478 */
479
480 if (power & MUSB_POWER_SUSPENDM) {
481 /* spurious */
482 musb->int_usb &= ~MUSB_INTR_SUSPEND;
483 DBG(2, "Spurious SUSPENDM\n");
484 break;
485 }
486
487 power &= ~MUSB_POWER_SUSPENDM;
488 musb_writeb(mbase, MUSB_POWER,
489 power | MUSB_POWER_RESUME);
490
491 musb->port1_status |=
492 (USB_PORT_STAT_C_SUSPEND << 16)
493 | MUSB_PORT_STAT_RESUME;
494 musb->rh_timer = jiffies
495 + msecs_to_jiffies(20);
496
David Brownell84e250f2009-03-31 12:30:04 -0700497 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300498 musb->is_active = 1;
499 usb_hcd_resume_root_hub(musb_to_hcd(musb));
500 break;
501 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700502 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300503 musb->is_active = 1;
504 MUSB_DEV_MODE(musb);
505 break;
506 default:
507 WARNING("bogus %s RESUME (%s)\n",
508 "host",
509 otg_state_string(musb));
510 }
511#endif
512 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700513 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300514#ifdef CONFIG_USB_MUSB_HDRC_HCD
515 case OTG_STATE_A_SUSPEND:
516 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700517 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300518 usb_hcd_resume_root_hub(musb_to_hcd(musb));
519 break;
520#endif
521#ifdef CONFIG_USB_GADGET_MUSB_HDRC
522 case OTG_STATE_B_WAIT_ACON:
523 case OTG_STATE_B_PERIPHERAL:
524 /* disconnect while suspended? we may
525 * not get a disconnect irq...
526 */
527 if ((devctl & MUSB_DEVCTL_VBUS)
528 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
529 ) {
530 musb->int_usb |= MUSB_INTR_DISCONNECT;
531 musb->int_usb &= ~MUSB_INTR_SUSPEND;
532 break;
533 }
534 musb_g_resume(musb);
535 break;
536 case OTG_STATE_B_IDLE:
537 musb->int_usb &= ~MUSB_INTR_SUSPEND;
538 break;
539#endif
540 default:
541 WARNING("bogus %s RESUME (%s)\n",
542 "peripheral",
543 otg_state_string(musb));
544 }
545 }
546 }
547
548#ifdef CONFIG_USB_MUSB_HDRC_HCD
549 /* see manual for the order of the tests */
550 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200551 void __iomem *mbase = musb->mregs;
552
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300553 if (devctl & MUSB_DEVCTL_BDEVICE) {
554 DBG(3, "SessReq while on B state\n");
555 return IRQ_HANDLED;
556 }
557
Felipe Balbi550a7372008-07-24 12:27:36 +0300558 DBG(1, "SESSION_REQUEST (%s)\n", otg_state_string(musb));
559
560 /* IRQ arrives from ID pin sense or (later, if VBUS power
561 * is removed) SRP. responses are time critical:
562 * - turn on VBUS (with silicon-specific mechanism)
563 * - go through A_WAIT_VRISE
564 * - ... to A_WAIT_BCON.
565 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
566 */
567 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
568 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700569 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300570 MUSB_HST_MODE(musb);
571 musb_set_vbus(musb, 1);
572
573 handled = IRQ_HANDLED;
574 }
575
576 if (int_usb & MUSB_INTR_VBUSERROR) {
577 int ignore = 0;
578
579 /* During connection as an A-Device, we may see a short
580 * current spikes causing voltage drop, because of cable
581 * and peripheral capacitance combined with vbus draw.
582 * (So: less common with truly self-powered devices, where
583 * vbus doesn't act like a power supply.)
584 *
585 * Such spikes are short; usually less than ~500 usec, max
586 * of ~2 msec. That is, they're not sustained overcurrent
587 * errors, though they're reported using VBUSERROR irqs.
588 *
589 * Workarounds: (a) hardware: use self powered devices.
590 * (b) software: ignore non-repeated VBUS errors.
591 *
592 * REVISIT: do delays from lots of DEBUG_KERNEL checks
593 * make trouble here, keeping VBUS < 4.4V ?
594 */
David Brownell84e250f2009-03-31 12:30:04 -0700595 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300596 case OTG_STATE_A_HOST:
597 /* recovery is dicey once we've gotten past the
598 * initial stages of enumeration, but if VBUS
599 * stayed ok at the other end of the link, and
600 * another reset is due (at least for high speed,
601 * to redo the chirp etc), it might work OK...
602 */
603 case OTG_STATE_A_WAIT_BCON:
604 case OTG_STATE_A_WAIT_VRISE:
605 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200606 void __iomem *mbase = musb->mregs;
607
Felipe Balbi550a7372008-07-24 12:27:36 +0300608 musb->vbuserr_retry--;
609 ignore = 1;
610 devctl |= MUSB_DEVCTL_SESSION;
611 musb_writeb(mbase, MUSB_DEVCTL, devctl);
612 } else {
613 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500614 USB_PORT_STAT_OVERCURRENT
615 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300616 }
617 break;
618 default:
619 break;
620 }
621
622 DBG(1, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
623 otg_state_string(musb),
624 devctl,
625 ({ char *s;
626 switch (devctl & MUSB_DEVCTL_VBUS) {
627 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
628 s = "<SessEnd"; break;
629 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
630 s = "<AValid"; break;
631 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
632 s = "<VBusValid"; break;
633 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
634 default:
635 s = "VALID"; break;
636 }; s; }),
637 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
638 musb->port1_status);
639
640 /* go through A_WAIT_VFALL then start a new session */
641 if (!ignore)
642 musb_set_vbus(musb, 0);
643 handled = IRQ_HANDLED;
644 }
645
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530646#endif
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200647 if (int_usb & MUSB_INTR_SUSPEND) {
648 DBG(1, "SUSPEND (%s) devctl %02x power %02x\n",
649 otg_state_string(musb), devctl, power);
650 handled = IRQ_HANDLED;
651
652 switch (musb->xceiv->state) {
653#ifdef CONFIG_USB_MUSB_OTG
654 case OTG_STATE_A_PERIPHERAL:
655 /* We also come here if the cable is removed, since
656 * this silicon doesn't report ID-no-longer-grounded.
657 *
658 * We depend on T(a_wait_bcon) to shut us down, and
659 * hope users don't do anything dicey during this
660 * undesired detour through A_WAIT_BCON.
661 */
662 musb_hnp_stop(musb);
663 usb_hcd_resume_root_hub(musb_to_hcd(musb));
664 musb_root_disconnect(musb);
665 musb_platform_try_idle(musb, jiffies
666 + msecs_to_jiffies(musb->a_wait_bcon
667 ? : OTG_TIME_A_WAIT_BCON));
668
669 break;
670#endif
671 case OTG_STATE_B_IDLE:
672 if (!musb->is_active)
673 break;
674 case OTG_STATE_B_PERIPHERAL:
675 musb_g_suspend(musb);
676 musb->is_active = is_otg_enabled(musb)
677 && musb->xceiv->gadget->b_hnp_enable;
678 if (musb->is_active) {
679#ifdef CONFIG_USB_MUSB_OTG
680 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
681 DBG(1, "HNP: Setting timer for b_ase0_brst\n");
682 mod_timer(&musb->otg_timer, jiffies
683 + msecs_to_jiffies(
684 OTG_TIME_B_ASE0_BRST));
685#endif
686 }
687 break;
688 case OTG_STATE_A_WAIT_BCON:
689 if (musb->a_wait_bcon != 0)
690 musb_platform_try_idle(musb, jiffies
691 + msecs_to_jiffies(musb->a_wait_bcon));
692 break;
693 case OTG_STATE_A_HOST:
694 musb->xceiv->state = OTG_STATE_A_SUSPEND;
695 musb->is_active = is_otg_enabled(musb)
696 && musb->xceiv->host->b_hnp_enable;
697 break;
698 case OTG_STATE_B_HOST:
699 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
700 DBG(1, "REVISIT: SUSPEND as B_HOST\n");
701 break;
702 default:
703 /* "should not happen" */
704 musb->is_active = 0;
705 break;
706 }
707 }
708
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530709#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +0300710 if (int_usb & MUSB_INTR_CONNECT) {
711 struct usb_hcd *hcd = musb_to_hcd(musb);
712
713 handled = IRQ_HANDLED;
714 musb->is_active = 1;
715 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
716
717 musb->ep0_stage = MUSB_EP0_START;
718
719#ifdef CONFIG_USB_MUSB_OTG
720 /* flush endpoints when transitioning from Device Mode */
721 if (is_peripheral_active(musb)) {
722 /* REVISIT HNP; just force disconnect */
723 }
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530724 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
725 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
726 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300727#endif
728 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
729 |USB_PORT_STAT_HIGH_SPEED
730 |USB_PORT_STAT_ENABLE
731 );
732 musb->port1_status |= USB_PORT_STAT_CONNECTION
733 |(USB_PORT_STAT_C_CONNECTION << 16);
734
735 /* high vs full speed is just a guess until after reset */
736 if (devctl & MUSB_DEVCTL_LSDEV)
737 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
738
Felipe Balbi550a7372008-07-24 12:27:36 +0300739 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700740 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300741 case OTG_STATE_B_PERIPHERAL:
742 if (int_usb & MUSB_INTR_SUSPEND) {
743 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300744 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700745 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300746 } else
747 DBG(1, "CONNECT as b_peripheral???\n");
748 break;
749 case OTG_STATE_B_WAIT_ACON:
David Brownell1de00da2009-04-02 10:16:11 -0700750 DBG(1, "HNP: CONNECT, now b_host\n");
751b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700752 musb->xceiv->state = OTG_STATE_B_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300753 hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700754 musb->ignore_disconnect = 0;
755 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300756 break;
757 default:
758 if ((devctl & MUSB_DEVCTL_VBUS)
759 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700760 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300761 hcd->self.is_b_host = 0;
762 }
763 break;
764 }
David Brownell1de00da2009-04-02 10:16:11 -0700765
766 /* poke the root hub */
767 MUSB_HST_MODE(musb);
768 if (hcd->status_urb)
769 usb_hcd_poll_rh_status(hcd);
770 else
771 usb_hcd_resume_root_hub(hcd);
772
Felipe Balbi550a7372008-07-24 12:27:36 +0300773 DBG(1, "CONNECT (%s) devctl %02x\n",
774 otg_state_string(musb), devctl);
775 }
776#endif /* CONFIG_USB_MUSB_HDRC_HCD */
777
Felipe Balbi550a7372008-07-24 12:27:36 +0300778 if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
779 DBG(1, "DISCONNECT (%s) as %s, devctl %02x\n",
780 otg_state_string(musb),
781 MUSB_MODE(musb), devctl);
782 handled = IRQ_HANDLED;
783
David Brownell84e250f2009-03-31 12:30:04 -0700784 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300785#ifdef CONFIG_USB_MUSB_HDRC_HCD
786 case OTG_STATE_A_HOST:
787 case OTG_STATE_A_SUSPEND:
Anand Gadiyar5c23c902009-02-21 15:31:40 -0800788 usb_hcd_resume_root_hub(musb_to_hcd(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300789 musb_root_disconnect(musb);
Ajay Kumar Gupta74382172009-02-24 15:29:04 -0800790 if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +0300791 musb_platform_try_idle(musb, jiffies
792 + msecs_to_jiffies(musb->a_wait_bcon));
793 break;
794#endif /* HOST */
795#ifdef CONFIG_USB_MUSB_OTG
796 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700797 /* REVISIT this behaves for "real disconnect"
798 * cases; make sure the other transitions from
799 * from B_HOST act right too. The B_HOST code
800 * in hnp_stop() is currently not used...
801 */
802 musb_root_disconnect(musb);
803 musb_to_hcd(musb)->self.is_b_host = 0;
804 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
805 MUSB_DEV_MODE(musb);
806 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300807 break;
808 case OTG_STATE_A_PERIPHERAL:
809 musb_hnp_stop(musb);
810 musb_root_disconnect(musb);
811 /* FALLTHROUGH */
812 case OTG_STATE_B_WAIT_ACON:
813 /* FALLTHROUGH */
814#endif /* OTG */
815#ifdef CONFIG_USB_GADGET_MUSB_HDRC
816 case OTG_STATE_B_PERIPHERAL:
817 case OTG_STATE_B_IDLE:
818 musb_g_disconnect(musb);
819 break;
820#endif /* GADGET */
821 default:
822 WARNING("unhandled DISCONNECT transition (%s)\n",
823 otg_state_string(musb));
824 break;
825 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300826 }
827
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200828 /* mentor saves a bit: bus reset and babble share the same irq.
829 * only host sees babble; only peripheral sees bus reset.
830 */
831 if (int_usb & MUSB_INTR_RESET) {
832 handled = IRQ_HANDLED;
833 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
834 /*
835 * Looks like non-HS BABBLE can be ignored, but
836 * HS BABBLE is an error condition. For HS the solution
837 * is to avoid babble in the first place and fix what
838 * caused BABBLE. When HS BABBLE happens we can only
839 * stop the session.
840 */
841 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
842 DBG(1, "BABBLE devctl: %02x\n", devctl);
843 else {
844 ERR("Stopping host session -- babble\n");
845 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
846 }
847 } else if (is_peripheral_capable()) {
848 DBG(1, "BUS RESET as %s\n", otg_state_string(musb));
849 switch (musb->xceiv->state) {
850#ifdef CONFIG_USB_OTG
851 case OTG_STATE_A_SUSPEND:
852 /* We need to ignore disconnect on suspend
853 * otherwise tusb 2.0 won't reconnect after a
854 * power cycle, which breaks otg compliance.
855 */
856 musb->ignore_disconnect = 1;
857 musb_g_reset(musb);
858 /* FALLTHROUGH */
859 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
860 /* never use invalid T(a_wait_bcon) */
861 DBG(1, "HNP: in %s, %d msec timeout\n",
862 otg_state_string(musb),
863 TA_WAIT_BCON(musb));
864 mod_timer(&musb->otg_timer, jiffies
865 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
866 break;
867 case OTG_STATE_A_PERIPHERAL:
868 musb->ignore_disconnect = 0;
869 del_timer(&musb->otg_timer);
870 musb_g_reset(musb);
871 break;
872 case OTG_STATE_B_WAIT_ACON:
873 DBG(1, "HNP: RESET (%s), to b_peripheral\n",
874 otg_state_string(musb));
875 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
876 musb_g_reset(musb);
877 break;
878#endif
879 case OTG_STATE_B_IDLE:
880 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
881 /* FALLTHROUGH */
882 case OTG_STATE_B_PERIPHERAL:
883 musb_g_reset(musb);
884 break;
885 default:
886 DBG(1, "Unhandled BUS RESET as %s\n",
887 otg_state_string(musb));
888 }
889 }
890 }
891
892#if 0
893/* REVISIT ... this would be for multiplexing periodic endpoints, or
894 * supporting transfer phasing to prevent exceeding ISO bandwidth
895 * limits of a given frame or microframe.
896 *
897 * It's not needed for peripheral side, which dedicates endpoints;
898 * though it _might_ use SOF irqs for other purposes.
899 *
900 * And it's not currently needed for host side, which also dedicates
901 * endpoints, relies on TX/RX interval registers, and isn't claimed
902 * to support ISO transfers yet.
903 */
904 if (int_usb & MUSB_INTR_SOF) {
905 void __iomem *mbase = musb->mregs;
906 struct musb_hw_ep *ep;
907 u8 epnum;
908 u16 frame;
909
910 DBG(6, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300911 handled = IRQ_HANDLED;
912
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200913 /* start any periodic Tx transfers waiting for current frame */
914 frame = musb_readw(mbase, MUSB_FRAME);
915 ep = musb->endpoints;
916 for (epnum = 1; (epnum < musb->nr_endpoints)
917 && (musb->epmask >= (1 << epnum));
918 epnum++, ep++) {
919 /*
920 * FIXME handle framecounter wraps (12 bits)
921 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300922 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200923 if (ep->dwWaitFrame >= frame) {
924 ep->dwWaitFrame = 0;
925 pr_debug("SOF --> periodic TX%s on %d\n",
926 ep->tx_channel ? " DMA" : "",
927 epnum);
928 if (!ep->tx_channel)
929 musb_h_tx_start(musb, epnum);
930 else
931 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300932 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200933 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300934 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200935#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300936
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200937 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300938
939 return handled;
940}
941
942/*-------------------------------------------------------------------------*/
943
944/*
945* Program the HDRC to start (enable interrupts, dma, etc.).
946*/
947void musb_start(struct musb *musb)
948{
949 void __iomem *regs = musb->mregs;
950 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
951
952 DBG(2, "<== devctl %02x\n", devctl);
953
954 /* Set INT enable registers, enable interrupts */
955 musb_writew(regs, MUSB_INTRTXE, musb->epmask);
956 musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
957 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
958
959 musb_writeb(regs, MUSB_TESTMODE, 0);
960
961 /* put into basic highspeed mode and start session */
962 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
963 | MUSB_POWER_SOFTCONN
964 | MUSB_POWER_HSENAB
965 /* ENSUSPEND wedges tusb */
966 /* | MUSB_POWER_ENSUSPEND */
967 );
968
969 musb->is_active = 0;
970 devctl = musb_readb(regs, MUSB_DEVCTL);
971 devctl &= ~MUSB_DEVCTL_SESSION;
972
973 if (is_otg_enabled(musb)) {
974 /* session started after:
975 * (a) ID-grounded irq, host mode;
976 * (b) vbus present/connect IRQ, peripheral mode;
977 * (c) peripheral initiates, using SRP
978 */
979 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
980 musb->is_active = 1;
981 else
982 devctl |= MUSB_DEVCTL_SESSION;
983
984 } else if (is_host_enabled(musb)) {
985 /* assume ID pin is hard-wired to ground */
986 devctl |= MUSB_DEVCTL_SESSION;
987
988 } else /* peripheral is enabled */ {
989 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
990 musb->is_active = 1;
991 }
992 musb_platform_enable(musb);
993 musb_writeb(regs, MUSB_DEVCTL, devctl);
994}
995
996
997static void musb_generic_disable(struct musb *musb)
998{
999 void __iomem *mbase = musb->mregs;
1000 u16 temp;
1001
1002 /* disable interrupts */
1003 musb_writeb(mbase, MUSB_INTRUSBE, 0);
1004 musb_writew(mbase, MUSB_INTRTXE, 0);
1005 musb_writew(mbase, MUSB_INTRRXE, 0);
1006
1007 /* off */
1008 musb_writeb(mbase, MUSB_DEVCTL, 0);
1009
1010 /* flush pending interrupts */
1011 temp = musb_readb(mbase, MUSB_INTRUSB);
1012 temp = musb_readw(mbase, MUSB_INTRTX);
1013 temp = musb_readw(mbase, MUSB_INTRRX);
1014
1015}
1016
1017/*
1018 * Make the HDRC stop (disable interrupts, etc.);
1019 * reversible by musb_start
1020 * called on gadget driver unregister
1021 * with controller locked, irqs blocked
1022 * acts as a NOP unless some role activated the hardware
1023 */
1024void musb_stop(struct musb *musb)
1025{
1026 /* stop IRQs, timers, ... */
1027 musb_platform_disable(musb);
1028 musb_generic_disable(musb);
1029 DBG(3, "HDRC disabled\n");
1030
1031 /* FIXME
1032 * - mark host and/or peripheral drivers unusable/inactive
1033 * - disable DMA (and enable it in HdrcStart)
1034 * - make sure we can musb_start() after musb_stop(); with
1035 * OTG mode, gadget driver module rmmod/modprobe cycles that
1036 * - ...
1037 */
1038 musb_platform_try_idle(musb, 0);
1039}
1040
1041static void musb_shutdown(struct platform_device *pdev)
1042{
1043 struct musb *musb = dev_to_musb(&pdev->dev);
1044 unsigned long flags;
1045
1046 spin_lock_irqsave(&musb->lock, flags);
1047 musb_platform_disable(musb);
1048 musb_generic_disable(musb);
Sergei Shtylyov3d0bfbf2010-03-25 13:14:29 +02001049 if (musb->clock)
Felipe Balbi550a7372008-07-24 12:27:36 +03001050 clk_put(musb->clock);
Felipe Balbi550a7372008-07-24 12:27:36 +03001051 spin_unlock_irqrestore(&musb->lock, flags);
1052
1053 /* FIXME power down */
1054}
1055
1056
1057/*-------------------------------------------------------------------------*/
1058
1059/*
1060 * The silicon either has hard-wired endpoint configurations, or else
1061 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001062 * writing only the dynamic sizing is very well tested. Since we switched
1063 * away from compile-time hardware parameters, we can no longer rely on
1064 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001065 *
1066 * We don't currently use dynamic fifo setup capability to do anything
1067 * more than selecting one of a bunch of predefined configurations.
1068 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001069#if defined(CONFIG_USB_TUSB6010) || \
Maulik Mankadfb9c58e2010-03-12 10:29:09 +02001070 defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
1071 || defined(CONFIG_ARCH_OMAP4)
Felipe Balbi550a7372008-07-24 12:27:36 +03001072static ushort __initdata fifo_mode = 4;
1073#else
1074static ushort __initdata fifo_mode = 2;
1075#endif
1076
1077/* "modprobe ... fifo_mode=1" etc */
1078module_param(fifo_mode, ushort, 0);
1079MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1080
Felipe Balbi550a7372008-07-24 12:27:36 +03001081/*
1082 * tables defining fifo_mode values. define more if you like.
1083 * for host side, make sure both halves of ep1 are set up.
1084 */
1085
1086/* mode 0 - fits in 2KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001087static struct musb_fifo_cfg __initdata mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001088{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1089{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1090{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1091{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1092{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1093};
1094
1095/* mode 1 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001096static struct musb_fifo_cfg __initdata mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001097{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1098{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1099{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1100{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1101{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1102};
1103
1104/* mode 2 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001105static struct musb_fifo_cfg __initdata mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001106{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1107{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1108{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1109{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1110{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1111{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1112};
1113
1114/* mode 3 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001115static struct musb_fifo_cfg __initdata mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001116{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1117{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1118{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1119{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1120{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1121{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1122};
1123
1124/* mode 4 - fits in 16KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001125static struct musb_fifo_cfg __initdata mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001126{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1127{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1128{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1129{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1130{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1131{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1132{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1133{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1134{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1135{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1136{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1137{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1138{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1139{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1140{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1141{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1142{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1143{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001144{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1145{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1146{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1147{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1148{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1149{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1150{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001151{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1152{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1153};
1154
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001155/* mode 5 - fits in 8KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001156static struct musb_fifo_cfg __initdata mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001157{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1158{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1159{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1160{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1161{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1162{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1163{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1164{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1165{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1166{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1167{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1168{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1169{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1170{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1171{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1172{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1173{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1174{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1175{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1176{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1177{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1178{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1179{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1180{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1181{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1182{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1183{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1184};
Felipe Balbi550a7372008-07-24 12:27:36 +03001185
1186/*
1187 * configure a fifo; for non-shared endpoints, this may be called
1188 * once for a tx fifo and once for an rx fifo.
1189 *
1190 * returns negative errno or offset for next fifo.
1191 */
1192static int __init
1193fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001194 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001195{
1196 void __iomem *mbase = musb->mregs;
1197 int size = 0;
1198 u16 maxpacket = cfg->maxpacket;
1199 u16 c_off = offset >> 3;
1200 u8 c_size;
1201
1202 /* expect hw_ep has already been zero-initialized */
1203
1204 size = ffs(max(maxpacket, (u16) 8)) - 1;
1205 maxpacket = 1 << size;
1206
1207 c_size = size - 3;
1208 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001209 if ((offset + (maxpacket << 1)) >
1210 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001211 return -EMSGSIZE;
1212 c_size |= MUSB_FIFOSZ_DPB;
1213 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001214 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001215 return -EMSGSIZE;
1216 }
1217
1218 /* configure the FIFO */
1219 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1220
1221#ifdef CONFIG_USB_MUSB_HDRC_HCD
1222 /* EP0 reserved endpoint for control, bidirectional;
1223 * EP1 reserved for bulk, two unidirection halves.
1224 */
1225 if (hw_ep->epnum == 1)
1226 musb->bulk_ep = hw_ep;
1227 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1228#endif
1229 switch (cfg->style) {
1230 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001231 musb_write_txfifosz(mbase, c_size);
1232 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001233 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1234 hw_ep->max_packet_sz_tx = maxpacket;
1235 break;
1236 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001237 musb_write_rxfifosz(mbase, c_size);
1238 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001239 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1240 hw_ep->max_packet_sz_rx = maxpacket;
1241 break;
1242 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001243 musb_write_txfifosz(mbase, c_size);
1244 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001245 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1246 hw_ep->max_packet_sz_rx = maxpacket;
1247
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001248 musb_write_rxfifosz(mbase, c_size);
1249 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001250 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1251 hw_ep->max_packet_sz_tx = maxpacket;
1252
1253 hw_ep->is_shared_fifo = true;
1254 break;
1255 }
1256
1257 /* NOTE rx and tx endpoint irqs aren't managed separately,
1258 * which happens to be ok
1259 */
1260 musb->epmask |= (1 << hw_ep->epnum);
1261
1262 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1263}
1264
Felipe Balbie6c213b2010-03-12 10:29:06 +02001265static struct musb_fifo_cfg __initdata ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001266 .style = FIFO_RXTX, .maxpacket = 64,
1267};
1268
1269static int __init ep_config_from_table(struct musb *musb)
1270{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001271 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001272 unsigned i, n;
1273 int offset;
1274 struct musb_hw_ep *hw_ep = musb->endpoints;
1275
Felipe Balbie6c213b2010-03-12 10:29:06 +02001276 if (musb->config->fifo_cfg) {
1277 cfg = musb->config->fifo_cfg;
1278 n = musb->config->fifo_cfg_size;
1279 goto done;
1280 }
1281
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 switch (fifo_mode) {
1283 default:
1284 fifo_mode = 0;
1285 /* FALLTHROUGH */
1286 case 0:
1287 cfg = mode_0_cfg;
1288 n = ARRAY_SIZE(mode_0_cfg);
1289 break;
1290 case 1:
1291 cfg = mode_1_cfg;
1292 n = ARRAY_SIZE(mode_1_cfg);
1293 break;
1294 case 2:
1295 cfg = mode_2_cfg;
1296 n = ARRAY_SIZE(mode_2_cfg);
1297 break;
1298 case 3:
1299 cfg = mode_3_cfg;
1300 n = ARRAY_SIZE(mode_3_cfg);
1301 break;
1302 case 4:
1303 cfg = mode_4_cfg;
1304 n = ARRAY_SIZE(mode_4_cfg);
1305 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001306 case 5:
1307 cfg = mode_5_cfg;
1308 n = ARRAY_SIZE(mode_5_cfg);
1309 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001310 }
1311
1312 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1313 musb_driver_name, fifo_mode);
1314
1315
Felipe Balbie6c213b2010-03-12 10:29:06 +02001316done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001317 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1318 /* assert(offset > 0) */
1319
1320 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001321 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001322 */
1323
1324 for (i = 0; i < n; i++) {
1325 u8 epn = cfg->hw_ep_num;
1326
Felipe Balbica6d1b12008-08-08 12:40:54 +03001327 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001328 pr_debug("%s: invalid ep %d\n",
1329 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001330 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001331 }
1332 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1333 if (offset < 0) {
1334 pr_debug("%s: mem overrun, ep %d\n",
1335 musb_driver_name, epn);
1336 return -EINVAL;
1337 }
1338 epn++;
1339 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1340 }
1341
1342 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1343 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001344 n + 1, musb->config->num_eps * 2 - 1,
1345 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001346
1347#ifdef CONFIG_USB_MUSB_HDRC_HCD
1348 if (!musb->bulk_ep) {
1349 pr_debug("%s: missing bulk\n", musb_driver_name);
1350 return -EINVAL;
1351 }
1352#endif
1353
1354 return 0;
1355}
1356
1357
1358/*
1359 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1360 * @param musb the controller
1361 */
1362static int __init ep_config_from_hw(struct musb *musb)
1363{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001364 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001365 struct musb_hw_ep *hw_ep;
1366 void *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001367 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001368
1369 DBG(2, "<== static silicon ep config\n");
1370
1371 /* FIXME pick up ep0 maxpacket size */
1372
Felipe Balbica6d1b12008-08-08 12:40:54 +03001373 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001374 musb_ep_select(mbase, epnum);
1375 hw_ep = musb->endpoints + epnum;
1376
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001377 ret = musb_read_fifosize(musb, hw_ep, epnum);
1378 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001379 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001380
1381 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1382
1383#ifdef CONFIG_USB_MUSB_HDRC_HCD
1384 /* pick an RX/TX endpoint for bulk */
1385 if (hw_ep->max_packet_sz_tx < 512
1386 || hw_ep->max_packet_sz_rx < 512)
1387 continue;
1388
1389 /* REVISIT: this algorithm is lazy, we should at least
1390 * try to pick a double buffered endpoint.
1391 */
1392 if (musb->bulk_ep)
1393 continue;
1394 musb->bulk_ep = hw_ep;
1395#endif
1396 }
1397
1398#ifdef CONFIG_USB_MUSB_HDRC_HCD
1399 if (!musb->bulk_ep) {
1400 pr_debug("%s: missing bulk\n", musb_driver_name);
1401 return -EINVAL;
1402 }
1403#endif
1404
1405 return 0;
1406}
1407
1408enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1409
1410/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1411 * configure endpoints, or take their config from silicon
1412 */
1413static int __init musb_core_init(u16 musb_type, struct musb *musb)
1414{
Felipe Balbi550a7372008-07-24 12:27:36 +03001415 u8 reg;
1416 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301417 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001418 void __iomem *mbase = musb->mregs;
1419 int status = 0;
1420 int i;
1421
1422 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001423 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001424
1425 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001426 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001427 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001428 musb->dyn_fifo = true;
1429 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001430 if (reg & MUSB_CONFIGDATA_MPRXE) {
1431 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001432 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001433 }
1434 if (reg & MUSB_CONFIGDATA_MPTXE) {
1435 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001436 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001437 }
1438 if (reg & MUSB_CONFIGDATA_HBRXE) {
1439 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001440 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001441 }
1442 if (reg & MUSB_CONFIGDATA_HBTXE) {
1443 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001444 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001445 }
1446 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1447 strcat(aInfo, ", SoftConn");
1448
1449 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1450 musb_driver_name, reg, aInfo);
1451
Felipe Balbi550a7372008-07-24 12:27:36 +03001452 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001453 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1454 musb->is_multipoint = 1;
1455 type = "M";
1456 } else {
1457 musb->is_multipoint = 0;
1458 type = "";
1459#ifdef CONFIG_USB_MUSB_HDRC_HCD
1460#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1461 printk(KERN_ERR
1462 "%s: kernel must blacklist external hubs\n",
1463 musb_driver_name);
1464#endif
1465#endif
1466 }
1467
1468 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301469 musb->hwvers = musb_read_hwvers(mbase);
1470 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1471 MUSB_HWVERS_MINOR(musb->hwvers),
1472 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001473 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1474 musb_driver_name, type, aRevision, aDate);
1475
1476 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001477 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001478
1479 /* discover endpoint configuration */
1480 musb->nr_endpoints = 1;
1481 musb->epmask = 1;
1482
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001483 if (musb->dyn_fifo)
1484 status = ep_config_from_table(musb);
1485 else
1486 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001487
1488 if (status < 0)
1489 return status;
1490
1491 /* finish init, and print endpoint config */
1492 for (i = 0; i < musb->nr_endpoints; i++) {
1493 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1494
1495 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
1496#ifdef CONFIG_USB_TUSB6010
1497 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1498 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1499 hw_ep->fifo_sync_va =
1500 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1501
1502 if (i == 0)
1503 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1504 else
1505 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1506#endif
1507
1508 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1509#ifdef CONFIG_USB_MUSB_HDRC_HCD
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001510 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001511 hw_ep->rx_reinit = 1;
1512 hw_ep->tx_reinit = 1;
1513#endif
1514
1515 if (hw_ep->max_packet_sz_tx) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301516 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001517 "%s: hw_ep %d%s, %smax %d\n",
1518 musb_driver_name, i,
1519 hw_ep->is_shared_fifo ? "shared" : "tx",
1520 hw_ep->tx_double_buffered
1521 ? "doublebuffer, " : "",
1522 hw_ep->max_packet_sz_tx);
1523 }
1524 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301525 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001526 "%s: hw_ep %d%s, %smax %d\n",
1527 musb_driver_name, i,
1528 "rx",
1529 hw_ep->rx_double_buffered
1530 ? "doublebuffer, " : "",
1531 hw_ep->max_packet_sz_rx);
1532 }
1533 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1534 DBG(1, "hw_ep %d not configured\n", i);
1535 }
1536
1537 return 0;
1538}
1539
1540/*-------------------------------------------------------------------------*/
1541
Maulik Mankadfb9c58e2010-03-12 10:29:09 +02001542#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3430) || \
1543 defined(CONFIG_ARCH_OMAP4)
Felipe Balbi550a7372008-07-24 12:27:36 +03001544
1545static irqreturn_t generic_interrupt(int irq, void *__hci)
1546{
1547 unsigned long flags;
1548 irqreturn_t retval = IRQ_NONE;
1549 struct musb *musb = __hci;
1550
1551 spin_lock_irqsave(&musb->lock, flags);
1552
1553 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1554 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1555 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1556
1557 if (musb->int_usb || musb->int_tx || musb->int_rx)
1558 retval = musb_interrupt(musb);
1559
1560 spin_unlock_irqrestore(&musb->lock, flags);
1561
Sergei Shtylyova5073b52009-03-27 12:52:43 -07001562 return retval;
Felipe Balbi550a7372008-07-24 12:27:36 +03001563}
1564
1565#else
1566#define generic_interrupt NULL
1567#endif
1568
1569/*
1570 * handle all the irqs defined by the HDRC core. for now we expect: other
1571 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1572 * will be assigned, and the irq will already have been acked.
1573 *
1574 * called in irq context with spinlock held, irqs blocked
1575 */
1576irqreturn_t musb_interrupt(struct musb *musb)
1577{
1578 irqreturn_t retval = IRQ_NONE;
1579 u8 devctl, power;
1580 int ep_num;
1581 u32 reg;
1582
1583 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1584 power = musb_readb(musb->mregs, MUSB_POWER);
1585
1586 DBG(4, "** IRQ %s usb%04x tx%04x rx%04x\n",
1587 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1588 musb->int_usb, musb->int_tx, musb->int_rx);
1589
Felipe Balbicd42fef2009-12-15 13:47:30 +02001590#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1591 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
1592 if (!musb->gadget_driver) {
1593 DBG(5, "No gadget driver loaded\n");
1594 return IRQ_HANDLED;
1595 }
1596#endif
1597
Felipe Balbi550a7372008-07-24 12:27:36 +03001598 /* the core can interrupt us for multiple reasons; docs have
1599 * a generic interrupt flowchart to follow
1600 */
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301601 if (musb->int_usb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001602 retval |= musb_stage0_irq(musb, musb->int_usb,
1603 devctl, power);
1604
1605 /* "stage 1" is handling endpoint irqs */
1606
1607 /* handle endpoint 0 first */
1608 if (musb->int_tx & 1) {
1609 if (devctl & MUSB_DEVCTL_HM)
1610 retval |= musb_h_ep0_irq(musb);
1611 else
1612 retval |= musb_g_ep0_irq(musb);
1613 }
1614
1615 /* RX on endpoints 1-15 */
1616 reg = musb->int_rx >> 1;
1617 ep_num = 1;
1618 while (reg) {
1619 if (reg & 1) {
1620 /* musb_ep_select(musb->mregs, ep_num); */
1621 /* REVISIT just retval = ep->rx_irq(...) */
1622 retval = IRQ_HANDLED;
1623 if (devctl & MUSB_DEVCTL_HM) {
1624 if (is_host_capable())
1625 musb_host_rx(musb, ep_num);
1626 } else {
1627 if (is_peripheral_capable())
1628 musb_g_rx(musb, ep_num);
1629 }
1630 }
1631
1632 reg >>= 1;
1633 ep_num++;
1634 }
1635
1636 /* TX on endpoints 1-15 */
1637 reg = musb->int_tx >> 1;
1638 ep_num = 1;
1639 while (reg) {
1640 if (reg & 1) {
1641 /* musb_ep_select(musb->mregs, ep_num); */
1642 /* REVISIT just retval |= ep->tx_irq(...) */
1643 retval = IRQ_HANDLED;
1644 if (devctl & MUSB_DEVCTL_HM) {
1645 if (is_host_capable())
1646 musb_host_tx(musb, ep_num);
1647 } else {
1648 if (is_peripheral_capable())
1649 musb_g_tx(musb, ep_num);
1650 }
1651 }
1652 reg >>= 1;
1653 ep_num++;
1654 }
1655
Felipe Balbi550a7372008-07-24 12:27:36 +03001656 return retval;
1657}
1658
1659
1660#ifndef CONFIG_MUSB_PIO_ONLY
1661static int __initdata use_dma = 1;
1662
1663/* "modprobe ... use_dma=0" etc */
1664module_param(use_dma, bool, 0);
1665MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1666
1667void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1668{
1669 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1670
1671 /* called with controller lock already held */
1672
1673 if (!epnum) {
1674#ifndef CONFIG_USB_TUSB_OMAP_DMA
1675 if (!is_cppi_enabled()) {
1676 /* endpoint 0 */
1677 if (devctl & MUSB_DEVCTL_HM)
1678 musb_h_ep0_irq(musb);
1679 else
1680 musb_g_ep0_irq(musb);
1681 }
1682#endif
1683 } else {
1684 /* endpoints 1..15 */
1685 if (transmit) {
1686 if (devctl & MUSB_DEVCTL_HM) {
1687 if (is_host_capable())
1688 musb_host_tx(musb, epnum);
1689 } else {
1690 if (is_peripheral_capable())
1691 musb_g_tx(musb, epnum);
1692 }
1693 } else {
1694 /* receive */
1695 if (devctl & MUSB_DEVCTL_HM) {
1696 if (is_host_capable())
1697 musb_host_rx(musb, epnum);
1698 } else {
1699 if (is_peripheral_capable())
1700 musb_g_rx(musb, epnum);
1701 }
1702 }
1703 }
1704}
1705
1706#else
1707#define use_dma 0
1708#endif
1709
1710/*-------------------------------------------------------------------------*/
1711
1712#ifdef CONFIG_SYSFS
1713
1714static ssize_t
1715musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1716{
1717 struct musb *musb = dev_to_musb(dev);
1718 unsigned long flags;
1719 int ret = -EINVAL;
1720
1721 spin_lock_irqsave(&musb->lock, flags);
1722 ret = sprintf(buf, "%s\n", otg_state_string(musb));
1723 spin_unlock_irqrestore(&musb->lock, flags);
1724
1725 return ret;
1726}
1727
1728static ssize_t
1729musb_mode_store(struct device *dev, struct device_attribute *attr,
1730 const char *buf, size_t n)
1731{
1732 struct musb *musb = dev_to_musb(dev);
1733 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001734 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001735
1736 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001737 if (sysfs_streq(buf, "host"))
1738 status = musb_platform_set_mode(musb, MUSB_HOST);
1739 else if (sysfs_streq(buf, "peripheral"))
1740 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1741 else if (sysfs_streq(buf, "otg"))
1742 status = musb_platform_set_mode(musb, MUSB_OTG);
1743 else
1744 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001745 spin_unlock_irqrestore(&musb->lock, flags);
1746
David Brownell96a274d2008-11-24 13:06:47 +02001747 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001748}
1749static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1750
1751static ssize_t
1752musb_vbus_store(struct device *dev, struct device_attribute *attr,
1753 const char *buf, size_t n)
1754{
1755 struct musb *musb = dev_to_musb(dev);
1756 unsigned long flags;
1757 unsigned long val;
1758
1759 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001760 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001761 return -EINVAL;
1762 }
1763
1764 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001765 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1766 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001767 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001768 musb->is_active = 0;
1769 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1770 spin_unlock_irqrestore(&musb->lock, flags);
1771
1772 return n;
1773}
1774
1775static ssize_t
1776musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1777{
1778 struct musb *musb = dev_to_musb(dev);
1779 unsigned long flags;
1780 unsigned long val;
1781 int vbus;
1782
1783 spin_lock_irqsave(&musb->lock, flags);
1784 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001785 /* FIXME get_vbus_status() is normally #defined as false...
1786 * and is effectively TUSB-specific.
1787 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001788 vbus = musb_platform_get_vbus_status(musb);
1789 spin_unlock_irqrestore(&musb->lock, flags);
1790
David Brownellf7f9d632009-03-31 12:32:12 -07001791 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001792 vbus ? "on" : "off", val);
1793}
1794static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1795
1796#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1797
1798/* Gadget drivers can't know that a host is connected so they might want
1799 * to start SRP, but users can. This allows userspace to trigger SRP.
1800 */
1801static ssize_t
1802musb_srp_store(struct device *dev, struct device_attribute *attr,
1803 const char *buf, size_t n)
1804{
1805 struct musb *musb = dev_to_musb(dev);
1806 unsigned short srp;
1807
1808 if (sscanf(buf, "%hu", &srp) != 1
1809 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001810 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001811 return -EINVAL;
1812 }
1813
1814 if (srp == 1)
1815 musb_g_wakeup(musb);
1816
1817 return n;
1818}
1819static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1820
1821#endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1822
Felipe Balbi94375752009-12-15 11:08:38 +02001823static struct attribute *musb_attributes[] = {
1824 &dev_attr_mode.attr,
1825 &dev_attr_vbus.attr,
1826#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1827 &dev_attr_srp.attr,
1828#endif
1829 NULL
1830};
1831
1832static const struct attribute_group musb_attr_group = {
1833 .attrs = musb_attributes,
1834};
1835
Felipe Balbi550a7372008-07-24 12:27:36 +03001836#endif /* sysfs */
1837
1838/* Only used to provide driver mode change events */
1839static void musb_irq_work(struct work_struct *data)
1840{
1841 struct musb *musb = container_of(data, struct musb, irq_work);
1842 static int old_state;
1843
David Brownell84e250f2009-03-31 12:30:04 -07001844 if (musb->xceiv->state != old_state) {
1845 old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001846 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1847 }
1848}
1849
1850/* --------------------------------------------------------------------------
1851 * Init support
1852 */
1853
1854static struct musb *__init
Felipe Balbica6d1b12008-08-08 12:40:54 +03001855allocate_instance(struct device *dev,
1856 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001857{
1858 struct musb *musb;
1859 struct musb_hw_ep *ep;
1860 int epnum;
1861#ifdef CONFIG_USB_MUSB_HDRC_HCD
1862 struct usb_hcd *hcd;
1863
Kay Sievers427c4f32008-11-07 01:52:53 +01001864 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
Felipe Balbi550a7372008-07-24 12:27:36 +03001865 if (!hcd)
1866 return NULL;
1867 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1868
1869 musb = hcd_to_musb(hcd);
1870 INIT_LIST_HEAD(&musb->control);
1871 INIT_LIST_HEAD(&musb->in_bulk);
1872 INIT_LIST_HEAD(&musb->out_bulk);
1873
1874 hcd->uses_new_polling = 1;
1875
1876 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001877 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001878#else
1879 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1880 if (!musb)
1881 return NULL;
1882 dev_set_drvdata(dev, musb);
1883
1884#endif
1885
1886 musb->mregs = mbase;
1887 musb->ctrl_base = mbase;
1888 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001889 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001890 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001891 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001892 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001893 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001894 ep->musb = musb;
1895 ep->epnum = epnum;
1896 }
1897
1898 musb->controller = dev;
1899 return musb;
1900}
1901
1902static void musb_free(struct musb *musb)
1903{
1904 /* this has multiple entry modes. it handles fault cleanup after
1905 * probe(), where things may be partially set up, as well as rmmod
1906 * cleanup after everything's been de-activated.
1907 */
1908
1909#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001910 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001911#endif
1912
1913#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1914 musb_gadget_cleanup(musb);
1915#endif
1916
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001917 if (musb->nIrq >= 0) {
1918 if (musb->irq_wake)
1919 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001920 free_irq(musb->nIrq, musb);
1921 }
1922 if (is_dma_capable() && musb->dma_controller) {
1923 struct dma_controller *c = musb->dma_controller;
1924
1925 (void) c->stop(c);
1926 dma_controller_destroy(c);
1927 }
1928
Ajay Kumar Guptac740d0d2009-08-03 11:43:40 +05301929#ifdef CONFIG_USB_MUSB_OTG
1930 put_device(musb->xceiv->dev);
1931#endif
1932
Felipe Balbi550a7372008-07-24 12:27:36 +03001933#ifdef CONFIG_USB_MUSB_HDRC_HCD
1934 usb_put_hcd(musb_to_hcd(musb));
1935#else
1936 kfree(musb);
1937#endif
1938}
1939
1940/*
1941 * Perform generic per-controller initialization.
1942 *
1943 * @pDevice: the controller (already clocked, etc)
1944 * @nIrq: irq
1945 * @mregs: virtual address of controller registers,
1946 * not yet corrected for platform-specific offsets
1947 */
1948static int __init
1949musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1950{
1951 int status;
1952 struct musb *musb;
1953 struct musb_hdrc_platform_data *plat = dev->platform_data;
1954
1955 /* The driver might handle more features than the board; OK.
1956 * Fail when the board needs a feature that's not enabled.
1957 */
1958 if (!plat) {
1959 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001960 status = -ENODEV;
1961 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001962 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001963
Felipe Balbi550a7372008-07-24 12:27:36 +03001964 switch (plat->mode) {
1965 case MUSB_HOST:
1966#ifdef CONFIG_USB_MUSB_HDRC_HCD
1967 break;
1968#else
1969 goto bad_config;
1970#endif
1971 case MUSB_PERIPHERAL:
1972#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1973 break;
1974#else
1975 goto bad_config;
1976#endif
1977 case MUSB_OTG:
1978#ifdef CONFIG_USB_MUSB_OTG
1979 break;
1980#else
1981bad_config:
1982#endif
1983 default:
1984 dev_err(dev, "incompatible Kconfig role setting\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001985 status = -EINVAL;
1986 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001987 }
1988
1989 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001990 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001991 if (!musb) {
1992 status = -ENOMEM;
1993 goto fail0;
1994 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001995
1996 spin_lock_init(&musb->lock);
1997 musb->board_mode = plat->mode;
1998 musb->board_set_power = plat->set_power;
1999 musb->set_clock = plat->set_clock;
2000 musb->min_power = plat->min_power;
2001
2002 /* Clock usage is chip-specific ... functional clock (DaVinci,
2003 * OMAP2430), or PHY ref (some TUSB6010 boards). All this core
2004 * code does is make sure a clock handle is available; platform
2005 * code manages it during start/stop and suspend/resume.
2006 */
2007 if (plat->clock) {
2008 musb->clock = clk_get(dev, plat->clock);
2009 if (IS_ERR(musb->clock)) {
2010 status = PTR_ERR(musb->clock);
2011 musb->clock = NULL;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002012 goto fail1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002013 }
2014 }
2015
David Brownell84e250f2009-03-31 12:30:04 -07002016 /* The musb_platform_init() call:
2017 * - adjusts musb->mregs and musb->isr if needed,
2018 * - may initialize an integrated tranceiver
2019 * - initializes musb->xceiv, usually by otg_get_transceiver()
2020 * - activates clocks.
2021 * - stops powering VBUS
2022 * - assigns musb->board_set_vbus if host mode is enabled
2023 *
2024 * There are various transciever configurations. Blackfin,
2025 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
2026 * external/discrete ones in various flavors (twl4030 family,
2027 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03002028 */
2029 musb->isr = generic_interrupt;
Maulik Mankadde2e1b02010-03-12 10:29:07 +02002030 status = musb_platform_init(musb, plat->board_data);
Felipe Balbi550a7372008-07-24 12:27:36 +03002031 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002032 goto fail2;
2033
Felipe Balbi550a7372008-07-24 12:27:36 +03002034 if (!musb->isr) {
2035 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002036 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002037 }
2038
Heikki Krogerusffb865b2010-03-25 13:25:28 +02002039 if (!musb->xceiv->io_ops) {
2040 musb->xceiv->io_priv = musb->mregs;
2041 musb->xceiv->io_ops = &musb_ulpi_access;
2042 }
2043
Felipe Balbi550a7372008-07-24 12:27:36 +03002044#ifndef CONFIG_MUSB_PIO_ONLY
2045 if (use_dma && dev->dma_mask) {
2046 struct dma_controller *c;
2047
2048 c = dma_controller_create(musb, musb->mregs);
2049 musb->dma_controller = c;
2050 if (c)
2051 (void) c->start(c);
2052 }
2053#endif
2054 /* ideally this would be abstracted in platform setup */
2055 if (!is_dma_capable() || !musb->dma_controller)
2056 dev->dma_mask = NULL;
2057
2058 /* be sure interrupts are disabled before connecting ISR */
2059 musb_platform_disable(musb);
2060 musb_generic_disable(musb);
2061
2062 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03002063 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03002064 ? MUSB_CONTROLLER_MHDRC
2065 : MUSB_CONTROLLER_HDRC, musb);
2066 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002067 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002068
Amit Kucheria3a9f5bd2009-07-27 12:03:19 +03002069#ifdef CONFIG_USB_MUSB_OTG
David Brownellf7f9d632009-03-31 12:32:12 -07002070 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2071#endif
2072
Felipe Balbi550a7372008-07-24 12:27:36 +03002073 /* Init IRQ workqueue before request_irq */
2074 INIT_WORK(&musb->irq_work, musb_irq_work);
2075
2076 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002077 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002078 dev_err(dev, "request_irq %d failed!\n", nIrq);
2079 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002080 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002081 }
2082 musb->nIrq = nIrq;
2083/* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002084 if (enable_irq_wake(nIrq) == 0) {
2085 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002086 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002087 } else {
2088 musb->irq_wake = 0;
2089 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002090
David Brownell84e250f2009-03-31 12:30:04 -07002091 /* host side needs more setup */
2092 if (is_host_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002093 struct usb_hcd *hcd = musb_to_hcd(musb);
2094
David Brownell84e250f2009-03-31 12:30:04 -07002095 otg_set_host(musb->xceiv, &hcd->self);
2096
2097 if (is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002098 hcd->self.otg_port = 1;
David Brownell84e250f2009-03-31 12:30:04 -07002099 musb->xceiv->host = &hcd->self;
Felipe Balbi550a7372008-07-24 12:27:36 +03002100 hcd->power_budget = 2 * (plat->power ? : 250);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002101
2102 /* program PHY to use external vBus if required */
2103 if (plat->extvbus) {
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002104 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002105 busctl |= MUSB_ULPI_USE_EXTVBUS;
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002106 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002107 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002108 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002109
2110 /* For the host-only role, we can activate right away.
2111 * (We expect the ID pin to be forcibly grounded!!)
2112 * Otherwise, wait till the gadget driver hooks up.
2113 */
2114 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
2115 MUSB_HST_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002116 musb->xceiv->default_a = 1;
2117 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002118
2119 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2120
2121 DBG(1, "%s mode, status %d, devctl %02x %c\n",
2122 "HOST", status,
2123 musb_readb(musb->mregs, MUSB_DEVCTL),
2124 (musb_readb(musb->mregs, MUSB_DEVCTL)
2125 & MUSB_DEVCTL_BDEVICE
2126 ? 'B' : 'A'));
2127
2128 } else /* peripheral is enabled */ {
2129 MUSB_DEV_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002130 musb->xceiv->default_a = 0;
2131 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002132
2133 status = musb_gadget_setup(musb);
2134
2135 DBG(1, "%s mode, status %d, dev%02x\n",
2136 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2137 status,
2138 musb_readb(musb->mregs, MUSB_DEVCTL));
2139
2140 }
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002141 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002142 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002143
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002144 status = musb_init_debugfs(musb);
2145 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002146 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002147
Felipe Balbi550a7372008-07-24 12:27:36 +03002148#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02002149 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002150 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002151 goto fail5;
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002152#endif
Felipe Balbi28c2c512008-09-11 11:53:25 +03002153
Felipe Balbiab3bbfa2010-01-21 15:33:58 +02002154 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
2155 ({char *s;
2156 switch (musb->board_mode) {
2157 case MUSB_HOST: s = "Host"; break;
2158 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2159 default: s = "OTG"; break;
2160 }; s; }),
2161 ctrl,
2162 (is_dma_capable() && musb->dma_controller)
2163 ? "DMA" : "PIO",
2164 musb->nIrq);
2165
Felipe Balbi28c2c512008-09-11 11:53:25 +03002166 return 0;
2167
Felipe Balbib0f9da72010-03-25 13:25:18 +02002168fail5:
2169 musb_exit_debugfs(musb);
2170
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002171fail4:
2172 if (!is_otg_enabled(musb) && is_host_enabled(musb))
2173 usb_remove_hcd(musb_to_hcd(musb));
2174 else
2175 musb_gadget_cleanup(musb);
2176
2177fail3:
2178 if (musb->irq_wake)
2179 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002180 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002181
2182fail2:
2183 if (musb->clock)
2184 clk_put(musb->clock);
2185
2186fail1:
Felipe Balbi28c2c512008-09-11 11:53:25 +03002187 dev_err(musb->controller,
2188 "musb_init_controller failed with status %d\n", status);
2189
Felipe Balbi28c2c512008-09-11 11:53:25 +03002190 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002191
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002192fail0:
2193
Felipe Balbi550a7372008-07-24 12:27:36 +03002194 return status;
2195
Felipe Balbi550a7372008-07-24 12:27:36 +03002196}
2197
2198/*-------------------------------------------------------------------------*/
2199
2200/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2201 * bridge to a platform device; this driver then suffices.
2202 */
2203
2204#ifndef CONFIG_MUSB_PIO_ONLY
2205static u64 *orig_dma_mask;
2206#endif
2207
2208static int __init musb_probe(struct platform_device *pdev)
2209{
2210 struct device *dev = &pdev->dev;
2211 int irq = platform_get_irq(pdev, 0);
Felipe Balbida5108e2010-01-21 15:33:57 +02002212 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002213 struct resource *iomem;
2214 void __iomem *base;
2215
2216 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2217 if (!iomem || irq == 0)
2218 return -ENODEV;
2219
Felipe Balbi195e9e42009-12-15 11:08:42 +02002220 base = ioremap(iomem->start, resource_size(iomem));
Felipe Balbi550a7372008-07-24 12:27:36 +03002221 if (!base) {
2222 dev_err(dev, "ioremap failed\n");
2223 return -ENOMEM;
2224 }
2225
2226#ifndef CONFIG_MUSB_PIO_ONLY
2227 /* clobbered by use_dma=n */
2228 orig_dma_mask = dev->dma_mask;
2229#endif
Felipe Balbida5108e2010-01-21 15:33:57 +02002230 status = musb_init_controller(dev, irq, base);
2231 if (status < 0)
2232 iounmap(base);
2233
2234 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002235}
2236
Felipe Balbie3060b12009-12-15 11:08:41 +02002237static int __exit musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002238{
2239 struct musb *musb = dev_to_musb(&pdev->dev);
2240 void __iomem *ctrl_base = musb->ctrl_base;
2241
2242 /* this gets called on rmmod.
2243 * - Host mode: host may still be active
2244 * - Peripheral mode: peripheral is deactivated (or never-activated)
2245 * - OTG mode: both roles are deactivated (or never-activated)
2246 */
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002247 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002248 musb_shutdown(pdev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002249#ifdef CONFIG_USB_MUSB_HDRC_HCD
2250 if (musb->board_mode == MUSB_HOST)
2251 usb_remove_hcd(musb_to_hcd(musb));
2252#endif
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002253 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
2254 musb_platform_exit(musb);
2255 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
2256
Felipe Balbi550a7372008-07-24 12:27:36 +03002257 musb_free(musb);
2258 iounmap(ctrl_base);
2259 device_init_wakeup(&pdev->dev, 0);
2260#ifndef CONFIG_MUSB_PIO_ONLY
2261 pdev->dev.dma_mask = orig_dma_mask;
2262#endif
2263 return 0;
2264}
2265
2266#ifdef CONFIG_PM
2267
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002268static struct musb_context_registers musb_context;
2269
2270void musb_save_context(struct musb *musb)
2271{
2272 int i;
2273 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002274 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002275
2276 if (is_host_enabled(musb)) {
2277 musb_context.frame = musb_readw(musb_base, MUSB_FRAME);
2278 musb_context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
Ajay Kumar Gupta5e0e61a2010-03-25 13:14:26 +02002279 musb_context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002280 }
2281 musb_context.power = musb_readb(musb_base, MUSB_POWER);
2282 musb_context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
2283 musb_context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
2284 musb_context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2285 musb_context.index = musb_readb(musb_base, MUSB_INDEX);
2286 musb_context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
2287
Bob Liuae9b2ad2010-09-24 13:44:07 +03002288 for (i = 0; i < musb->config->num_eps; ++i) {
2289 epio = musb->endpoints[i].regs;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002290 musb_context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002291 musb_readw(epio, MUSB_TXMAXP);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002292 musb_context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002293 musb_readw(epio, MUSB_TXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002294 musb_context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002295 musb_readw(epio, MUSB_RXMAXP);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002296 musb_context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002297 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002298
2299 if (musb->dyn_fifo) {
2300 musb_context.index_regs[i].txfifoadd =
2301 musb_read_txfifoadd(musb_base);
2302 musb_context.index_regs[i].rxfifoadd =
2303 musb_read_rxfifoadd(musb_base);
2304 musb_context.index_regs[i].txfifosz =
2305 musb_read_txfifosz(musb_base);
2306 musb_context.index_regs[i].rxfifosz =
2307 musb_read_rxfifosz(musb_base);
2308 }
2309 if (is_host_enabled(musb)) {
2310 musb_context.index_regs[i].txtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002311 musb_readb(epio, MUSB_TXTYPE);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002312 musb_context.index_regs[i].txinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002313 musb_readb(epio, MUSB_TXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002314 musb_context.index_regs[i].rxtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002315 musb_readb(epio, MUSB_RXTYPE);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002316 musb_context.index_regs[i].rxinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002317 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002318
2319 musb_context.index_regs[i].txfunaddr =
2320 musb_read_txfunaddr(musb_base, i);
2321 musb_context.index_regs[i].txhubaddr =
2322 musb_read_txhubaddr(musb_base, i);
2323 musb_context.index_regs[i].txhubport =
2324 musb_read_txhubport(musb_base, i);
2325
2326 musb_context.index_regs[i].rxfunaddr =
2327 musb_read_rxfunaddr(musb_base, i);
2328 musb_context.index_regs[i].rxhubaddr =
2329 musb_read_rxhubaddr(musb_base, i);
2330 musb_context.index_regs[i].rxhubport =
2331 musb_read_rxhubport(musb_base, i);
2332 }
2333 }
2334
Felipe Balbi8573e6a2010-01-21 15:33:53 +02002335 musb_platform_save_context(musb, &musb_context);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002336}
2337
2338void musb_restore_context(struct musb *musb)
2339{
2340 int i;
2341 void __iomem *musb_base = musb->mregs;
2342 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002343 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002344
Felipe Balbi8573e6a2010-01-21 15:33:53 +02002345 musb_platform_restore_context(musb, &musb_context);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002346
2347 if (is_host_enabled(musb)) {
2348 musb_writew(musb_base, MUSB_FRAME, musb_context.frame);
2349 musb_writeb(musb_base, MUSB_TESTMODE, musb_context.testmode);
Ajay Kumar Gupta5e0e61a2010-03-25 13:14:26 +02002350 musb_write_ulpi_buscontrol(musb->mregs, musb_context.busctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002351 }
2352 musb_writeb(musb_base, MUSB_POWER, musb_context.power);
2353 musb_writew(musb_base, MUSB_INTRTXE, musb_context.intrtxe);
2354 musb_writew(musb_base, MUSB_INTRRXE, musb_context.intrrxe);
2355 musb_writeb(musb_base, MUSB_INTRUSBE, musb_context.intrusbe);
2356 musb_writeb(musb_base, MUSB_DEVCTL, musb_context.devctl);
2357
Bob Liuae9b2ad2010-09-24 13:44:07 +03002358 for (i = 0; i < musb->config->num_eps; ++i) {
2359 epio = musb->endpoints[i].regs;
2360 musb_writew(epio, MUSB_TXMAXP,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002361 musb_context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002362 musb_writew(epio, MUSB_TXCSR,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002363 musb_context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002364 musb_writew(epio, MUSB_RXMAXP,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002365 musb_context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002366 musb_writew(epio, MUSB_RXCSR,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002367 musb_context.index_regs[i].rxcsr);
2368
2369 if (musb->dyn_fifo) {
2370 musb_write_txfifosz(musb_base,
2371 musb_context.index_regs[i].txfifosz);
2372 musb_write_rxfifosz(musb_base,
2373 musb_context.index_regs[i].rxfifosz);
2374 musb_write_txfifoadd(musb_base,
2375 musb_context.index_regs[i].txfifoadd);
2376 musb_write_rxfifoadd(musb_base,
2377 musb_context.index_regs[i].rxfifoadd);
2378 }
2379
2380 if (is_host_enabled(musb)) {
Bob Liuae9b2ad2010-09-24 13:44:07 +03002381 musb_writeb(epio, MUSB_TXTYPE,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002382 musb_context.index_regs[i].txtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002383 musb_writeb(epio, MUSB_TXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002384 musb_context.index_regs[i].txinterval);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002385 musb_writeb(epio, MUSB_RXTYPE,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002386 musb_context.index_regs[i].rxtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002387 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002388
2389 musb_context.index_regs[i].rxinterval);
2390 musb_write_txfunaddr(musb_base, i,
2391 musb_context.index_regs[i].txfunaddr);
2392 musb_write_txhubaddr(musb_base, i,
2393 musb_context.index_regs[i].txhubaddr);
2394 musb_write_txhubport(musb_base, i,
2395 musb_context.index_regs[i].txhubport);
2396
2397 ep_target_regs =
2398 musb_read_target_reg_base(i, musb_base);
2399
2400 musb_write_rxfunaddr(ep_target_regs,
2401 musb_context.index_regs[i].rxfunaddr);
2402 musb_write_rxhubaddr(ep_target_regs,
2403 musb_context.index_regs[i].rxhubaddr);
2404 musb_write_rxhubport(ep_target_regs,
2405 musb_context.index_regs[i].rxhubport);
2406 }
2407 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002408}
2409
Magnus Damm48fea962009-07-08 13:22:56 +02002410static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002411{
Magnus Damm48fea962009-07-08 13:22:56 +02002412 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002413 unsigned long flags;
2414 struct musb *musb = dev_to_musb(&pdev->dev);
2415
2416 if (!musb->clock)
2417 return 0;
2418
2419 spin_lock_irqsave(&musb->lock, flags);
2420
2421 if (is_peripheral_active(musb)) {
2422 /* FIXME force disconnect unless we know USB will wake
2423 * the system up quickly enough to respond ...
2424 */
2425 } else if (is_host_active(musb)) {
2426 /* we know all the children are suspended; sometimes
2427 * they will even be wakeup-enabled.
2428 */
2429 }
2430
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002431 musb_save_context(musb);
2432
Felipe Balbi550a7372008-07-24 12:27:36 +03002433 if (musb->set_clock)
2434 musb->set_clock(musb->clock, 0);
2435 else
2436 clk_disable(musb->clock);
2437 spin_unlock_irqrestore(&musb->lock, flags);
2438 return 0;
2439}
2440
Magnus Damm48fea962009-07-08 13:22:56 +02002441static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002442{
Magnus Damm48fea962009-07-08 13:22:56 +02002443 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002444 struct musb *musb = dev_to_musb(&pdev->dev);
2445
2446 if (!musb->clock)
2447 return 0;
2448
Felipe Balbi550a7372008-07-24 12:27:36 +03002449 if (musb->set_clock)
2450 musb->set_clock(musb->clock, 1);
2451 else
2452 clk_enable(musb->clock);
2453
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002454 musb_restore_context(musb);
2455
Felipe Balbi550a7372008-07-24 12:27:36 +03002456 /* for static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002457 * unless for some reason the whole soc powered down or the USB
2458 * module got reset through the PSC (vs just being disabled).
Felipe Balbi550a7372008-07-24 12:27:36 +03002459 */
Felipe Balbi550a7372008-07-24 12:27:36 +03002460 return 0;
2461}
2462
Alexey Dobriyan47145212009-12-14 18:00:08 -08002463static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002464 .suspend = musb_suspend,
2465 .resume_noirq = musb_resume_noirq,
2466};
2467
2468#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002469#else
Magnus Damm48fea962009-07-08 13:22:56 +02002470#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002471#endif
2472
2473static struct platform_driver musb_driver = {
2474 .driver = {
2475 .name = (char *)musb_driver_name,
2476 .bus = &platform_bus_type,
2477 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002478 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002479 },
Felipe Balbie3060b12009-12-15 11:08:41 +02002480 .remove = __exit_p(musb_remove),
Felipe Balbi550a7372008-07-24 12:27:36 +03002481 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002482};
2483
2484/*-------------------------------------------------------------------------*/
2485
2486static int __init musb_init(void)
2487{
2488#ifdef CONFIG_USB_MUSB_HDRC_HCD
2489 if (usb_disabled())
2490 return 0;
2491#endif
2492
2493 pr_info("%s: version " MUSB_VERSION ", "
2494#ifdef CONFIG_MUSB_PIO_ONLY
2495 "pio"
2496#elif defined(CONFIG_USB_TI_CPPI_DMA)
2497 "cppi-dma"
2498#elif defined(CONFIG_USB_INVENTRA_DMA)
2499 "musb-dma"
2500#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2501 "tusb-omap-dma"
2502#else
2503 "?dma?"
2504#endif
2505 ", "
2506#ifdef CONFIG_USB_MUSB_OTG
2507 "otg (peripheral+host)"
2508#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2509 "peripheral"
2510#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2511 "host"
2512#endif
2513 ", debug=%d\n",
Felipe Balbib60c72a2008-10-29 15:10:39 +02002514 musb_driver_name, musb_debug);
Felipe Balbi550a7372008-07-24 12:27:36 +03002515 return platform_driver_probe(&musb_driver, musb_probe);
2516}
2517
David Brownell34f32c92009-02-20 13:45:17 -08002518/* make us init after usbcore and i2c (transceivers, regulators, etc)
2519 * and before usb gadget and host-side drivers start to register
Felipe Balbi550a7372008-07-24 12:27:36 +03002520 */
David Brownell34f32c92009-02-20 13:45:17 -08002521fs_initcall(musb_init);
Felipe Balbi550a7372008-07-24 12:27:36 +03002522
2523static void __exit musb_cleanup(void)
2524{
2525 platform_driver_unregister(&musb_driver);
2526}
2527module_exit(musb_cleanup);