blob: c71b0372786e00482bf2f9fadf4bf6293721d27f [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>
Mike Frysinger93039612011-05-25 08:13:24 -040099#include <linux/prefetch.h>
Felipe Balbi550a7372008-07-24 12:27:36 +0300100#include <linux/platform_device.h>
101#include <linux/io.h>
102
Felipe Balbi550a7372008-07-24 12:27:36 +0300103#include "musb_core.h"
104
David Brownellf7f9d632009-03-31 12:32:12 -0700105#define TA_WAIT_BCON(m) max_t(int, (m)->a_wait_bcon, OTG_TIME_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +0300106
107
Felipe Balbi550a7372008-07-24 12:27:36 +0300108#define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
109#define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
110
Felipe Balbie8164f62008-08-10 21:22:35 +0300111#define MUSB_VERSION "6.0"
Felipe Balbi550a7372008-07-24 12:27:36 +0300112
113#define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
114
Felipe Balbi05ac10d2010-12-02 08:49:26 +0200115#define MUSB_DRIVER_NAME "musb-hdrc"
Felipe Balbi550a7372008-07-24 12:27:36 +0300116const char musb_driver_name[] = MUSB_DRIVER_NAME;
117
118MODULE_DESCRIPTION(DRIVER_INFO);
119MODULE_AUTHOR(DRIVER_AUTHOR);
120MODULE_LICENSE("GPL");
121MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
122
123
124/*-------------------------------------------------------------------------*/
125
126static inline struct musb *dev_to_musb(struct device *dev)
127{
Felipe Balbi550a7372008-07-24 12:27:36 +0300128 return dev_get_drvdata(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +0300129}
130
131/*-------------------------------------------------------------------------*/
132
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200133#ifndef CONFIG_BLACKFIN
134static int musb_ulpi_read(struct otg_transceiver *otg, u32 offset)
135{
136 void __iomem *addr = otg->io_priv;
137 int i = 0;
138 u8 r;
139 u8 power;
140
141 /* Make sure the transceiver is not in low power mode */
142 power = musb_readb(addr, MUSB_POWER);
143 power &= ~MUSB_POWER_SUSPENDM;
144 musb_writeb(addr, MUSB_POWER, power);
145
146 /* REVISIT: musbhdrc_ulpi_an.pdf recommends setting the
147 * ULPICarKitControlDisableUTMI after clearing POWER_SUSPENDM.
148 */
149
150 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
151 musb_writeb(addr, MUSB_ULPI_REG_CONTROL,
152 MUSB_ULPI_REG_REQ | MUSB_ULPI_RDN_WR);
153
154 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
155 & MUSB_ULPI_REG_CMPLT)) {
156 i++;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300157 if (i == 10000)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200158 return -ETIMEDOUT;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200159
160 }
161 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
162 r &= ~MUSB_ULPI_REG_CMPLT;
163 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
164
165 return musb_readb(addr, MUSB_ULPI_REG_DATA);
166}
167
168static int musb_ulpi_write(struct otg_transceiver *otg,
169 u32 offset, u32 data)
170{
171 void __iomem *addr = otg->io_priv;
172 int i = 0;
173 u8 r = 0;
174 u8 power;
175
176 /* Make sure the transceiver is not in low power mode */
177 power = musb_readb(addr, MUSB_POWER);
178 power &= ~MUSB_POWER_SUSPENDM;
179 musb_writeb(addr, MUSB_POWER, power);
180
181 musb_writeb(addr, MUSB_ULPI_REG_ADDR, (u8)offset);
182 musb_writeb(addr, MUSB_ULPI_REG_DATA, (u8)data);
183 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, MUSB_ULPI_REG_REQ);
184
185 while (!(musb_readb(addr, MUSB_ULPI_REG_CONTROL)
186 & MUSB_ULPI_REG_CMPLT)) {
187 i++;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300188 if (i == 10000)
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200189 return -ETIMEDOUT;
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200190 }
191
192 r = musb_readb(addr, MUSB_ULPI_REG_CONTROL);
193 r &= ~MUSB_ULPI_REG_CMPLT;
194 musb_writeb(addr, MUSB_ULPI_REG_CONTROL, r);
195
196 return 0;
197}
198#else
Mike Frysingerf2263db2010-06-24 23:07:08 +0530199#define musb_ulpi_read NULL
200#define musb_ulpi_write NULL
Heikki Krogerusffb865b2010-03-25 13:25:28 +0200201#endif
202
203static struct otg_io_access_ops musb_ulpi_access = {
204 .read = musb_ulpi_read,
205 .write = musb_ulpi_write,
206};
207
208/*-------------------------------------------------------------------------*/
209
Felipe Balbi7c925542010-12-01 14:23:48 +0200210#if !defined(CONFIG_USB_MUSB_TUSB6010) && !defined(CONFIG_USB_MUSB_BLACKFIN)
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200211
Felipe Balbi550a7372008-07-24 12:27:36 +0300212/*
213 * Load an endpoint's FIFO
214 */
215void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
216{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300217 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300218 void __iomem *fifo = hw_ep->fifo;
219
220 prefetch((u8 *)src);
221
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300222 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300223 'T', hw_ep->epnum, fifo, len, src);
224
225 /* we can't assume unaligned reads work */
226 if (likely((0x01 & (unsigned long) src) == 0)) {
227 u16 index = 0;
228
229 /* best case is 32bit-aligned source address */
230 if ((0x02 & (unsigned long) src) == 0) {
231 if (len >= 4) {
232 writesl(fifo, src + index, len >> 2);
233 index += len & ~0x03;
234 }
235 if (len & 0x02) {
236 musb_writew(fifo, 0, *(u16 *)&src[index]);
237 index += 2;
238 }
239 } else {
240 if (len >= 2) {
241 writesw(fifo, src + index, len >> 1);
242 index += len & ~0x01;
243 }
244 }
245 if (len & 0x01)
246 musb_writeb(fifo, 0, src[index]);
247 } else {
248 /* byte aligned */
249 writesb(fifo, src, len);
250 }
251}
252
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300253#if !defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +0300254/*
255 * Unload an endpoint's FIFO
256 */
257void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
258{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300259 struct musb *musb = hw_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300260 void __iomem *fifo = hw_ep->fifo;
261
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300262 dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300263 'R', hw_ep->epnum, fifo, len, dst);
264
265 /* we can't assume unaligned writes work */
266 if (likely((0x01 & (unsigned long) dst) == 0)) {
267 u16 index = 0;
268
269 /* best case is 32bit-aligned destination address */
270 if ((0x02 & (unsigned long) dst) == 0) {
271 if (len >= 4) {
272 readsl(fifo, dst, len >> 2);
273 index = len & ~0x03;
274 }
275 if (len & 0x02) {
276 *(u16 *)&dst[index] = musb_readw(fifo, 0);
277 index += 2;
278 }
279 } else {
280 if (len >= 2) {
281 readsw(fifo, dst, len >> 1);
282 index = len & ~0x01;
283 }
284 }
285 if (len & 0x01)
286 dst[index] = musb_readb(fifo, 0);
287 } else {
288 /* byte aligned */
289 readsb(fifo, dst, len);
290 }
291}
Ajay Kumar Gupta843bb1d2010-10-19 10:08:13 +0300292#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300293
294#endif /* normal PIO */
295
296
297/*-------------------------------------------------------------------------*/
298
299/* for high speed test mode; see USB 2.0 spec 7.1.20 */
300static const u8 musb_test_packet[53] = {
301 /* implicit SYNC then DATA0 to start */
302
303 /* JKJKJKJK x9 */
304 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
305 /* JJKKJJKK x8 */
306 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
307 /* JJJJKKKK x8 */
308 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
309 /* JJJJJJJKKKKKKK x8 */
310 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
311 /* JJJJJJJK x8 */
312 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
313 /* JKKKKKKK x10, JK */
314 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
315
316 /* implicit CRC16 then EOP to end */
317};
318
319void musb_load_testpacket(struct musb *musb)
320{
321 void __iomem *regs = musb->endpoints[0].regs;
322
323 musb_ep_select(musb->mregs, 0);
324 musb_write_fifo(musb->control_ep,
325 sizeof(musb_test_packet), musb_test_packet);
326 musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
327}
328
329/*-------------------------------------------------------------------------*/
330
Felipe Balbi550a7372008-07-24 12:27:36 +0300331#ifdef CONFIG_USB_MUSB_OTG
332
333/*
Felipe Balbi550a7372008-07-24 12:27:36 +0300334 * Handles OTG hnp timeouts, such as b_ase0_brst
335 */
336void musb_otg_timer_func(unsigned long data)
337{
338 struct musb *musb = (struct musb *)data;
339 unsigned long flags;
340
341 spin_lock_irqsave(&musb->lock, flags);
David Brownell84e250f2009-03-31 12:30:04 -0700342 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300343 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300344 dev_dbg(musb->controller, "HNP: b_wait_acon timeout; back to b_peripheral\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300345 musb_g_disconnect(musb);
David Brownell84e250f2009-03-31 12:30:04 -0700346 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300347 musb->is_active = 0;
348 break;
David Brownellab983f2a2009-03-31 12:35:09 -0700349 case OTG_STATE_A_SUSPEND:
Felipe Balbi550a7372008-07-24 12:27:36 +0300350 case OTG_STATE_A_WAIT_BCON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300351 dev_dbg(musb->controller, "HNP: %s timeout\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200352 otg_state_string(musb->xceiv->state));
Felipe Balbi743411b2010-12-01 13:22:05 +0200353 musb_platform_set_vbus(musb, 0);
David Brownellab983f2a2009-03-31 12:35:09 -0700354 musb->xceiv->state = OTG_STATE_A_WAIT_VFALL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300355 break;
356 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300357 dev_dbg(musb->controller, "HNP: Unhandled mode %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200358 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300359 }
360 musb->ignore_disconnect = 0;
361 spin_unlock_irqrestore(&musb->lock, flags);
362}
363
Felipe Balbi550a7372008-07-24 12:27:36 +0300364/*
David Brownellf7f9d632009-03-31 12:32:12 -0700365 * Stops the HNP transition. Caller must take care of locking.
Felipe Balbi550a7372008-07-24 12:27:36 +0300366 */
367void musb_hnp_stop(struct musb *musb)
368{
369 struct usb_hcd *hcd = musb_to_hcd(musb);
370 void __iomem *mbase = musb->mregs;
371 u8 reg;
372
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300373 dev_dbg(musb->controller, "HNP: stop from %s\n", otg_state_string(musb->xceiv->state));
David Brownellab983f2a2009-03-31 12:35:09 -0700374
David Brownell84e250f2009-03-31 12:30:04 -0700375 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300376 case OTG_STATE_A_PERIPHERAL:
Felipe Balbi550a7372008-07-24 12:27:36 +0300377 musb_g_disconnect(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300378 dev_dbg(musb->controller, "HNP: back to %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200379 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300380 break;
381 case OTG_STATE_B_HOST:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300382 dev_dbg(musb->controller, "HNP: Disabling HR\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300383 hcd->self.is_b_host = 0;
David Brownell84e250f2009-03-31 12:30:04 -0700384 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300385 MUSB_DEV_MODE(musb);
386 reg = musb_readb(mbase, MUSB_POWER);
387 reg |= MUSB_POWER_SUSPENDM;
388 musb_writeb(mbase, MUSB_POWER, reg);
389 /* REVISIT: Start SESSION_REQUEST here? */
390 break;
391 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300392 dev_dbg(musb->controller, "HNP: Stopping in unknown state %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200393 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300394 }
395
396 /*
397 * When returning to A state after HNP, avoid hub_port_rebounce(),
398 * which cause occasional OPT A "Did not receive reset after connect"
399 * errors.
400 */
Alan Stern749da5f2010-03-04 17:05:08 -0500401 musb->port1_status &= ~(USB_PORT_STAT_C_CONNECTION << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300402}
403
404#endif
405
406/*
407 * Interrupt Service Routine to record USB "global" interrupts.
408 * Since these do not happen often and signify things of
409 * paramount importance, it seems OK to check them individually;
410 * the order of the tests is specified in the manual
411 *
412 * @param musb instance pointer
413 * @param int_usb register contents
414 * @param devctl
415 * @param power
416 */
417
Felipe Balbi550a7372008-07-24 12:27:36 +0300418static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
419 u8 devctl, u8 power)
420{
421 irqreturn_t handled = IRQ_NONE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300422
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300423 dev_dbg(musb->controller, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
Felipe Balbi550a7372008-07-24 12:27:36 +0300424 int_usb);
425
426 /* in host mode, the peripheral may issue remote wakeup.
427 * in peripheral mode, the host may resume the link.
428 * spurious RESUME irqs happen too, paired with SUSPEND.
429 */
430 if (int_usb & MUSB_INTR_RESUME) {
431 handled = IRQ_HANDLED;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300432 dev_dbg(musb->controller, "RESUME (%s)\n", otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300433
434 if (devctl & MUSB_DEVCTL_HM) {
435#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbiaa471452010-03-12 10:27:24 +0200436 void __iomem *mbase = musb->mregs;
437
David Brownell84e250f2009-03-31 12:30:04 -0700438 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300439 case OTG_STATE_A_SUSPEND:
440 /* remote wakeup? later, GetPortStatus
441 * will stop RESUME signaling
442 */
443
444 if (power & MUSB_POWER_SUSPENDM) {
445 /* spurious */
446 musb->int_usb &= ~MUSB_INTR_SUSPEND;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300447 dev_dbg(musb->controller, "Spurious SUSPENDM\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300448 break;
449 }
450
451 power &= ~MUSB_POWER_SUSPENDM;
452 musb_writeb(mbase, MUSB_POWER,
453 power | MUSB_POWER_RESUME);
454
455 musb->port1_status |=
456 (USB_PORT_STAT_C_SUSPEND << 16)
457 | MUSB_PORT_STAT_RESUME;
458 musb->rh_timer = jiffies
459 + msecs_to_jiffies(20);
460
David Brownell84e250f2009-03-31 12:30:04 -0700461 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300462 musb->is_active = 1;
463 usb_hcd_resume_root_hub(musb_to_hcd(musb));
464 break;
465 case OTG_STATE_B_WAIT_ACON:
David Brownell84e250f2009-03-31 12:30:04 -0700466 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +0300467 musb->is_active = 1;
468 MUSB_DEV_MODE(musb);
469 break;
470 default:
471 WARNING("bogus %s RESUME (%s)\n",
472 "host",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200473 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300474 }
475#endif
476 } else {
David Brownell84e250f2009-03-31 12:30:04 -0700477 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300478#ifdef CONFIG_USB_MUSB_HDRC_HCD
479 case OTG_STATE_A_SUSPEND:
480 /* possibly DISCONNECT is upcoming */
David Brownell84e250f2009-03-31 12:30:04 -0700481 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300482 usb_hcd_resume_root_hub(musb_to_hcd(musb));
483 break;
484#endif
485#ifdef CONFIG_USB_GADGET_MUSB_HDRC
486 case OTG_STATE_B_WAIT_ACON:
487 case OTG_STATE_B_PERIPHERAL:
488 /* disconnect while suspended? we may
489 * not get a disconnect irq...
490 */
491 if ((devctl & MUSB_DEVCTL_VBUS)
492 != (3 << MUSB_DEVCTL_VBUS_SHIFT)
493 ) {
494 musb->int_usb |= MUSB_INTR_DISCONNECT;
495 musb->int_usb &= ~MUSB_INTR_SUSPEND;
496 break;
497 }
498 musb_g_resume(musb);
499 break;
500 case OTG_STATE_B_IDLE:
501 musb->int_usb &= ~MUSB_INTR_SUSPEND;
502 break;
503#endif
504 default:
505 WARNING("bogus %s RESUME (%s)\n",
506 "peripheral",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200507 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300508 }
509 }
510 }
511
512#ifdef CONFIG_USB_MUSB_HDRC_HCD
513 /* see manual for the order of the tests */
514 if (int_usb & MUSB_INTR_SESSREQ) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200515 void __iomem *mbase = musb->mregs;
516
Heikki Krogerus19aab562010-10-29 04:23:27 -0500517 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS
518 && (devctl & MUSB_DEVCTL_BDEVICE)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300519 dev_dbg(musb->controller, "SessReq while on B state\n");
Heikki Krogerusa6038ee2010-09-24 13:44:13 +0300520 return IRQ_HANDLED;
521 }
522
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300523 dev_dbg(musb->controller, "SESSION_REQUEST (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200524 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300525
526 /* IRQ arrives from ID pin sense or (later, if VBUS power
527 * is removed) SRP. responses are time critical:
528 * - turn on VBUS (with silicon-specific mechanism)
529 * - go through A_WAIT_VRISE
530 * - ... to A_WAIT_BCON.
531 * a_wait_vrise_tmout triggers VBUS_ERROR transitions
532 */
533 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
534 musb->ep0_stage = MUSB_EP0_START;
David Brownell84e250f2009-03-31 12:30:04 -0700535 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300536 MUSB_HST_MODE(musb);
Felipe Balbi743411b2010-12-01 13:22:05 +0200537 musb_platform_set_vbus(musb, 1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300538
539 handled = IRQ_HANDLED;
540 }
541
542 if (int_usb & MUSB_INTR_VBUSERROR) {
543 int ignore = 0;
544
545 /* During connection as an A-Device, we may see a short
546 * current spikes causing voltage drop, because of cable
547 * and peripheral capacitance combined with vbus draw.
548 * (So: less common with truly self-powered devices, where
549 * vbus doesn't act like a power supply.)
550 *
551 * Such spikes are short; usually less than ~500 usec, max
552 * of ~2 msec. That is, they're not sustained overcurrent
553 * errors, though they're reported using VBUSERROR irqs.
554 *
555 * Workarounds: (a) hardware: use self powered devices.
556 * (b) software: ignore non-repeated VBUS errors.
557 *
558 * REVISIT: do delays from lots of DEBUG_KERNEL checks
559 * make trouble here, keeping VBUS < 4.4V ?
560 */
David Brownell84e250f2009-03-31 12:30:04 -0700561 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300562 case OTG_STATE_A_HOST:
563 /* recovery is dicey once we've gotten past the
564 * initial stages of enumeration, but if VBUS
565 * stayed ok at the other end of the link, and
566 * another reset is due (at least for high speed,
567 * to redo the chirp etc), it might work OK...
568 */
569 case OTG_STATE_A_WAIT_BCON:
570 case OTG_STATE_A_WAIT_VRISE:
571 if (musb->vbuserr_retry) {
Felipe Balbiaa471452010-03-12 10:27:24 +0200572 void __iomem *mbase = musb->mregs;
573
Felipe Balbi550a7372008-07-24 12:27:36 +0300574 musb->vbuserr_retry--;
575 ignore = 1;
576 devctl |= MUSB_DEVCTL_SESSION;
577 musb_writeb(mbase, MUSB_DEVCTL, devctl);
578 } else {
579 musb->port1_status |=
Alan Stern749da5f2010-03-04 17:05:08 -0500580 USB_PORT_STAT_OVERCURRENT
581 | (USB_PORT_STAT_C_OVERCURRENT << 16);
Felipe Balbi550a7372008-07-24 12:27:36 +0300582 }
583 break;
584 default:
585 break;
586 }
587
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300588 dev_dbg(musb->controller, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200589 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300590 devctl,
591 ({ char *s;
592 switch (devctl & MUSB_DEVCTL_VBUS) {
593 case 0 << MUSB_DEVCTL_VBUS_SHIFT:
594 s = "<SessEnd"; break;
595 case 1 << MUSB_DEVCTL_VBUS_SHIFT:
596 s = "<AValid"; break;
597 case 2 << MUSB_DEVCTL_VBUS_SHIFT:
598 s = "<VBusValid"; break;
599 /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
600 default:
601 s = "VALID"; break;
602 }; s; }),
603 VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
604 musb->port1_status);
605
606 /* go through A_WAIT_VFALL then start a new session */
607 if (!ignore)
Felipe Balbi743411b2010-12-01 13:22:05 +0200608 musb_platform_set_vbus(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300609 handled = IRQ_HANDLED;
610 }
611
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530612#endif
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200613 if (int_usb & MUSB_INTR_SUSPEND) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300614 dev_dbg(musb->controller, "SUSPEND (%s) devctl %02x power %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200615 otg_state_string(musb->xceiv->state), devctl, power);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200616 handled = IRQ_HANDLED;
617
618 switch (musb->xceiv->state) {
619#ifdef CONFIG_USB_MUSB_OTG
620 case OTG_STATE_A_PERIPHERAL:
621 /* We also come here if the cable is removed, since
622 * this silicon doesn't report ID-no-longer-grounded.
623 *
624 * We depend on T(a_wait_bcon) to shut us down, and
625 * hope users don't do anything dicey during this
626 * undesired detour through A_WAIT_BCON.
627 */
628 musb_hnp_stop(musb);
629 usb_hcd_resume_root_hub(musb_to_hcd(musb));
630 musb_root_disconnect(musb);
631 musb_platform_try_idle(musb, jiffies
632 + msecs_to_jiffies(musb->a_wait_bcon
633 ? : OTG_TIME_A_WAIT_BCON));
634
635 break;
636#endif
637 case OTG_STATE_B_IDLE:
638 if (!musb->is_active)
639 break;
640 case OTG_STATE_B_PERIPHERAL:
641 musb_g_suspend(musb);
642 musb->is_active = is_otg_enabled(musb)
643 && musb->xceiv->gadget->b_hnp_enable;
644 if (musb->is_active) {
645#ifdef CONFIG_USB_MUSB_OTG
646 musb->xceiv->state = OTG_STATE_B_WAIT_ACON;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300647 dev_dbg(musb->controller, "HNP: Setting timer for b_ase0_brst\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200648 mod_timer(&musb->otg_timer, jiffies
649 + msecs_to_jiffies(
650 OTG_TIME_B_ASE0_BRST));
651#endif
652 }
653 break;
654 case OTG_STATE_A_WAIT_BCON:
655 if (musb->a_wait_bcon != 0)
656 musb_platform_try_idle(musb, jiffies
657 + msecs_to_jiffies(musb->a_wait_bcon));
658 break;
659 case OTG_STATE_A_HOST:
660 musb->xceiv->state = OTG_STATE_A_SUSPEND;
661 musb->is_active = is_otg_enabled(musb)
662 && musb->xceiv->host->b_hnp_enable;
663 break;
664 case OTG_STATE_B_HOST:
665 /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300666 dev_dbg(musb->controller, "REVISIT: SUSPEND as B_HOST\n");
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200667 break;
668 default:
669 /* "should not happen" */
670 musb->is_active = 0;
671 break;
672 }
673 }
674
Maulik Mankad2bb14cb2010-06-15 14:40:27 +0530675#ifdef CONFIG_USB_MUSB_HDRC_HCD
Felipe Balbi550a7372008-07-24 12:27:36 +0300676 if (int_usb & MUSB_INTR_CONNECT) {
677 struct usb_hcd *hcd = musb_to_hcd(musb);
678
679 handled = IRQ_HANDLED;
680 musb->is_active = 1;
681 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
682
683 musb->ep0_stage = MUSB_EP0_START;
684
685#ifdef CONFIG_USB_MUSB_OTG
686 /* flush endpoints when transitioning from Device Mode */
687 if (is_peripheral_active(musb)) {
688 /* REVISIT HNP; just force disconnect */
689 }
Ajay Kumar Guptad709d222010-07-08 14:03:00 +0530690 musb_writew(musb->mregs, MUSB_INTRTXE, musb->epmask);
691 musb_writew(musb->mregs, MUSB_INTRRXE, musb->epmask & 0xfffe);
692 musb_writeb(musb->mregs, MUSB_INTRUSBE, 0xf7);
Felipe Balbi550a7372008-07-24 12:27:36 +0300693#endif
694 musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
695 |USB_PORT_STAT_HIGH_SPEED
696 |USB_PORT_STAT_ENABLE
697 );
698 musb->port1_status |= USB_PORT_STAT_CONNECTION
699 |(USB_PORT_STAT_C_CONNECTION << 16);
700
701 /* high vs full speed is just a guess until after reset */
702 if (devctl & MUSB_DEVCTL_LSDEV)
703 musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
704
Felipe Balbi550a7372008-07-24 12:27:36 +0300705 /* indicate new connection to OTG machine */
David Brownell84e250f2009-03-31 12:30:04 -0700706 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300707 case OTG_STATE_B_PERIPHERAL:
708 if (int_usb & MUSB_INTR_SUSPEND) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300709 dev_dbg(musb->controller, "HNP: SUSPEND+CONNECT, now b_host\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300710 int_usb &= ~MUSB_INTR_SUSPEND;
David Brownell1de00da2009-04-02 10:16:11 -0700711 goto b_host;
Felipe Balbi550a7372008-07-24 12:27:36 +0300712 } else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300713 dev_dbg(musb->controller, "CONNECT as b_peripheral???\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300714 break;
715 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300716 dev_dbg(musb->controller, "HNP: CONNECT, now b_host\n");
David Brownell1de00da2009-04-02 10:16:11 -0700717b_host:
David Brownell84e250f2009-03-31 12:30:04 -0700718 musb->xceiv->state = OTG_STATE_B_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300719 hcd->self.is_b_host = 1;
David Brownell1de00da2009-04-02 10:16:11 -0700720 musb->ignore_disconnect = 0;
721 del_timer(&musb->otg_timer);
Felipe Balbi550a7372008-07-24 12:27:36 +0300722 break;
723 default:
724 if ((devctl & MUSB_DEVCTL_VBUS)
725 == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
David Brownell84e250f2009-03-31 12:30:04 -0700726 musb->xceiv->state = OTG_STATE_A_HOST;
Felipe Balbi550a7372008-07-24 12:27:36 +0300727 hcd->self.is_b_host = 0;
728 }
729 break;
730 }
David Brownell1de00da2009-04-02 10:16:11 -0700731
732 /* poke the root hub */
733 MUSB_HST_MODE(musb);
734 if (hcd->status_urb)
735 usb_hcd_poll_rh_status(hcd);
736 else
737 usb_hcd_resume_root_hub(hcd);
738
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300739 dev_dbg(musb->controller, "CONNECT (%s) devctl %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200740 otg_state_string(musb->xceiv->state), devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300741 }
742#endif /* CONFIG_USB_MUSB_HDRC_HCD */
743
Felipe Balbi550a7372008-07-24 12:27:36 +0300744 if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300745 dev_dbg(musb->controller, "DISCONNECT (%s) as %s, devctl %02x\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200746 otg_state_string(musb->xceiv->state),
Felipe Balbi550a7372008-07-24 12:27:36 +0300747 MUSB_MODE(musb), devctl);
748 handled = IRQ_HANDLED;
749
David Brownell84e250f2009-03-31 12:30:04 -0700750 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300751#ifdef CONFIG_USB_MUSB_HDRC_HCD
752 case OTG_STATE_A_HOST:
753 case OTG_STATE_A_SUSPEND:
Anand Gadiyar5c23c902009-02-21 15:31:40 -0800754 usb_hcd_resume_root_hub(musb_to_hcd(musb));
Felipe Balbi550a7372008-07-24 12:27:36 +0300755 musb_root_disconnect(musb);
Ajay Kumar Gupta74382172009-02-24 15:29:04 -0800756 if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +0300757 musb_platform_try_idle(musb, jiffies
758 + msecs_to_jiffies(musb->a_wait_bcon));
759 break;
760#endif /* HOST */
761#ifdef CONFIG_USB_MUSB_OTG
762 case OTG_STATE_B_HOST:
David Brownellab983f2a2009-03-31 12:35:09 -0700763 /* REVISIT this behaves for "real disconnect"
764 * cases; make sure the other transitions from
765 * from B_HOST act right too. The B_HOST code
766 * in hnp_stop() is currently not used...
767 */
768 musb_root_disconnect(musb);
769 musb_to_hcd(musb)->self.is_b_host = 0;
770 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
771 MUSB_DEV_MODE(musb);
772 musb_g_disconnect(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300773 break;
774 case OTG_STATE_A_PERIPHERAL:
775 musb_hnp_stop(musb);
776 musb_root_disconnect(musb);
777 /* FALLTHROUGH */
778 case OTG_STATE_B_WAIT_ACON:
779 /* FALLTHROUGH */
780#endif /* OTG */
781#ifdef CONFIG_USB_GADGET_MUSB_HDRC
782 case OTG_STATE_B_PERIPHERAL:
783 case OTG_STATE_B_IDLE:
784 musb_g_disconnect(musb);
785 break;
786#endif /* GADGET */
787 default:
788 WARNING("unhandled DISCONNECT transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200789 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +0300790 break;
791 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300792 }
793
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200794 /* mentor saves a bit: bus reset and babble share the same irq.
795 * only host sees babble; only peripheral sees bus reset.
796 */
797 if (int_usb & MUSB_INTR_RESET) {
798 handled = IRQ_HANDLED;
799 if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
800 /*
801 * Looks like non-HS BABBLE can be ignored, but
802 * HS BABBLE is an error condition. For HS the solution
803 * is to avoid babble in the first place and fix what
804 * caused BABBLE. When HS BABBLE happens we can only
805 * stop the session.
806 */
807 if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300808 dev_dbg(musb->controller, "BABBLE devctl: %02x\n", devctl);
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200809 else {
810 ERR("Stopping host session -- babble\n");
811 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
812 }
813 } else if (is_peripheral_capable()) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300814 dev_dbg(musb->controller, "BUS RESET as %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200815 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200816 switch (musb->xceiv->state) {
817#ifdef CONFIG_USB_OTG
818 case OTG_STATE_A_SUSPEND:
819 /* We need to ignore disconnect on suspend
820 * otherwise tusb 2.0 won't reconnect after a
821 * power cycle, which breaks otg compliance.
822 */
823 musb->ignore_disconnect = 1;
824 musb_g_reset(musb);
825 /* FALLTHROUGH */
826 case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
827 /* never use invalid T(a_wait_bcon) */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300828 dev_dbg(musb->controller, "HNP: in %s, %d msec timeout\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200829 otg_state_string(musb->xceiv->state),
830 TA_WAIT_BCON(musb));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200831 mod_timer(&musb->otg_timer, jiffies
832 + msecs_to_jiffies(TA_WAIT_BCON(musb)));
833 break;
834 case OTG_STATE_A_PERIPHERAL:
835 musb->ignore_disconnect = 0;
836 del_timer(&musb->otg_timer);
837 musb_g_reset(musb);
838 break;
839 case OTG_STATE_B_WAIT_ACON:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300840 dev_dbg(musb->controller, "HNP: RESET (%s), to b_peripheral\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200841 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200842 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
843 musb_g_reset(musb);
844 break;
845#endif
846 case OTG_STATE_B_IDLE:
847 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
848 /* FALLTHROUGH */
849 case OTG_STATE_B_PERIPHERAL:
850 musb_g_reset(musb);
851 break;
852 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300853 dev_dbg(musb->controller, "Unhandled BUS RESET as %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +0200854 otg_state_string(musb->xceiv->state));
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200855 }
856 }
857 }
858
859#if 0
860/* REVISIT ... this would be for multiplexing periodic endpoints, or
861 * supporting transfer phasing to prevent exceeding ISO bandwidth
862 * limits of a given frame or microframe.
863 *
864 * It's not needed for peripheral side, which dedicates endpoints;
865 * though it _might_ use SOF irqs for other purposes.
866 *
867 * And it's not currently needed for host side, which also dedicates
868 * endpoints, relies on TX/RX interval registers, and isn't claimed
869 * to support ISO transfers yet.
870 */
871 if (int_usb & MUSB_INTR_SOF) {
872 void __iomem *mbase = musb->mregs;
873 struct musb_hw_ep *ep;
874 u8 epnum;
875 u16 frame;
876
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300877 dev_dbg(musb->controller, "START_OF_FRAME\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300878 handled = IRQ_HANDLED;
879
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200880 /* start any periodic Tx transfers waiting for current frame */
881 frame = musb_readw(mbase, MUSB_FRAME);
882 ep = musb->endpoints;
883 for (epnum = 1; (epnum < musb->nr_endpoints)
884 && (musb->epmask >= (1 << epnum));
885 epnum++, ep++) {
886 /*
887 * FIXME handle framecounter wraps (12 bits)
888 * eliminate duplicated StartUrb logic
Felipe Balbi550a7372008-07-24 12:27:36 +0300889 */
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200890 if (ep->dwWaitFrame >= frame) {
891 ep->dwWaitFrame = 0;
892 pr_debug("SOF --> periodic TX%s on %d\n",
893 ep->tx_channel ? " DMA" : "",
894 epnum);
895 if (!ep->tx_channel)
896 musb_h_tx_start(musb, epnum);
897 else
898 cppi_hostdma_start(musb, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300899 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200900 } /* end of for loop */
Felipe Balbi550a7372008-07-24 12:27:36 +0300901 }
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200902#endif
Felipe Balbi550a7372008-07-24 12:27:36 +0300903
Arnaud Mandy1c25fda2009-12-28 13:40:40 +0200904 schedule_work(&musb->irq_work);
Felipe Balbi550a7372008-07-24 12:27:36 +0300905
906 return handled;
907}
908
909/*-------------------------------------------------------------------------*/
910
911/*
912* Program the HDRC to start (enable interrupts, dma, etc.).
913*/
914void musb_start(struct musb *musb)
915{
916 void __iomem *regs = musb->mregs;
917 u8 devctl = musb_readb(regs, MUSB_DEVCTL);
918
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300919 dev_dbg(musb->controller, "<== devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +0300920
921 /* Set INT enable registers, enable interrupts */
922 musb_writew(regs, MUSB_INTRTXE, musb->epmask);
923 musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
924 musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
925
926 musb_writeb(regs, MUSB_TESTMODE, 0);
927
928 /* put into basic highspeed mode and start session */
929 musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
930 | MUSB_POWER_SOFTCONN
931 | MUSB_POWER_HSENAB
932 /* ENSUSPEND wedges tusb */
933 /* | MUSB_POWER_ENSUSPEND */
934 );
935
936 musb->is_active = 0;
937 devctl = musb_readb(regs, MUSB_DEVCTL);
938 devctl &= ~MUSB_DEVCTL_SESSION;
939
940 if (is_otg_enabled(musb)) {
941 /* session started after:
942 * (a) ID-grounded irq, host mode;
943 * (b) vbus present/connect IRQ, peripheral mode;
944 * (c) peripheral initiates, using SRP
945 */
946 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
947 musb->is_active = 1;
948 else
949 devctl |= MUSB_DEVCTL_SESSION;
950
951 } else if (is_host_enabled(musb)) {
952 /* assume ID pin is hard-wired to ground */
953 devctl |= MUSB_DEVCTL_SESSION;
954
955 } else /* peripheral is enabled */ {
956 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
957 musb->is_active = 1;
958 }
959 musb_platform_enable(musb);
960 musb_writeb(regs, MUSB_DEVCTL, devctl);
961}
962
963
964static void musb_generic_disable(struct musb *musb)
965{
966 void __iomem *mbase = musb->mregs;
967 u16 temp;
968
969 /* disable interrupts */
970 musb_writeb(mbase, MUSB_INTRUSBE, 0);
971 musb_writew(mbase, MUSB_INTRTXE, 0);
972 musb_writew(mbase, MUSB_INTRRXE, 0);
973
974 /* off */
975 musb_writeb(mbase, MUSB_DEVCTL, 0);
976
977 /* flush pending interrupts */
978 temp = musb_readb(mbase, MUSB_INTRUSB);
979 temp = musb_readw(mbase, MUSB_INTRTX);
980 temp = musb_readw(mbase, MUSB_INTRRX);
981
982}
983
984/*
985 * Make the HDRC stop (disable interrupts, etc.);
986 * reversible by musb_start
987 * called on gadget driver unregister
988 * with controller locked, irqs blocked
989 * acts as a NOP unless some role activated the hardware
990 */
991void musb_stop(struct musb *musb)
992{
993 /* stop IRQs, timers, ... */
994 musb_platform_disable(musb);
995 musb_generic_disable(musb);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300996 dev_dbg(musb->controller, "HDRC disabled\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300997
998 /* FIXME
999 * - mark host and/or peripheral drivers unusable/inactive
1000 * - disable DMA (and enable it in HdrcStart)
1001 * - make sure we can musb_start() after musb_stop(); with
1002 * OTG mode, gadget driver module rmmod/modprobe cycles that
1003 * - ...
1004 */
1005 musb_platform_try_idle(musb, 0);
1006}
1007
1008static void musb_shutdown(struct platform_device *pdev)
1009{
1010 struct musb *musb = dev_to_musb(&pdev->dev);
1011 unsigned long flags;
1012
Hema HK4f9edd22011-03-22 16:02:12 +05301013 pm_runtime_get_sync(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001014 spin_lock_irqsave(&musb->lock, flags);
1015 musb_platform_disable(musb);
1016 musb_generic_disable(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001017 spin_unlock_irqrestore(&musb->lock, flags);
1018
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001019 if (!is_otg_enabled(musb) && is_host_enabled(musb))
1020 usb_remove_hcd(musb_to_hcd(musb));
1021 musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
1022 musb_platform_exit(musb);
Grazvydas Ignotas120d0742010-10-10 13:52:22 -05001023
Hema HK4f9edd22011-03-22 16:02:12 +05301024 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001025 /* FIXME power down */
1026}
1027
1028
1029/*-------------------------------------------------------------------------*/
1030
1031/*
1032 * The silicon either has hard-wired endpoint configurations, or else
1033 * "dynamic fifo" sizing. The driver has support for both, though at this
David Brownellc767c1c2008-09-11 11:53:23 +03001034 * writing only the dynamic sizing is very well tested. Since we switched
1035 * away from compile-time hardware parameters, we can no longer rely on
1036 * dead code elimination to leave only the relevant one in the object file.
Felipe Balbi550a7372008-07-24 12:27:36 +03001037 *
1038 * We don't currently use dynamic fifo setup capability to do anything
1039 * more than selecting one of a bunch of predefined configurations.
1040 */
Felipe Balbi7c925542010-12-01 14:23:48 +02001041#if defined(CONFIG_USB_MUSB_TUSB6010) || defined(CONFIG_USB_MUSB_OMAP2PLUS) \
1042 || defined(CONFIG_USB_MUSB_AM35X)
Felipe Balbi550a7372008-07-24 12:27:36 +03001043static ushort __initdata fifo_mode = 4;
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001044#elif defined(CONFIG_USB_MUSB_UX500)
1045static ushort __initdata fifo_mode = 5;
Felipe Balbi550a7372008-07-24 12:27:36 +03001046#else
1047static ushort __initdata fifo_mode = 2;
1048#endif
1049
1050/* "modprobe ... fifo_mode=1" etc */
1051module_param(fifo_mode, ushort, 0);
1052MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
1053
Felipe Balbi550a7372008-07-24 12:27:36 +03001054/*
1055 * tables defining fifo_mode values. define more if you like.
1056 * for host side, make sure both halves of ep1 are set up.
1057 */
1058
1059/* mode 0 - fits in 2KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001060static struct musb_fifo_cfg __initdata mode_0_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001061{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1062{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1063{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
1064{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1065{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1066};
1067
1068/* mode 1 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001069static struct musb_fifo_cfg __initdata mode_1_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001070{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1071{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1072{ .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1073{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1074{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1075};
1076
1077/* mode 2 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001078static struct musb_fifo_cfg __initdata mode_2_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001079{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1080{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1081{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1082{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1083{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1084{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1085};
1086
1087/* mode 3 - fits in 4KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001088static struct musb_fifo_cfg __initdata mode_3_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001089{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1090{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
1091{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1092{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1093{ .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
1094{ .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
1095};
1096
1097/* mode 4 - fits in 16KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001098static struct musb_fifo_cfg __initdata mode_4_cfg[] = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001099{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1100{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1101{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1102{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1103{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1104{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1105{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1106{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1107{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1108{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1109{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
1110{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
1111{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
1112{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
1113{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
1114{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
1115{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
1116{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001117{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 256, },
1118{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 64, },
1119{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 256, },
1120{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 64, },
1121{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 256, },
1122{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 64, },
1123{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 4096, },
Felipe Balbi550a7372008-07-24 12:27:36 +03001124{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1125{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1126};
1127
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001128/* mode 5 - fits in 8KB */
Felipe Balbie6c213b2010-03-12 10:29:06 +02001129static struct musb_fifo_cfg __initdata mode_5_cfg[] = {
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001130{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
1131{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
1132{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
1133{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
1134{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
1135{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
1136{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
1137{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
1138{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
1139{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
1140{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 32, },
1141{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 32, },
1142{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 32, },
1143{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 32, },
1144{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 32, },
1145{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 32, },
1146{ .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 32, },
1147{ .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 32, },
1148{ .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 32, },
1149{ .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 32, },
1150{ .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 32, },
1151{ .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 32, },
1152{ .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 32, },
1153{ .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 32, },
1154{ .hw_ep_num = 13, .style = FIFO_RXTX, .maxpacket = 512, },
1155{ .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
1156{ .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
1157};
Felipe Balbi550a7372008-07-24 12:27:36 +03001158
1159/*
1160 * configure a fifo; for non-shared endpoints, this may be called
1161 * once for a tx fifo and once for an rx fifo.
1162 *
1163 * returns negative errno or offset for next fifo.
1164 */
1165static int __init
1166fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
Felipe Balbie6c213b2010-03-12 10:29:06 +02001167 const struct musb_fifo_cfg *cfg, u16 offset)
Felipe Balbi550a7372008-07-24 12:27:36 +03001168{
1169 void __iomem *mbase = musb->mregs;
1170 int size = 0;
1171 u16 maxpacket = cfg->maxpacket;
1172 u16 c_off = offset >> 3;
1173 u8 c_size;
1174
1175 /* expect hw_ep has already been zero-initialized */
1176
1177 size = ffs(max(maxpacket, (u16) 8)) - 1;
1178 maxpacket = 1 << size;
1179
1180 c_size = size - 3;
1181 if (cfg->mode == BUF_DOUBLE) {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001182 if ((offset + (maxpacket << 1)) >
1183 (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001184 return -EMSGSIZE;
1185 c_size |= MUSB_FIFOSZ_DPB;
1186 } else {
Felipe Balbica6d1b12008-08-08 12:40:54 +03001187 if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
Felipe Balbi550a7372008-07-24 12:27:36 +03001188 return -EMSGSIZE;
1189 }
1190
1191 /* configure the FIFO */
1192 musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
1193
1194#ifdef CONFIG_USB_MUSB_HDRC_HCD
1195 /* EP0 reserved endpoint for control, bidirectional;
1196 * EP1 reserved for bulk, two unidirection halves.
1197 */
1198 if (hw_ep->epnum == 1)
1199 musb->bulk_ep = hw_ep;
1200 /* REVISIT error check: be sure ep0 can both rx and tx ... */
1201#endif
1202 switch (cfg->style) {
1203 case FIFO_TX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001204 musb_write_txfifosz(mbase, c_size);
1205 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001206 hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1207 hw_ep->max_packet_sz_tx = maxpacket;
1208 break;
1209 case FIFO_RX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001210 musb_write_rxfifosz(mbase, c_size);
1211 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001212 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1213 hw_ep->max_packet_sz_rx = maxpacket;
1214 break;
1215 case FIFO_RXTX:
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001216 musb_write_txfifosz(mbase, c_size);
1217 musb_write_txfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001218 hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
1219 hw_ep->max_packet_sz_rx = maxpacket;
1220
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001221 musb_write_rxfifosz(mbase, c_size);
1222 musb_write_rxfifoadd(mbase, c_off);
Felipe Balbi550a7372008-07-24 12:27:36 +03001223 hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
1224 hw_ep->max_packet_sz_tx = maxpacket;
1225
1226 hw_ep->is_shared_fifo = true;
1227 break;
1228 }
1229
1230 /* NOTE rx and tx endpoint irqs aren't managed separately,
1231 * which happens to be ok
1232 */
1233 musb->epmask |= (1 << hw_ep->epnum);
1234
1235 return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
1236}
1237
Felipe Balbie6c213b2010-03-12 10:29:06 +02001238static struct musb_fifo_cfg __initdata ep0_cfg = {
Felipe Balbi550a7372008-07-24 12:27:36 +03001239 .style = FIFO_RXTX, .maxpacket = 64,
1240};
1241
1242static int __init ep_config_from_table(struct musb *musb)
1243{
Felipe Balbie6c213b2010-03-12 10:29:06 +02001244 const struct musb_fifo_cfg *cfg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001245 unsigned i, n;
1246 int offset;
1247 struct musb_hw_ep *hw_ep = musb->endpoints;
1248
Felipe Balbie6c213b2010-03-12 10:29:06 +02001249 if (musb->config->fifo_cfg) {
1250 cfg = musb->config->fifo_cfg;
1251 n = musb->config->fifo_cfg_size;
1252 goto done;
1253 }
1254
Felipe Balbi550a7372008-07-24 12:27:36 +03001255 switch (fifo_mode) {
1256 default:
1257 fifo_mode = 0;
1258 /* FALLTHROUGH */
1259 case 0:
1260 cfg = mode_0_cfg;
1261 n = ARRAY_SIZE(mode_0_cfg);
1262 break;
1263 case 1:
1264 cfg = mode_1_cfg;
1265 n = ARRAY_SIZE(mode_1_cfg);
1266 break;
1267 case 2:
1268 cfg = mode_2_cfg;
1269 n = ARRAY_SIZE(mode_2_cfg);
1270 break;
1271 case 3:
1272 cfg = mode_3_cfg;
1273 n = ARRAY_SIZE(mode_3_cfg);
1274 break;
1275 case 4:
1276 cfg = mode_4_cfg;
1277 n = ARRAY_SIZE(mode_4_cfg);
1278 break;
Ajay Kumar Gupta3b151522009-12-28 13:40:34 +02001279 case 5:
1280 cfg = mode_5_cfg;
1281 n = ARRAY_SIZE(mode_5_cfg);
1282 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001283 }
1284
1285 printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
1286 musb_driver_name, fifo_mode);
1287
1288
Felipe Balbie6c213b2010-03-12 10:29:06 +02001289done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001290 offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
1291 /* assert(offset > 0) */
1292
1293 /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
Felipe Balbica6d1b12008-08-08 12:40:54 +03001294 * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
Felipe Balbi550a7372008-07-24 12:27:36 +03001295 */
1296
1297 for (i = 0; i < n; i++) {
1298 u8 epn = cfg->hw_ep_num;
1299
Felipe Balbica6d1b12008-08-08 12:40:54 +03001300 if (epn >= musb->config->num_eps) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001301 pr_debug("%s: invalid ep %d\n",
1302 musb_driver_name, epn);
David Brownellbb1c9ef2008-11-24 13:06:50 +02001303 return -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001304 }
1305 offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
1306 if (offset < 0) {
1307 pr_debug("%s: mem overrun, ep %d\n",
1308 musb_driver_name, epn);
1309 return -EINVAL;
1310 }
1311 epn++;
1312 musb->nr_endpoints = max(epn, musb->nr_endpoints);
1313 }
1314
1315 printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
1316 musb_driver_name,
Felipe Balbica6d1b12008-08-08 12:40:54 +03001317 n + 1, musb->config->num_eps * 2 - 1,
1318 offset, (1 << (musb->config->ram_bits + 2)));
Felipe Balbi550a7372008-07-24 12:27:36 +03001319
1320#ifdef CONFIG_USB_MUSB_HDRC_HCD
1321 if (!musb->bulk_ep) {
1322 pr_debug("%s: missing bulk\n", musb_driver_name);
1323 return -EINVAL;
1324 }
1325#endif
1326
1327 return 0;
1328}
1329
1330
1331/*
1332 * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
1333 * @param musb the controller
1334 */
1335static int __init ep_config_from_hw(struct musb *musb)
1336{
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001337 u8 epnum = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001338 struct musb_hw_ep *hw_ep;
1339 void *mbase = musb->mregs;
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001340 int ret = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001341
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001342 dev_dbg(musb->controller, "<== static silicon ep config\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001343
1344 /* FIXME pick up ep0 maxpacket size */
1345
Felipe Balbica6d1b12008-08-08 12:40:54 +03001346 for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001347 musb_ep_select(mbase, epnum);
1348 hw_ep = musb->endpoints + epnum;
1349
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001350 ret = musb_read_fifosize(musb, hw_ep, epnum);
1351 if (ret < 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03001352 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03001353
1354 /* FIXME set up hw_ep->{rx,tx}_double_buffered */
1355
1356#ifdef CONFIG_USB_MUSB_HDRC_HCD
1357 /* pick an RX/TX endpoint for bulk */
1358 if (hw_ep->max_packet_sz_tx < 512
1359 || hw_ep->max_packet_sz_rx < 512)
1360 continue;
1361
1362 /* REVISIT: this algorithm is lazy, we should at least
1363 * try to pick a double buffered endpoint.
1364 */
1365 if (musb->bulk_ep)
1366 continue;
1367 musb->bulk_ep = hw_ep;
1368#endif
1369 }
1370
1371#ifdef CONFIG_USB_MUSB_HDRC_HCD
1372 if (!musb->bulk_ep) {
1373 pr_debug("%s: missing bulk\n", musb_driver_name);
1374 return -EINVAL;
1375 }
1376#endif
1377
1378 return 0;
1379}
1380
1381enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
1382
1383/* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
1384 * configure endpoints, or take their config from silicon
1385 */
1386static int __init musb_core_init(u16 musb_type, struct musb *musb)
1387{
Felipe Balbi550a7372008-07-24 12:27:36 +03001388 u8 reg;
1389 char *type;
Maulik Mankad0ea52ff2009-12-22 16:19:53 +05301390 char aInfo[90], aRevision[32], aDate[12];
Felipe Balbi550a7372008-07-24 12:27:36 +03001391 void __iomem *mbase = musb->mregs;
1392 int status = 0;
1393 int i;
1394
1395 /* log core options (read using indexed model) */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001396 reg = musb_read_configdata(mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001397
1398 strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001399 if (reg & MUSB_CONFIGDATA_DYNFIFO) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001400 strcat(aInfo, ", dyn FIFOs");
Ajay Kumar Gupta51bf0d02009-12-28 13:40:41 +02001401 musb->dyn_fifo = true;
1402 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001403 if (reg & MUSB_CONFIGDATA_MPRXE) {
1404 strcat(aInfo, ", bulk combine");
Felipe Balbi550a7372008-07-24 12:27:36 +03001405 musb->bulk_combine = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001406 }
1407 if (reg & MUSB_CONFIGDATA_MPTXE) {
1408 strcat(aInfo, ", bulk split");
Felipe Balbi550a7372008-07-24 12:27:36 +03001409 musb->bulk_split = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001410 }
1411 if (reg & MUSB_CONFIGDATA_HBRXE) {
1412 strcat(aInfo, ", HB-ISO Rx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001413 musb->hb_iso_rx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001414 }
1415 if (reg & MUSB_CONFIGDATA_HBTXE) {
1416 strcat(aInfo, ", HB-ISO Tx");
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001417 musb->hb_iso_tx = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001418 }
1419 if (reg & MUSB_CONFIGDATA_SOFTCONE)
1420 strcat(aInfo, ", SoftConn");
1421
1422 printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
1423 musb_driver_name, reg, aInfo);
1424
Felipe Balbi550a7372008-07-24 12:27:36 +03001425 aDate[0] = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001426 if (MUSB_CONTROLLER_MHDRC == musb_type) {
1427 musb->is_multipoint = 1;
1428 type = "M";
1429 } else {
1430 musb->is_multipoint = 0;
1431 type = "";
1432#ifdef CONFIG_USB_MUSB_HDRC_HCD
1433#ifndef CONFIG_USB_OTG_BLACKLIST_HUB
1434 printk(KERN_ERR
1435 "%s: kernel must blacklist external hubs\n",
1436 musb_driver_name);
1437#endif
1438#endif
1439 }
1440
1441 /* log release info */
Anand Gadiyar32c3b942009-11-16 21:09:21 +05301442 musb->hwvers = musb_read_hwvers(mbase);
1443 snprintf(aRevision, 32, "%d.%d%s", MUSB_HWVERS_MAJOR(musb->hwvers),
1444 MUSB_HWVERS_MINOR(musb->hwvers),
1445 (musb->hwvers & MUSB_HWVERS_RC) ? "RC" : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001446 printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
1447 musb_driver_name, type, aRevision, aDate);
1448
1449 /* configure ep0 */
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001450 musb_configure_ep0(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001451
1452 /* discover endpoint configuration */
1453 musb->nr_endpoints = 1;
1454 musb->epmask = 1;
1455
Felipe Balbiad517e9e2010-01-21 15:33:54 +02001456 if (musb->dyn_fifo)
1457 status = ep_config_from_table(musb);
1458 else
1459 status = ep_config_from_hw(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001460
1461 if (status < 0)
1462 return status;
1463
1464 /* finish init, and print endpoint config */
1465 for (i = 0; i < musb->nr_endpoints; i++) {
1466 struct musb_hw_ep *hw_ep = musb->endpoints + i;
1467
1468 hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
Felipe Balbi7c925542010-12-01 14:23:48 +02001469#ifdef CONFIG_USB_MUSB_TUSB6010
Felipe Balbi550a7372008-07-24 12:27:36 +03001470 hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
1471 hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
1472 hw_ep->fifo_sync_va =
1473 musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
1474
1475 if (i == 0)
1476 hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
1477 else
1478 hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
1479#endif
1480
1481 hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
1482#ifdef CONFIG_USB_MUSB_HDRC_HCD
Bryan Wuc6cf8b02008-12-02 21:33:48 +02001483 hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
Felipe Balbi550a7372008-07-24 12:27:36 +03001484 hw_ep->rx_reinit = 1;
1485 hw_ep->tx_reinit = 1;
1486#endif
1487
1488 if (hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001489 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001490 "%s: hw_ep %d%s, %smax %d\n",
1491 musb_driver_name, i,
1492 hw_ep->is_shared_fifo ? "shared" : "tx",
1493 hw_ep->tx_double_buffered
1494 ? "doublebuffer, " : "",
1495 hw_ep->max_packet_sz_tx);
1496 }
1497 if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001498 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03001499 "%s: hw_ep %d%s, %smax %d\n",
1500 musb_driver_name, i,
1501 "rx",
1502 hw_ep->rx_double_buffered
1503 ? "doublebuffer, " : "",
1504 hw_ep->max_packet_sz_rx);
1505 }
1506 if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001507 dev_dbg(musb->controller, "hw_ep %d not configured\n", i);
Felipe Balbi550a7372008-07-24 12:27:36 +03001508 }
1509
1510 return 0;
1511}
1512
1513/*-------------------------------------------------------------------------*/
1514
Tony Lindgren59b479e2011-01-27 16:39:40 -08001515#if defined(CONFIG_SOC_OMAP2430) || defined(CONFIG_SOC_OMAP3430) || \
Mian Yousaf Kaukab4bc36fd2010-12-09 13:05:01 +01001516 defined(CONFIG_ARCH_OMAP4) || defined(CONFIG_ARCH_U8500) || \
1517 defined(CONFIG_ARCH_U5500)
Felipe Balbi550a7372008-07-24 12:27:36 +03001518
1519static irqreturn_t generic_interrupt(int irq, void *__hci)
1520{
1521 unsigned long flags;
1522 irqreturn_t retval = IRQ_NONE;
1523 struct musb *musb = __hci;
1524
1525 spin_lock_irqsave(&musb->lock, flags);
1526
1527 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
1528 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
1529 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
1530
1531 if (musb->int_usb || musb->int_tx || musb->int_rx)
1532 retval = musb_interrupt(musb);
1533
1534 spin_unlock_irqrestore(&musb->lock, flags);
1535
Sergei Shtylyova5073b52009-03-27 12:52:43 -07001536 return retval;
Felipe Balbi550a7372008-07-24 12:27:36 +03001537}
1538
1539#else
1540#define generic_interrupt NULL
1541#endif
1542
1543/*
1544 * handle all the irqs defined by the HDRC core. for now we expect: other
1545 * irq sources (phy, dma, etc) will be handled first, musb->int_* values
1546 * will be assigned, and the irq will already have been acked.
1547 *
1548 * called in irq context with spinlock held, irqs blocked
1549 */
1550irqreturn_t musb_interrupt(struct musb *musb)
1551{
1552 irqreturn_t retval = IRQ_NONE;
1553 u8 devctl, power;
1554 int ep_num;
1555 u32 reg;
1556
1557 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1558 power = musb_readb(musb->mregs, MUSB_POWER);
1559
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001560 dev_dbg(musb->controller, "** IRQ %s usb%04x tx%04x rx%04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001561 (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
1562 musb->int_usb, musb->int_tx, musb->int_rx);
1563
Felipe Balbicd42fef2009-12-15 13:47:30 +02001564#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1565 if (is_otg_enabled(musb) || is_peripheral_enabled(musb))
1566 if (!musb->gadget_driver) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001567 dev_dbg(musb->controller, "No gadget driver loaded\n");
Felipe Balbicd42fef2009-12-15 13:47:30 +02001568 return IRQ_HANDLED;
1569 }
1570#endif
1571
Felipe Balbi550a7372008-07-24 12:27:36 +03001572 /* the core can interrupt us for multiple reasons; docs have
1573 * a generic interrupt flowchart to follow
1574 */
Sergei Shtylyov7d9645f2010-06-24 23:07:06 +05301575 if (musb->int_usb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001576 retval |= musb_stage0_irq(musb, musb->int_usb,
1577 devctl, power);
1578
1579 /* "stage 1" is handling endpoint irqs */
1580
1581 /* handle endpoint 0 first */
1582 if (musb->int_tx & 1) {
1583 if (devctl & MUSB_DEVCTL_HM)
1584 retval |= musb_h_ep0_irq(musb);
1585 else
1586 retval |= musb_g_ep0_irq(musb);
1587 }
1588
1589 /* RX on endpoints 1-15 */
1590 reg = musb->int_rx >> 1;
1591 ep_num = 1;
1592 while (reg) {
1593 if (reg & 1) {
1594 /* musb_ep_select(musb->mregs, ep_num); */
1595 /* REVISIT just retval = ep->rx_irq(...) */
1596 retval = IRQ_HANDLED;
1597 if (devctl & MUSB_DEVCTL_HM) {
1598 if (is_host_capable())
1599 musb_host_rx(musb, ep_num);
1600 } else {
1601 if (is_peripheral_capable())
1602 musb_g_rx(musb, ep_num);
1603 }
1604 }
1605
1606 reg >>= 1;
1607 ep_num++;
1608 }
1609
1610 /* TX on endpoints 1-15 */
1611 reg = musb->int_tx >> 1;
1612 ep_num = 1;
1613 while (reg) {
1614 if (reg & 1) {
1615 /* musb_ep_select(musb->mregs, ep_num); */
1616 /* REVISIT just retval |= ep->tx_irq(...) */
1617 retval = IRQ_HANDLED;
1618 if (devctl & MUSB_DEVCTL_HM) {
1619 if (is_host_capable())
1620 musb_host_tx(musb, ep_num);
1621 } else {
1622 if (is_peripheral_capable())
1623 musb_g_tx(musb, ep_num);
1624 }
1625 }
1626 reg >>= 1;
1627 ep_num++;
1628 }
1629
Felipe Balbi550a7372008-07-24 12:27:36 +03001630 return retval;
1631}
Felipe Balbi981430a2011-05-11 13:02:23 +03001632EXPORT_SYMBOL_GPL(musb_interrupt);
Felipe Balbi550a7372008-07-24 12:27:36 +03001633
1634#ifndef CONFIG_MUSB_PIO_ONLY
1635static int __initdata use_dma = 1;
1636
1637/* "modprobe ... use_dma=0" etc */
1638module_param(use_dma, bool, 0);
1639MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
1640
1641void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
1642{
1643 u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1644
1645 /* called with controller lock already held */
1646
1647 if (!epnum) {
1648#ifndef CONFIG_USB_TUSB_OMAP_DMA
1649 if (!is_cppi_enabled()) {
1650 /* endpoint 0 */
1651 if (devctl & MUSB_DEVCTL_HM)
1652 musb_h_ep0_irq(musb);
1653 else
1654 musb_g_ep0_irq(musb);
1655 }
1656#endif
1657 } else {
1658 /* endpoints 1..15 */
1659 if (transmit) {
1660 if (devctl & MUSB_DEVCTL_HM) {
1661 if (is_host_capable())
1662 musb_host_tx(musb, epnum);
1663 } else {
1664 if (is_peripheral_capable())
1665 musb_g_tx(musb, epnum);
1666 }
1667 } else {
1668 /* receive */
1669 if (devctl & MUSB_DEVCTL_HM) {
1670 if (is_host_capable())
1671 musb_host_rx(musb, epnum);
1672 } else {
1673 if (is_peripheral_capable())
1674 musb_g_rx(musb, epnum);
1675 }
1676 }
1677 }
1678}
1679
1680#else
1681#define use_dma 0
1682#endif
1683
1684/*-------------------------------------------------------------------------*/
1685
1686#ifdef CONFIG_SYSFS
1687
1688static ssize_t
1689musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1690{
1691 struct musb *musb = dev_to_musb(dev);
1692 unsigned long flags;
1693 int ret = -EINVAL;
1694
1695 spin_lock_irqsave(&musb->lock, flags);
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001696 ret = sprintf(buf, "%s\n", otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001697 spin_unlock_irqrestore(&musb->lock, flags);
1698
1699 return ret;
1700}
1701
1702static ssize_t
1703musb_mode_store(struct device *dev, struct device_attribute *attr,
1704 const char *buf, size_t n)
1705{
1706 struct musb *musb = dev_to_musb(dev);
1707 unsigned long flags;
David Brownell96a274d2008-11-24 13:06:47 +02001708 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001709
1710 spin_lock_irqsave(&musb->lock, flags);
David Brownell96a274d2008-11-24 13:06:47 +02001711 if (sysfs_streq(buf, "host"))
1712 status = musb_platform_set_mode(musb, MUSB_HOST);
1713 else if (sysfs_streq(buf, "peripheral"))
1714 status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
1715 else if (sysfs_streq(buf, "otg"))
1716 status = musb_platform_set_mode(musb, MUSB_OTG);
1717 else
1718 status = -EINVAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001719 spin_unlock_irqrestore(&musb->lock, flags);
1720
David Brownell96a274d2008-11-24 13:06:47 +02001721 return (status == 0) ? n : status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001722}
1723static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
1724
1725static ssize_t
1726musb_vbus_store(struct device *dev, struct device_attribute *attr,
1727 const char *buf, size_t n)
1728{
1729 struct musb *musb = dev_to_musb(dev);
1730 unsigned long flags;
1731 unsigned long val;
1732
1733 if (sscanf(buf, "%lu", &val) < 1) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001734 dev_err(dev, "Invalid VBUS timeout ms value\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001735 return -EINVAL;
1736 }
1737
1738 spin_lock_irqsave(&musb->lock, flags);
David Brownellf7f9d632009-03-31 12:32:12 -07001739 /* force T(a_wait_bcon) to be zero/unlimited *OR* valid */
1740 musb->a_wait_bcon = val ? max_t(int, val, OTG_TIME_A_WAIT_BCON) : 0 ;
David Brownell84e250f2009-03-31 12:30:04 -07001741 if (musb->xceiv->state == OTG_STATE_A_WAIT_BCON)
Felipe Balbi550a7372008-07-24 12:27:36 +03001742 musb->is_active = 0;
1743 musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
1744 spin_unlock_irqrestore(&musb->lock, flags);
1745
1746 return n;
1747}
1748
1749static ssize_t
1750musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
1751{
1752 struct musb *musb = dev_to_musb(dev);
1753 unsigned long flags;
1754 unsigned long val;
1755 int vbus;
1756
1757 spin_lock_irqsave(&musb->lock, flags);
1758 val = musb->a_wait_bcon;
David Brownellf7f9d632009-03-31 12:32:12 -07001759 /* FIXME get_vbus_status() is normally #defined as false...
1760 * and is effectively TUSB-specific.
1761 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001762 vbus = musb_platform_get_vbus_status(musb);
1763 spin_unlock_irqrestore(&musb->lock, flags);
1764
David Brownellf7f9d632009-03-31 12:32:12 -07001765 return sprintf(buf, "Vbus %s, timeout %lu msec\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001766 vbus ? "on" : "off", val);
1767}
1768static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
1769
1770#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1771
1772/* Gadget drivers can't know that a host is connected so they might want
1773 * to start SRP, but users can. This allows userspace to trigger SRP.
1774 */
1775static ssize_t
1776musb_srp_store(struct device *dev, struct device_attribute *attr,
1777 const char *buf, size_t n)
1778{
1779 struct musb *musb = dev_to_musb(dev);
1780 unsigned short srp;
1781
1782 if (sscanf(buf, "%hu", &srp) != 1
1783 || (srp != 1)) {
Felipe Balbib3b1cc32009-12-15 11:08:43 +02001784 dev_err(dev, "SRP: Value must be 1\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001785 return -EINVAL;
1786 }
1787
1788 if (srp == 1)
1789 musb_g_wakeup(musb);
1790
1791 return n;
1792}
1793static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
1794
1795#endif /* CONFIG_USB_GADGET_MUSB_HDRC */
1796
Felipe Balbi94375752009-12-15 11:08:38 +02001797static struct attribute *musb_attributes[] = {
1798 &dev_attr_mode.attr,
1799 &dev_attr_vbus.attr,
1800#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1801 &dev_attr_srp.attr,
1802#endif
1803 NULL
1804};
1805
1806static const struct attribute_group musb_attr_group = {
1807 .attrs = musb_attributes,
1808};
1809
Felipe Balbi550a7372008-07-24 12:27:36 +03001810#endif /* sysfs */
1811
1812/* Only used to provide driver mode change events */
1813static void musb_irq_work(struct work_struct *data)
1814{
1815 struct musb *musb = container_of(data, struct musb, irq_work);
1816 static int old_state;
1817
David Brownell84e250f2009-03-31 12:30:04 -07001818 if (musb->xceiv->state != old_state) {
1819 old_state = musb->xceiv->state;
Felipe Balbi550a7372008-07-24 12:27:36 +03001820 sysfs_notify(&musb->controller->kobj, NULL, "mode");
1821 }
1822}
1823
1824/* --------------------------------------------------------------------------
1825 * Init support
1826 */
1827
1828static struct musb *__init
Felipe Balbica6d1b12008-08-08 12:40:54 +03001829allocate_instance(struct device *dev,
1830 struct musb_hdrc_config *config, void __iomem *mbase)
Felipe Balbi550a7372008-07-24 12:27:36 +03001831{
1832 struct musb *musb;
1833 struct musb_hw_ep *ep;
1834 int epnum;
1835#ifdef CONFIG_USB_MUSB_HDRC_HCD
1836 struct usb_hcd *hcd;
1837
Kay Sievers427c4f32008-11-07 01:52:53 +01001838 hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
Felipe Balbi550a7372008-07-24 12:27:36 +03001839 if (!hcd)
1840 return NULL;
1841 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
1842
1843 musb = hcd_to_musb(hcd);
1844 INIT_LIST_HEAD(&musb->control);
1845 INIT_LIST_HEAD(&musb->in_bulk);
1846 INIT_LIST_HEAD(&musb->out_bulk);
1847
1848 hcd->uses_new_polling = 1;
Felipe Balbiec95d352011-02-24 10:36:53 +02001849 hcd->has_tt = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001850
1851 musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
David Brownellf7f9d632009-03-31 12:32:12 -07001852 musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
Felipe Balbi550a7372008-07-24 12:27:36 +03001853#else
1854 musb = kzalloc(sizeof *musb, GFP_KERNEL);
1855 if (!musb)
1856 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001857
1858#endif
Ming Lei456bb162010-12-21 21:16:11 +08001859 dev_set_drvdata(dev, musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001860 musb->mregs = mbase;
1861 musb->ctrl_base = mbase;
1862 musb->nIrq = -ENODEV;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001863 musb->config = config;
Kevin Hilman02582b92008-09-15 12:09:31 +02001864 BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
Felipe Balbi550a7372008-07-24 12:27:36 +03001865 for (epnum = 0, ep = musb->endpoints;
Felipe Balbica6d1b12008-08-08 12:40:54 +03001866 epnum < musb->config->num_eps;
Felipe Balbi550a7372008-07-24 12:27:36 +03001867 epnum++, ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001868 ep->musb = musb;
1869 ep->epnum = epnum;
1870 }
1871
1872 musb->controller = dev;
Felipe Balbi743411b2010-12-01 13:22:05 +02001873
Felipe Balbi550a7372008-07-24 12:27:36 +03001874 return musb;
1875}
1876
1877static void musb_free(struct musb *musb)
1878{
1879 /* this has multiple entry modes. it handles fault cleanup after
1880 * probe(), where things may be partially set up, as well as rmmod
1881 * cleanup after everything's been de-activated.
1882 */
1883
1884#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02001885 sysfs_remove_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi550a7372008-07-24 12:27:36 +03001886#endif
1887
1888#ifdef CONFIG_USB_GADGET_MUSB_HDRC
1889 musb_gadget_cleanup(musb);
1890#endif
1891
Ajay Kumar Gupta97a39892009-01-24 17:56:39 -08001892 if (musb->nIrq >= 0) {
1893 if (musb->irq_wake)
1894 disable_irq_wake(musb->nIrq);
Felipe Balbi550a7372008-07-24 12:27:36 +03001895 free_irq(musb->nIrq, musb);
1896 }
1897 if (is_dma_capable() && musb->dma_controller) {
1898 struct dma_controller *c = musb->dma_controller;
1899
1900 (void) c->stop(c);
1901 dma_controller_destroy(c);
1902 }
1903
Felipe Balbi550a7372008-07-24 12:27:36 +03001904#ifdef CONFIG_USB_MUSB_HDRC_HCD
1905 usb_put_hcd(musb_to_hcd(musb));
1906#else
1907 kfree(musb);
1908#endif
1909}
1910
1911/*
1912 * Perform generic per-controller initialization.
1913 *
1914 * @pDevice: the controller (already clocked, etc)
1915 * @nIrq: irq
1916 * @mregs: virtual address of controller registers,
1917 * not yet corrected for platform-specific offsets
1918 */
1919static int __init
1920musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
1921{
1922 int status;
1923 struct musb *musb;
1924 struct musb_hdrc_platform_data *plat = dev->platform_data;
1925
1926 /* The driver might handle more features than the board; OK.
1927 * Fail when the board needs a feature that's not enabled.
1928 */
1929 if (!plat) {
1930 dev_dbg(dev, "no platform_data?\n");
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001931 status = -ENODEV;
1932 goto fail0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001933 }
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001934
Felipe Balbi550a7372008-07-24 12:27:36 +03001935 /* allocate */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001936 musb = allocate_instance(dev, plat->config, ctrl);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001937 if (!musb) {
1938 status = -ENOMEM;
1939 goto fail0;
1940 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001941
Hema HK7acc6192011-02-28 14:19:34 +05301942 pm_runtime_use_autosuspend(musb->controller);
1943 pm_runtime_set_autosuspend_delay(musb->controller, 200);
1944 pm_runtime_enable(musb->controller);
1945
Felipe Balbi550a7372008-07-24 12:27:36 +03001946 spin_lock_init(&musb->lock);
1947 musb->board_mode = plat->mode;
1948 musb->board_set_power = plat->set_power;
Felipe Balbi550a7372008-07-24 12:27:36 +03001949 musb->min_power = plat->min_power;
Felipe Balbif7ec9432010-12-02 09:48:58 +02001950 musb->ops = plat->platform_ops;
Felipe Balbi550a7372008-07-24 12:27:36 +03001951
David Brownell84e250f2009-03-31 12:30:04 -07001952 /* The musb_platform_init() call:
1953 * - adjusts musb->mregs and musb->isr if needed,
1954 * - may initialize an integrated tranceiver
1955 * - initializes musb->xceiv, usually by otg_get_transceiver()
David Brownell84e250f2009-03-31 12:30:04 -07001956 * - stops powering VBUS
David Brownell84e250f2009-03-31 12:30:04 -07001957 *
1958 * There are various transciever configurations. Blackfin,
1959 * DaVinci, TUSB60x0, and others integrate them. OMAP3 uses
1960 * external/discrete ones in various flavors (twl4030 family,
1961 * isp1504, non-OTG, etc) mostly hooking up through ULPI.
Felipe Balbi550a7372008-07-24 12:27:36 +03001962 */
1963 musb->isr = generic_interrupt;
Hema Kalliguddiea65df52010-09-22 19:27:40 -05001964 status = musb_platform_init(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001965 if (status < 0)
Felipe Balbi03491762010-12-02 09:57:08 +02001966 goto fail1;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001967
Felipe Balbi550a7372008-07-24 12:27:36 +03001968 if (!musb->isr) {
1969 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02001970 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03001971 }
1972
Heikki Krogerusffb865b2010-03-25 13:25:28 +02001973 if (!musb->xceiv->io_ops) {
1974 musb->xceiv->io_priv = musb->mregs;
1975 musb->xceiv->io_ops = &musb_ulpi_access;
1976 }
1977
Felipe Balbi550a7372008-07-24 12:27:36 +03001978#ifndef CONFIG_MUSB_PIO_ONLY
1979 if (use_dma && dev->dma_mask) {
1980 struct dma_controller *c;
1981
1982 c = dma_controller_create(musb, musb->mregs);
1983 musb->dma_controller = c;
1984 if (c)
1985 (void) c->start(c);
1986 }
1987#endif
1988 /* ideally this would be abstracted in platform setup */
1989 if (!is_dma_capable() || !musb->dma_controller)
1990 dev->dma_mask = NULL;
1991
1992 /* be sure interrupts are disabled before connecting ISR */
1993 musb_platform_disable(musb);
1994 musb_generic_disable(musb);
1995
1996 /* setup musb parts of the core (especially endpoints) */
Felipe Balbica6d1b12008-08-08 12:40:54 +03001997 status = musb_core_init(plat->config->multipoint
Felipe Balbi550a7372008-07-24 12:27:36 +03001998 ? MUSB_CONTROLLER_MHDRC
1999 : MUSB_CONTROLLER_HDRC, musb);
2000 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002001 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002002
Amit Kucheria3a9f5bd2009-07-27 12:03:19 +03002003#ifdef CONFIG_USB_MUSB_OTG
David Brownellf7f9d632009-03-31 12:32:12 -07002004 setup_timer(&musb->otg_timer, musb_otg_timer_func, (unsigned long) musb);
2005#endif
2006
Felipe Balbi550a7372008-07-24 12:27:36 +03002007 /* Init IRQ workqueue before request_irq */
2008 INIT_WORK(&musb->irq_work, musb_irq_work);
2009
2010 /* attach to the IRQ */
Kay Sievers427c4f32008-11-07 01:52:53 +01002011 if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002012 dev_err(dev, "request_irq %d failed!\n", nIrq);
2013 status = -ENODEV;
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002014 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002015 }
2016 musb->nIrq = nIrq;
2017/* FIXME this handles wakeup irqs wrong */
Felipe Balbic48a5152008-11-24 13:06:53 +02002018 if (enable_irq_wake(nIrq) == 0) {
2019 musb->irq_wake = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03002020 device_init_wakeup(dev, 1);
Felipe Balbic48a5152008-11-24 13:06:53 +02002021 } else {
2022 musb->irq_wake = 0;
2023 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002024
David Brownell84e250f2009-03-31 12:30:04 -07002025 /* host side needs more setup */
2026 if (is_host_enabled(musb)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002027 struct usb_hcd *hcd = musb_to_hcd(musb);
2028
David Brownell84e250f2009-03-31 12:30:04 -07002029 otg_set_host(musb->xceiv, &hcd->self);
2030
2031 if (is_otg_enabled(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002032 hcd->self.otg_port = 1;
David Brownell84e250f2009-03-31 12:30:04 -07002033 musb->xceiv->host = &hcd->self;
Felipe Balbi550a7372008-07-24 12:27:36 +03002034 hcd->power_budget = 2 * (plat->power ? : 250);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002035
2036 /* program PHY to use external vBus if required */
2037 if (plat->extvbus) {
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002038 u8 busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002039 busctl |= MUSB_ULPI_USE_EXTVBUS;
Mike Frysingeradb3ee42010-03-12 10:27:21 +02002040 musb_write_ulpi_buscontrol(musb->mregs, busctl);
Ajay Kumar Gupta5fc4e772009-12-28 13:40:42 +02002041 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002042 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002043
2044 /* For the host-only role, we can activate right away.
2045 * (We expect the ID pin to be forcibly grounded!!)
2046 * Otherwise, wait till the gadget driver hooks up.
2047 */
2048 if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302049 struct usb_hcd *hcd = musb_to_hcd(musb);
2050
Felipe Balbi550a7372008-07-24 12:27:36 +03002051 MUSB_HST_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002052 musb->xceiv->default_a = 1;
2053 musb->xceiv->state = OTG_STATE_A_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002054
2055 status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
2056
Anand Gadiyar07a8cdd2010-11-18 18:54:17 +05302057 hcd->self.uses_pio_for_control = 1;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002058 dev_dbg(musb->controller, "%s mode, status %d, devctl %02x %c\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002059 "HOST", status,
2060 musb_readb(musb->mregs, MUSB_DEVCTL),
2061 (musb_readb(musb->mregs, MUSB_DEVCTL)
2062 & MUSB_DEVCTL_BDEVICE
2063 ? 'B' : 'A'));
2064
2065 } else /* peripheral is enabled */ {
2066 MUSB_DEV_MODE(musb);
David Brownell84e250f2009-03-31 12:30:04 -07002067 musb->xceiv->default_a = 0;
2068 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002069
2070 status = musb_gadget_setup(musb);
2071
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002072 dev_dbg(musb->controller, "%s mode, status %d, dev%02x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002073 is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
2074 status,
2075 musb_readb(musb->mregs, MUSB_DEVCTL));
2076
2077 }
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002078 if (status < 0)
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002079 goto fail3;
Felipe Balbi550a7372008-07-24 12:27:36 +03002080
Hema HK7acc6192011-02-28 14:19:34 +05302081 pm_runtime_put(musb->controller);
2082
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002083 status = musb_init_debugfs(musb);
2084 if (status < 0)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002085 goto fail4;
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002086
Felipe Balbi550a7372008-07-24 12:27:36 +03002087#ifdef CONFIG_SYSFS
Felipe Balbi94375752009-12-15 11:08:38 +02002088 status = sysfs_create_group(&musb->controller->kobj, &musb_attr_group);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002089 if (status)
Felipe Balbib0f9da72010-03-25 13:25:18 +02002090 goto fail5;
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002091#endif
Felipe Balbi28c2c512008-09-11 11:53:25 +03002092
Felipe Balbiab3bbfa2010-01-21 15:33:58 +02002093 dev_info(dev, "USB %s mode controller at %p using %s, IRQ %d\n",
2094 ({char *s;
2095 switch (musb->board_mode) {
2096 case MUSB_HOST: s = "Host"; break;
2097 case MUSB_PERIPHERAL: s = "Peripheral"; break;
2098 default: s = "OTG"; break;
2099 }; s; }),
2100 ctrl,
2101 (is_dma_capable() && musb->dma_controller)
2102 ? "DMA" : "PIO",
2103 musb->nIrq);
2104
Felipe Balbi28c2c512008-09-11 11:53:25 +03002105 return 0;
2106
Felipe Balbib0f9da72010-03-25 13:25:18 +02002107fail5:
2108 musb_exit_debugfs(musb);
2109
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002110fail4:
2111 if (!is_otg_enabled(musb) && is_host_enabled(musb))
2112 usb_remove_hcd(musb_to_hcd(musb));
2113 else
2114 musb_gadget_cleanup(musb);
2115
2116fail3:
2117 if (musb->irq_wake)
2118 device_init_wakeup(dev, 0);
Felipe Balbi28c2c512008-09-11 11:53:25 +03002119 musb_platform_exit(musb);
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002120
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002121fail1:
Felipe Balbi28c2c512008-09-11 11:53:25 +03002122 dev_err(musb->controller,
2123 "musb_init_controller failed with status %d\n", status);
2124
Felipe Balbi28c2c512008-09-11 11:53:25 +03002125 musb_free(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002126
Sergei Shtylyov34e2beb2010-03-25 13:14:33 +02002127fail0:
2128
Felipe Balbi550a7372008-07-24 12:27:36 +03002129 return status;
2130
Felipe Balbi550a7372008-07-24 12:27:36 +03002131}
2132
2133/*-------------------------------------------------------------------------*/
2134
2135/* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
2136 * bridge to a platform device; this driver then suffices.
2137 */
2138
2139#ifndef CONFIG_MUSB_PIO_ONLY
2140static u64 *orig_dma_mask;
2141#endif
2142
2143static int __init musb_probe(struct platform_device *pdev)
2144{
2145 struct device *dev = &pdev->dev;
Hema Kalliguddifcf173e2010-09-29 11:26:39 -05002146 int irq = platform_get_irq_byname(pdev, "mc");
Felipe Balbida5108e2010-01-21 15:33:57 +02002147 int status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002148 struct resource *iomem;
2149 void __iomem *base;
2150
2151 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sergei Shtylyov541079d2010-12-10 21:03:29 +03002152 if (!iomem || irq <= 0)
Felipe Balbi550a7372008-07-24 12:27:36 +03002153 return -ENODEV;
2154
Felipe Balbi195e9e42009-12-15 11:08:42 +02002155 base = ioremap(iomem->start, resource_size(iomem));
Felipe Balbi550a7372008-07-24 12:27:36 +03002156 if (!base) {
2157 dev_err(dev, "ioremap failed\n");
2158 return -ENOMEM;
2159 }
2160
2161#ifndef CONFIG_MUSB_PIO_ONLY
2162 /* clobbered by use_dma=n */
2163 orig_dma_mask = dev->dma_mask;
2164#endif
Felipe Balbida5108e2010-01-21 15:33:57 +02002165 status = musb_init_controller(dev, irq, base);
2166 if (status < 0)
2167 iounmap(base);
2168
2169 return status;
Felipe Balbi550a7372008-07-24 12:27:36 +03002170}
2171
Felipe Balbie3060b12009-12-15 11:08:41 +02002172static int __exit musb_remove(struct platform_device *pdev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002173{
2174 struct musb *musb = dev_to_musb(&pdev->dev);
2175 void __iomem *ctrl_base = musb->ctrl_base;
2176
2177 /* this gets called on rmmod.
2178 * - Host mode: host may still be active
2179 * - Peripheral mode: peripheral is deactivated (or never-activated)
2180 * - OTG mode: both roles are deactivated (or never-activated)
2181 */
Hema HK7acc6192011-02-28 14:19:34 +05302182 pm_runtime_get_sync(musb->controller);
Felipe Balbi7f7f9e22010-03-12 10:29:11 +02002183 musb_exit_debugfs(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002184 musb_shutdown(pdev);
Sergei Shtylyov461972d2010-03-25 13:14:32 +02002185
Hema HK7acc6192011-02-28 14:19:34 +05302186 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03002187 musb_free(musb);
2188 iounmap(ctrl_base);
2189 device_init_wakeup(&pdev->dev, 0);
2190#ifndef CONFIG_MUSB_PIO_ONLY
2191 pdev->dev.dma_mask = orig_dma_mask;
2192#endif
2193 return 0;
2194}
2195
2196#ifdef CONFIG_PM
2197
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002198static void musb_save_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002199{
2200 int i;
2201 void __iomem *musb_base = musb->mregs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002202 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002203
2204 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002205 musb->context.frame = musb_readw(musb_base, MUSB_FRAME);
2206 musb->context.testmode = musb_readb(musb_base, MUSB_TESTMODE);
2207 musb->context.busctl = musb_read_ulpi_buscontrol(musb->mregs);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002208 }
Felipe Balbi74211072010-12-01 13:53:27 +02002209 musb->context.power = musb_readb(musb_base, MUSB_POWER);
2210 musb->context.intrtxe = musb_readw(musb_base, MUSB_INTRTXE);
2211 musb->context.intrrxe = musb_readw(musb_base, MUSB_INTRRXE);
2212 musb->context.intrusbe = musb_readb(musb_base, MUSB_INTRUSBE);
2213 musb->context.index = musb_readb(musb_base, MUSB_INDEX);
2214 musb->context.devctl = musb_readb(musb_base, MUSB_DEVCTL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002215
Bob Liuae9b2ad2010-09-24 13:44:07 +03002216 for (i = 0; i < musb->config->num_eps; ++i) {
2217 epio = musb->endpoints[i].regs;
Felipe Balbi74211072010-12-01 13:53:27 +02002218 musb->context.index_regs[i].txmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002219 musb_readw(epio, MUSB_TXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002220 musb->context.index_regs[i].txcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002221 musb_readw(epio, MUSB_TXCSR);
Felipe Balbi74211072010-12-01 13:53:27 +02002222 musb->context.index_regs[i].rxmaxp =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002223 musb_readw(epio, MUSB_RXMAXP);
Felipe Balbi74211072010-12-01 13:53:27 +02002224 musb->context.index_regs[i].rxcsr =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002225 musb_readw(epio, MUSB_RXCSR);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002226
2227 if (musb->dyn_fifo) {
Felipe Balbi74211072010-12-01 13:53:27 +02002228 musb->context.index_regs[i].txfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002229 musb_read_txfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002230 musb->context.index_regs[i].rxfifoadd =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002231 musb_read_rxfifoadd(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002232 musb->context.index_regs[i].txfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002233 musb_read_txfifosz(musb_base);
Felipe Balbi74211072010-12-01 13:53:27 +02002234 musb->context.index_regs[i].rxfifosz =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002235 musb_read_rxfifosz(musb_base);
2236 }
2237 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002238 musb->context.index_regs[i].txtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002239 musb_readb(epio, MUSB_TXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002240 musb->context.index_regs[i].txinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002241 musb_readb(epio, MUSB_TXINTERVAL);
Felipe Balbi74211072010-12-01 13:53:27 +02002242 musb->context.index_regs[i].rxtype =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002243 musb_readb(epio, MUSB_RXTYPE);
Felipe Balbi74211072010-12-01 13:53:27 +02002244 musb->context.index_regs[i].rxinterval =
Bob Liuae9b2ad2010-09-24 13:44:07 +03002245 musb_readb(epio, MUSB_RXINTERVAL);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002246
Felipe Balbi74211072010-12-01 13:53:27 +02002247 musb->context.index_regs[i].txfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002248 musb_read_txfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002249 musb->context.index_regs[i].txhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002250 musb_read_txhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002251 musb->context.index_regs[i].txhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002252 musb_read_txhubport(musb_base, i);
2253
Felipe Balbi74211072010-12-01 13:53:27 +02002254 musb->context.index_regs[i].rxfunaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002255 musb_read_rxfunaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002256 musb->context.index_regs[i].rxhubaddr =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002257 musb_read_rxhubaddr(musb_base, i);
Felipe Balbi74211072010-12-01 13:53:27 +02002258 musb->context.index_regs[i].rxhubport =
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002259 musb_read_rxhubport(musb_base, i);
2260 }
2261 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002262}
2263
Felipe Balbi3c8a5fc2010-12-02 12:28:39 +02002264static void musb_restore_context(struct musb *musb)
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002265{
2266 int i;
2267 void __iomem *musb_base = musb->mregs;
2268 void __iomem *ep_target_regs;
Bob Liuae9b2ad2010-09-24 13:44:07 +03002269 void __iomem *epio;
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002270
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002271 if (is_host_enabled(musb)) {
Felipe Balbi74211072010-12-01 13:53:27 +02002272 musb_writew(musb_base, MUSB_FRAME, musb->context.frame);
2273 musb_writeb(musb_base, MUSB_TESTMODE, musb->context.testmode);
2274 musb_write_ulpi_buscontrol(musb->mregs, musb->context.busctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002275 }
Felipe Balbi74211072010-12-01 13:53:27 +02002276 musb_writeb(musb_base, MUSB_POWER, musb->context.power);
2277 musb_writew(musb_base, MUSB_INTRTXE, musb->context.intrtxe);
2278 musb_writew(musb_base, MUSB_INTRRXE, musb->context.intrrxe);
2279 musb_writeb(musb_base, MUSB_INTRUSBE, musb->context.intrusbe);
2280 musb_writeb(musb_base, MUSB_DEVCTL, musb->context.devctl);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002281
Bob Liuae9b2ad2010-09-24 13:44:07 +03002282 for (i = 0; i < musb->config->num_eps; ++i) {
2283 epio = musb->endpoints[i].regs;
2284 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002285 musb->context.index_regs[i].txmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002286 musb_writew(epio, MUSB_TXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002287 musb->context.index_regs[i].txcsr);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002288 musb_writew(epio, MUSB_RXMAXP,
Felipe Balbi74211072010-12-01 13:53:27 +02002289 musb->context.index_regs[i].rxmaxp);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002290 musb_writew(epio, MUSB_RXCSR,
Felipe Balbi74211072010-12-01 13:53:27 +02002291 musb->context.index_regs[i].rxcsr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002292
2293 if (musb->dyn_fifo) {
2294 musb_write_txfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002295 musb->context.index_regs[i].txfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002296 musb_write_rxfifosz(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002297 musb->context.index_regs[i].rxfifosz);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002298 musb_write_txfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002299 musb->context.index_regs[i].txfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002300 musb_write_rxfifoadd(musb_base,
Felipe Balbi74211072010-12-01 13:53:27 +02002301 musb->context.index_regs[i].rxfifoadd);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002302 }
2303
2304 if (is_host_enabled(musb)) {
Bob Liuae9b2ad2010-09-24 13:44:07 +03002305 musb_writeb(epio, MUSB_TXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002306 musb->context.index_regs[i].txtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002307 musb_writeb(epio, MUSB_TXINTERVAL,
Felipe Balbi74211072010-12-01 13:53:27 +02002308 musb->context.index_regs[i].txinterval);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002309 musb_writeb(epio, MUSB_RXTYPE,
Felipe Balbi74211072010-12-01 13:53:27 +02002310 musb->context.index_regs[i].rxtype);
Bob Liuae9b2ad2010-09-24 13:44:07 +03002311 musb_writeb(epio, MUSB_RXINTERVAL,
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002312
Felipe Balbi74211072010-12-01 13:53:27 +02002313 musb->context.index_regs[i].rxinterval);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002314 musb_write_txfunaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002315 musb->context.index_regs[i].txfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002316 musb_write_txhubaddr(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002317 musb->context.index_regs[i].txhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002318 musb_write_txhubport(musb_base, i,
Felipe Balbi74211072010-12-01 13:53:27 +02002319 musb->context.index_regs[i].txhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002320
2321 ep_target_regs =
2322 musb_read_target_reg_base(i, musb_base);
2323
2324 musb_write_rxfunaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002325 musb->context.index_regs[i].rxfunaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002326 musb_write_rxhubaddr(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002327 musb->context.index_regs[i].rxhubaddr);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002328 musb_write_rxhubport(ep_target_regs,
Felipe Balbi74211072010-12-01 13:53:27 +02002329 musb->context.index_regs[i].rxhubport);
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002330 }
2331 }
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002332}
2333
Magnus Damm48fea962009-07-08 13:22:56 +02002334static int musb_suspend(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002335{
Magnus Damm48fea962009-07-08 13:22:56 +02002336 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002337 unsigned long flags;
2338 struct musb *musb = dev_to_musb(&pdev->dev);
2339
Felipe Balbi550a7372008-07-24 12:27:36 +03002340 spin_lock_irqsave(&musb->lock, flags);
2341
2342 if (is_peripheral_active(musb)) {
2343 /* FIXME force disconnect unless we know USB will wake
2344 * the system up quickly enough to respond ...
2345 */
2346 } else if (is_host_active(musb)) {
2347 /* we know all the children are suspended; sometimes
2348 * they will even be wakeup-enabled.
2349 */
2350 }
2351
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002352 musb_save_context(musb);
2353
Felipe Balbi550a7372008-07-24 12:27:36 +03002354 spin_unlock_irqrestore(&musb->lock, flags);
2355 return 0;
2356}
2357
Magnus Damm48fea962009-07-08 13:22:56 +02002358static int musb_resume_noirq(struct device *dev)
Felipe Balbi550a7372008-07-24 12:27:36 +03002359{
Magnus Damm48fea962009-07-08 13:22:56 +02002360 struct platform_device *pdev = to_platform_device(dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03002361 struct musb *musb = dev_to_musb(&pdev->dev);
2362
Ajay Kumar Gupta4f712e02010-01-21 15:33:52 +02002363 musb_restore_context(musb);
2364
Felipe Balbi550a7372008-07-24 12:27:36 +03002365 /* for static cmos like DaVinci, register values were preserved
Kim Kyuwon0ec8fd72009-03-26 18:56:51 -07002366 * unless for some reason the whole soc powered down or the USB
2367 * module got reset through the PSC (vs just being disabled).
Felipe Balbi550a7372008-07-24 12:27:36 +03002368 */
Felipe Balbi550a7372008-07-24 12:27:36 +03002369 return 0;
2370}
2371
Hema HK7acc6192011-02-28 14:19:34 +05302372static int musb_runtime_suspend(struct device *dev)
2373{
2374 struct musb *musb = dev_to_musb(dev);
2375
2376 musb_save_context(musb);
2377
2378 return 0;
2379}
2380
2381static int musb_runtime_resume(struct device *dev)
2382{
2383 struct musb *musb = dev_to_musb(dev);
2384 static int first = 1;
2385
2386 /*
2387 * When pm_runtime_get_sync called for the first time in driver
2388 * init, some of the structure is still not initialized which is
2389 * used in restore function. But clock needs to be
2390 * enabled before any register access, so
2391 * pm_runtime_get_sync has to be called.
2392 * Also context restore without save does not make
2393 * any sense
2394 */
2395 if (!first)
2396 musb_restore_context(musb);
2397 first = 0;
2398
2399 return 0;
2400}
2401
Alexey Dobriyan47145212009-12-14 18:00:08 -08002402static const struct dev_pm_ops musb_dev_pm_ops = {
Magnus Damm48fea962009-07-08 13:22:56 +02002403 .suspend = musb_suspend,
2404 .resume_noirq = musb_resume_noirq,
Hema HK7acc6192011-02-28 14:19:34 +05302405 .runtime_suspend = musb_runtime_suspend,
2406 .runtime_resume = musb_runtime_resume,
Magnus Damm48fea962009-07-08 13:22:56 +02002407};
2408
2409#define MUSB_DEV_PM_OPS (&musb_dev_pm_ops)
Felipe Balbi550a7372008-07-24 12:27:36 +03002410#else
Magnus Damm48fea962009-07-08 13:22:56 +02002411#define MUSB_DEV_PM_OPS NULL
Felipe Balbi550a7372008-07-24 12:27:36 +03002412#endif
2413
2414static struct platform_driver musb_driver = {
2415 .driver = {
2416 .name = (char *)musb_driver_name,
2417 .bus = &platform_bus_type,
2418 .owner = THIS_MODULE,
Magnus Damm48fea962009-07-08 13:22:56 +02002419 .pm = MUSB_DEV_PM_OPS,
Felipe Balbi550a7372008-07-24 12:27:36 +03002420 },
Felipe Balbie3060b12009-12-15 11:08:41 +02002421 .remove = __exit_p(musb_remove),
Felipe Balbi550a7372008-07-24 12:27:36 +03002422 .shutdown = musb_shutdown,
Felipe Balbi550a7372008-07-24 12:27:36 +03002423};
2424
2425/*-------------------------------------------------------------------------*/
2426
2427static int __init musb_init(void)
2428{
2429#ifdef CONFIG_USB_MUSB_HDRC_HCD
2430 if (usb_disabled())
2431 return 0;
2432#endif
2433
2434 pr_info("%s: version " MUSB_VERSION ", "
2435#ifdef CONFIG_MUSB_PIO_ONLY
2436 "pio"
2437#elif defined(CONFIG_USB_TI_CPPI_DMA)
2438 "cppi-dma"
2439#elif defined(CONFIG_USB_INVENTRA_DMA)
2440 "musb-dma"
2441#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
2442 "tusb-omap-dma"
Mian Yousaf Kaukabe3c496f2011-03-22 15:55:57 +01002443#elif defined(CONFIG_USB_UX500_DMA)
2444 "ux500-dma"
Felipe Balbi550a7372008-07-24 12:27:36 +03002445#else
2446 "?dma?"
2447#endif
2448 ", "
2449#ifdef CONFIG_USB_MUSB_OTG
2450 "otg (peripheral+host)"
2451#elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
2452 "peripheral"
2453#elif defined(CONFIG_USB_MUSB_HDRC_HCD)
2454 "host"
2455#endif
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002456 ,
2457 musb_driver_name);
Felipe Balbi550a7372008-07-24 12:27:36 +03002458 return platform_driver_probe(&musb_driver, musb_probe);
2459}
2460
David Brownell34f32c92009-02-20 13:45:17 -08002461/* make us init after usbcore and i2c (transceivers, regulators, etc)
2462 * and before usb gadget and host-side drivers start to register
Felipe Balbi550a7372008-07-24 12:27:36 +03002463 */
David Brownell34f32c92009-02-20 13:45:17 -08002464fs_initcall(musb_init);
Felipe Balbi550a7372008-07-24 12:27:36 +03002465
2466static void __exit musb_cleanup(void)
2467{
2468 platform_driver_unregister(&musb_driver);
2469}
2470module_exit(musb_cleanup);