blob: 9a280872c2b47fed320c89c7ffd869a2177baa8e [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * MUSB OTG driver core code
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35/*
36 * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
37 *
38 * This consists of a Host Controller Driver (HCD) and a peripheral
39 * controller driver implementing the "Gadget" API; OTG support is
40 * in the works. These are normal Linux-USB controller drivers which
41 * use IRQs and have no dedicated thread.
42 *
43 * This version of the driver has only been used with products from
44 * Texas Instruments. Those products integrate the Inventra logic
45 * with other DMA, IRQ, and bus modules, as well as other logic that
46 * needs to be reflected in this driver.
47 *
48 *
49 * NOTE: the original Mentor code here was pretty much a collection
50 * of mechanisms that don't seem to have been fully integrated/working
51 * for *any* Linux kernel version. This version aims at Linux 2.6.now,
52 * Key open issues include:
53 *
54 * - Lack of host-side transaction scheduling, for all transfer types.
55 * The hardware doesn't do it; instead, software must.
56 *
57 * This is not an issue for OTG devices that don't support external
58 * hubs, but for more "normal" USB hosts it's a user issue that the
59 * "multipoint" support doesn't scale in the expected ways. That
60 * includes DaVinci EVM in a common non-OTG mode.
61 *
62 * * Control and bulk use dedicated endpoints, and there's as
63 * yet no mechanism to either (a) reclaim the hardware when
64 * peripherals are NAKing, which gets complicated with bulk
65 * endpoints, or (b) use more than a single bulk endpoint in
66 * each direction.
67 *
68 * RESULT: one device may be perceived as blocking another one.
69 *
70 * * Interrupt and isochronous will dynamically allocate endpoint
71 * hardware, but (a) there's no record keeping for bandwidth;
72 * (b) in the common case that few endpoints are available, there
73 * is no mechanism to reuse endpoints to talk to multiple devices.
74 *
75 * RESULT: At one extreme, bandwidth can be overcommitted in
76 * some hardware configurations, no faults will be reported.
77 * At the other extreme, the bandwidth capabilities which do
78 * exist tend to be severely undercommitted. You can't yet hook
79 * up both a keyboard and a mouse to an external USB hub.
80 */
81
82/*
83 * This gets many kinds of configuration information:
84 * - Kconfig for everything user-configurable
Felipe Balbi550a7372008-07-24 12:27:36 +030085 * - platform_device for addressing, irq, and platform_data
86 * - platform_data is mostly for board-specific informarion
David Brownellc767c1c2008-09-11 11:53:23 +030087 * (plus recentrly, SOC or family details)
Felipe Balbi550a7372008-07-24 12:27:36 +030088 *
89 * Most of the conditional compilation will (someday) vanish.
90 */
91
92#include <linux/module.h>
93#include <linux/kernel.h>
94#include <linux/sched.h>
95#include <linux/slab.h>
96#include <linux/init.h>
97#include <linux/list.h>
98#include <linux/kobject.h>
99#include <linux/platform_device.h>
100#include <linux/io.h>
101
Felipe Balbi550a7372008-07-24 12:27:36 +0300102#include "musb_core.h"
103
David Brownellf7f9d632009-03-31 12:32:12 -0700104#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +0300105
106
Felipe Balbib60c72a2008-10-29 15:10:39 +0200107unsigned musb_debug;
David Brownell34f32c92009-02-20 13:45:17 -0800108module_param_named(debug, musb_debug, uint, S_IRUGO | S_IWUSR);
Felipe Balbie8164f62008-08-10 21:22:35 +0300109MODULE_PARM_DESC(debug, "Debug message level. Default = 0");
Felipe Balbi550a7372008-07-24 12:27:36 +0300110
111#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
112#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
113
Felipe Balbie8164f62008-08-10 21:22:35 +0300114#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300115
116#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
117
Felipe Balbi05ac10d2010-12-02 08:49:26 +0200118#define MUSB_DRIVER_NAME "musb-hdrc"
Felipe Balbi550a7372008-07-24 12:27:36 +0300119const char musb_driver_name[] = MUSB_DRIVER_NAME;
120
121MODULE_DESCRIPTION(DRIVER_INFO);
122MODULE_AUTHOR(DRIVER_AUTHOR);
123MODULE_LICENSE("GPL");
124MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
125
126
127/*-------------------------------------------------------------------------*/
128
129static inline struct musb *dev_to_musb(struct device *dev)
130{
Felipe Balbi550a7372008-07-24 12:27:36 +0300131 return dev_get_drvdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300132}
133
134/*-------------------------------------------------------------------------*/
135
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200136#ifndef CONFIG_BLACKFIN
137static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
138{
139 void __iomem *addr = otg->io_priv;
140 int i = 0;
141 u8 r;
142 u8 power;
143
144 /* Make sure the transceiver is not in low power mode */
145 power = musb_readb(addr, MUSB_POWER);
146 power &= ~MUSB_POWER_SUSPENDM;
147 musb_writeb(addr, MUSB_POWER, power);
148
149 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
150 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
151 */
152
153 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
154 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
155 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
156
157 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
158 & MUSB_ULPI_REG_CMPLT)) {
159 i++;
160 if (i == 10000) {
161 DBG(3, "ULPI read timed out\n");
162 return -ETIMEDOUT;
163 }
164
165 }
166 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
167 r &= ~MUSB_ULPI_REG_CMPLT;
168 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
169
170 return musb_readb(addr, MUSB_ULPI_REG_DATA);
171}
172
173static int musb_ulpi_write(struct otg_transceiver *otg,
174 u32 offset, u32 data)
175{
176 void __iomem *addr = otg->io_priv;
177 int i = 0;
178 u8 r = 0;
179 u8 power;
180
181 /* Make sure the transceiver is not in low power mode */
182 power = musb_readb(addr, MUSB_POWER);
183 power &= ~MUSB_POWER_SUSPENDM;
184 musb_writeb(addr, MUSB_POWER, power);
185
186 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
187 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
188 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
189
190 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
191 & MUSB_ULPI_REG_CMPLT)) {
192 i++;
193 if (i == 10000) {
194 DBG(3, "ULPI write timed out\n");
195 return -ETIMEDOUT;
196 }
197 }
198
199 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
200 r &= ~MUSB_ULPI_REG_CMPLT;
201 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
202
203 return 0;
204}
205#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530206#define musb_ulpi_read NULL
207#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200208#endif
209
210static struct otg_io_access_ops musb_ulpi_access = {
211 .read = musb_ulpi_read,
212 .write = musb_ulpi_write,
213};
214
215/*-------------------------------------------------------------------------*/
216
Felipe Balbi7c925542010-12-01 14:23:48 +0200217#if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200218
Felipe Balbi550a7372008-07-24 12:27:36 +0300219/*
220 * Load an endpoint's FIFO
221 */
222void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
223{
224 void __iomem *fifo = hw_ep->fifo;
225
226 prefetch((u8 *)src);
227
228 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
229 'T', hw_ep->epnum, fifo, len, src);
230
231 /* we can't assume unaligned reads work */
232 if (likely((0x01 & (unsigned long) src) == 0)) {
233 u16 index = 0;
234
235 /* best case is 32bit-aligned source address */
236 if ((0x02 & (unsigned long) src) == 0) {
237 if (len >= 4) {
238 writesl(fifo, src + index, len >> 2);
239 index += len & ~0x03;
240 }
241 if (len & 0x02) {
242 musb_writew(fifo, 0, *(u16 *)&src[index]);
243 index += 2;
244 }
245 } else {
246 if (len >= 2) {
247 writesw(fifo, src + index, len >> 1);
248 index += len & ~0x01;
249 }
250 }
251 if (len & 0x01)
252 musb_writeb(fifo, 0, src[index]);
253 } else {
254 /* byte aligned */
255 writesb(fifo, src, len);
256 }
257}
258
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300259#if !defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +0300260/*
261 * Unload an endpoint's FIFO
262 */
263void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
264{
265 void __iomem *fifo = hw_ep->fifo;
266
267 DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
268 'R', hw_ep->epnum, fifo, len, dst);
269
270 /* we can't assume unaligned writes work */
271 if (likely((0x01 & (unsigned long) dst) == 0)) {
272 u16 index = 0;
273
274 /* best case is 32bit-aligned destination address */
275 if ((0x02 & (unsigned long) dst) == 0) {
276 if (len >= 4) {
277 readsl(fifo, dst, len >> 2);
278 index = len & ~0x03;
279 }
280 if (len & 0x02) {
281 *(u16 *)&dst[index] = musb_readw(fifo, 0);
282 index += 2;
283 }
284 } else {
285 if (len >= 2) {
286 readsw(fifo, dst, len >> 1);
287 index = len & ~0x01;
288 }
289 }
290 if (len & 0x01)
291 dst[index] = musb_readb(fifo, 0);
292 } else {
293 /* byte aligned */
294 readsb(fifo, dst, len);
295 }
296}
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300297#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300298
299#endif /* normal PIO */
300
301
302/*-------------------------------------------------------------------------*/
303
304/* for high speed test mode; see USB 2.0 spec 7.1.20 */
305static const u8 musb_test_packet[53] = {
306 /* implicit SYNC then DATA0 to start */
307
308 /* JKJKJKJK x9 */
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 /* JJKKJJKK x8 */
311 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
312 /* JJJJKKKK x8 */
313 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
314 /* JJJJJJJKKKKKKK x8 */
315 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
316 /* JJJJJJJK x8 */
317 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
318 /* JKKKKKKK x10, JK */
319 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
320
321 /* implicit CRC16 then EOP to end */
322};
323
324void musb_load_testpacket(struct musb *musb)
325{
326 void __iomem *regs = musb->endpoints[0].regs;
327
328 musb_ep_select(musb->mregs, 0);
329 musb_write_fifo(musb->control_ep,
330 sizeof(musb_test_packet), musb_test_packet);
331 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
332}
333
334/*-------------------------------------------------------------------------*/
335
Felipe Balbi550a7372008-07-24 12:27:36 +0300336#ifdef CONFIG_USB_MUSB_OTG
337
338/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300339 * Handles OTG hnp timeouts, such as b_ase0_brst
340 */
341void musb_otg_timer_func(unsigned long data)
342{
343 struct musb *musb = (struct musb *)data;
344 unsigned long flags;
345
346 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700347 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300348 case OTG_STATE_B_WAIT_ACON:
349 DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
350 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700351 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300352 musb->is_active = 0;
353 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700354 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300355 case OTG_STATE_A_WAIT_BCON:
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200356 DBG(1, "HNP: %s timeout\n",
357 otg_state_string(musb->xceiv->state));
Felipe Balbi743411b2010-12-01 13:22:05 +0200358 musb_platform_set_vbus(musb, 0);
David Brownellab983f2a2009-03-31 12:35:09 -0700359 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300360 break;
361 default:
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200362 DBG(1, "HNP: Unhandled mode %s\n",
363 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 }
365 musb->ignore_disconnect = 0;
366 spin_unlock_irqrestore(&musb->lock, flags);
367}
368
Felipe Balbi550a7372008-07-24 12:27:36 +0300369/*
David Brownellf7f9d632009-03-31 12:32:12 -0700370 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300371 */
372void musb_hnp_stop(struct musb *musb)
373{
374 struct usb_hcd *hcd = musb_to_hcd(musb);
375 void __iomem *mbase = musb->mregs;
376 u8 reg;
377
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200378 DBG(1, "HNP: stop from %s\n", otg_state_string(musb->xceiv->state));
David Brownellab983f2a2009-03-31 12:35:09 -0700379
David Brownell84e250f2009-03-31 12:30:04 -0700380 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300381 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300382 musb_g_disconnect(musb);
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200383 DBG(1, "HNP: back to %s\n",
384 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300385 break;
386 case OTG_STATE_B_HOST:
387 DBG(1, "HNP: Disabling HR\n");
388 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700389 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300390 MUSB_DEV_MODE(musb);
391 reg = musb_readb(mbase, MUSB_POWER);
392 reg |= MUSB_POWER_SUSPENDM;
393 musb_writeb(mbase, MUSB_POWER, reg);
394 /* REVISIT: Start SESSION_REQUEST here? */
395 break;
396 default:
397 DBG(1, "HNP: Stopping in unknown state %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200398 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300399 }
400
401 /*
402 * When returning to A state after HNP, avoid hub_port_rebounce(),
403 * which cause occasional OPT A "Did not receive reset after connect"
404 * errors.
405 */
Alan Stern749da5f2010-03-04 17:05:08 -0500406 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300407}
408
409#endif
410
411/*
412 * Interrupt Service Routine to record USB "global" interrupts.
413 * Since these do not happen often and signify things of
414 * paramount importance, it seems OK to check them individually;
415 * the order of the tests is specified in the manual
416 *
417 * @param musb instance pointer
418 * @param int_usb register contents
419 * @param devctl
420 * @param power
421 */
422
Felipe Balbi550a7372008-07-24 12:27:36 +0300423static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
424 u8 devctl, u8 power)
425{
426 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300427
428 DBG(3, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
429 int_usb);
430
431 /* in host mode, the peripheral may issue remote wakeup.
432 * in peripheral mode, the host may resume the link.
433 * spurious RESUME irqs happen too, paired with SUSPEND.
434 */
435 if (int_usb & MUSB_INTR_RESUME) {
436 handled = IRQ_HANDLED;
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200437 DBG(3, "RESUME (%s)\n", otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300438
439 if (devctl & MUSB_DEVCTL_HM) {
440#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbiaa471452010-03-12 10:27:24 +0200441 void __iomem *mbase = musb->mregs;
442
David Brownell84e250f2009-03-31 12:30:04 -0700443 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300444 case OTG_STATE_A_SUSPEND:
445 /* remote wakeup? later, GetPortStatus
446 * will stop RESUME signaling
447 */
448
449 if (power & MUSB_POWER_SUSPENDM) {
450 /* spurious */
451 musb->int_usb &= ~MUSB_INTR_SUSPEND;
452 DBG(2, "Spurious SUSPENDM\n");
453 break;
454 }
455
456 power &= ~MUSB_POWER_SUSPENDM;
457 musb_writeb(mbase, MUSB_POWER,
458 power | MUSB_POWER_RESUME);
459
460 musb->port1_status |=
461 (USB_PORT_STAT_C_SUSPEND << 16)
462 | MUSB_PORT_STAT_RESUME;
463 musb->rh_timer = jiffies
464 + msecs_to_jiffies(20);
465
David Brownell84e250f2009-03-31 12:30:04 -0700466 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300467 musb->is_active = 1;
468 usb_hcd_resume_root_hub(musb_to_hcd(musb));
469 break;
470 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700471 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300472 musb->is_active = 1;
473 MUSB_DEV_MODE(musb);
474 break;
475 default:
476 WARNING("bogus %s RESUME (%s)\n",
477 "host",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200478 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300479 }
480#endif
481 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700482 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300483#ifdef CONFIG_USB_MUSB_HDRC_HCD
484 case OTG_STATE_A_SUSPEND:
485 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700486 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300487 usb_hcd_resume_root_hub(musb_to_hcd(musb));
488 break;
489#endif
490#ifdef CONFIG_USB_GADGET_MUSB_HDRC
491 case OTG_STATE_B_WAIT_ACON:
492 case OTG_STATE_B_PERIPHERAL:
493 /* disconnect while suspended? we may
494 * not get a disconnect irq...
495 */
496 if ((devctl & MUSB_DEVCTL_VBUS)
497 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
498 ) {
499 musb->int_usb |= MUSB_INTR_DISCONNECT;
500 musb->int_usb &= ~MUSB_INTR_SUSPEND;
501 break;
502 }
503 musb_g_resume(musb);
504 break;
505 case OTG_STATE_B_IDLE:
506 musb->int_usb &= ~MUSB_INTR_SUSPEND;
507 break;
508#endif
509 default:
510 WARNING("bogus %s RESUME (%s)\n",
511 "peripheral",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200512 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300513 }
514 }
515 }
516
517#ifdef CONFIG_USB_MUSB_HDRC_HCD
518 /* see manual for the order of the tests */
519 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200520 void __iomem *mbase = musb->mregs;
521
Heikki Krogerus19aab562010-10-29 04:23:27 -0500522 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
523 && (devctl & MUSB_DEVCTL_BDEVICE)) {
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300524 DBG(3, "SessReq while on B state\n");
525 return IRQ_HANDLED;
526 }
527
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200528 DBG(1, "SESSION_REQUEST (%s)\n",
529 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300530
531 /* IRQ arrives from ID pin sense or (later, if VBUS power
532 * is removed) SRP. responses are time critical:
533 * - turn on VBUS (with silicon-specific mechanism)
534 * - go through A_WAIT_VRISE
535 * - ... to A_WAIT_BCON.
536 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
537 */
538 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
539 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700540 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300541 MUSB_HST_MODE(musb);
Felipe Balbi743411b2010-12-01 13:22:05 +0200542 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300543
544 handled = IRQ_HANDLED;
545 }
546
547 if (int_usb & MUSB_INTR_VBUSERROR) {
548 int ignore = 0;
549
550 /* During connection as an A-Device, we may see a short
551 * current spikes causing voltage drop, because of cable
552 * and peripheral capacitance combined with vbus draw.
553 * (So: less common with truly self-powered devices, where
554 * vbus doesn't act like a power supply.)
555 *
556 * Such spikes are short; usually less than ~500 usec, max
557 * of ~2 msec. That is, they're not sustained overcurrent
558 * errors, though they're reported using VBUSERROR irqs.
559 *
560 * Workarounds: (a) hardware: use self powered devices.
561 * (b) software: ignore non-repeated VBUS errors.
562 *
563 * REVISIT: do delays from lots of DEBUG_KERNEL checks
564 * make trouble here, keeping VBUS < 4.4V ?
565 */
David Brownell84e250f2009-03-31 12:30:04 -0700566 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300567 case OTG_STATE_A_HOST:
568 /* recovery is dicey once we've gotten past the
569 * initial stages of enumeration, but if VBUS
570 * stayed ok at the other end of the link, and
571 * another reset is due (at least for high speed,
572 * to redo the chirp etc), it might work OK...
573 */
574 case OTG_STATE_A_WAIT_BCON:
575 case OTG_STATE_A_WAIT_VRISE:
576 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200577 void __iomem *mbase = musb->mregs;
578
Felipe Balbi550a7372008-07-24 12:27:36 +0300579 musb->vbuserr_retry--;
580 ignore = 1;
581 devctl |= MUSB_DEVCTL_SESSION;
582 musb_writeb(mbase, MUSB_DEVCTL, devctl);
583 } else {
584 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500585 USB_PORT_STAT_OVERCURRENT
586 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300587 }
588 break;
589 default:
590 break;
591 }
592
593 DBG(1, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200594 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300595 devctl,
596 ({ char *s;
597 switch (devctl & MUSB_DEVCTL_VBUS) {
598 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
599 s = "<SessEnd"; break;
600 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
601 s = "<AValid"; break;
602 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
603 s = "<VBusValid"; break;
604 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
605 default:
606 s = "VALID"; break;
607 }; s; }),
608 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
609 musb->port1_status);
610
611 /* go through A_WAIT_VFALL then start a new session */
612 if (!ignore)
Felipe Balbi743411b2010-12-01 13:22:05 +0200613 musb_platform_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300614 handled = IRQ_HANDLED;
615 }
616
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530617#endif
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200618 if (int_usb & MUSB_INTR_SUSPEND) {
619 DBG(1, "SUSPEND (%s) devctl %02x power %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200620 otg_state_string(musb->xceiv->state), devctl, power);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200621 handled = IRQ_HANDLED;
622
623 switch (musb->xceiv->state) {
624#ifdef CONFIG_USB_MUSB_OTG
625 case OTG_STATE_A_PERIPHERAL:
626 /* We also come here if the cable is removed, since
627 * this silicon doesn't report ID-no-longer-grounded.
628 *
629 * We depend on T(a_wait_bcon) to shut us down, and
630 * hope users don't do anything dicey during this
631 * undesired detour through A_WAIT_BCON.
632 */
633 musb_hnp_stop(musb);
634 usb_hcd_resume_root_hub(musb_to_hcd(musb));
635 musb_root_disconnect(musb);
636 musb_platform_try_idle(musb, jiffies
637 + msecs_to_jiffies(musb->a_wait_bcon
638 ? : OTG_TIME_A_WAIT_BCON));
639
640 break;
641#endif
642 case OTG_STATE_B_IDLE:
643 if (!musb->is_active)
644 break;
645 case OTG_STATE_B_PERIPHERAL:
646 musb_g_suspend(musb);
647 musb->is_active = is_otg_enabled(musb)
648 && musb->xceiv->gadget->b_hnp_enable;
649 if (musb->is_active) {
650#ifdef CONFIG_USB_MUSB_OTG
651 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
652 DBG(1, "HNP: Setting timer for b_ase0_brst\n");
653 mod_timer(&musb->otg_timer, jiffies
654 + msecs_to_jiffies(
655 OTG_TIME_B_ASE0_BRST));
656#endif
657 }
658 break;
659 case OTG_STATE_A_WAIT_BCON:
660 if (musb->a_wait_bcon != 0)
661 musb_platform_try_idle(musb, jiffies
662 + msecs_to_jiffies(musb->a_wait_bcon));
663 break;
664 case OTG_STATE_A_HOST:
665 musb->xceiv->state = OTG_STATE_A_SUSPEND;
666 musb->is_active = is_otg_enabled(musb)
667 && musb->xceiv->host->b_hnp_enable;
668 break;
669 case OTG_STATE_B_HOST:
670 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
671 DBG(1, "REVISIT: SUSPEND as B_HOST\n");
672 break;
673 default:
674 /* "should not happen" */
675 musb->is_active = 0;
676 break;
677 }
678 }
679
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530680#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +0300681 if (int_usb & MUSB_INTR_CONNECT) {
682 struct usb_hcd *hcd = musb_to_hcd(musb);
683
684 handled = IRQ_HANDLED;
685 musb->is_active = 1;
686 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
687
688 musb->ep0_stage = MUSB_EP0_START;
689
690#ifdef CONFIG_USB_MUSB_OTG
691 /* flush endpoints when transitioning from Device Mode */
692 if (is_peripheral_active(musb)) {
693 /* REVISIT HNP; just force disconnect */
694 }
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530695 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
696 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
697 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300698#endif
699 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
700 |USB_PORT_STAT_HIGH_SPEED
701 |USB_PORT_STAT_ENABLE
702 );
703 musb->port1_status |= USB_PORT_STAT_CONNECTION
704 |(USB_PORT_STAT_C_CONNECTION << 16);
705
706 /* high vs full speed is just a guess until after reset */
707 if (devctl & MUSB_DEVCTL_LSDEV)
708 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
709
Felipe Balbi550a7372008-07-24 12:27:36 +0300710 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700711 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300712 case OTG_STATE_B_PERIPHERAL:
713 if (int_usb & MUSB_INTR_SUSPEND) {
714 DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300715 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700716 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300717 } else
718 DBG(1, "CONNECT as b_peripheral???\n");
719 break;
720 case OTG_STATE_B_WAIT_ACON:
David Brownell1de00da2009-04-02 10:16:11 -0700721 DBG(1, "HNP: CONNECT, now b_host\n");
722b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700723 musb->xceiv->state = OTG_STATE_B_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300724 hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700725 musb->ignore_disconnect = 0;
726 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300727 break;
728 default:
729 if ((devctl & MUSB_DEVCTL_VBUS)
730 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700731 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300732 hcd->self.is_b_host = 0;
733 }
734 break;
735 }
David Brownell1de00da2009-04-02 10:16:11 -0700736
737 /* poke the root hub */
738 MUSB_HST_MODE(musb);
739 if (hcd->status_urb)
740 usb_hcd_poll_rh_status(hcd);
741 else
742 usb_hcd_resume_root_hub(hcd);
743
Felipe Balbi550a7372008-07-24 12:27:36 +0300744 DBG(1, "CONNECT (%s) devctl %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200745 otg_state_string(musb->xceiv->state), devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300746 }
747#endif /* CONFIG_USB_MUSB_HDRC_HCD */
748
Felipe Balbi550a7372008-07-24 12:27:36 +0300749 if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
750 DBG(1, "DISCONNECT (%s) as %s, devctl %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200751 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300752 MUSB_MODE(musb), devctl);
753 handled = IRQ_HANDLED;
754
David Brownell84e250f2009-03-31 12:30:04 -0700755 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300756#ifdef CONFIG_USB_MUSB_HDRC_HCD
757 case OTG_STATE_A_HOST:
758 case OTG_STATE_A_SUSPEND:
Anand Gadiyar5c23c902009-02-21 15:31:40 -0800759 usb_hcd_resume_root_hub(musb_to_hcd(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300760 musb_root_disconnect(musb);
Ajay Kumar Gupta74382172009-02-24 15:29:04 -0800761 if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +0300762 musb_platform_try_idle(musb, jiffies
763 + msecs_to_jiffies(musb->a_wait_bcon));
764 break;
765#endif /* HOST */
766#ifdef CONFIG_USB_MUSB_OTG
767 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700768 /* REVISIT this behaves for "real disconnect"
769 * cases; make sure the other transitions from
770 * from B_HOST act right too. The B_HOST code
771 * in hnp_stop() is currently not used...
772 */
773 musb_root_disconnect(musb);
774 musb_to_hcd(musb)->self.is_b_host = 0;
775 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
776 MUSB_DEV_MODE(musb);
777 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300778 break;
779 case OTG_STATE_A_PERIPHERAL:
780 musb_hnp_stop(musb);
781 musb_root_disconnect(musb);
782 /* FALLTHROUGH */
783 case OTG_STATE_B_WAIT_ACON:
784 /* FALLTHROUGH */
785#endif /* OTG */
786#ifdef CONFIG_USB_GADGET_MUSB_HDRC
787 case OTG_STATE_B_PERIPHERAL:
788 case OTG_STATE_B_IDLE:
789 musb_g_disconnect(musb);
790 break;
791#endif /* GADGET */
792 default:
793 WARNING("unhandled DISCONNECT transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200794 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300795 break;
796 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300797 }
798
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200799 /* mentor saves a bit: bus reset and babble share the same irq.
800 * only host sees babble; only peripheral sees bus reset.
801 */
802 if (int_usb & MUSB_INTR_RESET) {
803 handled = IRQ_HANDLED;
804 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
805 /*
806 * Looks like non-HS BABBLE can be ignored, but
807 * HS BABBLE is an error condition. For HS the solution
808 * is to avoid babble in the first place and fix what
809 * caused BABBLE. When HS BABBLE happens we can only
810 * stop the session.
811 */
812 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
813 DBG(1, "BABBLE devctl: %02x\n", devctl);
814 else {
815 ERR("Stopping host session -- babble\n");
816 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
817 }
818 } else if (is_peripheral_capable()) {
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200819 DBG(1, "BUS RESET as %s\n",
820 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200821 switch (musb->xceiv->state) {
822#ifdef CONFIG_USB_OTG
823 case OTG_STATE_A_SUSPEND:
824 /* We need to ignore disconnect on suspend
825 * otherwise tusb 2.0 won't reconnect after a
826 * power cycle, which breaks otg compliance.
827 */
828 musb->ignore_disconnect = 1;
829 musb_g_reset(musb);
830 /* FALLTHROUGH */
831 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
832 /* never use invalid T(a_wait_bcon) */
833 DBG(1, "HNP: in %s, %d msec timeout\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200834 otg_state_string(musb->xceiv->state),
835 TA_WAIT_BCON(musb));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200836 mod_timer(&musb->otg_timer, jiffies
837 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
838 break;
839 case OTG_STATE_A_PERIPHERAL:
840 musb->ignore_disconnect = 0;
841 del_timer(&musb->otg_timer);
842 musb_g_reset(musb);
843 break;
844 case OTG_STATE_B_WAIT_ACON:
845 DBG(1, "HNP: RESET (%s), to b_peripheral\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200846 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200847 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
848 musb_g_reset(musb);
849 break;
850#endif
851 case OTG_STATE_B_IDLE:
852 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
853 /* FALLTHROUGH */
854 case OTG_STATE_B_PERIPHERAL:
855 musb_g_reset(musb);
856 break;
857 default:
858 DBG(1, "Unhandled BUS RESET as %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200859 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200860 }
861 }
862 }
863
864#if 0
865/* REVISIT ... this would be for multiplexing periodic endpoints, or
866 * supporting transfer phasing to prevent exceeding ISO bandwidth
867 * limits of a given frame or microframe.
868 *
869 * It's not needed for peripheral side, which dedicates endpoints;
870 * though it _might_ use SOF irqs for other purposes.
871 *
872 * And it's not currently needed for host side, which also dedicates
873 * endpoints, relies on TX/RX interval registers, and isn't claimed
874 * to support ISO transfers yet.
875 */
876 if (int_usb & MUSB_INTR_SOF) {
877 void __iomem *mbase = musb->mregs;
878 struct musb_hw_ep *ep;
879 u8 epnum;
880 u16 frame;
881
882 DBG(6, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300883 handled = IRQ_HANDLED;
884
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200885 /* start any periodic Tx transfers waiting for current frame */
886 frame = musb_readw(mbase, MUSB_FRAME);
887 ep = musb->endpoints;
888 for (epnum = 1; (epnum < musb->nr_endpoints)
889 && (musb->epmask >= (1 << epnum));
890 epnum++, ep++) {
891 /*
892 * FIXME handle framecounter wraps (12 bits)
893 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300894 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200895 if (ep->dwWaitFrame >= frame) {
896 ep->dwWaitFrame = 0;
897 pr_debug("SOF --> periodic TX%s on %d\n",
898 ep->tx_channel ? " DMA" : "",
899 epnum);
900 if (!ep->tx_channel)
901 musb_h_tx_start(musb, epnum);
902 else
903 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300904 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200905 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300906 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200907#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300908
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200909 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300910
911 return handled;
912}
913
914/*-------------------------------------------------------------------------*/
915
916/*
917* Program the HDRC to start (enable interrupts, dma, etc.).
918*/
919void musb_start(struct musb *musb)
920{
921 void __iomem *regs = musb->mregs;
922 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
923
924 DBG(2, "<== devctl %02x\n", devctl);
925
926 /* Set INT enable registers, enable interrupts */
927 musb_writew(regs, MUSB_INTRTXE, musb->epmask);
928 musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
929 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
930
931 musb_writeb(regs, MUSB_TESTMODE, 0);
932
933 /* put into basic highspeed mode and start session */
934 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
935 | MUSB_POWER_SOFTCONN
936 | MUSB_POWER_HSENAB
937 /* ENSUSPEND wedges tusb */
938 /* | MUSB_POWER_ENSUSPEND */
939 );
940
941 musb->is_active = 0;
942 devctl = musb_readb(regs, MUSB_DEVCTL);
943 devctl &= ~MUSB_DEVCTL_SESSION;
944
945 if (is_otg_enabled(musb)) {
946 /* session started after:
947 * (a) ID-grounded irq, host mode;
948 * (b) vbus present/connect IRQ, peripheral mode;
949 * (c) peripheral initiates, using SRP
950 */
951 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
952 musb->is_active = 1;
953 else
954 devctl |= MUSB_DEVCTL_SESSION;
955
956 } else if (is_host_enabled(musb)) {
957 /* assume ID pin is hard-wired to ground */
958 devctl |= MUSB_DEVCTL_SESSION;
959
960 } else /* peripheral is enabled */ {
961 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
962 musb->is_active = 1;
963 }
964 musb_platform_enable(musb);
965 musb_writeb(regs, MUSB_DEVCTL, devctl);
966}
967
968
969static void musb_generic_disable(struct musb *musb)
970{
971 void __iomem *mbase = musb->mregs;
972 u16 temp;
973
974 /* disable interrupts */
975 musb_writeb(mbase, MUSB_INTRUSBE, 0);
976 musb_writew(mbase, MUSB_INTRTXE, 0);
977 musb_writew(mbase, MUSB_INTRRXE, 0);
978
979 /* off */
980 musb_writeb(mbase, MUSB_DEVCTL, 0);
981
982 /* flush pending interrupts */
983 temp = musb_readb(mbase, MUSB_INTRUSB);
984 temp = musb_readw(mbase, MUSB_INTRTX);
985 temp = musb_readw(mbase, MUSB_INTRRX);
986
987}
988
989/*
990 * Make the HDRC stop (disable interrupts, etc.);
991 * reversible by musb_start
992 * called on gadget driver unregister
993 * with controller locked, irqs blocked
994 * acts as a NOP unless some role activated the hardware
995 */
996void musb_stop(struct musb *musb)
997{
998 /* stop IRQs, timers, ... */
999 musb_platform_disable(musb);
1000 musb_generic_disable(musb);
1001 DBG(3, "HDRC disabled\n");
1002
1003 /* FIXME
1004 * - mark host and/or peripheral drivers unusable/inactive
1005 * - disable DMA (and enable it in HdrcStart)
1006 * - make sure we can musb_start() after musb_stop(); with
1007 * OTG mode, gadget driver module rmmod/modprobe cycles that
1008 * - ...
1009 */
1010 musb_platform_try_idle(musb, 0);
1011}
1012
1013static void musb_shutdown(struct platform_device *pdev)
1014{
1015 struct musb *musb = dev_to_musb(&pdev->dev);
1016 unsigned long flags;
1017
Hema HK4f9edd22011-03-22 16:02:12 +05301018 pm_runtime_get_sync(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001019 spin_lock_irqsave(&musb->lock, flags);
1020 musb_platform_disable(musb);
1021 musb_generic_disable(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001022 spin_unlock_irqrestore(&musb->lock, flags);
1023
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001024 if (!is_otg_enabled(musb) && is_host_enabled(musb))
1025 usb_remove_hcd(musb_to_hcd(musb));
1026 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1027 musb_platform_exit(musb);
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001028
Hema HK4f9edd22011-03-22 16:02:12 +05301029 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001030 /* FIXME power down */
1031}
1032
1033
1034/*-------------------------------------------------------------------------*/
1035
1036/*
1037 * The silicon either has hard-wired endpoint configurations, or else
1038 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001039 * writing only the dynamic sizing is very well tested. Since we switched
1040 * away from compile-time hardware parameters, we can no longer rely on
1041 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001042 *
1043 * We don't currently use dynamic fifo setup capability to do anything
1044 * more than selecting one of a bunch of predefined configurations.
1045 */
Felipe Balbi7c925542010-12-01 14:23:48 +02001046#if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
1047 || defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +03001048static ushort __initdata fifo_mode = 4;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001049#elif defined(CONFIG_USB_MUSB_UX500)
1050static ushort __initdata fifo_mode = 5;
Felipe Balbi550a7372008-07-24 12:27:36 +03001051#else
1052static ushort __initdata fifo_mode = 2;
1053#endif
1054
1055/* "modprobe ... fifo_mode=1" etc */
1056module_param(fifo_mode, ushort, 0);
1057MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1058
Felipe Balbi550a7372008-07-24 12:27:36 +03001059/*
1060 * tables defining fifo_mode values. define more if you like.
1061 * for host side, make sure both halves of ep1 are set up.
1062 */
1063
1064/* mode 0 - fits in 2KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001065static struct musb_fifo_cfg __initdata mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001066{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1067{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1068{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1069{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1070{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1071};
1072
1073/* mode 1 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001074static struct musb_fifo_cfg __initdata mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001075{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1076{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1077{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1078{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1079{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1080};
1081
1082/* mode 2 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001083static struct musb_fifo_cfg __initdata mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001084{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1085{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1086{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1087{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1088{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1089{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1090};
1091
1092/* mode 3 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001093static struct musb_fifo_cfg __initdata mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001094{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1095{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1096{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1097{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1098{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1099{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1100};
1101
1102/* mode 4 - fits in 16KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001103static struct musb_fifo_cfg __initdata mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001104{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1105{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1106{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1107{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1108{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1109{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1110{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1111{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1112{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1113{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1114{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1115{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1116{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1117{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1118{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1119{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1120{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1121{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001122{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1123{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1124{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1125{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1126{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1127{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1128{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001129{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1130{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1131};
1132
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001133/* mode 5 - fits in 8KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001134static struct musb_fifo_cfg __initdata mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001135{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1136{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1137{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1138{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1139{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1140{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1141{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1142{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1143{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1144{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1145{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1146{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1147{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1148{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1149{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1150{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1151{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1152{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1153{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1154{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1155{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1156{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1157{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1158{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1159{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1160{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1161{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1162};
Felipe Balbi550a7372008-07-24 12:27:36 +03001163
1164/*
1165 * configure a fifo; for non-shared endpoints, this may be called
1166 * once for a tx fifo and once for an rx fifo.
1167 *
1168 * returns negative errno or offset for next fifo.
1169 */
1170static int __init
1171fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001172 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001173{
1174 void __iomem *mbase = musb->mregs;
1175 int size = 0;
1176 u16 maxpacket = cfg->maxpacket;
1177 u16 c_off = offset >> 3;
1178 u8 c_size;
1179
1180 /* expect hw_ep has already been zero-initialized */
1181
1182 size = ffs(max(maxpacket, (u16) 8)) - 1;
1183 maxpacket = 1 << size;
1184
1185 c_size = size - 3;
1186 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001187 if ((offset + (maxpacket << 1)) >
1188 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001189 return -EMSGSIZE;
1190 c_size |= MUSB_FIFOSZ_DPB;
1191 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001192 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001193 return -EMSGSIZE;
1194 }
1195
1196 /* configure the FIFO */
1197 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1198
1199#ifdef CONFIG_USB_MUSB_HDRC_HCD
1200 /* EP0 reserved endpoint for control, bidirectional;
1201 * EP1 reserved for bulk, two unidirection halves.
1202 */
1203 if (hw_ep->epnum == 1)
1204 musb->bulk_ep = hw_ep;
1205 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1206#endif
1207 switch (cfg->style) {
1208 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001209 musb_write_txfifosz(mbase, c_size);
1210 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001211 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1212 hw_ep->max_packet_sz_tx = maxpacket;
1213 break;
1214 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001215 musb_write_rxfifosz(mbase, c_size);
1216 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001217 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1218 hw_ep->max_packet_sz_rx = maxpacket;
1219 break;
1220 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001221 musb_write_txfifosz(mbase, c_size);
1222 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001223 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1224 hw_ep->max_packet_sz_rx = maxpacket;
1225
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001226 musb_write_rxfifosz(mbase, c_size);
1227 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001228 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1229 hw_ep->max_packet_sz_tx = maxpacket;
1230
1231 hw_ep->is_shared_fifo = true;
1232 break;
1233 }
1234
1235 /* NOTE rx and tx endpoint irqs aren't managed separately,
1236 * which happens to be ok
1237 */
1238 musb->epmask |= (1 << hw_ep->epnum);
1239
1240 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1241}
1242
Felipe Balbie6c213b2010-03-12 10:29:06 +02001243static struct musb_fifo_cfg __initdata ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001244 .style = FIFO_RXTX, .maxpacket = 64,
1245};
1246
1247static int __init ep_config_from_table(struct musb *musb)
1248{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001249 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001250 unsigned i, n;
1251 int offset;
1252 struct musb_hw_ep *hw_ep = musb->endpoints;
1253
Felipe Balbie6c213b2010-03-12 10:29:06 +02001254 if (musb->config->fifo_cfg) {
1255 cfg = musb->config->fifo_cfg;
1256 n = musb->config->fifo_cfg_size;
1257 goto done;
1258 }
1259
Felipe Balbi550a7372008-07-24 12:27:36 +03001260 switch (fifo_mode) {
1261 default:
1262 fifo_mode = 0;
1263 /* FALLTHROUGH */
1264 case 0:
1265 cfg = mode_0_cfg;
1266 n = ARRAY_SIZE(mode_0_cfg);
1267 break;
1268 case 1:
1269 cfg = mode_1_cfg;
1270 n = ARRAY_SIZE(mode_1_cfg);
1271 break;
1272 case 2:
1273 cfg = mode_2_cfg;
1274 n = ARRAY_SIZE(mode_2_cfg);
1275 break;
1276 case 3:
1277 cfg = mode_3_cfg;
1278 n = ARRAY_SIZE(mode_3_cfg);
1279 break;
1280 case 4:
1281 cfg = mode_4_cfg;
1282 n = ARRAY_SIZE(mode_4_cfg);
1283 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001284 case 5:
1285 cfg = mode_5_cfg;
1286 n = ARRAY_SIZE(mode_5_cfg);
1287 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001288 }
1289
1290 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1291 musb_driver_name, fifo_mode);
1292
1293
Felipe Balbie6c213b2010-03-12 10:29:06 +02001294done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001295 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1296 /* assert(offset > 0) */
1297
1298 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001299 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001300 */
1301
1302 for (i = 0; i < n; i++) {
1303 u8 epn = cfg->hw_ep_num;
1304
Felipe Balbica6d1b12008-08-08 12:40:54 +03001305 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001306 pr_debug("%s: invalid ep %d\n",
1307 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001308 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001309 }
1310 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1311 if (offset < 0) {
1312 pr_debug("%s: mem overrun, ep %d\n",
1313 musb_driver_name, epn);
1314 return -EINVAL;
1315 }
1316 epn++;
1317 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1318 }
1319
1320 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1321 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001322 n + 1, musb->config->num_eps * 2 - 1,
1323 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001324
1325#ifdef CONFIG_USB_MUSB_HDRC_HCD
1326 if (!musb->bulk_ep) {
1327 pr_debug("%s: missing bulk\n", musb_driver_name);
1328 return -EINVAL;
1329 }
1330#endif
1331
1332 return 0;
1333}
1334
1335
1336/*
1337 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1338 * @param musb the controller
1339 */
1340static int __init ep_config_from_hw(struct musb *musb)
1341{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001342 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001343 struct musb_hw_ep *hw_ep;
1344 void *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001345 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001346
1347 DBG(2, "<== static silicon ep config\n");
1348
1349 /* FIXME pick up ep0 maxpacket size */
1350
Felipe Balbica6d1b12008-08-08 12:40:54 +03001351 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001352 musb_ep_select(mbase, epnum);
1353 hw_ep = musb->endpoints + epnum;
1354
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001355 ret = musb_read_fifosize(musb, hw_ep, epnum);
1356 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001357 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001358
1359 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1360
1361#ifdef CONFIG_USB_MUSB_HDRC_HCD
1362 /* pick an RX/TX endpoint for bulk */
1363 if (hw_ep->max_packet_sz_tx < 512
1364 || hw_ep->max_packet_sz_rx < 512)
1365 continue;
1366
1367 /* REVISIT: this algorithm is lazy, we should at least
1368 * try to pick a double buffered endpoint.
1369 */
1370 if (musb->bulk_ep)
1371 continue;
1372 musb->bulk_ep = hw_ep;
1373#endif
1374 }
1375
1376#ifdef CONFIG_USB_MUSB_HDRC_HCD
1377 if (!musb->bulk_ep) {
1378 pr_debug("%s: missing bulk\n", musb_driver_name);
1379 return -EINVAL;
1380 }
1381#endif
1382
1383 return 0;
1384}
1385
1386enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1387
1388/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1389 * configure endpoints, or take their config from silicon
1390 */
1391static int __init musb_core_init(u16 musb_type, struct musb *musb)
1392{
Felipe Balbi550a7372008-07-24 12:27:36 +03001393 u8 reg;
1394 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301395 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001396 void __iomem *mbase = musb->mregs;
1397 int status = 0;
1398 int i;
1399
1400 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001401 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001402
1403 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001404 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001405 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001406 musb->dyn_fifo = true;
1407 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001408 if (reg & MUSB_CONFIGDATA_MPRXE) {
1409 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001410 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001411 }
1412 if (reg & MUSB_CONFIGDATA_MPTXE) {
1413 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001414 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001415 }
1416 if (reg & MUSB_CONFIGDATA_HBRXE) {
1417 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001418 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001419 }
1420 if (reg & MUSB_CONFIGDATA_HBTXE) {
1421 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001422 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001423 }
1424 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1425 strcat(aInfo, ", SoftConn");
1426
1427 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1428 musb_driver_name, reg, aInfo);
1429
Felipe Balbi550a7372008-07-24 12:27:36 +03001430 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001431 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1432 musb->is_multipoint = 1;
1433 type = "M";
1434 } else {
1435 musb->is_multipoint = 0;
1436 type = "";
1437#ifdef CONFIG_USB_MUSB_HDRC_HCD
1438#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1439 printk(KERN_ERR
1440 "%s: kernel must blacklist external hubs\n",
1441 musb_driver_name);
1442#endif
1443#endif
1444 }
1445
1446 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301447 musb->hwvers = musb_read_hwvers(mbase);
1448 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1449 MUSB_HWVERS_MINOR(musb->hwvers),
1450 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001451 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1452 musb_driver_name, type, aRevision, aDate);
1453
1454 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001455 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001456
1457 /* discover endpoint configuration */
1458 musb->nr_endpoints = 1;
1459 musb->epmask = 1;
1460
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001461 if (musb->dyn_fifo)
1462 status = ep_config_from_table(musb);
1463 else
1464 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001465
1466 if (status < 0)
1467 return status;
1468
1469 /* finish init, and print endpoint config */
1470 for (i = 0; i < musb->nr_endpoints; i++) {
1471 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1472
1473 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
Felipe Balbi7c925542010-12-01 14:23:48 +02001474#ifdef CONFIG_USB_MUSB_TUSB6010
Felipe Balbi550a7372008-07-24 12:27:36 +03001475 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1476 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1477 hw_ep->fifo_sync_va =
1478 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1479
1480 if (i == 0)
1481 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1482 else
1483 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1484#endif
1485
1486 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1487#ifdef CONFIG_USB_MUSB_HDRC_HCD
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001488 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001489 hw_ep->rx_reinit = 1;
1490 hw_ep->tx_reinit = 1;
1491#endif
1492
1493 if (hw_ep->max_packet_sz_tx) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301494 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001495 "%s: hw_ep %d%s, %smax %d\n",
1496 musb_driver_name, i,
1497 hw_ep->is_shared_fifo ? "shared" : "tx",
1498 hw_ep->tx_double_buffered
1499 ? "doublebuffer, " : "",
1500 hw_ep->max_packet_sz_tx);
1501 }
1502 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Ajay Kumar Gupta12304352009-11-17 15:22:54 +05301503 DBG(1,
Felipe Balbi550a7372008-07-24 12:27:36 +03001504 "%s: hw_ep %d%s, %smax %d\n",
1505 musb_driver_name, i,
1506 "rx",
1507 hw_ep->rx_double_buffered
1508 ? "doublebuffer, " : "",
1509 hw_ep->max_packet_sz_rx);
1510 }
1511 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
1512 DBG(1, "hw_ep %d not configured\n", i);
1513 }
1514
1515 return 0;
1516}
1517
1518/*-------------------------------------------------------------------------*/
1519
Tony Lindgren59b479e2011-01-27 16:39:40 -08001520#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001521 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \
1522 defined(CONFIG_ARCH_U5500)
Felipe Balbi550a7372008-07-24 12:27:36 +03001523
1524static irqreturn_t generic_interrupt(int irq, void *__hci)
1525{
1526 unsigned long flags;
1527 irqreturn_t retval = IRQ_NONE;
1528 struct musb *musb = __hci;
1529
1530 spin_lock_irqsave(&musb->lock, flags);
1531
1532 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1533 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1534 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1535
1536 if (musb->int_usb || musb->int_tx || musb->int_rx)
1537 retval = musb_interrupt(musb);
1538
1539 spin_unlock_irqrestore(&musb->lock, flags);
1540
Sergei Shtylyova5073b52009-03-27 12:52:43 -07001541 return retval;
Felipe Balbi550a7372008-07-24 12:27:36 +03001542}
1543
1544#else
1545#define generic_interrupt NULL
1546#endif
1547
1548/*
1549 * handle all the irqs defined by the HDRC core. for now we expect: other
1550 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1551 * will be assigned, and the irq will already have been acked.
1552 *
1553 * called in irq context with spinlock held, irqs blocked
1554 */
1555irqreturn_t musb_interrupt(struct musb *musb)
1556{
1557 irqreturn_t retval = IRQ_NONE;
1558 u8 devctl, power;
1559 int ep_num;
1560 u32 reg;
1561
1562 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1563 power = musb_readb(musb->mregs, MUSB_POWER);
1564
1565 DBG(4, "** IRQ %s usb%04x tx%04x rx%04x\n",
1566 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1567 musb->int_usb, musb->int_tx, musb->int_rx);
1568
Felipe Balbicd42fef2009-12-15 13:47:30 +02001569#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1570 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
1571 if (!musb->gadget_driver) {
1572 DBG(5, "No gadget driver loaded\n");
1573 return IRQ_HANDLED;
1574 }
1575#endif
1576
Felipe Balbi550a7372008-07-24 12:27:36 +03001577 /* the core can interrupt us for multiple reasons; docs have
1578 * a generic interrupt flowchart to follow
1579 */
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301580 if (musb->int_usb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001581 retval |= musb_stage0_irq(musb, musb->int_usb,
1582 devctl, power);
1583
1584 /* "stage 1" is handling endpoint irqs */
1585
1586 /* handle endpoint 0 first */
1587 if (musb->int_tx & 1) {
1588 if (devctl & MUSB_DEVCTL_HM)
1589 retval |= musb_h_ep0_irq(musb);
1590 else
1591 retval |= musb_g_ep0_irq(musb);
1592 }
1593
1594 /* RX on endpoints 1-15 */
1595 reg = musb->int_rx >> 1;
1596 ep_num = 1;
1597 while (reg) {
1598 if (reg & 1) {
1599 /* musb_ep_select(musb->mregs, ep_num); */
1600 /* REVISIT just retval = ep->rx_irq(...) */
1601 retval = IRQ_HANDLED;
1602 if (devctl & MUSB_DEVCTL_HM) {
1603 if (is_host_capable())
1604 musb_host_rx(musb, ep_num);
1605 } else {
1606 if (is_peripheral_capable())
1607 musb_g_rx(musb, ep_num);
1608 }
1609 }
1610
1611 reg >>= 1;
1612 ep_num++;
1613 }
1614
1615 /* TX on endpoints 1-15 */
1616 reg = musb->int_tx >> 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->tx_irq(...) */
1622 retval = IRQ_HANDLED;
1623 if (devctl & MUSB_DEVCTL_HM) {
1624 if (is_host_capable())
1625 musb_host_tx(musb, ep_num);
1626 } else {
1627 if (is_peripheral_capable())
1628 musb_g_tx(musb, ep_num);
1629 }
1630 }
1631 reg >>= 1;
1632 ep_num++;
1633 }
1634
Felipe Balbi550a7372008-07-24 12:27:36 +03001635 return retval;
1636}
1637
1638
1639#ifndef CONFIG_MUSB_PIO_ONLY
1640static int __initdata use_dma = 1;
1641
1642/* "modprobe ... use_dma=0" etc */
1643module_param(use_dma, bool, 0);
1644MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1645
1646void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1647{
1648 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1649
1650 /* called with controller lock already held */
1651
1652 if (!epnum) {
1653#ifndef CONFIG_USB_TUSB_OMAP_DMA
1654 if (!is_cppi_enabled()) {
1655 /* endpoint 0 */
1656 if (devctl & MUSB_DEVCTL_HM)
1657 musb_h_ep0_irq(musb);
1658 else
1659 musb_g_ep0_irq(musb);
1660 }
1661#endif
1662 } else {
1663 /* endpoints 1..15 */
1664 if (transmit) {
1665 if (devctl & MUSB_DEVCTL_HM) {
1666 if (is_host_capable())
1667 musb_host_tx(musb, epnum);
1668 } else {
1669 if (is_peripheral_capable())
1670 musb_g_tx(musb, epnum);
1671 }
1672 } else {
1673 /* receive */
1674 if (devctl & MUSB_DEVCTL_HM) {
1675 if (is_host_capable())
1676 musb_host_rx(musb, epnum);
1677 } else {
1678 if (is_peripheral_capable())
1679 musb_g_rx(musb, epnum);
1680 }
1681 }
1682 }
1683}
1684
1685#else
1686#define use_dma 0
1687#endif
1688
1689/*-------------------------------------------------------------------------*/
1690
1691#ifdef CONFIG_SYSFS
1692
1693static ssize_t
1694musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1695{
1696 struct musb *musb = dev_to_musb(dev);
1697 unsigned long flags;
1698 int ret = -EINVAL;
1699
1700 spin_lock_irqsave(&musb->lock, flags);
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001701 ret = sprintf(buf, "%s\n", otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001702 spin_unlock_irqrestore(&musb->lock, flags);
1703
1704 return ret;
1705}
1706
1707static ssize_t
1708musb_mode_store(struct device *dev, struct device_attribute *attr,
1709 const char *buf, size_t n)
1710{
1711 struct musb *musb = dev_to_musb(dev);
1712 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001713 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001714
1715 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001716 if (sysfs_streq(buf, "host"))
1717 status = musb_platform_set_mode(musb, MUSB_HOST);
1718 else if (sysfs_streq(buf, "peripheral"))
1719 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1720 else if (sysfs_streq(buf, "otg"))
1721 status = musb_platform_set_mode(musb, MUSB_OTG);
1722 else
1723 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001724 spin_unlock_irqrestore(&musb->lock, flags);
1725
David Brownell96a274d2008-11-24 13:06:47 +02001726 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001727}
1728static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1729
1730static ssize_t
1731musb_vbus_store(struct device *dev, struct device_attribute *attr,
1732 const char *buf, size_t n)
1733{
1734 struct musb *musb = dev_to_musb(dev);
1735 unsigned long flags;
1736 unsigned long val;
1737
1738 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001739 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001740 return -EINVAL;
1741 }
1742
1743 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001744 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1745 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001746 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001747 musb->is_active = 0;
1748 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1749 spin_unlock_irqrestore(&musb->lock, flags);
1750
1751 return n;
1752}
1753
1754static ssize_t
1755musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1756{
1757 struct musb *musb = dev_to_musb(dev);
1758 unsigned long flags;
1759 unsigned long val;
1760 int vbus;
1761
1762 spin_lock_irqsave(&musb->lock, flags);
1763 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001764 /* FIXME get_vbus_status() is normally #defined as false...
1765 * and is effectively TUSB-specific.
1766 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001767 vbus = musb_platform_get_vbus_status(musb);
1768 spin_unlock_irqrestore(&musb->lock, flags);
1769
David Brownellf7f9d632009-03-31 12:32:12 -07001770 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001771 vbus ? "on" : "off", val);
1772}
1773static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1774
1775#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1776
1777/* Gadget drivers can't know that a host is connected so they might want
1778 * to start SRP, but users can. This allows userspace to trigger SRP.
1779 */
1780static ssize_t
1781musb_srp_store(struct device *dev, struct device_attribute *attr,
1782 const char *buf, size_t n)
1783{
1784 struct musb *musb = dev_to_musb(dev);
1785 unsigned short srp;
1786
1787 if (sscanf(buf, "%hu", &srp) != 1
1788 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001789 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001790 return -EINVAL;
1791 }
1792
1793 if (srp == 1)
1794 musb_g_wakeup(musb);
1795
1796 return n;
1797}
1798static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1799
1800#endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1801
Felipe Balbi94375752009-12-15 11:08:38 +02001802static struct attribute *musb_attributes[] = {
1803 &dev_attr_mode.attr,
1804 &dev_attr_vbus.attr,
1805#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1806 &dev_attr_srp.attr,
1807#endif
1808 NULL
1809};
1810
1811static const struct attribute_group musb_attr_group = {
1812 .attrs = musb_attributes,
1813};
1814
Felipe Balbi550a7372008-07-24 12:27:36 +03001815#endif /* sysfs */
1816
1817/* Only used to provide driver mode change events */
1818static void musb_irq_work(struct work_struct *data)
1819{
1820 struct musb *musb = container_of(data, struct musb, irq_work);
1821 static int old_state;
1822
David Brownell84e250f2009-03-31 12:30:04 -07001823 if (musb->xceiv->state != old_state) {
1824 old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001825 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1826 }
1827}
1828
1829/* --------------------------------------------------------------------------
1830 * Init support
1831 */
1832
1833static struct musb *__init
Felipe Balbica6d1b12008-08-08 12:40:54 +03001834allocate_instance(struct device *dev,
1835 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001836{
1837 struct musb *musb;
1838 struct musb_hw_ep *ep;
1839 int epnum;
1840#ifdef CONFIG_USB_MUSB_HDRC_HCD
1841 struct usb_hcd *hcd;
1842
Kay Sievers427c4f32008-11-07 01:52:53 +01001843 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
Felipe Balbi550a7372008-07-24 12:27:36 +03001844 if (!hcd)
1845 return NULL;
1846 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1847
1848 musb = hcd_to_musb(hcd);
1849 INIT_LIST_HEAD(&musb->control);
1850 INIT_LIST_HEAD(&musb->in_bulk);
1851 INIT_LIST_HEAD(&musb->out_bulk);
1852
1853 hcd->uses_new_polling = 1;
Felipe Balbiec95d352011-02-24 10:36:53 +02001854 hcd->has_tt = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001855
1856 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001857 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001858#else
1859 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1860 if (!musb)
1861 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001862
1863#endif
Ming Lei456bb162010-12-21 21:16:11 +08001864 dev_set_drvdata(dev, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001865 musb->mregs = mbase;
1866 musb->ctrl_base = mbase;
1867 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001868 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001869 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001870 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001871 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001872 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001873 ep->musb = musb;
1874 ep->epnum = epnum;
1875 }
1876
1877 musb->controller = dev;
Felipe Balbi743411b2010-12-01 13:22:05 +02001878
Felipe Balbi550a7372008-07-24 12:27:36 +03001879 return musb;
1880}
1881
1882static void musb_free(struct musb *musb)
1883{
1884 /* this has multiple entry modes. it handles fault cleanup after
1885 * probe(), where things may be partially set up, as well as rmmod
1886 * cleanup after everything's been de-activated.
1887 */
1888
1889#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001890 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001891#endif
1892
1893#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1894 musb_gadget_cleanup(musb);
1895#endif
1896
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001897 if (musb->nIrq >= 0) {
1898 if (musb->irq_wake)
1899 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001900 free_irq(musb->nIrq, musb);
1901 }
1902 if (is_dma_capable() && musb->dma_controller) {
1903 struct dma_controller *c = musb->dma_controller;
1904
1905 (void) c->stop(c);
1906 dma_controller_destroy(c);
1907 }
1908
Felipe Balbi550a7372008-07-24 12:27:36 +03001909#ifdef CONFIG_USB_MUSB_HDRC_HCD
1910 usb_put_hcd(musb_to_hcd(musb));
1911#else
1912 kfree(musb);
1913#endif
1914}
1915
1916/*
1917 * Perform generic per-controller initialization.
1918 *
1919 * @pDevice: the controller (already clocked, etc)
1920 * @nIrq: irq
1921 * @mregs: virtual address of controller registers,
1922 * not yet corrected for platform-specific offsets
1923 */
1924static int __init
1925musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1926{
1927 int status;
1928 struct musb *musb;
1929 struct musb_hdrc_platform_data *plat = dev->platform_data;
1930
1931 /* The driver might handle more features than the board; OK.
1932 * Fail when the board needs a feature that's not enabled.
1933 */
1934 if (!plat) {
1935 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001936 status = -ENODEV;
1937 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001938 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001939
Felipe Balbi550a7372008-07-24 12:27:36 +03001940 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001941 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001942 if (!musb) {
1943 status = -ENOMEM;
1944 goto fail0;
1945 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001946
Hema HK7acc6192011-02-28 14:19:34 +05301947 pm_runtime_use_autosuspend(musb->controller);
1948 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1949 pm_runtime_enable(musb->controller);
1950
Felipe Balbi550a7372008-07-24 12:27:36 +03001951 spin_lock_init(&musb->lock);
1952 musb->board_mode = plat->mode;
1953 musb->board_set_power = plat->set_power;
Felipe Balbi550a7372008-07-24 12:27:36 +03001954 musb->min_power = plat->min_power;
Felipe Balbif7ec9432010-12-02 09:48:58 +02001955 musb->ops = plat->platform_ops;
Felipe Balbi550a7372008-07-24 12:27:36 +03001956
David Brownell84e250f2009-03-31 12:30:04 -07001957 /* The musb_platform_init() call:
1958 * - adjusts musb->mregs and musb->isr if needed,
1959 * - may initialize an integrated tranceiver
1960 * - initializes musb->xceiv, usually by otg_get_transceiver()
David Brownell84e250f2009-03-31 12:30:04 -07001961 * - stops powering VBUS
David Brownell84e250f2009-03-31 12:30:04 -07001962 *
1963 * There are various transciever configurations. Blackfin,
1964 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1965 * external/discrete ones in various flavors (twl4030 family,
1966 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03001967 */
1968 musb->isr = generic_interrupt;
Hema Kalliguddiea65df52010-09-22 19:27:40 -05001969 status = musb_platform_init(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001970 if (status < 0)
Felipe Balbi03491762010-12-02 09:57:08 +02001971 goto fail1;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001972
Felipe Balbi550a7372008-07-24 12:27:36 +03001973 if (!musb->isr) {
1974 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001975 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001976 }
1977
Heikki Krogerusffb865b2010-03-25 13:25:28 +02001978 if (!musb->xceiv->io_ops) {
1979 musb->xceiv->io_priv = musb->mregs;
1980 musb->xceiv->io_ops = &musb_ulpi_access;
1981 }
1982
Felipe Balbi550a7372008-07-24 12:27:36 +03001983#ifndef CONFIG_MUSB_PIO_ONLY
1984 if (use_dma && dev->dma_mask) {
1985 struct dma_controller *c;
1986
1987 c = dma_controller_create(musb, musb->mregs);
1988 musb->dma_controller = c;
1989 if (c)
1990 (void) c->start(c);
1991 }
1992#endif
1993 /* ideally this would be abstracted in platform setup */
1994 if (!is_dma_capable() || !musb->dma_controller)
1995 dev->dma_mask = NULL;
1996
1997 /* be sure interrupts are disabled before connecting ISR */
1998 musb_platform_disable(musb);
1999 musb_generic_disable(musb);
2000
2001 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03002002 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03002003 ? MUSB_CONTROLLER_MHDRC
2004 : MUSB_CONTROLLER_HDRC, musb);
2005 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002006 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002007
Amit Kucheria3a9f5bd2009-07-27 12:03:19 +03002008#ifdef CONFIG_USB_MUSB_OTG
David Brownellf7f9d632009-03-31 12:32:12 -07002009 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2010#endif
2011
Felipe Balbi550a7372008-07-24 12:27:36 +03002012 /* Init IRQ workqueue before request_irq */
2013 INIT_WORK(&musb->irq_work, musb_irq_work);
2014
2015 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002016 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002017 dev_err(dev, "request_irq %d failed!\n", nIrq);
2018 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002019 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002020 }
2021 musb->nIrq = nIrq;
2022/* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002023 if (enable_irq_wake(nIrq) == 0) {
2024 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002025 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002026 } else {
2027 musb->irq_wake = 0;
2028 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002029
David Brownell84e250f2009-03-31 12:30:04 -07002030 /* host side needs more setup */
2031 if (is_host_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002032 struct usb_hcd *hcd = musb_to_hcd(musb);
2033
David Brownell84e250f2009-03-31 12:30:04 -07002034 otg_set_host(musb->xceiv, &hcd->self);
2035
2036 if (is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002037 hcd->self.otg_port = 1;
David Brownell84e250f2009-03-31 12:30:04 -07002038 musb->xceiv->host = &hcd->self;
Felipe Balbi550a7372008-07-24 12:27:36 +03002039 hcd->power_budget = 2 * (plat->power ? : 250);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002040
2041 /* program PHY to use external vBus if required */
2042 if (plat->extvbus) {
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002043 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002044 busctl |= MUSB_ULPI_USE_EXTVBUS;
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002045 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002046 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002047 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002048
2049 /* For the host-only role, we can activate right away.
2050 * (We expect the ID pin to be forcibly grounded!!)
2051 * Otherwise, wait till the gadget driver hooks up.
2052 */
2053 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302054 struct usb_hcd *hcd = musb_to_hcd(musb);
2055
Felipe Balbi550a7372008-07-24 12:27:36 +03002056 MUSB_HST_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002057 musb->xceiv->default_a = 1;
2058 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002059
2060 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2061
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302062 hcd->self.uses_pio_for_control = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002063 DBG(1, "%s mode, status %d, devctl %02x %c\n",
2064 "HOST", status,
2065 musb_readb(musb->mregs, MUSB_DEVCTL),
2066 (musb_readb(musb->mregs, MUSB_DEVCTL)
2067 & MUSB_DEVCTL_BDEVICE
2068 ? 'B' : 'A'));
2069
2070 } else /* peripheral is enabled */ {
2071 MUSB_DEV_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002072 musb->xceiv->default_a = 0;
2073 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002074
2075 status = musb_gadget_setup(musb);
2076
2077 DBG(1, "%s mode, status %d, dev%02x\n",
2078 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2079 status,
2080 musb_readb(musb->mregs, MUSB_DEVCTL));
2081
2082 }
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002083 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002084 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002085
Hema HK7acc6192011-02-28 14:19:34 +05302086 pm_runtime_put(musb->controller);
2087
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002088 status = musb_init_debugfs(musb);
2089 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002090 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002091
Felipe Balbi550a7372008-07-24 12:27:36 +03002092#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02002093 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002094 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002095 goto fail5;
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002096#endif
Felipe Balbi28c2c512008-09-11 11:53:25 +03002097
Felipe Balbiab3bbfa2010-01-21 15:33:58 +02002098 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
2099 ({char *s;
2100 switch (musb->board_mode) {
2101 case MUSB_HOST: s = "Host"; break;
2102 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2103 default: s = "OTG"; break;
2104 }; s; }),
2105 ctrl,
2106 (is_dma_capable() && musb->dma_controller)
2107 ? "DMA" : "PIO",
2108 musb->nIrq);
2109
Felipe Balbi28c2c512008-09-11 11:53:25 +03002110 return 0;
2111
Felipe Balbib0f9da72010-03-25 13:25:18 +02002112fail5:
2113 musb_exit_debugfs(musb);
2114
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002115fail4:
2116 if (!is_otg_enabled(musb) && is_host_enabled(musb))
2117 usb_remove_hcd(musb_to_hcd(musb));
2118 else
2119 musb_gadget_cleanup(musb);
2120
2121fail3:
2122 if (musb->irq_wake)
2123 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002124 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002125
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002126fail1:
Felipe Balbi28c2c512008-09-11 11:53:25 +03002127 dev_err(musb->controller,
2128 "musb_init_controller failed with status %d\n", status);
2129
Felipe Balbi28c2c512008-09-11 11:53:25 +03002130 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002131
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002132fail0:
2133
Felipe Balbi550a7372008-07-24 12:27:36 +03002134 return status;
2135
Felipe Balbi550a7372008-07-24 12:27:36 +03002136}
2137
2138/*-------------------------------------------------------------------------*/
2139
2140/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2141 * bridge to a platform device; this driver then suffices.
2142 */
2143
2144#ifndef CONFIG_MUSB_PIO_ONLY
2145static u64 *orig_dma_mask;
2146#endif
2147
2148static int __init musb_probe(struct platform_device *pdev)
2149{
2150 struct device *dev = &pdev->dev;
Hema Kalliguddifcf173e2010-09-29 11:26:39 -05002151 int irq = platform_get_irq_byname(pdev, "mc");
Felipe Balbida5108e2010-01-21 15:33:57 +02002152 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002153 struct resource *iomem;
2154 void __iomem *base;
2155
2156 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sergei Shtylyov541079d2010-12-10 21:03:29 +03002157 if (!iomem || irq <= 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03002158 return -ENODEV;
2159
Felipe Balbi195e9e42009-12-15 11:08:42 +02002160 base = ioremap(iomem->start, resource_size(iomem));
Felipe Balbi550a7372008-07-24 12:27:36 +03002161 if (!base) {
2162 dev_err(dev, "ioremap failed\n");
2163 return -ENOMEM;
2164 }
2165
2166#ifndef CONFIG_MUSB_PIO_ONLY
2167 /* clobbered by use_dma=n */
2168 orig_dma_mask = dev->dma_mask;
2169#endif
Felipe Balbida5108e2010-01-21 15:33:57 +02002170 status = musb_init_controller(dev, irq, base);
2171 if (status < 0)
2172 iounmap(base);
2173
2174 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002175}
2176
Felipe Balbie3060b12009-12-15 11:08:41 +02002177static int __exit musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002178{
2179 struct musb *musb = dev_to_musb(&pdev->dev);
2180 void __iomem *ctrl_base = musb->ctrl_base;
2181
2182 /* this gets called on rmmod.
2183 * - Host mode: host may still be active
2184 * - Peripheral mode: peripheral is deactivated (or never-activated)
2185 * - OTG mode: both roles are deactivated (or never-activated)
2186 */
Hema HK7acc6192011-02-28 14:19:34 +05302187 pm_runtime_get_sync(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002188 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002189 musb_shutdown(pdev);
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002190
Hema HK7acc6192011-02-28 14:19:34 +05302191 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03002192 musb_free(musb);
2193 iounmap(ctrl_base);
2194 device_init_wakeup(&pdev->dev, 0);
2195#ifndef CONFIG_MUSB_PIO_ONLY
2196 pdev->dev.dma_mask = orig_dma_mask;
2197#endif
2198 return 0;
2199}
2200
2201#ifdef CONFIG_PM
2202
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002203static void musb_save_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002204{
2205 int i;
2206 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002207 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002208
2209 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002210 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2211 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2212 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002213 }
Felipe Balbi74211072010-12-01 13:53:27 +02002214 musb->context.power = musb_readb(musb_base, MUSB_POWER);
2215 musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
2216 musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
2217 musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2218 musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2219 musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002220
Bob Liuae9b2ad2010-09-24 13:44:07 +03002221 for (i = 0; i < musb->config->num_eps; ++i) {
2222 epio = musb->endpoints[i].regs;
Felipe Balbi74211072010-12-01 13:53:27 +02002223 musb->context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002224 musb_readw(epio, MUSB_TXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002225 musb->context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002226 musb_readw(epio, MUSB_TXCSR);
Felipe Balbi74211072010-12-01 13:53:27 +02002227 musb->context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002228 musb_readw(epio, MUSB_RXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002229 musb->context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002230 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002231
2232 if (musb->dyn_fifo) {
Felipe Balbi74211072010-12-01 13:53:27 +02002233 musb->context.index_regs[i].txfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002234 musb_read_txfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002235 musb->context.index_regs[i].rxfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002236 musb_read_rxfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002237 musb->context.index_regs[i].txfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002238 musb_read_txfifosz(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002239 musb->context.index_regs[i].rxfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002240 musb_read_rxfifosz(musb_base);
2241 }
2242 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002243 musb->context.index_regs[i].txtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002244 musb_readb(epio, MUSB_TXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002245 musb->context.index_regs[i].txinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002246 musb_readb(epio, MUSB_TXINTERVAL);
Felipe Balbi74211072010-12-01 13:53:27 +02002247 musb->context.index_regs[i].rxtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002248 musb_readb(epio, MUSB_RXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002249 musb->context.index_regs[i].rxinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002250 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002251
Felipe Balbi74211072010-12-01 13:53:27 +02002252 musb->context.index_regs[i].txfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002253 musb_read_txfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002254 musb->context.index_regs[i].txhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002255 musb_read_txhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002256 musb->context.index_regs[i].txhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002257 musb_read_txhubport(musb_base, i);
2258
Felipe Balbi74211072010-12-01 13:53:27 +02002259 musb->context.index_regs[i].rxfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002260 musb_read_rxfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002261 musb->context.index_regs[i].rxhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002262 musb_read_rxhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002263 musb->context.index_regs[i].rxhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002264 musb_read_rxhubport(musb_base, i);
2265 }
2266 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002267}
2268
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002269static void musb_restore_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002270{
2271 int i;
2272 void __iomem *musb_base = musb->mregs;
2273 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002274 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002275
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002276 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002277 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2278 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2279 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002280 }
Felipe Balbi74211072010-12-01 13:53:27 +02002281 musb_writeb(musb_base, MUSB_POWER, musb->context.power);
2282 musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
2283 musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
2284 musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2285 musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002286
Bob Liuae9b2ad2010-09-24 13:44:07 +03002287 for (i = 0; i < musb->config->num_eps; ++i) {
2288 epio = musb->endpoints[i].regs;
2289 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002290 musb->context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002291 musb_writew(epio, MUSB_TXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002292 musb->context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002293 musb_writew(epio, MUSB_RXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002294 musb->context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002295 musb_writew(epio, MUSB_RXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002296 musb->context.index_regs[i].rxcsr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002297
2298 if (musb->dyn_fifo) {
2299 musb_write_txfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002300 musb->context.index_regs[i].txfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002301 musb_write_rxfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002302 musb->context.index_regs[i].rxfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002303 musb_write_txfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002304 musb->context.index_regs[i].txfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002305 musb_write_rxfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002306 musb->context.index_regs[i].rxfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002307 }
2308
2309 if (is_host_enabled(musb)) {
Bob Liuae9b2ad2010-09-24 13:44:07 +03002310 musb_writeb(epio, MUSB_TXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002311 musb->context.index_regs[i].txtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002312 musb_writeb(epio, MUSB_TXINTERVAL,
Felipe Balbi74211072010-12-01 13:53:27 +02002313 musb->context.index_regs[i].txinterval);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002314 musb_writeb(epio, MUSB_RXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002315 musb->context.index_regs[i].rxtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002316 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002317
Felipe Balbi74211072010-12-01 13:53:27 +02002318 musb->context.index_regs[i].rxinterval);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002319 musb_write_txfunaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002320 musb->context.index_regs[i].txfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002321 musb_write_txhubaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002322 musb->context.index_regs[i].txhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002323 musb_write_txhubport(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002324 musb->context.index_regs[i].txhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002325
2326 ep_target_regs =
2327 musb_read_target_reg_base(i, musb_base);
2328
2329 musb_write_rxfunaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002330 musb->context.index_regs[i].rxfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002331 musb_write_rxhubaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002332 musb->context.index_regs[i].rxhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002333 musb_write_rxhubport(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002334 musb->context.index_regs[i].rxhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002335 }
2336 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002337}
2338
Magnus Damm48fea962009-07-08 13:22:56 +02002339static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002340{
Magnus Damm48fea962009-07-08 13:22:56 +02002341 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002342 unsigned long flags;
2343 struct musb *musb = dev_to_musb(&pdev->dev);
2344
Felipe Balbi550a7372008-07-24 12:27:36 +03002345 spin_lock_irqsave(&musb->lock, flags);
2346
2347 if (is_peripheral_active(musb)) {
2348 /* FIXME force disconnect unless we know USB will wake
2349 * the system up quickly enough to respond ...
2350 */
2351 } else if (is_host_active(musb)) {
2352 /* we know all the children are suspended; sometimes
2353 * they will even be wakeup-enabled.
2354 */
2355 }
2356
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002357 musb_save_context(musb);
2358
Felipe Balbi550a7372008-07-24 12:27:36 +03002359 spin_unlock_irqrestore(&musb->lock, flags);
2360 return 0;
2361}
2362
Magnus Damm48fea962009-07-08 13:22:56 +02002363static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002364{
Magnus Damm48fea962009-07-08 13:22:56 +02002365 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002366 struct musb *musb = dev_to_musb(&pdev->dev);
2367
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002368 musb_restore_context(musb);
2369
Felipe Balbi550a7372008-07-24 12:27:36 +03002370 /* for static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002371 * unless for some reason the whole soc powered down or the USB
2372 * module got reset through the PSC (vs just being disabled).
Felipe Balbi550a7372008-07-24 12:27:36 +03002373 */
Felipe Balbi550a7372008-07-24 12:27:36 +03002374 return 0;
2375}
2376
Hema HK7acc6192011-02-28 14:19:34 +05302377static int musb_runtime_suspend(struct device *dev)
2378{
2379 struct musb *musb = dev_to_musb(dev);
2380
2381 musb_save_context(musb);
2382
2383 return 0;
2384}
2385
2386static int musb_runtime_resume(struct device *dev)
2387{
2388 struct musb *musb = dev_to_musb(dev);
2389 static int first = 1;
2390
2391 /*
2392 * When pm_runtime_get_sync called for the first time in driver
2393 * init, some of the structure is still not initialized which is
2394 * used in restore function. But clock needs to be
2395 * enabled before any register access, so
2396 * pm_runtime_get_sync has to be called.
2397 * Also context restore without save does not make
2398 * any sense
2399 */
2400 if (!first)
2401 musb_restore_context(musb);
2402 first = 0;
2403
2404 return 0;
2405}
2406
Alexey Dobriyan47145212009-12-14 18:00:08 -08002407static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002408 .suspend = musb_suspend,
2409 .resume_noirq = musb_resume_noirq,
Hema HK7acc6192011-02-28 14:19:34 +05302410 .runtime_suspend = musb_runtime_suspend,
2411 .runtime_resume = musb_runtime_resume,
Magnus Damm48fea962009-07-08 13:22:56 +02002412};
2413
2414#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002415#else
Magnus Damm48fea962009-07-08 13:22:56 +02002416#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002417#endif
2418
2419static struct platform_driver musb_driver = {
2420 .driver = {
2421 .name = (char *)musb_driver_name,
2422 .bus = &platform_bus_type,
2423 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002424 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002425 },
Felipe Balbie3060b12009-12-15 11:08:41 +02002426 .remove = __exit_p(musb_remove),
Felipe Balbi550a7372008-07-24 12:27:36 +03002427 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002428};
2429
2430/*-------------------------------------------------------------------------*/
2431
2432static int __init musb_init(void)
2433{
2434#ifdef CONFIG_USB_MUSB_HDRC_HCD
2435 if (usb_disabled())
2436 return 0;
2437#endif
2438
2439 pr_info("%s: version " MUSB_VERSION ", "
2440#ifdef CONFIG_MUSB_PIO_ONLY
2441 "pio"
2442#elif defined(CONFIG_USB_TI_CPPI_DMA)
2443 "cppi-dma"
2444#elif defined(CONFIG_USB_INVENTRA_DMA)
2445 "musb-dma"
2446#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2447 "tusb-omap-dma"
2448#else
2449 "?dma?"
2450#endif
2451 ", "
2452#ifdef CONFIG_USB_MUSB_OTG
2453 "otg (peripheral+host)"
2454#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2455 "peripheral"
2456#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2457 "host"
2458#endif
2459 ", debug=%d\n",
Felipe Balbib60c72a2008-10-29 15:10:39 +02002460 musb_driver_name, musb_debug);
Felipe Balbi550a7372008-07-24 12:27:36 +03002461 return platform_driver_probe(&musb_driver, musb_probe);
2462}
2463
David Brownell34f32c92009-02-20 13:45:17 -08002464/* make us init after usbcore and i2c (transceivers, regulators, etc)
2465 * and before usb gadget and host-side drivers start to register
Felipe Balbi550a7372008-07-24 12:27:36 +03002466 */
David Brownell34f32c92009-02-20 13:45:17 -08002467fs_initcall(musb_init);
Felipe Balbi550a7372008-07-24 12:27:36 +03002468
2469static void __exit musb_cleanup(void)
2470{
2471 platform_driver_unregister(&musb_driver);
2472}
2473module_exit(musb_cleanup);