blob: 2a4749c3eb3f7bd7679f768ca5cdab27de0db72f [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>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053038#include <linux/err.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070039#include <linux/prefetch.h>
Felipe Balbi80dd1352012-05-29 14:26:09 +030040#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/unaligned.h>
45#include <asm/mach-types.h>
46
Tony Lindgrence491cf2009-10-20 09:40:47 -070047#include <plat/dma.h>
Tony Lindgrenb924b202012-06-04 00:56:15 -070048
49#include <mach/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "omap_udc.h"
52
53#undef USB_TRACE
54
55/* bulk DMA seems to be behaving for both IN and OUT */
56#define USE_DMA
57
58/* ISO too */
59#define USE_ISO
60
61#define DRIVER_DESC "OMAP UDC driver"
62#define DRIVER_VERSION "4 October 2004"
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 Shayevitz77964b32012-06-04 13:35:25 +0300153 if (!_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
Felipe Balbi70617db2012-05-29 14:36:42 +0300274 INIT_LIST_HEAD(&req->queue);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return &req->req;
277}
278
279static void
280omap_free_request(struct usb_ep *ep, struct usb_request *_req)
281{
282 struct omap_req *req = container_of(_req, struct omap_req, req);
283
Felipe Balbi23673d72012-05-29 14:38:32 +0300284 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*-------------------------------------------------------------------------*/
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static void
290done(struct omap_ep *ep, struct omap_req *req, int status)
291{
Felipe Balbidd8e9382012-05-29 14:42:56 +0300292 struct omap_udc *udc = ep->udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 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
Felipe Balbidd8e9382012-05-29 14:42:56 +0300302 if (use_dma && ep->has_dma)
303 usb_gadget_unmap_request(&udc->gadget, &req->req,
304 (ep->bEndpointAddress & USB_DIR_IN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306#ifndef USB_TRACE
307 if (status && status != -ESHUTDOWN)
308#endif
309 VDBG("complete %s req %p stat %d len %u/%u\n",
310 ep->ep.name, &req->req, status,
311 req->req.actual, req->req.length);
312
313 /* don't modify queue heads during completion callback */
314 ep->stopped = 1;
315 spin_unlock(&ep->udc->lock);
316 req->req.complete(&ep->ep, &req->req);
317 spin_lock(&ep->udc->lock);
318 ep->stopped = stopped;
319}
320
321/*-------------------------------------------------------------------------*/
322
David Brownell313980c2005-04-11 15:38:25 -0700323#define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
324#define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326#define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
327#define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
328
David Brownelle6a6e472006-12-10 11:47:04 -0800329static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330write_packet(u8 *buf, struct omap_req *req, unsigned max)
331{
332 unsigned len;
333 u16 *wp;
334
335 len = min(req->req.length - req->req.actual, max);
336 req->req.actual += len;
337
338 max = len;
339 if (likely((((int)buf) & 1) == 0)) {
340 wp = (u16 *)buf;
341 while (max >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300342 omap_writew(*wp++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 max -= 2;
344 }
345 buf = (u8 *)wp;
346 }
347 while (max--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300348 omap_writeb(*buf++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return len;
350}
351
Felipe Balbi80dd1352012-05-29 14:26:09 +0300352/* FIXME change r/w fifo calling convention */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354
Felipe Balbi80dd1352012-05-29 14:26:09 +0300355/* return: 0 = still running, 1 = completed, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static int write_fifo(struct omap_ep *ep, struct omap_req *req)
357{
358 u8 *buf;
359 unsigned count;
360 int is_last;
361 u16 ep_stat;
362
363 buf = req->req.buf + req->req.actual;
364 prefetch(buf);
365
366 /* PIO-IN isn't double buffered except for iso */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300367 ep_stat = omap_readw(UDC_STAT_FLG);
David Brownell313980c2005-04-11 15:38:25 -0700368 if (ep_stat & UDC_FIFO_UNWRITABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return 0;
370
371 count = ep->ep.maxpacket;
372 count = write_packet(buf, req, count);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300373 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ep->ackwait = 1;
375
376 /* last packet is often short (sometimes a zlp) */
377 if (count != ep->ep.maxpacket)
378 is_last = 1;
379 else if (req->req.length == req->req.actual
380 && !req->req.zero)
381 is_last = 1;
382 else
383 is_last = 0;
384
385 /* NOTE: requests complete when all IN data is in a
386 * FIFO (or sometimes later, if a zlp was needed).
387 * Use usb_ep_fifo_status() where needed.
388 */
389 if (is_last)
390 done(ep, req, 0);
391 return is_last;
392}
393
David Brownelle6a6e472006-12-10 11:47:04 -0800394static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395read_packet(u8 *buf, struct omap_req *req, unsigned avail)
396{
397 unsigned len;
398 u16 *wp;
399
400 len = min(req->req.length - req->req.actual, avail);
401 req->req.actual += len;
402 avail = len;
403
404 if (likely((((int)buf) & 1) == 0)) {
405 wp = (u16 *)buf;
406 while (avail >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300407 *wp++ = omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 avail -= 2;
409 }
410 buf = (u8 *)wp;
411 }
412 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300413 *buf++ = omap_readb(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return len;
415}
416
Felipe Balbi80dd1352012-05-29 14:26:09 +0300417/* return: 0 = still running, 1 = queue empty, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static int read_fifo(struct omap_ep *ep, struct omap_req *req)
419{
420 u8 *buf;
421 unsigned count, avail;
422 int is_last;
423
424 buf = req->req.buf + req->req.actual;
425 prefetchw(buf);
426
427 for (;;) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300428 u16 ep_stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 is_last = 0;
431 if (ep_stat & FIFO_EMPTY) {
432 if (!ep->double_buf)
433 break;
434 ep->fnf = 1;
435 }
436 if (ep_stat & UDC_EP_HALTED)
437 break;
438
David Brownell313980c2005-04-11 15:38:25 -0700439 if (ep_stat & UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 avail = ep->ep.maxpacket;
441 else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300442 avail = omap_readw(UDC_RXFSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 ep->fnf = ep->double_buf;
444 }
445 count = read_packet(buf, req, avail);
446
447 /* partial packet reads may not be errors */
448 if (count < ep->ep.maxpacket) {
449 is_last = 1;
450 /* overflowed this request? flush extra data */
451 if (count != avail) {
452 req->req.status = -EOVERFLOW;
453 avail -= count;
454 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300455 omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 } else if (req->req.length == req->req.actual)
458 is_last = 1;
459 else
460 is_last = 0;
461
462 if (!ep->bEndpointAddress)
463 break;
464 if (is_last)
465 done(ep, req, 0);
466 break;
467 }
468 return is_last;
469}
470
471/*-------------------------------------------------------------------------*/
472
473static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
474{
475 dma_addr_t end;
476
477 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
478 * the last transfer's bytecount by more than a FIFO's worth.
479 */
480 if (cpu_is_omap15xx())
481 return 0;
482
Tony Lindgren0499bde2008-07-03 12:24:36 +0300483 end = omap_get_dma_src_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (end == ep->dma_counter)
485 return 0;
486
487 end |= start & (0xffff << 16);
488 if (end < start)
489 end += 0x10000;
490 return end - start;
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
494{
495 dma_addr_t end;
496
Tony Lindgren0499bde2008-07-03 12:24:36 +0300497 end = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (end == ep->dma_counter)
499 return 0;
500
501 end |= start & (0xffff << 16);
502 if (cpu_is_omap15xx())
503 end++;
504 if (end < start)
505 end += 0x10000;
506 return end - start;
507}
508
509
510/* Each USB transfer request using DMA maps to one or more DMA transfers.
511 * When DMA completion isn't request completion, the UDC continues with
512 * the next DMA transfer for that USB transfer.
513 */
514
515static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
516{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300517 u16 txdma_ctrl, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 unsigned length = req->req.length - req->req.actual;
519 const int sync_mode = cpu_is_omap15xx()
520 ? OMAP_DMA_SYNC_FRAME
521 : OMAP_DMA_SYNC_ELEMENT;
Kyungmin Park527ea732007-11-21 15:13:15 -0800522 int dma_trigger = 0;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* measure length in either bytes or packets */
David Brownell65111082005-04-28 13:52:31 -0700525 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
527 txdma_ctrl = UDC_TXN_EOT | length;
528 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
Kyungmin Park527ea732007-11-21 15:13:15 -0800529 length, 1, sync_mode, dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } else {
531 length = min(length / ep->maxpacket,
532 (unsigned) UDC_TXN_TSC + 1);
David Brownelle6a6e472006-12-10 11:47:04 -0800533 txdma_ctrl = length;
David Brownell65111082005-04-28 13:52:31 -0700534 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
David Brownelle6a6e472006-12-10 11:47:04 -0800535 ep->ep.maxpacket >> 1, length, sync_mode,
Kyungmin Park527ea732007-11-21 15:13:15 -0800536 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 length *= ep->maxpacket;
538 }
539 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800540 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
541 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 omap_start_dma(ep->lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300544 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300545 w = omap_readw(UDC_DMA_IRQ_EN);
546 w |= UDC_TX_DONE_IE(ep->dma_channel);
547 omap_writew(w, UDC_DMA_IRQ_EN);
548 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 req->dma_bytes = length;
550}
551
552static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
553{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300554 u16 w;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (status == 0) {
557 req->req.actual += req->dma_bytes;
558
559 /* return if this request needs to send data or zlp */
560 if (req->req.actual < req->req.length)
561 return;
562 if (req->req.zero
563 && req->dma_bytes != 0
564 && (req->req.actual % ep->maxpacket) == 0)
565 return;
566 } else
567 req->req.actual += dma_src_len(ep, req->req.dma
568 + req->req.actual);
569
570 /* tx completion */
571 omap_stop_dma(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300572 w = omap_readw(UDC_DMA_IRQ_EN);
573 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
574 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 done(ep, req, status);
576}
577
578static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
579{
Kyungmin Park527ea732007-11-21 15:13:15 -0800580 unsigned packets = req->req.length - req->req.actual;
581 int dma_trigger = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300582 u16 w;
Kyungmin Park527ea732007-11-21 15:13:15 -0800583
Tony Lindgrenae372572012-05-21 12:01:11 -0700584 /* set up this DMA transfer, enable the fifo, start */
585 packets /= ep->ep.maxpacket;
586 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
587 req->dma_bytes = packets * ep->ep.maxpacket;
588 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
589 ep->ep.maxpacket >> 1, packets,
590 OMAP_DMA_SYNC_ELEMENT,
591 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800593 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
594 0, 0);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300595 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300597 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
598 w = omap_readw(UDC_DMA_IRQ_EN);
599 w |= UDC_RX_EOT_IE(ep->dma_channel);
600 omap_writew(w, UDC_DMA_IRQ_EN);
601 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
602 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
604 omap_start_dma(ep->lch);
605}
606
607static void
David Brownellcb97c5c2005-10-16 15:06:51 -0700608finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300610 u16 count, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (status == 0)
613 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
614 count = dma_dest_len(ep, req->req.dma + req->req.actual);
615 count += req->req.actual;
David Brownellcb97c5c2005-10-16 15:06:51 -0700616 if (one)
617 count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (count <= req->req.length)
619 req->req.actual = count;
620
621 if (count != req->dma_bytes || status)
622 omap_stop_dma(ep->lch);
623
624 /* if this wasn't short, request may need another transfer */
625 else if (req->req.actual < req->req.length)
626 return;
627
628 /* rx completion */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300629 w = omap_readw(UDC_DMA_IRQ_EN);
630 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
631 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 done(ep, req, status);
633}
634
635static void dma_irq(struct omap_udc *udc, u16 irq_src)
636{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300637 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct omap_ep *ep;
639 struct omap_req *req;
640
641 /* IN dma: tx to host */
642 if (irq_src & UDC_TXN_DONE) {
643 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
644 ep->irqs++;
645 /* can see TXN_DONE after dma abort */
646 if (!list_empty(&ep->queue)) {
647 req = container_of(ep->queue.next,
648 struct omap_req, queue);
649 finish_in_dma(ep, req, 0);
650 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300651 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Felipe Balbi80dd1352012-05-29 14:26:09 +0300653 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 req = container_of(ep->queue.next,
655 struct omap_req, queue);
656 next_in_dma(ep, req);
657 }
658 }
659
660 /* OUT dma: rx from host */
661 if (irq_src & UDC_RXN_EOT) {
662 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
663 ep->irqs++;
664 /* can see RXN_EOT after dma abort */
665 if (!list_empty(&ep->queue)) {
666 req = container_of(ep->queue.next,
667 struct omap_req, queue);
David Brownellcb97c5c2005-10-16 15:06:51 -0700668 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300670 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Felipe Balbi80dd1352012-05-29 14:26:09 +0300672 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 req = container_of(ep->queue.next,
674 struct omap_req, queue);
675 next_out_dma(ep, req);
676 }
677 }
678
679 if (irq_src & UDC_RXN_CNT) {
680 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
681 ep->irqs++;
682 /* omap15xx does this unasked... */
683 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300684 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686}
687
688static void dma_error(int lch, u16 ch_status, void *data)
689{
690 struct omap_ep *ep = data;
691
692 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700693 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
695
696 /* complete current transfer ... */
697}
698
699static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
700{
701 u16 reg;
702 int status, restart, is_in;
Kyungmin Park527ea732007-11-21 15:13:15 -0800703 int dma_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 is_in = ep->bEndpointAddress & USB_DIR_IN;
706 if (is_in)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300707 reg = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300709 reg = omap_readw(UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700710 reg |= UDC_DMA_REQ; /* "pulse" activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 ep->dma_channel = 0;
713 ep->lch = -1;
714 if (channel == 0 || channel > 3) {
715 if ((reg & 0x0f00) == 0)
716 channel = 3;
717 else if ((reg & 0x00f0) == 0)
718 channel = 2;
719 else if ((reg & 0x000f) == 0) /* preferred for ISO */
720 channel = 1;
721 else {
722 status = -EMLINK;
723 goto just_restart;
724 }
725 }
726 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
727 ep->dma_channel = channel;
728
729 if (is_in) {
Tony Lindgrenae372572012-05-21 12:01:11 -0700730 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800731 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ep->ep.name, dma_error, ep, &ep->lch);
733 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300734 omap_writew(reg, UDC_TXDMA_CFG);
Kyungmin Park527ea732007-11-21 15:13:15 -0800735 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700736 omap_set_dma_src_burst_mode(ep->lch,
737 OMAP_DMA_DATA_BURST_4);
738 omap_set_dma_src_data_pack(ep->lch, 1);
739 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 omap_set_dma_dest_params(ep->lch,
741 OMAP_DMA_PORT_TIPB,
742 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700743 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800744 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 } else {
Tony Lindgrenae372572012-05-21 12:01:11 -0700747 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800748 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ep->ep.name, dma_error, ep, &ep->lch);
750 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300751 omap_writew(reg, UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700752 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 omap_set_dma_src_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);
Kyungmin Park527ea732007-11-21 15:13:15 -0800758 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700759 omap_set_dma_dest_burst_mode(ep->lch,
760 OMAP_DMA_DATA_BURST_4);
761 omap_set_dma_dest_data_pack(ep->lch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 }
764 if (status)
765 ep->dma_channel = 0;
766 else {
767 ep->has_dma = 1;
768 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
769
770 /* channel type P: hw synch (fifo) */
Tony Lindgrenae372572012-05-21 12:01:11 -0700771 if (!cpu_is_omap15xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300772 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
775just_restart:
776 /* restart any queue, even if the claim failed */
777 restart = !ep->stopped && !list_empty(&ep->queue);
778
779 if (status)
780 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
781 restart ? " (restart)" : "");
782 else
783 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
784 is_in ? 't' : 'r',
785 ep->dma_channel - 1, ep->lch,
786 restart ? " (restart)" : "");
787
788 if (restart) {
789 struct omap_req *req;
790 req = container_of(ep->queue.next, struct omap_req, queue);
791 if (ep->has_dma)
792 (is_in ? next_in_dma : next_out_dma)(ep, req);
793 else {
794 use_ep(ep, UDC_EP_SEL);
795 (is_in ? write_fifo : read_fifo)(ep, req);
796 deselect_ep();
797 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300798 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ep->ackwait = 1 + ep->double_buf;
800 }
801 /* IN: 6 wait states before it'll tx */
802 }
803 }
804}
805
806static void dma_channel_release(struct omap_ep *ep)
807{
808 int shift = 4 * (ep->dma_channel - 1);
809 u16 mask = 0x0f << shift;
810 struct omap_req *req;
811 int active;
812
813 /* abort any active usb transfer request */
814 if (!list_empty(&ep->queue))
815 req = container_of(ep->queue.next, struct omap_req, queue);
816 else
David Brownell313980c2005-04-11 15:38:25 -0700817 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Tony Lindgren0499bde2008-07-03 12:24:36 +0300819 active = omap_get_dma_active_status(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
822 active ? "active" : "idle",
823 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
824 ep->dma_channel - 1, req);
825
David Brownell65111082005-04-28 13:52:31 -0700826 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
827 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
828 */
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /* wait till current packet DMA finishes, and fifo empties */
831 if (ep->bEndpointAddress & USB_DIR_IN) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300832 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
833 UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (req) {
836 finish_in_dma(ep, req, -ECONNRESET);
837
838 /* clear FIFO; hosts probably won't empty it */
839 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300840 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 deselect_ep();
842 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300843 while (omap_readw(UDC_TXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 udelay(10);
845 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300846 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
847 UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* dma empties the fifo */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300850 while (omap_readw(UDC_RXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 udelay(10);
852 if (req)
David Brownellcb97c5c2005-10-16 15:06:51 -0700853 finish_out_dma(ep, req, -ECONNRESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855 omap_free_dma(ep->lch);
856 ep->dma_channel = 0;
857 ep->lch = -1;
858 /* has_dma still set, till endpoint is fully quiesced */
859}
860
861
862/*-------------------------------------------------------------------------*/
863
864static int
Al Viro55016f12005-10-21 03:21:58 -0400865omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
868 struct omap_req *req = container_of(_req, struct omap_req, req);
869 struct omap_udc *udc;
870 unsigned long flags;
871 int is_iso = 0;
872
873 /* catch various bogus parameters */
874 if (!_req || !req->req.complete || !req->req.buf
875 || !list_empty(&req->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800876 DBG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -EINVAL;
878 }
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200879 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800880 DBG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EINVAL;
882 }
883 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
884 if (req->req.length > ep->ep.maxpacket)
885 return -EMSGSIZE;
886 is_iso = 1;
887 }
888
889 /* this isn't bogus, but OMAP DMA isn't the only hardware to
890 * have a hard time with partial packet reads... reject it.
891 */
892 if (use_dma
893 && ep->has_dma
894 && ep->bEndpointAddress != 0
895 && (ep->bEndpointAddress & USB_DIR_IN) == 0
896 && (req->req.length % ep->ep.maxpacket) != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800897 DBG("%s, no partial packet OUT reads\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -EMSGSIZE;
899 }
900
901 udc = ep->udc;
902 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
903 return -ESHUTDOWN;
904
Felipe Balbidd8e9382012-05-29 14:42:56 +0300905 if (use_dma && ep->has_dma)
906 usb_gadget_map_request(&udc->gadget, &req->req,
907 (ep->bEndpointAddress & USB_DIR_IN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 VDBG("%s queue req %p, len %d buf %p\n",
910 ep->ep.name, _req, _req->length, _req->buf);
911
912 spin_lock_irqsave(&udc->lock, flags);
913
914 req->req.status = -EINPROGRESS;
915 req->req.actual = 0;
916
917 /* maybe kickstart non-iso i/o queues */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300918 if (is_iso) {
919 u16 w;
920
921 w = omap_readw(UDC_IRQ_EN);
922 w |= UDC_SOF_IE;
923 omap_writew(w, UDC_IRQ_EN);
924 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int is_in;
926
927 if (ep->bEndpointAddress == 0) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300928 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 spin_unlock_irqrestore(&udc->lock, flags);
930 return -EL2HLT;
931 }
932
933 /* empty DATA stage? */
934 is_in = udc->ep0_in;
935 if (!req->req.length) {
936
937 /* chip became CONFIGURED or ADDRESSED
938 * earlier; drivers may already have queued
939 * requests to non-control endpoints
940 */
941 if (udc->ep0_set_config) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300942 u16 irq_en = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
945 if (!udc->ep0_reset_config)
946 irq_en |= UDC_EPN_RX_IE
947 | UDC_EPN_TX_IE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300948 omap_writew(irq_en, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
David Brownell313980c2005-04-11 15:38:25 -0700951 /* STATUS for zero length DATA stages is
952 * always an IN ... even for IN transfers,
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800953 * a weird case which seem to stall OMAP.
David Brownell313980c2005-04-11 15:38:25 -0700954 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300955 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
956 UDC_EP_NUM);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300957 omap_writew(UDC_CLR_EP, UDC_CTRL);
958 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
959 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* cleanup */
962 udc->ep0_pending = 0;
963 done(ep, req, 0);
David Brownell313980c2005-04-11 15:38:25 -0700964 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /* non-empty DATA stage */
967 } else if (is_in) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300968 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
969 UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 } else {
971 if (udc->ep0_setup)
972 goto irq_wait;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300973 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975 } else {
976 is_in = ep->bEndpointAddress & USB_DIR_IN;
977 if (!ep->has_dma)
978 use_ep(ep, UDC_EP_SEL);
979 /* if ISO: SOF IRQs must be enabled/disabled! */
980 }
981
982 if (ep->has_dma)
983 (is_in ? next_in_dma : next_out_dma)(ep, req);
984 else if (req) {
985 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
David Brownell313980c2005-04-11 15:38:25 -0700986 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 deselect_ep();
988 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300989 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ep->ackwait = 1 + ep->double_buf;
991 }
992 /* IN: 6 wait states before it'll tx */
993 }
994 }
995
996irq_wait:
997 /* irq handler advances the queue */
David Brownell313980c2005-04-11 15:38:25 -0700998 if (req != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 list_add_tail(&req->queue, &ep->queue);
1000 spin_unlock_irqrestore(&udc->lock, flags);
1001
1002 return 0;
1003}
1004
1005static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1006{
1007 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1008 struct omap_req *req;
1009 unsigned long flags;
1010
1011 if (!_ep || !_req)
1012 return -EINVAL;
1013
1014 spin_lock_irqsave(&ep->udc->lock, flags);
1015
1016 /* make sure it's actually queued on this endpoint */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001017 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 if (&req->req == _req)
1019 break;
1020 }
1021 if (&req->req != _req) {
1022 spin_unlock_irqrestore(&ep->udc->lock, flags);
1023 return -EINVAL;
1024 }
1025
1026 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1027 int channel = ep->dma_channel;
1028
1029 /* releasing the channel cancels the request,
1030 * reclaiming the channel restarts the queue
1031 */
1032 dma_channel_release(ep);
1033 dma_channel_claim(ep, channel);
David Brownelle6a6e472006-12-10 11:47:04 -08001034 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 done(ep, req, -ECONNRESET);
1036 spin_unlock_irqrestore(&ep->udc->lock, flags);
1037 return 0;
1038}
1039
1040/*-------------------------------------------------------------------------*/
1041
1042static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1043{
1044 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1045 unsigned long flags;
1046 int status = -EOPNOTSUPP;
1047
1048 spin_lock_irqsave(&ep->udc->lock, flags);
1049
1050 /* just use protocol stalls for ep0; real halts are annoying */
1051 if (ep->bEndpointAddress == 0) {
1052 if (!ep->udc->ep0_pending)
1053 status = -EINVAL;
1054 else if (value) {
1055 if (ep->udc->ep0_set_config) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001056 WARNING("error changing config?\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001057 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001059 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 ep->udc->ep0_pending = 0;
1061 status = 0;
1062 } else /* NOP */
1063 status = 0;
1064
1065 /* otherwise, all active non-ISO endpoints can halt */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001066 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /* IN endpoints must already be idle */
1069 if ((ep->bEndpointAddress & USB_DIR_IN)
David Brownelle6a6e472006-12-10 11:47:04 -08001070 && !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 status = -EAGAIN;
1072 goto done;
1073 }
1074
1075 if (value) {
1076 int channel;
1077
1078 if (use_dma && ep->dma_channel
1079 && !list_empty(&ep->queue)) {
1080 channel = ep->dma_channel;
1081 dma_channel_release(ep);
1082 } else
1083 channel = 0;
1084
1085 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001086 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1087 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 status = 0;
1089 } else
1090 status = -EAGAIN;
1091 deselect_ep();
1092
1093 if (channel)
1094 dma_channel_claim(ep, channel);
1095 } else {
1096 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001097 omap_writew(ep->udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 ep->ackwait = 0;
1099 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001100 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 ep->ackwait = 1 + ep->double_buf;
1102 }
1103 }
1104 }
1105done:
1106 VDBG("%s %s halt stat %d\n", ep->ep.name,
1107 value ? "set" : "clear", status);
1108
1109 spin_unlock_irqrestore(&ep->udc->lock, flags);
1110 return status;
1111}
1112
1113static struct usb_ep_ops omap_ep_ops = {
1114 .enable = omap_ep_enable,
1115 .disable = omap_ep_disable,
1116
1117 .alloc_request = omap_alloc_request,
1118 .free_request = omap_free_request,
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 .queue = omap_ep_queue,
1121 .dequeue = omap_ep_dequeue,
1122
1123 .set_halt = omap_ep_set_halt,
Felipe Balbi80dd1352012-05-29 14:26:09 +03001124 /* fifo_status ... report bytes in fifo */
1125 /* fifo_flush ... flush fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126};
1127
1128/*-------------------------------------------------------------------------*/
1129
1130static int omap_get_frame(struct usb_gadget *gadget)
1131{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001132 u16 sof = omap_readw(UDC_SOF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1134}
1135
1136static int omap_wakeup(struct usb_gadget *gadget)
1137{
1138 struct omap_udc *udc;
1139 unsigned long flags;
1140 int retval = -EHOSTUNREACH;
1141
1142 udc = container_of(gadget, struct omap_udc, gadget);
1143
1144 spin_lock_irqsave(&udc->lock, flags);
1145 if (udc->devstat & UDC_SUS) {
1146 /* NOTE: OTG spec erratum says that OTG devices may
1147 * issue wakeups without host enable.
1148 */
1149 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1150 DBG("remote wakeup...\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001151 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 retval = 0;
1153 }
1154
1155 /* NOTE: non-OTG systems may use SRP TOO... */
1156 } else if (!(udc->devstat & UDC_ATT)) {
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301157 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001158 retval = otg_start_srp(udc->transceiver->otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160 spin_unlock_irqrestore(&udc->lock, flags);
1161
1162 return retval;
1163}
1164
1165static int
1166omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1167{
1168 struct omap_udc *udc;
1169 unsigned long flags;
1170 u16 syscon1;
1171
1172 udc = container_of(gadget, struct omap_udc, gadget);
1173 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001174 syscon1 = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (is_selfpowered)
1176 syscon1 |= UDC_SELF_PWR;
1177 else
1178 syscon1 &= ~UDC_SELF_PWR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001179 omap_writew(syscon1, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_unlock_irqrestore(&udc->lock, flags);
1181
1182 return 0;
1183}
1184
1185static int can_pullup(struct omap_udc *udc)
1186{
1187 return udc->driver && udc->softconnect && udc->vbus_active;
1188}
1189
1190static void pullup_enable(struct omap_udc *udc)
1191{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001192 u16 w;
1193
1194 w = omap_readw(UDC_SYSCON1);
1195 w |= UDC_PULLUP_EN;
1196 omap_writew(w, UDC_SYSCON1);
1197 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1198 u32 l;
1199
1200 l = omap_readl(OTG_CTRL);
1201 l |= OTG_BSESSVLD;
1202 omap_writel(l, OTG_CTRL);
1203 }
1204 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
1207static void pullup_disable(struct omap_udc *udc)
1208{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001209 u16 w;
1210
1211 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1212 u32 l;
1213
1214 l = omap_readl(OTG_CTRL);
1215 l &= ~OTG_BSESSVLD;
1216 omap_writel(l, OTG_CTRL);
1217 }
1218 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1219 w = omap_readw(UDC_SYSCON1);
1220 w &= ~UDC_PULLUP_EN;
1221 omap_writew(w, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
David Brownelle6a6e472006-12-10 11:47:04 -08001224static struct omap_udc *udc;
1225
1226static void omap_udc_enable_clock(int enable)
1227{
1228 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1229 return;
1230
1231 if (enable) {
1232 clk_enable(udc->dc_clk);
1233 clk_enable(udc->hhc_clk);
1234 udelay(100);
1235 } else {
1236 clk_disable(udc->hhc_clk);
1237 clk_disable(udc->dc_clk);
1238 }
1239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
1242 * Called by whatever detects VBUS sessions: external transceiver
1243 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1244 */
1245static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1246{
1247 struct omap_udc *udc;
1248 unsigned long flags;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001249 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 udc = container_of(gadget, struct omap_udc, gadget);
1252 spin_lock_irqsave(&udc->lock, flags);
1253 VDBG("VBUS %s\n", is_active ? "on" : "off");
1254 udc->vbus_active = (is_active != 0);
1255 if (cpu_is_omap15xx()) {
1256 /* "software" detect, ignored if !VBUS_MODE_1510 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001257 l = omap_readl(FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (is_active)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001259 l |= VBUS_CTRL_1510;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001261 l &= ~VBUS_CTRL_1510;
1262 omap_writel(l, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
David Brownelle6a6e472006-12-10 11:47:04 -08001264 if (udc->dc_clk != NULL && is_active) {
1265 if (!udc->clk_requested) {
1266 omap_udc_enable_clock(1);
1267 udc->clk_requested = 1;
1268 }
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (can_pullup(udc))
1271 pullup_enable(udc);
1272 else
1273 pullup_disable(udc);
David Brownelle6a6e472006-12-10 11:47:04 -08001274 if (udc->dc_clk != NULL && !is_active) {
1275 if (udc->clk_requested) {
1276 omap_udc_enable_clock(0);
1277 udc->clk_requested = 0;
1278 }
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 spin_unlock_irqrestore(&udc->lock, flags);
1281 return 0;
1282}
1283
1284static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1285{
1286 struct omap_udc *udc;
1287
1288 udc = container_of(gadget, struct omap_udc, gadget);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301289 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001290 return usb_phy_set_power(udc->transceiver, mA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EOPNOTSUPP;
1292}
1293
1294static int omap_pullup(struct usb_gadget *gadget, int is_on)
1295{
1296 struct omap_udc *udc;
1297 unsigned long flags;
1298
1299 udc = container_of(gadget, struct omap_udc, gadget);
1300 spin_lock_irqsave(&udc->lock, flags);
1301 udc->softconnect = (is_on != 0);
1302 if (can_pullup(udc))
1303 pullup_enable(udc);
1304 else
1305 pullup_disable(udc);
1306 spin_unlock_irqrestore(&udc->lock, flags);
1307 return 0;
1308}
1309
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001310static int omap_udc_start(struct usb_gadget_driver *driver,
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02001311 int (*bind)(struct usb_gadget *, struct usb_gadget_driver *));
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001312static int omap_udc_stop(struct usb_gadget_driver *driver);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static struct usb_gadget_ops omap_gadget_ops = {
1315 .get_frame = omap_get_frame,
1316 .wakeup = omap_wakeup,
1317 .set_selfpowered = omap_set_selfpowered,
1318 .vbus_session = omap_vbus_session,
1319 .vbus_draw = omap_vbus_draw,
1320 .pullup = omap_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001321 .start = omap_udc_start,
1322 .stop = omap_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323};
1324
1325/*-------------------------------------------------------------------------*/
1326
1327/* dequeue ALL requests; caller holds udc->lock */
1328static void nuke(struct omap_ep *ep, int status)
1329{
1330 struct omap_req *req;
1331
1332 ep->stopped = 1;
1333
1334 if (use_dma && ep->dma_channel)
1335 dma_channel_release(ep);
1336
1337 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001338 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001340 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 while (!list_empty(&ep->queue)) {
1343 req = list_entry(ep->queue.next, struct omap_req, queue);
1344 done(ep, req, status);
1345 }
1346}
1347
1348/* caller holds udc->lock */
1349static void udc_quiesce(struct omap_udc *udc)
1350{
1351 struct omap_ep *ep;
1352
1353 udc->gadget.speed = USB_SPEED_UNKNOWN;
1354 nuke(&udc->ep[0], -ESHUTDOWN);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001355 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 nuke(ep, -ESHUTDOWN);
1357}
1358
1359/*-------------------------------------------------------------------------*/
1360
1361static void update_otg(struct omap_udc *udc)
1362{
1363 u16 devstat;
1364
David Brownell9cfbba72007-10-26 13:42:18 -07001365 if (!gadget_is_otg(&udc->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return;
1367
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001368 if (omap_readl(OTG_CTRL) & OTG_ID)
1369 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 else
1371 devstat = 0;
1372
1373 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1374 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1375 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1376
1377 /* Enable HNP early, avoiding races on suspend irq path.
1378 * ASSUMES OTG state machine B_BUS_REQ input is true.
1379 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001380 if (udc->gadget.b_hnp_enable) {
1381 u32 l;
1382
1383 l = omap_readl(OTG_CTRL);
1384 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1385 l &= ~OTG_PULLUP;
1386 omap_writel(l, OTG_CTRL);
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1391{
1392 struct omap_ep *ep0 = &udc->ep[0];
David Brownell313980c2005-04-11 15:38:25 -07001393 struct omap_req *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 ep0->irqs++;
1396
1397 /* Clear any pending requests and then scrub any rx/tx state
1398 * before starting to handle the SETUP request.
1399 */
1400 if (irq_src & UDC_SETUP) {
1401 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1402
1403 nuke(ep0, 0);
1404 if (ack) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001405 omap_writew(ack, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 irq_src = UDC_SETUP;
1407 }
1408 }
1409
David Brownelle6a6e472006-12-10 11:47:04 -08001410 /* IN/OUT packets mean we're in the DATA or STATUS stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 * This driver uses only uses protocol stalls (ep0 never halts),
1412 * and if we got this far the gadget driver already had a
1413 * chance to stall. Tries to be forgiving of host oddities.
1414 *
1415 * NOTE: the last chance gadget drivers have to stall control
1416 * requests is during their request completion callback.
1417 */
1418 if (!list_empty(&ep0->queue))
1419 req = container_of(ep0->queue.next, struct omap_req, queue);
1420
1421 /* IN == TX to host */
1422 if (irq_src & UDC_EP0_TX) {
1423 int stat;
1424
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001425 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1426 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1427 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (stat & UDC_ACK) {
1429 if (udc->ep0_in) {
1430 /* write next IN packet from response,
1431 * or set up the status stage.
1432 */
1433 if (req)
1434 stat = write_fifo(ep0, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001435 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!req && udc->ep0_pending) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001437 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1438 omap_writew(UDC_CLR_EP, UDC_CTRL);
1439 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1440 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 udc->ep0_pending = 0;
1442 } /* else: 6 wait states before it'll tx */
1443 } else {
1444 /* ack status stage of OUT transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001445 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (req)
1447 done(ep0, req, 0);
1448 }
David Brownell313980c2005-04-11 15:38:25 -07001449 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001451 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1452 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001454 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 }
1457
1458 /* OUT == RX from host */
1459 if (irq_src & UDC_EP0_RX) {
1460 int stat;
1461
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001462 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1463 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1464 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (stat & UDC_ACK) {
1466 if (!udc->ep0_in) {
1467 stat = 0;
1468 /* read next OUT packet of request, maybe
1469 * reactiviting the fifo; stall on errors.
1470 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001471 stat = read_fifo(ep0, req);
1472 if (!req || stat < 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001473 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 udc->ep0_pending = 0;
1475 stat = 0;
1476 } else if (stat == 0)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001477 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1478 omap_writew(0, UDC_EP_NUM);
David Brownelle6a6e472006-12-10 11:47:04 -08001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* activate status stage */
1481 if (stat == 1) {
1482 done(ep0, req, 0);
1483 /* that may have STALLed ep0... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001484 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1485 UDC_EP_NUM);
1486 omap_writew(UDC_CLR_EP, UDC_CTRL);
1487 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1488 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 udc->ep0_pending = 0;
1490 }
1491 } else {
1492 /* ack status stage of IN transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001493 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (req)
1495 done(ep0, req, 0);
1496 }
1497 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001498 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1499 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001501 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 }
1504
1505 /* SETUP starts all control transfers */
1506 if (irq_src & UDC_SETUP) {
1507 union u {
1508 u16 word[4];
1509 struct usb_ctrlrequest r;
1510 } u;
1511 int status = -EINVAL;
1512 struct omap_ep *ep;
1513
1514 /* read the (latest) SETUP message */
1515 do {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001516 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 /* two bytes at a time */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001518 u.word[0] = omap_readw(UDC_DATA);
1519 u.word[1] = omap_readw(UDC_DATA);
1520 u.word[2] = omap_readw(UDC_DATA);
1521 u.word[3] = omap_readw(UDC_DATA);
1522 omap_writew(0, UDC_EP_NUM);
1523 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Brownell01ee7d72007-05-25 20:40:14 -07001525#define w_value le16_to_cpu(u.r.wValue)
1526#define w_index le16_to_cpu(u.r.wIndex)
1527#define w_length le16_to_cpu(u.r.wLength)
David Brownell65111082005-04-28 13:52:31 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* Delegate almost all control requests to the gadget driver,
1530 * except for a handful of ch9 status/feature requests that
1531 * hardware doesn't autodecode _and_ the gadget API hides.
1532 */
1533 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1534 udc->ep0_set_config = 0;
1535 udc->ep0_pending = 1;
1536 ep0->stopped = 0;
1537 ep0->ackwait = 0;
1538 switch (u.r.bRequest) {
1539 case USB_REQ_SET_CONFIGURATION:
1540 /* udc needs to know when ep != 0 is valid */
1541 if (u.r.bRequestType != USB_RECIP_DEVICE)
1542 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001543 if (w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto do_stall;
1545 udc->ep0_set_config = 1;
David Brownell65111082005-04-28 13:52:31 -07001546 udc->ep0_reset_config = (w_value == 0);
1547 VDBG("set config %d\n", w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /* update udc NOW since gadget driver may start
1550 * queueing requests immediately; clear config
1551 * later if it fails the request.
1552 */
1553 if (udc->ep0_reset_config)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001554 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001556 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 update_otg(udc);
1558 goto delegate;
1559 case USB_REQ_CLEAR_FEATURE:
1560 /* clear endpoint halt */
1561 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1562 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001563 if (w_value != USB_ENDPOINT_HALT
1564 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001566 ep = &udc->ep[w_index & 0xf];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (ep != ep0) {
David Brownell65111082005-04-28 13:52:31 -07001568 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 ep += 16;
1570 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001571 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 goto do_stall;
1573 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001574 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 ep->ackwait = 0;
1576 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001577 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ep->ackwait = 1 + ep->double_buf;
1579 }
David Brownell313980c2005-04-11 15:38:25 -07001580 /* NOTE: assumes the host behaves sanely,
1581 * only clearing real halts. Else we may
1582 * need to kill pending transfers and then
1583 * restart the queue... very messy for DMA!
1584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586 VDBG("%s halt cleared by host\n", ep->name);
1587 goto ep0out_status_stage;
1588 case USB_REQ_SET_FEATURE:
1589 /* set endpoint halt */
1590 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1591 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001592 if (w_value != USB_ENDPOINT_HALT
1593 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001595 ep = &udc->ep[w_index & 0xf];
1596 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ep += 16;
1598 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001599 || ep == ep0 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto do_stall;
1601 if (use_dma && ep->has_dma) {
1602 /* this has rude side-effects (aborts) and
1603 * can't really work if DMA-IN is active
1604 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001605 DBG("%s host set_halt, NYET\n", ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto do_stall;
1607 }
1608 use_ep(ep, 0);
1609 /* can't halt if fifo isn't empty... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001610 omap_writew(UDC_CLR_EP, UDC_CTRL);
1611 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 VDBG("%s halted by host\n", ep->name);
1613ep0out_status_stage:
1614 status = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001615 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1616 omap_writew(UDC_CLR_EP, UDC_CTRL);
1617 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1618 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 udc->ep0_pending = 0;
1620 break;
1621 case USB_REQ_GET_STATUS:
David Brownell8a3c1f52007-03-21 12:26:32 -07001622 /* USB_ENDPOINT_HALT status? */
1623 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1624 goto intf_status;
1625
1626 /* ep0 never stalls */
1627 if (!(w_index & 0xf))
1628 goto zero_status;
1629
1630 /* only active endpoints count */
1631 ep = &udc->ep[w_index & 0xf];
1632 if (w_index & USB_DIR_IN)
1633 ep += 16;
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001634 if (!ep->ep.desc)
David Brownell8a3c1f52007-03-21 12:26:32 -07001635 goto do_stall;
1636
1637 /* iso never stalls */
1638 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1639 goto zero_status;
1640
1641 /* FIXME don't assume non-halted endpoints!! */
1642 ERR("%s status, can't report\n", ep->ep.name);
1643 goto do_stall;
1644
1645intf_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /* return interface status. if we were pedantic,
1647 * we'd detect non-existent interfaces, and stall.
1648 */
1649 if (u.r.bRequestType
1650 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1651 goto delegate;
David Brownell8a3c1f52007-03-21 12:26:32 -07001652
1653zero_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /* return two zero bytes */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001655 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1656 omap_writew(0, UDC_DATA);
1657 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1658 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 status = 0;
David Brownell65111082005-04-28 13:52:31 -07001660 VDBG("GET_STATUS, interface %d\n", w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /* next, status stage */
1662 break;
1663 default:
1664delegate:
1665 /* activate the ep0out fifo right away */
David Brownell65111082005-04-28 13:52:31 -07001666 if (!udc->ep0_in && w_length) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001667 omap_writew(0, UDC_EP_NUM);
1668 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
1671 /* gadget drivers see class/vendor specific requests,
1672 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1673 * and more
1674 */
1675 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1676 u.r.bRequestType, u.r.bRequest,
David Brownell65111082005-04-28 13:52:31 -07001677 w_value, w_index, w_length);
1678
1679#undef w_value
1680#undef w_index
1681#undef w_length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 /* The gadget driver may return an error here,
1684 * causing an immediate protocol stall.
1685 *
1686 * Else it must issue a response, either queueing a
1687 * response buffer for the DATA stage, or halting ep0
1688 * (causing a protocol stall, not a real halt). A
1689 * zero length buffer means no DATA stage.
1690 *
1691 * It's fine to issue that response after the setup()
1692 * call returns, and this IRQ was handled.
1693 */
1694 udc->ep0_setup = 1;
1695 spin_unlock(&udc->lock);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001696 status = udc->driver->setup(&udc->gadget, &u.r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 spin_lock(&udc->lock);
1698 udc->ep0_setup = 0;
1699 }
1700
1701 if (status < 0) {
1702do_stall:
1703 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1704 u.r.bRequestType, u.r.bRequest, status);
1705 if (udc->ep0_set_config) {
1706 if (udc->ep0_reset_config)
Arjan van de Venb6c63932008-07-25 01:45:52 -07001707 WARNING("error resetting config?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001709 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001711 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 udc->ep0_pending = 0;
1713 }
1714 }
1715}
1716
1717/*-------------------------------------------------------------------------*/
1718
1719#define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1720
1721static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1722{
1723 u16 devstat, change;
1724
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001725 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 change = devstat ^ udc->devstat;
1727 udc->devstat = devstat;
1728
1729 if (change & (UDC_USB_RESET|UDC_ATT)) {
1730 udc_quiesce(udc);
1731
1732 if (change & UDC_ATT) {
1733 /* driver for any external transceiver will
1734 * have called omap_vbus_session() already
1735 */
1736 if (devstat & UDC_ATT) {
1737 udc->gadget.speed = USB_SPEED_FULL;
1738 VDBG("connect\n");
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301739 if (IS_ERR_OR_NULL(udc->transceiver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 pullup_enable(udc);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001741 /* if (driver->connect) call it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1743 udc->gadget.speed = USB_SPEED_UNKNOWN;
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301744 if (IS_ERR_OR_NULL(udc->transceiver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 pullup_disable(udc);
1746 DBG("disconnect, gadget %s\n",
1747 udc->driver->driver.name);
1748 if (udc->driver->disconnect) {
1749 spin_unlock(&udc->lock);
1750 udc->driver->disconnect(&udc->gadget);
1751 spin_lock(&udc->lock);
1752 }
1753 }
1754 change &= ~UDC_ATT;
1755 }
1756
1757 if (change & UDC_USB_RESET) {
1758 if (devstat & UDC_USB_RESET) {
1759 VDBG("RESET=1\n");
1760 } else {
1761 udc->gadget.speed = USB_SPEED_FULL;
1762 INFO("USB reset done, gadget %s\n",
1763 udc->driver->driver.name);
1764 /* ep0 traffic is legal from now on */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001765 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1766 UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 }
1768 change &= ~UDC_USB_RESET;
1769 }
1770 }
1771 if (change & UDC_SUS) {
1772 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03001773 /* FIXME tell isp1301 to suspend/resume (?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 if (devstat & UDC_SUS) {
1775 VDBG("suspend\n");
1776 update_otg(udc);
1777 /* HNP could be under way already */
1778 if (udc->gadget.speed == USB_SPEED_FULL
1779 && udc->driver->suspend) {
1780 spin_unlock(&udc->lock);
1781 udc->driver->suspend(&udc->gadget);
1782 spin_lock(&udc->lock);
1783 }
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301784 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001785 usb_phy_set_suspend(
1786 udc->transceiver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 } else {
1788 VDBG("resume\n");
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301789 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001790 usb_phy_set_suspend(
1791 udc->transceiver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (udc->gadget.speed == USB_SPEED_FULL
1793 && udc->driver->resume) {
1794 spin_unlock(&udc->lock);
1795 udc->driver->resume(&udc->gadget);
1796 spin_lock(&udc->lock);
1797 }
1798 }
1799 }
1800 change &= ~UDC_SUS;
1801 }
1802 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1803 update_otg(udc);
1804 change &= ~OTG_FLAGS;
1805 }
1806
1807 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1808 if (change)
1809 VDBG("devstat %03x, ignore change %03x\n",
1810 devstat, change);
1811
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001812 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
David Howells7d12e782006-10-05 14:55:46 +01001815static irqreturn_t omap_udc_irq(int irq, void *_udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
1817 struct omap_udc *udc = _udc;
1818 u16 irq_src;
1819 irqreturn_t status = IRQ_NONE;
1820 unsigned long flags;
1821
1822 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001823 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* Device state change (usb ch9 stuff) */
1826 if (irq_src & UDC_DS_CHG) {
1827 devstate_irq(_udc, irq_src);
1828 status = IRQ_HANDLED;
1829 irq_src &= ~UDC_DS_CHG;
1830 }
1831
1832 /* EP0 control transfers */
1833 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1834 ep0_irq(_udc, irq_src);
1835 status = IRQ_HANDLED;
1836 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1837 }
1838
1839 /* DMA transfer completion */
1840 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1841 dma_irq(_udc, irq_src);
1842 status = IRQ_HANDLED;
1843 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1844 }
1845
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001846 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (irq_src)
1848 DBG("udc_irq, unhandled %03x\n", irq_src);
1849 spin_unlock_irqrestore(&udc->lock, flags);
1850
1851 return status;
1852}
1853
1854/* workaround for seemingly-lost IRQs for RX ACKs... */
1855#define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1856#define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1857
1858static void pio_out_timer(unsigned long _ep)
1859{
1860 struct omap_ep *ep = (void *) _ep;
1861 unsigned long flags;
1862 u16 stat_flg;
1863
1864 spin_lock_irqsave(&ep->udc->lock, flags);
1865 if (!list_empty(&ep->queue) && ep->ackwait) {
David Brownelle6a6e472006-12-10 11:47:04 -08001866 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001867 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1870 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1871 struct omap_req *req;
1872
1873 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1874 req = container_of(ep->queue.next,
1875 struct omap_req, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 (void) read_fifo(ep, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001877 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1878 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 ep->ackwait = 1 + ep->double_buf;
David Brownelle6a6e472006-12-10 11:47:04 -08001880 } else
1881 deselect_ep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1884 spin_unlock_irqrestore(&ep->udc->lock, flags);
1885}
1886
David Howells7d12e782006-10-05 14:55:46 +01001887static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 u16 epn_stat, irq_src;
1890 irqreturn_t status = IRQ_NONE;
1891 struct omap_ep *ep;
1892 int epnum;
1893 struct omap_udc *udc = _dev;
1894 struct omap_req *req;
1895 unsigned long flags;
1896
1897 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001898 epn_stat = omap_readw(UDC_EPN_STAT);
1899 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* handle OUT first, to avoid some wasteful NAKs */
1902 if (irq_src & UDC_EPN_RX) {
1903 epnum = (epn_stat >> 8) & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001904 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 status = IRQ_HANDLED;
1906 ep = &udc->ep[epnum];
1907 ep->irqs++;
1908
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001909 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ep->fnf = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001911 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 ep->ackwait--;
1913 if (!list_empty(&ep->queue)) {
1914 int stat;
1915 req = container_of(ep->queue.next,
1916 struct omap_req, queue);
1917 stat = read_fifo(ep, req);
1918 if (!ep->double_buf)
1919 ep->fnf = 1;
1920 }
1921 }
1922 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001923 epn_stat = omap_readw(UDC_EPN_STAT);
1924 epn_stat = omap_readw(UDC_EPN_STAT);
1925 omap_writew(epnum, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 /* enabling fifo _after_ clearing ACK, contrary to docs,
1928 * reduces lossage; timer still needed though (sigh).
1929 */
1930 if (ep->fnf) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001931 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ep->ackwait = 1 + ep->double_buf;
1933 }
1934 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1935 }
1936
1937 /* then IN transfers */
1938 else if (irq_src & UDC_EPN_TX) {
1939 epnum = epn_stat & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001940 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 status = IRQ_HANDLED;
1942 ep = &udc->ep[16 + epnum];
1943 ep->irqs++;
1944
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001945 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1946 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 ep->ackwait = 0;
1948 if (!list_empty(&ep->queue)) {
1949 req = container_of(ep->queue.next,
1950 struct omap_req, queue);
1951 (void) write_fifo(ep, req);
1952 }
1953 }
1954 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001955 epn_stat = omap_readw(UDC_EPN_STAT);
1956 epn_stat = omap_readw(UDC_EPN_STAT);
1957 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /* then 6 clocks before it'd tx */
1959 }
1960
1961 spin_unlock_irqrestore(&udc->lock, flags);
1962 return status;
1963}
1964
1965#ifdef USE_ISO
David Howells7d12e782006-10-05 14:55:46 +01001966static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
1968 struct omap_udc *udc = _dev;
1969 struct omap_ep *ep;
1970 int pending = 0;
1971 unsigned long flags;
1972
1973 spin_lock_irqsave(&udc->lock, flags);
1974
1975 /* handle all non-DMA ISO transfers */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001976 list_for_each_entry(ep, &udc->iso, iso) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 u16 stat;
1978 struct omap_req *req;
1979
1980 if (ep->has_dma || list_empty(&ep->queue))
1981 continue;
1982 req = list_entry(ep->queue.next, struct omap_req, queue);
1983
1984 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001985 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 /* NOTE: like the other controller drivers, this isn't
1988 * currently reporting lost or damaged frames.
1989 */
1990 if (ep->bEndpointAddress & USB_DIR_IN) {
1991 if (stat & UDC_MISS_IN)
1992 /* done(ep, req, -EPROTO) */;
1993 else
1994 write_fifo(ep, req);
1995 } else {
1996 int status = 0;
1997
1998 if (stat & UDC_NO_RXPACKET)
1999 status = -EREMOTEIO;
2000 else if (stat & UDC_ISO_ERR)
2001 status = -EILSEQ;
2002 else if (stat & UDC_DATA_FLUSH)
2003 status = -ENOSR;
2004
2005 if (status)
2006 /* done(ep, req, status) */;
2007 else
2008 read_fifo(ep, req);
2009 }
2010 deselect_ep();
2011 /* 6 wait states before next EP */
2012
2013 ep->irqs++;
2014 if (!list_empty(&ep->queue))
2015 pending = 1;
2016 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002017 if (!pending) {
2018 u16 w;
2019
2020 w = omap_readw(UDC_IRQ_EN);
2021 w &= ~UDC_SOF_IE;
2022 omap_writew(w, UDC_IRQ_EN);
2023 }
2024 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 spin_unlock_irqrestore(&udc->lock, flags);
2027 return IRQ_HANDLED;
2028}
2029#endif
2030
2031/*-------------------------------------------------------------------------*/
2032
David Brownell8a3c1f52007-03-21 12:26:32 -07002033static inline int machine_without_vbus_sense(void)
David Brownelle6a6e472006-12-10 11:47:04 -08002034{
Felipe Balbi80dd1352012-05-29 14:26:09 +03002035 return machine_is_omap_innovator()
David Brownelle6a6e472006-12-10 11:47:04 -08002036 || machine_is_omap_osk()
David Brownelle6a6e472006-12-10 11:47:04 -08002037 || machine_is_sx1()
Felipe Balbi80dd1352012-05-29 14:26:09 +03002038 /* No known omap7xx boards with vbus sense */
2039 || cpu_is_omap7xx();
David Brownelle6a6e472006-12-10 11:47:04 -08002040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002042static int omap_udc_start(struct usb_gadget_driver *driver,
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002043 int (*bind)(struct usb_gadget *, struct usb_gadget_driver *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 int status = -ENODEV;
2046 struct omap_ep *ep;
2047 unsigned long flags;
2048
2049 /* basic sanity tests */
2050 if (!udc)
2051 return -ENODEV;
2052 if (!driver
Felipe Balbi80dd1352012-05-29 14:26:09 +03002053 /* FIXME if otg, check: driver->is_otg */
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002054 || driver->max_speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002055 || !bind || !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return -EINVAL;
2057
2058 spin_lock_irqsave(&udc->lock, flags);
2059 if (udc->driver) {
2060 spin_unlock_irqrestore(&udc->lock, flags);
2061 return -EBUSY;
2062 }
2063
2064 /* reset state */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002065 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 ep->irqs = 0;
2067 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2068 continue;
2069 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002070 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 udc->ep0_pending = 0;
2073 udc->ep[0].irqs = 0;
2074 udc->softconnect = 1;
2075
2076 /* hook up the driver */
David Brownell313980c2005-04-11 15:38:25 -07002077 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 udc->driver = driver;
2079 udc->gadget.dev.driver = &driver->driver;
2080 spin_unlock_irqrestore(&udc->lock, flags);
2081
David Brownelle6a6e472006-12-10 11:47:04 -08002082 if (udc->dc_clk != NULL)
2083 omap_udc_enable_clock(1);
2084
Sebastian Andrzej Siewiorffe0b332012-09-07 09:53:17 +02002085 status = bind(&udc->gadget, driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (status) {
2087 DBG("bind to %s --> %d\n", driver->driver.name, status);
David Brownell313980c2005-04-11 15:38:25 -07002088 udc->gadget.dev.driver = NULL;
2089 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 goto done;
2091 }
2092 DBG("bound to driver %s\n", driver->driver.name);
2093
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002094 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 /* connect to bus through transceiver */
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302097 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002098 status = otg_set_peripheral(udc->transceiver->otg,
2099 &udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (status < 0) {
2101 ERR("can't bind to transceiver\n");
David Brownell6bea4762006-12-05 03:15:33 -08002102 if (driver->unbind) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002103 driver->unbind(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002104 udc->gadget.dev.driver = NULL;
2105 udc->driver = NULL;
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 goto done;
2108 }
2109 } else {
2110 if (can_pullup(udc))
Felipe Balbi80dd1352012-05-29 14:26:09 +03002111 pullup_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002113 pullup_disable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
2116 /* boards that don't have VBUS sensing can't autogate 48MHz;
2117 * can't enter deep sleep while a gadget driver is active.
2118 */
David Brownell8a3c1f52007-03-21 12:26:32 -07002119 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 omap_vbus_session(&udc->gadget, 1);
2121
2122done:
David Brownelle6a6e472006-12-10 11:47:04 -08002123 if (udc->dc_clk != NULL)
2124 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return status;
2126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002128static int omap_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
2130 unsigned long flags;
2131 int status = -ENODEV;
2132
2133 if (!udc)
2134 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08002135 if (!driver || driver != udc->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return -EINVAL;
2137
David Brownelle6a6e472006-12-10 11:47:04 -08002138 if (udc->dc_clk != NULL)
2139 omap_udc_enable_clock(1);
2140
David Brownell8a3c1f52007-03-21 12:26:32 -07002141 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 omap_vbus_session(&udc->gadget, 0);
2143
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302144 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002145 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 else
2147 pullup_disable(udc);
2148
2149 spin_lock_irqsave(&udc->lock, flags);
2150 udc_quiesce(udc);
2151 spin_unlock_irqrestore(&udc->lock, flags);
2152
2153 driver->unbind(&udc->gadget);
David Brownell313980c2005-04-11 15:38:25 -07002154 udc->gadget.dev.driver = NULL;
2155 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
David Brownelle6a6e472006-12-10 11:47:04 -08002157 if (udc->dc_clk != NULL)
2158 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 DBG("unregistered driver '%s'\n", driver->driver.name);
2160 return status;
2161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163/*-------------------------------------------------------------------------*/
2164
2165#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2166
2167#include <linux/seq_file.h>
2168
2169static const char proc_filename[] = "driver/udc";
2170
2171#define FOURBITS "%s%s%s%s"
Felipe Balbi80dd1352012-05-29 14:26:09 +03002172#define EIGHTBITS "%s%s%s%s%s%s%s%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2175{
2176 u16 stat_flg;
2177 struct omap_req *req;
2178 char buf[20];
2179
2180 use_ep(ep, 0);
2181
2182 if (use_dma && ep->has_dma)
2183 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2184 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2185 ep->dma_channel - 1, ep->lch);
2186 else
2187 buf[0] = 0;
2188
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002189 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 seq_printf(s,
2191 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2192 ep->name, buf,
2193 ep->double_buf ? "dbuf " : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002194 ({ char *s;
2195 switch (ep->ackwait) {
2196 case 0:
2197 s = "";
2198 break;
2199 case 1:
2200 s = "(ackw) ";
2201 break;
2202 case 2:
2203 s = "(ackw2) ";
2204 break;
2205 default:
2206 s = "(?) ";
2207 break;
2208 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 ep->irqs, stat_flg,
2210 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2211 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2212 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2213 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2214 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2215 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2216 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2217 (stat_flg & UDC_STALL) ? "STALL " : "",
2218 (stat_flg & UDC_NAK) ? "NAK " : "",
2219 (stat_flg & UDC_ACK) ? "ACK " : "",
2220 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2221 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2222 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2223
Felipe Balbi80dd1352012-05-29 14:26:09 +03002224 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 seq_printf(s, "\t(queue empty)\n");
2226 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002227 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 unsigned length = req->req.actual;
2229
2230 if (use_dma && buf[0]) {
2231 length += ((ep->bEndpointAddress & USB_DIR_IN)
2232 ? dma_src_len : dma_dest_len)
2233 (ep, req->req.dma + length);
2234 buf[0] = 0;
2235 }
2236 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2237 &req->req, length,
2238 req->req.length, req->req.buf);
2239 }
2240}
2241
2242static char *trx_mode(unsigned m, int enabled)
2243{
2244 switch (m) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002245 case 0:
2246 return enabled ? "*6wire" : "unused";
2247 case 1:
2248 return "4wire";
2249 case 2:
2250 return "3wire";
2251 case 3:
2252 return "6wire";
2253 default:
2254 return "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256}
2257
2258static int proc_otg_show(struct seq_file *s)
2259{
2260 u32 tmp;
Paul Walmsley4814ced2010-10-08 11:40:20 -06002261 u32 trans = 0;
2262 char *ctrl_name = "(UNKNOWN)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Dmitry Baryshkove12cc342008-08-07 16:29:25 +04002264 tmp = omap_readl(OTG_REV);
Tony Lindgrenae372572012-05-21 12:01:11 -07002265 ctrl_name = "tranceiver_ctrl";
2266 trans = omap_readw(USB_TRANSCEIVER_CTRL);
David Brownelle6a6e472006-12-10 11:47:04 -08002267 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2268 tmp >> 4, tmp & 0xf, ctrl_name, trans);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002269 tmp = omap_readw(OTG_SYSCON_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2271 FOURBITS "\n", tmp,
2272 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2273 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
David Brownell65111082005-04-28 13:52:31 -07002274 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 ? "internal"
2276 : trx_mode(USB0_TRX_MODE(tmp), 1),
2277 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2278 (tmp & HST_IDLE_EN) ? " !host" : "",
2279 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2280 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002281 tmp = omap_readl(OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2283 " b_ase_brst=%d hmc=%d\n", tmp,
2284 (tmp & OTG_EN) ? " otg_en" : "",
2285 (tmp & USBX_SYNCHRO) ? " synchro" : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002286 /* much more SRP stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 (tmp & SRP_DATA) ? " srp_data" : "",
2288 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2289 (tmp & OTG_PADEN) ? " otg_paden" : "",
2290 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2291 (tmp & UHOST_EN) ? " uhost_en" : "",
2292 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2293 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2294 B_ASE_BRST(tmp),
2295 OTG_HMC(tmp));
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002296 tmp = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2298 (tmp & OTG_ASESSVLD) ? " asess" : "",
2299 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2300 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2301 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2302 (tmp & OTG_ID) ? " id" : "",
2303 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2304 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2305 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2306 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2307 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2308 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2309 (tmp & OTG_PULLDOWN) ? " down" : "",
2310 (tmp & OTG_PULLUP) ? " up" : "",
2311 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2312 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2313 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2314 (tmp & OTG_PU_ID) ? " pu_id" : ""
2315 );
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002316 tmp = omap_readw(OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002318 tmp = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002320 tmp = omap_readw(OTG_OUTCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002322 tmp = omap_readw(OTG_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 seq_printf(s, "otg_test %04x" "\n", tmp);
David Brownell313980c2005-04-11 15:38:25 -07002324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
2327static int proc_udc_show(struct seq_file *s, void *_)
2328{
2329 u32 tmp;
2330 struct omap_ep *ep;
2331 unsigned long flags;
2332
2333 spin_lock_irqsave(&udc->lock, flags);
2334
2335 seq_printf(s, "%s, version: " DRIVER_VERSION
2336#ifdef USE_ISO
2337 " (iso)"
2338#endif
2339 "%s\n",
2340 driver_desc,
2341 use_dma ? " (dma)" : "");
2342
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002343 tmp = omap_readw(UDC_REV) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 seq_printf(s,
2345 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2346 "hmc %d, transceiver %s\n",
2347 tmp >> 4, tmp & 0xf,
2348 fifo_mode,
2349 udc->driver ? udc->driver->driver.name : "(none)",
2350 HMC,
David Brownelle6a6e472006-12-10 11:47:04 -08002351 udc->transceiver
2352 ? udc->transceiver->label
Tony Lindgrenae372572012-05-21 12:01:11 -07002353 : (cpu_is_omap1710()
David Brownelle6a6e472006-12-10 11:47:04 -08002354 ? "external" : "(none)"));
Tony Lindgrenae372572012-05-21 12:01:11 -07002355 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2356 omap_readw(ULPD_CLOCK_CTRL),
2357 omap_readw(ULPD_SOFT_REQ),
2358 omap_readw(ULPD_STATUS_REQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 /* OTG controller registers */
2361 if (!cpu_is_omap15xx())
2362 proc_otg_show(s);
2363
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002364 tmp = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2366 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2367 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2368 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2369 (tmp & UDC_NAK_EN) ? " nak" : "",
2370 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2371 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2372 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2373 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
Felipe Balbi80dd1352012-05-29 14:26:09 +03002374 /* syscon2 is write-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /* UDC controller registers */
2377 if (!(tmp & UDC_PULLUP_EN)) {
2378 seq_printf(s, "(suspended)\n");
2379 spin_unlock_irqrestore(&udc->lock, flags);
2380 return 0;
2381 }
2382
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002383 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2385 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2386 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2387 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2388 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2389 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2390 (tmp & UDC_SUS) ? " SUS" : "",
2391 (tmp & UDC_CFG) ? " CFG" : "",
2392 (tmp & UDC_ADD) ? " ADD" : "",
2393 (tmp & UDC_DEF) ? " DEF" : "",
2394 (tmp & UDC_ATT) ? " ATT" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002395 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2396 tmp = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2398 (tmp & UDC_SOF_IE) ? " sof" : "",
2399 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2400 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2401 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2402 (tmp & UDC_EP0_IE) ? " ep0" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002403 tmp = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2405 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2406 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2407 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002408 (tmp & UDC_IRQ_SOF) ? " sof" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2410 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2411 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2412 (tmp & UDC_SETUP) ? " setup" : "",
2413 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2414 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2415 if (use_dma) {
2416 unsigned i;
2417
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002418 tmp = omap_readw(UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2420 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2421 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2422 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2423
2424 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2425 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2426 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2427
2428 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2429 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2430 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2431
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002432 tmp = omap_readw(UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2434 if (tmp) {
2435 for (i = 0; i < 3; i++) {
2436 if ((tmp & (0x0f << (i * 4))) == 0)
2437 continue;
2438 seq_printf(s, "rxdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002439 omap_readw(UDC_RXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002442 tmp = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 seq_printf(s, "txdma_cfg %04x\n", tmp);
2444 if (tmp) {
2445 for (i = 0; i < 3; i++) {
2446 if (!(tmp & (0x0f << (i * 4))))
2447 continue;
2448 seq_printf(s, "txdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002449 omap_readw(UDC_TXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451 }
2452 }
2453
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002454 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (tmp & UDC_ATT) {
2456 proc_ep_show(s, &udc->ep[0]);
2457 if (tmp & UDC_ADD) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002458 list_for_each_entry(ep, &udc->gadget.ep_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 ep.ep_list) {
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02002460 if (ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 proc_ep_show(s, ep);
2462 }
2463 }
2464 }
2465 spin_unlock_irqrestore(&udc->lock, flags);
2466 return 0;
2467}
2468
2469static int proc_udc_open(struct inode *inode, struct file *file)
2470{
David Brownell313980c2005-04-11 15:38:25 -07002471 return single_open(file, proc_udc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03002474static const struct file_operations proc_ops = {
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002475 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 .open = proc_udc_open,
2477 .read = seq_read,
2478 .llseek = seq_lseek,
2479 .release = single_release,
2480};
2481
2482static void create_proc_file(void)
2483{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002484 proc_create(proc_filename, 0, NULL, &proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
2487static void remove_proc_file(void)
2488{
David Brownell313980c2005-04-11 15:38:25 -07002489 remove_proc_entry(proc_filename, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
2492#else
2493
2494static inline void create_proc_file(void) {}
2495static inline void remove_proc_file(void) {}
2496
2497#endif
2498
2499/*-------------------------------------------------------------------------*/
2500
2501/* Before this controller can enumerate, we need to pick an endpoint
2502 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2503 * buffer space among the endpoints we'll be operating.
David Brownell65111082005-04-28 13:52:31 -07002504 *
2505 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002506 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
David Brownell65111082005-04-28 13:52:31 -07002507 * capability yet though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 */
Felipe Balbidc1737c2012-05-29 14:33:30 +03002509static unsigned __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510omap_ep_setup(char *name, u8 addr, u8 type,
2511 unsigned buf, unsigned maxp, int dbuf)
2512{
2513 struct omap_ep *ep;
2514 u16 epn_rxtx = 0;
2515
2516 /* OUT endpoints first, then IN */
2517 ep = &udc->ep[addr & 0xf];
2518 if (addr & USB_DIR_IN)
2519 ep += 16;
2520
2521 /* in case of ep init table bugs */
2522 BUG_ON(ep->name[0]);
2523
2524 /* chip setup ... bit values are same for IN, OUT */
2525 if (type == USB_ENDPOINT_XFER_ISOC) {
2526 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002527 case 8:
2528 epn_rxtx = 0 << 12;
2529 break;
2530 case 16:
2531 epn_rxtx = 1 << 12;
2532 break;
2533 case 32:
2534 epn_rxtx = 2 << 12;
2535 break;
2536 case 64:
2537 epn_rxtx = 3 << 12;
2538 break;
2539 case 128:
2540 epn_rxtx = 4 << 12;
2541 break;
2542 case 256:
2543 epn_rxtx = 5 << 12;
2544 break;
2545 case 512:
2546 epn_rxtx = 6 << 12;
2547 break;
2548 default:
2549 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551 epn_rxtx |= UDC_EPN_RX_ISO;
2552 dbuf = 1;
2553 } else {
2554 /* double-buffering "not supported" on 15xx,
David Brownelle6a6e472006-12-10 11:47:04 -08002555 * and ignored for PIO-IN on newer chips
2556 * (for more reliable behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 */
Tony Lindgrenae372572012-05-21 12:01:11 -07002558 if (!use_dma || cpu_is_omap15xx())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 dbuf = 0;
2560
2561 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002562 case 8:
2563 epn_rxtx = 0 << 12;
2564 break;
2565 case 16:
2566 epn_rxtx = 1 << 12;
2567 break;
2568 case 32:
2569 epn_rxtx = 2 << 12;
2570 break;
2571 case 64:
2572 epn_rxtx = 3 << 12;
2573 break;
2574 default:
2575 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 if (dbuf && addr)
2578 epn_rxtx |= UDC_EPN_RX_DB;
2579 init_timer(&ep->timer);
2580 ep->timer.function = pio_out_timer;
2581 ep->timer.data = (unsigned long) ep;
2582 }
2583 if (addr)
2584 epn_rxtx |= UDC_EPN_RX_VALID;
2585 BUG_ON(buf & 0x07);
2586 epn_rxtx |= buf >> 3;
2587
2588 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2589 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2590
2591 if (addr & USB_DIR_IN)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002592 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002594 omap_writew(epn_rxtx, UDC_EP_RX(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 /* next endpoint's buffer starts after this one's */
2597 buf += maxp;
2598 if (dbuf)
2599 buf += maxp;
2600 BUG_ON(buf > 2048);
2601
2602 /* set up driver data structures */
2603 BUG_ON(strlen(name) >= sizeof ep->name);
2604 strlcpy(ep->name, name, sizeof ep->name);
2605 INIT_LIST_HEAD(&ep->queue);
2606 INIT_LIST_HEAD(&ep->iso);
2607 ep->bEndpointAddress = addr;
2608 ep->bmAttributes = type;
2609 ep->double_buf = dbuf;
David Brownelle6a6e472006-12-10 11:47:04 -08002610 ep->udc = udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 ep->ep.name = ep->name;
2613 ep->ep.ops = &omap_ep_ops;
2614 ep->ep.maxpacket = ep->maxpacket = maxp;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002615 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 return buf;
2618}
2619
2620static void omap_udc_release(struct device *dev)
2621{
2622 complete(udc->done);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002623 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002624 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Felipe Balbidc1737c2012-05-29 14:33:30 +03002627static int __devinit
Heikki Krogerus86753812012-02-13 13:24:02 +02002628omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 unsigned tmp, buf;
2631
2632 /* abolish any previous hardware state */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002633 omap_writew(0, UDC_SYSCON1);
2634 omap_writew(0, UDC_IRQ_EN);
2635 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2636 omap_writew(0, UDC_DMA_IRQ_EN);
2637 omap_writew(0, UDC_RXDMA_CFG);
2638 omap_writew(0, UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 /* UDC_PULLUP_EN gates the chip clock */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002641 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Christoph Lametere94b1762006-12-06 20:33:17 -08002643 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (!udc)
2645 return -ENOMEM;
2646
Felipe Balbi80dd1352012-05-29 14:26:09 +03002647 spin_lock_init(&udc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 udc->gadget.ops = &omap_gadget_ops;
2650 udc->gadget.ep0 = &udc->ep[0].ep;
2651 INIT_LIST_HEAD(&udc->gadget.ep_list);
2652 INIT_LIST_HEAD(&udc->iso);
2653 udc->gadget.speed = USB_SPEED_UNKNOWN;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002654 udc->gadget.max_speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 udc->gadget.name = driver_name;
2656
2657 device_initialize(&udc->gadget.dev);
Kay Sievers0031a062008-05-02 06:02:41 +02002658 dev_set_name(&udc->gadget.dev, "gadget");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 udc->gadget.dev.release = omap_udc_release;
2660 udc->gadget.dev.parent = &odev->dev;
2661 if (use_dma)
2662 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2663
2664 udc->transceiver = xceiv;
2665
2666 /* ep0 is special; put it right after the SETUP buffer */
2667 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2668 8 /* after SETUP */, 64 /* maxpacket */, 0);
2669 list_del_init(&udc->ep[0].ep.ep_list);
2670
2671 /* initially disable all non-ep0 endpoints */
2672 for (tmp = 1; tmp < 15; tmp++) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002673 omap_writew(0, UDC_EP_RX(tmp));
2674 omap_writew(0, UDC_EP_TX(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676
Felipe Balbi80dd1352012-05-29 14:26:09 +03002677#define OMAP_BULK_EP(name, addr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 buf = omap_ep_setup(name "-bulk", addr, \
2679 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002680#define OMAP_INT_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 buf = omap_ep_setup(name "-int", addr, \
2682 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002683#define OMAP_ISO_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 buf = omap_ep_setup(name "-iso", addr, \
2685 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2686
2687 switch (fifo_mode) {
2688 case 0:
2689 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2690 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2691 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2692 break;
2693 case 1:
2694 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2695 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
David Brownell313980c2005-04-11 15:38:25 -07002696 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2699 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
David Brownell313980c2005-04-11 15:38:25 -07002700 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2703 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
David Brownell313980c2005-04-11 15:38:25 -07002704 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2707 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
David Brownell313980c2005-04-11 15:38:25 -07002708 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2711 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
David Brownell313980c2005-04-11 15:38:25 -07002712 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2713 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2716 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
David Brownell313980c2005-04-11 15:38:25 -07002717 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2718 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
David Brownell313980c2005-04-11 15:38:25 -07002720 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2721 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 break;
2724
2725#ifdef USE_ISO
2726 case 2: /* mixed iso/bulk */
2727 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2728 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2729 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2730 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2731
2732 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2733
2734 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2735 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2736 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2737 break;
2738 case 3: /* mixed bulk/iso */
2739 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2740 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2741 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2742
2743 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2744 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2745 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2746
2747 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2748 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2749 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2750 break;
2751#endif
2752
2753 /* add more modes as needed */
2754
2755 default:
2756 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2757 return -ENODEV;
2758 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002759 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2761 return 0;
2762}
2763
Felipe Balbidc1737c2012-05-29 14:33:30 +03002764static int __devinit omap_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 int status = -ENODEV;
2767 int hmc;
Heikki Krogerus86753812012-02-13 13:24:02 +02002768 struct usb_phy *xceiv = NULL;
David Brownell313980c2005-04-11 15:38:25 -07002769 const char *type = NULL;
Russell King3ae5eae2005-11-09 22:32:44 +00002770 struct omap_usb_config *config = pdev->dev.platform_data;
Tony Lindgrenae372572012-05-21 12:01:11 -07002771 struct clk *dc_clk = NULL;
2772 struct clk *hhc_clk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Felipe Balbi5b6d84b2012-05-29 14:27:52 +03002774 if (cpu_is_omap7xx())
2775 use_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
2777 /* NOTE: "knows" the order of the resources! */
David Brownelle6a6e472006-12-10 11:47:04 -08002778 if (!request_mem_region(pdev->resource[0].start,
Russell King3ae5eae2005-11-09 22:32:44 +00002779 pdev->resource[0].end - pdev->resource[0].start + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 driver_name)) {
2781 DBG("request_mem_region failed\n");
2782 return -EBUSY;
2783 }
2784
David Brownelle6a6e472006-12-10 11:47:04 -08002785 if (cpu_is_omap16xx()) {
2786 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2787 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2788 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2789 /* can't use omap_udc_enable_clock yet */
2790 clk_enable(dc_clk);
2791 clk_enable(hhc_clk);
2792 udelay(100);
2793 }
2794
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002795 if (cpu_is_omap7xx()) {
2796 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2797 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2798 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2799 /* can't use omap_udc_enable_clock yet */
2800 clk_enable(dc_clk);
2801 clk_enable(hhc_clk);
2802 udelay(100);
2803 }
2804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 INFO("OMAP UDC rev %d.%d%s\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002806 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 config->otg ? ", Mini-AB" : "");
2808
2809 /* use the mode given to us by board init code */
2810 if (cpu_is_omap15xx()) {
2811 hmc = HMC_1510;
2812 type = "(unknown)";
2813
David Brownell8a3c1f52007-03-21 12:26:32 -07002814 if (machine_without_vbus_sense()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /* just set up software VBUS detect, and then
2816 * later rig it so we always report VBUS.
2817 * FIXME without really sensing VBUS, we can't
2818 * know when to turn PULLUP_EN on/off; and that
2819 * means we always "need" the 48MHz clock.
2820 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002821 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2822 tmp &= ~VBUS_CTRL_1510;
2823 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 tmp |= VBUS_MODE_1510;
2825 tmp &= ~VBUS_CTRL_1510;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002826 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828 } else {
David Brownell65111082005-04-28 13:52:31 -07002829 /* The transceiver may package some GPIO logic or handle
2830 * loopback and/or transceiverless setup; if we find one,
2831 * use it. Except for OTG, we don't _need_ to talk to one;
2832 * but not having one probably means no VBUS detection.
2833 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05302834 xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302835 if (!IS_ERR_OR_NULL(xceiv))
David Brownell65111082005-04-28 13:52:31 -07002836 type = xceiv->label;
2837 else if (config->otg) {
2838 DBG("OTG requires external transceiver!\n");
2839 goto cleanup0;
2840 }
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 hmc = HMC_1610;
David Brownelle6a6e472006-12-10 11:47:04 -08002843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 switch (hmc) {
David Brownell313980c2005-04-11 15:38:25 -07002845 case 0: /* POWERUP DEFAULT == 0 */
2846 case 4:
2847 case 12:
2848 case 20:
2849 if (!cpu_is_omap1710()) {
2850 type = "integrated";
2851 break;
2852 }
2853 /* FALL THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 case 3:
2855 case 11:
2856 case 16:
2857 case 19:
2858 case 25:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302859 if (IS_ERR_OR_NULL(xceiv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 DBG("external transceiver not registered!\n");
David Brownell313980c2005-04-11 15:38:25 -07002861 type = "unknown";
David Brownell65111082005-04-28 13:52:31 -07002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 case 21: /* internal loopback */
David Brownell313980c2005-04-11 15:38:25 -07002865 type = "loopback";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 break;
2867 case 14: /* transceiverless */
David Brownell65111082005-04-28 13:52:31 -07002868 if (cpu_is_omap1710())
2869 goto bad_on_1710;
2870 /* FALL THROUGH */
2871 case 13:
2872 case 15:
David Brownell313980c2005-04-11 15:38:25 -07002873 type = "no";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 break;
2875
2876 default:
David Brownell65111082005-04-28 13:52:31 -07002877bad_on_1710:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 ERR("unrecognized UDC HMC mode %d\n", hmc);
David Brownell65111082005-04-28 13:52:31 -07002879 goto cleanup0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881 }
Tony Lindgrenae372572012-05-21 12:01:11 -07002882
David Brownell313980c2005-04-11 15:38:25 -07002883 INFO("hmc mode %d, %s transceiver\n", hmc, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 /* a "gadget" abstracts/virtualizes the controller */
Russell King3ae5eae2005-11-09 22:32:44 +00002886 status = omap_udc_setup(pdev, xceiv);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002887 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 goto cleanup0;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002889
David Brownell313980c2005-04-11 15:38:25 -07002890 xceiv = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002891 /* "udc" is now valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 pullup_disable(udc);
2893#if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2894 udc->gadget.is_otg = (config->otg != 0);
2895#endif
2896
David Brownell65111082005-04-28 13:52:31 -07002897 /* starting with omap1710 es2.0, clear toggle is a separate bit */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002898 if (omap_readw(UDC_REV) >= 0x61)
David Brownell65111082005-04-28 13:52:31 -07002899 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2900 else
2901 udc->clr_halt = UDC_RESET_EP;
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 /* USB general purpose IRQ: ep0, state changes, dma, etc */
Russell King3ae5eae2005-11-09 22:32:44 +00002904 status = request_irq(pdev->resource[1].start, omap_udc_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002905 0, driver_name, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002907 ERR("can't get irq %d, err %d\n",
2908 (int) pdev->resource[1].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 goto cleanup1;
2910 }
2911
2912 /* USB "non-iso" IRQ (PIO for all but ep0) */
Russell King3ae5eae2005-11-09 22:32:44 +00002913 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002914 0, "omap_udc pio", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002916 ERR("can't get irq %d, err %d\n",
2917 (int) pdev->resource[2].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto cleanup2;
2919 }
2920#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002921 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002922 0, "omap_udc iso", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002924 ERR("can't get irq %d, err %d\n",
2925 (int) pdev->resource[3].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 goto cleanup3;
2927 }
2928#endif
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002929 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002930 udc->dc_clk = dc_clk;
2931 udc->hhc_clk = hhc_clk;
2932 clk_disable(hhc_clk);
2933 clk_disable(dc_clk);
2934 }
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 create_proc_file();
David Brownelle6a6e472006-12-10 11:47:04 -08002937 status = device_add(&udc->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002938 if (status)
2939 goto cleanup4;
2940
2941 status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
David Brownelle6a6e472006-12-10 11:47:04 -08002942 if (!status)
2943 return status;
2944 /* If fail, fall through */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002945cleanup4:
2946 remove_proc_file();
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948#ifdef USE_ISO
2949cleanup3:
Russell King3ae5eae2005-11-09 22:32:44 +00002950 free_irq(pdev->resource[2].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951#endif
2952
2953cleanup2:
Russell King3ae5eae2005-11-09 22:32:44 +00002954 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956cleanup1:
Felipe Balbi80dd1352012-05-29 14:26:09 +03002957 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002958 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
2960cleanup0:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302961 if (!IS_ERR_OR_NULL(xceiv))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05302962 usb_put_phy(xceiv);
David Brownelle6a6e472006-12-10 11:47:04 -08002963
Tony Lindgrenae372572012-05-21 12:01:11 -07002964 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002965 clk_disable(hhc_clk);
2966 clk_disable(dc_clk);
2967 clk_put(hhc_clk);
2968 clk_put(dc_clk);
2969 }
2970
Russell King3ae5eae2005-11-09 22:32:44 +00002971 release_mem_region(pdev->resource[0].start,
2972 pdev->resource[0].end - pdev->resource[0].start + 1);
David Brownelle6a6e472006-12-10 11:47:04 -08002973
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return status;
2975}
2976
Felipe Balbidc1737c2012-05-29 14:33:30 +03002977static int __devexit omap_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002979 DECLARE_COMPLETION_ONSTACK(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 if (!udc)
2982 return -ENODEV;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002983
2984 usb_del_gadget_udc(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002985 if (udc->driver)
2986 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 udc->done = &done;
2989
2990 pullup_disable(udc);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302991 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05302992 usb_put_phy(udc->transceiver);
David Brownell313980c2005-04-11 15:38:25 -07002993 udc->transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002995 omap_writew(0, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 remove_proc_file();
2998
2999#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00003000 free_irq(pdev->resource[3].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001#endif
Russell King3ae5eae2005-11-09 22:32:44 +00003002 free_irq(pdev->resource[2].start, udc);
3003 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
David Brownelle6a6e472006-12-10 11:47:04 -08003005 if (udc->dc_clk) {
3006 if (udc->clk_requested)
3007 omap_udc_enable_clock(0);
3008 clk_put(udc->hhc_clk);
3009 clk_put(udc->dc_clk);
3010 }
3011
Russell King3ae5eae2005-11-09 22:32:44 +00003012 release_mem_region(pdev->resource[0].start,
3013 pdev->resource[0].end - pdev->resource[0].start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 device_unregister(&udc->gadget.dev);
3016 wait_for_completion(&done);
3017
3018 return 0;
3019}
3020
David Brownell313980c2005-04-11 15:38:25 -07003021/* suspend/resume/wakeup from sysfs (echo > power/state) or when the
3022 * system is forced into deep sleep
3023 *
3024 * REVISIT we should probably reject suspend requests when there's a host
3025 * session active, rather than disconnecting, at least on boards that can
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003026 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
David Brownell313980c2005-04-11 15:38:25 -07003027 * make host resumes and VBUS detection trigger OMAP wakeup events; that
3028 * may involve talking to an external transceiver (e.g. isp1301).
3029 */
david-b@pacbell.net1d7beee2005-06-29 07:00:56 -07003030
Russell King3ae5eae2005-11-09 22:32:44 +00003031static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
David Brownell313980c2005-04-11 15:38:25 -07003033 u32 devstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003035 devstat = omap_readw(UDC_DEVSTAT);
David Brownell313980c2005-04-11 15:38:25 -07003036
3037 /* we're requesting 48 MHz clock if the pullup is enabled
3038 * (== we're attached to the host) and we're not suspended,
3039 * which would prevent entry to deep sleep...
3040 */
3041 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003042 WARNING("session active; suspend requires disconnect\n");
David Brownell313980c2005-04-11 15:38:25 -07003043 omap_pullup(&udc->gadget, 0);
3044 }
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return 0;
3047}
3048
Russell King3ae5eae2005-11-09 22:32:44 +00003049static int omap_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 DBG("resume + wakeup/SRP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 omap_pullup(&udc->gadget, 1);
3053
3054 /* maybe the host would enumerate us if we nudged it */
3055 msleep(100);
3056 return omap_wakeup(&udc->gadget);
3057}
3058
3059/*-------------------------------------------------------------------------*/
3060
Russell King3ae5eae2005-11-09 22:32:44 +00003061static struct platform_driver udc_driver = {
Felipe Balbidc1737c2012-05-29 14:33:30 +03003062 .probe = omap_udc_probe,
3063 .remove = __devexit_p(omap_udc_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 .suspend = omap_udc_suspend,
3065 .resume = omap_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003066 .driver = {
3067 .owner = THIS_MODULE,
3068 .name = (char *) driver_name,
3069 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070};
3071
Felipe Balbidc1737c2012-05-29 14:33:30 +03003072module_platform_driver(udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074MODULE_DESCRIPTION(DRIVER_DESC);
3075MODULE_LICENSE("GPL");
Kay Sieversf34c32f2008-04-10 21:29:21 -07003076MODULE_ALIAS("platform:omap_udc");