blob: b6cc8b19f39ffbc99a9f82405d75d92f323d5949 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
3 *
4 * Copyright (C) 2004 Texas Instruments, Inc.
5 * Copyright (C) 2004-2005 David Brownell
6 *
Kyungmin Park527ea732007-11-21 15:13:15 -08007 * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#undef DEBUG
16#undef VERBOSE
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/ioport.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/timer.h>
27#include <linux/list.h>
28#include <linux/interrupt.h>
29#include <linux/proc_fs.h>
30#include <linux/mm.h>
31#include <linux/moduleparam.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
David Brownell5f848132006-12-16 15:34:53 -080033#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070034#include <linux/usb/gadget.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070035#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/dma-mapping.h>
David Brownelle6a6e472006-12-10 11:47:04 -080037#include <linux/clk.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070038#include <linux/prefetch.h>
Felipe Balbi80dd1352012-05-29 14:26:09 +030039#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44#include <asm/mach-types.h>
45
Tony Lindgrence491cf2009-10-20 09:40:47 -070046#include <plat/dma.h>
47#include <plat/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "omap_udc.h"
50
51#undef USB_TRACE
52
53/* bulk DMA seems to be behaving for both IN and OUT */
54#define USE_DMA
55
56/* ISO too */
57#define USE_ISO
58
59#define DRIVER_DESC "OMAP UDC driver"
60#define DRIVER_VERSION "4 October 2004"
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
63 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
64 * D+ pullup to allow enumeration. That's too early for the gadget
65 * framework to use from usb_endpoint_enable(), which happens after
66 * enumeration as part of activating an interface. (But if we add an
67 * optional new "UDC not yet running" state to the gadget driver model,
68 * even just during driver binding, the endpoint autoconfig logic is the
69 * natural spot to manufacture new endpoints.)
70 *
71 * So instead of using endpoint enable calls to control the hardware setup,
72 * this driver defines a "fifo mode" parameter. It's used during driver
73 * initialization to choose among a set of pre-defined endpoint configs.
74 * See omap_udc_setup() for available modes, or to add others. That code
75 * lives in an init section, so use this driver as a module if you need
76 * to change the fifo mode after the kernel boots.
77 *
78 * Gadget drivers normally ignore endpoints they don't care about, and
79 * won't include them in configuration descriptors. That means only
80 * misbehaving hosts would even notice they exist.
81 */
82#ifdef USE_ISO
83static unsigned fifo_mode = 3;
84#else
Felipe Balbi80dd1352012-05-29 14:26:09 +030085static unsigned fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#endif
87
88/* "modprobe omap_udc fifo_mode=42", or else as a kernel
89 * boot parameter "omap_udc:fifo_mode=42"
90 */
Felipe Balbi80dd1352012-05-29 14:26:09 +030091module_param(fifo_mode, uint, 0);
92MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94#ifdef USE_DMA
Rusty Russell90ab5ee2012-01-13 09:32:20 +103095static bool use_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* "modprobe omap_udc use_dma=y", or else as a kernel
98 * boot parameter "omap_udc:use_dma=y"
99 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300100module_param(use_dma, bool, 0);
101MODULE_PARM_DESC(use_dma, "enable/disable DMA");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#else /* !USE_DMA */
103
104/* save a bit of code */
105#define use_dma 0
106#endif /* !USE_DMA */
107
108
Felipe Balbi80dd1352012-05-29 14:26:09 +0300109static const char driver_name[] = "omap_udc";
110static const char driver_desc[] = DRIVER_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*-------------------------------------------------------------------------*/
113
114/* there's a notion of "current endpoint" for modifying endpoint
David Brownelle6a6e472006-12-10 11:47:04 -0800115 * state, and PIO access to its FIFO.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
117
118static void use_ep(struct omap_ep *ep, u16 select)
119{
120 u16 num = ep->bEndpointAddress & 0x0f;
121
122 if (ep->bEndpointAddress & USB_DIR_IN)
123 num |= UDC_EP_DIR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300124 omap_writew(num | select, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* when select, MUST deselect later !! */
126}
127
128static inline void deselect_ep(void)
129{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300130 u16 w;
131
132 w = omap_readw(UDC_EP_NUM);
133 w &= ~UDC_EP_SEL;
134 omap_writew(w, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 /* 6 wait states before TX will happen */
136}
137
138static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
139
140/*-------------------------------------------------------------------------*/
141
142static int omap_ep_enable(struct usb_ep *_ep,
143 const struct usb_endpoint_descriptor *desc)
144{
145 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
146 struct omap_udc *udc;
147 unsigned long flags;
148 u16 maxp;
149
150 /* catch various bogus parameters */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200151 if (!_ep || !desc || ep->ep.desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 || desc->bDescriptorType != USB_DT_ENDPOINT
153 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700154 || ep->maxpacket < usb_endpoint_maxp(desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800155 DBG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EINVAL;
157 }
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700158 maxp = usb_endpoint_maxp(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
160 && maxp != ep->maxpacket)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700161 || usb_endpoint_maxp(desc) > ep->maxpacket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800163 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return -ERANGE;
165 }
166
167#ifdef USE_ISO
168 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
169 && desc->bInterval != 1)) {
170 /* hardware wants period = 1; USB allows 2^(Interval-1) */
171 DBG("%s, unsupported ISO period %dms\n", _ep->name,
172 1 << (desc->bInterval - 1));
173 return -EDOM;
174 }
175#else
176 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
177 DBG("%s, ISO nyet\n", _ep->name);
178 return -EDOM;
179 }
180#endif
181
182 /* xfer types must match, except that interrupt ~= bulk */
183 if (ep->bmAttributes != desc->bmAttributes
184 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
185 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800186 DBG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return -EINVAL;
188 }
189
190 udc = ep->udc;
191 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800192 DBG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return -ESHUTDOWN;
194 }
195
196 spin_lock_irqsave(&udc->lock, flags);
197
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200198 ep->ep.desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 ep->irqs = 0;
200 ep->stopped = 0;
201 ep->ep.maxpacket = maxp;
202
203 /* set endpoint to initial state */
204 ep->dma_channel = 0;
205 ep->has_dma = 0;
206 ep->lch = -1;
207 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300208 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 ep->ackwait = 0;
210 deselect_ep();
211
212 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
213 list_add(&ep->iso, &udc->iso);
214
215 /* maybe assign a DMA channel to this endpoint */
216 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
217 /* FIXME ISO can dma, but prefers first channel */
218 dma_channel_claim(ep, 0);
219
220 /* PIO OUT may RX packets */
221 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
222 && !ep->has_dma
223 && !(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300224 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 ep->ackwait = 1 + ep->double_buf;
226 }
227
228 spin_unlock_irqrestore(&udc->lock, flags);
229 VDBG("%s enabled\n", _ep->name);
230 return 0;
231}
232
233static void nuke(struct omap_ep *, int status);
234
235static int omap_ep_disable(struct usb_ep *_ep)
236{
237 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
238 unsigned long flags;
239
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200240 if (!_ep || !ep->ep.desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800241 DBG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 _ep ? ep->ep.name : NULL);
243 return -EINVAL;
244 }
245
246 spin_lock_irqsave(&ep->udc->lock, flags);
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200247 ep->ep.desc = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +0300248 nuke(ep, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 ep->ep.maxpacket = ep->maxpacket;
250 ep->has_dma = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300251 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 list_del_init(&ep->iso);
253 del_timer(&ep->timer);
254
255 spin_unlock_irqrestore(&ep->udc->lock, flags);
256
257 VDBG("%s disabled\n", _ep->name);
258 return 0;
259}
260
261/*-------------------------------------------------------------------------*/
262
263static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400264omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct omap_req *req;
267
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800268 req = kzalloc(sizeof(*req), gfp_flags);
Felipe Balbi70617db2012-05-29 14:36:42 +0300269 if (!req)
270 return NULL;
271
Felipe Balbi70617db2012-05-29 14:36:42 +0300272 INIT_LIST_HEAD(&req->queue);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return &req->req;
275}
276
277static void
278omap_free_request(struct usb_ep *ep, struct usb_request *_req)
279{
280 struct omap_req *req = container_of(_req, struct omap_req, req);
281
Felipe Balbi23673d72012-05-29 14:38:32 +0300282 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*-------------------------------------------------------------------------*/
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static void
288done(struct omap_ep *ep, struct omap_req *req, int status)
289{
Felipe Balbidd8e9382012-05-29 14:42:56 +0300290 struct omap_udc *udc = ep->udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned stopped = ep->stopped;
292
293 list_del_init(&req->queue);
294
295 if (req->req.status == -EINPROGRESS)
296 req->req.status = status;
297 else
298 status = req->req.status;
299
Felipe Balbidd8e9382012-05-29 14:42:56 +0300300 if (use_dma && ep->has_dma)
301 usb_gadget_unmap_request(&udc->gadget, &req->req,
302 (ep->bEndpointAddress & USB_DIR_IN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304#ifndef USB_TRACE
305 if (status && status != -ESHUTDOWN)
306#endif
307 VDBG("complete %s req %p stat %d len %u/%u\n",
308 ep->ep.name, &req->req, status,
309 req->req.actual, req->req.length);
310
311 /* don't modify queue heads during completion callback */
312 ep->stopped = 1;
313 spin_unlock(&ep->udc->lock);
314 req->req.complete(&ep->ep, &req->req);
315 spin_lock(&ep->udc->lock);
316 ep->stopped = stopped;
317}
318
319/*-------------------------------------------------------------------------*/
320
David Brownell313980c2005-04-11 15:38:25 -0700321#define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
322#define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324#define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
325#define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
326
David Brownelle6a6e472006-12-10 11:47:04 -0800327static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328write_packet(u8 *buf, struct omap_req *req, unsigned max)
329{
330 unsigned len;
331 u16 *wp;
332
333 len = min(req->req.length - req->req.actual, max);
334 req->req.actual += len;
335
336 max = len;
337 if (likely((((int)buf) & 1) == 0)) {
338 wp = (u16 *)buf;
339 while (max >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300340 omap_writew(*wp++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 max -= 2;
342 }
343 buf = (u8 *)wp;
344 }
345 while (max--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300346 omap_writeb(*buf++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return len;
348}
349
Felipe Balbi80dd1352012-05-29 14:26:09 +0300350/* FIXME change r/w fifo calling convention */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352
Felipe Balbi80dd1352012-05-29 14:26:09 +0300353/* return: 0 = still running, 1 = completed, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static int write_fifo(struct omap_ep *ep, struct omap_req *req)
355{
356 u8 *buf;
357 unsigned count;
358 int is_last;
359 u16 ep_stat;
360
361 buf = req->req.buf + req->req.actual;
362 prefetch(buf);
363
364 /* PIO-IN isn't double buffered except for iso */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300365 ep_stat = omap_readw(UDC_STAT_FLG);
David Brownell313980c2005-04-11 15:38:25 -0700366 if (ep_stat & UDC_FIFO_UNWRITABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368
369 count = ep->ep.maxpacket;
370 count = write_packet(buf, req, count);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300371 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 ep->ackwait = 1;
373
374 /* last packet is often short (sometimes a zlp) */
375 if (count != ep->ep.maxpacket)
376 is_last = 1;
377 else if (req->req.length == req->req.actual
378 && !req->req.zero)
379 is_last = 1;
380 else
381 is_last = 0;
382
383 /* NOTE: requests complete when all IN data is in a
384 * FIFO (or sometimes later, if a zlp was needed).
385 * Use usb_ep_fifo_status() where needed.
386 */
387 if (is_last)
388 done(ep, req, 0);
389 return is_last;
390}
391
David Brownelle6a6e472006-12-10 11:47:04 -0800392static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393read_packet(u8 *buf, struct omap_req *req, unsigned avail)
394{
395 unsigned len;
396 u16 *wp;
397
398 len = min(req->req.length - req->req.actual, avail);
399 req->req.actual += len;
400 avail = len;
401
402 if (likely((((int)buf) & 1) == 0)) {
403 wp = (u16 *)buf;
404 while (avail >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300405 *wp++ = omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 avail -= 2;
407 }
408 buf = (u8 *)wp;
409 }
410 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300411 *buf++ = omap_readb(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return len;
413}
414
Felipe Balbi80dd1352012-05-29 14:26:09 +0300415/* return: 0 = still running, 1 = queue empty, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static int read_fifo(struct omap_ep *ep, struct omap_req *req)
417{
418 u8 *buf;
419 unsigned count, avail;
420 int is_last;
421
422 buf = req->req.buf + req->req.actual;
423 prefetchw(buf);
424
425 for (;;) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300426 u16 ep_stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 is_last = 0;
429 if (ep_stat & FIFO_EMPTY) {
430 if (!ep->double_buf)
431 break;
432 ep->fnf = 1;
433 }
434 if (ep_stat & UDC_EP_HALTED)
435 break;
436
David Brownell313980c2005-04-11 15:38:25 -0700437 if (ep_stat & UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 avail = ep->ep.maxpacket;
439 else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300440 avail = omap_readw(UDC_RXFSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 ep->fnf = ep->double_buf;
442 }
443 count = read_packet(buf, req, avail);
444
445 /* partial packet reads may not be errors */
446 if (count < ep->ep.maxpacket) {
447 is_last = 1;
448 /* overflowed this request? flush extra data */
449 if (count != avail) {
450 req->req.status = -EOVERFLOW;
451 avail -= count;
452 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300453 omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 } else if (req->req.length == req->req.actual)
456 is_last = 1;
457 else
458 is_last = 0;
459
460 if (!ep->bEndpointAddress)
461 break;
462 if (is_last)
463 done(ep, req, 0);
464 break;
465 }
466 return is_last;
467}
468
469/*-------------------------------------------------------------------------*/
470
471static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
472{
473 dma_addr_t end;
474
475 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
476 * the last transfer's bytecount by more than a FIFO's worth.
477 */
478 if (cpu_is_omap15xx())
479 return 0;
480
Tony Lindgren0499bde2008-07-03 12:24:36 +0300481 end = omap_get_dma_src_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (end == ep->dma_counter)
483 return 0;
484
485 end |= start & (0xffff << 16);
486 if (end < start)
487 end += 0x10000;
488 return end - start;
489}
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
492{
493 dma_addr_t end;
494
Tony Lindgren0499bde2008-07-03 12:24:36 +0300495 end = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (end == ep->dma_counter)
497 return 0;
498
499 end |= start & (0xffff << 16);
500 if (cpu_is_omap15xx())
501 end++;
502 if (end < start)
503 end += 0x10000;
504 return end - start;
505}
506
507
508/* Each USB transfer request using DMA maps to one or more DMA transfers.
509 * When DMA completion isn't request completion, the UDC continues with
510 * the next DMA transfer for that USB transfer.
511 */
512
513static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
514{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300515 u16 txdma_ctrl, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 unsigned length = req->req.length - req->req.actual;
517 const int sync_mode = cpu_is_omap15xx()
518 ? OMAP_DMA_SYNC_FRAME
519 : OMAP_DMA_SYNC_ELEMENT;
Kyungmin Park527ea732007-11-21 15:13:15 -0800520 int dma_trigger = 0;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* measure length in either bytes or packets */
David Brownell65111082005-04-28 13:52:31 -0700523 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
525 txdma_ctrl = UDC_TXN_EOT | length;
526 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
Kyungmin Park527ea732007-11-21 15:13:15 -0800527 length, 1, sync_mode, dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else {
529 length = min(length / ep->maxpacket,
530 (unsigned) UDC_TXN_TSC + 1);
David Brownelle6a6e472006-12-10 11:47:04 -0800531 txdma_ctrl = length;
David Brownell65111082005-04-28 13:52:31 -0700532 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
David Brownelle6a6e472006-12-10 11:47:04 -0800533 ep->ep.maxpacket >> 1, length, sync_mode,
Kyungmin Park527ea732007-11-21 15:13:15 -0800534 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 length *= ep->maxpacket;
536 }
537 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800538 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
539 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 omap_start_dma(ep->lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300542 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300543 w = omap_readw(UDC_DMA_IRQ_EN);
544 w |= UDC_TX_DONE_IE(ep->dma_channel);
545 omap_writew(w, UDC_DMA_IRQ_EN);
546 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 req->dma_bytes = length;
548}
549
550static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
551{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300552 u16 w;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (status == 0) {
555 req->req.actual += req->dma_bytes;
556
557 /* return if this request needs to send data or zlp */
558 if (req->req.actual < req->req.length)
559 return;
560 if (req->req.zero
561 && req->dma_bytes != 0
562 && (req->req.actual % ep->maxpacket) == 0)
563 return;
564 } else
565 req->req.actual += dma_src_len(ep, req->req.dma
566 + req->req.actual);
567
568 /* tx completion */
569 omap_stop_dma(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300570 w = omap_readw(UDC_DMA_IRQ_EN);
571 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
572 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 done(ep, req, status);
574}
575
576static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
577{
Kyungmin Park527ea732007-11-21 15:13:15 -0800578 unsigned packets = req->req.length - req->req.actual;
579 int dma_trigger = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300580 u16 w;
Kyungmin Park527ea732007-11-21 15:13:15 -0800581
Tony Lindgrenae372572012-05-21 12:01:11 -0700582 /* set up this DMA transfer, enable the fifo, start */
583 packets /= ep->ep.maxpacket;
584 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
585 req->dma_bytes = packets * ep->ep.maxpacket;
586 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
587 ep->ep.maxpacket >> 1, packets,
588 OMAP_DMA_SYNC_ELEMENT,
589 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800591 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
592 0, 0);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300593 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300595 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
596 w = omap_readw(UDC_DMA_IRQ_EN);
597 w |= UDC_RX_EOT_IE(ep->dma_channel);
598 omap_writew(w, UDC_DMA_IRQ_EN);
599 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
600 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 omap_start_dma(ep->lch);
603}
604
605static void
David Brownellcb97c5c2005-10-16 15:06:51 -0700606finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300608 u16 count, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (status == 0)
611 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
612 count = dma_dest_len(ep, req->req.dma + req->req.actual);
613 count += req->req.actual;
David Brownellcb97c5c2005-10-16 15:06:51 -0700614 if (one)
615 count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (count <= req->req.length)
617 req->req.actual = count;
618
619 if (count != req->dma_bytes || status)
620 omap_stop_dma(ep->lch);
621
622 /* if this wasn't short, request may need another transfer */
623 else if (req->req.actual < req->req.length)
624 return;
625
626 /* rx completion */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300627 w = omap_readw(UDC_DMA_IRQ_EN);
628 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
629 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 done(ep, req, status);
631}
632
633static void dma_irq(struct omap_udc *udc, u16 irq_src)
634{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300635 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 struct omap_ep *ep;
637 struct omap_req *req;
638
639 /* IN dma: tx to host */
640 if (irq_src & UDC_TXN_DONE) {
641 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
642 ep->irqs++;
643 /* can see TXN_DONE after dma abort */
644 if (!list_empty(&ep->queue)) {
645 req = container_of(ep->queue.next,
646 struct omap_req, queue);
647 finish_in_dma(ep, req, 0);
648 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300649 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Felipe Balbi80dd1352012-05-29 14:26:09 +0300651 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 req = container_of(ep->queue.next,
653 struct omap_req, queue);
654 next_in_dma(ep, req);
655 }
656 }
657
658 /* OUT dma: rx from host */
659 if (irq_src & UDC_RXN_EOT) {
660 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
661 ep->irqs++;
662 /* can see RXN_EOT after dma abort */
663 if (!list_empty(&ep->queue)) {
664 req = container_of(ep->queue.next,
665 struct omap_req, queue);
David Brownellcb97c5c2005-10-16 15:06:51 -0700666 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300668 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Felipe Balbi80dd1352012-05-29 14:26:09 +0300670 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 req = container_of(ep->queue.next,
672 struct omap_req, queue);
673 next_out_dma(ep, req);
674 }
675 }
676
677 if (irq_src & UDC_RXN_CNT) {
678 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
679 ep->irqs++;
680 /* omap15xx does this unasked... */
681 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300682 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684}
685
686static void dma_error(int lch, u16 ch_status, void *data)
687{
688 struct omap_ep *ep = data;
689
690 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700691 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
693
694 /* complete current transfer ... */
695}
696
697static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
698{
699 u16 reg;
700 int status, restart, is_in;
Kyungmin Park527ea732007-11-21 15:13:15 -0800701 int dma_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 is_in = ep->bEndpointAddress & USB_DIR_IN;
704 if (is_in)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300705 reg = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300707 reg = omap_readw(UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700708 reg |= UDC_DMA_REQ; /* "pulse" activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 ep->dma_channel = 0;
711 ep->lch = -1;
712 if (channel == 0 || channel > 3) {
713 if ((reg & 0x0f00) == 0)
714 channel = 3;
715 else if ((reg & 0x00f0) == 0)
716 channel = 2;
717 else if ((reg & 0x000f) == 0) /* preferred for ISO */
718 channel = 1;
719 else {
720 status = -EMLINK;
721 goto just_restart;
722 }
723 }
724 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
725 ep->dma_channel = channel;
726
727 if (is_in) {
Tony Lindgrenae372572012-05-21 12:01:11 -0700728 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800729 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ep->ep.name, dma_error, ep, &ep->lch);
731 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300732 omap_writew(reg, UDC_TXDMA_CFG);
Kyungmin Park527ea732007-11-21 15:13:15 -0800733 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700734 omap_set_dma_src_burst_mode(ep->lch,
735 OMAP_DMA_DATA_BURST_4);
736 omap_set_dma_src_data_pack(ep->lch, 1);
737 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 omap_set_dma_dest_params(ep->lch,
739 OMAP_DMA_PORT_TIPB,
740 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700741 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800742 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744 } else {
Tony Lindgrenae372572012-05-21 12:01:11 -0700745 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800746 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ep->ep.name, dma_error, ep, &ep->lch);
748 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300749 omap_writew(reg, UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700750 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 omap_set_dma_src_params(ep->lch,
752 OMAP_DMA_PORT_TIPB,
753 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700754 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800755 0, 0);
Kyungmin Park527ea732007-11-21 15:13:15 -0800756 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700757 omap_set_dma_dest_burst_mode(ep->lch,
758 OMAP_DMA_DATA_BURST_4);
759 omap_set_dma_dest_data_pack(ep->lch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 }
762 if (status)
763 ep->dma_channel = 0;
764 else {
765 ep->has_dma = 1;
766 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
767
768 /* channel type P: hw synch (fifo) */
Tony Lindgrenae372572012-05-21 12:01:11 -0700769 if (!cpu_is_omap15xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300770 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772
773just_restart:
774 /* restart any queue, even if the claim failed */
775 restart = !ep->stopped && !list_empty(&ep->queue);
776
777 if (status)
778 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
779 restart ? " (restart)" : "");
780 else
781 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
782 is_in ? 't' : 'r',
783 ep->dma_channel - 1, ep->lch,
784 restart ? " (restart)" : "");
785
786 if (restart) {
787 struct omap_req *req;
788 req = container_of(ep->queue.next, struct omap_req, queue);
789 if (ep->has_dma)
790 (is_in ? next_in_dma : next_out_dma)(ep, req);
791 else {
792 use_ep(ep, UDC_EP_SEL);
793 (is_in ? write_fifo : read_fifo)(ep, req);
794 deselect_ep();
795 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300796 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ep->ackwait = 1 + ep->double_buf;
798 }
799 /* IN: 6 wait states before it'll tx */
800 }
801 }
802}
803
804static void dma_channel_release(struct omap_ep *ep)
805{
806 int shift = 4 * (ep->dma_channel - 1);
807 u16 mask = 0x0f << shift;
808 struct omap_req *req;
809 int active;
810
811 /* abort any active usb transfer request */
812 if (!list_empty(&ep->queue))
813 req = container_of(ep->queue.next, struct omap_req, queue);
814 else
David Brownell313980c2005-04-11 15:38:25 -0700815 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Tony Lindgren0499bde2008-07-03 12:24:36 +0300817 active = omap_get_dma_active_status(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
820 active ? "active" : "idle",
821 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
822 ep->dma_channel - 1, req);
823
David Brownell65111082005-04-28 13:52:31 -0700824 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
825 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
826 */
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* wait till current packet DMA finishes, and fifo empties */
829 if (ep->bEndpointAddress & USB_DIR_IN) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300830 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
831 UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 if (req) {
834 finish_in_dma(ep, req, -ECONNRESET);
835
836 /* clear FIFO; hosts probably won't empty it */
837 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300838 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 deselect_ep();
840 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300841 while (omap_readw(UDC_TXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 udelay(10);
843 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300844 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
845 UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* dma empties the fifo */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300848 while (omap_readw(UDC_RXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 udelay(10);
850 if (req)
David Brownellcb97c5c2005-10-16 15:06:51 -0700851 finish_out_dma(ep, req, -ECONNRESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853 omap_free_dma(ep->lch);
854 ep->dma_channel = 0;
855 ep->lch = -1;
856 /* has_dma still set, till endpoint is fully quiesced */
857}
858
859
860/*-------------------------------------------------------------------------*/
861
862static int
Al Viro55016f12005-10-21 03:21:58 -0400863omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
866 struct omap_req *req = container_of(_req, struct omap_req, req);
867 struct omap_udc *udc;
868 unsigned long flags;
869 int is_iso = 0;
870
871 /* catch various bogus parameters */
872 if (!_req || !req->req.complete || !req->req.buf
873 || !list_empty(&req->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800874 DBG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return -EINVAL;
876 }
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200877 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800878 DBG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -EINVAL;
880 }
881 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
882 if (req->req.length > ep->ep.maxpacket)
883 return -EMSGSIZE;
884 is_iso = 1;
885 }
886
887 /* this isn't bogus, but OMAP DMA isn't the only hardware to
888 * have a hard time with partial packet reads... reject it.
889 */
890 if (use_dma
891 && ep->has_dma
892 && ep->bEndpointAddress != 0
893 && (ep->bEndpointAddress & USB_DIR_IN) == 0
894 && (req->req.length % ep->ep.maxpacket) != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800895 DBG("%s, no partial packet OUT reads\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return -EMSGSIZE;
897 }
898
899 udc = ep->udc;
900 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
901 return -ESHUTDOWN;
902
Felipe Balbidd8e9382012-05-29 14:42:56 +0300903 if (use_dma && ep->has_dma)
904 usb_gadget_map_request(&udc->gadget, &req->req,
905 (ep->bEndpointAddress & USB_DIR_IN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 VDBG("%s queue req %p, len %d buf %p\n",
908 ep->ep.name, _req, _req->length, _req->buf);
909
910 spin_lock_irqsave(&udc->lock, flags);
911
912 req->req.status = -EINPROGRESS;
913 req->req.actual = 0;
914
915 /* maybe kickstart non-iso i/o queues */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300916 if (is_iso) {
917 u16 w;
918
919 w = omap_readw(UDC_IRQ_EN);
920 w |= UDC_SOF_IE;
921 omap_writew(w, UDC_IRQ_EN);
922 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int is_in;
924
925 if (ep->bEndpointAddress == 0) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300926 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 spin_unlock_irqrestore(&udc->lock, flags);
928 return -EL2HLT;
929 }
930
931 /* empty DATA stage? */
932 is_in = udc->ep0_in;
933 if (!req->req.length) {
934
935 /* chip became CONFIGURED or ADDRESSED
936 * earlier; drivers may already have queued
937 * requests to non-control endpoints
938 */
939 if (udc->ep0_set_config) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300940 u16 irq_en = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
943 if (!udc->ep0_reset_config)
944 irq_en |= UDC_EPN_RX_IE
945 | UDC_EPN_TX_IE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300946 omap_writew(irq_en, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948
David Brownell313980c2005-04-11 15:38:25 -0700949 /* STATUS for zero length DATA stages is
950 * always an IN ... even for IN transfers,
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800951 * a weird case which seem to stall OMAP.
David Brownell313980c2005-04-11 15:38:25 -0700952 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300953 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
954 UDC_EP_NUM);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300955 omap_writew(UDC_CLR_EP, UDC_CTRL);
956 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
957 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /* cleanup */
960 udc->ep0_pending = 0;
961 done(ep, req, 0);
David Brownell313980c2005-04-11 15:38:25 -0700962 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* non-empty DATA stage */
965 } else if (is_in) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300966 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
967 UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 } else {
969 if (udc->ep0_setup)
970 goto irq_wait;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300971 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 } else {
974 is_in = ep->bEndpointAddress & USB_DIR_IN;
975 if (!ep->has_dma)
976 use_ep(ep, UDC_EP_SEL);
977 /* if ISO: SOF IRQs must be enabled/disabled! */
978 }
979
980 if (ep->has_dma)
981 (is_in ? next_in_dma : next_out_dma)(ep, req);
982 else if (req) {
983 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
David Brownell313980c2005-04-11 15:38:25 -0700984 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 deselect_ep();
986 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300987 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ep->ackwait = 1 + ep->double_buf;
989 }
990 /* IN: 6 wait states before it'll tx */
991 }
992 }
993
994irq_wait:
995 /* irq handler advances the queue */
David Brownell313980c2005-04-11 15:38:25 -0700996 if (req != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 list_add_tail(&req->queue, &ep->queue);
998 spin_unlock_irqrestore(&udc->lock, flags);
999
1000 return 0;
1001}
1002
1003static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1004{
1005 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1006 struct omap_req *req;
1007 unsigned long flags;
1008
1009 if (!_ep || !_req)
1010 return -EINVAL;
1011
1012 spin_lock_irqsave(&ep->udc->lock, flags);
1013
1014 /* make sure it's actually queued on this endpoint */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001015 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (&req->req == _req)
1017 break;
1018 }
1019 if (&req->req != _req) {
1020 spin_unlock_irqrestore(&ep->udc->lock, flags);
1021 return -EINVAL;
1022 }
1023
1024 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1025 int channel = ep->dma_channel;
1026
1027 /* releasing the channel cancels the request,
1028 * reclaiming the channel restarts the queue
1029 */
1030 dma_channel_release(ep);
1031 dma_channel_claim(ep, channel);
David Brownelle6a6e472006-12-10 11:47:04 -08001032 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 done(ep, req, -ECONNRESET);
1034 spin_unlock_irqrestore(&ep->udc->lock, flags);
1035 return 0;
1036}
1037
1038/*-------------------------------------------------------------------------*/
1039
1040static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1041{
1042 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1043 unsigned long flags;
1044 int status = -EOPNOTSUPP;
1045
1046 spin_lock_irqsave(&ep->udc->lock, flags);
1047
1048 /* just use protocol stalls for ep0; real halts are annoying */
1049 if (ep->bEndpointAddress == 0) {
1050 if (!ep->udc->ep0_pending)
1051 status = -EINVAL;
1052 else if (value) {
1053 if (ep->udc->ep0_set_config) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001054 WARNING("error changing config?\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001055 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001057 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 ep->udc->ep0_pending = 0;
1059 status = 0;
1060 } else /* NOP */
1061 status = 0;
1062
1063 /* otherwise, all active non-ISO endpoints can halt */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001064 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* IN endpoints must already be idle */
1067 if ((ep->bEndpointAddress & USB_DIR_IN)
David Brownelle6a6e472006-12-10 11:47:04 -08001068 && !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 status = -EAGAIN;
1070 goto done;
1071 }
1072
1073 if (value) {
1074 int channel;
1075
1076 if (use_dma && ep->dma_channel
1077 && !list_empty(&ep->queue)) {
1078 channel = ep->dma_channel;
1079 dma_channel_release(ep);
1080 } else
1081 channel = 0;
1082
1083 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001084 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1085 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 status = 0;
1087 } else
1088 status = -EAGAIN;
1089 deselect_ep();
1090
1091 if (channel)
1092 dma_channel_claim(ep, channel);
1093 } else {
1094 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001095 omap_writew(ep->udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 ep->ackwait = 0;
1097 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001098 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ep->ackwait = 1 + ep->double_buf;
1100 }
1101 }
1102 }
1103done:
1104 VDBG("%s %s halt stat %d\n", ep->ep.name,
1105 value ? "set" : "clear", status);
1106
1107 spin_unlock_irqrestore(&ep->udc->lock, flags);
1108 return status;
1109}
1110
1111static struct usb_ep_ops omap_ep_ops = {
1112 .enable = omap_ep_enable,
1113 .disable = omap_ep_disable,
1114
1115 .alloc_request = omap_alloc_request,
1116 .free_request = omap_free_request,
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .queue = omap_ep_queue,
1119 .dequeue = omap_ep_dequeue,
1120
1121 .set_halt = omap_ep_set_halt,
Felipe Balbi80dd1352012-05-29 14:26:09 +03001122 /* fifo_status ... report bytes in fifo */
1123 /* fifo_flush ... flush fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124};
1125
1126/*-------------------------------------------------------------------------*/
1127
1128static int omap_get_frame(struct usb_gadget *gadget)
1129{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001130 u16 sof = omap_readw(UDC_SOF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1132}
1133
1134static int omap_wakeup(struct usb_gadget *gadget)
1135{
1136 struct omap_udc *udc;
1137 unsigned long flags;
1138 int retval = -EHOSTUNREACH;
1139
1140 udc = container_of(gadget, struct omap_udc, gadget);
1141
1142 spin_lock_irqsave(&udc->lock, flags);
1143 if (udc->devstat & UDC_SUS) {
1144 /* NOTE: OTG spec erratum says that OTG devices may
1145 * issue wakeups without host enable.
1146 */
1147 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1148 DBG("remote wakeup...\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001149 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 retval = 0;
1151 }
1152
1153 /* NOTE: non-OTG systems may use SRP TOO... */
1154 } else if (!(udc->devstat & UDC_ATT)) {
1155 if (udc->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001156 retval = otg_start_srp(udc->transceiver->otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 spin_unlock_irqrestore(&udc->lock, flags);
1159
1160 return retval;
1161}
1162
1163static int
1164omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1165{
1166 struct omap_udc *udc;
1167 unsigned long flags;
1168 u16 syscon1;
1169
1170 udc = container_of(gadget, struct omap_udc, gadget);
1171 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001172 syscon1 = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (is_selfpowered)
1174 syscon1 |= UDC_SELF_PWR;
1175 else
1176 syscon1 &= ~UDC_SELF_PWR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001177 omap_writew(syscon1, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_unlock_irqrestore(&udc->lock, flags);
1179
1180 return 0;
1181}
1182
1183static int can_pullup(struct omap_udc *udc)
1184{
1185 return udc->driver && udc->softconnect && udc->vbus_active;
1186}
1187
1188static void pullup_enable(struct omap_udc *udc)
1189{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001190 u16 w;
1191
1192 w = omap_readw(UDC_SYSCON1);
1193 w |= UDC_PULLUP_EN;
1194 omap_writew(w, UDC_SYSCON1);
1195 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1196 u32 l;
1197
1198 l = omap_readl(OTG_CTRL);
1199 l |= OTG_BSESSVLD;
1200 omap_writel(l, OTG_CTRL);
1201 }
1202 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
1205static void pullup_disable(struct omap_udc *udc)
1206{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001207 u16 w;
1208
1209 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1210 u32 l;
1211
1212 l = omap_readl(OTG_CTRL);
1213 l &= ~OTG_BSESSVLD;
1214 omap_writel(l, OTG_CTRL);
1215 }
1216 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1217 w = omap_readw(UDC_SYSCON1);
1218 w &= ~UDC_PULLUP_EN;
1219 omap_writew(w, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
David Brownelle6a6e472006-12-10 11:47:04 -08001222static struct omap_udc *udc;
1223
1224static void omap_udc_enable_clock(int enable)
1225{
1226 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1227 return;
1228
1229 if (enable) {
1230 clk_enable(udc->dc_clk);
1231 clk_enable(udc->hhc_clk);
1232 udelay(100);
1233 } else {
1234 clk_disable(udc->hhc_clk);
1235 clk_disable(udc->dc_clk);
1236 }
1237}
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239/*
1240 * Called by whatever detects VBUS sessions: external transceiver
1241 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1242 */
1243static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1244{
1245 struct omap_udc *udc;
1246 unsigned long flags;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001247 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 udc = container_of(gadget, struct omap_udc, gadget);
1250 spin_lock_irqsave(&udc->lock, flags);
1251 VDBG("VBUS %s\n", is_active ? "on" : "off");
1252 udc->vbus_active = (is_active != 0);
1253 if (cpu_is_omap15xx()) {
1254 /* "software" detect, ignored if !VBUS_MODE_1510 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001255 l = omap_readl(FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (is_active)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001257 l |= VBUS_CTRL_1510;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001259 l &= ~VBUS_CTRL_1510;
1260 omap_writel(l, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
David Brownelle6a6e472006-12-10 11:47:04 -08001262 if (udc->dc_clk != NULL && is_active) {
1263 if (!udc->clk_requested) {
1264 omap_udc_enable_clock(1);
1265 udc->clk_requested = 1;
1266 }
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (can_pullup(udc))
1269 pullup_enable(udc);
1270 else
1271 pullup_disable(udc);
David Brownelle6a6e472006-12-10 11:47:04 -08001272 if (udc->dc_clk != NULL && !is_active) {
1273 if (udc->clk_requested) {
1274 omap_udc_enable_clock(0);
1275 udc->clk_requested = 0;
1276 }
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 spin_unlock_irqrestore(&udc->lock, flags);
1279 return 0;
1280}
1281
1282static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1283{
1284 struct omap_udc *udc;
1285
1286 udc = container_of(gadget, struct omap_udc, gadget);
1287 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001288 return usb_phy_set_power(udc->transceiver, mA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EOPNOTSUPP;
1290}
1291
1292static int omap_pullup(struct usb_gadget *gadget, int is_on)
1293{
1294 struct omap_udc *udc;
1295 unsigned long flags;
1296
1297 udc = container_of(gadget, struct omap_udc, gadget);
1298 spin_lock_irqsave(&udc->lock, flags);
1299 udc->softconnect = (is_on != 0);
1300 if (can_pullup(udc))
1301 pullup_enable(udc);
1302 else
1303 pullup_disable(udc);
1304 spin_unlock_irqrestore(&udc->lock, flags);
1305 return 0;
1306}
1307
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001308static int omap_udc_start(struct usb_gadget_driver *driver,
1309 int (*bind)(struct usb_gadget *));
1310static int omap_udc_stop(struct usb_gadget_driver *driver);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312static struct usb_gadget_ops omap_gadget_ops = {
1313 .get_frame = omap_get_frame,
1314 .wakeup = omap_wakeup,
1315 .set_selfpowered = omap_set_selfpowered,
1316 .vbus_session = omap_vbus_session,
1317 .vbus_draw = omap_vbus_draw,
1318 .pullup = omap_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001319 .start = omap_udc_start,
1320 .stop = omap_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321};
1322
1323/*-------------------------------------------------------------------------*/
1324
1325/* dequeue ALL requests; caller holds udc->lock */
1326static void nuke(struct omap_ep *ep, int status)
1327{
1328 struct omap_req *req;
1329
1330 ep->stopped = 1;
1331
1332 if (use_dma && ep->dma_channel)
1333 dma_channel_release(ep);
1334
1335 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001336 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001338 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 while (!list_empty(&ep->queue)) {
1341 req = list_entry(ep->queue.next, struct omap_req, queue);
1342 done(ep, req, status);
1343 }
1344}
1345
1346/* caller holds udc->lock */
1347static void udc_quiesce(struct omap_udc *udc)
1348{
1349 struct omap_ep *ep;
1350
1351 udc->gadget.speed = USB_SPEED_UNKNOWN;
1352 nuke(&udc->ep[0], -ESHUTDOWN);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001353 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 nuke(ep, -ESHUTDOWN);
1355}
1356
1357/*-------------------------------------------------------------------------*/
1358
1359static void update_otg(struct omap_udc *udc)
1360{
1361 u16 devstat;
1362
David Brownell9cfbba72007-10-26 13:42:18 -07001363 if (!gadget_is_otg(&udc->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return;
1365
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001366 if (omap_readl(OTG_CTRL) & OTG_ID)
1367 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 else
1369 devstat = 0;
1370
1371 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1372 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1373 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1374
1375 /* Enable HNP early, avoiding races on suspend irq path.
1376 * ASSUMES OTG state machine B_BUS_REQ input is true.
1377 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001378 if (udc->gadget.b_hnp_enable) {
1379 u32 l;
1380
1381 l = omap_readl(OTG_CTRL);
1382 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1383 l &= ~OTG_PULLUP;
1384 omap_writel(l, OTG_CTRL);
1385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1389{
1390 struct omap_ep *ep0 = &udc->ep[0];
David Brownell313980c2005-04-11 15:38:25 -07001391 struct omap_req *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 ep0->irqs++;
1394
1395 /* Clear any pending requests and then scrub any rx/tx state
1396 * before starting to handle the SETUP request.
1397 */
1398 if (irq_src & UDC_SETUP) {
1399 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1400
1401 nuke(ep0, 0);
1402 if (ack) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001403 omap_writew(ack, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 irq_src = UDC_SETUP;
1405 }
1406 }
1407
David Brownelle6a6e472006-12-10 11:47:04 -08001408 /* IN/OUT packets mean we're in the DATA or STATUS stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 * This driver uses only uses protocol stalls (ep0 never halts),
1410 * and if we got this far the gadget driver already had a
1411 * chance to stall. Tries to be forgiving of host oddities.
1412 *
1413 * NOTE: the last chance gadget drivers have to stall control
1414 * requests is during their request completion callback.
1415 */
1416 if (!list_empty(&ep0->queue))
1417 req = container_of(ep0->queue.next, struct omap_req, queue);
1418
1419 /* IN == TX to host */
1420 if (irq_src & UDC_EP0_TX) {
1421 int stat;
1422
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001423 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1424 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1425 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (stat & UDC_ACK) {
1427 if (udc->ep0_in) {
1428 /* write next IN packet from response,
1429 * or set up the status stage.
1430 */
1431 if (req)
1432 stat = write_fifo(ep0, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001433 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (!req && udc->ep0_pending) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001435 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1436 omap_writew(UDC_CLR_EP, UDC_CTRL);
1437 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1438 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 udc->ep0_pending = 0;
1440 } /* else: 6 wait states before it'll tx */
1441 } else {
1442 /* ack status stage of OUT transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001443 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (req)
1445 done(ep0, req, 0);
1446 }
David Brownell313980c2005-04-11 15:38:25 -07001447 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001449 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1450 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001452 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 }
1455
1456 /* OUT == RX from host */
1457 if (irq_src & UDC_EP0_RX) {
1458 int stat;
1459
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001460 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1461 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1462 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (stat & UDC_ACK) {
1464 if (!udc->ep0_in) {
1465 stat = 0;
1466 /* read next OUT packet of request, maybe
1467 * reactiviting the fifo; stall on errors.
1468 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001469 stat = read_fifo(ep0, req);
1470 if (!req || stat < 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001471 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 udc->ep0_pending = 0;
1473 stat = 0;
1474 } else if (stat == 0)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001475 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1476 omap_writew(0, UDC_EP_NUM);
David Brownelle6a6e472006-12-10 11:47:04 -08001477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* activate status stage */
1479 if (stat == 1) {
1480 done(ep0, req, 0);
1481 /* that may have STALLed ep0... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001482 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1483 UDC_EP_NUM);
1484 omap_writew(UDC_CLR_EP, UDC_CTRL);
1485 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1486 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 udc->ep0_pending = 0;
1488 }
1489 } else {
1490 /* ack status stage of IN transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001491 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (req)
1493 done(ep0, req, 0);
1494 }
1495 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001496 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1497 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001499 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 }
1502
1503 /* SETUP starts all control transfers */
1504 if (irq_src & UDC_SETUP) {
1505 union u {
1506 u16 word[4];
1507 struct usb_ctrlrequest r;
1508 } u;
1509 int status = -EINVAL;
1510 struct omap_ep *ep;
1511
1512 /* read the (latest) SETUP message */
1513 do {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001514 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 /* two bytes at a time */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001516 u.word[0] = omap_readw(UDC_DATA);
1517 u.word[1] = omap_readw(UDC_DATA);
1518 u.word[2] = omap_readw(UDC_DATA);
1519 u.word[3] = omap_readw(UDC_DATA);
1520 omap_writew(0, UDC_EP_NUM);
1521 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
David Brownell01ee7d72007-05-25 20:40:14 -07001523#define w_value le16_to_cpu(u.r.wValue)
1524#define w_index le16_to_cpu(u.r.wIndex)
1525#define w_length le16_to_cpu(u.r.wLength)
David Brownell65111082005-04-28 13:52:31 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /* Delegate almost all control requests to the gadget driver,
1528 * except for a handful of ch9 status/feature requests that
1529 * hardware doesn't autodecode _and_ the gadget API hides.
1530 */
1531 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1532 udc->ep0_set_config = 0;
1533 udc->ep0_pending = 1;
1534 ep0->stopped = 0;
1535 ep0->ackwait = 0;
1536 switch (u.r.bRequest) {
1537 case USB_REQ_SET_CONFIGURATION:
1538 /* udc needs to know when ep != 0 is valid */
1539 if (u.r.bRequestType != USB_RECIP_DEVICE)
1540 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001541 if (w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto do_stall;
1543 udc->ep0_set_config = 1;
David Brownell65111082005-04-28 13:52:31 -07001544 udc->ep0_reset_config = (w_value == 0);
1545 VDBG("set config %d\n", w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /* update udc NOW since gadget driver may start
1548 * queueing requests immediately; clear config
1549 * later if it fails the request.
1550 */
1551 if (udc->ep0_reset_config)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001552 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001554 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 update_otg(udc);
1556 goto delegate;
1557 case USB_REQ_CLEAR_FEATURE:
1558 /* clear endpoint halt */
1559 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1560 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001561 if (w_value != USB_ENDPOINT_HALT
1562 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001564 ep = &udc->ep[w_index & 0xf];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (ep != ep0) {
David Brownell65111082005-04-28 13:52:31 -07001566 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 ep += 16;
1568 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001569 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto do_stall;
1571 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001572 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 ep->ackwait = 0;
1574 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001575 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ep->ackwait = 1 + ep->double_buf;
1577 }
David Brownell313980c2005-04-11 15:38:25 -07001578 /* NOTE: assumes the host behaves sanely,
1579 * only clearing real halts. Else we may
1580 * need to kill pending transfers and then
1581 * restart the queue... very messy for DMA!
1582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584 VDBG("%s halt cleared by host\n", ep->name);
1585 goto ep0out_status_stage;
1586 case USB_REQ_SET_FEATURE:
1587 /* set endpoint halt */
1588 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1589 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001590 if (w_value != USB_ENDPOINT_HALT
1591 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001593 ep = &udc->ep[w_index & 0xf];
1594 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 ep += 16;
1596 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001597 || ep == ep0 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 goto do_stall;
1599 if (use_dma && ep->has_dma) {
1600 /* this has rude side-effects (aborts) and
1601 * can't really work if DMA-IN is active
1602 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001603 DBG("%s host set_halt, NYET\n", ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto do_stall;
1605 }
1606 use_ep(ep, 0);
1607 /* can't halt if fifo isn't empty... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001608 omap_writew(UDC_CLR_EP, UDC_CTRL);
1609 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 VDBG("%s halted by host\n", ep->name);
1611ep0out_status_stage:
1612 status = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001613 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1614 omap_writew(UDC_CLR_EP, UDC_CTRL);
1615 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1616 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 udc->ep0_pending = 0;
1618 break;
1619 case USB_REQ_GET_STATUS:
David Brownell8a3c1f52007-03-21 12:26:32 -07001620 /* USB_ENDPOINT_HALT status? */
1621 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1622 goto intf_status;
1623
1624 /* ep0 never stalls */
1625 if (!(w_index & 0xf))
1626 goto zero_status;
1627
1628 /* only active endpoints count */
1629 ep = &udc->ep[w_index & 0xf];
1630 if (w_index & USB_DIR_IN)
1631 ep += 16;
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001632 if (!ep->ep.desc)
David Brownell8a3c1f52007-03-21 12:26:32 -07001633 goto do_stall;
1634
1635 /* iso never stalls */
1636 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1637 goto zero_status;
1638
1639 /* FIXME don't assume non-halted endpoints!! */
1640 ERR("%s status, can't report\n", ep->ep.name);
1641 goto do_stall;
1642
1643intf_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /* return interface status. if we were pedantic,
1645 * we'd detect non-existent interfaces, and stall.
1646 */
1647 if (u.r.bRequestType
1648 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1649 goto delegate;
David Brownell8a3c1f52007-03-21 12:26:32 -07001650
1651zero_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /* return two zero bytes */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001653 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1654 omap_writew(0, UDC_DATA);
1655 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1656 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 status = 0;
David Brownell65111082005-04-28 13:52:31 -07001658 VDBG("GET_STATUS, interface %d\n", w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 /* next, status stage */
1660 break;
1661 default:
1662delegate:
1663 /* activate the ep0out fifo right away */
David Brownell65111082005-04-28 13:52:31 -07001664 if (!udc->ep0_in && w_length) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001665 omap_writew(0, UDC_EP_NUM);
1666 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
1669 /* gadget drivers see class/vendor specific requests,
1670 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1671 * and more
1672 */
1673 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1674 u.r.bRequestType, u.r.bRequest,
David Brownell65111082005-04-28 13:52:31 -07001675 w_value, w_index, w_length);
1676
1677#undef w_value
1678#undef w_index
1679#undef w_length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* The gadget driver may return an error here,
1682 * causing an immediate protocol stall.
1683 *
1684 * Else it must issue a response, either queueing a
1685 * response buffer for the DATA stage, or halting ep0
1686 * (causing a protocol stall, not a real halt). A
1687 * zero length buffer means no DATA stage.
1688 *
1689 * It's fine to issue that response after the setup()
1690 * call returns, and this IRQ was handled.
1691 */
1692 udc->ep0_setup = 1;
1693 spin_unlock(&udc->lock);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001694 status = udc->driver->setup(&udc->gadget, &u.r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 spin_lock(&udc->lock);
1696 udc->ep0_setup = 0;
1697 }
1698
1699 if (status < 0) {
1700do_stall:
1701 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1702 u.r.bRequestType, u.r.bRequest, status);
1703 if (udc->ep0_set_config) {
1704 if (udc->ep0_reset_config)
Arjan van de Venb6c63932008-07-25 01:45:52 -07001705 WARNING("error resetting config?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001707 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001709 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 udc->ep0_pending = 0;
1711 }
1712 }
1713}
1714
1715/*-------------------------------------------------------------------------*/
1716
1717#define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1718
1719static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1720{
1721 u16 devstat, change;
1722
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001723 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 change = devstat ^ udc->devstat;
1725 udc->devstat = devstat;
1726
1727 if (change & (UDC_USB_RESET|UDC_ATT)) {
1728 udc_quiesce(udc);
1729
1730 if (change & UDC_ATT) {
1731 /* driver for any external transceiver will
1732 * have called omap_vbus_session() already
1733 */
1734 if (devstat & UDC_ATT) {
1735 udc->gadget.speed = USB_SPEED_FULL;
1736 VDBG("connect\n");
1737 if (!udc->transceiver)
1738 pullup_enable(udc);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001739 /* if (driver->connect) call it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1741 udc->gadget.speed = USB_SPEED_UNKNOWN;
1742 if (!udc->transceiver)
1743 pullup_disable(udc);
1744 DBG("disconnect, gadget %s\n",
1745 udc->driver->driver.name);
1746 if (udc->driver->disconnect) {
1747 spin_unlock(&udc->lock);
1748 udc->driver->disconnect(&udc->gadget);
1749 spin_lock(&udc->lock);
1750 }
1751 }
1752 change &= ~UDC_ATT;
1753 }
1754
1755 if (change & UDC_USB_RESET) {
1756 if (devstat & UDC_USB_RESET) {
1757 VDBG("RESET=1\n");
1758 } else {
1759 udc->gadget.speed = USB_SPEED_FULL;
1760 INFO("USB reset done, gadget %s\n",
1761 udc->driver->driver.name);
1762 /* ep0 traffic is legal from now on */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001763 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1764 UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 change &= ~UDC_USB_RESET;
1767 }
1768 }
1769 if (change & UDC_SUS) {
1770 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03001771 /* FIXME tell isp1301 to suspend/resume (?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (devstat & UDC_SUS) {
1773 VDBG("suspend\n");
1774 update_otg(udc);
1775 /* HNP could be under way already */
1776 if (udc->gadget.speed == USB_SPEED_FULL
1777 && udc->driver->suspend) {
1778 spin_unlock(&udc->lock);
1779 udc->driver->suspend(&udc->gadget);
1780 spin_lock(&udc->lock);
1781 }
Juha Yrj?l?4e671852005-10-16 15:47:04 -07001782 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001783 usb_phy_set_suspend(
1784 udc->transceiver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 } else {
1786 VDBG("resume\n");
Juha Yrj?l?4e671852005-10-16 15:47:04 -07001787 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001788 usb_phy_set_suspend(
1789 udc->transceiver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (udc->gadget.speed == USB_SPEED_FULL
1791 && udc->driver->resume) {
1792 spin_unlock(&udc->lock);
1793 udc->driver->resume(&udc->gadget);
1794 spin_lock(&udc->lock);
1795 }
1796 }
1797 }
1798 change &= ~UDC_SUS;
1799 }
1800 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1801 update_otg(udc);
1802 change &= ~OTG_FLAGS;
1803 }
1804
1805 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1806 if (change)
1807 VDBG("devstat %03x, ignore change %03x\n",
1808 devstat, change);
1809
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001810 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
David Howells7d12e782006-10-05 14:55:46 +01001813static irqreturn_t omap_udc_irq(int irq, void *_udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct omap_udc *udc = _udc;
1816 u16 irq_src;
1817 irqreturn_t status = IRQ_NONE;
1818 unsigned long flags;
1819
1820 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001821 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
1823 /* Device state change (usb ch9 stuff) */
1824 if (irq_src & UDC_DS_CHG) {
1825 devstate_irq(_udc, irq_src);
1826 status = IRQ_HANDLED;
1827 irq_src &= ~UDC_DS_CHG;
1828 }
1829
1830 /* EP0 control transfers */
1831 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1832 ep0_irq(_udc, irq_src);
1833 status = IRQ_HANDLED;
1834 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1835 }
1836
1837 /* DMA transfer completion */
1838 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1839 dma_irq(_udc, irq_src);
1840 status = IRQ_HANDLED;
1841 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1842 }
1843
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001844 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (irq_src)
1846 DBG("udc_irq, unhandled %03x\n", irq_src);
1847 spin_unlock_irqrestore(&udc->lock, flags);
1848
1849 return status;
1850}
1851
1852/* workaround for seemingly-lost IRQs for RX ACKs... */
1853#define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1854#define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1855
1856static void pio_out_timer(unsigned long _ep)
1857{
1858 struct omap_ep *ep = (void *) _ep;
1859 unsigned long flags;
1860 u16 stat_flg;
1861
1862 spin_lock_irqsave(&ep->udc->lock, flags);
1863 if (!list_empty(&ep->queue) && ep->ackwait) {
David Brownelle6a6e472006-12-10 11:47:04 -08001864 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001865 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1868 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1869 struct omap_req *req;
1870
1871 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1872 req = container_of(ep->queue.next,
1873 struct omap_req, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 (void) read_fifo(ep, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001875 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1876 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 ep->ackwait = 1 + ep->double_buf;
David Brownelle6a6e472006-12-10 11:47:04 -08001878 } else
1879 deselect_ep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1882 spin_unlock_irqrestore(&ep->udc->lock, flags);
1883}
1884
David Howells7d12e782006-10-05 14:55:46 +01001885static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 u16 epn_stat, irq_src;
1888 irqreturn_t status = IRQ_NONE;
1889 struct omap_ep *ep;
1890 int epnum;
1891 struct omap_udc *udc = _dev;
1892 struct omap_req *req;
1893 unsigned long flags;
1894
1895 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001896 epn_stat = omap_readw(UDC_EPN_STAT);
1897 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /* handle OUT first, to avoid some wasteful NAKs */
1900 if (irq_src & UDC_EPN_RX) {
1901 epnum = (epn_stat >> 8) & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001902 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 status = IRQ_HANDLED;
1904 ep = &udc->ep[epnum];
1905 ep->irqs++;
1906
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001907 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ep->fnf = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001909 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ep->ackwait--;
1911 if (!list_empty(&ep->queue)) {
1912 int stat;
1913 req = container_of(ep->queue.next,
1914 struct omap_req, queue);
1915 stat = read_fifo(ep, req);
1916 if (!ep->double_buf)
1917 ep->fnf = 1;
1918 }
1919 }
1920 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001921 epn_stat = omap_readw(UDC_EPN_STAT);
1922 epn_stat = omap_readw(UDC_EPN_STAT);
1923 omap_writew(epnum, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 /* enabling fifo _after_ clearing ACK, contrary to docs,
1926 * reduces lossage; timer still needed though (sigh).
1927 */
1928 if (ep->fnf) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001929 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 ep->ackwait = 1 + ep->double_buf;
1931 }
1932 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1933 }
1934
1935 /* then IN transfers */
1936 else if (irq_src & UDC_EPN_TX) {
1937 epnum = epn_stat & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001938 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 status = IRQ_HANDLED;
1940 ep = &udc->ep[16 + epnum];
1941 ep->irqs++;
1942
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001943 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1944 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 ep->ackwait = 0;
1946 if (!list_empty(&ep->queue)) {
1947 req = container_of(ep->queue.next,
1948 struct omap_req, queue);
1949 (void) write_fifo(ep, req);
1950 }
1951 }
1952 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001953 epn_stat = omap_readw(UDC_EPN_STAT);
1954 epn_stat = omap_readw(UDC_EPN_STAT);
1955 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* then 6 clocks before it'd tx */
1957 }
1958
1959 spin_unlock_irqrestore(&udc->lock, flags);
1960 return status;
1961}
1962
1963#ifdef USE_ISO
David Howells7d12e782006-10-05 14:55:46 +01001964static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct omap_udc *udc = _dev;
1967 struct omap_ep *ep;
1968 int pending = 0;
1969 unsigned long flags;
1970
1971 spin_lock_irqsave(&udc->lock, flags);
1972
1973 /* handle all non-DMA ISO transfers */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001974 list_for_each_entry(ep, &udc->iso, iso) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 u16 stat;
1976 struct omap_req *req;
1977
1978 if (ep->has_dma || list_empty(&ep->queue))
1979 continue;
1980 req = list_entry(ep->queue.next, struct omap_req, queue);
1981
1982 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001983 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* NOTE: like the other controller drivers, this isn't
1986 * currently reporting lost or damaged frames.
1987 */
1988 if (ep->bEndpointAddress & USB_DIR_IN) {
1989 if (stat & UDC_MISS_IN)
1990 /* done(ep, req, -EPROTO) */;
1991 else
1992 write_fifo(ep, req);
1993 } else {
1994 int status = 0;
1995
1996 if (stat & UDC_NO_RXPACKET)
1997 status = -EREMOTEIO;
1998 else if (stat & UDC_ISO_ERR)
1999 status = -EILSEQ;
2000 else if (stat & UDC_DATA_FLUSH)
2001 status = -ENOSR;
2002
2003 if (status)
2004 /* done(ep, req, status) */;
2005 else
2006 read_fifo(ep, req);
2007 }
2008 deselect_ep();
2009 /* 6 wait states before next EP */
2010
2011 ep->irqs++;
2012 if (!list_empty(&ep->queue))
2013 pending = 1;
2014 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002015 if (!pending) {
2016 u16 w;
2017
2018 w = omap_readw(UDC_IRQ_EN);
2019 w &= ~UDC_SOF_IE;
2020 omap_writew(w, UDC_IRQ_EN);
2021 }
2022 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 spin_unlock_irqrestore(&udc->lock, flags);
2025 return IRQ_HANDLED;
2026}
2027#endif
2028
2029/*-------------------------------------------------------------------------*/
2030
David Brownell8a3c1f52007-03-21 12:26:32 -07002031static inline int machine_without_vbus_sense(void)
David Brownelle6a6e472006-12-10 11:47:04 -08002032{
Felipe Balbi80dd1352012-05-29 14:26:09 +03002033 return machine_is_omap_innovator()
David Brownelle6a6e472006-12-10 11:47:04 -08002034 || machine_is_omap_osk()
David Brownelle6a6e472006-12-10 11:47:04 -08002035 || machine_is_sx1()
Felipe Balbi80dd1352012-05-29 14:26:09 +03002036 /* No known omap7xx boards with vbus sense */
2037 || cpu_is_omap7xx();
David Brownelle6a6e472006-12-10 11:47:04 -08002038}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002040static int omap_udc_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002041 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 int status = -ENODEV;
2044 struct omap_ep *ep;
2045 unsigned long flags;
2046
2047 /* basic sanity tests */
2048 if (!udc)
2049 return -ENODEV;
2050 if (!driver
Felipe Balbi80dd1352012-05-29 14:26:09 +03002051 /* FIXME if otg, check: driver->is_otg */
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002052 || driver->max_speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002053 || !bind || !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return -EINVAL;
2055
2056 spin_lock_irqsave(&udc->lock, flags);
2057 if (udc->driver) {
2058 spin_unlock_irqrestore(&udc->lock, flags);
2059 return -EBUSY;
2060 }
2061
2062 /* reset state */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002063 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 ep->irqs = 0;
2065 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2066 continue;
2067 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002068 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070 udc->ep0_pending = 0;
2071 udc->ep[0].irqs = 0;
2072 udc->softconnect = 1;
2073
2074 /* hook up the driver */
David Brownell313980c2005-04-11 15:38:25 -07002075 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 udc->driver = driver;
2077 udc->gadget.dev.driver = &driver->driver;
2078 spin_unlock_irqrestore(&udc->lock, flags);
2079
David Brownelle6a6e472006-12-10 11:47:04 -08002080 if (udc->dc_clk != NULL)
2081 omap_udc_enable_clock(1);
2082
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002083 status = bind(&udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (status) {
2085 DBG("bind to %s --> %d\n", driver->driver.name, status);
David Brownell313980c2005-04-11 15:38:25 -07002086 udc->gadget.dev.driver = NULL;
2087 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto done;
2089 }
2090 DBG("bound to driver %s\n", driver->driver.name);
2091
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002092 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /* connect to bus through transceiver */
2095 if (udc->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002096 status = otg_set_peripheral(udc->transceiver->otg,
2097 &udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (status < 0) {
2099 ERR("can't bind to transceiver\n");
David Brownell6bea4762006-12-05 03:15:33 -08002100 if (driver->unbind) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002101 driver->unbind(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002102 udc->gadget.dev.driver = NULL;
2103 udc->driver = NULL;
2104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto done;
2106 }
2107 } else {
2108 if (can_pullup(udc))
Felipe Balbi80dd1352012-05-29 14:26:09 +03002109 pullup_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002111 pullup_disable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
2113
2114 /* boards that don't have VBUS sensing can't autogate 48MHz;
2115 * can't enter deep sleep while a gadget driver is active.
2116 */
David Brownell8a3c1f52007-03-21 12:26:32 -07002117 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 omap_vbus_session(&udc->gadget, 1);
2119
2120done:
David Brownelle6a6e472006-12-10 11:47:04 -08002121 if (udc->dc_clk != NULL)
2122 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return status;
2124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002126static int omap_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 unsigned long flags;
2129 int status = -ENODEV;
2130
2131 if (!udc)
2132 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08002133 if (!driver || driver != udc->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return -EINVAL;
2135
David Brownelle6a6e472006-12-10 11:47:04 -08002136 if (udc->dc_clk != NULL)
2137 omap_udc_enable_clock(1);
2138
David Brownell8a3c1f52007-03-21 12:26:32 -07002139 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 omap_vbus_session(&udc->gadget, 0);
2141
2142 if (udc->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002143 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 else
2145 pullup_disable(udc);
2146
2147 spin_lock_irqsave(&udc->lock, flags);
2148 udc_quiesce(udc);
2149 spin_unlock_irqrestore(&udc->lock, flags);
2150
2151 driver->unbind(&udc->gadget);
David Brownell313980c2005-04-11 15:38:25 -07002152 udc->gadget.dev.driver = NULL;
2153 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
David Brownelle6a6e472006-12-10 11:47:04 -08002155 if (udc->dc_clk != NULL)
2156 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 DBG("unregistered driver '%s'\n", driver->driver.name);
2158 return status;
2159}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161/*-------------------------------------------------------------------------*/
2162
2163#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2164
2165#include <linux/seq_file.h>
2166
2167static const char proc_filename[] = "driver/udc";
2168
2169#define FOURBITS "%s%s%s%s"
Felipe Balbi80dd1352012-05-29 14:26:09 +03002170#define EIGHTBITS "%s%s%s%s%s%s%s%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2173{
2174 u16 stat_flg;
2175 struct omap_req *req;
2176 char buf[20];
2177
2178 use_ep(ep, 0);
2179
2180 if (use_dma && ep->has_dma)
2181 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2182 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2183 ep->dma_channel - 1, ep->lch);
2184 else
2185 buf[0] = 0;
2186
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002187 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 seq_printf(s,
2189 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2190 ep->name, buf,
2191 ep->double_buf ? "dbuf " : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002192 ({ char *s;
2193 switch (ep->ackwait) {
2194 case 0:
2195 s = "";
2196 break;
2197 case 1:
2198 s = "(ackw) ";
2199 break;
2200 case 2:
2201 s = "(ackw2) ";
2202 break;
2203 default:
2204 s = "(?) ";
2205 break;
2206 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 ep->irqs, stat_flg,
2208 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2209 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2210 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2211 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2212 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2213 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2214 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2215 (stat_flg & UDC_STALL) ? "STALL " : "",
2216 (stat_flg & UDC_NAK) ? "NAK " : "",
2217 (stat_flg & UDC_ACK) ? "ACK " : "",
2218 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2219 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2220 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2221
Felipe Balbi80dd1352012-05-29 14:26:09 +03002222 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 seq_printf(s, "\t(queue empty)\n");
2224 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002225 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 unsigned length = req->req.actual;
2227
2228 if (use_dma && buf[0]) {
2229 length += ((ep->bEndpointAddress & USB_DIR_IN)
2230 ? dma_src_len : dma_dest_len)
2231 (ep, req->req.dma + length);
2232 buf[0] = 0;
2233 }
2234 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2235 &req->req, length,
2236 req->req.length, req->req.buf);
2237 }
2238}
2239
2240static char *trx_mode(unsigned m, int enabled)
2241{
2242 switch (m) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002243 case 0:
2244 return enabled ? "*6wire" : "unused";
2245 case 1:
2246 return "4wire";
2247 case 2:
2248 return "3wire";
2249 case 3:
2250 return "6wire";
2251 default:
2252 return "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254}
2255
2256static int proc_otg_show(struct seq_file *s)
2257{
2258 u32 tmp;
Paul Walmsley4814ced2010-10-08 11:40:20 -06002259 u32 trans = 0;
2260 char *ctrl_name = "(UNKNOWN)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Dmitry Baryshkove12cc342008-08-07 16:29:25 +04002262 tmp = omap_readl(OTG_REV);
Tony Lindgrenae372572012-05-21 12:01:11 -07002263 ctrl_name = "tranceiver_ctrl";
2264 trans = omap_readw(USB_TRANSCEIVER_CTRL);
David Brownelle6a6e472006-12-10 11:47:04 -08002265 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2266 tmp >> 4, tmp & 0xf, ctrl_name, trans);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002267 tmp = omap_readw(OTG_SYSCON_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2269 FOURBITS "\n", tmp,
2270 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2271 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
David Brownell65111082005-04-28 13:52:31 -07002272 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 ? "internal"
2274 : trx_mode(USB0_TRX_MODE(tmp), 1),
2275 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2276 (tmp & HST_IDLE_EN) ? " !host" : "",
2277 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2278 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002279 tmp = omap_readl(OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2281 " b_ase_brst=%d hmc=%d\n", tmp,
2282 (tmp & OTG_EN) ? " otg_en" : "",
2283 (tmp & USBX_SYNCHRO) ? " synchro" : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002284 /* much more SRP stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 (tmp & SRP_DATA) ? " srp_data" : "",
2286 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2287 (tmp & OTG_PADEN) ? " otg_paden" : "",
2288 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2289 (tmp & UHOST_EN) ? " uhost_en" : "",
2290 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2291 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2292 B_ASE_BRST(tmp),
2293 OTG_HMC(tmp));
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002294 tmp = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2296 (tmp & OTG_ASESSVLD) ? " asess" : "",
2297 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2298 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2299 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2300 (tmp & OTG_ID) ? " id" : "",
2301 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2302 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2303 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2304 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2305 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2306 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2307 (tmp & OTG_PULLDOWN) ? " down" : "",
2308 (tmp & OTG_PULLUP) ? " up" : "",
2309 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2310 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2311 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2312 (tmp & OTG_PU_ID) ? " pu_id" : ""
2313 );
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002314 tmp = omap_readw(OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002316 tmp = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002318 tmp = omap_readw(OTG_OUTCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002320 tmp = omap_readw(OTG_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 seq_printf(s, "otg_test %04x" "\n", tmp);
David Brownell313980c2005-04-11 15:38:25 -07002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
2325static int proc_udc_show(struct seq_file *s, void *_)
2326{
2327 u32 tmp;
2328 struct omap_ep *ep;
2329 unsigned long flags;
2330
2331 spin_lock_irqsave(&udc->lock, flags);
2332
2333 seq_printf(s, "%s, version: " DRIVER_VERSION
2334#ifdef USE_ISO
2335 " (iso)"
2336#endif
2337 "%s\n",
2338 driver_desc,
2339 use_dma ? " (dma)" : "");
2340
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002341 tmp = omap_readw(UDC_REV) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 seq_printf(s,
2343 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2344 "hmc %d, transceiver %s\n",
2345 tmp >> 4, tmp & 0xf,
2346 fifo_mode,
2347 udc->driver ? udc->driver->driver.name : "(none)",
2348 HMC,
David Brownelle6a6e472006-12-10 11:47:04 -08002349 udc->transceiver
2350 ? udc->transceiver->label
Tony Lindgrenae372572012-05-21 12:01:11 -07002351 : (cpu_is_omap1710()
David Brownelle6a6e472006-12-10 11:47:04 -08002352 ? "external" : "(none)"));
Tony Lindgrenae372572012-05-21 12:01:11 -07002353 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2354 omap_readw(ULPD_CLOCK_CTRL),
2355 omap_readw(ULPD_SOFT_REQ),
2356 omap_readw(ULPD_STATUS_REQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 /* OTG controller registers */
2359 if (!cpu_is_omap15xx())
2360 proc_otg_show(s);
2361
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002362 tmp = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2364 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2365 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2366 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2367 (tmp & UDC_NAK_EN) ? " nak" : "",
2368 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2369 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2370 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2371 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
Felipe Balbi80dd1352012-05-29 14:26:09 +03002372 /* syscon2 is write-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 /* UDC controller registers */
2375 if (!(tmp & UDC_PULLUP_EN)) {
2376 seq_printf(s, "(suspended)\n");
2377 spin_unlock_irqrestore(&udc->lock, flags);
2378 return 0;
2379 }
2380
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002381 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2383 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2384 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2385 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2386 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2387 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2388 (tmp & UDC_SUS) ? " SUS" : "",
2389 (tmp & UDC_CFG) ? " CFG" : "",
2390 (tmp & UDC_ADD) ? " ADD" : "",
2391 (tmp & UDC_DEF) ? " DEF" : "",
2392 (tmp & UDC_ATT) ? " ATT" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002393 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2394 tmp = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2396 (tmp & UDC_SOF_IE) ? " sof" : "",
2397 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2398 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2399 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2400 (tmp & UDC_EP0_IE) ? " ep0" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002401 tmp = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2403 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2404 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2405 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002406 (tmp & UDC_IRQ_SOF) ? " sof" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2408 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2409 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2410 (tmp & UDC_SETUP) ? " setup" : "",
2411 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2412 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2413 if (use_dma) {
2414 unsigned i;
2415
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002416 tmp = omap_readw(UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2418 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2419 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2420 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2421
2422 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2423 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2424 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2425
2426 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2427 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2428 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2429
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002430 tmp = omap_readw(UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2432 if (tmp) {
2433 for (i = 0; i < 3; i++) {
2434 if ((tmp & (0x0f << (i * 4))) == 0)
2435 continue;
2436 seq_printf(s, "rxdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002437 omap_readw(UDC_RXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002440 tmp = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 seq_printf(s, "txdma_cfg %04x\n", tmp);
2442 if (tmp) {
2443 for (i = 0; i < 3; i++) {
2444 if (!(tmp & (0x0f << (i * 4))))
2445 continue;
2446 seq_printf(s, "txdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002447 omap_readw(UDC_TXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449 }
2450 }
2451
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002452 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (tmp & UDC_ATT) {
2454 proc_ep_show(s, &udc->ep[0]);
2455 if (tmp & UDC_ADD) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002456 list_for_each_entry(ep, &udc->gadget.ep_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ep.ep_list) {
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02002458 if (ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 proc_ep_show(s, ep);
2460 }
2461 }
2462 }
2463 spin_unlock_irqrestore(&udc->lock, flags);
2464 return 0;
2465}
2466
2467static int proc_udc_open(struct inode *inode, struct file *file)
2468{
David Brownell313980c2005-04-11 15:38:25 -07002469 return single_open(file, proc_udc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03002472static const struct file_operations proc_ops = {
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002473 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 .open = proc_udc_open,
2475 .read = seq_read,
2476 .llseek = seq_lseek,
2477 .release = single_release,
2478};
2479
2480static void create_proc_file(void)
2481{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002482 proc_create(proc_filename, 0, NULL, &proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
2485static void remove_proc_file(void)
2486{
David Brownell313980c2005-04-11 15:38:25 -07002487 remove_proc_entry(proc_filename, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
2490#else
2491
2492static inline void create_proc_file(void) {}
2493static inline void remove_proc_file(void) {}
2494
2495#endif
2496
2497/*-------------------------------------------------------------------------*/
2498
2499/* Before this controller can enumerate, we need to pick an endpoint
2500 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2501 * buffer space among the endpoints we'll be operating.
David Brownell65111082005-04-28 13:52:31 -07002502 *
2503 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002504 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
David Brownell65111082005-04-28 13:52:31 -07002505 * capability yet though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Felipe Balbidc1737c2012-05-29 14:33:30 +03002507static unsigned __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508omap_ep_setup(char *name, u8 addr, u8 type,
2509 unsigned buf, unsigned maxp, int dbuf)
2510{
2511 struct omap_ep *ep;
2512 u16 epn_rxtx = 0;
2513
2514 /* OUT endpoints first, then IN */
2515 ep = &udc->ep[addr & 0xf];
2516 if (addr & USB_DIR_IN)
2517 ep += 16;
2518
2519 /* in case of ep init table bugs */
2520 BUG_ON(ep->name[0]);
2521
2522 /* chip setup ... bit values are same for IN, OUT */
2523 if (type == USB_ENDPOINT_XFER_ISOC) {
2524 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002525 case 8:
2526 epn_rxtx = 0 << 12;
2527 break;
2528 case 16:
2529 epn_rxtx = 1 << 12;
2530 break;
2531 case 32:
2532 epn_rxtx = 2 << 12;
2533 break;
2534 case 64:
2535 epn_rxtx = 3 << 12;
2536 break;
2537 case 128:
2538 epn_rxtx = 4 << 12;
2539 break;
2540 case 256:
2541 epn_rxtx = 5 << 12;
2542 break;
2543 case 512:
2544 epn_rxtx = 6 << 12;
2545 break;
2546 default:
2547 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549 epn_rxtx |= UDC_EPN_RX_ISO;
2550 dbuf = 1;
2551 } else {
2552 /* double-buffering "not supported" on 15xx,
David Brownelle6a6e472006-12-10 11:47:04 -08002553 * and ignored for PIO-IN on newer chips
2554 * (for more reliable behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 */
Tony Lindgrenae372572012-05-21 12:01:11 -07002556 if (!use_dma || cpu_is_omap15xx())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 dbuf = 0;
2558
2559 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002560 case 8:
2561 epn_rxtx = 0 << 12;
2562 break;
2563 case 16:
2564 epn_rxtx = 1 << 12;
2565 break;
2566 case 32:
2567 epn_rxtx = 2 << 12;
2568 break;
2569 case 64:
2570 epn_rxtx = 3 << 12;
2571 break;
2572 default:
2573 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575 if (dbuf && addr)
2576 epn_rxtx |= UDC_EPN_RX_DB;
2577 init_timer(&ep->timer);
2578 ep->timer.function = pio_out_timer;
2579 ep->timer.data = (unsigned long) ep;
2580 }
2581 if (addr)
2582 epn_rxtx |= UDC_EPN_RX_VALID;
2583 BUG_ON(buf & 0x07);
2584 epn_rxtx |= buf >> 3;
2585
2586 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2587 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2588
2589 if (addr & USB_DIR_IN)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002590 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002592 omap_writew(epn_rxtx, UDC_EP_RX(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 /* next endpoint's buffer starts after this one's */
2595 buf += maxp;
2596 if (dbuf)
2597 buf += maxp;
2598 BUG_ON(buf > 2048);
2599
2600 /* set up driver data structures */
2601 BUG_ON(strlen(name) >= sizeof ep->name);
2602 strlcpy(ep->name, name, sizeof ep->name);
2603 INIT_LIST_HEAD(&ep->queue);
2604 INIT_LIST_HEAD(&ep->iso);
2605 ep->bEndpointAddress = addr;
2606 ep->bmAttributes = type;
2607 ep->double_buf = dbuf;
David Brownelle6a6e472006-12-10 11:47:04 -08002608 ep->udc = udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 ep->ep.name = ep->name;
2611 ep->ep.ops = &omap_ep_ops;
2612 ep->ep.maxpacket = ep->maxpacket = maxp;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002613 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 return buf;
2616}
2617
2618static void omap_udc_release(struct device *dev)
2619{
2620 complete(udc->done);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002621 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002622 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Felipe Balbidc1737c2012-05-29 14:33:30 +03002625static int __devinit
Heikki Krogerus86753812012-02-13 13:24:02 +02002626omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
2628 unsigned tmp, buf;
2629
2630 /* abolish any previous hardware state */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002631 omap_writew(0, UDC_SYSCON1);
2632 omap_writew(0, UDC_IRQ_EN);
2633 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2634 omap_writew(0, UDC_DMA_IRQ_EN);
2635 omap_writew(0, UDC_RXDMA_CFG);
2636 omap_writew(0, UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /* UDC_PULLUP_EN gates the chip clock */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002639 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Christoph Lametere94b1762006-12-06 20:33:17 -08002641 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (!udc)
2643 return -ENOMEM;
2644
Felipe Balbi80dd1352012-05-29 14:26:09 +03002645 spin_lock_init(&udc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 udc->gadget.ops = &omap_gadget_ops;
2648 udc->gadget.ep0 = &udc->ep[0].ep;
2649 INIT_LIST_HEAD(&udc->gadget.ep_list);
2650 INIT_LIST_HEAD(&udc->iso);
2651 udc->gadget.speed = USB_SPEED_UNKNOWN;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002652 udc->gadget.max_speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 udc->gadget.name = driver_name;
2654
2655 device_initialize(&udc->gadget.dev);
Kay Sievers0031a062008-05-02 06:02:41 +02002656 dev_set_name(&udc->gadget.dev, "gadget");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 udc->gadget.dev.release = omap_udc_release;
2658 udc->gadget.dev.parent = &odev->dev;
2659 if (use_dma)
2660 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2661
2662 udc->transceiver = xceiv;
2663
2664 /* ep0 is special; put it right after the SETUP buffer */
2665 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2666 8 /* after SETUP */, 64 /* maxpacket */, 0);
2667 list_del_init(&udc->ep[0].ep.ep_list);
2668
2669 /* initially disable all non-ep0 endpoints */
2670 for (tmp = 1; tmp < 15; tmp++) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002671 omap_writew(0, UDC_EP_RX(tmp));
2672 omap_writew(0, UDC_EP_TX(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
2674
Felipe Balbi80dd1352012-05-29 14:26:09 +03002675#define OMAP_BULK_EP(name, addr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 buf = omap_ep_setup(name "-bulk", addr, \
2677 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002678#define OMAP_INT_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 buf = omap_ep_setup(name "-int", addr, \
2680 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002681#define OMAP_ISO_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 buf = omap_ep_setup(name "-iso", addr, \
2683 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2684
2685 switch (fifo_mode) {
2686 case 0:
2687 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2688 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2689 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2690 break;
2691 case 1:
2692 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2693 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
David Brownell313980c2005-04-11 15:38:25 -07002694 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2697 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
David Brownell313980c2005-04-11 15:38:25 -07002698 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2701 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
David Brownell313980c2005-04-11 15:38:25 -07002702 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2705 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
David Brownell313980c2005-04-11 15:38:25 -07002706 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2709 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
David Brownell313980c2005-04-11 15:38:25 -07002710 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2711 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2714 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
David Brownell313980c2005-04-11 15:38:25 -07002715 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2716 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
David Brownell313980c2005-04-11 15:38:25 -07002718 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2719 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 break;
2722
2723#ifdef USE_ISO
2724 case 2: /* mixed iso/bulk */
2725 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2726 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2727 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2728 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2729
2730 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2731
2732 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2733 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2734 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2735 break;
2736 case 3: /* mixed bulk/iso */
2737 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2738 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2739 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2740
2741 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2742 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2743 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2744
2745 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2746 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2747 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2748 break;
2749#endif
2750
2751 /* add more modes as needed */
2752
2753 default:
2754 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2755 return -ENODEV;
2756 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002757 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2759 return 0;
2760}
2761
Felipe Balbidc1737c2012-05-29 14:33:30 +03002762static int __devinit omap_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 int status = -ENODEV;
2765 int hmc;
Heikki Krogerus86753812012-02-13 13:24:02 +02002766 struct usb_phy *xceiv = NULL;
David Brownell313980c2005-04-11 15:38:25 -07002767 const char *type = NULL;
Russell King3ae5eae2005-11-09 22:32:44 +00002768 struct omap_usb_config *config = pdev->dev.platform_data;
Tony Lindgrenae372572012-05-21 12:01:11 -07002769 struct clk *dc_clk = NULL;
2770 struct clk *hhc_clk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Felipe Balbi5b6d84b2012-05-29 14:27:52 +03002772 if (cpu_is_omap7xx())
2773 use_dma = 0;
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 /* NOTE: "knows" the order of the resources! */
David Brownelle6a6e472006-12-10 11:47:04 -08002776 if (!request_mem_region(pdev->resource[0].start,
Russell King3ae5eae2005-11-09 22:32:44 +00002777 pdev->resource[0].end - pdev->resource[0].start + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 driver_name)) {
2779 DBG("request_mem_region failed\n");
2780 return -EBUSY;
2781 }
2782
David Brownelle6a6e472006-12-10 11:47:04 -08002783 if (cpu_is_omap16xx()) {
2784 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2785 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2786 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2787 /* can't use omap_udc_enable_clock yet */
2788 clk_enable(dc_clk);
2789 clk_enable(hhc_clk);
2790 udelay(100);
2791 }
2792
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002793 if (cpu_is_omap7xx()) {
2794 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2795 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2796 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2797 /* can't use omap_udc_enable_clock yet */
2798 clk_enable(dc_clk);
2799 clk_enable(hhc_clk);
2800 udelay(100);
2801 }
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 INFO("OMAP UDC rev %d.%d%s\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002804 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 config->otg ? ", Mini-AB" : "");
2806
2807 /* use the mode given to us by board init code */
2808 if (cpu_is_omap15xx()) {
2809 hmc = HMC_1510;
2810 type = "(unknown)";
2811
David Brownell8a3c1f52007-03-21 12:26:32 -07002812 if (machine_without_vbus_sense()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /* just set up software VBUS detect, and then
2814 * later rig it so we always report VBUS.
2815 * FIXME without really sensing VBUS, we can't
2816 * know when to turn PULLUP_EN on/off; and that
2817 * means we always "need" the 48MHz clock.
2818 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002819 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2820 tmp &= ~VBUS_CTRL_1510;
2821 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 tmp |= VBUS_MODE_1510;
2823 tmp &= ~VBUS_CTRL_1510;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002824 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
2826 } else {
David Brownell65111082005-04-28 13:52:31 -07002827 /* The transceiver may package some GPIO logic or handle
2828 * loopback and/or transceiverless setup; if we find one,
2829 * use it. Except for OTG, we don't _need_ to talk to one;
2830 * but not having one probably means no VBUS detection.
2831 */
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002832 xceiv = usb_get_transceiver();
David Brownell65111082005-04-28 13:52:31 -07002833 if (xceiv)
2834 type = xceiv->label;
2835 else if (config->otg) {
2836 DBG("OTG requires external transceiver!\n");
2837 goto cleanup0;
2838 }
2839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 hmc = HMC_1610;
David Brownelle6a6e472006-12-10 11:47:04 -08002841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 switch (hmc) {
David Brownell313980c2005-04-11 15:38:25 -07002843 case 0: /* POWERUP DEFAULT == 0 */
2844 case 4:
2845 case 12:
2846 case 20:
2847 if (!cpu_is_omap1710()) {
2848 type = "integrated";
2849 break;
2850 }
2851 /* FALL THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 case 3:
2853 case 11:
2854 case 16:
2855 case 19:
2856 case 25:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (!xceiv) {
2858 DBG("external transceiver not registered!\n");
David Brownell313980c2005-04-11 15:38:25 -07002859 type = "unknown";
David Brownell65111082005-04-28 13:52:31 -07002860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 case 21: /* internal loopback */
David Brownell313980c2005-04-11 15:38:25 -07002863 type = "loopback";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 break;
2865 case 14: /* transceiverless */
David Brownell65111082005-04-28 13:52:31 -07002866 if (cpu_is_omap1710())
2867 goto bad_on_1710;
2868 /* FALL THROUGH */
2869 case 13:
2870 case 15:
David Brownell313980c2005-04-11 15:38:25 -07002871 type = "no";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 break;
2873
2874 default:
David Brownell65111082005-04-28 13:52:31 -07002875bad_on_1710:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 ERR("unrecognized UDC HMC mode %d\n", hmc);
David Brownell65111082005-04-28 13:52:31 -07002877 goto cleanup0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879 }
Tony Lindgrenae372572012-05-21 12:01:11 -07002880
David Brownell313980c2005-04-11 15:38:25 -07002881 INFO("hmc mode %d, %s transceiver\n", hmc, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /* a "gadget" abstracts/virtualizes the controller */
Russell King3ae5eae2005-11-09 22:32:44 +00002884 status = omap_udc_setup(pdev, xceiv);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002885 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 goto cleanup0;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002887
David Brownell313980c2005-04-11 15:38:25 -07002888 xceiv = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002889 /* "udc" is now valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 pullup_disable(udc);
2891#if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2892 udc->gadget.is_otg = (config->otg != 0);
2893#endif
2894
David Brownell65111082005-04-28 13:52:31 -07002895 /* starting with omap1710 es2.0, clear toggle is a separate bit */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002896 if (omap_readw(UDC_REV) >= 0x61)
David Brownell65111082005-04-28 13:52:31 -07002897 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2898 else
2899 udc->clr_halt = UDC_RESET_EP;
2900
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 /* USB general purpose IRQ: ep0, state changes, dma, etc */
Russell King3ae5eae2005-11-09 22:32:44 +00002902 status = request_irq(pdev->resource[1].start, omap_udc_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002903 0, driver_name, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002905 ERR("can't get irq %d, err %d\n",
2906 (int) pdev->resource[1].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto cleanup1;
2908 }
2909
2910 /* USB "non-iso" IRQ (PIO for all but ep0) */
Russell King3ae5eae2005-11-09 22:32:44 +00002911 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002912 0, "omap_udc pio", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002914 ERR("can't get irq %d, err %d\n",
2915 (int) pdev->resource[2].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 goto cleanup2;
2917 }
2918#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002919 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002920 0, "omap_udc iso", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002922 ERR("can't get irq %d, err %d\n",
2923 (int) pdev->resource[3].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 goto cleanup3;
2925 }
2926#endif
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002927 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002928 udc->dc_clk = dc_clk;
2929 udc->hhc_clk = hhc_clk;
2930 clk_disable(hhc_clk);
2931 clk_disable(dc_clk);
2932 }
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 create_proc_file();
David Brownelle6a6e472006-12-10 11:47:04 -08002935 status = device_add(&udc->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002936 if (status)
2937 goto cleanup4;
2938
2939 status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
David Brownelle6a6e472006-12-10 11:47:04 -08002940 if (!status)
2941 return status;
2942 /* If fail, fall through */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002943cleanup4:
2944 remove_proc_file();
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946#ifdef USE_ISO
2947cleanup3:
Russell King3ae5eae2005-11-09 22:32:44 +00002948 free_irq(pdev->resource[2].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949#endif
2950
2951cleanup2:
Russell King3ae5eae2005-11-09 22:32:44 +00002952 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954cleanup1:
Felipe Balbi80dd1352012-05-29 14:26:09 +03002955 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002956 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958cleanup0:
2959 if (xceiv)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002960 usb_put_transceiver(xceiv);
David Brownelle6a6e472006-12-10 11:47:04 -08002961
Tony Lindgrenae372572012-05-21 12:01:11 -07002962 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002963 clk_disable(hhc_clk);
2964 clk_disable(dc_clk);
2965 clk_put(hhc_clk);
2966 clk_put(dc_clk);
2967 }
2968
Russell King3ae5eae2005-11-09 22:32:44 +00002969 release_mem_region(pdev->resource[0].start,
2970 pdev->resource[0].end - pdev->resource[0].start + 1);
David Brownelle6a6e472006-12-10 11:47:04 -08002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return status;
2973}
2974
Felipe Balbidc1737c2012-05-29 14:33:30 +03002975static int __devexit omap_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002977 DECLARE_COMPLETION_ONSTACK(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 if (!udc)
2980 return -ENODEV;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002981
2982 usb_del_gadget_udc(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002983 if (udc->driver)
2984 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 udc->done = &done;
2987
2988 pullup_disable(udc);
2989 if (udc->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002990 usb_put_transceiver(udc->transceiver);
David Brownell313980c2005-04-11 15:38:25 -07002991 udc->transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002993 omap_writew(0, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 remove_proc_file();
2996
2997#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002998 free_irq(pdev->resource[3].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999#endif
Russell King3ae5eae2005-11-09 22:32:44 +00003000 free_irq(pdev->resource[2].start, udc);
3001 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
David Brownelle6a6e472006-12-10 11:47:04 -08003003 if (udc->dc_clk) {
3004 if (udc->clk_requested)
3005 omap_udc_enable_clock(0);
3006 clk_put(udc->hhc_clk);
3007 clk_put(udc->dc_clk);
3008 }
3009
Russell King3ae5eae2005-11-09 22:32:44 +00003010 release_mem_region(pdev->resource[0].start,
3011 pdev->resource[0].end - pdev->resource[0].start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 device_unregister(&udc->gadget.dev);
3014 wait_for_completion(&done);
3015
3016 return 0;
3017}
3018
David Brownell313980c2005-04-11 15:38:25 -07003019/* suspend/resume/wakeup from sysfs (echo > power/state) or when the
3020 * system is forced into deep sleep
3021 *
3022 * REVISIT we should probably reject suspend requests when there's a host
3023 * session active, rather than disconnecting, at least on boards that can
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003024 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
David Brownell313980c2005-04-11 15:38:25 -07003025 * make host resumes and VBUS detection trigger OMAP wakeup events; that
3026 * may involve talking to an external transceiver (e.g. isp1301).
3027 */
david-b@pacbell.net1d7beee2005-06-29 07:00:56 -07003028
Russell King3ae5eae2005-11-09 22:32:44 +00003029static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
David Brownell313980c2005-04-11 15:38:25 -07003031 u32 devstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003033 devstat = omap_readw(UDC_DEVSTAT);
David Brownell313980c2005-04-11 15:38:25 -07003034
3035 /* we're requesting 48 MHz clock if the pullup is enabled
3036 * (== we're attached to the host) and we're not suspended,
3037 * which would prevent entry to deep sleep...
3038 */
3039 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003040 WARNING("session active; suspend requires disconnect\n");
David Brownell313980c2005-04-11 15:38:25 -07003041 omap_pullup(&udc->gadget, 0);
3042 }
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
3045}
3046
Russell King3ae5eae2005-11-09 22:32:44 +00003047static int omap_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 DBG("resume + wakeup/SRP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 omap_pullup(&udc->gadget, 1);
3051
3052 /* maybe the host would enumerate us if we nudged it */
3053 msleep(100);
3054 return omap_wakeup(&udc->gadget);
3055}
3056
3057/*-------------------------------------------------------------------------*/
3058
Russell King3ae5eae2005-11-09 22:32:44 +00003059static struct platform_driver udc_driver = {
Felipe Balbidc1737c2012-05-29 14:33:30 +03003060 .probe = omap_udc_probe,
3061 .remove = __devexit_p(omap_udc_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 .suspend = omap_udc_suspend,
3063 .resume = omap_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003064 .driver = {
3065 .owner = THIS_MODULE,
3066 .name = (char *) driver_name,
3067 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068};
3069
Felipe Balbidc1737c2012-05-29 14:33:30 +03003070module_platform_driver(udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072MODULE_DESCRIPTION(DRIVER_DESC);
3073MODULE_LICENSE("GPL");
Kay Sieversf34c32f2008-04-10 21:29:21 -07003074MODULE_ALIAS("platform:omap_udc");