blob: 50e8490867ed7a077dc8bea7a091f046beddef0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
3 *
4 * Copyright (C) 2004 Texas Instruments, Inc.
5 * Copyright (C) 2004-2005 David Brownell
6 *
Kyungmin Park527ea732007-11-21 15:13:15 -08007 * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#undef DEBUG
16#undef VERBOSE
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/ioport.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/timer.h>
27#include <linux/list.h>
28#include <linux/interrupt.h>
29#include <linux/proc_fs.h>
30#include <linux/mm.h>
31#include <linux/moduleparam.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
David Brownell5f848132006-12-16 15:34:53 -080033#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070034#include <linux/usb/gadget.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070035#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/dma-mapping.h>
David Brownelle6a6e472006-12-10 11:47:04 -080037#include <linux/clk.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070038#include <linux/prefetch.h>
Felipe Balbi80dd1352012-05-29 14:26:09 +030039#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44#include <asm/mach-types.h>
45
Tony Lindgrence491cf2009-10-20 09:40:47 -070046#include <plat/dma.h>
47#include <plat/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "omap_udc.h"
50
51#undef USB_TRACE
52
53/* bulk DMA seems to be behaving for both IN and OUT */
54#define USE_DMA
55
56/* ISO too */
57#define USE_ISO
58
59#define DRIVER_DESC "OMAP UDC driver"
60#define DRIVER_VERSION "4 October 2004"
61
62#define DMA_ADDR_INVALID (~(dma_addr_t)0)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
66 * D+ pullup to allow enumeration. That's too early for the gadget
67 * framework to use from usb_endpoint_enable(), which happens after
68 * enumeration as part of activating an interface. (But if we add an
69 * optional new "UDC not yet running" state to the gadget driver model,
70 * even just during driver binding, the endpoint autoconfig logic is the
71 * natural spot to manufacture new endpoints.)
72 *
73 * So instead of using endpoint enable calls to control the hardware setup,
74 * this driver defines a "fifo mode" parameter. It's used during driver
75 * initialization to choose among a set of pre-defined endpoint configs.
76 * See omap_udc_setup() for available modes, or to add others. That code
77 * lives in an init section, so use this driver as a module if you need
78 * to change the fifo mode after the kernel boots.
79 *
80 * Gadget drivers normally ignore endpoints they don't care about, and
81 * won't include them in configuration descriptors. That means only
82 * misbehaving hosts would even notice they exist.
83 */
84#ifdef USE_ISO
85static unsigned fifo_mode = 3;
86#else
Felipe Balbi80dd1352012-05-29 14:26:09 +030087static unsigned fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89
90/* "modprobe omap_udc fifo_mode=42", or else as a kernel
91 * boot parameter "omap_udc:fifo_mode=42"
92 */
Felipe Balbi80dd1352012-05-29 14:26:09 +030093module_param(fifo_mode, uint, 0);
94MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#ifdef USE_DMA
Rusty Russell90ab5ee2012-01-13 09:32:20 +103097static bool use_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* "modprobe omap_udc use_dma=y", or else as a kernel
100 * boot parameter "omap_udc:use_dma=y"
101 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300102module_param(use_dma, bool, 0);
103MODULE_PARM_DESC(use_dma, "enable/disable DMA");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#else /* !USE_DMA */
105
106/* save a bit of code */
107#define use_dma 0
108#endif /* !USE_DMA */
109
110
Felipe Balbi80dd1352012-05-29 14:26:09 +0300111static const char driver_name[] = "omap_udc";
112static const char driver_desc[] = DRIVER_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*-------------------------------------------------------------------------*/
115
116/* there's a notion of "current endpoint" for modifying endpoint
David Brownelle6a6e472006-12-10 11:47:04 -0800117 * state, and PIO access to its FIFO.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
120static void use_ep(struct omap_ep *ep, u16 select)
121{
122 u16 num = ep->bEndpointAddress & 0x0f;
123
124 if (ep->bEndpointAddress & USB_DIR_IN)
125 num |= UDC_EP_DIR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300126 omap_writew(num | select, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* when select, MUST deselect later !! */
128}
129
130static inline void deselect_ep(void)
131{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300132 u16 w;
133
134 w = omap_readw(UDC_EP_NUM);
135 w &= ~UDC_EP_SEL;
136 omap_writew(w, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* 6 wait states before TX will happen */
138}
139
140static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
141
142/*-------------------------------------------------------------------------*/
143
144static int omap_ep_enable(struct usb_ep *_ep,
145 const struct usb_endpoint_descriptor *desc)
146{
147 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
148 struct omap_udc *udc;
149 unsigned long flags;
150 u16 maxp;
151
152 /* catch various bogus parameters */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200153 if (!_ep || !desc || ep->ep.desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 || desc->bDescriptorType != USB_DT_ENDPOINT
155 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700156 || ep->maxpacket < usb_endpoint_maxp(desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800157 DBG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
159 }
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700160 maxp = usb_endpoint_maxp(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
162 && maxp != ep->maxpacket)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700163 || usb_endpoint_maxp(desc) > ep->maxpacket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800165 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ERANGE;
167 }
168
169#ifdef USE_ISO
170 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
171 && desc->bInterval != 1)) {
172 /* hardware wants period = 1; USB allows 2^(Interval-1) */
173 DBG("%s, unsupported ISO period %dms\n", _ep->name,
174 1 << (desc->bInterval - 1));
175 return -EDOM;
176 }
177#else
178 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
179 DBG("%s, ISO nyet\n", _ep->name);
180 return -EDOM;
181 }
182#endif
183
184 /* xfer types must match, except that interrupt ~= bulk */
185 if (ep->bmAttributes != desc->bmAttributes
186 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
187 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800188 DBG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190 }
191
192 udc = ep->udc;
193 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800194 DBG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -ESHUTDOWN;
196 }
197
198 spin_lock_irqsave(&udc->lock, flags);
199
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200200 ep->ep.desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ep->irqs = 0;
202 ep->stopped = 0;
203 ep->ep.maxpacket = maxp;
204
205 /* set endpoint to initial state */
206 ep->dma_channel = 0;
207 ep->has_dma = 0;
208 ep->lch = -1;
209 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300210 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ep->ackwait = 0;
212 deselect_ep();
213
214 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
215 list_add(&ep->iso, &udc->iso);
216
217 /* maybe assign a DMA channel to this endpoint */
218 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
219 /* FIXME ISO can dma, but prefers first channel */
220 dma_channel_claim(ep, 0);
221
222 /* PIO OUT may RX packets */
223 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
224 && !ep->has_dma
225 && !(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300226 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ep->ackwait = 1 + ep->double_buf;
228 }
229
230 spin_unlock_irqrestore(&udc->lock, flags);
231 VDBG("%s enabled\n", _ep->name);
232 return 0;
233}
234
235static void nuke(struct omap_ep *, int status);
236
237static int omap_ep_disable(struct usb_ep *_ep)
238{
239 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
240 unsigned long flags;
241
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200242 if (!_ep || !ep->ep.desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800243 DBG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 _ep ? ep->ep.name : NULL);
245 return -EINVAL;
246 }
247
248 spin_lock_irqsave(&ep->udc->lock, flags);
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200249 ep->ep.desc = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +0300250 nuke(ep, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ep->ep.maxpacket = ep->maxpacket;
252 ep->has_dma = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300253 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 list_del_init(&ep->iso);
255 del_timer(&ep->timer);
256
257 spin_unlock_irqrestore(&ep->udc->lock, flags);
258
259 VDBG("%s disabled\n", _ep->name);
260 return 0;
261}
262
263/*-------------------------------------------------------------------------*/
264
265static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400266omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct omap_req *req;
269
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800270 req = kzalloc(sizeof(*req), gfp_flags);
Felipe Balbi70617db2012-05-29 14:36:42 +0300271 if (!req)
272 return NULL;
273
274 req->req.dma = DMA_ADDR_INVALID;
275 INIT_LIST_HEAD(&req->queue);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return &req->req;
278}
279
280static void
281omap_free_request(struct usb_ep *ep, struct usb_request *_req)
282{
283 struct omap_req *req = container_of(_req, struct omap_req, req);
284
Felipe Balbi23673d72012-05-29 14:38:32 +0300285 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*-------------------------------------------------------------------------*/
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static void
291done(struct omap_ep *ep, struct omap_req *req, int status)
292{
293 unsigned stopped = ep->stopped;
294
295 list_del_init(&req->queue);
296
297 if (req->req.status == -EINPROGRESS)
298 req->req.status = status;
299 else
300 status = req->req.status;
301
302 if (use_dma && ep->has_dma) {
303 if (req->mapped) {
304 dma_unmap_single(ep->udc->gadget.dev.parent,
305 req->req.dma, req->req.length,
306 (ep->bEndpointAddress & USB_DIR_IN)
307 ? DMA_TO_DEVICE
308 : DMA_FROM_DEVICE);
309 req->req.dma = DMA_ADDR_INVALID;
310 req->mapped = 0;
311 } else
312 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
313 req->req.dma, req->req.length,
314 (ep->bEndpointAddress & USB_DIR_IN)
315 ? DMA_TO_DEVICE
316 : DMA_FROM_DEVICE);
317 }
318
319#ifndef USB_TRACE
320 if (status && status != -ESHUTDOWN)
321#endif
322 VDBG("complete %s req %p stat %d len %u/%u\n",
323 ep->ep.name, &req->req, status,
324 req->req.actual, req->req.length);
325
326 /* don't modify queue heads during completion callback */
327 ep->stopped = 1;
328 spin_unlock(&ep->udc->lock);
329 req->req.complete(&ep->ep, &req->req);
330 spin_lock(&ep->udc->lock);
331 ep->stopped = stopped;
332}
333
334/*-------------------------------------------------------------------------*/
335
David Brownell313980c2005-04-11 15:38:25 -0700336#define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
337#define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339#define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
340#define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
341
David Brownelle6a6e472006-12-10 11:47:04 -0800342static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343write_packet(u8 *buf, struct omap_req *req, unsigned max)
344{
345 unsigned len;
346 u16 *wp;
347
348 len = min(req->req.length - req->req.actual, max);
349 req->req.actual += len;
350
351 max = len;
352 if (likely((((int)buf) & 1) == 0)) {
353 wp = (u16 *)buf;
354 while (max >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300355 omap_writew(*wp++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 max -= 2;
357 }
358 buf = (u8 *)wp;
359 }
360 while (max--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300361 omap_writeb(*buf++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return len;
363}
364
Felipe Balbi80dd1352012-05-29 14:26:09 +0300365/* FIXME change r/w fifo calling convention */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367
Felipe Balbi80dd1352012-05-29 14:26:09 +0300368/* return: 0 = still running, 1 = completed, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int write_fifo(struct omap_ep *ep, struct omap_req *req)
370{
371 u8 *buf;
372 unsigned count;
373 int is_last;
374 u16 ep_stat;
375
376 buf = req->req.buf + req->req.actual;
377 prefetch(buf);
378
379 /* PIO-IN isn't double buffered except for iso */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300380 ep_stat = omap_readw(UDC_STAT_FLG);
David Brownell313980c2005-04-11 15:38:25 -0700381 if (ep_stat & UDC_FIFO_UNWRITABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383
384 count = ep->ep.maxpacket;
385 count = write_packet(buf, req, count);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300386 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 ep->ackwait = 1;
388
389 /* last packet is often short (sometimes a zlp) */
390 if (count != ep->ep.maxpacket)
391 is_last = 1;
392 else if (req->req.length == req->req.actual
393 && !req->req.zero)
394 is_last = 1;
395 else
396 is_last = 0;
397
398 /* NOTE: requests complete when all IN data is in a
399 * FIFO (or sometimes later, if a zlp was needed).
400 * Use usb_ep_fifo_status() where needed.
401 */
402 if (is_last)
403 done(ep, req, 0);
404 return is_last;
405}
406
David Brownelle6a6e472006-12-10 11:47:04 -0800407static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408read_packet(u8 *buf, struct omap_req *req, unsigned avail)
409{
410 unsigned len;
411 u16 *wp;
412
413 len = min(req->req.length - req->req.actual, avail);
414 req->req.actual += len;
415 avail = len;
416
417 if (likely((((int)buf) & 1) == 0)) {
418 wp = (u16 *)buf;
419 while (avail >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300420 *wp++ = omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 avail -= 2;
422 }
423 buf = (u8 *)wp;
424 }
425 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300426 *buf++ = omap_readb(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return len;
428}
429
Felipe Balbi80dd1352012-05-29 14:26:09 +0300430/* return: 0 = still running, 1 = queue empty, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static int read_fifo(struct omap_ep *ep, struct omap_req *req)
432{
433 u8 *buf;
434 unsigned count, avail;
435 int is_last;
436
437 buf = req->req.buf + req->req.actual;
438 prefetchw(buf);
439
440 for (;;) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300441 u16 ep_stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 is_last = 0;
444 if (ep_stat & FIFO_EMPTY) {
445 if (!ep->double_buf)
446 break;
447 ep->fnf = 1;
448 }
449 if (ep_stat & UDC_EP_HALTED)
450 break;
451
David Brownell313980c2005-04-11 15:38:25 -0700452 if (ep_stat & UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 avail = ep->ep.maxpacket;
454 else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300455 avail = omap_readw(UDC_RXFSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ep->fnf = ep->double_buf;
457 }
458 count = read_packet(buf, req, avail);
459
460 /* partial packet reads may not be errors */
461 if (count < ep->ep.maxpacket) {
462 is_last = 1;
463 /* overflowed this request? flush extra data */
464 if (count != avail) {
465 req->req.status = -EOVERFLOW;
466 avail -= count;
467 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300468 omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 } else if (req->req.length == req->req.actual)
471 is_last = 1;
472 else
473 is_last = 0;
474
475 if (!ep->bEndpointAddress)
476 break;
477 if (is_last)
478 done(ep, req, 0);
479 break;
480 }
481 return is_last;
482}
483
484/*-------------------------------------------------------------------------*/
485
486static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
487{
488 dma_addr_t end;
489
490 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
491 * the last transfer's bytecount by more than a FIFO's worth.
492 */
493 if (cpu_is_omap15xx())
494 return 0;
495
Tony Lindgren0499bde2008-07-03 12:24:36 +0300496 end = omap_get_dma_src_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (end == ep->dma_counter)
498 return 0;
499
500 end |= start & (0xffff << 16);
501 if (end < start)
502 end += 0x10000;
503 return end - start;
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
507{
508 dma_addr_t end;
509
Tony Lindgren0499bde2008-07-03 12:24:36 +0300510 end = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (end == ep->dma_counter)
512 return 0;
513
514 end |= start & (0xffff << 16);
515 if (cpu_is_omap15xx())
516 end++;
517 if (end < start)
518 end += 0x10000;
519 return end - start;
520}
521
522
523/* Each USB transfer request using DMA maps to one or more DMA transfers.
524 * When DMA completion isn't request completion, the UDC continues with
525 * the next DMA transfer for that USB transfer.
526 */
527
528static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
529{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300530 u16 txdma_ctrl, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 unsigned length = req->req.length - req->req.actual;
532 const int sync_mode = cpu_is_omap15xx()
533 ? OMAP_DMA_SYNC_FRAME
534 : OMAP_DMA_SYNC_ELEMENT;
Kyungmin Park527ea732007-11-21 15:13:15 -0800535 int dma_trigger = 0;
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* measure length in either bytes or packets */
David Brownell65111082005-04-28 13:52:31 -0700538 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
540 txdma_ctrl = UDC_TXN_EOT | length;
541 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
Kyungmin Park527ea732007-11-21 15:13:15 -0800542 length, 1, sync_mode, dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 } else {
544 length = min(length / ep->maxpacket,
545 (unsigned) UDC_TXN_TSC + 1);
David Brownelle6a6e472006-12-10 11:47:04 -0800546 txdma_ctrl = length;
David Brownell65111082005-04-28 13:52:31 -0700547 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
David Brownelle6a6e472006-12-10 11:47:04 -0800548 ep->ep.maxpacket >> 1, length, sync_mode,
Kyungmin Park527ea732007-11-21 15:13:15 -0800549 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 length *= ep->maxpacket;
551 }
552 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800553 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
554 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 omap_start_dma(ep->lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300557 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300558 w = omap_readw(UDC_DMA_IRQ_EN);
559 w |= UDC_TX_DONE_IE(ep->dma_channel);
560 omap_writew(w, UDC_DMA_IRQ_EN);
561 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 req->dma_bytes = length;
563}
564
565static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
566{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300567 u16 w;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (status == 0) {
570 req->req.actual += req->dma_bytes;
571
572 /* return if this request needs to send data or zlp */
573 if (req->req.actual < req->req.length)
574 return;
575 if (req->req.zero
576 && req->dma_bytes != 0
577 && (req->req.actual % ep->maxpacket) == 0)
578 return;
579 } else
580 req->req.actual += dma_src_len(ep, req->req.dma
581 + req->req.actual);
582
583 /* tx completion */
584 omap_stop_dma(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300585 w = omap_readw(UDC_DMA_IRQ_EN);
586 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
587 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 done(ep, req, status);
589}
590
591static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
592{
Kyungmin Park527ea732007-11-21 15:13:15 -0800593 unsigned packets = req->req.length - req->req.actual;
594 int dma_trigger = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300595 u16 w;
Kyungmin Park527ea732007-11-21 15:13:15 -0800596
Tony Lindgrenae372572012-05-21 12:01:11 -0700597 /* set up this DMA transfer, enable the fifo, start */
598 packets /= ep->ep.maxpacket;
599 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
600 req->dma_bytes = packets * ep->ep.maxpacket;
601 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
602 ep->ep.maxpacket >> 1, packets,
603 OMAP_DMA_SYNC_ELEMENT,
604 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800606 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
607 0, 0);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300608 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300610 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
611 w = omap_readw(UDC_DMA_IRQ_EN);
612 w |= UDC_RX_EOT_IE(ep->dma_channel);
613 omap_writew(w, UDC_DMA_IRQ_EN);
614 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
615 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 omap_start_dma(ep->lch);
618}
619
620static void
David Brownellcb97c5c2005-10-16 15:06:51 -0700621finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300623 u16 count, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (status == 0)
626 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
627 count = dma_dest_len(ep, req->req.dma + req->req.actual);
628 count += req->req.actual;
David Brownellcb97c5c2005-10-16 15:06:51 -0700629 if (one)
630 count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (count <= req->req.length)
632 req->req.actual = count;
633
634 if (count != req->dma_bytes || status)
635 omap_stop_dma(ep->lch);
636
637 /* if this wasn't short, request may need another transfer */
638 else if (req->req.actual < req->req.length)
639 return;
640
641 /* rx completion */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300642 w = omap_readw(UDC_DMA_IRQ_EN);
643 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
644 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 done(ep, req, status);
646}
647
648static void dma_irq(struct omap_udc *udc, u16 irq_src)
649{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300650 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct omap_ep *ep;
652 struct omap_req *req;
653
654 /* IN dma: tx to host */
655 if (irq_src & UDC_TXN_DONE) {
656 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
657 ep->irqs++;
658 /* can see TXN_DONE after dma abort */
659 if (!list_empty(&ep->queue)) {
660 req = container_of(ep->queue.next,
661 struct omap_req, queue);
662 finish_in_dma(ep, req, 0);
663 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300664 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Felipe Balbi80dd1352012-05-29 14:26:09 +0300666 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 req = container_of(ep->queue.next,
668 struct omap_req, queue);
669 next_in_dma(ep, req);
670 }
671 }
672
673 /* OUT dma: rx from host */
674 if (irq_src & UDC_RXN_EOT) {
675 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
676 ep->irqs++;
677 /* can see RXN_EOT after dma abort */
678 if (!list_empty(&ep->queue)) {
679 req = container_of(ep->queue.next,
680 struct omap_req, queue);
David Brownellcb97c5c2005-10-16 15:06:51 -0700681 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300683 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Felipe Balbi80dd1352012-05-29 14:26:09 +0300685 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 req = container_of(ep->queue.next,
687 struct omap_req, queue);
688 next_out_dma(ep, req);
689 }
690 }
691
692 if (irq_src & UDC_RXN_CNT) {
693 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
694 ep->irqs++;
695 /* omap15xx does this unasked... */
696 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300697 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699}
700
701static void dma_error(int lch, u16 ch_status, void *data)
702{
703 struct omap_ep *ep = data;
704
705 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700706 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
708
709 /* complete current transfer ... */
710}
711
712static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
713{
714 u16 reg;
715 int status, restart, is_in;
Kyungmin Park527ea732007-11-21 15:13:15 -0800716 int dma_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 is_in = ep->bEndpointAddress & USB_DIR_IN;
719 if (is_in)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300720 reg = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300722 reg = omap_readw(UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700723 reg |= UDC_DMA_REQ; /* "pulse" activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 ep->dma_channel = 0;
726 ep->lch = -1;
727 if (channel == 0 || channel > 3) {
728 if ((reg & 0x0f00) == 0)
729 channel = 3;
730 else if ((reg & 0x00f0) == 0)
731 channel = 2;
732 else if ((reg & 0x000f) == 0) /* preferred for ISO */
733 channel = 1;
734 else {
735 status = -EMLINK;
736 goto just_restart;
737 }
738 }
739 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
740 ep->dma_channel = channel;
741
742 if (is_in) {
Tony Lindgrenae372572012-05-21 12:01:11 -0700743 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800744 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ep->ep.name, dma_error, ep, &ep->lch);
746 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300747 omap_writew(reg, UDC_TXDMA_CFG);
Kyungmin Park527ea732007-11-21 15:13:15 -0800748 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700749 omap_set_dma_src_burst_mode(ep->lch,
750 OMAP_DMA_DATA_BURST_4);
751 omap_set_dma_src_data_pack(ep->lch, 1);
752 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 omap_set_dma_dest_params(ep->lch,
754 OMAP_DMA_PORT_TIPB,
755 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700756 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800757 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 } else {
Tony Lindgrenae372572012-05-21 12:01:11 -0700760 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800761 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ep->ep.name, dma_error, ep, &ep->lch);
763 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300764 omap_writew(reg, UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700765 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 omap_set_dma_src_params(ep->lch,
767 OMAP_DMA_PORT_TIPB,
768 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700769 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800770 0, 0);
Kyungmin Park527ea732007-11-21 15:13:15 -0800771 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700772 omap_set_dma_dest_burst_mode(ep->lch,
773 OMAP_DMA_DATA_BURST_4);
774 omap_set_dma_dest_data_pack(ep->lch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 }
777 if (status)
778 ep->dma_channel = 0;
779 else {
780 ep->has_dma = 1;
781 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
782
783 /* channel type P: hw synch (fifo) */
Tony Lindgrenae372572012-05-21 12:01:11 -0700784 if (!cpu_is_omap15xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300785 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788just_restart:
789 /* restart any queue, even if the claim failed */
790 restart = !ep->stopped && !list_empty(&ep->queue);
791
792 if (status)
793 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
794 restart ? " (restart)" : "");
795 else
796 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
797 is_in ? 't' : 'r',
798 ep->dma_channel - 1, ep->lch,
799 restart ? " (restart)" : "");
800
801 if (restart) {
802 struct omap_req *req;
803 req = container_of(ep->queue.next, struct omap_req, queue);
804 if (ep->has_dma)
805 (is_in ? next_in_dma : next_out_dma)(ep, req);
806 else {
807 use_ep(ep, UDC_EP_SEL);
808 (is_in ? write_fifo : read_fifo)(ep, req);
809 deselect_ep();
810 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300811 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 ep->ackwait = 1 + ep->double_buf;
813 }
814 /* IN: 6 wait states before it'll tx */
815 }
816 }
817}
818
819static void dma_channel_release(struct omap_ep *ep)
820{
821 int shift = 4 * (ep->dma_channel - 1);
822 u16 mask = 0x0f << shift;
823 struct omap_req *req;
824 int active;
825
826 /* abort any active usb transfer request */
827 if (!list_empty(&ep->queue))
828 req = container_of(ep->queue.next, struct omap_req, queue);
829 else
David Brownell313980c2005-04-11 15:38:25 -0700830 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Tony Lindgren0499bde2008-07-03 12:24:36 +0300832 active = omap_get_dma_active_status(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
835 active ? "active" : "idle",
836 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
837 ep->dma_channel - 1, req);
838
David Brownell65111082005-04-28 13:52:31 -0700839 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
840 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
841 */
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /* wait till current packet DMA finishes, and fifo empties */
844 if (ep->bEndpointAddress & USB_DIR_IN) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300845 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
846 UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (req) {
849 finish_in_dma(ep, req, -ECONNRESET);
850
851 /* clear FIFO; hosts probably won't empty it */
852 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300853 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 deselect_ep();
855 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300856 while (omap_readw(UDC_TXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 udelay(10);
858 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300859 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
860 UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* dma empties the fifo */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300863 while (omap_readw(UDC_RXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 udelay(10);
865 if (req)
David Brownellcb97c5c2005-10-16 15:06:51 -0700866 finish_out_dma(ep, req, -ECONNRESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868 omap_free_dma(ep->lch);
869 ep->dma_channel = 0;
870 ep->lch = -1;
871 /* has_dma still set, till endpoint is fully quiesced */
872}
873
874
875/*-------------------------------------------------------------------------*/
876
877static int
Al Viro55016f12005-10-21 03:21:58 -0400878omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
881 struct omap_req *req = container_of(_req, struct omap_req, req);
882 struct omap_udc *udc;
883 unsigned long flags;
884 int is_iso = 0;
885
886 /* catch various bogus parameters */
887 if (!_req || !req->req.complete || !req->req.buf
888 || !list_empty(&req->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800889 DBG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return -EINVAL;
891 }
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200892 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800893 DBG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return -EINVAL;
895 }
896 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
897 if (req->req.length > ep->ep.maxpacket)
898 return -EMSGSIZE;
899 is_iso = 1;
900 }
901
902 /* this isn't bogus, but OMAP DMA isn't the only hardware to
903 * have a hard time with partial packet reads... reject it.
904 */
905 if (use_dma
906 && ep->has_dma
907 && ep->bEndpointAddress != 0
908 && (ep->bEndpointAddress & USB_DIR_IN) == 0
909 && (req->req.length % ep->ep.maxpacket) != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800910 DBG("%s, no partial packet OUT reads\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return -EMSGSIZE;
912 }
913
914 udc = ep->udc;
915 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
916 return -ESHUTDOWN;
917
918 if (use_dma && ep->has_dma) {
919 if (req->req.dma == DMA_ADDR_INVALID) {
920 req->req.dma = dma_map_single(
921 ep->udc->gadget.dev.parent,
922 req->req.buf,
923 req->req.length,
924 (ep->bEndpointAddress & USB_DIR_IN)
925 ? DMA_TO_DEVICE
926 : DMA_FROM_DEVICE);
927 req->mapped = 1;
928 } else {
929 dma_sync_single_for_device(
930 ep->udc->gadget.dev.parent,
931 req->req.dma, req->req.length,
932 (ep->bEndpointAddress & USB_DIR_IN)
933 ? DMA_TO_DEVICE
934 : DMA_FROM_DEVICE);
935 req->mapped = 0;
936 }
937 }
938
939 VDBG("%s queue req %p, len %d buf %p\n",
940 ep->ep.name, _req, _req->length, _req->buf);
941
942 spin_lock_irqsave(&udc->lock, flags);
943
944 req->req.status = -EINPROGRESS;
945 req->req.actual = 0;
946
947 /* maybe kickstart non-iso i/o queues */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300948 if (is_iso) {
949 u16 w;
950
951 w = omap_readw(UDC_IRQ_EN);
952 w |= UDC_SOF_IE;
953 omap_writew(w, UDC_IRQ_EN);
954 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 int is_in;
956
957 if (ep->bEndpointAddress == 0) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300958 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 spin_unlock_irqrestore(&udc->lock, flags);
960 return -EL2HLT;
961 }
962
963 /* empty DATA stage? */
964 is_in = udc->ep0_in;
965 if (!req->req.length) {
966
967 /* chip became CONFIGURED or ADDRESSED
968 * earlier; drivers may already have queued
969 * requests to non-control endpoints
970 */
971 if (udc->ep0_set_config) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300972 u16 irq_en = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
975 if (!udc->ep0_reset_config)
976 irq_en |= UDC_EPN_RX_IE
977 | UDC_EPN_TX_IE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300978 omap_writew(irq_en, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
David Brownell313980c2005-04-11 15:38:25 -0700981 /* STATUS for zero length DATA stages is
982 * always an IN ... even for IN transfers,
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800983 * a weird case which seem to stall OMAP.
David Brownell313980c2005-04-11 15:38:25 -0700984 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300985 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
986 UDC_EP_NUM);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300987 omap_writew(UDC_CLR_EP, UDC_CTRL);
988 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
989 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /* cleanup */
992 udc->ep0_pending = 0;
993 done(ep, req, 0);
David Brownell313980c2005-04-11 15:38:25 -0700994 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /* non-empty DATA stage */
997 } else if (is_in) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300998 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
999 UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 } else {
1001 if (udc->ep0_setup)
1002 goto irq_wait;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001003 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 } else {
1006 is_in = ep->bEndpointAddress & USB_DIR_IN;
1007 if (!ep->has_dma)
1008 use_ep(ep, UDC_EP_SEL);
1009 /* if ISO: SOF IRQs must be enabled/disabled! */
1010 }
1011
1012 if (ep->has_dma)
1013 (is_in ? next_in_dma : next_out_dma)(ep, req);
1014 else if (req) {
1015 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
David Brownell313980c2005-04-11 15:38:25 -07001016 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 deselect_ep();
1018 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001019 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 ep->ackwait = 1 + ep->double_buf;
1021 }
1022 /* IN: 6 wait states before it'll tx */
1023 }
1024 }
1025
1026irq_wait:
1027 /* irq handler advances the queue */
David Brownell313980c2005-04-11 15:38:25 -07001028 if (req != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 list_add_tail(&req->queue, &ep->queue);
1030 spin_unlock_irqrestore(&udc->lock, flags);
1031
1032 return 0;
1033}
1034
1035static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1036{
1037 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1038 struct omap_req *req;
1039 unsigned long flags;
1040
1041 if (!_ep || !_req)
1042 return -EINVAL;
1043
1044 spin_lock_irqsave(&ep->udc->lock, flags);
1045
1046 /* make sure it's actually queued on this endpoint */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001047 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (&req->req == _req)
1049 break;
1050 }
1051 if (&req->req != _req) {
1052 spin_unlock_irqrestore(&ep->udc->lock, flags);
1053 return -EINVAL;
1054 }
1055
1056 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1057 int channel = ep->dma_channel;
1058
1059 /* releasing the channel cancels the request,
1060 * reclaiming the channel restarts the queue
1061 */
1062 dma_channel_release(ep);
1063 dma_channel_claim(ep, channel);
David Brownelle6a6e472006-12-10 11:47:04 -08001064 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 done(ep, req, -ECONNRESET);
1066 spin_unlock_irqrestore(&ep->udc->lock, flags);
1067 return 0;
1068}
1069
1070/*-------------------------------------------------------------------------*/
1071
1072static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1073{
1074 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1075 unsigned long flags;
1076 int status = -EOPNOTSUPP;
1077
1078 spin_lock_irqsave(&ep->udc->lock, flags);
1079
1080 /* just use protocol stalls for ep0; real halts are annoying */
1081 if (ep->bEndpointAddress == 0) {
1082 if (!ep->udc->ep0_pending)
1083 status = -EINVAL;
1084 else if (value) {
1085 if (ep->udc->ep0_set_config) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001086 WARNING("error changing config?\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001087 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001089 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 ep->udc->ep0_pending = 0;
1091 status = 0;
1092 } else /* NOP */
1093 status = 0;
1094
1095 /* otherwise, all active non-ISO endpoints can halt */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001096 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /* IN endpoints must already be idle */
1099 if ((ep->bEndpointAddress & USB_DIR_IN)
David Brownelle6a6e472006-12-10 11:47:04 -08001100 && !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 status = -EAGAIN;
1102 goto done;
1103 }
1104
1105 if (value) {
1106 int channel;
1107
1108 if (use_dma && ep->dma_channel
1109 && !list_empty(&ep->queue)) {
1110 channel = ep->dma_channel;
1111 dma_channel_release(ep);
1112 } else
1113 channel = 0;
1114
1115 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001116 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1117 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 status = 0;
1119 } else
1120 status = -EAGAIN;
1121 deselect_ep();
1122
1123 if (channel)
1124 dma_channel_claim(ep, channel);
1125 } else {
1126 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001127 omap_writew(ep->udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ep->ackwait = 0;
1129 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001130 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ep->ackwait = 1 + ep->double_buf;
1132 }
1133 }
1134 }
1135done:
1136 VDBG("%s %s halt stat %d\n", ep->ep.name,
1137 value ? "set" : "clear", status);
1138
1139 spin_unlock_irqrestore(&ep->udc->lock, flags);
1140 return status;
1141}
1142
1143static struct usb_ep_ops omap_ep_ops = {
1144 .enable = omap_ep_enable,
1145 .disable = omap_ep_disable,
1146
1147 .alloc_request = omap_alloc_request,
1148 .free_request = omap_free_request,
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 .queue = omap_ep_queue,
1151 .dequeue = omap_ep_dequeue,
1152
1153 .set_halt = omap_ep_set_halt,
Felipe Balbi80dd1352012-05-29 14:26:09 +03001154 /* fifo_status ... report bytes in fifo */
1155 /* fifo_flush ... flush fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156};
1157
1158/*-------------------------------------------------------------------------*/
1159
1160static int omap_get_frame(struct usb_gadget *gadget)
1161{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001162 u16 sof = omap_readw(UDC_SOF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1164}
1165
1166static int omap_wakeup(struct usb_gadget *gadget)
1167{
1168 struct omap_udc *udc;
1169 unsigned long flags;
1170 int retval = -EHOSTUNREACH;
1171
1172 udc = container_of(gadget, struct omap_udc, gadget);
1173
1174 spin_lock_irqsave(&udc->lock, flags);
1175 if (udc->devstat & UDC_SUS) {
1176 /* NOTE: OTG spec erratum says that OTG devices may
1177 * issue wakeups without host enable.
1178 */
1179 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1180 DBG("remote wakeup...\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001181 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 retval = 0;
1183 }
1184
1185 /* NOTE: non-OTG systems may use SRP TOO... */
1186 } else if (!(udc->devstat & UDC_ATT)) {
1187 if (udc->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001188 retval = otg_start_srp(udc->transceiver->otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 spin_unlock_irqrestore(&udc->lock, flags);
1191
1192 return retval;
1193}
1194
1195static int
1196omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1197{
1198 struct omap_udc *udc;
1199 unsigned long flags;
1200 u16 syscon1;
1201
1202 udc = container_of(gadget, struct omap_udc, gadget);
1203 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001204 syscon1 = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (is_selfpowered)
1206 syscon1 |= UDC_SELF_PWR;
1207 else
1208 syscon1 &= ~UDC_SELF_PWR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001209 omap_writew(syscon1, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 spin_unlock_irqrestore(&udc->lock, flags);
1211
1212 return 0;
1213}
1214
1215static int can_pullup(struct omap_udc *udc)
1216{
1217 return udc->driver && udc->softconnect && udc->vbus_active;
1218}
1219
1220static void pullup_enable(struct omap_udc *udc)
1221{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001222 u16 w;
1223
1224 w = omap_readw(UDC_SYSCON1);
1225 w |= UDC_PULLUP_EN;
1226 omap_writew(w, UDC_SYSCON1);
1227 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1228 u32 l;
1229
1230 l = omap_readl(OTG_CTRL);
1231 l |= OTG_BSESSVLD;
1232 omap_writel(l, OTG_CTRL);
1233 }
1234 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
1237static void pullup_disable(struct omap_udc *udc)
1238{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001239 u16 w;
1240
1241 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1242 u32 l;
1243
1244 l = omap_readl(OTG_CTRL);
1245 l &= ~OTG_BSESSVLD;
1246 omap_writel(l, OTG_CTRL);
1247 }
1248 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1249 w = omap_readw(UDC_SYSCON1);
1250 w &= ~UDC_PULLUP_EN;
1251 omap_writew(w, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
David Brownelle6a6e472006-12-10 11:47:04 -08001254static struct omap_udc *udc;
1255
1256static void omap_udc_enable_clock(int enable)
1257{
1258 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1259 return;
1260
1261 if (enable) {
1262 clk_enable(udc->dc_clk);
1263 clk_enable(udc->hhc_clk);
1264 udelay(100);
1265 } else {
1266 clk_disable(udc->hhc_clk);
1267 clk_disable(udc->dc_clk);
1268 }
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271/*
1272 * Called by whatever detects VBUS sessions: external transceiver
1273 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1274 */
1275static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1276{
1277 struct omap_udc *udc;
1278 unsigned long flags;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001279 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 udc = container_of(gadget, struct omap_udc, gadget);
1282 spin_lock_irqsave(&udc->lock, flags);
1283 VDBG("VBUS %s\n", is_active ? "on" : "off");
1284 udc->vbus_active = (is_active != 0);
1285 if (cpu_is_omap15xx()) {
1286 /* "software" detect, ignored if !VBUS_MODE_1510 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001287 l = omap_readl(FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (is_active)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001289 l |= VBUS_CTRL_1510;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001291 l &= ~VBUS_CTRL_1510;
1292 omap_writel(l, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
David Brownelle6a6e472006-12-10 11:47:04 -08001294 if (udc->dc_clk != NULL && is_active) {
1295 if (!udc->clk_requested) {
1296 omap_udc_enable_clock(1);
1297 udc->clk_requested = 1;
1298 }
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (can_pullup(udc))
1301 pullup_enable(udc);
1302 else
1303 pullup_disable(udc);
David Brownelle6a6e472006-12-10 11:47:04 -08001304 if (udc->dc_clk != NULL && !is_active) {
1305 if (udc->clk_requested) {
1306 omap_udc_enable_clock(0);
1307 udc->clk_requested = 0;
1308 }
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 spin_unlock_irqrestore(&udc->lock, flags);
1311 return 0;
1312}
1313
1314static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1315{
1316 struct omap_udc *udc;
1317
1318 udc = container_of(gadget, struct omap_udc, gadget);
1319 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001320 return usb_phy_set_power(udc->transceiver, mA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return -EOPNOTSUPP;
1322}
1323
1324static int omap_pullup(struct usb_gadget *gadget, int is_on)
1325{
1326 struct omap_udc *udc;
1327 unsigned long flags;
1328
1329 udc = container_of(gadget, struct omap_udc, gadget);
1330 spin_lock_irqsave(&udc->lock, flags);
1331 udc->softconnect = (is_on != 0);
1332 if (can_pullup(udc))
1333 pullup_enable(udc);
1334 else
1335 pullup_disable(udc);
1336 spin_unlock_irqrestore(&udc->lock, flags);
1337 return 0;
1338}
1339
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001340static int omap_udc_start(struct usb_gadget_driver *driver,
1341 int (*bind)(struct usb_gadget *));
1342static int omap_udc_stop(struct usb_gadget_driver *driver);
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344static struct usb_gadget_ops omap_gadget_ops = {
1345 .get_frame = omap_get_frame,
1346 .wakeup = omap_wakeup,
1347 .set_selfpowered = omap_set_selfpowered,
1348 .vbus_session = omap_vbus_session,
1349 .vbus_draw = omap_vbus_draw,
1350 .pullup = omap_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001351 .start = omap_udc_start,
1352 .stop = omap_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353};
1354
1355/*-------------------------------------------------------------------------*/
1356
1357/* dequeue ALL requests; caller holds udc->lock */
1358static void nuke(struct omap_ep *ep, int status)
1359{
1360 struct omap_req *req;
1361
1362 ep->stopped = 1;
1363
1364 if (use_dma && ep->dma_channel)
1365 dma_channel_release(ep);
1366
1367 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001368 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001370 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 while (!list_empty(&ep->queue)) {
1373 req = list_entry(ep->queue.next, struct omap_req, queue);
1374 done(ep, req, status);
1375 }
1376}
1377
1378/* caller holds udc->lock */
1379static void udc_quiesce(struct omap_udc *udc)
1380{
1381 struct omap_ep *ep;
1382
1383 udc->gadget.speed = USB_SPEED_UNKNOWN;
1384 nuke(&udc->ep[0], -ESHUTDOWN);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001385 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 nuke(ep, -ESHUTDOWN);
1387}
1388
1389/*-------------------------------------------------------------------------*/
1390
1391static void update_otg(struct omap_udc *udc)
1392{
1393 u16 devstat;
1394
David Brownell9cfbba72007-10-26 13:42:18 -07001395 if (!gadget_is_otg(&udc->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return;
1397
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001398 if (omap_readl(OTG_CTRL) & OTG_ID)
1399 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 else
1401 devstat = 0;
1402
1403 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1404 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1405 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1406
1407 /* Enable HNP early, avoiding races on suspend irq path.
1408 * ASSUMES OTG state machine B_BUS_REQ input is true.
1409 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001410 if (udc->gadget.b_hnp_enable) {
1411 u32 l;
1412
1413 l = omap_readl(OTG_CTRL);
1414 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1415 l &= ~OTG_PULLUP;
1416 omap_writel(l, OTG_CTRL);
1417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1421{
1422 struct omap_ep *ep0 = &udc->ep[0];
David Brownell313980c2005-04-11 15:38:25 -07001423 struct omap_req *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 ep0->irqs++;
1426
1427 /* Clear any pending requests and then scrub any rx/tx state
1428 * before starting to handle the SETUP request.
1429 */
1430 if (irq_src & UDC_SETUP) {
1431 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1432
1433 nuke(ep0, 0);
1434 if (ack) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001435 omap_writew(ack, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 irq_src = UDC_SETUP;
1437 }
1438 }
1439
David Brownelle6a6e472006-12-10 11:47:04 -08001440 /* IN/OUT packets mean we're in the DATA or STATUS stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * This driver uses only uses protocol stalls (ep0 never halts),
1442 * and if we got this far the gadget driver already had a
1443 * chance to stall. Tries to be forgiving of host oddities.
1444 *
1445 * NOTE: the last chance gadget drivers have to stall control
1446 * requests is during their request completion callback.
1447 */
1448 if (!list_empty(&ep0->queue))
1449 req = container_of(ep0->queue.next, struct omap_req, queue);
1450
1451 /* IN == TX to host */
1452 if (irq_src & UDC_EP0_TX) {
1453 int stat;
1454
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001455 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1456 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1457 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (stat & UDC_ACK) {
1459 if (udc->ep0_in) {
1460 /* write next IN packet from response,
1461 * or set up the status stage.
1462 */
1463 if (req)
1464 stat = write_fifo(ep0, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001465 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (!req && udc->ep0_pending) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001467 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1468 omap_writew(UDC_CLR_EP, UDC_CTRL);
1469 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1470 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 udc->ep0_pending = 0;
1472 } /* else: 6 wait states before it'll tx */
1473 } else {
1474 /* ack status stage of OUT transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001475 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (req)
1477 done(ep0, req, 0);
1478 }
David Brownell313980c2005-04-11 15:38:25 -07001479 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001481 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1482 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001484 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486 }
1487
1488 /* OUT == RX from host */
1489 if (irq_src & UDC_EP0_RX) {
1490 int stat;
1491
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001492 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1493 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1494 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (stat & UDC_ACK) {
1496 if (!udc->ep0_in) {
1497 stat = 0;
1498 /* read next OUT packet of request, maybe
1499 * reactiviting the fifo; stall on errors.
1500 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001501 stat = read_fifo(ep0, req);
1502 if (!req || stat < 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001503 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 udc->ep0_pending = 0;
1505 stat = 0;
1506 } else if (stat == 0)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001507 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1508 omap_writew(0, UDC_EP_NUM);
David Brownelle6a6e472006-12-10 11:47:04 -08001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 /* activate status stage */
1511 if (stat == 1) {
1512 done(ep0, req, 0);
1513 /* that may have STALLed ep0... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001514 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1515 UDC_EP_NUM);
1516 omap_writew(UDC_CLR_EP, UDC_CTRL);
1517 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1518 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 udc->ep0_pending = 0;
1520 }
1521 } else {
1522 /* ack status stage of IN transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001523 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (req)
1525 done(ep0, req, 0);
1526 }
1527 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001528 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1529 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001531 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 }
1534
1535 /* SETUP starts all control transfers */
1536 if (irq_src & UDC_SETUP) {
1537 union u {
1538 u16 word[4];
1539 struct usb_ctrlrequest r;
1540 } u;
1541 int status = -EINVAL;
1542 struct omap_ep *ep;
1543
1544 /* read the (latest) SETUP message */
1545 do {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001546 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* two bytes at a time */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001548 u.word[0] = omap_readw(UDC_DATA);
1549 u.word[1] = omap_readw(UDC_DATA);
1550 u.word[2] = omap_readw(UDC_DATA);
1551 u.word[3] = omap_readw(UDC_DATA);
1552 omap_writew(0, UDC_EP_NUM);
1553 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
David Brownell01ee7d72007-05-25 20:40:14 -07001555#define w_value le16_to_cpu(u.r.wValue)
1556#define w_index le16_to_cpu(u.r.wIndex)
1557#define w_length le16_to_cpu(u.r.wLength)
David Brownell65111082005-04-28 13:52:31 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* Delegate almost all control requests to the gadget driver,
1560 * except for a handful of ch9 status/feature requests that
1561 * hardware doesn't autodecode _and_ the gadget API hides.
1562 */
1563 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1564 udc->ep0_set_config = 0;
1565 udc->ep0_pending = 1;
1566 ep0->stopped = 0;
1567 ep0->ackwait = 0;
1568 switch (u.r.bRequest) {
1569 case USB_REQ_SET_CONFIGURATION:
1570 /* udc needs to know when ep != 0 is valid */
1571 if (u.r.bRequestType != USB_RECIP_DEVICE)
1572 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001573 if (w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 goto do_stall;
1575 udc->ep0_set_config = 1;
David Brownell65111082005-04-28 13:52:31 -07001576 udc->ep0_reset_config = (w_value == 0);
1577 VDBG("set config %d\n", w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 /* update udc NOW since gadget driver may start
1580 * queueing requests immediately; clear config
1581 * later if it fails the request.
1582 */
1583 if (udc->ep0_reset_config)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001584 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001586 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 update_otg(udc);
1588 goto delegate;
1589 case USB_REQ_CLEAR_FEATURE:
1590 /* clear endpoint halt */
1591 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1592 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001593 if (w_value != USB_ENDPOINT_HALT
1594 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001596 ep = &udc->ep[w_index & 0xf];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (ep != ep0) {
David Brownell65111082005-04-28 13:52:31 -07001598 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ep += 16;
1600 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001601 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto do_stall;
1603 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001604 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 ep->ackwait = 0;
1606 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001607 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ep->ackwait = 1 + ep->double_buf;
1609 }
David Brownell313980c2005-04-11 15:38:25 -07001610 /* NOTE: assumes the host behaves sanely,
1611 * only clearing real halts. Else we may
1612 * need to kill pending transfers and then
1613 * restart the queue... very messy for DMA!
1614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 VDBG("%s halt cleared by host\n", ep->name);
1617 goto ep0out_status_stage;
1618 case USB_REQ_SET_FEATURE:
1619 /* set endpoint halt */
1620 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1621 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001622 if (w_value != USB_ENDPOINT_HALT
1623 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001625 ep = &udc->ep[w_index & 0xf];
1626 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 ep += 16;
1628 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001629 || ep == ep0 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto do_stall;
1631 if (use_dma && ep->has_dma) {
1632 /* this has rude side-effects (aborts) and
1633 * can't really work if DMA-IN is active
1634 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001635 DBG("%s host set_halt, NYET\n", ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 goto do_stall;
1637 }
1638 use_ep(ep, 0);
1639 /* can't halt if fifo isn't empty... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001640 omap_writew(UDC_CLR_EP, UDC_CTRL);
1641 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 VDBG("%s halted by host\n", ep->name);
1643ep0out_status_stage:
1644 status = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001645 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1646 omap_writew(UDC_CLR_EP, UDC_CTRL);
1647 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1648 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 udc->ep0_pending = 0;
1650 break;
1651 case USB_REQ_GET_STATUS:
David Brownell8a3c1f52007-03-21 12:26:32 -07001652 /* USB_ENDPOINT_HALT status? */
1653 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1654 goto intf_status;
1655
1656 /* ep0 never stalls */
1657 if (!(w_index & 0xf))
1658 goto zero_status;
1659
1660 /* only active endpoints count */
1661 ep = &udc->ep[w_index & 0xf];
1662 if (w_index & USB_DIR_IN)
1663 ep += 16;
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001664 if (!ep->ep.desc)
David Brownell8a3c1f52007-03-21 12:26:32 -07001665 goto do_stall;
1666
1667 /* iso never stalls */
1668 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1669 goto zero_status;
1670
1671 /* FIXME don't assume non-halted endpoints!! */
1672 ERR("%s status, can't report\n", ep->ep.name);
1673 goto do_stall;
1674
1675intf_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* return interface status. if we were pedantic,
1677 * we'd detect non-existent interfaces, and stall.
1678 */
1679 if (u.r.bRequestType
1680 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1681 goto delegate;
David Brownell8a3c1f52007-03-21 12:26:32 -07001682
1683zero_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /* return two zero bytes */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001685 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1686 omap_writew(0, UDC_DATA);
1687 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1688 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 status = 0;
David Brownell65111082005-04-28 13:52:31 -07001690 VDBG("GET_STATUS, interface %d\n", w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 /* next, status stage */
1692 break;
1693 default:
1694delegate:
1695 /* activate the ep0out fifo right away */
David Brownell65111082005-04-28 13:52:31 -07001696 if (!udc->ep0_in && w_length) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001697 omap_writew(0, UDC_EP_NUM);
1698 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
1701 /* gadget drivers see class/vendor specific requests,
1702 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1703 * and more
1704 */
1705 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1706 u.r.bRequestType, u.r.bRequest,
David Brownell65111082005-04-28 13:52:31 -07001707 w_value, w_index, w_length);
1708
1709#undef w_value
1710#undef w_index
1711#undef w_length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /* The gadget driver may return an error here,
1714 * causing an immediate protocol stall.
1715 *
1716 * Else it must issue a response, either queueing a
1717 * response buffer for the DATA stage, or halting ep0
1718 * (causing a protocol stall, not a real halt). A
1719 * zero length buffer means no DATA stage.
1720 *
1721 * It's fine to issue that response after the setup()
1722 * call returns, and this IRQ was handled.
1723 */
1724 udc->ep0_setup = 1;
1725 spin_unlock(&udc->lock);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001726 status = udc->driver->setup(&udc->gadget, &u.r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 spin_lock(&udc->lock);
1728 udc->ep0_setup = 0;
1729 }
1730
1731 if (status < 0) {
1732do_stall:
1733 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1734 u.r.bRequestType, u.r.bRequest, status);
1735 if (udc->ep0_set_config) {
1736 if (udc->ep0_reset_config)
Arjan van de Venb6c63932008-07-25 01:45:52 -07001737 WARNING("error resetting config?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001739 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001741 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 udc->ep0_pending = 0;
1743 }
1744 }
1745}
1746
1747/*-------------------------------------------------------------------------*/
1748
1749#define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1750
1751static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1752{
1753 u16 devstat, change;
1754
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001755 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 change = devstat ^ udc->devstat;
1757 udc->devstat = devstat;
1758
1759 if (change & (UDC_USB_RESET|UDC_ATT)) {
1760 udc_quiesce(udc);
1761
1762 if (change & UDC_ATT) {
1763 /* driver for any external transceiver will
1764 * have called omap_vbus_session() already
1765 */
1766 if (devstat & UDC_ATT) {
1767 udc->gadget.speed = USB_SPEED_FULL;
1768 VDBG("connect\n");
1769 if (!udc->transceiver)
1770 pullup_enable(udc);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001771 /* if (driver->connect) call it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1773 udc->gadget.speed = USB_SPEED_UNKNOWN;
1774 if (!udc->transceiver)
1775 pullup_disable(udc);
1776 DBG("disconnect, gadget %s\n",
1777 udc->driver->driver.name);
1778 if (udc->driver->disconnect) {
1779 spin_unlock(&udc->lock);
1780 udc->driver->disconnect(&udc->gadget);
1781 spin_lock(&udc->lock);
1782 }
1783 }
1784 change &= ~UDC_ATT;
1785 }
1786
1787 if (change & UDC_USB_RESET) {
1788 if (devstat & UDC_USB_RESET) {
1789 VDBG("RESET=1\n");
1790 } else {
1791 udc->gadget.speed = USB_SPEED_FULL;
1792 INFO("USB reset done, gadget %s\n",
1793 udc->driver->driver.name);
1794 /* ep0 traffic is legal from now on */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001795 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1796 UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798 change &= ~UDC_USB_RESET;
1799 }
1800 }
1801 if (change & UDC_SUS) {
1802 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03001803 /* FIXME tell isp1301 to suspend/resume (?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (devstat & UDC_SUS) {
1805 VDBG("suspend\n");
1806 update_otg(udc);
1807 /* HNP could be under way already */
1808 if (udc->gadget.speed == USB_SPEED_FULL
1809 && udc->driver->suspend) {
1810 spin_unlock(&udc->lock);
1811 udc->driver->suspend(&udc->gadget);
1812 spin_lock(&udc->lock);
1813 }
Juha Yrj?l?4e671852005-10-16 15:47:04 -07001814 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001815 usb_phy_set_suspend(
1816 udc->transceiver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 } else {
1818 VDBG("resume\n");
Juha Yrj?l?4e671852005-10-16 15:47:04 -07001819 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001820 usb_phy_set_suspend(
1821 udc->transceiver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (udc->gadget.speed == USB_SPEED_FULL
1823 && udc->driver->resume) {
1824 spin_unlock(&udc->lock);
1825 udc->driver->resume(&udc->gadget);
1826 spin_lock(&udc->lock);
1827 }
1828 }
1829 }
1830 change &= ~UDC_SUS;
1831 }
1832 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1833 update_otg(udc);
1834 change &= ~OTG_FLAGS;
1835 }
1836
1837 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1838 if (change)
1839 VDBG("devstat %03x, ignore change %03x\n",
1840 devstat, change);
1841
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001842 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
David Howells7d12e782006-10-05 14:55:46 +01001845static irqreturn_t omap_udc_irq(int irq, void *_udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct omap_udc *udc = _udc;
1848 u16 irq_src;
1849 irqreturn_t status = IRQ_NONE;
1850 unsigned long flags;
1851
1852 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001853 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /* Device state change (usb ch9 stuff) */
1856 if (irq_src & UDC_DS_CHG) {
1857 devstate_irq(_udc, irq_src);
1858 status = IRQ_HANDLED;
1859 irq_src &= ~UDC_DS_CHG;
1860 }
1861
1862 /* EP0 control transfers */
1863 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1864 ep0_irq(_udc, irq_src);
1865 status = IRQ_HANDLED;
1866 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1867 }
1868
1869 /* DMA transfer completion */
1870 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1871 dma_irq(_udc, irq_src);
1872 status = IRQ_HANDLED;
1873 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1874 }
1875
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001876 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (irq_src)
1878 DBG("udc_irq, unhandled %03x\n", irq_src);
1879 spin_unlock_irqrestore(&udc->lock, flags);
1880
1881 return status;
1882}
1883
1884/* workaround for seemingly-lost IRQs for RX ACKs... */
1885#define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1886#define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1887
1888static void pio_out_timer(unsigned long _ep)
1889{
1890 struct omap_ep *ep = (void *) _ep;
1891 unsigned long flags;
1892 u16 stat_flg;
1893
1894 spin_lock_irqsave(&ep->udc->lock, flags);
1895 if (!list_empty(&ep->queue) && ep->ackwait) {
David Brownelle6a6e472006-12-10 11:47:04 -08001896 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001897 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1900 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1901 struct omap_req *req;
1902
1903 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1904 req = container_of(ep->queue.next,
1905 struct omap_req, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 (void) read_fifo(ep, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001907 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1908 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 ep->ackwait = 1 + ep->double_buf;
David Brownelle6a6e472006-12-10 11:47:04 -08001910 } else
1911 deselect_ep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1914 spin_unlock_irqrestore(&ep->udc->lock, flags);
1915}
1916
David Howells7d12e782006-10-05 14:55:46 +01001917static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 u16 epn_stat, irq_src;
1920 irqreturn_t status = IRQ_NONE;
1921 struct omap_ep *ep;
1922 int epnum;
1923 struct omap_udc *udc = _dev;
1924 struct omap_req *req;
1925 unsigned long flags;
1926
1927 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001928 epn_stat = omap_readw(UDC_EPN_STAT);
1929 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* handle OUT first, to avoid some wasteful NAKs */
1932 if (irq_src & UDC_EPN_RX) {
1933 epnum = (epn_stat >> 8) & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001934 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 status = IRQ_HANDLED;
1936 ep = &udc->ep[epnum];
1937 ep->irqs++;
1938
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001939 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 ep->fnf = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001941 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 ep->ackwait--;
1943 if (!list_empty(&ep->queue)) {
1944 int stat;
1945 req = container_of(ep->queue.next,
1946 struct omap_req, queue);
1947 stat = read_fifo(ep, req);
1948 if (!ep->double_buf)
1949 ep->fnf = 1;
1950 }
1951 }
1952 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001953 epn_stat = omap_readw(UDC_EPN_STAT);
1954 epn_stat = omap_readw(UDC_EPN_STAT);
1955 omap_writew(epnum, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 /* enabling fifo _after_ clearing ACK, contrary to docs,
1958 * reduces lossage; timer still needed though (sigh).
1959 */
1960 if (ep->fnf) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001961 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 ep->ackwait = 1 + ep->double_buf;
1963 }
1964 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1965 }
1966
1967 /* then IN transfers */
1968 else if (irq_src & UDC_EPN_TX) {
1969 epnum = epn_stat & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001970 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 status = IRQ_HANDLED;
1972 ep = &udc->ep[16 + epnum];
1973 ep->irqs++;
1974
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001975 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1976 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 ep->ackwait = 0;
1978 if (!list_empty(&ep->queue)) {
1979 req = container_of(ep->queue.next,
1980 struct omap_req, queue);
1981 (void) write_fifo(ep, req);
1982 }
1983 }
1984 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001985 epn_stat = omap_readw(UDC_EPN_STAT);
1986 epn_stat = omap_readw(UDC_EPN_STAT);
1987 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /* then 6 clocks before it'd tx */
1989 }
1990
1991 spin_unlock_irqrestore(&udc->lock, flags);
1992 return status;
1993}
1994
1995#ifdef USE_ISO
David Howells7d12e782006-10-05 14:55:46 +01001996static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct omap_udc *udc = _dev;
1999 struct omap_ep *ep;
2000 int pending = 0;
2001 unsigned long flags;
2002
2003 spin_lock_irqsave(&udc->lock, flags);
2004
2005 /* handle all non-DMA ISO transfers */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002006 list_for_each_entry(ep, &udc->iso, iso) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 u16 stat;
2008 struct omap_req *req;
2009
2010 if (ep->has_dma || list_empty(&ep->queue))
2011 continue;
2012 req = list_entry(ep->queue.next, struct omap_req, queue);
2013
2014 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002015 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 /* NOTE: like the other controller drivers, this isn't
2018 * currently reporting lost or damaged frames.
2019 */
2020 if (ep->bEndpointAddress & USB_DIR_IN) {
2021 if (stat & UDC_MISS_IN)
2022 /* done(ep, req, -EPROTO) */;
2023 else
2024 write_fifo(ep, req);
2025 } else {
2026 int status = 0;
2027
2028 if (stat & UDC_NO_RXPACKET)
2029 status = -EREMOTEIO;
2030 else if (stat & UDC_ISO_ERR)
2031 status = -EILSEQ;
2032 else if (stat & UDC_DATA_FLUSH)
2033 status = -ENOSR;
2034
2035 if (status)
2036 /* done(ep, req, status) */;
2037 else
2038 read_fifo(ep, req);
2039 }
2040 deselect_ep();
2041 /* 6 wait states before next EP */
2042
2043 ep->irqs++;
2044 if (!list_empty(&ep->queue))
2045 pending = 1;
2046 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002047 if (!pending) {
2048 u16 w;
2049
2050 w = omap_readw(UDC_IRQ_EN);
2051 w &= ~UDC_SOF_IE;
2052 omap_writew(w, UDC_IRQ_EN);
2053 }
2054 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 spin_unlock_irqrestore(&udc->lock, flags);
2057 return IRQ_HANDLED;
2058}
2059#endif
2060
2061/*-------------------------------------------------------------------------*/
2062
David Brownell8a3c1f52007-03-21 12:26:32 -07002063static inline int machine_without_vbus_sense(void)
David Brownelle6a6e472006-12-10 11:47:04 -08002064{
Felipe Balbi80dd1352012-05-29 14:26:09 +03002065 return machine_is_omap_innovator()
David Brownelle6a6e472006-12-10 11:47:04 -08002066 || machine_is_omap_osk()
David Brownelle6a6e472006-12-10 11:47:04 -08002067 || machine_is_sx1()
Felipe Balbi80dd1352012-05-29 14:26:09 +03002068 /* No known omap7xx boards with vbus sense */
2069 || cpu_is_omap7xx();
David Brownelle6a6e472006-12-10 11:47:04 -08002070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002072static int omap_udc_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002073 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 int status = -ENODEV;
2076 struct omap_ep *ep;
2077 unsigned long flags;
2078
2079 /* basic sanity tests */
2080 if (!udc)
2081 return -ENODEV;
2082 if (!driver
Felipe Balbi80dd1352012-05-29 14:26:09 +03002083 /* FIXME if otg, check: driver->is_otg */
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002084 || driver->max_speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002085 || !bind || !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return -EINVAL;
2087
2088 spin_lock_irqsave(&udc->lock, flags);
2089 if (udc->driver) {
2090 spin_unlock_irqrestore(&udc->lock, flags);
2091 return -EBUSY;
2092 }
2093
2094 /* reset state */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002095 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 ep->irqs = 0;
2097 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2098 continue;
2099 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002100 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102 udc->ep0_pending = 0;
2103 udc->ep[0].irqs = 0;
2104 udc->softconnect = 1;
2105
2106 /* hook up the driver */
David Brownell313980c2005-04-11 15:38:25 -07002107 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 udc->driver = driver;
2109 udc->gadget.dev.driver = &driver->driver;
2110 spin_unlock_irqrestore(&udc->lock, flags);
2111
David Brownelle6a6e472006-12-10 11:47:04 -08002112 if (udc->dc_clk != NULL)
2113 omap_udc_enable_clock(1);
2114
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002115 status = bind(&udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (status) {
2117 DBG("bind to %s --> %d\n", driver->driver.name, status);
David Brownell313980c2005-04-11 15:38:25 -07002118 udc->gadget.dev.driver = NULL;
2119 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto done;
2121 }
2122 DBG("bound to driver %s\n", driver->driver.name);
2123
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002124 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* connect to bus through transceiver */
2127 if (udc->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002128 status = otg_set_peripheral(udc->transceiver->otg,
2129 &udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (status < 0) {
2131 ERR("can't bind to transceiver\n");
David Brownell6bea4762006-12-05 03:15:33 -08002132 if (driver->unbind) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002133 driver->unbind(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002134 udc->gadget.dev.driver = NULL;
2135 udc->driver = NULL;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 goto done;
2138 }
2139 } else {
2140 if (can_pullup(udc))
Felipe Balbi80dd1352012-05-29 14:26:09 +03002141 pullup_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002143 pullup_disable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
2146 /* boards that don't have VBUS sensing can't autogate 48MHz;
2147 * can't enter deep sleep while a gadget driver is active.
2148 */
David Brownell8a3c1f52007-03-21 12:26:32 -07002149 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 omap_vbus_session(&udc->gadget, 1);
2151
2152done:
David Brownelle6a6e472006-12-10 11:47:04 -08002153 if (udc->dc_clk != NULL)
2154 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return status;
2156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002158static int omap_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 unsigned long flags;
2161 int status = -ENODEV;
2162
2163 if (!udc)
2164 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08002165 if (!driver || driver != udc->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -EINVAL;
2167
David Brownelle6a6e472006-12-10 11:47:04 -08002168 if (udc->dc_clk != NULL)
2169 omap_udc_enable_clock(1);
2170
David Brownell8a3c1f52007-03-21 12:26:32 -07002171 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 omap_vbus_session(&udc->gadget, 0);
2173
2174 if (udc->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002175 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 else
2177 pullup_disable(udc);
2178
2179 spin_lock_irqsave(&udc->lock, flags);
2180 udc_quiesce(udc);
2181 spin_unlock_irqrestore(&udc->lock, flags);
2182
2183 driver->unbind(&udc->gadget);
David Brownell313980c2005-04-11 15:38:25 -07002184 udc->gadget.dev.driver = NULL;
2185 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
David Brownelle6a6e472006-12-10 11:47:04 -08002187 if (udc->dc_clk != NULL)
2188 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 DBG("unregistered driver '%s'\n", driver->driver.name);
2190 return status;
2191}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193/*-------------------------------------------------------------------------*/
2194
2195#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2196
2197#include <linux/seq_file.h>
2198
2199static const char proc_filename[] = "driver/udc";
2200
2201#define FOURBITS "%s%s%s%s"
Felipe Balbi80dd1352012-05-29 14:26:09 +03002202#define EIGHTBITS "%s%s%s%s%s%s%s%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2205{
2206 u16 stat_flg;
2207 struct omap_req *req;
2208 char buf[20];
2209
2210 use_ep(ep, 0);
2211
2212 if (use_dma && ep->has_dma)
2213 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2214 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2215 ep->dma_channel - 1, ep->lch);
2216 else
2217 buf[0] = 0;
2218
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002219 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 seq_printf(s,
2221 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2222 ep->name, buf,
2223 ep->double_buf ? "dbuf " : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002224 ({ char *s;
2225 switch (ep->ackwait) {
2226 case 0:
2227 s = "";
2228 break;
2229 case 1:
2230 s = "(ackw) ";
2231 break;
2232 case 2:
2233 s = "(ackw2) ";
2234 break;
2235 default:
2236 s = "(?) ";
2237 break;
2238 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 ep->irqs, stat_flg,
2240 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2241 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2242 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2243 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2244 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2245 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2246 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2247 (stat_flg & UDC_STALL) ? "STALL " : "",
2248 (stat_flg & UDC_NAK) ? "NAK " : "",
2249 (stat_flg & UDC_ACK) ? "ACK " : "",
2250 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2251 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2252 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2253
Felipe Balbi80dd1352012-05-29 14:26:09 +03002254 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 seq_printf(s, "\t(queue empty)\n");
2256 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002257 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 unsigned length = req->req.actual;
2259
2260 if (use_dma && buf[0]) {
2261 length += ((ep->bEndpointAddress & USB_DIR_IN)
2262 ? dma_src_len : dma_dest_len)
2263 (ep, req->req.dma + length);
2264 buf[0] = 0;
2265 }
2266 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2267 &req->req, length,
2268 req->req.length, req->req.buf);
2269 }
2270}
2271
2272static char *trx_mode(unsigned m, int enabled)
2273{
2274 switch (m) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002275 case 0:
2276 return enabled ? "*6wire" : "unused";
2277 case 1:
2278 return "4wire";
2279 case 2:
2280 return "3wire";
2281 case 3:
2282 return "6wire";
2283 default:
2284 return "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286}
2287
2288static int proc_otg_show(struct seq_file *s)
2289{
2290 u32 tmp;
Paul Walmsley4814ced2010-10-08 11:40:20 -06002291 u32 trans = 0;
2292 char *ctrl_name = "(UNKNOWN)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Dmitry Baryshkove12cc342008-08-07 16:29:25 +04002294 tmp = omap_readl(OTG_REV);
Tony Lindgrenae372572012-05-21 12:01:11 -07002295 ctrl_name = "tranceiver_ctrl";
2296 trans = omap_readw(USB_TRANSCEIVER_CTRL);
David Brownelle6a6e472006-12-10 11:47:04 -08002297 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2298 tmp >> 4, tmp & 0xf, ctrl_name, trans);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002299 tmp = omap_readw(OTG_SYSCON_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2301 FOURBITS "\n", tmp,
2302 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2303 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
David Brownell65111082005-04-28 13:52:31 -07002304 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 ? "internal"
2306 : trx_mode(USB0_TRX_MODE(tmp), 1),
2307 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2308 (tmp & HST_IDLE_EN) ? " !host" : "",
2309 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2310 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002311 tmp = omap_readl(OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2313 " b_ase_brst=%d hmc=%d\n", tmp,
2314 (tmp & OTG_EN) ? " otg_en" : "",
2315 (tmp & USBX_SYNCHRO) ? " synchro" : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002316 /* much more SRP stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 (tmp & SRP_DATA) ? " srp_data" : "",
2318 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2319 (tmp & OTG_PADEN) ? " otg_paden" : "",
2320 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2321 (tmp & UHOST_EN) ? " uhost_en" : "",
2322 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2323 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2324 B_ASE_BRST(tmp),
2325 OTG_HMC(tmp));
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002326 tmp = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2328 (tmp & OTG_ASESSVLD) ? " asess" : "",
2329 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2330 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2331 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2332 (tmp & OTG_ID) ? " id" : "",
2333 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2334 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2335 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2336 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2337 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2338 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2339 (tmp & OTG_PULLDOWN) ? " down" : "",
2340 (tmp & OTG_PULLUP) ? " up" : "",
2341 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2342 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2343 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2344 (tmp & OTG_PU_ID) ? " pu_id" : ""
2345 );
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002346 tmp = omap_readw(OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002348 tmp = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002350 tmp = omap_readw(OTG_OUTCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002352 tmp = omap_readw(OTG_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 seq_printf(s, "otg_test %04x" "\n", tmp);
David Brownell313980c2005-04-11 15:38:25 -07002354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355}
2356
2357static int proc_udc_show(struct seq_file *s, void *_)
2358{
2359 u32 tmp;
2360 struct omap_ep *ep;
2361 unsigned long flags;
2362
2363 spin_lock_irqsave(&udc->lock, flags);
2364
2365 seq_printf(s, "%s, version: " DRIVER_VERSION
2366#ifdef USE_ISO
2367 " (iso)"
2368#endif
2369 "%s\n",
2370 driver_desc,
2371 use_dma ? " (dma)" : "");
2372
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002373 tmp = omap_readw(UDC_REV) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 seq_printf(s,
2375 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2376 "hmc %d, transceiver %s\n",
2377 tmp >> 4, tmp & 0xf,
2378 fifo_mode,
2379 udc->driver ? udc->driver->driver.name : "(none)",
2380 HMC,
David Brownelle6a6e472006-12-10 11:47:04 -08002381 udc->transceiver
2382 ? udc->transceiver->label
Tony Lindgrenae372572012-05-21 12:01:11 -07002383 : (cpu_is_omap1710()
David Brownelle6a6e472006-12-10 11:47:04 -08002384 ? "external" : "(none)"));
Tony Lindgrenae372572012-05-21 12:01:11 -07002385 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2386 omap_readw(ULPD_CLOCK_CTRL),
2387 omap_readw(ULPD_SOFT_REQ),
2388 omap_readw(ULPD_STATUS_REQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* OTG controller registers */
2391 if (!cpu_is_omap15xx())
2392 proc_otg_show(s);
2393
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002394 tmp = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2396 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2397 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2398 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2399 (tmp & UDC_NAK_EN) ? " nak" : "",
2400 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2401 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2402 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2403 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
Felipe Balbi80dd1352012-05-29 14:26:09 +03002404 /* syscon2 is write-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 /* UDC controller registers */
2407 if (!(tmp & UDC_PULLUP_EN)) {
2408 seq_printf(s, "(suspended)\n");
2409 spin_unlock_irqrestore(&udc->lock, flags);
2410 return 0;
2411 }
2412
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002413 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2415 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2416 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2417 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2418 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2419 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2420 (tmp & UDC_SUS) ? " SUS" : "",
2421 (tmp & UDC_CFG) ? " CFG" : "",
2422 (tmp & UDC_ADD) ? " ADD" : "",
2423 (tmp & UDC_DEF) ? " DEF" : "",
2424 (tmp & UDC_ATT) ? " ATT" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002425 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2426 tmp = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2428 (tmp & UDC_SOF_IE) ? " sof" : "",
2429 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2430 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2431 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2432 (tmp & UDC_EP0_IE) ? " ep0" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002433 tmp = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2435 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2436 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2437 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002438 (tmp & UDC_IRQ_SOF) ? " sof" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2440 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2441 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2442 (tmp & UDC_SETUP) ? " setup" : "",
2443 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2444 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2445 if (use_dma) {
2446 unsigned i;
2447
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002448 tmp = omap_readw(UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2450 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2451 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2452 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2453
2454 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2455 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2456 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2457
2458 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2459 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2460 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2461
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002462 tmp = omap_readw(UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2464 if (tmp) {
2465 for (i = 0; i < 3; i++) {
2466 if ((tmp & (0x0f << (i * 4))) == 0)
2467 continue;
2468 seq_printf(s, "rxdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002469 omap_readw(UDC_RXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002472 tmp = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 seq_printf(s, "txdma_cfg %04x\n", tmp);
2474 if (tmp) {
2475 for (i = 0; i < 3; i++) {
2476 if (!(tmp & (0x0f << (i * 4))))
2477 continue;
2478 seq_printf(s, "txdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002479 omap_readw(UDC_TXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481 }
2482 }
2483
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002484 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (tmp & UDC_ATT) {
2486 proc_ep_show(s, &udc->ep[0]);
2487 if (tmp & UDC_ADD) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002488 list_for_each_entry(ep, &udc->gadget.ep_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ep.ep_list) {
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02002490 if (ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 proc_ep_show(s, ep);
2492 }
2493 }
2494 }
2495 spin_unlock_irqrestore(&udc->lock, flags);
2496 return 0;
2497}
2498
2499static int proc_udc_open(struct inode *inode, struct file *file)
2500{
David Brownell313980c2005-04-11 15:38:25 -07002501 return single_open(file, proc_udc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03002504static const struct file_operations proc_ops = {
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002505 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .open = proc_udc_open,
2507 .read = seq_read,
2508 .llseek = seq_lseek,
2509 .release = single_release,
2510};
2511
2512static void create_proc_file(void)
2513{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002514 proc_create(proc_filename, 0, NULL, &proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517static void remove_proc_file(void)
2518{
David Brownell313980c2005-04-11 15:38:25 -07002519 remove_proc_entry(proc_filename, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522#else
2523
2524static inline void create_proc_file(void) {}
2525static inline void remove_proc_file(void) {}
2526
2527#endif
2528
2529/*-------------------------------------------------------------------------*/
2530
2531/* Before this controller can enumerate, we need to pick an endpoint
2532 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2533 * buffer space among the endpoints we'll be operating.
David Brownell65111082005-04-28 13:52:31 -07002534 *
2535 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002536 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
David Brownell65111082005-04-28 13:52:31 -07002537 * capability yet though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 */
Felipe Balbidc1737c2012-05-29 14:33:30 +03002539static unsigned __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540omap_ep_setup(char *name, u8 addr, u8 type,
2541 unsigned buf, unsigned maxp, int dbuf)
2542{
2543 struct omap_ep *ep;
2544 u16 epn_rxtx = 0;
2545
2546 /* OUT endpoints first, then IN */
2547 ep = &udc->ep[addr & 0xf];
2548 if (addr & USB_DIR_IN)
2549 ep += 16;
2550
2551 /* in case of ep init table bugs */
2552 BUG_ON(ep->name[0]);
2553
2554 /* chip setup ... bit values are same for IN, OUT */
2555 if (type == USB_ENDPOINT_XFER_ISOC) {
2556 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002557 case 8:
2558 epn_rxtx = 0 << 12;
2559 break;
2560 case 16:
2561 epn_rxtx = 1 << 12;
2562 break;
2563 case 32:
2564 epn_rxtx = 2 << 12;
2565 break;
2566 case 64:
2567 epn_rxtx = 3 << 12;
2568 break;
2569 case 128:
2570 epn_rxtx = 4 << 12;
2571 break;
2572 case 256:
2573 epn_rxtx = 5 << 12;
2574 break;
2575 case 512:
2576 epn_rxtx = 6 << 12;
2577 break;
2578 default:
2579 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581 epn_rxtx |= UDC_EPN_RX_ISO;
2582 dbuf = 1;
2583 } else {
2584 /* double-buffering "not supported" on 15xx,
David Brownelle6a6e472006-12-10 11:47:04 -08002585 * and ignored for PIO-IN on newer chips
2586 * (for more reliable behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
Tony Lindgrenae372572012-05-21 12:01:11 -07002588 if (!use_dma || cpu_is_omap15xx())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 dbuf = 0;
2590
2591 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002592 case 8:
2593 epn_rxtx = 0 << 12;
2594 break;
2595 case 16:
2596 epn_rxtx = 1 << 12;
2597 break;
2598 case 32:
2599 epn_rxtx = 2 << 12;
2600 break;
2601 case 64:
2602 epn_rxtx = 3 << 12;
2603 break;
2604 default:
2605 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607 if (dbuf && addr)
2608 epn_rxtx |= UDC_EPN_RX_DB;
2609 init_timer(&ep->timer);
2610 ep->timer.function = pio_out_timer;
2611 ep->timer.data = (unsigned long) ep;
2612 }
2613 if (addr)
2614 epn_rxtx |= UDC_EPN_RX_VALID;
2615 BUG_ON(buf & 0x07);
2616 epn_rxtx |= buf >> 3;
2617
2618 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2619 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2620
2621 if (addr & USB_DIR_IN)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002622 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002624 omap_writew(epn_rxtx, UDC_EP_RX(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 /* next endpoint's buffer starts after this one's */
2627 buf += maxp;
2628 if (dbuf)
2629 buf += maxp;
2630 BUG_ON(buf > 2048);
2631
2632 /* set up driver data structures */
2633 BUG_ON(strlen(name) >= sizeof ep->name);
2634 strlcpy(ep->name, name, sizeof ep->name);
2635 INIT_LIST_HEAD(&ep->queue);
2636 INIT_LIST_HEAD(&ep->iso);
2637 ep->bEndpointAddress = addr;
2638 ep->bmAttributes = type;
2639 ep->double_buf = dbuf;
David Brownelle6a6e472006-12-10 11:47:04 -08002640 ep->udc = udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 ep->ep.name = ep->name;
2643 ep->ep.ops = &omap_ep_ops;
2644 ep->ep.maxpacket = ep->maxpacket = maxp;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002645 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 return buf;
2648}
2649
2650static void omap_udc_release(struct device *dev)
2651{
2652 complete(udc->done);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002653 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002654 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
Felipe Balbidc1737c2012-05-29 14:33:30 +03002657static int __devinit
Heikki Krogerus86753812012-02-13 13:24:02 +02002658omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 unsigned tmp, buf;
2661
2662 /* abolish any previous hardware state */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002663 omap_writew(0, UDC_SYSCON1);
2664 omap_writew(0, UDC_IRQ_EN);
2665 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2666 omap_writew(0, UDC_DMA_IRQ_EN);
2667 omap_writew(0, UDC_RXDMA_CFG);
2668 omap_writew(0, UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 /* UDC_PULLUP_EN gates the chip clock */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002671 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Christoph Lametere94b1762006-12-06 20:33:17 -08002673 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (!udc)
2675 return -ENOMEM;
2676
Felipe Balbi80dd1352012-05-29 14:26:09 +03002677 spin_lock_init(&udc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 udc->gadget.ops = &omap_gadget_ops;
2680 udc->gadget.ep0 = &udc->ep[0].ep;
2681 INIT_LIST_HEAD(&udc->gadget.ep_list);
2682 INIT_LIST_HEAD(&udc->iso);
2683 udc->gadget.speed = USB_SPEED_UNKNOWN;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002684 udc->gadget.max_speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 udc->gadget.name = driver_name;
2686
2687 device_initialize(&udc->gadget.dev);
Kay Sievers0031a062008-05-02 06:02:41 +02002688 dev_set_name(&udc->gadget.dev, "gadget");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 udc->gadget.dev.release = omap_udc_release;
2690 udc->gadget.dev.parent = &odev->dev;
2691 if (use_dma)
2692 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2693
2694 udc->transceiver = xceiv;
2695
2696 /* ep0 is special; put it right after the SETUP buffer */
2697 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2698 8 /* after SETUP */, 64 /* maxpacket */, 0);
2699 list_del_init(&udc->ep[0].ep.ep_list);
2700
2701 /* initially disable all non-ep0 endpoints */
2702 for (tmp = 1; tmp < 15; tmp++) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002703 omap_writew(0, UDC_EP_RX(tmp));
2704 omap_writew(0, UDC_EP_TX(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
2706
Felipe Balbi80dd1352012-05-29 14:26:09 +03002707#define OMAP_BULK_EP(name, addr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 buf = omap_ep_setup(name "-bulk", addr, \
2709 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002710#define OMAP_INT_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 buf = omap_ep_setup(name "-int", addr, \
2712 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002713#define OMAP_ISO_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 buf = omap_ep_setup(name "-iso", addr, \
2715 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2716
2717 switch (fifo_mode) {
2718 case 0:
2719 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2720 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2721 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2722 break;
2723 case 1:
2724 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2725 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
David Brownell313980c2005-04-11 15:38:25 -07002726 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2729 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
David Brownell313980c2005-04-11 15:38:25 -07002730 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
2732 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2733 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
David Brownell313980c2005-04-11 15:38:25 -07002734 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2737 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
David Brownell313980c2005-04-11 15:38:25 -07002738 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2741 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
David Brownell313980c2005-04-11 15:38:25 -07002742 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2743 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2746 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
David Brownell313980c2005-04-11 15:38:25 -07002747 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2748 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
David Brownell313980c2005-04-11 15:38:25 -07002750 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2751 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 break;
2754
2755#ifdef USE_ISO
2756 case 2: /* mixed iso/bulk */
2757 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2758 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2759 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2760 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2761
2762 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2763
2764 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2765 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2766 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2767 break;
2768 case 3: /* mixed bulk/iso */
2769 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2770 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2771 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2772
2773 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2774 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2775 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2776
2777 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2778 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2779 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2780 break;
2781#endif
2782
2783 /* add more modes as needed */
2784
2785 default:
2786 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2787 return -ENODEV;
2788 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002789 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2791 return 0;
2792}
2793
Felipe Balbidc1737c2012-05-29 14:33:30 +03002794static int __devinit omap_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 int status = -ENODEV;
2797 int hmc;
Heikki Krogerus86753812012-02-13 13:24:02 +02002798 struct usb_phy *xceiv = NULL;
David Brownell313980c2005-04-11 15:38:25 -07002799 const char *type = NULL;
Russell King3ae5eae2005-11-09 22:32:44 +00002800 struct omap_usb_config *config = pdev->dev.platform_data;
Tony Lindgrenae372572012-05-21 12:01:11 -07002801 struct clk *dc_clk = NULL;
2802 struct clk *hhc_clk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Felipe Balbi5b6d84b2012-05-29 14:27:52 +03002804 if (cpu_is_omap7xx())
2805 use_dma = 0;
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /* NOTE: "knows" the order of the resources! */
David Brownelle6a6e472006-12-10 11:47:04 -08002808 if (!request_mem_region(pdev->resource[0].start,
Russell King3ae5eae2005-11-09 22:32:44 +00002809 pdev->resource[0].end - pdev->resource[0].start + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 driver_name)) {
2811 DBG("request_mem_region failed\n");
2812 return -EBUSY;
2813 }
2814
David Brownelle6a6e472006-12-10 11:47:04 -08002815 if (cpu_is_omap16xx()) {
2816 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2817 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2818 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2819 /* can't use omap_udc_enable_clock yet */
2820 clk_enable(dc_clk);
2821 clk_enable(hhc_clk);
2822 udelay(100);
2823 }
2824
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002825 if (cpu_is_omap7xx()) {
2826 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2827 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2828 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2829 /* can't use omap_udc_enable_clock yet */
2830 clk_enable(dc_clk);
2831 clk_enable(hhc_clk);
2832 udelay(100);
2833 }
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 INFO("OMAP UDC rev %d.%d%s\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002836 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 config->otg ? ", Mini-AB" : "");
2838
2839 /* use the mode given to us by board init code */
2840 if (cpu_is_omap15xx()) {
2841 hmc = HMC_1510;
2842 type = "(unknown)";
2843
David Brownell8a3c1f52007-03-21 12:26:32 -07002844 if (machine_without_vbus_sense()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /* just set up software VBUS detect, and then
2846 * later rig it so we always report VBUS.
2847 * FIXME without really sensing VBUS, we can't
2848 * know when to turn PULLUP_EN on/off; and that
2849 * means we always "need" the 48MHz clock.
2850 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002851 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2852 tmp &= ~VBUS_CTRL_1510;
2853 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 tmp |= VBUS_MODE_1510;
2855 tmp &= ~VBUS_CTRL_1510;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002856 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
2858 } else {
David Brownell65111082005-04-28 13:52:31 -07002859 /* The transceiver may package some GPIO logic or handle
2860 * loopback and/or transceiverless setup; if we find one,
2861 * use it. Except for OTG, we don't _need_ to talk to one;
2862 * but not having one probably means no VBUS detection.
2863 */
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002864 xceiv = usb_get_transceiver();
David Brownell65111082005-04-28 13:52:31 -07002865 if (xceiv)
2866 type = xceiv->label;
2867 else if (config->otg) {
2868 DBG("OTG requires external transceiver!\n");
2869 goto cleanup0;
2870 }
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 hmc = HMC_1610;
David Brownelle6a6e472006-12-10 11:47:04 -08002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 switch (hmc) {
David Brownell313980c2005-04-11 15:38:25 -07002875 case 0: /* POWERUP DEFAULT == 0 */
2876 case 4:
2877 case 12:
2878 case 20:
2879 if (!cpu_is_omap1710()) {
2880 type = "integrated";
2881 break;
2882 }
2883 /* FALL THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 case 3:
2885 case 11:
2886 case 16:
2887 case 19:
2888 case 25:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (!xceiv) {
2890 DBG("external transceiver not registered!\n");
David Brownell313980c2005-04-11 15:38:25 -07002891 type = "unknown";
David Brownell65111082005-04-28 13:52:31 -07002892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 case 21: /* internal loopback */
David Brownell313980c2005-04-11 15:38:25 -07002895 type = "loopback";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 break;
2897 case 14: /* transceiverless */
David Brownell65111082005-04-28 13:52:31 -07002898 if (cpu_is_omap1710())
2899 goto bad_on_1710;
2900 /* FALL THROUGH */
2901 case 13:
2902 case 15:
David Brownell313980c2005-04-11 15:38:25 -07002903 type = "no";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 break;
2905
2906 default:
David Brownell65111082005-04-28 13:52:31 -07002907bad_on_1710:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 ERR("unrecognized UDC HMC mode %d\n", hmc);
David Brownell65111082005-04-28 13:52:31 -07002909 goto cleanup0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911 }
Tony Lindgrenae372572012-05-21 12:01:11 -07002912
David Brownell313980c2005-04-11 15:38:25 -07002913 INFO("hmc mode %d, %s transceiver\n", hmc, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 /* a "gadget" abstracts/virtualizes the controller */
Russell King3ae5eae2005-11-09 22:32:44 +00002916 status = omap_udc_setup(pdev, xceiv);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002917 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto cleanup0;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002919
David Brownell313980c2005-04-11 15:38:25 -07002920 xceiv = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002921 /* "udc" is now valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 pullup_disable(udc);
2923#if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2924 udc->gadget.is_otg = (config->otg != 0);
2925#endif
2926
David Brownell65111082005-04-28 13:52:31 -07002927 /* starting with omap1710 es2.0, clear toggle is a separate bit */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002928 if (omap_readw(UDC_REV) >= 0x61)
David Brownell65111082005-04-28 13:52:31 -07002929 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2930 else
2931 udc->clr_halt = UDC_RESET_EP;
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 /* USB general purpose IRQ: ep0, state changes, dma, etc */
Russell King3ae5eae2005-11-09 22:32:44 +00002934 status = request_irq(pdev->resource[1].start, omap_udc_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002935 0, driver_name, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002937 ERR("can't get irq %d, err %d\n",
2938 (int) pdev->resource[1].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 goto cleanup1;
2940 }
2941
2942 /* USB "non-iso" IRQ (PIO for all but ep0) */
Russell King3ae5eae2005-11-09 22:32:44 +00002943 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002944 0, "omap_udc pio", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002946 ERR("can't get irq %d, err %d\n",
2947 (int) pdev->resource[2].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 goto cleanup2;
2949 }
2950#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002951 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002952 0, "omap_udc iso", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002954 ERR("can't get irq %d, err %d\n",
2955 (int) pdev->resource[3].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto cleanup3;
2957 }
2958#endif
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002959 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002960 udc->dc_clk = dc_clk;
2961 udc->hhc_clk = hhc_clk;
2962 clk_disable(hhc_clk);
2963 clk_disable(dc_clk);
2964 }
2965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 create_proc_file();
David Brownelle6a6e472006-12-10 11:47:04 -08002967 status = device_add(&udc->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002968 if (status)
2969 goto cleanup4;
2970
2971 status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
David Brownelle6a6e472006-12-10 11:47:04 -08002972 if (!status)
2973 return status;
2974 /* If fail, fall through */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002975cleanup4:
2976 remove_proc_file();
2977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978#ifdef USE_ISO
2979cleanup3:
Russell King3ae5eae2005-11-09 22:32:44 +00002980 free_irq(pdev->resource[2].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981#endif
2982
2983cleanup2:
Russell King3ae5eae2005-11-09 22:32:44 +00002984 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986cleanup1:
Felipe Balbi80dd1352012-05-29 14:26:09 +03002987 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002988 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990cleanup0:
2991 if (xceiv)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002992 usb_put_transceiver(xceiv);
David Brownelle6a6e472006-12-10 11:47:04 -08002993
Tony Lindgrenae372572012-05-21 12:01:11 -07002994 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002995 clk_disable(hhc_clk);
2996 clk_disable(dc_clk);
2997 clk_put(hhc_clk);
2998 clk_put(dc_clk);
2999 }
3000
Russell King3ae5eae2005-11-09 22:32:44 +00003001 release_mem_region(pdev->resource[0].start,
3002 pdev->resource[0].end - pdev->resource[0].start + 1);
David Brownelle6a6e472006-12-10 11:47:04 -08003003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return status;
3005}
3006
Felipe Balbidc1737c2012-05-29 14:33:30 +03003007static int __devexit omap_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07003009 DECLARE_COMPLETION_ONSTACK(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 if (!udc)
3012 return -ENODEV;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003013
3014 usb_del_gadget_udc(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08003015 if (udc->driver)
3016 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 udc->done = &done;
3019
3020 pullup_disable(udc);
3021 if (udc->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02003022 usb_put_transceiver(udc->transceiver);
David Brownell313980c2005-04-11 15:38:25 -07003023 udc->transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003025 omap_writew(0, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 remove_proc_file();
3028
3029#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00003030 free_irq(pdev->resource[3].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031#endif
Russell King3ae5eae2005-11-09 22:32:44 +00003032 free_irq(pdev->resource[2].start, udc);
3033 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
David Brownelle6a6e472006-12-10 11:47:04 -08003035 if (udc->dc_clk) {
3036 if (udc->clk_requested)
3037 omap_udc_enable_clock(0);
3038 clk_put(udc->hhc_clk);
3039 clk_put(udc->dc_clk);
3040 }
3041
Russell King3ae5eae2005-11-09 22:32:44 +00003042 release_mem_region(pdev->resource[0].start,
3043 pdev->resource[0].end - pdev->resource[0].start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 device_unregister(&udc->gadget.dev);
3046 wait_for_completion(&done);
3047
3048 return 0;
3049}
3050
David Brownell313980c2005-04-11 15:38:25 -07003051/* suspend/resume/wakeup from sysfs (echo > power/state) or when the
3052 * system is forced into deep sleep
3053 *
3054 * REVISIT we should probably reject suspend requests when there's a host
3055 * session active, rather than disconnecting, at least on boards that can
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003056 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
David Brownell313980c2005-04-11 15:38:25 -07003057 * make host resumes and VBUS detection trigger OMAP wakeup events; that
3058 * may involve talking to an external transceiver (e.g. isp1301).
3059 */
david-b@pacbell.net1d7beee2005-06-29 07:00:56 -07003060
Russell King3ae5eae2005-11-09 22:32:44 +00003061static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
David Brownell313980c2005-04-11 15:38:25 -07003063 u32 devstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003065 devstat = omap_readw(UDC_DEVSTAT);
David Brownell313980c2005-04-11 15:38:25 -07003066
3067 /* we're requesting 48 MHz clock if the pullup is enabled
3068 * (== we're attached to the host) and we're not suspended,
3069 * which would prevent entry to deep sleep...
3070 */
3071 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003072 WARNING("session active; suspend requires disconnect\n");
David Brownell313980c2005-04-11 15:38:25 -07003073 omap_pullup(&udc->gadget, 0);
3074 }
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return 0;
3077}
3078
Russell King3ae5eae2005-11-09 22:32:44 +00003079static int omap_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 DBG("resume + wakeup/SRP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 omap_pullup(&udc->gadget, 1);
3083
3084 /* maybe the host would enumerate us if we nudged it */
3085 msleep(100);
3086 return omap_wakeup(&udc->gadget);
3087}
3088
3089/*-------------------------------------------------------------------------*/
3090
Russell King3ae5eae2005-11-09 22:32:44 +00003091static struct platform_driver udc_driver = {
Felipe Balbidc1737c2012-05-29 14:33:30 +03003092 .probe = omap_udc_probe,
3093 .remove = __devexit_p(omap_udc_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 .suspend = omap_udc_suspend,
3095 .resume = omap_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003096 .driver = {
3097 .owner = THIS_MODULE,
3098 .name = (char *) driver_name,
3099 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100};
3101
Felipe Balbidc1737c2012-05-29 14:33:30 +03003102module_platform_driver(udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104MODULE_DESCRIPTION(DRIVER_DESC);
3105MODULE_LICENSE("GPL");
Kay Sieversf34c32f2008-04-10 21:29:21 -07003106MODULE_ALIAS("platform:omap_udc");