blob: c4893267b4e2e6fd67079464ab82a42bac151250 [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
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200152#if !defined(CONFIG_USB_TUSB6010) && !defined(CONFIG_BLACKFIN)
153
Felipe Balbi550a7372008-07-24 12:27:36 +0300154/*
155 * Load an endpoint's FIFO
156 */
157void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
158{
159 void __iomem *fifo = hw_ep->fifo;
160
161 prefetch((u8 *)src);
162
163 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
164 'T', hw_ep->epnum, fifo, len, src);
165
166 /* we can't assume unaligned reads work */
167 if (likely((0x01 & (unsigned long) src) == 0)) {
168 u16 index = 0;
169
170 /* best case is 32bit-aligned source address */
171 if ((0x02 & (unsigned long) src) == 0) {
172 if (len >= 4) {
173 writesl(fifo, src + index, len >> 2);
174 index += len & ~0x03;
175 }
176 if (len & 0x02) {
177 musb_writew(fifo, 0, *(u16 *)&src[index]);
178 index += 2;
179 }
180 } else {
181 if (len >= 2) {
182 writesw(fifo, src + index, len >> 1);
183 index += len & ~0x01;
184 }
185 }
186 if (len & 0x01)
187 musb_writeb(fifo, 0, src[index]);
188 } else {
189 /* byte aligned */
190 writesb(fifo, src, len);
191 }
192}
193
194/*
195 * Unload an endpoint's FIFO
196 */
197void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
198{
199 void __iomem *fifo = hw_ep->fifo;
200
201 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
202 'R', hw_ep->epnum, fifo, len, dst);
203
204 /* we can't assume unaligned writes work */
205 if (likely((0x01 & (unsigned long) dst) == 0)) {
206 u16 index = 0;
207
208 /* best case is 32bit-aligned destination address */
209 if ((0x02 & (unsigned long) dst) == 0) {
210 if (len >= 4) {
211 readsl(fifo, dst, len >> 2);
212 index = len & ~0x03;
213 }
214 if (len & 0x02) {
215 *(u16 *)&dst[index] = musb_readw(fifo, 0);
216 index += 2;
217 }
218 } else {
219 if (len >= 2) {
220 readsw(fifo, dst, len >> 1);
221 index = len & ~0x01;
222 }
223 }
224 if (len & 0x01)
225 dst[index] = musb_readb(fifo, 0);
226 } else {
227 /* byte aligned */
228 readsb(fifo, dst, len);
229 }
230}
231
232#endif /* normal PIO */
233
234
235/*-------------------------------------------------------------------------*/
236
237/* for high speed test mode; see USB 2.0 spec 7.1.20 */
238static const u8 musb_test_packet[53] = {
239 /* implicit SYNC then DATA0 to start */
240
241 /* JKJKJKJK x9 */
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 /* JJKKJJKK x8 */
244 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
245 /* JJJJKKKK x8 */
246 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
247 /* JJJJJJJKKKKKKK x8 */
248 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
249 /* JJJJJJJK x8 */
250 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
251 /* JKKKKKKK x10, JK */
252 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
253
254 /* implicit CRC16 then EOP to end */
255};
256
257void musb_load_testpacket(struct musb *musb)
258{
259 void __iomem *regs = musb->endpoints[0].regs;
260
261 musb_ep_select(musb->mregs, 0);
262 musb_write_fifo(musb->control_ep,
263 sizeof(musb_test_packet), musb_test_packet);
264 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
265}
266
267/*-------------------------------------------------------------------------*/
268
269const char *otg_state_string(struct musb *musb)
270{
David Brownell84e250f2009-03-31 12:30:04 -0700271 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300272 case OTG_STATE_A_IDLE: return "a_idle";
273 case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise";
274 case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon";
275 case OTG_STATE_A_HOST: return "a_host";
276 case OTG_STATE_A_SUSPEND: return "a_suspend";
277 case OTG_STATE_A_PERIPHERAL: return "a_peripheral";
278 case OTG_STATE_A_WAIT_VFALL: return "a_wait_vfall";
279 case OTG_STATE_A_VBUS_ERR: return "a_vbus_err";
280 case OTG_STATE_B_IDLE: return "b_idle";
281 case OTG_STATE_B_SRP_INIT: return "b_srp_init";
282 case OTG_STATE_B_PERIPHERAL: return "b_peripheral";
283 case OTG_STATE_B_WAIT_ACON: return "b_wait_acon";
284 case OTG_STATE_B_HOST: return "b_host";
285 default: return "UNDEFINED";
286 }
287}
288
289#ifdef CONFIG_USB_MUSB_OTG
290
291/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300292 * Handles OTG hnp timeouts, such as b_ase0_brst
293 */
294void musb_otg_timer_func(unsigned long data)
295{
296 struct musb *musb = (struct musb *)data;
297 unsigned long flags;
298
299 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700300 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300301 case OTG_STATE_B_WAIT_ACON:
302 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
303 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700304 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300305 musb->is_active = 0;
306 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700307 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300308 case OTG_STATE_A_WAIT_BCON:
David Brownellab983f2a2009-03-31 12:35:09 -0700309 DBG(1, "HNP: %s timeout\n", otg_state_string(musb));
310 musb_set_vbus(musb, 0);
311 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300312 break;
313 default:
314 DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb));
315 }
316 musb->ignore_disconnect = 0;
317 spin_unlock_irqrestore(&musb->lock, flags);
318}
319
Felipe Balbi550a7372008-07-24 12:27:36 +0300320/*
David Brownellf7f9d632009-03-31 12:32:12 -0700321 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300322 */
323void musb_hnp_stop(struct musb *musb)
324{
325 struct usb_hcd *hcd = musb_to_hcd(musb);
326 void __iomem *mbase = musb->mregs;
327 u8 reg;
328
David Brownellab983f2a2009-03-31 12:35:09 -0700329 DBG(1, "HNP: stop from %s\n", otg_state_string(musb));
330
David Brownell84e250f2009-03-31 12:30:04 -0700331 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300332 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300333 musb_g_disconnect(musb);
David Brownellab983f2a2009-03-31 12:35:09 -0700334 DBG(1, "HNP: back to %s\n", otg_state_string(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300335 break;
336 case OTG_STATE_B_HOST:
337 DBG(1, "HNP: Disabling HR\n");
338 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700339 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300340 MUSB_DEV_MODE(musb);
341 reg = musb_readb(mbase, MUSB_POWER);
342 reg |= MUSB_POWER_SUSPENDM;
343 musb_writeb(mbase, MUSB_POWER, reg);
344 /* REVISIT: Start SESSION_REQUEST here? */
345 break;
346 default:
347 DBG(1, "HNP: Stopping in unknown state %s\n",
348 otg_state_string(musb));
349 }
350
351 /*
352 * When returning to A state after HNP, avoid hub_port_rebounce(),
353 * which cause occasional OPT A "Did not receive reset after connect"
354 * errors.
355 */
356 musb->port1_status &=
357 ~(1 << USB_PORT_FEAT_C_CONNECTION);
358}
359
360#endif
361
362/*
363 * Interrupt Service Routine to record USB "global" interrupts.
364 * Since these do not happen often and signify things of
365 * paramount importance, it seems OK to check them individually;
366 * the order of the tests is specified in the manual
367 *
368 * @param musb instance pointer
369 * @param int_usb register contents
370 * @param devctl
371 * @param power
372 */
373
374#define STAGE0_MASK (MUSB_INTR_RESUME | MUSB_INTR_SESSREQ \
375 | MUSB_INTR_VBUSERROR | MUSB_INTR_CONNECT \
376 | MUSB_INTR_RESET)
377
378static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
379 u8 devctl, u8 power)
380{
381 irqreturn_t handled = IRQ_NONE;
382 void __iomem *mbase = musb->mregs;
383
384 DBG(3, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
385 int_usb);
386
387 /* in host mode, the peripheral may issue remote wakeup.
388 * in peripheral mode, the host may resume the link.
389 * spurious RESUME irqs happen too, paired with SUSPEND.
390 */
391 if (int_usb & MUSB_INTR_RESUME) {
392 handled = IRQ_HANDLED;
393 DBG(3, "RESUME (%s)\n", otg_state_string(musb));
394
395 if (devctl & MUSB_DEVCTL_HM) {
396#ifdef CONFIG_USB_MUSB_HDRC_HCD
David Brownell84e250f2009-03-31 12:30:04 -0700397 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300398 case OTG_STATE_A_SUSPEND:
399 /* remote wakeup? later, GetPortStatus
400 * will stop RESUME signaling
401 */
402
403 if (power & MUSB_POWER_SUSPENDM) {
404 /* spurious */
405 musb->int_usb &= ~MUSB_INTR_SUSPEND;
406 DBG(2, "Spurious SUSPENDM\n");
407 break;
408 }
409
410 power &= ~MUSB_POWER_SUSPENDM;
411 musb_writeb(mbase, MUSB_POWER,
412 power | MUSB_POWER_RESUME);
413
414 musb->port1_status |=
415 (USB_PORT_STAT_C_SUSPEND << 16)
416 | MUSB_PORT_STAT_RESUME;
417 musb->rh_timer = jiffies
418 + msecs_to_jiffies(20);
419
David Brownell84e250f2009-03-31 12:30:04 -0700420 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300421 musb->is_active = 1;
422 usb_hcd_resume_root_hub(musb_to_hcd(musb));
423 break;
424 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700425 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300426 musb->is_active = 1;
427 MUSB_DEV_MODE(musb);
428 break;
429 default:
430 WARNING("bogus %s RESUME (%s)\n",
431 "host",
432 otg_state_string(musb));
433 }
434#endif
435 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700436 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300437#ifdef CONFIG_USB_MUSB_HDRC_HCD
438 case OTG_STATE_A_SUSPEND:
439 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700440 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300441 usb_hcd_resume_root_hub(musb_to_hcd(musb));
442 break;
443#endif
444#ifdef CONFIG_USB_GADGET_MUSB_HDRC
445 case OTG_STATE_B_WAIT_ACON:
446 case OTG_STATE_B_PERIPHERAL:
447 /* disconnect while suspended? we may
448 * not get a disconnect irq...
449 */
450 if ((devctl & MUSB_DEVCTL_VBUS)
451 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
452 ) {
453 musb->int_usb |= MUSB_INTR_DISCONNECT;
454 musb->int_usb &= ~MUSB_INTR_SUSPEND;
455 break;
456 }
457 musb_g_resume(musb);
458 break;
459 case OTG_STATE_B_IDLE:
460 musb->int_usb &= ~MUSB_INTR_SUSPEND;
461 break;
462#endif
463 default:
464 WARNING("bogus %s RESUME (%s)\n",
465 "peripheral",
466 otg_state_string(musb));
467 }
468 }
469 }
470
471#ifdef CONFIG_USB_MUSB_HDRC_HCD
472 /* see manual for the order of the tests */
473 if (int_usb & MUSB_INTR_SESSREQ) {
474 DBG(1, "SESSION_REQUEST (%s)\n", otg_state_string(musb));
475
476 /* IRQ arrives from ID pin sense or (later, if VBUS power
477 * is removed) SRP. responses are time critical:
478 * - turn on VBUS (with silicon-specific mechanism)
479 * - go through A_WAIT_VRISE
480 * - ... to A_WAIT_BCON.
481 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
482 */
483 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
484 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700485 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300486 MUSB_HST_MODE(musb);
487 musb_set_vbus(musb, 1);
488
489 handled = IRQ_HANDLED;
490 }
491
492 if (int_usb & MUSB_INTR_VBUSERROR) {
493 int ignore = 0;
494
495 /* During connection as an A-Device, we may see a short
496 * current spikes causing voltage drop, because of cable
497 * and peripheral capacitance combined with vbus draw.
498 * (So: less common with truly self-powered devices, where
499 * vbus doesn't act like a power supply.)
500 *
501 * Such spikes are short; usually less than ~500 usec, max
502 * of ~2 msec. That is, they're not sustained overcurrent
503 * errors, though they're reported using VBUSERROR irqs.
504 *
505 * Workarounds: (a) hardware: use self powered devices.
506 * (b) software: ignore non-repeated VBUS errors.
507 *
508 * REVISIT: do delays from lots of DEBUG_KERNEL checks
509 * make trouble here, keeping VBUS < 4.4V ?
510 */
David Brownell84e250f2009-03-31 12:30:04 -0700511 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300512 case OTG_STATE_A_HOST:
513 /* recovery is dicey once we've gotten past the
514 * initial stages of enumeration, but if VBUS
515 * stayed ok at the other end of the link, and
516 * another reset is due (at least for high speed,
517 * to redo the chirp etc), it might work OK...
518 */
519 case OTG_STATE_A_WAIT_BCON:
520 case OTG_STATE_A_WAIT_VRISE:
521 if (musb->vbuserr_retry) {
522 musb->vbuserr_retry--;
523 ignore = 1;
524 devctl |= MUSB_DEVCTL_SESSION;
525 musb_writeb(mbase, MUSB_DEVCTL, devctl);
526 } else {
527 musb->port1_status |=
528 (1 << USB_PORT_FEAT_OVER_CURRENT)
529 | (1 << USB_PORT_FEAT_C_OVER_CURRENT);
530 }
531 break;
532 default:
533 break;
534 }
535
536 DBG(1, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
537 otg_state_string(musb),
538 devctl,
539 ({ char *s;
540 switch (devctl & MUSB_DEVCTL_VBUS) {
541 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
542 s = "<SessEnd"; break;
543 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
544 s = "<AValid"; break;
545 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
546 s = "<VBusValid"; break;
547 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
548 default:
549 s = "VALID"; break;
550 }; s; }),
551 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
552 musb->port1_status);
553
554 /* go through A_WAIT_VFALL then start a new session */
555 if (!ignore)
556 musb_set_vbus(musb, 0);
557 handled = IRQ_HANDLED;
558 }
559
560 if (int_usb & MUSB_INTR_CONNECT) {
561 struct usb_hcd *hcd = musb_to_hcd(musb);
562
563 handled = IRQ_HANDLED;
564 musb->is_active = 1;
565 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
566
567 musb->ep0_stage = MUSB_EP0_START;
568
569#ifdef CONFIG_USB_MUSB_OTG
570 /* flush endpoints when transitioning from Device Mode */
571 if (is_peripheral_active(musb)) {
572 /* REVISIT HNP; just force disconnect */
573 }
574 musb_writew(mbase, MUSB_INTRTXE, musb->epmask);
575 musb_writew(mbase, MUSB_INTRRXE, musb->epmask & 0xfffe);
576 musb_writeb(mbase, MUSB_INTRUSBE, 0xf7);
577#endif
578 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
579 |USB_PORT_STAT_HIGH_SPEED
580 |USB_PORT_STAT_ENABLE
581 );
582 musb->port1_status |= USB_PORT_STAT_CONNECTION
583 |(USB_PORT_STAT_C_CONNECTION << 16);
584
585 /* high vs full speed is just a guess until after reset */
586 if (devctl & MUSB_DEVCTL_LSDEV)
587 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
588
Felipe Balbi550a7372008-07-24 12:27:36 +0300589 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700590 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300591 case OTG_STATE_B_PERIPHERAL:
592 if (int_usb & MUSB_INTR_SUSPEND) {
593 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300594 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700595 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300596 } else
597 DBG(1, "CONNECT as b_peripheral???\n");
598 break;
599 case OTG_STATE_B_WAIT_ACON:
David Brownell1de00da2009-04-02 10:16:11 -0700600 DBG(1, "HNP: CONNECT, now b_host\n");
601b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700602 musb->xceiv->state = OTG_STATE_B_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300603 hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700604 musb->ignore_disconnect = 0;
605 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300606 break;
607 default:
608 if ((devctl & MUSB_DEVCTL_VBUS)
609 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700610 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300611 hcd->self.is_b_host = 0;
612 }
613 break;
614 }
David Brownell1de00da2009-04-02 10:16:11 -0700615
616 /* poke the root hub */
617 MUSB_HST_MODE(musb);
618 if (hcd->status_urb)
619 usb_hcd_poll_rh_status(hcd);
620 else
621 usb_hcd_resume_root_hub(hcd);
622
Felipe Balbi550a7372008-07-24 12:27:36 +0300623 DBG(1, "CONNECT (%s) devctl %02x\n",
624 otg_state_string(musb), devctl);
625 }
626#endif /* CONFIG_USB_MUSB_HDRC_HCD */
627
628 /* mentor saves a bit: bus reset and babble share the same irq.
629 * only host sees babble; only peripheral sees bus reset.
630 */
631 if (int_usb & MUSB_INTR_RESET) {
632 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
633 /*
634 * Looks like non-HS BABBLE can be ignored, but
635 * HS BABBLE is an error condition. For HS the solution
636 * is to avoid babble in the first place and fix what
637 * caused BABBLE. When HS BABBLE happens we can only
638 * stop the session.
639 */
640 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
641 DBG(1, "BABBLE devctl: %02x\n", devctl);
642 else {
643 ERR("Stopping host session -- babble\n");
644 musb_writeb(mbase, MUSB_DEVCTL, 0);
645 }
646 } else if (is_peripheral_capable()) {
647 DBG(1, "BUS RESET as %s\n", otg_state_string(musb));
David Brownell84e250f2009-03-31 12:30:04 -0700648 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300649#ifdef CONFIG_USB_OTG
650 case OTG_STATE_A_SUSPEND:
651 /* We need to ignore disconnect on suspend
652 * otherwise tusb 2.0 won't reconnect after a
653 * power cycle, which breaks otg compliance.
654 */
655 musb->ignore_disconnect = 1;
656 musb_g_reset(musb);
657 /* FALLTHROUGH */
658 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
David Brownellf7f9d632009-03-31 12:32:12 -0700659 /* never use invalid T(a_wait_bcon) */
660 DBG(1, "HNP: in %s, %d msec timeout\n",
661 otg_state_string(musb),
662 TA_WAIT_BCON(musb));
663 mod_timer(&musb->otg_timer, jiffies
664 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
Felipe Balbi550a7372008-07-24 12:27:36 +0300665 break;
666 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -0700667 musb->ignore_disconnect = 0;
668 del_timer(&musb->otg_timer);
669 musb_g_reset(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300670 break;
671 case OTG_STATE_B_WAIT_ACON:
672 DBG(1, "HNP: RESET (%s), to b_peripheral\n",
673 otg_state_string(musb));
David Brownell84e250f2009-03-31 12:30:04 -0700674 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300675 musb_g_reset(musb);
676 break;
677#endif
678 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -0700679 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300680 /* FALLTHROUGH */
681 case OTG_STATE_B_PERIPHERAL:
682 musb_g_reset(musb);
683 break;
684 default:
685 DBG(1, "Unhandled BUS RESET as %s\n",
686 otg_state_string(musb));
687 }
688 }
689
690 handled = IRQ_HANDLED;
691 }
692 schedule_work(&musb->irq_work);
693
694 return handled;
695}
696
697/*
698 * Interrupt Service Routine to record USB "global" interrupts.
699 * Since these do not happen often and signify things of
700 * paramount importance, it seems OK to check them individually;
701 * the order of the tests is specified in the manual
702 *
703 * @param musb instance pointer
704 * @param int_usb register contents
705 * @param devctl
706 * @param power
707 */
708static irqreturn_t musb_stage2_irq(struct musb *musb, u8 int_usb,
709 u8 devctl, u8 power)
710{
711 irqreturn_t handled = IRQ_NONE;
712
713#if 0
714/* REVISIT ... this would be for multiplexing periodic endpoints, or
715 * supporting transfer phasing to prevent exceeding ISO bandwidth
716 * limits of a given frame or microframe.
717 *
718 * It's not needed for peripheral side, which dedicates endpoints;
719 * though it _might_ use SOF irqs for other purposes.
720 *
721 * And it's not currently needed for host side, which also dedicates
722 * endpoints, relies on TX/RX interval registers, and isn't claimed
723 * to support ISO transfers yet.
724 */
725 if (int_usb & MUSB_INTR_SOF) {
726 void __iomem *mbase = musb->mregs;
727 struct musb_hw_ep *ep;
728 u8 epnum;
729 u16 frame;
730
731 DBG(6, "START_OF_FRAME\n");
732 handled = IRQ_HANDLED;
733
734 /* start any periodic Tx transfers waiting for current frame */
735 frame = musb_readw(mbase, MUSB_FRAME);
736 ep = musb->endpoints;
737 for (epnum = 1; (epnum < musb->nr_endpoints)
738 && (musb->epmask >= (1 << epnum));
739 epnum++, ep++) {
740 /*
741 * FIXME handle framecounter wraps (12 bits)
742 * eliminate duplicated StartUrb logic
743 */
744 if (ep->dwWaitFrame >= frame) {
745 ep->dwWaitFrame = 0;
746 pr_debug("SOF --> periodic TX%s on %d\n",
747 ep->tx_channel ? " DMA" : "",
748 epnum);
749 if (!ep->tx_channel)
750 musb_h_tx_start(musb, epnum);
751 else
752 cppi_hostdma_start(musb, epnum);
753 }
754 } /* end of for loop */
755 }
756#endif
757
758 if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
759 DBG(1, "DISCONNECT (%s) as %s, devctl %02x\n",
760 otg_state_string(musb),
761 MUSB_MODE(musb), devctl);
762 handled = IRQ_HANDLED;
763
David Brownell84e250f2009-03-31 12:30:04 -0700764 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300765#ifdef CONFIG_USB_MUSB_HDRC_HCD
766 case OTG_STATE_A_HOST:
767 case OTG_STATE_A_SUSPEND:
Anand Gadiyar5c23c902009-02-21 15:31:40 -0800768 usb_hcd_resume_root_hub(musb_to_hcd(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300769 musb_root_disconnect(musb);
Ajay Kumar Gupta74382172009-02-24 15:29:04 -0800770 if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +0300771 musb_platform_try_idle(musb, jiffies
772 + msecs_to_jiffies(musb->a_wait_bcon));
773 break;
774#endif /* HOST */
775#ifdef CONFIG_USB_MUSB_OTG
776 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700777 /* REVISIT this behaves for "real disconnect"
778 * cases; make sure the other transitions from
779 * from B_HOST act right too. The B_HOST code
780 * in hnp_stop() is currently not used...
781 */
782 musb_root_disconnect(musb);
783 musb_to_hcd(musb)->self.is_b_host = 0;
784 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
785 MUSB_DEV_MODE(musb);
786 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300787 break;
788 case OTG_STATE_A_PERIPHERAL:
789 musb_hnp_stop(musb);
790 musb_root_disconnect(musb);
791 /* FALLTHROUGH */
792 case OTG_STATE_B_WAIT_ACON:
793 /* FALLTHROUGH */
794#endif /* OTG */
795#ifdef CONFIG_USB_GADGET_MUSB_HDRC
796 case OTG_STATE_B_PERIPHERAL:
797 case OTG_STATE_B_IDLE:
798 musb_g_disconnect(musb);
799 break;
800#endif /* GADGET */
801 default:
802 WARNING("unhandled DISCONNECT transition (%s)\n",
803 otg_state_string(musb));
804 break;
805 }
806
807 schedule_work(&musb->irq_work);
808 }
809
810 if (int_usb & MUSB_INTR_SUSPEND) {
811 DBG(1, "SUSPEND (%s) devctl %02x power %02x\n",
812 otg_state_string(musb), devctl, power);
813 handled = IRQ_HANDLED;
814
David Brownell84e250f2009-03-31 12:30:04 -0700815 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300816#ifdef CONFIG_USB_MUSB_OTG
817 case OTG_STATE_A_PERIPHERAL:
David Brownellab983f2a2009-03-31 12:35:09 -0700818 /* We also come here if the cable is removed, since
819 * this silicon doesn't report ID-no-longer-grounded.
820 *
821 * We depend on T(a_wait_bcon) to shut us down, and
822 * hope users don't do anything dicey during this
823 * undesired detour through A_WAIT_BCON.
Felipe Balbi550a7372008-07-24 12:27:36 +0300824 */
David Brownellab983f2a2009-03-31 12:35:09 -0700825 musb_hnp_stop(musb);
826 usb_hcd_resume_root_hub(musb_to_hcd(musb));
827 musb_root_disconnect(musb);
828 musb_platform_try_idle(musb, jiffies
829 + msecs_to_jiffies(musb->a_wait_bcon
830 ? : OTG_TIME_A_WAIT_BCON));
Felipe Balbi550a7372008-07-24 12:27:36 +0300831 break;
832#endif
833 case OTG_STATE_B_PERIPHERAL:
834 musb_g_suspend(musb);
835 musb->is_active = is_otg_enabled(musb)
David Brownell84e250f2009-03-31 12:30:04 -0700836 && musb->xceiv->gadget->b_hnp_enable;
Felipe Balbi550a7372008-07-24 12:27:36 +0300837 if (musb->is_active) {
838#ifdef CONFIG_USB_MUSB_OTG
David Brownell84e250f2009-03-31 12:30:04 -0700839 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
Felipe Balbi550a7372008-07-24 12:27:36 +0300840 DBG(1, "HNP: Setting timer for b_ase0_brst\n");
David Brownellf7f9d632009-03-31 12:32:12 -0700841 mod_timer(&musb->otg_timer, jiffies
842 + msecs_to_jiffies(
843 OTG_TIME_B_ASE0_BRST));
Felipe Balbi550a7372008-07-24 12:27:36 +0300844#endif
845 }
846 break;
847 case OTG_STATE_A_WAIT_BCON:
848 if (musb->a_wait_bcon != 0)
849 musb_platform_try_idle(musb, jiffies
850 + msecs_to_jiffies(musb->a_wait_bcon));
851 break;
852 case OTG_STATE_A_HOST:
David Brownell84e250f2009-03-31 12:30:04 -0700853 musb->xceiv->state = OTG_STATE_A_SUSPEND;
Felipe Balbi550a7372008-07-24 12:27:36 +0300854 musb->is_active = is_otg_enabled(musb)
David Brownell84e250f2009-03-31 12:30:04 -0700855 && musb->xceiv->host->b_hnp_enable;
Felipe Balbi550a7372008-07-24 12:27:36 +0300856 break;
857 case OTG_STATE_B_HOST:
858 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
859 DBG(1, "REVISIT: SUSPEND as B_HOST\n");
860 break;
861 default:
862 /* "should not happen" */
863 musb->is_active = 0;
864 break;
865 }
866 schedule_work(&musb->irq_work);
867 }
868
869
870 return handled;
871}
872
873/*-------------------------------------------------------------------------*/
874
875/*
876* Program the HDRC to start (enable interrupts, dma, etc.).
877*/
878void musb_start(struct musb *musb)
879{
880 void __iomem *regs = musb->mregs;
881 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
882
883 DBG(2, "<== devctl %02x\n", devctl);
884
885 /* Set INT enable registers, enable interrupts */
886 musb_writew(regs, MUSB_INTRTXE, musb->epmask);
887 musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
888 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
889
890 musb_writeb(regs, MUSB_TESTMODE, 0);
891
892 /* put into basic highspeed mode and start session */
893 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
894 | MUSB_POWER_SOFTCONN
895 | MUSB_POWER_HSENAB
896 /* ENSUSPEND wedges tusb */
897 /* | MUSB_POWER_ENSUSPEND */
898 );
899
900 musb->is_active = 0;
901 devctl = musb_readb(regs, MUSB_DEVCTL);
902 devctl &= ~MUSB_DEVCTL_SESSION;
903
904 if (is_otg_enabled(musb)) {
905 /* session started after:
906 * (a) ID-grounded irq, host mode;
907 * (b) vbus present/connect IRQ, peripheral mode;
908 * (c) peripheral initiates, using SRP
909 */
910 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
911 musb->is_active = 1;
912 else
913 devctl |= MUSB_DEVCTL_SESSION;
914
915 } else if (is_host_enabled(musb)) {
916 /* assume ID pin is hard-wired to ground */
917 devctl |= MUSB_DEVCTL_SESSION;
918
919 } else /* peripheral is enabled */ {
920 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
921 musb->is_active = 1;
922 }
923 musb_platform_enable(musb);
924 musb_writeb(regs, MUSB_DEVCTL, devctl);
925}
926
927
928static void musb_generic_disable(struct musb *musb)
929{
930 void __iomem *mbase = musb->mregs;
931 u16 temp;
932
933 /* disable interrupts */
934 musb_writeb(mbase, MUSB_INTRUSBE, 0);
935 musb_writew(mbase, MUSB_INTRTXE, 0);
936 musb_writew(mbase, MUSB_INTRRXE, 0);
937
938 /* off */
939 musb_writeb(mbase, MUSB_DEVCTL, 0);
940
941 /* flush pending interrupts */
942 temp = musb_readb(mbase, MUSB_INTRUSB);
943 temp = musb_readw(mbase, MUSB_INTRTX);
944 temp = musb_readw(mbase, MUSB_INTRRX);
945
946}
947
948/*
949 * Make the HDRC stop (disable interrupts, etc.);
950 * reversible by musb_start
951 * called on gadget driver unregister
952 * with controller locked, irqs blocked
953 * acts as a NOP unless some role activated the hardware
954 */
955void musb_stop(struct musb *musb)
956{
957 /* stop IRQs, timers, ... */
958 musb_platform_disable(musb);
959 musb_generic_disable(musb);
960 DBG(3, "HDRC disabled\n");
961
962 /* FIXME
963 * - mark host and/or peripheral drivers unusable/inactive
964 * - disable DMA (and enable it in HdrcStart)
965 * - make sure we can musb_start() after musb_stop(); with
966 * OTG mode, gadget driver module rmmod/modprobe cycles that
967 * - ...
968 */
969 musb_platform_try_idle(musb, 0);
970}
971
972static void musb_shutdown(struct platform_device *pdev)
973{
974 struct musb *musb = dev_to_musb(&pdev->dev);
975 unsigned long flags;
976
977 spin_lock_irqsave(&musb->lock, flags);
978 musb_platform_disable(musb);
979 musb_generic_disable(musb);
980 if (musb->clock) {
981 clk_put(musb->clock);
982 musb->clock = NULL;
983 }
984 spin_unlock_irqrestore(&musb->lock, flags);
985
986 /* FIXME power down */
987}
988
989
990/*-------------------------------------------------------------------------*/
991
992/*
993 * The silicon either has hard-wired endpoint configurations, or else
994 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +0300995 * writing only the dynamic sizing is very well tested. Since we switched
996 * away from compile-time hardware parameters, we can no longer rely on
997 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +0300998 *
999 * We don't currently use dynamic fifo setup capability to do anything
1000 * more than selecting one of a bunch of predefined configurations.
1001 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001002#if defined(CONFIG_USB_TUSB6010) || \
1003 defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
1004static ushort __initdata fifo_mode = 4;
1005#else
1006static ushort __initdata fifo_mode = 2;
1007#endif
1008
1009/* "modprobe ... fifo_mode=1" etc */
1010module_param(fifo_mode, ushort, 0);
1011MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1012
1013
Felipe Balbi550a7372008-07-24 12:27:36 +03001014enum fifo_style { FIFO_RXTX, FIFO_TX, FIFO_RX } __attribute__ ((packed));
1015enum buf_mode { BUF_SINGLE, BUF_DOUBLE } __attribute__ ((packed));
1016
1017struct fifo_cfg {
1018 u8 hw_ep_num;
1019 enum fifo_style style;
1020 enum buf_mode mode;
1021 u16 maxpacket;
1022};
1023
1024/*
1025 * tables defining fifo_mode values. define more if you like.
1026 * for host side, make sure both halves of ep1 are set up.
1027 */
1028
1029/* mode 0 - fits in 2KB */
1030static struct fifo_cfg __initdata mode_0_cfg[] = {
1031{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1032{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1033{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1034{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1035{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1036};
1037
1038/* mode 1 - fits in 4KB */
1039static struct fifo_cfg __initdata mode_1_cfg[] = {
1040{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1041{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1042{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1043{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1044{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1045};
1046
1047/* mode 2 - fits in 4KB */
1048static struct fifo_cfg __initdata mode_2_cfg[] = {
1049{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1050{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1051{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1052{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1053{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1054{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1055};
1056
1057/* mode 3 - fits in 4KB */
1058static struct fifo_cfg __initdata mode_3_cfg[] = {
1059{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1060{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1061{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1062{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1063{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1064{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1065};
1066
1067/* mode 4 - fits in 16KB */
1068static struct fifo_cfg __initdata mode_4_cfg[] = {
1069{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1070{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1071{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1072{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1073{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1074{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1075{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1076{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1077{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1078{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1079{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1080{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1081{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1082{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1083{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1084{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1085{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1086{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001087{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1088{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1089{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1090{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1091{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1092{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1093{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001094{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1095{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1096};
1097
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001098/* mode 5 - fits in 8KB */
1099static struct fifo_cfg __initdata mode_5_cfg[] = {
1100{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1101{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1102{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1103{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1104{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1105{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1106{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1107{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1108{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1109{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1110{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1111{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1112{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1113{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1114{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1115{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1116{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1117{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1118{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1119{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1120{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1121{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1122{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1123{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1124{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1125{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1126{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1127};
Felipe Balbi550a7372008-07-24 12:27:36 +03001128
1129/*
1130 * configure a fifo; for non-shared endpoints, this may be called
1131 * once for a tx fifo and once for an rx fifo.
1132 *
1133 * returns negative errno or offset for next fifo.
1134 */
1135static int __init
1136fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
1137 const struct fifo_cfg *cfg, u16 offset)
1138{
1139 void __iomem *mbase = musb->mregs;
1140 int size = 0;
1141 u16 maxpacket = cfg->maxpacket;
1142 u16 c_off = offset >> 3;
1143 u8 c_size;
1144
1145 /* expect hw_ep has already been zero-initialized */
1146
1147 size = ffs(max(maxpacket, (u16) 8)) - 1;
1148 maxpacket = 1 << size;
1149
1150 c_size = size - 3;
1151 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001152 if ((offset + (maxpacket << 1)) >
1153 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001154 return -EMSGSIZE;
1155 c_size |= MUSB_FIFOSZ_DPB;
1156 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001157 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001158 return -EMSGSIZE;
1159 }
1160
1161 /* configure the FIFO */
1162 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1163
1164#ifdef CONFIG_USB_MUSB_HDRC_HCD
1165 /* EP0 reserved endpoint for control, bidirectional;
1166 * EP1 reserved for bulk, two unidirection halves.
1167 */
1168 if (hw_ep->epnum == 1)
1169 musb->bulk_ep = hw_ep;
1170 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1171#endif
1172 switch (cfg->style) {
1173 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001174 musb_write_txfifosz(mbase, c_size);
1175 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001176 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1177 hw_ep->max_packet_sz_tx = maxpacket;
1178 break;
1179 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001180 musb_write_rxfifosz(mbase, c_size);
1181 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001182 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1183 hw_ep->max_packet_sz_rx = maxpacket;
1184 break;
1185 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001186 musb_write_txfifosz(mbase, c_size);
1187 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001188 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1189 hw_ep->max_packet_sz_rx = maxpacket;
1190
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001191 musb_write_rxfifosz(mbase, c_size);
1192 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001193 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1194 hw_ep->max_packet_sz_tx = maxpacket;
1195
1196 hw_ep->is_shared_fifo = true;
1197 break;
1198 }
1199
1200 /* NOTE rx and tx endpoint irqs aren't managed separately,
1201 * which happens to be ok
1202 */
1203 musb->epmask |= (1 << hw_ep->epnum);
1204
1205 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1206}
1207
1208static struct fifo_cfg __initdata ep0_cfg = {
1209 .style = FIFO_RXTX, .maxpacket = 64,
1210};
1211
1212static int __init ep_config_from_table(struct musb *musb)
1213{
1214 const struct fifo_cfg *cfg;
1215 unsigned i, n;
1216 int offset;
1217 struct musb_hw_ep *hw_ep = musb->endpoints;
1218
1219 switch (fifo_mode) {
1220 default:
1221 fifo_mode = 0;
1222 /* FALLTHROUGH */
1223 case 0:
1224 cfg = mode_0_cfg;
1225 n = ARRAY_SIZE(mode_0_cfg);
1226 break;
1227 case 1:
1228 cfg = mode_1_cfg;
1229 n = ARRAY_SIZE(mode_1_cfg);
1230 break;
1231 case 2:
1232 cfg = mode_2_cfg;
1233 n = ARRAY_SIZE(mode_2_cfg);
1234 break;
1235 case 3:
1236 cfg = mode_3_cfg;
1237 n = ARRAY_SIZE(mode_3_cfg);
1238 break;
1239 case 4:
1240 cfg = mode_4_cfg;
1241 n = ARRAY_SIZE(mode_4_cfg);
1242 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001243 case 5:
1244 cfg = mode_5_cfg;
1245 n = ARRAY_SIZE(mode_5_cfg);
1246 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001247 }
1248
1249 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1250 musb_driver_name, fifo_mode);
1251
1252
1253 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1254 /* assert(offset > 0) */
1255
1256 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001257 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001258 */
1259
1260 for (i = 0; i < n; i++) {
1261 u8 epn = cfg->hw_ep_num;
1262
Felipe Balbica6d1b12008-08-08 12:40:54 +03001263 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001264 pr_debug("%s: invalid ep %d\n",
1265 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001266 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001267 }
1268 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1269 if (offset < 0) {
1270 pr_debug("%s: mem overrun, ep %d\n",
1271 musb_driver_name, epn);
1272 return -EINVAL;
1273 }
1274 epn++;
1275 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1276 }
1277
1278 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1279 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001280 n + 1, musb->config->num_eps * 2 - 1,
1281 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001282
1283#ifdef CONFIG_USB_MUSB_HDRC_HCD
1284 if (!musb->bulk_ep) {
1285 pr_debug("%s: missing bulk\n", musb_driver_name);
1286 return -EINVAL;
1287 }
1288#endif
1289
1290 return 0;
1291}
1292
1293
1294/*
1295 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1296 * @param musb the controller
1297 */
1298static int __init ep_config_from_hw(struct musb *musb)
1299{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001300 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001301 struct musb_hw_ep *hw_ep;
1302 void *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001303 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001304
1305 DBG(2, "<== static silicon ep config\n");
1306
1307 /* FIXME pick up ep0 maxpacket size */
1308
Felipe Balbica6d1b12008-08-08 12:40:54 +03001309 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001310 musb_ep_select(mbase, epnum);
1311 hw_ep = musb->endpoints + epnum;
1312
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001313 ret = musb_read_fifosize(musb, hw_ep, epnum);
1314 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001315 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001316
1317 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1318
1319#ifdef CONFIG_USB_MUSB_HDRC_HCD
1320 /* pick an RX/TX endpoint for bulk */
1321 if (hw_ep->max_packet_sz_tx < 512
1322 || hw_ep->max_packet_sz_rx < 512)
1323 continue;
1324
1325 /* REVISIT: this algorithm is lazy, we should at least
1326 * try to pick a double buffered endpoint.
1327 */
1328 if (musb->bulk_ep)
1329 continue;
1330 musb->bulk_ep = hw_ep;
1331#endif
1332 }
1333
1334#ifdef CONFIG_USB_MUSB_HDRC_HCD
1335 if (!musb->bulk_ep) {
1336 pr_debug("%s: missing bulk\n", musb_driver_name);
1337 return -EINVAL;
1338 }
1339#endif
1340
1341 return 0;
1342}
1343
1344enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1345
1346/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1347 * configure endpoints, or take their config from silicon
1348 */
1349static int __init musb_core_init(u16 musb_type, struct musb *musb)
1350{
Felipe Balbi550a7372008-07-24 12:27:36 +03001351 u8 reg;
1352 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301353 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001354 void __iomem *mbase = musb->mregs;
1355 int status = 0;
1356 int i;
1357
1358 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001359 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001360
1361 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
1362 if (reg & MUSB_CONFIGDATA_DYNFIFO)
1363 strcat(aInfo, ", dyn FIFOs");
1364 if (reg & MUSB_CONFIGDATA_MPRXE) {
1365 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001366 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001367 }
1368 if (reg & MUSB_CONFIGDATA_MPTXE) {
1369 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001370 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001371 }
1372 if (reg & MUSB_CONFIGDATA_HBRXE) {
1373 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001374 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001375 }
1376 if (reg & MUSB_CONFIGDATA_HBTXE) {
1377 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001378 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001379 }
1380 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1381 strcat(aInfo, ", SoftConn");
1382
1383 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1384 musb_driver_name, reg, aInfo);
1385
Felipe Balbi550a7372008-07-24 12:27:36 +03001386 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001387 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1388 musb->is_multipoint = 1;
1389 type = "M";
1390 } else {
1391 musb->is_multipoint = 0;
1392 type = "";
1393#ifdef CONFIG_USB_MUSB_HDRC_HCD
1394#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1395 printk(KERN_ERR
1396 "%s: kernel must blacklist external hubs\n",
1397 musb_driver_name);
1398#endif
1399#endif
1400 }
1401
1402 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301403 musb->hwvers = musb_read_hwvers(mbase);
1404 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1405 MUSB_HWVERS_MINOR(musb->hwvers),
1406 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001407 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1408 musb_driver_name, type, aRevision, aDate);
1409
1410 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001411 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001412
1413 /* discover endpoint configuration */
1414 musb->nr_endpoints = 1;
1415 musb->epmask = 1;
1416
1417 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001418 if (musb->config->dyn_fifo)
Felipe Balbi550a7372008-07-24 12:27:36 +03001419 status = ep_config_from_table(musb);
1420 else {
1421 ERR("reconfigure software for Dynamic FIFOs\n");
1422 status = -ENODEV;
1423 }
1424 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001425 if (!musb->config->dyn_fifo)
Felipe Balbi550a7372008-07-24 12:27:36 +03001426 status = ep_config_from_hw(musb);
1427 else {
1428 ERR("reconfigure software for static FIFOs\n");
1429 return -ENODEV;
1430 }
1431 }
1432
1433 if (status < 0)
1434 return status;
1435
1436 /* finish init, and print endpoint config */
1437 for (i = 0; i < musb->nr_endpoints; i++) {
1438 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1439
1440 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
1441#ifdef CONFIG_USB_TUSB6010
1442 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1443 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1444 hw_ep->fifo_sync_va =
1445 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1446
1447 if (i == 0)
1448 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1449 else
1450 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1451#endif
1452
1453 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1454#ifdef CONFIG_USB_MUSB_HDRC_HCD
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001455 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001456 hw_ep->rx_reinit = 1;
1457 hw_ep->tx_reinit = 1;
1458#endif
1459
1460 if (hw_ep->max_packet_sz_tx) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301461 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001462 "%s: hw_ep %d%s, %smax %d\n",
1463 musb_driver_name, i,
1464 hw_ep->is_shared_fifo ? "shared" : "tx",
1465 hw_ep->tx_double_buffered
1466 ? "doublebuffer, " : "",
1467 hw_ep->max_packet_sz_tx);
1468 }
1469 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301470 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001471 "%s: hw_ep %d%s, %smax %d\n",
1472 musb_driver_name, i,
1473 "rx",
1474 hw_ep->rx_double_buffered
1475 ? "doublebuffer, " : "",
1476 hw_ep->max_packet_sz_rx);
1477 }
1478 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1479 DBG(1, "hw_ep %d not configured\n", i);
1480 }
1481
1482 return 0;
1483}
1484
1485/*-------------------------------------------------------------------------*/
1486
1487#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3430)
1488
1489static irqreturn_t generic_interrupt(int irq, void *__hci)
1490{
1491 unsigned long flags;
1492 irqreturn_t retval = IRQ_NONE;
1493 struct musb *musb = __hci;
1494
1495 spin_lock_irqsave(&musb->lock, flags);
1496
1497 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1498 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1499 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1500
1501 if (musb->int_usb || musb->int_tx || musb->int_rx)
1502 retval = musb_interrupt(musb);
1503
1504 spin_unlock_irqrestore(&musb->lock, flags);
1505
Sergei Shtylyova5073b52009-03-27 12:52:43 -07001506 return retval;
Felipe Balbi550a7372008-07-24 12:27:36 +03001507}
1508
1509#else
1510#define generic_interrupt NULL
1511#endif
1512
1513/*
1514 * handle all the irqs defined by the HDRC core. for now we expect: other
1515 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1516 * will be assigned, and the irq will already have been acked.
1517 *
1518 * called in irq context with spinlock held, irqs blocked
1519 */
1520irqreturn_t musb_interrupt(struct musb *musb)
1521{
1522 irqreturn_t retval = IRQ_NONE;
1523 u8 devctl, power;
1524 int ep_num;
1525 u32 reg;
1526
1527 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1528 power = musb_readb(musb->mregs, MUSB_POWER);
1529
1530 DBG(4, "** IRQ %s usb%04x tx%04x rx%04x\n",
1531 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1532 musb->int_usb, musb->int_tx, musb->int_rx);
1533
Felipe Balbicd42fef2009-12-15 13:47:30 +02001534#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1535 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
1536 if (!musb->gadget_driver) {
1537 DBG(5, "No gadget driver loaded\n");
1538 return IRQ_HANDLED;
1539 }
1540#endif
1541
Felipe Balbi550a7372008-07-24 12:27:36 +03001542 /* the core can interrupt us for multiple reasons; docs have
1543 * a generic interrupt flowchart to follow
1544 */
1545 if (musb->int_usb & STAGE0_MASK)
1546 retval |= musb_stage0_irq(musb, musb->int_usb,
1547 devctl, power);
1548
1549 /* "stage 1" is handling endpoint irqs */
1550
1551 /* handle endpoint 0 first */
1552 if (musb->int_tx & 1) {
1553 if (devctl & MUSB_DEVCTL_HM)
1554 retval |= musb_h_ep0_irq(musb);
1555 else
1556 retval |= musb_g_ep0_irq(musb);
1557 }
1558
1559 /* RX on endpoints 1-15 */
1560 reg = musb->int_rx >> 1;
1561 ep_num = 1;
1562 while (reg) {
1563 if (reg & 1) {
1564 /* musb_ep_select(musb->mregs, ep_num); */
1565 /* REVISIT just retval = ep->rx_irq(...) */
1566 retval = IRQ_HANDLED;
1567 if (devctl & MUSB_DEVCTL_HM) {
1568 if (is_host_capable())
1569 musb_host_rx(musb, ep_num);
1570 } else {
1571 if (is_peripheral_capable())
1572 musb_g_rx(musb, ep_num);
1573 }
1574 }
1575
1576 reg >>= 1;
1577 ep_num++;
1578 }
1579
1580 /* TX on endpoints 1-15 */
1581 reg = musb->int_tx >> 1;
1582 ep_num = 1;
1583 while (reg) {
1584 if (reg & 1) {
1585 /* musb_ep_select(musb->mregs, ep_num); */
1586 /* REVISIT just retval |= ep->tx_irq(...) */
1587 retval = IRQ_HANDLED;
1588 if (devctl & MUSB_DEVCTL_HM) {
1589 if (is_host_capable())
1590 musb_host_tx(musb, ep_num);
1591 } else {
1592 if (is_peripheral_capable())
1593 musb_g_tx(musb, ep_num);
1594 }
1595 }
1596 reg >>= 1;
1597 ep_num++;
1598 }
1599
1600 /* finish handling "global" interrupts after handling fifos */
1601 if (musb->int_usb)
1602 retval |= musb_stage2_irq(musb,
1603 musb->int_usb, devctl, power);
1604
1605 return retval;
1606}
1607
1608
1609#ifndef CONFIG_MUSB_PIO_ONLY
1610static int __initdata use_dma = 1;
1611
1612/* "modprobe ... use_dma=0" etc */
1613module_param(use_dma, bool, 0);
1614MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1615
1616void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1617{
1618 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1619
1620 /* called with controller lock already held */
1621
1622 if (!epnum) {
1623#ifndef CONFIG_USB_TUSB_OMAP_DMA
1624 if (!is_cppi_enabled()) {
1625 /* endpoint 0 */
1626 if (devctl & MUSB_DEVCTL_HM)
1627 musb_h_ep0_irq(musb);
1628 else
1629 musb_g_ep0_irq(musb);
1630 }
1631#endif
1632 } else {
1633 /* endpoints 1..15 */
1634 if (transmit) {
1635 if (devctl & MUSB_DEVCTL_HM) {
1636 if (is_host_capable())
1637 musb_host_tx(musb, epnum);
1638 } else {
1639 if (is_peripheral_capable())
1640 musb_g_tx(musb, epnum);
1641 }
1642 } else {
1643 /* receive */
1644 if (devctl & MUSB_DEVCTL_HM) {
1645 if (is_host_capable())
1646 musb_host_rx(musb, epnum);
1647 } else {
1648 if (is_peripheral_capable())
1649 musb_g_rx(musb, epnum);
1650 }
1651 }
1652 }
1653}
1654
1655#else
1656#define use_dma 0
1657#endif
1658
1659/*-------------------------------------------------------------------------*/
1660
1661#ifdef CONFIG_SYSFS
1662
1663static ssize_t
1664musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1665{
1666 struct musb *musb = dev_to_musb(dev);
1667 unsigned long flags;
1668 int ret = -EINVAL;
1669
1670 spin_lock_irqsave(&musb->lock, flags);
1671 ret = sprintf(buf, "%s\n", otg_state_string(musb));
1672 spin_unlock_irqrestore(&musb->lock, flags);
1673
1674 return ret;
1675}
1676
1677static ssize_t
1678musb_mode_store(struct device *dev, struct device_attribute *attr,
1679 const char *buf, size_t n)
1680{
1681 struct musb *musb = dev_to_musb(dev);
1682 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001683 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001684
1685 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001686 if (sysfs_streq(buf, "host"))
1687 status = musb_platform_set_mode(musb, MUSB_HOST);
1688 else if (sysfs_streq(buf, "peripheral"))
1689 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1690 else if (sysfs_streq(buf, "otg"))
1691 status = musb_platform_set_mode(musb, MUSB_OTG);
1692 else
1693 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001694 spin_unlock_irqrestore(&musb->lock, flags);
1695
David Brownell96a274d2008-11-24 13:06:47 +02001696 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001697}
1698static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1699
1700static ssize_t
1701musb_vbus_store(struct device *dev, struct device_attribute *attr,
1702 const char *buf, size_t n)
1703{
1704 struct musb *musb = dev_to_musb(dev);
1705 unsigned long flags;
1706 unsigned long val;
1707
1708 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001709 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001710 return -EINVAL;
1711 }
1712
1713 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001714 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1715 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001716 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001717 musb->is_active = 0;
1718 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1719 spin_unlock_irqrestore(&musb->lock, flags);
1720
1721 return n;
1722}
1723
1724static ssize_t
1725musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1726{
1727 struct musb *musb = dev_to_musb(dev);
1728 unsigned long flags;
1729 unsigned long val;
1730 int vbus;
1731
1732 spin_lock_irqsave(&musb->lock, flags);
1733 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001734 /* FIXME get_vbus_status() is normally #defined as false...
1735 * and is effectively TUSB-specific.
1736 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001737 vbus = musb_platform_get_vbus_status(musb);
1738 spin_unlock_irqrestore(&musb->lock, flags);
1739
David Brownellf7f9d632009-03-31 12:32:12 -07001740 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001741 vbus ? "on" : "off", val);
1742}
1743static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1744
1745#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1746
1747/* Gadget drivers can't know that a host is connected so they might want
1748 * to start SRP, but users can. This allows userspace to trigger SRP.
1749 */
1750static ssize_t
1751musb_srp_store(struct device *dev, struct device_attribute *attr,
1752 const char *buf, size_t n)
1753{
1754 struct musb *musb = dev_to_musb(dev);
1755 unsigned short srp;
1756
1757 if (sscanf(buf, "%hu", &srp) != 1
1758 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001759 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001760 return -EINVAL;
1761 }
1762
1763 if (srp == 1)
1764 musb_g_wakeup(musb);
1765
1766 return n;
1767}
1768static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1769
1770#endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1771
Felipe Balbi94375752009-12-15 11:08:38 +02001772static struct attribute *musb_attributes[] = {
1773 &dev_attr_mode.attr,
1774 &dev_attr_vbus.attr,
1775#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1776 &dev_attr_srp.attr,
1777#endif
1778 NULL
1779};
1780
1781static const struct attribute_group musb_attr_group = {
1782 .attrs = musb_attributes,
1783};
1784
Felipe Balbi550a7372008-07-24 12:27:36 +03001785#endif /* sysfs */
1786
1787/* Only used to provide driver mode change events */
1788static void musb_irq_work(struct work_struct *data)
1789{
1790 struct musb *musb = container_of(data, struct musb, irq_work);
1791 static int old_state;
1792
David Brownell84e250f2009-03-31 12:30:04 -07001793 if (musb->xceiv->state != old_state) {
1794 old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001795 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1796 }
1797}
1798
1799/* --------------------------------------------------------------------------
1800 * Init support
1801 */
1802
1803static struct musb *__init
Felipe Balbica6d1b12008-08-08 12:40:54 +03001804allocate_instance(struct device *dev,
1805 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001806{
1807 struct musb *musb;
1808 struct musb_hw_ep *ep;
1809 int epnum;
1810#ifdef CONFIG_USB_MUSB_HDRC_HCD
1811 struct usb_hcd *hcd;
1812
Kay Sievers427c4f32008-11-07 01:52:53 +01001813 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
Felipe Balbi550a7372008-07-24 12:27:36 +03001814 if (!hcd)
1815 return NULL;
1816 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1817
1818 musb = hcd_to_musb(hcd);
1819 INIT_LIST_HEAD(&musb->control);
1820 INIT_LIST_HEAD(&musb->in_bulk);
1821 INIT_LIST_HEAD(&musb->out_bulk);
1822
1823 hcd->uses_new_polling = 1;
1824
1825 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001826 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001827#else
1828 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1829 if (!musb)
1830 return NULL;
1831 dev_set_drvdata(dev, musb);
1832
1833#endif
1834
1835 musb->mregs = mbase;
1836 musb->ctrl_base = mbase;
1837 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001838 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001839 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001840 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001841 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001842 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001843 ep->musb = musb;
1844 ep->epnum = epnum;
1845 }
1846
1847 musb->controller = dev;
1848 return musb;
1849}
1850
1851static void musb_free(struct musb *musb)
1852{
1853 /* this has multiple entry modes. it handles fault cleanup after
1854 * probe(), where things may be partially set up, as well as rmmod
1855 * cleanup after everything's been de-activated.
1856 */
1857
1858#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001859 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001860#endif
1861
1862#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1863 musb_gadget_cleanup(musb);
1864#endif
1865
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001866 if (musb->nIrq >= 0) {
1867 if (musb->irq_wake)
1868 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001869 free_irq(musb->nIrq, musb);
1870 }
1871 if (is_dma_capable() && musb->dma_controller) {
1872 struct dma_controller *c = musb->dma_controller;
1873
1874 (void) c->stop(c);
1875 dma_controller_destroy(c);
1876 }
1877
Ajay Kumar Guptac740d0d2009-08-03 11:43:40 +05301878#ifdef CONFIG_USB_MUSB_OTG
1879 put_device(musb->xceiv->dev);
1880#endif
1881
Felipe Balbi550a7372008-07-24 12:27:36 +03001882 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1883 musb_platform_exit(musb);
1884 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1885
1886 if (musb->clock) {
1887 clk_disable(musb->clock);
1888 clk_put(musb->clock);
1889 }
1890
Felipe Balbi550a7372008-07-24 12:27:36 +03001891#ifdef CONFIG_USB_MUSB_HDRC_HCD
1892 usb_put_hcd(musb_to_hcd(musb));
1893#else
1894 kfree(musb);
1895#endif
1896}
1897
1898/*
1899 * Perform generic per-controller initialization.
1900 *
1901 * @pDevice: the controller (already clocked, etc)
1902 * @nIrq: irq
1903 * @mregs: virtual address of controller registers,
1904 * not yet corrected for platform-specific offsets
1905 */
1906static int __init
1907musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1908{
1909 int status;
1910 struct musb *musb;
1911 struct musb_hdrc_platform_data *plat = dev->platform_data;
1912
1913 /* The driver might handle more features than the board; OK.
1914 * Fail when the board needs a feature that's not enabled.
1915 */
1916 if (!plat) {
1917 dev_dbg(dev, "no platform_data?\n");
1918 return -ENODEV;
1919 }
1920 switch (plat->mode) {
1921 case MUSB_HOST:
1922#ifdef CONFIG_USB_MUSB_HDRC_HCD
1923 break;
1924#else
1925 goto bad_config;
1926#endif
1927 case MUSB_PERIPHERAL:
1928#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1929 break;
1930#else
1931 goto bad_config;
1932#endif
1933 case MUSB_OTG:
1934#ifdef CONFIG_USB_MUSB_OTG
1935 break;
1936#else
1937bad_config:
1938#endif
1939 default:
1940 dev_err(dev, "incompatible Kconfig role setting\n");
1941 return -EINVAL;
1942 }
1943
1944 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001945 musb = allocate_instance(dev, plat->config, ctrl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001946 if (!musb)
1947 return -ENOMEM;
1948
1949 spin_lock_init(&musb->lock);
1950 musb->board_mode = plat->mode;
1951 musb->board_set_power = plat->set_power;
1952 musb->set_clock = plat->set_clock;
1953 musb->min_power = plat->min_power;
1954
1955 /* Clock usage is chip-specific ... functional clock (DaVinci,
1956 * OMAP2430), or PHY ref (some TUSB6010 boards). All this core
1957 * code does is make sure a clock handle is available; platform
1958 * code manages it during start/stop and suspend/resume.
1959 */
1960 if (plat->clock) {
1961 musb->clock = clk_get(dev, plat->clock);
1962 if (IS_ERR(musb->clock)) {
1963 status = PTR_ERR(musb->clock);
1964 musb->clock = NULL;
1965 goto fail;
1966 }
1967 }
1968
David Brownell84e250f2009-03-31 12:30:04 -07001969 /* The musb_platform_init() call:
1970 * - adjusts musb->mregs and musb->isr if needed,
1971 * - may initialize an integrated tranceiver
1972 * - initializes musb->xceiv, usually by otg_get_transceiver()
1973 * - activates clocks.
1974 * - stops powering VBUS
1975 * - assigns musb->board_set_vbus if host mode is enabled
1976 *
1977 * There are various transciever configurations. Blackfin,
1978 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1979 * external/discrete ones in various flavors (twl4030 family,
1980 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03001981 */
1982 musb->isr = generic_interrupt;
1983 status = musb_platform_init(musb);
1984
1985 if (status < 0)
1986 goto fail;
1987 if (!musb->isr) {
1988 status = -ENODEV;
1989 goto fail2;
1990 }
1991
1992#ifndef CONFIG_MUSB_PIO_ONLY
1993 if (use_dma && dev->dma_mask) {
1994 struct dma_controller *c;
1995
1996 c = dma_controller_create(musb, musb->mregs);
1997 musb->dma_controller = c;
1998 if (c)
1999 (void) c->start(c);
2000 }
2001#endif
2002 /* ideally this would be abstracted in platform setup */
2003 if (!is_dma_capable() || !musb->dma_controller)
2004 dev->dma_mask = NULL;
2005
2006 /* be sure interrupts are disabled before connecting ISR */
2007 musb_platform_disable(musb);
2008 musb_generic_disable(musb);
2009
2010 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03002011 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03002012 ? MUSB_CONTROLLER_MHDRC
2013 : MUSB_CONTROLLER_HDRC, musb);
2014 if (status < 0)
2015 goto fail2;
2016
Amit Kucheria3a9f5bd2009-07-27 12:03:19 +03002017#ifdef CONFIG_USB_MUSB_OTG
David Brownellf7f9d632009-03-31 12:32:12 -07002018 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2019#endif
2020
Felipe Balbi550a7372008-07-24 12:27:36 +03002021 /* Init IRQ workqueue before request_irq */
2022 INIT_WORK(&musb->irq_work, musb_irq_work);
2023
2024 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002025 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002026 dev_err(dev, "request_irq %d failed!\n", nIrq);
2027 status = -ENODEV;
2028 goto fail2;
2029 }
2030 musb->nIrq = nIrq;
2031/* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002032 if (enable_irq_wake(nIrq) == 0) {
2033 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002034 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002035 } else {
2036 musb->irq_wake = 0;
2037 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002038
2039 pr_info("%s: USB %s mode controller at %p using %s, IRQ %d\n",
2040 musb_driver_name,
2041 ({char *s;
2042 switch (musb->board_mode) {
2043 case MUSB_HOST: s = "Host"; break;
2044 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2045 default: s = "OTG"; break;
2046 }; s; }),
2047 ctrl,
2048 (is_dma_capable() && musb->dma_controller)
2049 ? "DMA" : "PIO",
2050 musb->nIrq);
2051
David Brownell84e250f2009-03-31 12:30:04 -07002052 /* host side needs more setup */
2053 if (is_host_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002054 struct usb_hcd *hcd = musb_to_hcd(musb);
2055
David Brownell84e250f2009-03-31 12:30:04 -07002056 otg_set_host(musb->xceiv, &hcd->self);
2057
2058 if (is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002059 hcd->self.otg_port = 1;
David Brownell84e250f2009-03-31 12:30:04 -07002060 musb->xceiv->host = &hcd->self;
Felipe Balbi550a7372008-07-24 12:27:36 +03002061 hcd->power_budget = 2 * (plat->power ? : 250);
2062 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002063
2064 /* For the host-only role, we can activate right away.
2065 * (We expect the ID pin to be forcibly grounded!!)
2066 * Otherwise, wait till the gadget driver hooks up.
2067 */
2068 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
2069 MUSB_HST_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002070 musb->xceiv->default_a = 1;
2071 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002072
2073 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
Felipe Balbi746cdd02008-08-10 21:22:34 +03002074 if (status)
2075 goto fail;
Felipe Balbi550a7372008-07-24 12:27:36 +03002076
2077 DBG(1, "%s mode, status %d, devctl %02x %c\n",
2078 "HOST", status,
2079 musb_readb(musb->mregs, MUSB_DEVCTL),
2080 (musb_readb(musb->mregs, MUSB_DEVCTL)
2081 & MUSB_DEVCTL_BDEVICE
2082 ? 'B' : 'A'));
2083
2084 } else /* peripheral is enabled */ {
2085 MUSB_DEV_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002086 musb->xceiv->default_a = 0;
2087 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002088
2089 status = musb_gadget_setup(musb);
Felipe Balbi746cdd02008-08-10 21:22:34 +03002090 if (status)
2091 goto fail;
Felipe Balbi550a7372008-07-24 12:27:36 +03002092
2093 DBG(1, "%s mode, status %d, dev%02x\n",
2094 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2095 status,
2096 musb_readb(musb->mregs, MUSB_DEVCTL));
2097
2098 }
2099
Felipe Balbi550a7372008-07-24 12:27:36 +03002100#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02002101 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03002102#endif
Felipe Balbi28c2c512008-09-11 11:53:25 +03002103 if (status)
2104 goto fail2;
2105
2106 return 0;
2107
2108fail2:
Felipe Balbi28c2c512008-09-11 11:53:25 +03002109 musb_platform_exit(musb);
2110fail:
2111 dev_err(musb->controller,
2112 "musb_init_controller failed with status %d\n", status);
2113
2114 if (musb->clock)
2115 clk_put(musb->clock);
2116 device_init_wakeup(dev, 0);
2117 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002118
2119 return status;
2120
Felipe Balbi550a7372008-07-24 12:27:36 +03002121}
2122
2123/*-------------------------------------------------------------------------*/
2124
2125/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2126 * bridge to a platform device; this driver then suffices.
2127 */
2128
2129#ifndef CONFIG_MUSB_PIO_ONLY
2130static u64 *orig_dma_mask;
2131#endif
2132
2133static int __init musb_probe(struct platform_device *pdev)
2134{
2135 struct device *dev = &pdev->dev;
2136 int irq = platform_get_irq(pdev, 0);
2137 struct resource *iomem;
2138 void __iomem *base;
2139
2140 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2141 if (!iomem || irq == 0)
2142 return -ENODEV;
2143
Felipe Balbi195e9e42009-12-15 11:08:42 +02002144 base = ioremap(iomem->start, resource_size(iomem));
Felipe Balbi550a7372008-07-24 12:27:36 +03002145 if (!base) {
2146 dev_err(dev, "ioremap failed\n");
2147 return -ENOMEM;
2148 }
2149
2150#ifndef CONFIG_MUSB_PIO_ONLY
2151 /* clobbered by use_dma=n */
2152 orig_dma_mask = dev->dma_mask;
2153#endif
2154 return musb_init_controller(dev, irq, base);
2155}
2156
Felipe Balbie3060b12009-12-15 11:08:41 +02002157static int __exit musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002158{
2159 struct musb *musb = dev_to_musb(&pdev->dev);
2160 void __iomem *ctrl_base = musb->ctrl_base;
2161
2162 /* this gets called on rmmod.
2163 * - Host mode: host may still be active
2164 * - Peripheral mode: peripheral is deactivated (or never-activated)
2165 * - OTG mode: both roles are deactivated (or never-activated)
2166 */
2167 musb_shutdown(pdev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002168#ifdef CONFIG_USB_MUSB_HDRC_HCD
2169 if (musb->board_mode == MUSB_HOST)
2170 usb_remove_hcd(musb_to_hcd(musb));
2171#endif
2172 musb_free(musb);
2173 iounmap(ctrl_base);
2174 device_init_wakeup(&pdev->dev, 0);
2175#ifndef CONFIG_MUSB_PIO_ONLY
2176 pdev->dev.dma_mask = orig_dma_mask;
2177#endif
2178 return 0;
2179}
2180
2181#ifdef CONFIG_PM
2182
Magnus Damm48fea962009-07-08 13:22:56 +02002183static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002184{
Magnus Damm48fea962009-07-08 13:22:56 +02002185 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002186 unsigned long flags;
2187 struct musb *musb = dev_to_musb(&pdev->dev);
2188
2189 if (!musb->clock)
2190 return 0;
2191
2192 spin_lock_irqsave(&musb->lock, flags);
2193
2194 if (is_peripheral_active(musb)) {
2195 /* FIXME force disconnect unless we know USB will wake
2196 * the system up quickly enough to respond ...
2197 */
2198 } else if (is_host_active(musb)) {
2199 /* we know all the children are suspended; sometimes
2200 * they will even be wakeup-enabled.
2201 */
2202 }
2203
2204 if (musb->set_clock)
2205 musb->set_clock(musb->clock, 0);
2206 else
2207 clk_disable(musb->clock);
2208 spin_unlock_irqrestore(&musb->lock, flags);
2209 return 0;
2210}
2211
Magnus Damm48fea962009-07-08 13:22:56 +02002212static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002213{
Magnus Damm48fea962009-07-08 13:22:56 +02002214 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002215 struct musb *musb = dev_to_musb(&pdev->dev);
2216
2217 if (!musb->clock)
2218 return 0;
2219
Felipe Balbi550a7372008-07-24 12:27:36 +03002220 if (musb->set_clock)
2221 musb->set_clock(musb->clock, 1);
2222 else
2223 clk_enable(musb->clock);
2224
2225 /* for static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002226 * unless for some reason the whole soc powered down or the USB
2227 * module got reset through the PSC (vs just being disabled).
Felipe Balbi550a7372008-07-24 12:27:36 +03002228 */
Felipe Balbi550a7372008-07-24 12:27:36 +03002229 return 0;
2230}
2231
Alexey Dobriyan47145212009-12-14 18:00:08 -08002232static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002233 .suspend = musb_suspend,
2234 .resume_noirq = musb_resume_noirq,
2235};
2236
2237#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002238#else
Magnus Damm48fea962009-07-08 13:22:56 +02002239#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002240#endif
2241
2242static struct platform_driver musb_driver = {
2243 .driver = {
2244 .name = (char *)musb_driver_name,
2245 .bus = &platform_bus_type,
2246 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002247 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002248 },
Felipe Balbie3060b12009-12-15 11:08:41 +02002249 .remove = __exit_p(musb_remove),
Felipe Balbi550a7372008-07-24 12:27:36 +03002250 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002251};
2252
2253/*-------------------------------------------------------------------------*/
2254
2255static int __init musb_init(void)
2256{
2257#ifdef CONFIG_USB_MUSB_HDRC_HCD
2258 if (usb_disabled())
2259 return 0;
2260#endif
2261
2262 pr_info("%s: version " MUSB_VERSION ", "
2263#ifdef CONFIG_MUSB_PIO_ONLY
2264 "pio"
2265#elif defined(CONFIG_USB_TI_CPPI_DMA)
2266 "cppi-dma"
2267#elif defined(CONFIG_USB_INVENTRA_DMA)
2268 "musb-dma"
2269#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2270 "tusb-omap-dma"
2271#else
2272 "?dma?"
2273#endif
2274 ", "
2275#ifdef CONFIG_USB_MUSB_OTG
2276 "otg (peripheral+host)"
2277#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2278 "peripheral"
2279#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2280 "host"
2281#endif
2282 ", debug=%d\n",
Felipe Balbib60c72a2008-10-29 15:10:39 +02002283 musb_driver_name, musb_debug);
Felipe Balbi550a7372008-07-24 12:27:36 +03002284 return platform_driver_probe(&musb_driver, musb_probe);
2285}
2286
David Brownell34f32c92009-02-20 13:45:17 -08002287/* make us init after usbcore and i2c (transceivers, regulators, etc)
2288 * and before usb gadget and host-side drivers start to register
Felipe Balbi550a7372008-07-24 12:27:36 +03002289 */
David Brownell34f32c92009-02-20 13:45:17 -08002290fs_initcall(musb_init);
Felipe Balbi550a7372008-07-24 12:27:36 +03002291
2292static void __exit musb_cleanup(void)
2293{
2294 platform_driver_unregister(&musb_driver);
2295}
2296module_exit(musb_cleanup);