blob: f13bcdc756760eedd38c346bb85614b059a3938c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
3 *
4 * Copyright (C) 2004 Texas Instruments, Inc.
5 * Copyright (C) 2004-2005 David Brownell
6 *
Kyungmin Park527ea732007-11-21 15:13:15 -08007 * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#undef DEBUG
16#undef VERBOSE
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/ioport.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/timer.h>
27#include <linux/list.h>
28#include <linux/interrupt.h>
29#include <linux/proc_fs.h>
30#include <linux/mm.h>
31#include <linux/moduleparam.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
David Brownell5f848132006-12-16 15:34:53 -080033#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070034#include <linux/usb/gadget.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070035#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/dma-mapping.h>
David Brownelle6a6e472006-12-10 11:47:04 -080037#include <linux/clk.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070038#include <linux/prefetch.h>
Felipe Balbi80dd1352012-05-29 14:26:09 +030039#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44#include <asm/mach-types.h>
45
Tony Lindgrence491cf2009-10-20 09:40:47 -070046#include <plat/dma.h>
47#include <plat/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include "omap_udc.h"
50
51#undef USB_TRACE
52
53/* bulk DMA seems to be behaving for both IN and OUT */
54#define USE_DMA
55
56/* ISO too */
57#define USE_ISO
58
59#define DRIVER_DESC "OMAP UDC driver"
60#define DRIVER_VERSION "4 October 2004"
61
62#define DMA_ADDR_INVALID (~(dma_addr_t)0)
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
66 * D+ pullup to allow enumeration. That's too early for the gadget
67 * framework to use from usb_endpoint_enable(), which happens after
68 * enumeration as part of activating an interface. (But if we add an
69 * optional new "UDC not yet running" state to the gadget driver model,
70 * even just during driver binding, the endpoint autoconfig logic is the
71 * natural spot to manufacture new endpoints.)
72 *
73 * So instead of using endpoint enable calls to control the hardware setup,
74 * this driver defines a "fifo mode" parameter. It's used during driver
75 * initialization to choose among a set of pre-defined endpoint configs.
76 * See omap_udc_setup() for available modes, or to add others. That code
77 * lives in an init section, so use this driver as a module if you need
78 * to change the fifo mode after the kernel boots.
79 *
80 * Gadget drivers normally ignore endpoints they don't care about, and
81 * won't include them in configuration descriptors. That means only
82 * misbehaving hosts would even notice they exist.
83 */
84#ifdef USE_ISO
85static unsigned fifo_mode = 3;
86#else
Felipe Balbi80dd1352012-05-29 14:26:09 +030087static unsigned fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89
90/* "modprobe omap_udc fifo_mode=42", or else as a kernel
91 * boot parameter "omap_udc:fifo_mode=42"
92 */
Felipe Balbi80dd1352012-05-29 14:26:09 +030093module_param(fifo_mode, uint, 0);
94MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96#ifdef USE_DMA
Rusty Russell90ab5ee2012-01-13 09:32:20 +103097static bool use_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* "modprobe omap_udc use_dma=y", or else as a kernel
100 * boot parameter "omap_udc:use_dma=y"
101 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300102module_param(use_dma, bool, 0);
103MODULE_PARM_DESC(use_dma, "enable/disable DMA");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#else /* !USE_DMA */
105
106/* save a bit of code */
107#define use_dma 0
108#endif /* !USE_DMA */
109
110
Felipe Balbi80dd1352012-05-29 14:26:09 +0300111static const char driver_name[] = "omap_udc";
112static const char driver_desc[] = DRIVER_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/*-------------------------------------------------------------------------*/
115
116/* there's a notion of "current endpoint" for modifying endpoint
David Brownelle6a6e472006-12-10 11:47:04 -0800117 * state, and PIO access to its FIFO.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
120static void use_ep(struct omap_ep *ep, u16 select)
121{
122 u16 num = ep->bEndpointAddress & 0x0f;
123
124 if (ep->bEndpointAddress & USB_DIR_IN)
125 num |= UDC_EP_DIR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300126 omap_writew(num | select, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* when select, MUST deselect later !! */
128}
129
130static inline void deselect_ep(void)
131{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300132 u16 w;
133
134 w = omap_readw(UDC_EP_NUM);
135 w &= ~UDC_EP_SEL;
136 omap_writew(w, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 /* 6 wait states before TX will happen */
138}
139
140static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
141
142/*-------------------------------------------------------------------------*/
143
144static int omap_ep_enable(struct usb_ep *_ep,
145 const struct usb_endpoint_descriptor *desc)
146{
147 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
148 struct omap_udc *udc;
149 unsigned long flags;
150 u16 maxp;
151
152 /* catch various bogus parameters */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200153 if (!_ep || !desc || ep->ep.desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 || desc->bDescriptorType != USB_DT_ENDPOINT
155 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700156 || ep->maxpacket < usb_endpoint_maxp(desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800157 DBG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return -EINVAL;
159 }
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700160 maxp = usb_endpoint_maxp(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
162 && maxp != ep->maxpacket)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700163 || usb_endpoint_maxp(desc) > ep->maxpacket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800165 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return -ERANGE;
167 }
168
169#ifdef USE_ISO
170 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
171 && desc->bInterval != 1)) {
172 /* hardware wants period = 1; USB allows 2^(Interval-1) */
173 DBG("%s, unsupported ISO period %dms\n", _ep->name,
174 1 << (desc->bInterval - 1));
175 return -EDOM;
176 }
177#else
178 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
179 DBG("%s, ISO nyet\n", _ep->name);
180 return -EDOM;
181 }
182#endif
183
184 /* xfer types must match, except that interrupt ~= bulk */
185 if (ep->bmAttributes != desc->bmAttributes
186 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
187 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800188 DBG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return -EINVAL;
190 }
191
192 udc = ep->udc;
193 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800194 DBG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -ESHUTDOWN;
196 }
197
198 spin_lock_irqsave(&udc->lock, flags);
199
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200200 ep->ep.desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ep->irqs = 0;
202 ep->stopped = 0;
203 ep->ep.maxpacket = maxp;
204
205 /* set endpoint to initial state */
206 ep->dma_channel = 0;
207 ep->has_dma = 0;
208 ep->lch = -1;
209 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300210 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ep->ackwait = 0;
212 deselect_ep();
213
214 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
215 list_add(&ep->iso, &udc->iso);
216
217 /* maybe assign a DMA channel to this endpoint */
218 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
219 /* FIXME ISO can dma, but prefers first channel */
220 dma_channel_claim(ep, 0);
221
222 /* PIO OUT may RX packets */
223 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
224 && !ep->has_dma
225 && !(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300226 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ep->ackwait = 1 + ep->double_buf;
228 }
229
230 spin_unlock_irqrestore(&udc->lock, flags);
231 VDBG("%s enabled\n", _ep->name);
232 return 0;
233}
234
235static void nuke(struct omap_ep *, int status);
236
237static int omap_ep_disable(struct usb_ep *_ep)
238{
239 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
240 unsigned long flags;
241
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200242 if (!_ep || !ep->ep.desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800243 DBG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 _ep ? ep->ep.name : NULL);
245 return -EINVAL;
246 }
247
248 spin_lock_irqsave(&ep->udc->lock, flags);
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200249 ep->ep.desc = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +0300250 nuke(ep, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 ep->ep.maxpacket = ep->maxpacket;
252 ep->has_dma = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300253 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 list_del_init(&ep->iso);
255 del_timer(&ep->timer);
256
257 spin_unlock_irqrestore(&ep->udc->lock, flags);
258
259 VDBG("%s disabled\n", _ep->name);
260 return 0;
261}
262
263/*-------------------------------------------------------------------------*/
264
265static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400266omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct omap_req *req;
269
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800270 req = kzalloc(sizeof(*req), gfp_flags);
Felipe Balbi70617db2012-05-29 14:36:42 +0300271 if (!req)
272 return NULL;
273
274 req->req.dma = DMA_ADDR_INVALID;
275 INIT_LIST_HEAD(&req->queue);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return &req->req;
278}
279
280static void
281omap_free_request(struct usb_ep *ep, struct usb_request *_req)
282{
283 struct omap_req *req = container_of(_req, struct omap_req, req);
284
285 if (_req)
Felipe Balbi80dd1352012-05-29 14:26:09 +0300286 kfree(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*-------------------------------------------------------------------------*/
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static void
292done(struct omap_ep *ep, struct omap_req *req, int status)
293{
294 unsigned stopped = ep->stopped;
295
296 list_del_init(&req->queue);
297
298 if (req->req.status == -EINPROGRESS)
299 req->req.status = status;
300 else
301 status = req->req.status;
302
303 if (use_dma && ep->has_dma) {
304 if (req->mapped) {
305 dma_unmap_single(ep->udc->gadget.dev.parent,
306 req->req.dma, req->req.length,
307 (ep->bEndpointAddress & USB_DIR_IN)
308 ? DMA_TO_DEVICE
309 : DMA_FROM_DEVICE);
310 req->req.dma = DMA_ADDR_INVALID;
311 req->mapped = 0;
312 } else
313 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
314 req->req.dma, req->req.length,
315 (ep->bEndpointAddress & USB_DIR_IN)
316 ? DMA_TO_DEVICE
317 : DMA_FROM_DEVICE);
318 }
319
320#ifndef USB_TRACE
321 if (status && status != -ESHUTDOWN)
322#endif
323 VDBG("complete %s req %p stat %d len %u/%u\n",
324 ep->ep.name, &req->req, status,
325 req->req.actual, req->req.length);
326
327 /* don't modify queue heads during completion callback */
328 ep->stopped = 1;
329 spin_unlock(&ep->udc->lock);
330 req->req.complete(&ep->ep, &req->req);
331 spin_lock(&ep->udc->lock);
332 ep->stopped = stopped;
333}
334
335/*-------------------------------------------------------------------------*/
336
David Brownell313980c2005-04-11 15:38:25 -0700337#define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
338#define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
341#define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
342
David Brownelle6a6e472006-12-10 11:47:04 -0800343static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344write_packet(u8 *buf, struct omap_req *req, unsigned max)
345{
346 unsigned len;
347 u16 *wp;
348
349 len = min(req->req.length - req->req.actual, max);
350 req->req.actual += len;
351
352 max = len;
353 if (likely((((int)buf) & 1) == 0)) {
354 wp = (u16 *)buf;
355 while (max >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300356 omap_writew(*wp++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 max -= 2;
358 }
359 buf = (u8 *)wp;
360 }
361 while (max--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300362 omap_writeb(*buf++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return len;
364}
365
Felipe Balbi80dd1352012-05-29 14:26:09 +0300366/* FIXME change r/w fifo calling convention */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368
Felipe Balbi80dd1352012-05-29 14:26:09 +0300369/* return: 0 = still running, 1 = completed, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int write_fifo(struct omap_ep *ep, struct omap_req *req)
371{
372 u8 *buf;
373 unsigned count;
374 int is_last;
375 u16 ep_stat;
376
377 buf = req->req.buf + req->req.actual;
378 prefetch(buf);
379
380 /* PIO-IN isn't double buffered except for iso */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300381 ep_stat = omap_readw(UDC_STAT_FLG);
David Brownell313980c2005-04-11 15:38:25 -0700382 if (ep_stat & UDC_FIFO_UNWRITABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return 0;
384
385 count = ep->ep.maxpacket;
386 count = write_packet(buf, req, count);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300387 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 ep->ackwait = 1;
389
390 /* last packet is often short (sometimes a zlp) */
391 if (count != ep->ep.maxpacket)
392 is_last = 1;
393 else if (req->req.length == req->req.actual
394 && !req->req.zero)
395 is_last = 1;
396 else
397 is_last = 0;
398
399 /* NOTE: requests complete when all IN data is in a
400 * FIFO (or sometimes later, if a zlp was needed).
401 * Use usb_ep_fifo_status() where needed.
402 */
403 if (is_last)
404 done(ep, req, 0);
405 return is_last;
406}
407
David Brownelle6a6e472006-12-10 11:47:04 -0800408static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409read_packet(u8 *buf, struct omap_req *req, unsigned avail)
410{
411 unsigned len;
412 u16 *wp;
413
414 len = min(req->req.length - req->req.actual, avail);
415 req->req.actual += len;
416 avail = len;
417
418 if (likely((((int)buf) & 1) == 0)) {
419 wp = (u16 *)buf;
420 while (avail >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300421 *wp++ = omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 avail -= 2;
423 }
424 buf = (u8 *)wp;
425 }
426 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300427 *buf++ = omap_readb(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return len;
429}
430
Felipe Balbi80dd1352012-05-29 14:26:09 +0300431/* return: 0 = still running, 1 = queue empty, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432static int read_fifo(struct omap_ep *ep, struct omap_req *req)
433{
434 u8 *buf;
435 unsigned count, avail;
436 int is_last;
437
438 buf = req->req.buf + req->req.actual;
439 prefetchw(buf);
440
441 for (;;) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300442 u16 ep_stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 is_last = 0;
445 if (ep_stat & FIFO_EMPTY) {
446 if (!ep->double_buf)
447 break;
448 ep->fnf = 1;
449 }
450 if (ep_stat & UDC_EP_HALTED)
451 break;
452
David Brownell313980c2005-04-11 15:38:25 -0700453 if (ep_stat & UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 avail = ep->ep.maxpacket;
455 else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300456 avail = omap_readw(UDC_RXFSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ep->fnf = ep->double_buf;
458 }
459 count = read_packet(buf, req, avail);
460
461 /* partial packet reads may not be errors */
462 if (count < ep->ep.maxpacket) {
463 is_last = 1;
464 /* overflowed this request? flush extra data */
465 if (count != avail) {
466 req->req.status = -EOVERFLOW;
467 avail -= count;
468 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300469 omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 } else if (req->req.length == req->req.actual)
472 is_last = 1;
473 else
474 is_last = 0;
475
476 if (!ep->bEndpointAddress)
477 break;
478 if (is_last)
479 done(ep, req, 0);
480 break;
481 }
482 return is_last;
483}
484
485/*-------------------------------------------------------------------------*/
486
487static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
488{
489 dma_addr_t end;
490
491 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
492 * the last transfer's bytecount by more than a FIFO's worth.
493 */
494 if (cpu_is_omap15xx())
495 return 0;
496
Tony Lindgren0499bde2008-07-03 12:24:36 +0300497 end = omap_get_dma_src_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (end == ep->dma_counter)
499 return 0;
500
501 end |= start & (0xffff << 16);
502 if (end < start)
503 end += 0x10000;
504 return end - start;
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
508{
509 dma_addr_t end;
510
Tony Lindgren0499bde2008-07-03 12:24:36 +0300511 end = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (end == ep->dma_counter)
513 return 0;
514
515 end |= start & (0xffff << 16);
516 if (cpu_is_omap15xx())
517 end++;
518 if (end < start)
519 end += 0x10000;
520 return end - start;
521}
522
523
524/* Each USB transfer request using DMA maps to one or more DMA transfers.
525 * When DMA completion isn't request completion, the UDC continues with
526 * the next DMA transfer for that USB transfer.
527 */
528
529static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
530{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300531 u16 txdma_ctrl, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned length = req->req.length - req->req.actual;
533 const int sync_mode = cpu_is_omap15xx()
534 ? OMAP_DMA_SYNC_FRAME
535 : OMAP_DMA_SYNC_ELEMENT;
Kyungmin Park527ea732007-11-21 15:13:15 -0800536 int dma_trigger = 0;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* measure length in either bytes or packets */
David Brownell65111082005-04-28 13:52:31 -0700539 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
541 txdma_ctrl = UDC_TXN_EOT | length;
542 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
Kyungmin Park527ea732007-11-21 15:13:15 -0800543 length, 1, sync_mode, dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else {
545 length = min(length / ep->maxpacket,
546 (unsigned) UDC_TXN_TSC + 1);
David Brownelle6a6e472006-12-10 11:47:04 -0800547 txdma_ctrl = length;
David Brownell65111082005-04-28 13:52:31 -0700548 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
David Brownelle6a6e472006-12-10 11:47:04 -0800549 ep->ep.maxpacket >> 1, length, sync_mode,
Kyungmin Park527ea732007-11-21 15:13:15 -0800550 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 length *= ep->maxpacket;
552 }
553 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800554 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
555 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 omap_start_dma(ep->lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300558 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300559 w = omap_readw(UDC_DMA_IRQ_EN);
560 w |= UDC_TX_DONE_IE(ep->dma_channel);
561 omap_writew(w, UDC_DMA_IRQ_EN);
562 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 req->dma_bytes = length;
564}
565
566static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
567{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300568 u16 w;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (status == 0) {
571 req->req.actual += req->dma_bytes;
572
573 /* return if this request needs to send data or zlp */
574 if (req->req.actual < req->req.length)
575 return;
576 if (req->req.zero
577 && req->dma_bytes != 0
578 && (req->req.actual % ep->maxpacket) == 0)
579 return;
580 } else
581 req->req.actual += dma_src_len(ep, req->req.dma
582 + req->req.actual);
583
584 /* tx completion */
585 omap_stop_dma(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300586 w = omap_readw(UDC_DMA_IRQ_EN);
587 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
588 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 done(ep, req, status);
590}
591
592static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
593{
Kyungmin Park527ea732007-11-21 15:13:15 -0800594 unsigned packets = req->req.length - req->req.actual;
595 int dma_trigger = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300596 u16 w;
Kyungmin Park527ea732007-11-21 15:13:15 -0800597
Tony Lindgrenae372572012-05-21 12:01:11 -0700598 /* set up this DMA transfer, enable the fifo, start */
599 packets /= ep->ep.maxpacket;
600 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
601 req->dma_bytes = packets * ep->ep.maxpacket;
602 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
603 ep->ep.maxpacket >> 1, packets,
604 OMAP_DMA_SYNC_ELEMENT,
605 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800607 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
608 0, 0);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300609 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300611 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
612 w = omap_readw(UDC_DMA_IRQ_EN);
613 w |= UDC_RX_EOT_IE(ep->dma_channel);
614 omap_writew(w, UDC_DMA_IRQ_EN);
615 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
616 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 omap_start_dma(ep->lch);
619}
620
621static void
David Brownellcb97c5c2005-10-16 15:06:51 -0700622finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300624 u16 count, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 if (status == 0)
627 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
628 count = dma_dest_len(ep, req->req.dma + req->req.actual);
629 count += req->req.actual;
David Brownellcb97c5c2005-10-16 15:06:51 -0700630 if (one)
631 count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (count <= req->req.length)
633 req->req.actual = count;
634
635 if (count != req->dma_bytes || status)
636 omap_stop_dma(ep->lch);
637
638 /* if this wasn't short, request may need another transfer */
639 else if (req->req.actual < req->req.length)
640 return;
641
642 /* rx completion */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300643 w = omap_readw(UDC_DMA_IRQ_EN);
644 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
645 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 done(ep, req, status);
647}
648
649static void dma_irq(struct omap_udc *udc, u16 irq_src)
650{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300651 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct omap_ep *ep;
653 struct omap_req *req;
654
655 /* IN dma: tx to host */
656 if (irq_src & UDC_TXN_DONE) {
657 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
658 ep->irqs++;
659 /* can see TXN_DONE after dma abort */
660 if (!list_empty(&ep->queue)) {
661 req = container_of(ep->queue.next,
662 struct omap_req, queue);
663 finish_in_dma(ep, req, 0);
664 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300665 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Felipe Balbi80dd1352012-05-29 14:26:09 +0300667 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 req = container_of(ep->queue.next,
669 struct omap_req, queue);
670 next_in_dma(ep, req);
671 }
672 }
673
674 /* OUT dma: rx from host */
675 if (irq_src & UDC_RXN_EOT) {
676 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
677 ep->irqs++;
678 /* can see RXN_EOT after dma abort */
679 if (!list_empty(&ep->queue)) {
680 req = container_of(ep->queue.next,
681 struct omap_req, queue);
David Brownellcb97c5c2005-10-16 15:06:51 -0700682 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300684 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Felipe Balbi80dd1352012-05-29 14:26:09 +0300686 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 req = container_of(ep->queue.next,
688 struct omap_req, queue);
689 next_out_dma(ep, req);
690 }
691 }
692
693 if (irq_src & UDC_RXN_CNT) {
694 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
695 ep->irqs++;
696 /* omap15xx does this unasked... */
697 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300698 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700}
701
702static void dma_error(int lch, u16 ch_status, void *data)
703{
704 struct omap_ep *ep = data;
705
706 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700707 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
709
710 /* complete current transfer ... */
711}
712
713static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
714{
715 u16 reg;
716 int status, restart, is_in;
Kyungmin Park527ea732007-11-21 15:13:15 -0800717 int dma_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 is_in = ep->bEndpointAddress & USB_DIR_IN;
720 if (is_in)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300721 reg = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300723 reg = omap_readw(UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700724 reg |= UDC_DMA_REQ; /* "pulse" activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 ep->dma_channel = 0;
727 ep->lch = -1;
728 if (channel == 0 || channel > 3) {
729 if ((reg & 0x0f00) == 0)
730 channel = 3;
731 else if ((reg & 0x00f0) == 0)
732 channel = 2;
733 else if ((reg & 0x000f) == 0) /* preferred for ISO */
734 channel = 1;
735 else {
736 status = -EMLINK;
737 goto just_restart;
738 }
739 }
740 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
741 ep->dma_channel = channel;
742
743 if (is_in) {
Tony Lindgrenae372572012-05-21 12:01:11 -0700744 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800745 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 ep->ep.name, dma_error, ep, &ep->lch);
747 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300748 omap_writew(reg, UDC_TXDMA_CFG);
Kyungmin Park527ea732007-11-21 15:13:15 -0800749 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700750 omap_set_dma_src_burst_mode(ep->lch,
751 OMAP_DMA_DATA_BURST_4);
752 omap_set_dma_src_data_pack(ep->lch, 1);
753 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 omap_set_dma_dest_params(ep->lch,
755 OMAP_DMA_PORT_TIPB,
756 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700757 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800758 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 } else {
Tony Lindgrenae372572012-05-21 12:01:11 -0700761 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800762 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 ep->ep.name, dma_error, ep, &ep->lch);
764 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300765 omap_writew(reg, UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700766 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 omap_set_dma_src_params(ep->lch,
768 OMAP_DMA_PORT_TIPB,
769 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700770 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800771 0, 0);
Kyungmin Park527ea732007-11-21 15:13:15 -0800772 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700773 omap_set_dma_dest_burst_mode(ep->lch,
774 OMAP_DMA_DATA_BURST_4);
775 omap_set_dma_dest_data_pack(ep->lch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 }
778 if (status)
779 ep->dma_channel = 0;
780 else {
781 ep->has_dma = 1;
782 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
783
784 /* channel type P: hw synch (fifo) */
Tony Lindgrenae372572012-05-21 12:01:11 -0700785 if (!cpu_is_omap15xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300786 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789just_restart:
790 /* restart any queue, even if the claim failed */
791 restart = !ep->stopped && !list_empty(&ep->queue);
792
793 if (status)
794 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
795 restart ? " (restart)" : "");
796 else
797 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
798 is_in ? 't' : 'r',
799 ep->dma_channel - 1, ep->lch,
800 restart ? " (restart)" : "");
801
802 if (restart) {
803 struct omap_req *req;
804 req = container_of(ep->queue.next, struct omap_req, queue);
805 if (ep->has_dma)
806 (is_in ? next_in_dma : next_out_dma)(ep, req);
807 else {
808 use_ep(ep, UDC_EP_SEL);
809 (is_in ? write_fifo : read_fifo)(ep, req);
810 deselect_ep();
811 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300812 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ep->ackwait = 1 + ep->double_buf;
814 }
815 /* IN: 6 wait states before it'll tx */
816 }
817 }
818}
819
820static void dma_channel_release(struct omap_ep *ep)
821{
822 int shift = 4 * (ep->dma_channel - 1);
823 u16 mask = 0x0f << shift;
824 struct omap_req *req;
825 int active;
826
827 /* abort any active usb transfer request */
828 if (!list_empty(&ep->queue))
829 req = container_of(ep->queue.next, struct omap_req, queue);
830 else
David Brownell313980c2005-04-11 15:38:25 -0700831 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Tony Lindgren0499bde2008-07-03 12:24:36 +0300833 active = omap_get_dma_active_status(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
836 active ? "active" : "idle",
837 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
838 ep->dma_channel - 1, req);
839
David Brownell65111082005-04-28 13:52:31 -0700840 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
841 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
842 */
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* wait till current packet DMA finishes, and fifo empties */
845 if (ep->bEndpointAddress & USB_DIR_IN) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300846 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
847 UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (req) {
850 finish_in_dma(ep, req, -ECONNRESET);
851
852 /* clear FIFO; hosts probably won't empty it */
853 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300854 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 deselect_ep();
856 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300857 while (omap_readw(UDC_TXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 udelay(10);
859 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300860 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
861 UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* dma empties the fifo */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300864 while (omap_readw(UDC_RXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 udelay(10);
866 if (req)
David Brownellcb97c5c2005-10-16 15:06:51 -0700867 finish_out_dma(ep, req, -ECONNRESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 omap_free_dma(ep->lch);
870 ep->dma_channel = 0;
871 ep->lch = -1;
872 /* has_dma still set, till endpoint is fully quiesced */
873}
874
875
876/*-------------------------------------------------------------------------*/
877
878static int
Al Viro55016f12005-10-21 03:21:58 -0400879omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
882 struct omap_req *req = container_of(_req, struct omap_req, req);
883 struct omap_udc *udc;
884 unsigned long flags;
885 int is_iso = 0;
886
887 /* catch various bogus parameters */
888 if (!_req || !req->req.complete || !req->req.buf
889 || !list_empty(&req->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800890 DBG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EINVAL;
892 }
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200893 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800894 DBG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return -EINVAL;
896 }
897 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
898 if (req->req.length > ep->ep.maxpacket)
899 return -EMSGSIZE;
900 is_iso = 1;
901 }
902
903 /* this isn't bogus, but OMAP DMA isn't the only hardware to
904 * have a hard time with partial packet reads... reject it.
905 */
906 if (use_dma
907 && ep->has_dma
908 && ep->bEndpointAddress != 0
909 && (ep->bEndpointAddress & USB_DIR_IN) == 0
910 && (req->req.length % ep->ep.maxpacket) != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800911 DBG("%s, no partial packet OUT reads\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return -EMSGSIZE;
913 }
914
915 udc = ep->udc;
916 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
917 return -ESHUTDOWN;
918
919 if (use_dma && ep->has_dma) {
920 if (req->req.dma == DMA_ADDR_INVALID) {
921 req->req.dma = dma_map_single(
922 ep->udc->gadget.dev.parent,
923 req->req.buf,
924 req->req.length,
925 (ep->bEndpointAddress & USB_DIR_IN)
926 ? DMA_TO_DEVICE
927 : DMA_FROM_DEVICE);
928 req->mapped = 1;
929 } else {
930 dma_sync_single_for_device(
931 ep->udc->gadget.dev.parent,
932 req->req.dma, req->req.length,
933 (ep->bEndpointAddress & USB_DIR_IN)
934 ? DMA_TO_DEVICE
935 : DMA_FROM_DEVICE);
936 req->mapped = 0;
937 }
938 }
939
940 VDBG("%s queue req %p, len %d buf %p\n",
941 ep->ep.name, _req, _req->length, _req->buf);
942
943 spin_lock_irqsave(&udc->lock, flags);
944
945 req->req.status = -EINPROGRESS;
946 req->req.actual = 0;
947
948 /* maybe kickstart non-iso i/o queues */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300949 if (is_iso) {
950 u16 w;
951
952 w = omap_readw(UDC_IRQ_EN);
953 w |= UDC_SOF_IE;
954 omap_writew(w, UDC_IRQ_EN);
955 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 int is_in;
957
958 if (ep->bEndpointAddress == 0) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300959 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_unlock_irqrestore(&udc->lock, flags);
961 return -EL2HLT;
962 }
963
964 /* empty DATA stage? */
965 is_in = udc->ep0_in;
966 if (!req->req.length) {
967
968 /* chip became CONFIGURED or ADDRESSED
969 * earlier; drivers may already have queued
970 * requests to non-control endpoints
971 */
972 if (udc->ep0_set_config) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300973 u16 irq_en = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
976 if (!udc->ep0_reset_config)
977 irq_en |= UDC_EPN_RX_IE
978 | UDC_EPN_TX_IE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300979 omap_writew(irq_en, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
David Brownell313980c2005-04-11 15:38:25 -0700982 /* STATUS for zero length DATA stages is
983 * always an IN ... even for IN transfers,
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800984 * a weird case which seem to stall OMAP.
David Brownell313980c2005-04-11 15:38:25 -0700985 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300986 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
987 UDC_EP_NUM);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300988 omap_writew(UDC_CLR_EP, UDC_CTRL);
989 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
990 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 /* cleanup */
993 udc->ep0_pending = 0;
994 done(ep, req, 0);
David Brownell313980c2005-04-11 15:38:25 -0700995 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /* non-empty DATA stage */
998 } else if (is_in) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300999 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1000 UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 } else {
1002 if (udc->ep0_setup)
1003 goto irq_wait;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001004 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006 } else {
1007 is_in = ep->bEndpointAddress & USB_DIR_IN;
1008 if (!ep->has_dma)
1009 use_ep(ep, UDC_EP_SEL);
1010 /* if ISO: SOF IRQs must be enabled/disabled! */
1011 }
1012
1013 if (ep->has_dma)
1014 (is_in ? next_in_dma : next_out_dma)(ep, req);
1015 else if (req) {
1016 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
David Brownell313980c2005-04-11 15:38:25 -07001017 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 deselect_ep();
1019 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001020 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ep->ackwait = 1 + ep->double_buf;
1022 }
1023 /* IN: 6 wait states before it'll tx */
1024 }
1025 }
1026
1027irq_wait:
1028 /* irq handler advances the queue */
David Brownell313980c2005-04-11 15:38:25 -07001029 if (req != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 list_add_tail(&req->queue, &ep->queue);
1031 spin_unlock_irqrestore(&udc->lock, flags);
1032
1033 return 0;
1034}
1035
1036static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1037{
1038 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1039 struct omap_req *req;
1040 unsigned long flags;
1041
1042 if (!_ep || !_req)
1043 return -EINVAL;
1044
1045 spin_lock_irqsave(&ep->udc->lock, flags);
1046
1047 /* make sure it's actually queued on this endpoint */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001048 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (&req->req == _req)
1050 break;
1051 }
1052 if (&req->req != _req) {
1053 spin_unlock_irqrestore(&ep->udc->lock, flags);
1054 return -EINVAL;
1055 }
1056
1057 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1058 int channel = ep->dma_channel;
1059
1060 /* releasing the channel cancels the request,
1061 * reclaiming the channel restarts the queue
1062 */
1063 dma_channel_release(ep);
1064 dma_channel_claim(ep, channel);
David Brownelle6a6e472006-12-10 11:47:04 -08001065 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 done(ep, req, -ECONNRESET);
1067 spin_unlock_irqrestore(&ep->udc->lock, flags);
1068 return 0;
1069}
1070
1071/*-------------------------------------------------------------------------*/
1072
1073static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1074{
1075 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1076 unsigned long flags;
1077 int status = -EOPNOTSUPP;
1078
1079 spin_lock_irqsave(&ep->udc->lock, flags);
1080
1081 /* just use protocol stalls for ep0; real halts are annoying */
1082 if (ep->bEndpointAddress == 0) {
1083 if (!ep->udc->ep0_pending)
1084 status = -EINVAL;
1085 else if (value) {
1086 if (ep->udc->ep0_set_config) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001087 WARNING("error changing config?\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001088 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001090 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 ep->udc->ep0_pending = 0;
1092 status = 0;
1093 } else /* NOP */
1094 status = 0;
1095
1096 /* otherwise, all active non-ISO endpoints can halt */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001097 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* IN endpoints must already be idle */
1100 if ((ep->bEndpointAddress & USB_DIR_IN)
David Brownelle6a6e472006-12-10 11:47:04 -08001101 && !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 status = -EAGAIN;
1103 goto done;
1104 }
1105
1106 if (value) {
1107 int channel;
1108
1109 if (use_dma && ep->dma_channel
1110 && !list_empty(&ep->queue)) {
1111 channel = ep->dma_channel;
1112 dma_channel_release(ep);
1113 } else
1114 channel = 0;
1115
1116 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001117 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1118 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 status = 0;
1120 } else
1121 status = -EAGAIN;
1122 deselect_ep();
1123
1124 if (channel)
1125 dma_channel_claim(ep, channel);
1126 } else {
1127 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001128 omap_writew(ep->udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 ep->ackwait = 0;
1130 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001131 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 ep->ackwait = 1 + ep->double_buf;
1133 }
1134 }
1135 }
1136done:
1137 VDBG("%s %s halt stat %d\n", ep->ep.name,
1138 value ? "set" : "clear", status);
1139
1140 spin_unlock_irqrestore(&ep->udc->lock, flags);
1141 return status;
1142}
1143
1144static struct usb_ep_ops omap_ep_ops = {
1145 .enable = omap_ep_enable,
1146 .disable = omap_ep_disable,
1147
1148 .alloc_request = omap_alloc_request,
1149 .free_request = omap_free_request,
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 .queue = omap_ep_queue,
1152 .dequeue = omap_ep_dequeue,
1153
1154 .set_halt = omap_ep_set_halt,
Felipe Balbi80dd1352012-05-29 14:26:09 +03001155 /* fifo_status ... report bytes in fifo */
1156 /* fifo_flush ... flush fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157};
1158
1159/*-------------------------------------------------------------------------*/
1160
1161static int omap_get_frame(struct usb_gadget *gadget)
1162{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001163 u16 sof = omap_readw(UDC_SOF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1165}
1166
1167static int omap_wakeup(struct usb_gadget *gadget)
1168{
1169 struct omap_udc *udc;
1170 unsigned long flags;
1171 int retval = -EHOSTUNREACH;
1172
1173 udc = container_of(gadget, struct omap_udc, gadget);
1174
1175 spin_lock_irqsave(&udc->lock, flags);
1176 if (udc->devstat & UDC_SUS) {
1177 /* NOTE: OTG spec erratum says that OTG devices may
1178 * issue wakeups without host enable.
1179 */
1180 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1181 DBG("remote wakeup...\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001182 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 retval = 0;
1184 }
1185
1186 /* NOTE: non-OTG systems may use SRP TOO... */
1187 } else if (!(udc->devstat & UDC_ATT)) {
1188 if (udc->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001189 retval = otg_start_srp(udc->transceiver->otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191 spin_unlock_irqrestore(&udc->lock, flags);
1192
1193 return retval;
1194}
1195
1196static int
1197omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1198{
1199 struct omap_udc *udc;
1200 unsigned long flags;
1201 u16 syscon1;
1202
1203 udc = container_of(gadget, struct omap_udc, gadget);
1204 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001205 syscon1 = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (is_selfpowered)
1207 syscon1 |= UDC_SELF_PWR;
1208 else
1209 syscon1 &= ~UDC_SELF_PWR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001210 omap_writew(syscon1, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 spin_unlock_irqrestore(&udc->lock, flags);
1212
1213 return 0;
1214}
1215
1216static int can_pullup(struct omap_udc *udc)
1217{
1218 return udc->driver && udc->softconnect && udc->vbus_active;
1219}
1220
1221static void pullup_enable(struct omap_udc *udc)
1222{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001223 u16 w;
1224
1225 w = omap_readw(UDC_SYSCON1);
1226 w |= UDC_PULLUP_EN;
1227 omap_writew(w, UDC_SYSCON1);
1228 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1229 u32 l;
1230
1231 l = omap_readl(OTG_CTRL);
1232 l |= OTG_BSESSVLD;
1233 omap_writel(l, OTG_CTRL);
1234 }
1235 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
1238static void pullup_disable(struct omap_udc *udc)
1239{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001240 u16 w;
1241
1242 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1243 u32 l;
1244
1245 l = omap_readl(OTG_CTRL);
1246 l &= ~OTG_BSESSVLD;
1247 omap_writel(l, OTG_CTRL);
1248 }
1249 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1250 w = omap_readw(UDC_SYSCON1);
1251 w &= ~UDC_PULLUP_EN;
1252 omap_writew(w, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
David Brownelle6a6e472006-12-10 11:47:04 -08001255static struct omap_udc *udc;
1256
1257static void omap_udc_enable_clock(int enable)
1258{
1259 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1260 return;
1261
1262 if (enable) {
1263 clk_enable(udc->dc_clk);
1264 clk_enable(udc->hhc_clk);
1265 udelay(100);
1266 } else {
1267 clk_disable(udc->hhc_clk);
1268 clk_disable(udc->dc_clk);
1269 }
1270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272/*
1273 * Called by whatever detects VBUS sessions: external transceiver
1274 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1275 */
1276static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1277{
1278 struct omap_udc *udc;
1279 unsigned long flags;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001280 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 udc = container_of(gadget, struct omap_udc, gadget);
1283 spin_lock_irqsave(&udc->lock, flags);
1284 VDBG("VBUS %s\n", is_active ? "on" : "off");
1285 udc->vbus_active = (is_active != 0);
1286 if (cpu_is_omap15xx()) {
1287 /* "software" detect, ignored if !VBUS_MODE_1510 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001288 l = omap_readl(FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (is_active)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001290 l |= VBUS_CTRL_1510;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001292 l &= ~VBUS_CTRL_1510;
1293 omap_writel(l, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
David Brownelle6a6e472006-12-10 11:47:04 -08001295 if (udc->dc_clk != NULL && is_active) {
1296 if (!udc->clk_requested) {
1297 omap_udc_enable_clock(1);
1298 udc->clk_requested = 1;
1299 }
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (can_pullup(udc))
1302 pullup_enable(udc);
1303 else
1304 pullup_disable(udc);
David Brownelle6a6e472006-12-10 11:47:04 -08001305 if (udc->dc_clk != NULL && !is_active) {
1306 if (udc->clk_requested) {
1307 omap_udc_enable_clock(0);
1308 udc->clk_requested = 0;
1309 }
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 spin_unlock_irqrestore(&udc->lock, flags);
1312 return 0;
1313}
1314
1315static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1316{
1317 struct omap_udc *udc;
1318
1319 udc = container_of(gadget, struct omap_udc, gadget);
1320 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001321 return usb_phy_set_power(udc->transceiver, mA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -EOPNOTSUPP;
1323}
1324
1325static int omap_pullup(struct usb_gadget *gadget, int is_on)
1326{
1327 struct omap_udc *udc;
1328 unsigned long flags;
1329
1330 udc = container_of(gadget, struct omap_udc, gadget);
1331 spin_lock_irqsave(&udc->lock, flags);
1332 udc->softconnect = (is_on != 0);
1333 if (can_pullup(udc))
1334 pullup_enable(udc);
1335 else
1336 pullup_disable(udc);
1337 spin_unlock_irqrestore(&udc->lock, flags);
1338 return 0;
1339}
1340
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001341static int omap_udc_start(struct usb_gadget_driver *driver,
1342 int (*bind)(struct usb_gadget *));
1343static int omap_udc_stop(struct usb_gadget_driver *driver);
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345static struct usb_gadget_ops omap_gadget_ops = {
1346 .get_frame = omap_get_frame,
1347 .wakeup = omap_wakeup,
1348 .set_selfpowered = omap_set_selfpowered,
1349 .vbus_session = omap_vbus_session,
1350 .vbus_draw = omap_vbus_draw,
1351 .pullup = omap_pullup,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001352 .start = omap_udc_start,
1353 .stop = omap_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354};
1355
1356/*-------------------------------------------------------------------------*/
1357
1358/* dequeue ALL requests; caller holds udc->lock */
1359static void nuke(struct omap_ep *ep, int status)
1360{
1361 struct omap_req *req;
1362
1363 ep->stopped = 1;
1364
1365 if (use_dma && ep->dma_channel)
1366 dma_channel_release(ep);
1367
1368 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001369 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001371 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 while (!list_empty(&ep->queue)) {
1374 req = list_entry(ep->queue.next, struct omap_req, queue);
1375 done(ep, req, status);
1376 }
1377}
1378
1379/* caller holds udc->lock */
1380static void udc_quiesce(struct omap_udc *udc)
1381{
1382 struct omap_ep *ep;
1383
1384 udc->gadget.speed = USB_SPEED_UNKNOWN;
1385 nuke(&udc->ep[0], -ESHUTDOWN);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001386 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 nuke(ep, -ESHUTDOWN);
1388}
1389
1390/*-------------------------------------------------------------------------*/
1391
1392static void update_otg(struct omap_udc *udc)
1393{
1394 u16 devstat;
1395
David Brownell9cfbba72007-10-26 13:42:18 -07001396 if (!gadget_is_otg(&udc->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return;
1398
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001399 if (omap_readl(OTG_CTRL) & OTG_ID)
1400 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 else
1402 devstat = 0;
1403
1404 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1405 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1406 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1407
1408 /* Enable HNP early, avoiding races on suspend irq path.
1409 * ASSUMES OTG state machine B_BUS_REQ input is true.
1410 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001411 if (udc->gadget.b_hnp_enable) {
1412 u32 l;
1413
1414 l = omap_readl(OTG_CTRL);
1415 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1416 l &= ~OTG_PULLUP;
1417 omap_writel(l, OTG_CTRL);
1418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
1420
1421static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1422{
1423 struct omap_ep *ep0 = &udc->ep[0];
David Brownell313980c2005-04-11 15:38:25 -07001424 struct omap_req *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 ep0->irqs++;
1427
1428 /* Clear any pending requests and then scrub any rx/tx state
1429 * before starting to handle the SETUP request.
1430 */
1431 if (irq_src & UDC_SETUP) {
1432 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1433
1434 nuke(ep0, 0);
1435 if (ack) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001436 omap_writew(ack, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 irq_src = UDC_SETUP;
1438 }
1439 }
1440
David Brownelle6a6e472006-12-10 11:47:04 -08001441 /* IN/OUT packets mean we're in the DATA or STATUS stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 * This driver uses only uses protocol stalls (ep0 never halts),
1443 * and if we got this far the gadget driver already had a
1444 * chance to stall. Tries to be forgiving of host oddities.
1445 *
1446 * NOTE: the last chance gadget drivers have to stall control
1447 * requests is during their request completion callback.
1448 */
1449 if (!list_empty(&ep0->queue))
1450 req = container_of(ep0->queue.next, struct omap_req, queue);
1451
1452 /* IN == TX to host */
1453 if (irq_src & UDC_EP0_TX) {
1454 int stat;
1455
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001456 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1457 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1458 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 if (stat & UDC_ACK) {
1460 if (udc->ep0_in) {
1461 /* write next IN packet from response,
1462 * or set up the status stage.
1463 */
1464 if (req)
1465 stat = write_fifo(ep0, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001466 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!req && udc->ep0_pending) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001468 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1469 omap_writew(UDC_CLR_EP, UDC_CTRL);
1470 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1471 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 udc->ep0_pending = 0;
1473 } /* else: 6 wait states before it'll tx */
1474 } else {
1475 /* ack status stage of OUT transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001476 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (req)
1478 done(ep0, req, 0);
1479 }
David Brownell313980c2005-04-11 15:38:25 -07001480 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001482 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1483 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001485 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
1487 }
1488
1489 /* OUT == RX from host */
1490 if (irq_src & UDC_EP0_RX) {
1491 int stat;
1492
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001493 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1494 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1495 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (stat & UDC_ACK) {
1497 if (!udc->ep0_in) {
1498 stat = 0;
1499 /* read next OUT packet of request, maybe
1500 * reactiviting the fifo; stall on errors.
1501 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001502 stat = read_fifo(ep0, req);
1503 if (!req || stat < 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001504 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 udc->ep0_pending = 0;
1506 stat = 0;
1507 } else if (stat == 0)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001508 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1509 omap_writew(0, UDC_EP_NUM);
David Brownelle6a6e472006-12-10 11:47:04 -08001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* activate status stage */
1512 if (stat == 1) {
1513 done(ep0, req, 0);
1514 /* that may have STALLed ep0... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001515 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1516 UDC_EP_NUM);
1517 omap_writew(UDC_CLR_EP, UDC_CTRL);
1518 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1519 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 udc->ep0_pending = 0;
1521 }
1522 } else {
1523 /* ack status stage of IN transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001524 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (req)
1526 done(ep0, req, 0);
1527 }
1528 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001529 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1530 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001532 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 }
1534 }
1535
1536 /* SETUP starts all control transfers */
1537 if (irq_src & UDC_SETUP) {
1538 union u {
1539 u16 word[4];
1540 struct usb_ctrlrequest r;
1541 } u;
1542 int status = -EINVAL;
1543 struct omap_ep *ep;
1544
1545 /* read the (latest) SETUP message */
1546 do {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001547 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /* two bytes at a time */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001549 u.word[0] = omap_readw(UDC_DATA);
1550 u.word[1] = omap_readw(UDC_DATA);
1551 u.word[2] = omap_readw(UDC_DATA);
1552 u.word[3] = omap_readw(UDC_DATA);
1553 omap_writew(0, UDC_EP_NUM);
1554 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
David Brownell01ee7d72007-05-25 20:40:14 -07001556#define w_value le16_to_cpu(u.r.wValue)
1557#define w_index le16_to_cpu(u.r.wIndex)
1558#define w_length le16_to_cpu(u.r.wLength)
David Brownell65111082005-04-28 13:52:31 -07001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* Delegate almost all control requests to the gadget driver,
1561 * except for a handful of ch9 status/feature requests that
1562 * hardware doesn't autodecode _and_ the gadget API hides.
1563 */
1564 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1565 udc->ep0_set_config = 0;
1566 udc->ep0_pending = 1;
1567 ep0->stopped = 0;
1568 ep0->ackwait = 0;
1569 switch (u.r.bRequest) {
1570 case USB_REQ_SET_CONFIGURATION:
1571 /* udc needs to know when ep != 0 is valid */
1572 if (u.r.bRequestType != USB_RECIP_DEVICE)
1573 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001574 if (w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto do_stall;
1576 udc->ep0_set_config = 1;
David Brownell65111082005-04-28 13:52:31 -07001577 udc->ep0_reset_config = (w_value == 0);
1578 VDBG("set config %d\n", w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* update udc NOW since gadget driver may start
1581 * queueing requests immediately; clear config
1582 * later if it fails the request.
1583 */
1584 if (udc->ep0_reset_config)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001585 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001587 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 update_otg(udc);
1589 goto delegate;
1590 case USB_REQ_CLEAR_FEATURE:
1591 /* clear endpoint halt */
1592 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1593 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001594 if (w_value != USB_ENDPOINT_HALT
1595 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001597 ep = &udc->ep[w_index & 0xf];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (ep != ep0) {
David Brownell65111082005-04-28 13:52:31 -07001599 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 ep += 16;
1601 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001602 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto do_stall;
1604 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001605 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 ep->ackwait = 0;
1607 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001608 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ep->ackwait = 1 + ep->double_buf;
1610 }
David Brownell313980c2005-04-11 15:38:25 -07001611 /* NOTE: assumes the host behaves sanely,
1612 * only clearing real halts. Else we may
1613 * need to kill pending transfers and then
1614 * restart the queue... very messy for DMA!
1615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 VDBG("%s halt cleared by host\n", ep->name);
1618 goto ep0out_status_stage;
1619 case USB_REQ_SET_FEATURE:
1620 /* set endpoint halt */
1621 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1622 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001623 if (w_value != USB_ENDPOINT_HALT
1624 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001626 ep = &udc->ep[w_index & 0xf];
1627 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 ep += 16;
1629 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001630 || ep == ep0 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto do_stall;
1632 if (use_dma && ep->has_dma) {
1633 /* this has rude side-effects (aborts) and
1634 * can't really work if DMA-IN is active
1635 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001636 DBG("%s host set_halt, NYET\n", ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto do_stall;
1638 }
1639 use_ep(ep, 0);
1640 /* can't halt if fifo isn't empty... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001641 omap_writew(UDC_CLR_EP, UDC_CTRL);
1642 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 VDBG("%s halted by host\n", ep->name);
1644ep0out_status_stage:
1645 status = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001646 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1647 omap_writew(UDC_CLR_EP, UDC_CTRL);
1648 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1649 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 udc->ep0_pending = 0;
1651 break;
1652 case USB_REQ_GET_STATUS:
David Brownell8a3c1f52007-03-21 12:26:32 -07001653 /* USB_ENDPOINT_HALT status? */
1654 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1655 goto intf_status;
1656
1657 /* ep0 never stalls */
1658 if (!(w_index & 0xf))
1659 goto zero_status;
1660
1661 /* only active endpoints count */
1662 ep = &udc->ep[w_index & 0xf];
1663 if (w_index & USB_DIR_IN)
1664 ep += 16;
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001665 if (!ep->ep.desc)
David Brownell8a3c1f52007-03-21 12:26:32 -07001666 goto do_stall;
1667
1668 /* iso never stalls */
1669 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1670 goto zero_status;
1671
1672 /* FIXME don't assume non-halted endpoints!! */
1673 ERR("%s status, can't report\n", ep->ep.name);
1674 goto do_stall;
1675
1676intf_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /* return interface status. if we were pedantic,
1678 * we'd detect non-existent interfaces, and stall.
1679 */
1680 if (u.r.bRequestType
1681 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1682 goto delegate;
David Brownell8a3c1f52007-03-21 12:26:32 -07001683
1684zero_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /* return two zero bytes */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001686 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1687 omap_writew(0, UDC_DATA);
1688 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1689 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 status = 0;
David Brownell65111082005-04-28 13:52:31 -07001691 VDBG("GET_STATUS, interface %d\n", w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /* next, status stage */
1693 break;
1694 default:
1695delegate:
1696 /* activate the ep0out fifo right away */
David Brownell65111082005-04-28 13:52:31 -07001697 if (!udc->ep0_in && w_length) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001698 omap_writew(0, UDC_EP_NUM);
1699 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701
1702 /* gadget drivers see class/vendor specific requests,
1703 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1704 * and more
1705 */
1706 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1707 u.r.bRequestType, u.r.bRequest,
David Brownell65111082005-04-28 13:52:31 -07001708 w_value, w_index, w_length);
1709
1710#undef w_value
1711#undef w_index
1712#undef w_length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 /* The gadget driver may return an error here,
1715 * causing an immediate protocol stall.
1716 *
1717 * Else it must issue a response, either queueing a
1718 * response buffer for the DATA stage, or halting ep0
1719 * (causing a protocol stall, not a real halt). A
1720 * zero length buffer means no DATA stage.
1721 *
1722 * It's fine to issue that response after the setup()
1723 * call returns, and this IRQ was handled.
1724 */
1725 udc->ep0_setup = 1;
1726 spin_unlock(&udc->lock);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001727 status = udc->driver->setup(&udc->gadget, &u.r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 spin_lock(&udc->lock);
1729 udc->ep0_setup = 0;
1730 }
1731
1732 if (status < 0) {
1733do_stall:
1734 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1735 u.r.bRequestType, u.r.bRequest, status);
1736 if (udc->ep0_set_config) {
1737 if (udc->ep0_reset_config)
Arjan van de Venb6c63932008-07-25 01:45:52 -07001738 WARNING("error resetting config?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001740 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001742 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 udc->ep0_pending = 0;
1744 }
1745 }
1746}
1747
1748/*-------------------------------------------------------------------------*/
1749
1750#define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1751
1752static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1753{
1754 u16 devstat, change;
1755
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001756 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 change = devstat ^ udc->devstat;
1758 udc->devstat = devstat;
1759
1760 if (change & (UDC_USB_RESET|UDC_ATT)) {
1761 udc_quiesce(udc);
1762
1763 if (change & UDC_ATT) {
1764 /* driver for any external transceiver will
1765 * have called omap_vbus_session() already
1766 */
1767 if (devstat & UDC_ATT) {
1768 udc->gadget.speed = USB_SPEED_FULL;
1769 VDBG("connect\n");
1770 if (!udc->transceiver)
1771 pullup_enable(udc);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001772 /* if (driver->connect) call it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1774 udc->gadget.speed = USB_SPEED_UNKNOWN;
1775 if (!udc->transceiver)
1776 pullup_disable(udc);
1777 DBG("disconnect, gadget %s\n",
1778 udc->driver->driver.name);
1779 if (udc->driver->disconnect) {
1780 spin_unlock(&udc->lock);
1781 udc->driver->disconnect(&udc->gadget);
1782 spin_lock(&udc->lock);
1783 }
1784 }
1785 change &= ~UDC_ATT;
1786 }
1787
1788 if (change & UDC_USB_RESET) {
1789 if (devstat & UDC_USB_RESET) {
1790 VDBG("RESET=1\n");
1791 } else {
1792 udc->gadget.speed = USB_SPEED_FULL;
1793 INFO("USB reset done, gadget %s\n",
1794 udc->driver->driver.name);
1795 /* ep0 traffic is legal from now on */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001796 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1797 UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 change &= ~UDC_USB_RESET;
1800 }
1801 }
1802 if (change & UDC_SUS) {
1803 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03001804 /* FIXME tell isp1301 to suspend/resume (?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (devstat & UDC_SUS) {
1806 VDBG("suspend\n");
1807 update_otg(udc);
1808 /* HNP could be under way already */
1809 if (udc->gadget.speed == USB_SPEED_FULL
1810 && udc->driver->suspend) {
1811 spin_unlock(&udc->lock);
1812 udc->driver->suspend(&udc->gadget);
1813 spin_lock(&udc->lock);
1814 }
Juha Yrj?l?4e671852005-10-16 15:47:04 -07001815 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001816 usb_phy_set_suspend(
1817 udc->transceiver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 } else {
1819 VDBG("resume\n");
Juha Yrj?l?4e671852005-10-16 15:47:04 -07001820 if (udc->transceiver)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001821 usb_phy_set_suspend(
1822 udc->transceiver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (udc->gadget.speed == USB_SPEED_FULL
1824 && udc->driver->resume) {
1825 spin_unlock(&udc->lock);
1826 udc->driver->resume(&udc->gadget);
1827 spin_lock(&udc->lock);
1828 }
1829 }
1830 }
1831 change &= ~UDC_SUS;
1832 }
1833 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1834 update_otg(udc);
1835 change &= ~OTG_FLAGS;
1836 }
1837
1838 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1839 if (change)
1840 VDBG("devstat %03x, ignore change %03x\n",
1841 devstat, change);
1842
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001843 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
David Howells7d12e782006-10-05 14:55:46 +01001846static irqreturn_t omap_udc_irq(int irq, void *_udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 struct omap_udc *udc = _udc;
1849 u16 irq_src;
1850 irqreturn_t status = IRQ_NONE;
1851 unsigned long flags;
1852
1853 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001854 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /* Device state change (usb ch9 stuff) */
1857 if (irq_src & UDC_DS_CHG) {
1858 devstate_irq(_udc, irq_src);
1859 status = IRQ_HANDLED;
1860 irq_src &= ~UDC_DS_CHG;
1861 }
1862
1863 /* EP0 control transfers */
1864 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1865 ep0_irq(_udc, irq_src);
1866 status = IRQ_HANDLED;
1867 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1868 }
1869
1870 /* DMA transfer completion */
1871 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1872 dma_irq(_udc, irq_src);
1873 status = IRQ_HANDLED;
1874 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1875 }
1876
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001877 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (irq_src)
1879 DBG("udc_irq, unhandled %03x\n", irq_src);
1880 spin_unlock_irqrestore(&udc->lock, flags);
1881
1882 return status;
1883}
1884
1885/* workaround for seemingly-lost IRQs for RX ACKs... */
1886#define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1887#define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1888
1889static void pio_out_timer(unsigned long _ep)
1890{
1891 struct omap_ep *ep = (void *) _ep;
1892 unsigned long flags;
1893 u16 stat_flg;
1894
1895 spin_lock_irqsave(&ep->udc->lock, flags);
1896 if (!list_empty(&ep->queue) && ep->ackwait) {
David Brownelle6a6e472006-12-10 11:47:04 -08001897 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001898 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1901 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1902 struct omap_req *req;
1903
1904 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1905 req = container_of(ep->queue.next,
1906 struct omap_req, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 (void) read_fifo(ep, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001908 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1909 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ep->ackwait = 1 + ep->double_buf;
David Brownelle6a6e472006-12-10 11:47:04 -08001911 } else
1912 deselect_ep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1915 spin_unlock_irqrestore(&ep->udc->lock, flags);
1916}
1917
David Howells7d12e782006-10-05 14:55:46 +01001918static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 u16 epn_stat, irq_src;
1921 irqreturn_t status = IRQ_NONE;
1922 struct omap_ep *ep;
1923 int epnum;
1924 struct omap_udc *udc = _dev;
1925 struct omap_req *req;
1926 unsigned long flags;
1927
1928 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001929 epn_stat = omap_readw(UDC_EPN_STAT);
1930 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* handle OUT first, to avoid some wasteful NAKs */
1933 if (irq_src & UDC_EPN_RX) {
1934 epnum = (epn_stat >> 8) & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001935 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 status = IRQ_HANDLED;
1937 ep = &udc->ep[epnum];
1938 ep->irqs++;
1939
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001940 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 ep->fnf = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001942 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 ep->ackwait--;
1944 if (!list_empty(&ep->queue)) {
1945 int stat;
1946 req = container_of(ep->queue.next,
1947 struct omap_req, queue);
1948 stat = read_fifo(ep, req);
1949 if (!ep->double_buf)
1950 ep->fnf = 1;
1951 }
1952 }
1953 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001954 epn_stat = omap_readw(UDC_EPN_STAT);
1955 epn_stat = omap_readw(UDC_EPN_STAT);
1956 omap_writew(epnum, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* enabling fifo _after_ clearing ACK, contrary to docs,
1959 * reduces lossage; timer still needed though (sigh).
1960 */
1961 if (ep->fnf) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001962 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ep->ackwait = 1 + ep->double_buf;
1964 }
1965 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1966 }
1967
1968 /* then IN transfers */
1969 else if (irq_src & UDC_EPN_TX) {
1970 epnum = epn_stat & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001971 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 status = IRQ_HANDLED;
1973 ep = &udc->ep[16 + epnum];
1974 ep->irqs++;
1975
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001976 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1977 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 ep->ackwait = 0;
1979 if (!list_empty(&ep->queue)) {
1980 req = container_of(ep->queue.next,
1981 struct omap_req, queue);
1982 (void) write_fifo(ep, req);
1983 }
1984 }
1985 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001986 epn_stat = omap_readw(UDC_EPN_STAT);
1987 epn_stat = omap_readw(UDC_EPN_STAT);
1988 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 /* then 6 clocks before it'd tx */
1990 }
1991
1992 spin_unlock_irqrestore(&udc->lock, flags);
1993 return status;
1994}
1995
1996#ifdef USE_ISO
David Howells7d12e782006-10-05 14:55:46 +01001997static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
1999 struct omap_udc *udc = _dev;
2000 struct omap_ep *ep;
2001 int pending = 0;
2002 unsigned long flags;
2003
2004 spin_lock_irqsave(&udc->lock, flags);
2005
2006 /* handle all non-DMA ISO transfers */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002007 list_for_each_entry(ep, &udc->iso, iso) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 u16 stat;
2009 struct omap_req *req;
2010
2011 if (ep->has_dma || list_empty(&ep->queue))
2012 continue;
2013 req = list_entry(ep->queue.next, struct omap_req, queue);
2014
2015 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002016 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 /* NOTE: like the other controller drivers, this isn't
2019 * currently reporting lost or damaged frames.
2020 */
2021 if (ep->bEndpointAddress & USB_DIR_IN) {
2022 if (stat & UDC_MISS_IN)
2023 /* done(ep, req, -EPROTO) */;
2024 else
2025 write_fifo(ep, req);
2026 } else {
2027 int status = 0;
2028
2029 if (stat & UDC_NO_RXPACKET)
2030 status = -EREMOTEIO;
2031 else if (stat & UDC_ISO_ERR)
2032 status = -EILSEQ;
2033 else if (stat & UDC_DATA_FLUSH)
2034 status = -ENOSR;
2035
2036 if (status)
2037 /* done(ep, req, status) */;
2038 else
2039 read_fifo(ep, req);
2040 }
2041 deselect_ep();
2042 /* 6 wait states before next EP */
2043
2044 ep->irqs++;
2045 if (!list_empty(&ep->queue))
2046 pending = 1;
2047 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002048 if (!pending) {
2049 u16 w;
2050
2051 w = omap_readw(UDC_IRQ_EN);
2052 w &= ~UDC_SOF_IE;
2053 omap_writew(w, UDC_IRQ_EN);
2054 }
2055 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 spin_unlock_irqrestore(&udc->lock, flags);
2058 return IRQ_HANDLED;
2059}
2060#endif
2061
2062/*-------------------------------------------------------------------------*/
2063
David Brownell8a3c1f52007-03-21 12:26:32 -07002064static inline int machine_without_vbus_sense(void)
David Brownelle6a6e472006-12-10 11:47:04 -08002065{
Felipe Balbi80dd1352012-05-29 14:26:09 +03002066 return machine_is_omap_innovator()
David Brownelle6a6e472006-12-10 11:47:04 -08002067 || machine_is_omap_osk()
David Brownelle6a6e472006-12-10 11:47:04 -08002068 || machine_is_sx1()
Felipe Balbi80dd1352012-05-29 14:26:09 +03002069 /* No known omap7xx boards with vbus sense */
2070 || cpu_is_omap7xx();
David Brownelle6a6e472006-12-10 11:47:04 -08002071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002073static int omap_udc_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002074 int (*bind)(struct usb_gadget *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 int status = -ENODEV;
2077 struct omap_ep *ep;
2078 unsigned long flags;
2079
2080 /* basic sanity tests */
2081 if (!udc)
2082 return -ENODEV;
2083 if (!driver
Felipe Balbi80dd1352012-05-29 14:26:09 +03002084 /* FIXME if otg, check: driver->is_otg */
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002085 || driver->max_speed < USB_SPEED_FULL
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002086 || !bind || !driver->setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return -EINVAL;
2088
2089 spin_lock_irqsave(&udc->lock, flags);
2090 if (udc->driver) {
2091 spin_unlock_irqrestore(&udc->lock, flags);
2092 return -EBUSY;
2093 }
2094
2095 /* reset state */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002096 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 ep->irqs = 0;
2098 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2099 continue;
2100 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002101 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 udc->ep0_pending = 0;
2104 udc->ep[0].irqs = 0;
2105 udc->softconnect = 1;
2106
2107 /* hook up the driver */
David Brownell313980c2005-04-11 15:38:25 -07002108 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 udc->driver = driver;
2110 udc->gadget.dev.driver = &driver->driver;
2111 spin_unlock_irqrestore(&udc->lock, flags);
2112
David Brownelle6a6e472006-12-10 11:47:04 -08002113 if (udc->dc_clk != NULL)
2114 omap_udc_enable_clock(1);
2115
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002116 status = bind(&udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (status) {
2118 DBG("bind to %s --> %d\n", driver->driver.name, status);
David Brownell313980c2005-04-11 15:38:25 -07002119 udc->gadget.dev.driver = NULL;
2120 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 goto done;
2122 }
2123 DBG("bound to driver %s\n", driver->driver.name);
2124
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002125 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* connect to bus through transceiver */
2128 if (udc->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002129 status = otg_set_peripheral(udc->transceiver->otg,
2130 &udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (status < 0) {
2132 ERR("can't bind to transceiver\n");
David Brownell6bea4762006-12-05 03:15:33 -08002133 if (driver->unbind) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002134 driver->unbind(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002135 udc->gadget.dev.driver = NULL;
2136 udc->driver = NULL;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 goto done;
2139 }
2140 } else {
2141 if (can_pullup(udc))
Felipe Balbi80dd1352012-05-29 14:26:09 +03002142 pullup_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002144 pullup_disable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146
2147 /* boards that don't have VBUS sensing can't autogate 48MHz;
2148 * can't enter deep sleep while a gadget driver is active.
2149 */
David Brownell8a3c1f52007-03-21 12:26:32 -07002150 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 omap_vbus_session(&udc->gadget, 1);
2152
2153done:
David Brownelle6a6e472006-12-10 11:47:04 -08002154 if (udc->dc_clk != NULL)
2155 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return status;
2157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002159static int omap_udc_stop(struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 unsigned long flags;
2162 int status = -ENODEV;
2163
2164 if (!udc)
2165 return -ENODEV;
David Brownell6bea4762006-12-05 03:15:33 -08002166 if (!driver || driver != udc->driver || !driver->unbind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return -EINVAL;
2168
David Brownelle6a6e472006-12-10 11:47:04 -08002169 if (udc->dc_clk != NULL)
2170 omap_udc_enable_clock(1);
2171
David Brownell8a3c1f52007-03-21 12:26:32 -07002172 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 omap_vbus_session(&udc->gadget, 0);
2174
2175 if (udc->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002176 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 else
2178 pullup_disable(udc);
2179
2180 spin_lock_irqsave(&udc->lock, flags);
2181 udc_quiesce(udc);
2182 spin_unlock_irqrestore(&udc->lock, flags);
2183
2184 driver->unbind(&udc->gadget);
David Brownell313980c2005-04-11 15:38:25 -07002185 udc->gadget.dev.driver = NULL;
2186 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
David Brownelle6a6e472006-12-10 11:47:04 -08002188 if (udc->dc_clk != NULL)
2189 omap_udc_enable_clock(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 DBG("unregistered driver '%s'\n", driver->driver.name);
2191 return status;
2192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194/*-------------------------------------------------------------------------*/
2195
2196#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2197
2198#include <linux/seq_file.h>
2199
2200static const char proc_filename[] = "driver/udc";
2201
2202#define FOURBITS "%s%s%s%s"
Felipe Balbi80dd1352012-05-29 14:26:09 +03002203#define EIGHTBITS "%s%s%s%s%s%s%s%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2206{
2207 u16 stat_flg;
2208 struct omap_req *req;
2209 char buf[20];
2210
2211 use_ep(ep, 0);
2212
2213 if (use_dma && ep->has_dma)
2214 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2215 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2216 ep->dma_channel - 1, ep->lch);
2217 else
2218 buf[0] = 0;
2219
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002220 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 seq_printf(s,
2222 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2223 ep->name, buf,
2224 ep->double_buf ? "dbuf " : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002225 ({ char *s;
2226 switch (ep->ackwait) {
2227 case 0:
2228 s = "";
2229 break;
2230 case 1:
2231 s = "(ackw) ";
2232 break;
2233 case 2:
2234 s = "(ackw2) ";
2235 break;
2236 default:
2237 s = "(?) ";
2238 break;
2239 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 ep->irqs, stat_flg,
2241 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2242 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2243 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2244 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2245 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2246 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2247 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2248 (stat_flg & UDC_STALL) ? "STALL " : "",
2249 (stat_flg & UDC_NAK) ? "NAK " : "",
2250 (stat_flg & UDC_ACK) ? "ACK " : "",
2251 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2252 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2253 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2254
Felipe Balbi80dd1352012-05-29 14:26:09 +03002255 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 seq_printf(s, "\t(queue empty)\n");
2257 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002258 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 unsigned length = req->req.actual;
2260
2261 if (use_dma && buf[0]) {
2262 length += ((ep->bEndpointAddress & USB_DIR_IN)
2263 ? dma_src_len : dma_dest_len)
2264 (ep, req->req.dma + length);
2265 buf[0] = 0;
2266 }
2267 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2268 &req->req, length,
2269 req->req.length, req->req.buf);
2270 }
2271}
2272
2273static char *trx_mode(unsigned m, int enabled)
2274{
2275 switch (m) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002276 case 0:
2277 return enabled ? "*6wire" : "unused";
2278 case 1:
2279 return "4wire";
2280 case 2:
2281 return "3wire";
2282 case 3:
2283 return "6wire";
2284 default:
2285 return "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287}
2288
2289static int proc_otg_show(struct seq_file *s)
2290{
2291 u32 tmp;
Paul Walmsley4814ced2010-10-08 11:40:20 -06002292 u32 trans = 0;
2293 char *ctrl_name = "(UNKNOWN)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Dmitry Baryshkove12cc342008-08-07 16:29:25 +04002295 tmp = omap_readl(OTG_REV);
Tony Lindgrenae372572012-05-21 12:01:11 -07002296 ctrl_name = "tranceiver_ctrl";
2297 trans = omap_readw(USB_TRANSCEIVER_CTRL);
David Brownelle6a6e472006-12-10 11:47:04 -08002298 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2299 tmp >> 4, tmp & 0xf, ctrl_name, trans);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002300 tmp = omap_readw(OTG_SYSCON_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2302 FOURBITS "\n", tmp,
2303 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2304 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
David Brownell65111082005-04-28 13:52:31 -07002305 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 ? "internal"
2307 : trx_mode(USB0_TRX_MODE(tmp), 1),
2308 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2309 (tmp & HST_IDLE_EN) ? " !host" : "",
2310 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2311 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002312 tmp = omap_readl(OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2314 " b_ase_brst=%d hmc=%d\n", tmp,
2315 (tmp & OTG_EN) ? " otg_en" : "",
2316 (tmp & USBX_SYNCHRO) ? " synchro" : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002317 /* much more SRP stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 (tmp & SRP_DATA) ? " srp_data" : "",
2319 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2320 (tmp & OTG_PADEN) ? " otg_paden" : "",
2321 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2322 (tmp & UHOST_EN) ? " uhost_en" : "",
2323 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2324 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2325 B_ASE_BRST(tmp),
2326 OTG_HMC(tmp));
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002327 tmp = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2329 (tmp & OTG_ASESSVLD) ? " asess" : "",
2330 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2331 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2332 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2333 (tmp & OTG_ID) ? " id" : "",
2334 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2335 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2336 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2337 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2338 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2339 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2340 (tmp & OTG_PULLDOWN) ? " down" : "",
2341 (tmp & OTG_PULLUP) ? " up" : "",
2342 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2343 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2344 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2345 (tmp & OTG_PU_ID) ? " pu_id" : ""
2346 );
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002347 tmp = omap_readw(OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002349 tmp = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002351 tmp = omap_readw(OTG_OUTCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002353 tmp = omap_readw(OTG_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 seq_printf(s, "otg_test %04x" "\n", tmp);
David Brownell313980c2005-04-11 15:38:25 -07002355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358static int proc_udc_show(struct seq_file *s, void *_)
2359{
2360 u32 tmp;
2361 struct omap_ep *ep;
2362 unsigned long flags;
2363
2364 spin_lock_irqsave(&udc->lock, flags);
2365
2366 seq_printf(s, "%s, version: " DRIVER_VERSION
2367#ifdef USE_ISO
2368 " (iso)"
2369#endif
2370 "%s\n",
2371 driver_desc,
2372 use_dma ? " (dma)" : "");
2373
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002374 tmp = omap_readw(UDC_REV) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 seq_printf(s,
2376 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2377 "hmc %d, transceiver %s\n",
2378 tmp >> 4, tmp & 0xf,
2379 fifo_mode,
2380 udc->driver ? udc->driver->driver.name : "(none)",
2381 HMC,
David Brownelle6a6e472006-12-10 11:47:04 -08002382 udc->transceiver
2383 ? udc->transceiver->label
Tony Lindgrenae372572012-05-21 12:01:11 -07002384 : (cpu_is_omap1710()
David Brownelle6a6e472006-12-10 11:47:04 -08002385 ? "external" : "(none)"));
Tony Lindgrenae372572012-05-21 12:01:11 -07002386 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2387 omap_readw(ULPD_CLOCK_CTRL),
2388 omap_readw(ULPD_SOFT_REQ),
2389 omap_readw(ULPD_STATUS_REQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 /* OTG controller registers */
2392 if (!cpu_is_omap15xx())
2393 proc_otg_show(s);
2394
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002395 tmp = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2397 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2398 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2399 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2400 (tmp & UDC_NAK_EN) ? " nak" : "",
2401 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2402 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2403 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2404 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
Felipe Balbi80dd1352012-05-29 14:26:09 +03002405 /* syscon2 is write-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /* UDC controller registers */
2408 if (!(tmp & UDC_PULLUP_EN)) {
2409 seq_printf(s, "(suspended)\n");
2410 spin_unlock_irqrestore(&udc->lock, flags);
2411 return 0;
2412 }
2413
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002414 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2416 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2417 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2418 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2419 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2420 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2421 (tmp & UDC_SUS) ? " SUS" : "",
2422 (tmp & UDC_CFG) ? " CFG" : "",
2423 (tmp & UDC_ADD) ? " ADD" : "",
2424 (tmp & UDC_DEF) ? " DEF" : "",
2425 (tmp & UDC_ATT) ? " ATT" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002426 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2427 tmp = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2429 (tmp & UDC_SOF_IE) ? " sof" : "",
2430 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2431 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2432 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2433 (tmp & UDC_EP0_IE) ? " ep0" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002434 tmp = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2436 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2437 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2438 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002439 (tmp & UDC_IRQ_SOF) ? " sof" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2441 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2442 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2443 (tmp & UDC_SETUP) ? " setup" : "",
2444 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2445 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2446 if (use_dma) {
2447 unsigned i;
2448
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002449 tmp = omap_readw(UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2451 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2452 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2453 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2454
2455 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2456 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2457 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2458
2459 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2460 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2461 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2462
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002463 tmp = omap_readw(UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2465 if (tmp) {
2466 for (i = 0; i < 3; i++) {
2467 if ((tmp & (0x0f << (i * 4))) == 0)
2468 continue;
2469 seq_printf(s, "rxdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002470 omap_readw(UDC_RXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002473 tmp = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 seq_printf(s, "txdma_cfg %04x\n", tmp);
2475 if (tmp) {
2476 for (i = 0; i < 3; i++) {
2477 if (!(tmp & (0x0f << (i * 4))))
2478 continue;
2479 seq_printf(s, "txdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002480 omap_readw(UDC_TXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
2482 }
2483 }
2484
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002485 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (tmp & UDC_ATT) {
2487 proc_ep_show(s, &udc->ep[0]);
2488 if (tmp & UDC_ADD) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002489 list_for_each_entry(ep, &udc->gadget.ep_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 ep.ep_list) {
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02002491 if (ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 proc_ep_show(s, ep);
2493 }
2494 }
2495 }
2496 spin_unlock_irqrestore(&udc->lock, flags);
2497 return 0;
2498}
2499
2500static int proc_udc_open(struct inode *inode, struct file *file)
2501{
David Brownell313980c2005-04-11 15:38:25 -07002502 return single_open(file, proc_udc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03002505static const struct file_operations proc_ops = {
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002506 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .open = proc_udc_open,
2508 .read = seq_read,
2509 .llseek = seq_lseek,
2510 .release = single_release,
2511};
2512
2513static void create_proc_file(void)
2514{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002515 proc_create(proc_filename, 0, NULL, &proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
2518static void remove_proc_file(void)
2519{
David Brownell313980c2005-04-11 15:38:25 -07002520 remove_proc_entry(proc_filename, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523#else
2524
2525static inline void create_proc_file(void) {}
2526static inline void remove_proc_file(void) {}
2527
2528#endif
2529
2530/*-------------------------------------------------------------------------*/
2531
2532/* Before this controller can enumerate, we need to pick an endpoint
2533 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2534 * buffer space among the endpoints we'll be operating.
David Brownell65111082005-04-28 13:52:31 -07002535 *
2536 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002537 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
David Brownell65111082005-04-28 13:52:31 -07002538 * capability yet though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 */
Felipe Balbidc1737c2012-05-29 14:33:30 +03002540static unsigned __devinit
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541omap_ep_setup(char *name, u8 addr, u8 type,
2542 unsigned buf, unsigned maxp, int dbuf)
2543{
2544 struct omap_ep *ep;
2545 u16 epn_rxtx = 0;
2546
2547 /* OUT endpoints first, then IN */
2548 ep = &udc->ep[addr & 0xf];
2549 if (addr & USB_DIR_IN)
2550 ep += 16;
2551
2552 /* in case of ep init table bugs */
2553 BUG_ON(ep->name[0]);
2554
2555 /* chip setup ... bit values are same for IN, OUT */
2556 if (type == USB_ENDPOINT_XFER_ISOC) {
2557 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002558 case 8:
2559 epn_rxtx = 0 << 12;
2560 break;
2561 case 16:
2562 epn_rxtx = 1 << 12;
2563 break;
2564 case 32:
2565 epn_rxtx = 2 << 12;
2566 break;
2567 case 64:
2568 epn_rxtx = 3 << 12;
2569 break;
2570 case 128:
2571 epn_rxtx = 4 << 12;
2572 break;
2573 case 256:
2574 epn_rxtx = 5 << 12;
2575 break;
2576 case 512:
2577 epn_rxtx = 6 << 12;
2578 break;
2579 default:
2580 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582 epn_rxtx |= UDC_EPN_RX_ISO;
2583 dbuf = 1;
2584 } else {
2585 /* double-buffering "not supported" on 15xx,
David Brownelle6a6e472006-12-10 11:47:04 -08002586 * and ignored for PIO-IN on newer chips
2587 * (for more reliable behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 */
Tony Lindgrenae372572012-05-21 12:01:11 -07002589 if (!use_dma || cpu_is_omap15xx())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 dbuf = 0;
2591
2592 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002593 case 8:
2594 epn_rxtx = 0 << 12;
2595 break;
2596 case 16:
2597 epn_rxtx = 1 << 12;
2598 break;
2599 case 32:
2600 epn_rxtx = 2 << 12;
2601 break;
2602 case 64:
2603 epn_rxtx = 3 << 12;
2604 break;
2605 default:
2606 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608 if (dbuf && addr)
2609 epn_rxtx |= UDC_EPN_RX_DB;
2610 init_timer(&ep->timer);
2611 ep->timer.function = pio_out_timer;
2612 ep->timer.data = (unsigned long) ep;
2613 }
2614 if (addr)
2615 epn_rxtx |= UDC_EPN_RX_VALID;
2616 BUG_ON(buf & 0x07);
2617 epn_rxtx |= buf >> 3;
2618
2619 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2620 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2621
2622 if (addr & USB_DIR_IN)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002623 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002625 omap_writew(epn_rxtx, UDC_EP_RX(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 /* next endpoint's buffer starts after this one's */
2628 buf += maxp;
2629 if (dbuf)
2630 buf += maxp;
2631 BUG_ON(buf > 2048);
2632
2633 /* set up driver data structures */
2634 BUG_ON(strlen(name) >= sizeof ep->name);
2635 strlcpy(ep->name, name, sizeof ep->name);
2636 INIT_LIST_HEAD(&ep->queue);
2637 INIT_LIST_HEAD(&ep->iso);
2638 ep->bEndpointAddress = addr;
2639 ep->bmAttributes = type;
2640 ep->double_buf = dbuf;
David Brownelle6a6e472006-12-10 11:47:04 -08002641 ep->udc = udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 ep->ep.name = ep->name;
2644 ep->ep.ops = &omap_ep_ops;
2645 ep->ep.maxpacket = ep->maxpacket = maxp;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002646 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 return buf;
2649}
2650
2651static void omap_udc_release(struct device *dev)
2652{
2653 complete(udc->done);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002654 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002655 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
Felipe Balbidc1737c2012-05-29 14:33:30 +03002658static int __devinit
Heikki Krogerus86753812012-02-13 13:24:02 +02002659omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
2661 unsigned tmp, buf;
2662
2663 /* abolish any previous hardware state */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002664 omap_writew(0, UDC_SYSCON1);
2665 omap_writew(0, UDC_IRQ_EN);
2666 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2667 omap_writew(0, UDC_DMA_IRQ_EN);
2668 omap_writew(0, UDC_RXDMA_CFG);
2669 omap_writew(0, UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
2671 /* UDC_PULLUP_EN gates the chip clock */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002672 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Christoph Lametere94b1762006-12-06 20:33:17 -08002674 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (!udc)
2676 return -ENOMEM;
2677
Felipe Balbi80dd1352012-05-29 14:26:09 +03002678 spin_lock_init(&udc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680 udc->gadget.ops = &omap_gadget_ops;
2681 udc->gadget.ep0 = &udc->ep[0].ep;
2682 INIT_LIST_HEAD(&udc->gadget.ep_list);
2683 INIT_LIST_HEAD(&udc->iso);
2684 udc->gadget.speed = USB_SPEED_UNKNOWN;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002685 udc->gadget.max_speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 udc->gadget.name = driver_name;
2687
2688 device_initialize(&udc->gadget.dev);
Kay Sievers0031a062008-05-02 06:02:41 +02002689 dev_set_name(&udc->gadget.dev, "gadget");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 udc->gadget.dev.release = omap_udc_release;
2691 udc->gadget.dev.parent = &odev->dev;
2692 if (use_dma)
2693 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2694
2695 udc->transceiver = xceiv;
2696
2697 /* ep0 is special; put it right after the SETUP buffer */
2698 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2699 8 /* after SETUP */, 64 /* maxpacket */, 0);
2700 list_del_init(&udc->ep[0].ep.ep_list);
2701
2702 /* initially disable all non-ep0 endpoints */
2703 for (tmp = 1; tmp < 15; tmp++) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002704 omap_writew(0, UDC_EP_RX(tmp));
2705 omap_writew(0, UDC_EP_TX(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707
Felipe Balbi80dd1352012-05-29 14:26:09 +03002708#define OMAP_BULK_EP(name, addr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 buf = omap_ep_setup(name "-bulk", addr, \
2710 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002711#define OMAP_INT_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 buf = omap_ep_setup(name "-int", addr, \
2713 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002714#define OMAP_ISO_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 buf = omap_ep_setup(name "-iso", addr, \
2716 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2717
2718 switch (fifo_mode) {
2719 case 0:
2720 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2721 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2722 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2723 break;
2724 case 1:
2725 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2726 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
David Brownell313980c2005-04-11 15:38:25 -07002727 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2730 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
David Brownell313980c2005-04-11 15:38:25 -07002731 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2734 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
David Brownell313980c2005-04-11 15:38:25 -07002735 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2738 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
David Brownell313980c2005-04-11 15:38:25 -07002739 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2742 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
David Brownell313980c2005-04-11 15:38:25 -07002743 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2744 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2747 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
David Brownell313980c2005-04-11 15:38:25 -07002748 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2749 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
David Brownell313980c2005-04-11 15:38:25 -07002751 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2752 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 break;
2755
2756#ifdef USE_ISO
2757 case 2: /* mixed iso/bulk */
2758 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2759 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2760 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2761 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2762
2763 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2764
2765 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2766 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2767 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2768 break;
2769 case 3: /* mixed bulk/iso */
2770 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2771 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2772 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2773
2774 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2775 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2776 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2777
2778 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2779 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2780 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2781 break;
2782#endif
2783
2784 /* add more modes as needed */
2785
2786 default:
2787 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2788 return -ENODEV;
2789 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002790 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2792 return 0;
2793}
2794
Felipe Balbidc1737c2012-05-29 14:33:30 +03002795static int __devinit omap_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 int status = -ENODEV;
2798 int hmc;
Heikki Krogerus86753812012-02-13 13:24:02 +02002799 struct usb_phy *xceiv = NULL;
David Brownell313980c2005-04-11 15:38:25 -07002800 const char *type = NULL;
Russell King3ae5eae2005-11-09 22:32:44 +00002801 struct omap_usb_config *config = pdev->dev.platform_data;
Tony Lindgrenae372572012-05-21 12:01:11 -07002802 struct clk *dc_clk = NULL;
2803 struct clk *hhc_clk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Felipe Balbi5b6d84b2012-05-29 14:27:52 +03002805 if (cpu_is_omap7xx())
2806 use_dma = 0;
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* NOTE: "knows" the order of the resources! */
David Brownelle6a6e472006-12-10 11:47:04 -08002809 if (!request_mem_region(pdev->resource[0].start,
Russell King3ae5eae2005-11-09 22:32:44 +00002810 pdev->resource[0].end - pdev->resource[0].start + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 driver_name)) {
2812 DBG("request_mem_region failed\n");
2813 return -EBUSY;
2814 }
2815
David Brownelle6a6e472006-12-10 11:47:04 -08002816 if (cpu_is_omap16xx()) {
2817 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2818 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2819 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2820 /* can't use omap_udc_enable_clock yet */
2821 clk_enable(dc_clk);
2822 clk_enable(hhc_clk);
2823 udelay(100);
2824 }
2825
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002826 if (cpu_is_omap7xx()) {
2827 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2828 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2829 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2830 /* can't use omap_udc_enable_clock yet */
2831 clk_enable(dc_clk);
2832 clk_enable(hhc_clk);
2833 udelay(100);
2834 }
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 INFO("OMAP UDC rev %d.%d%s\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002837 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 config->otg ? ", Mini-AB" : "");
2839
2840 /* use the mode given to us by board init code */
2841 if (cpu_is_omap15xx()) {
2842 hmc = HMC_1510;
2843 type = "(unknown)";
2844
David Brownell8a3c1f52007-03-21 12:26:32 -07002845 if (machine_without_vbus_sense()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 /* just set up software VBUS detect, and then
2847 * later rig it so we always report VBUS.
2848 * FIXME without really sensing VBUS, we can't
2849 * know when to turn PULLUP_EN on/off; and that
2850 * means we always "need" the 48MHz clock.
2851 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002852 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2853 tmp &= ~VBUS_CTRL_1510;
2854 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 tmp |= VBUS_MODE_1510;
2856 tmp &= ~VBUS_CTRL_1510;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002857 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
2859 } else {
David Brownell65111082005-04-28 13:52:31 -07002860 /* The transceiver may package some GPIO logic or handle
2861 * loopback and/or transceiverless setup; if we find one,
2862 * use it. Except for OTG, we don't _need_ to talk to one;
2863 * but not having one probably means no VBUS detection.
2864 */
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002865 xceiv = usb_get_transceiver();
David Brownell65111082005-04-28 13:52:31 -07002866 if (xceiv)
2867 type = xceiv->label;
2868 else if (config->otg) {
2869 DBG("OTG requires external transceiver!\n");
2870 goto cleanup0;
2871 }
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 hmc = HMC_1610;
David Brownelle6a6e472006-12-10 11:47:04 -08002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 switch (hmc) {
David Brownell313980c2005-04-11 15:38:25 -07002876 case 0: /* POWERUP DEFAULT == 0 */
2877 case 4:
2878 case 12:
2879 case 20:
2880 if (!cpu_is_omap1710()) {
2881 type = "integrated";
2882 break;
2883 }
2884 /* FALL THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 case 3:
2886 case 11:
2887 case 16:
2888 case 19:
2889 case 25:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 if (!xceiv) {
2891 DBG("external transceiver not registered!\n");
David Brownell313980c2005-04-11 15:38:25 -07002892 type = "unknown";
David Brownell65111082005-04-28 13:52:31 -07002893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 case 21: /* internal loopback */
David Brownell313980c2005-04-11 15:38:25 -07002896 type = "loopback";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 break;
2898 case 14: /* transceiverless */
David Brownell65111082005-04-28 13:52:31 -07002899 if (cpu_is_omap1710())
2900 goto bad_on_1710;
2901 /* FALL THROUGH */
2902 case 13:
2903 case 15:
David Brownell313980c2005-04-11 15:38:25 -07002904 type = "no";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906
2907 default:
David Brownell65111082005-04-28 13:52:31 -07002908bad_on_1710:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 ERR("unrecognized UDC HMC mode %d\n", hmc);
David Brownell65111082005-04-28 13:52:31 -07002910 goto cleanup0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912 }
Tony Lindgrenae372572012-05-21 12:01:11 -07002913
David Brownell313980c2005-04-11 15:38:25 -07002914 INFO("hmc mode %d, %s transceiver\n", hmc, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 /* a "gadget" abstracts/virtualizes the controller */
Russell King3ae5eae2005-11-09 22:32:44 +00002917 status = omap_udc_setup(pdev, xceiv);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002918 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 goto cleanup0;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002920
David Brownell313980c2005-04-11 15:38:25 -07002921 xceiv = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002922 /* "udc" is now valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 pullup_disable(udc);
2924#if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2925 udc->gadget.is_otg = (config->otg != 0);
2926#endif
2927
David Brownell65111082005-04-28 13:52:31 -07002928 /* starting with omap1710 es2.0, clear toggle is a separate bit */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002929 if (omap_readw(UDC_REV) >= 0x61)
David Brownell65111082005-04-28 13:52:31 -07002930 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2931 else
2932 udc->clr_halt = UDC_RESET_EP;
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 /* USB general purpose IRQ: ep0, state changes, dma, etc */
Russell King3ae5eae2005-11-09 22:32:44 +00002935 status = request_irq(pdev->resource[1].start, omap_udc_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002936 0, driver_name, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002938 ERR("can't get irq %d, err %d\n",
2939 (int) pdev->resource[1].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 goto cleanup1;
2941 }
2942
2943 /* USB "non-iso" IRQ (PIO for all but ep0) */
Russell King3ae5eae2005-11-09 22:32:44 +00002944 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002945 0, "omap_udc pio", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002947 ERR("can't get irq %d, err %d\n",
2948 (int) pdev->resource[2].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 goto cleanup2;
2950 }
2951#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002952 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002953 0, "omap_udc iso", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002955 ERR("can't get irq %d, err %d\n",
2956 (int) pdev->resource[3].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 goto cleanup3;
2958 }
2959#endif
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002960 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002961 udc->dc_clk = dc_clk;
2962 udc->hhc_clk = hhc_clk;
2963 clk_disable(hhc_clk);
2964 clk_disable(dc_clk);
2965 }
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 create_proc_file();
David Brownelle6a6e472006-12-10 11:47:04 -08002968 status = device_add(&udc->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002969 if (status)
2970 goto cleanup4;
2971
2972 status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
David Brownelle6a6e472006-12-10 11:47:04 -08002973 if (!status)
2974 return status;
2975 /* If fail, fall through */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002976cleanup4:
2977 remove_proc_file();
2978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979#ifdef USE_ISO
2980cleanup3:
Russell King3ae5eae2005-11-09 22:32:44 +00002981 free_irq(pdev->resource[2].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982#endif
2983
2984cleanup2:
Russell King3ae5eae2005-11-09 22:32:44 +00002985 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987cleanup1:
Felipe Balbi80dd1352012-05-29 14:26:09 +03002988 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002989 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991cleanup0:
2992 if (xceiv)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02002993 usb_put_transceiver(xceiv);
David Brownelle6a6e472006-12-10 11:47:04 -08002994
Tony Lindgrenae372572012-05-21 12:01:11 -07002995 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002996 clk_disable(hhc_clk);
2997 clk_disable(dc_clk);
2998 clk_put(hhc_clk);
2999 clk_put(dc_clk);
3000 }
3001
Russell King3ae5eae2005-11-09 22:32:44 +00003002 release_mem_region(pdev->resource[0].start,
3003 pdev->resource[0].end - pdev->resource[0].start + 1);
David Brownelle6a6e472006-12-10 11:47:04 -08003004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 return status;
3006}
3007
Felipe Balbidc1737c2012-05-29 14:33:30 +03003008static int __devexit omap_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07003010 DECLARE_COMPLETION_ONSTACK(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 if (!udc)
3013 return -ENODEV;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003014
3015 usb_del_gadget_udc(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08003016 if (udc->driver)
3017 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 udc->done = &done;
3020
3021 pullup_disable(udc);
3022 if (udc->transceiver) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02003023 usb_put_transceiver(udc->transceiver);
David Brownell313980c2005-04-11 15:38:25 -07003024 udc->transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003026 omap_writew(0, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
3028 remove_proc_file();
3029
3030#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00003031 free_irq(pdev->resource[3].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032#endif
Russell King3ae5eae2005-11-09 22:32:44 +00003033 free_irq(pdev->resource[2].start, udc);
3034 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
David Brownelle6a6e472006-12-10 11:47:04 -08003036 if (udc->dc_clk) {
3037 if (udc->clk_requested)
3038 omap_udc_enable_clock(0);
3039 clk_put(udc->hhc_clk);
3040 clk_put(udc->dc_clk);
3041 }
3042
Russell King3ae5eae2005-11-09 22:32:44 +00003043 release_mem_region(pdev->resource[0].start,
3044 pdev->resource[0].end - pdev->resource[0].start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
3046 device_unregister(&udc->gadget.dev);
3047 wait_for_completion(&done);
3048
3049 return 0;
3050}
3051
David Brownell313980c2005-04-11 15:38:25 -07003052/* suspend/resume/wakeup from sysfs (echo > power/state) or when the
3053 * system is forced into deep sleep
3054 *
3055 * REVISIT we should probably reject suspend requests when there's a host
3056 * session active, rather than disconnecting, at least on boards that can
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003057 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
David Brownell313980c2005-04-11 15:38:25 -07003058 * make host resumes and VBUS detection trigger OMAP wakeup events; that
3059 * may involve talking to an external transceiver (e.g. isp1301).
3060 */
david-b@pacbell.net1d7beee2005-06-29 07:00:56 -07003061
Russell King3ae5eae2005-11-09 22:32:44 +00003062static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
David Brownell313980c2005-04-11 15:38:25 -07003064 u32 devstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003066 devstat = omap_readw(UDC_DEVSTAT);
David Brownell313980c2005-04-11 15:38:25 -07003067
3068 /* we're requesting 48 MHz clock if the pullup is enabled
3069 * (== we're attached to the host) and we're not suspended,
3070 * which would prevent entry to deep sleep...
3071 */
3072 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003073 WARNING("session active; suspend requires disconnect\n");
David Brownell313980c2005-04-11 15:38:25 -07003074 omap_pullup(&udc->gadget, 0);
3075 }
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return 0;
3078}
3079
Russell King3ae5eae2005-11-09 22:32:44 +00003080static int omap_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 DBG("resume + wakeup/SRP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 omap_pullup(&udc->gadget, 1);
3084
3085 /* maybe the host would enumerate us if we nudged it */
3086 msleep(100);
3087 return omap_wakeup(&udc->gadget);
3088}
3089
3090/*-------------------------------------------------------------------------*/
3091
Russell King3ae5eae2005-11-09 22:32:44 +00003092static struct platform_driver udc_driver = {
Felipe Balbidc1737c2012-05-29 14:33:30 +03003093 .probe = omap_udc_probe,
3094 .remove = __devexit_p(omap_udc_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .suspend = omap_udc_suspend,
3096 .resume = omap_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003097 .driver = {
3098 .owner = THIS_MODULE,
3099 .name = (char *) driver_name,
3100 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101};
3102
Felipe Balbidc1737c2012-05-29 14:33:30 +03003103module_platform_driver(udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105MODULE_DESCRIPTION(DRIVER_DESC);
3106MODULE_LICENSE("GPL");
Kay Sieversf34c32f2008-04-10 21:29:21 -07003107MODULE_ALIAS("platform:omap_udc");