blob: d0c87b15b9a324d41375ac9fe7e89077f64ead01 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
3 *
4 * Copyright (C) 2004 Texas Instruments, Inc.
5 * Copyright (C) 2004-2005 David Brownell
6 *
Kyungmin Park527ea732007-11-21 15:13:15 -08007 * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#undef DEBUG
16#undef VERBOSE
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/ioport.h>
21#include <linux/types.h>
22#include <linux/errno.h>
23#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/timer.h>
27#include <linux/list.h>
28#include <linux/interrupt.h>
29#include <linux/proc_fs.h>
30#include <linux/mm.h>
31#include <linux/moduleparam.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010032#include <linux/platform_device.h>
David Brownell5f848132006-12-16 15:34:53 -080033#include <linux/usb/ch9.h>
David Brownell9454a572007-10-04 18:05:17 -070034#include <linux/usb/gadget.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070035#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/dma-mapping.h>
David Brownelle6a6e472006-12-10 11:47:04 -080037#include <linux/clk.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053038#include <linux/err.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070039#include <linux/prefetch.h>
Felipe Balbi80dd1352012-05-29 14:26:09 +030040#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/unaligned.h>
45#include <asm/mach-types.h>
46
Tony Lindgren45c3eb72012-11-30 08:41:50 -080047#include <linux/omap-dma.h>
Tony Lindgrenb924b202012-06-04 00:56:15 -070048
49#include <mach/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include "omap_udc.h"
52
53#undef USB_TRACE
54
55/* bulk DMA seems to be behaving for both IN and OUT */
56#define USE_DMA
57
58/* ISO too */
59#define USE_ISO
60
61#define DRIVER_DESC "OMAP UDC driver"
62#define DRIVER_VERSION "4 October 2004"
63
Lokesh Vutla8c4cc002012-10-15 14:01:01 -070064#define OMAP_DMA_USB_W2FC_TX0 29
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
68 * D+ pullup to allow enumeration. That's too early for the gadget
69 * framework to use from usb_endpoint_enable(), which happens after
70 * enumeration as part of activating an interface. (But if we add an
71 * optional new "UDC not yet running" state to the gadget driver model,
72 * even just during driver binding, the endpoint autoconfig logic is the
73 * natural spot to manufacture new endpoints.)
74 *
75 * So instead of using endpoint enable calls to control the hardware setup,
76 * this driver defines a "fifo mode" parameter. It's used during driver
77 * initialization to choose among a set of pre-defined endpoint configs.
78 * See omap_udc_setup() for available modes, or to add others. That code
79 * lives in an init section, so use this driver as a module if you need
80 * to change the fifo mode after the kernel boots.
81 *
82 * Gadget drivers normally ignore endpoints they don't care about, and
83 * won't include them in configuration descriptors. That means only
84 * misbehaving hosts would even notice they exist.
85 */
86#ifdef USE_ISO
87static unsigned fifo_mode = 3;
88#else
Felipe Balbi80dd1352012-05-29 14:26:09 +030089static unsigned fifo_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#endif
91
92/* "modprobe omap_udc fifo_mode=42", or else as a kernel
93 * boot parameter "omap_udc:fifo_mode=42"
94 */
Felipe Balbi80dd1352012-05-29 14:26:09 +030095module_param(fifo_mode, uint, 0);
96MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98#ifdef USE_DMA
Rusty Russell90ab5ee2012-01-13 09:32:20 +103099static bool use_dma = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/* "modprobe omap_udc use_dma=y", or else as a kernel
102 * boot parameter "omap_udc:use_dma=y"
103 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300104module_param(use_dma, bool, 0);
105MODULE_PARM_DESC(use_dma, "enable/disable DMA");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#else /* !USE_DMA */
107
108/* save a bit of code */
109#define use_dma 0
110#endif /* !USE_DMA */
111
112
Felipe Balbi80dd1352012-05-29 14:26:09 +0300113static const char driver_name[] = "omap_udc";
114static const char driver_desc[] = DRIVER_DESC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*-------------------------------------------------------------------------*/
117
118/* there's a notion of "current endpoint" for modifying endpoint
David Brownelle6a6e472006-12-10 11:47:04 -0800119 * state, and PIO access to its FIFO.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 */
121
122static void use_ep(struct omap_ep *ep, u16 select)
123{
124 u16 num = ep->bEndpointAddress & 0x0f;
125
126 if (ep->bEndpointAddress & USB_DIR_IN)
127 num |= UDC_EP_DIR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300128 omap_writew(num | select, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* when select, MUST deselect later !! */
130}
131
132static inline void deselect_ep(void)
133{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300134 u16 w;
135
136 w = omap_readw(UDC_EP_NUM);
137 w &= ~UDC_EP_SEL;
138 omap_writew(w, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* 6 wait states before TX will happen */
140}
141
142static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
143
144/*-------------------------------------------------------------------------*/
145
146static int omap_ep_enable(struct usb_ep *_ep,
147 const struct usb_endpoint_descriptor *desc)
148{
149 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
150 struct omap_udc *udc;
151 unsigned long flags;
152 u16 maxp;
153
154 /* catch various bogus parameters */
Ido Shayevitz77964b32012-06-04 13:35:25 +0300155 if (!_ep || !desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 || desc->bDescriptorType != USB_DT_ENDPOINT
157 || ep->bEndpointAddress != desc->bEndpointAddress
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700158 || ep->maxpacket < usb_endpoint_maxp(desc)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800159 DBG("%s, bad ep or descriptor\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161 }
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700162 maxp = usb_endpoint_maxp(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
164 && maxp != ep->maxpacket)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700165 || usb_endpoint_maxp(desc) > ep->maxpacket
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 || !desc->wMaxPacketSize) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800167 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return -ERANGE;
169 }
170
171#ifdef USE_ISO
172 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
173 && desc->bInterval != 1)) {
174 /* hardware wants period = 1; USB allows 2^(Interval-1) */
175 DBG("%s, unsupported ISO period %dms\n", _ep->name,
176 1 << (desc->bInterval - 1));
177 return -EDOM;
178 }
179#else
180 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
181 DBG("%s, ISO nyet\n", _ep->name);
182 return -EDOM;
183 }
184#endif
185
186 /* xfer types must match, except that interrupt ~= bulk */
187 if (ep->bmAttributes != desc->bmAttributes
188 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
189 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800190 DBG("%s, %s type mismatch\n", __func__, _ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return -EINVAL;
192 }
193
194 udc = ep->udc;
195 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800196 DBG("%s, bogus device state\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -ESHUTDOWN;
198 }
199
200 spin_lock_irqsave(&udc->lock, flags);
201
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200202 ep->ep.desc = desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ep->irqs = 0;
204 ep->stopped = 0;
205 ep->ep.maxpacket = maxp;
206
207 /* set endpoint to initial state */
208 ep->dma_channel = 0;
209 ep->has_dma = 0;
210 ep->lch = -1;
211 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300212 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 ep->ackwait = 0;
214 deselect_ep();
215
216 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
217 list_add(&ep->iso, &udc->iso);
218
219 /* maybe assign a DMA channel to this endpoint */
220 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
221 /* FIXME ISO can dma, but prefers first channel */
222 dma_channel_claim(ep, 0);
223
224 /* PIO OUT may RX packets */
225 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
226 && !ep->has_dma
227 && !(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300228 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ep->ackwait = 1 + ep->double_buf;
230 }
231
232 spin_unlock_irqrestore(&udc->lock, flags);
233 VDBG("%s enabled\n", _ep->name);
234 return 0;
235}
236
237static void nuke(struct omap_ep *, int status);
238
239static int omap_ep_disable(struct usb_ep *_ep)
240{
241 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
242 unsigned long flags;
243
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200244 if (!_ep || !ep->ep.desc) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800245 DBG("%s, %s not enabled\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 _ep ? ep->ep.name : NULL);
247 return -EINVAL;
248 }
249
250 spin_lock_irqsave(&ep->udc->lock, flags);
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200251 ep->ep.desc = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +0300252 nuke(ep, -ESHUTDOWN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ep->ep.maxpacket = ep->maxpacket;
254 ep->has_dma = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300255 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 list_del_init(&ep->iso);
257 del_timer(&ep->timer);
258
259 spin_unlock_irqrestore(&ep->udc->lock, flags);
260
261 VDBG("%s disabled\n", _ep->name);
262 return 0;
263}
264
265/*-------------------------------------------------------------------------*/
266
267static struct usb_request *
Al Viro55016f12005-10-21 03:21:58 -0400268omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 struct omap_req *req;
271
Eric Sesterhenn7039f422006-02-27 13:34:10 -0800272 req = kzalloc(sizeof(*req), gfp_flags);
Felipe Balbi70617db2012-05-29 14:36:42 +0300273 if (!req)
274 return NULL;
275
Felipe Balbi70617db2012-05-29 14:36:42 +0300276 INIT_LIST_HEAD(&req->queue);
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return &req->req;
279}
280
281static void
282omap_free_request(struct usb_ep *ep, struct usb_request *_req)
283{
284 struct omap_req *req = container_of(_req, struct omap_req, req);
285
Felipe Balbi23673d72012-05-29 14:38:32 +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{
Felipe Balbidd8e9382012-05-29 14:42:56 +0300294 struct omap_udc *udc = ep->udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 unsigned stopped = ep->stopped;
296
297 list_del_init(&req->queue);
298
299 if (req->req.status == -EINPROGRESS)
300 req->req.status = status;
301 else
302 status = req->req.status;
303
Felipe Balbidd8e9382012-05-29 14:42:56 +0300304 if (use_dma && ep->has_dma)
305 usb_gadget_unmap_request(&udc->gadget, &req->req,
306 (ep->bEndpointAddress & USB_DIR_IN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308#ifndef USB_TRACE
309 if (status && status != -ESHUTDOWN)
310#endif
311 VDBG("complete %s req %p stat %d len %u/%u\n",
312 ep->ep.name, &req->req, status,
313 req->req.actual, req->req.length);
314
315 /* don't modify queue heads during completion callback */
316 ep->stopped = 1;
317 spin_unlock(&ep->udc->lock);
318 req->req.complete(&ep->ep, &req->req);
319 spin_lock(&ep->udc->lock);
320 ep->stopped = stopped;
321}
322
323/*-------------------------------------------------------------------------*/
324
David Brownell313980c2005-04-11 15:38:25 -0700325#define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
326#define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328#define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
329#define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
330
David Brownelle6a6e472006-12-10 11:47:04 -0800331static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332write_packet(u8 *buf, struct omap_req *req, unsigned max)
333{
334 unsigned len;
335 u16 *wp;
336
337 len = min(req->req.length - req->req.actual, max);
338 req->req.actual += len;
339
340 max = len;
341 if (likely((((int)buf) & 1) == 0)) {
342 wp = (u16 *)buf;
343 while (max >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300344 omap_writew(*wp++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 max -= 2;
346 }
347 buf = (u8 *)wp;
348 }
349 while (max--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300350 omap_writeb(*buf++, UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return len;
352}
353
Felipe Balbi80dd1352012-05-29 14:26:09 +0300354/* FIXME change r/w fifo calling convention */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356
Felipe Balbi80dd1352012-05-29 14:26:09 +0300357/* return: 0 = still running, 1 = completed, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358static int write_fifo(struct omap_ep *ep, struct omap_req *req)
359{
360 u8 *buf;
361 unsigned count;
362 int is_last;
363 u16 ep_stat;
364
365 buf = req->req.buf + req->req.actual;
366 prefetch(buf);
367
368 /* PIO-IN isn't double buffered except for iso */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300369 ep_stat = omap_readw(UDC_STAT_FLG);
David Brownell313980c2005-04-11 15:38:25 -0700370 if (ep_stat & UDC_FIFO_UNWRITABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return 0;
372
373 count = ep->ep.maxpacket;
374 count = write_packet(buf, req, count);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300375 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ep->ackwait = 1;
377
378 /* last packet is often short (sometimes a zlp) */
379 if (count != ep->ep.maxpacket)
380 is_last = 1;
381 else if (req->req.length == req->req.actual
382 && !req->req.zero)
383 is_last = 1;
384 else
385 is_last = 0;
386
387 /* NOTE: requests complete when all IN data is in a
388 * FIFO (or sometimes later, if a zlp was needed).
389 * Use usb_ep_fifo_status() where needed.
390 */
391 if (is_last)
392 done(ep, req, 0);
393 return is_last;
394}
395
David Brownelle6a6e472006-12-10 11:47:04 -0800396static inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397read_packet(u8 *buf, struct omap_req *req, unsigned avail)
398{
399 unsigned len;
400 u16 *wp;
401
402 len = min(req->req.length - req->req.actual, avail);
403 req->req.actual += len;
404 avail = len;
405
406 if (likely((((int)buf) & 1) == 0)) {
407 wp = (u16 *)buf;
408 while (avail >= 2) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300409 *wp++ = omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 avail -= 2;
411 }
412 buf = (u8 *)wp;
413 }
414 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300415 *buf++ = omap_readb(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return len;
417}
418
Felipe Balbi80dd1352012-05-29 14:26:09 +0300419/* return: 0 = still running, 1 = queue empty, negative = errno */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static int read_fifo(struct omap_ep *ep, struct omap_req *req)
421{
422 u8 *buf;
423 unsigned count, avail;
424 int is_last;
425
426 buf = req->req.buf + req->req.actual;
427 prefetchw(buf);
428
429 for (;;) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300430 u16 ep_stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 is_last = 0;
433 if (ep_stat & FIFO_EMPTY) {
434 if (!ep->double_buf)
435 break;
436 ep->fnf = 1;
437 }
438 if (ep_stat & UDC_EP_HALTED)
439 break;
440
David Brownell313980c2005-04-11 15:38:25 -0700441 if (ep_stat & UDC_FIFO_FULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 avail = ep->ep.maxpacket;
443 else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300444 avail = omap_readw(UDC_RXFSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 ep->fnf = ep->double_buf;
446 }
447 count = read_packet(buf, req, avail);
448
449 /* partial packet reads may not be errors */
450 if (count < ep->ep.maxpacket) {
451 is_last = 1;
452 /* overflowed this request? flush extra data */
453 if (count != avail) {
454 req->req.status = -EOVERFLOW;
455 avail -= count;
456 while (avail--)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300457 omap_readw(UDC_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 } else if (req->req.length == req->req.actual)
460 is_last = 1;
461 else
462 is_last = 0;
463
464 if (!ep->bEndpointAddress)
465 break;
466 if (is_last)
467 done(ep, req, 0);
468 break;
469 }
470 return is_last;
471}
472
473/*-------------------------------------------------------------------------*/
474
475static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
476{
477 dma_addr_t end;
478
479 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
480 * the last transfer's bytecount by more than a FIFO's worth.
481 */
482 if (cpu_is_omap15xx())
483 return 0;
484
Tony Lindgren0499bde2008-07-03 12:24:36 +0300485 end = omap_get_dma_src_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (end == ep->dma_counter)
487 return 0;
488
489 end |= start & (0xffff << 16);
490 if (end < start)
491 end += 0x10000;
492 return end - start;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
496{
497 dma_addr_t end;
498
Tony Lindgren0499bde2008-07-03 12:24:36 +0300499 end = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (end == ep->dma_counter)
501 return 0;
502
503 end |= start & (0xffff << 16);
504 if (cpu_is_omap15xx())
505 end++;
506 if (end < start)
507 end += 0x10000;
508 return end - start;
509}
510
511
512/* Each USB transfer request using DMA maps to one or more DMA transfers.
513 * When DMA completion isn't request completion, the UDC continues with
514 * the next DMA transfer for that USB transfer.
515 */
516
517static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
518{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300519 u16 txdma_ctrl, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 unsigned length = req->req.length - req->req.actual;
521 const int sync_mode = cpu_is_omap15xx()
522 ? OMAP_DMA_SYNC_FRAME
523 : OMAP_DMA_SYNC_ELEMENT;
Kyungmin Park527ea732007-11-21 15:13:15 -0800524 int dma_trigger = 0;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* measure length in either bytes or packets */
David Brownell65111082005-04-28 13:52:31 -0700527 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
529 txdma_ctrl = UDC_TXN_EOT | length;
530 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
Kyungmin Park527ea732007-11-21 15:13:15 -0800531 length, 1, sync_mode, dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 } else {
533 length = min(length / ep->maxpacket,
534 (unsigned) UDC_TXN_TSC + 1);
David Brownelle6a6e472006-12-10 11:47:04 -0800535 txdma_ctrl = length;
David Brownell65111082005-04-28 13:52:31 -0700536 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
David Brownelle6a6e472006-12-10 11:47:04 -0800537 ep->ep.maxpacket >> 1, length, sync_mode,
Kyungmin Park527ea732007-11-21 15:13:15 -0800538 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 length *= ep->maxpacket;
540 }
541 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800542 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
543 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 omap_start_dma(ep->lch);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300546 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300547 w = omap_readw(UDC_DMA_IRQ_EN);
548 w |= UDC_TX_DONE_IE(ep->dma_channel);
549 omap_writew(w, UDC_DMA_IRQ_EN);
550 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 req->dma_bytes = length;
552}
553
554static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
555{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300556 u16 w;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (status == 0) {
559 req->req.actual += req->dma_bytes;
560
561 /* return if this request needs to send data or zlp */
562 if (req->req.actual < req->req.length)
563 return;
564 if (req->req.zero
565 && req->dma_bytes != 0
566 && (req->req.actual % ep->maxpacket) == 0)
567 return;
568 } else
569 req->req.actual += dma_src_len(ep, req->req.dma
570 + req->req.actual);
571
572 /* tx completion */
573 omap_stop_dma(ep->lch);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300574 w = omap_readw(UDC_DMA_IRQ_EN);
575 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
576 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 done(ep, req, status);
578}
579
580static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
581{
Kyungmin Park527ea732007-11-21 15:13:15 -0800582 unsigned packets = req->req.length - req->req.actual;
583 int dma_trigger = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300584 u16 w;
Kyungmin Park527ea732007-11-21 15:13:15 -0800585
Tony Lindgrenae372572012-05-21 12:01:11 -0700586 /* set up this DMA transfer, enable the fifo, start */
587 packets /= ep->ep.maxpacket;
588 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
589 req->dma_bytes = packets * ep->ep.maxpacket;
590 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
591 ep->ep.maxpacket >> 1, packets,
592 OMAP_DMA_SYNC_ELEMENT,
593 dma_trigger, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
David Brownelle6a6e472006-12-10 11:47:04 -0800595 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
596 0, 0);
Tony Lindgren0499bde2008-07-03 12:24:36 +0300597 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300599 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
600 w = omap_readw(UDC_DMA_IRQ_EN);
601 w |= UDC_RX_EOT_IE(ep->dma_channel);
602 omap_writew(w, UDC_DMA_IRQ_EN);
603 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
604 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 omap_start_dma(ep->lch);
607}
608
609static void
David Brownellcb97c5c2005-10-16 15:06:51 -0700610finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300612 u16 count, w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (status == 0)
615 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
616 count = dma_dest_len(ep, req->req.dma + req->req.actual);
617 count += req->req.actual;
David Brownellcb97c5c2005-10-16 15:06:51 -0700618 if (one)
619 count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (count <= req->req.length)
621 req->req.actual = count;
622
623 if (count != req->dma_bytes || status)
624 omap_stop_dma(ep->lch);
625
626 /* if this wasn't short, request may need another transfer */
627 else if (req->req.actual < req->req.length)
628 return;
629
630 /* rx completion */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300631 w = omap_readw(UDC_DMA_IRQ_EN);
632 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
633 omap_writew(w, UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 done(ep, req, status);
635}
636
637static void dma_irq(struct omap_udc *udc, u16 irq_src)
638{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300639 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct omap_ep *ep;
641 struct omap_req *req;
642
643 /* IN dma: tx to host */
644 if (irq_src & UDC_TXN_DONE) {
645 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
646 ep->irqs++;
647 /* can see TXN_DONE after dma abort */
648 if (!list_empty(&ep->queue)) {
649 req = container_of(ep->queue.next,
650 struct omap_req, queue);
651 finish_in_dma(ep, req, 0);
652 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300653 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Felipe Balbi80dd1352012-05-29 14:26:09 +0300655 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 req = container_of(ep->queue.next,
657 struct omap_req, queue);
658 next_in_dma(ep, req);
659 }
660 }
661
662 /* OUT dma: rx from host */
663 if (irq_src & UDC_RXN_EOT) {
664 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
665 ep->irqs++;
666 /* can see RXN_EOT after dma abort */
667 if (!list_empty(&ep->queue)) {
668 req = container_of(ep->queue.next,
669 struct omap_req, queue);
David Brownellcb97c5c2005-10-16 15:06:51 -0700670 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300672 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Felipe Balbi80dd1352012-05-29 14:26:09 +0300674 if (!list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 req = container_of(ep->queue.next,
676 struct omap_req, queue);
677 next_out_dma(ep, req);
678 }
679 }
680
681 if (irq_src & UDC_RXN_CNT) {
682 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
683 ep->irqs++;
684 /* omap15xx does this unasked... */
685 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300686 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688}
689
690static void dma_error(int lch, u16 ch_status, void *data)
691{
692 struct omap_ep *ep = data;
693
694 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
Tony Lindgren7ff879d2006-06-26 16:16:15 -0700695 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
697
698 /* complete current transfer ... */
699}
700
701static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
702{
703 u16 reg;
704 int status, restart, is_in;
Kyungmin Park527ea732007-11-21 15:13:15 -0800705 int dma_channel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 is_in = ep->bEndpointAddress & USB_DIR_IN;
708 if (is_in)
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300709 reg = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300711 reg = omap_readw(UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700712 reg |= UDC_DMA_REQ; /* "pulse" activated */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 ep->dma_channel = 0;
715 ep->lch = -1;
716 if (channel == 0 || channel > 3) {
717 if ((reg & 0x0f00) == 0)
718 channel = 3;
719 else if ((reg & 0x00f0) == 0)
720 channel = 2;
721 else if ((reg & 0x000f) == 0) /* preferred for ISO */
722 channel = 1;
723 else {
724 status = -EMLINK;
725 goto just_restart;
726 }
727 }
728 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
729 ep->dma_channel = channel;
730
731 if (is_in) {
Tony Lindgrenae372572012-05-21 12:01:11 -0700732 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800733 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ep->ep.name, dma_error, ep, &ep->lch);
735 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300736 omap_writew(reg, UDC_TXDMA_CFG);
Kyungmin Park527ea732007-11-21 15:13:15 -0800737 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700738 omap_set_dma_src_burst_mode(ep->lch,
739 OMAP_DMA_DATA_BURST_4);
740 omap_set_dma_src_data_pack(ep->lch, 1);
741 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 omap_set_dma_dest_params(ep->lch,
743 OMAP_DMA_PORT_TIPB,
744 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700745 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800746 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 } else {
Tony Lindgrenae372572012-05-21 12:01:11 -0700749 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
Kyungmin Park527ea732007-11-21 15:13:15 -0800750 status = omap_request_dma(dma_channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ep->ep.name, dma_error, ep, &ep->lch);
752 if (status == 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300753 omap_writew(reg, UDC_RXDMA_CFG);
David Brownell65111082005-04-28 13:52:31 -0700754 /* TIPB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 omap_set_dma_src_params(ep->lch,
756 OMAP_DMA_PORT_TIPB,
757 OMAP_DMA_AMODE_CONSTANT,
David Brownellc3e32082008-08-31 18:04:27 -0700758 UDC_DATA_DMA,
David Brownelle6a6e472006-12-10 11:47:04 -0800759 0, 0);
Kyungmin Park527ea732007-11-21 15:13:15 -0800760 /* EMIFF or SDRC */
David Brownell65111082005-04-28 13:52:31 -0700761 omap_set_dma_dest_burst_mode(ep->lch,
762 OMAP_DMA_DATA_BURST_4);
763 omap_set_dma_dest_data_pack(ep->lch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765 }
766 if (status)
767 ep->dma_channel = 0;
768 else {
769 ep->has_dma = 1;
770 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
771
772 /* channel type P: hw synch (fifo) */
Tony Lindgrenae372572012-05-21 12:01:11 -0700773 if (!cpu_is_omap15xx())
Tony Lindgren0499bde2008-07-03 12:24:36 +0300774 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777just_restart:
778 /* restart any queue, even if the claim failed */
779 restart = !ep->stopped && !list_empty(&ep->queue);
780
781 if (status)
782 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
783 restart ? " (restart)" : "");
784 else
785 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
786 is_in ? 't' : 'r',
787 ep->dma_channel - 1, ep->lch,
788 restart ? " (restart)" : "");
789
790 if (restart) {
791 struct omap_req *req;
792 req = container_of(ep->queue.next, struct omap_req, queue);
793 if (ep->has_dma)
794 (is_in ? next_in_dma : next_out_dma)(ep, req);
795 else {
796 use_ep(ep, UDC_EP_SEL);
797 (is_in ? write_fifo : read_fifo)(ep, req);
798 deselect_ep();
799 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300800 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ep->ackwait = 1 + ep->double_buf;
802 }
803 /* IN: 6 wait states before it'll tx */
804 }
805 }
806}
807
808static void dma_channel_release(struct omap_ep *ep)
809{
810 int shift = 4 * (ep->dma_channel - 1);
811 u16 mask = 0x0f << shift;
812 struct omap_req *req;
813 int active;
814
815 /* abort any active usb transfer request */
816 if (!list_empty(&ep->queue))
817 req = container_of(ep->queue.next, struct omap_req, queue);
818 else
David Brownell313980c2005-04-11 15:38:25 -0700819 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Tony Lindgren0499bde2008-07-03 12:24:36 +0300821 active = omap_get_dma_active_status(ep->lch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
824 active ? "active" : "idle",
825 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
826 ep->dma_channel - 1, req);
827
David Brownell65111082005-04-28 13:52:31 -0700828 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
829 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
830 */
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* wait till current packet DMA finishes, and fifo empties */
833 if (ep->bEndpointAddress & USB_DIR_IN) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300834 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
835 UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (req) {
838 finish_in_dma(ep, req, -ECONNRESET);
839
840 /* clear FIFO; hosts probably won't empty it */
841 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300842 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 deselect_ep();
844 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300845 while (omap_readw(UDC_TXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 udelay(10);
847 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300848 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
849 UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 /* dma empties the fifo */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300852 while (omap_readw(UDC_RXDMA_CFG) & mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 udelay(10);
854 if (req)
David Brownellcb97c5c2005-10-16 15:06:51 -0700855 finish_out_dma(ep, req, -ECONNRESET, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857 omap_free_dma(ep->lch);
858 ep->dma_channel = 0;
859 ep->lch = -1;
860 /* has_dma still set, till endpoint is fully quiesced */
861}
862
863
864/*-------------------------------------------------------------------------*/
865
866static int
Al Viro55016f12005-10-21 03:21:58 -0400867omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
870 struct omap_req *req = container_of(_req, struct omap_req, req);
871 struct omap_udc *udc;
872 unsigned long flags;
873 int is_iso = 0;
874
875 /* catch various bogus parameters */
876 if (!_req || !req->req.complete || !req->req.buf
877 || !list_empty(&req->queue)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800878 DBG("%s, bad params\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return -EINVAL;
880 }
Ido Shayevitzf8bdae02012-03-12 20:25:35 +0200881 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800882 DBG("%s, bad ep\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EINVAL;
884 }
885 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
886 if (req->req.length > ep->ep.maxpacket)
887 return -EMSGSIZE;
888 is_iso = 1;
889 }
890
891 /* this isn't bogus, but OMAP DMA isn't the only hardware to
892 * have a hard time with partial packet reads... reject it.
893 */
894 if (use_dma
895 && ep->has_dma
896 && ep->bEndpointAddress != 0
897 && (ep->bEndpointAddress & USB_DIR_IN) == 0
898 && (req->req.length % ep->ep.maxpacket) != 0) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800899 DBG("%s, no partial packet OUT reads\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -EMSGSIZE;
901 }
902
903 udc = ep->udc;
904 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
905 return -ESHUTDOWN;
906
Felipe Balbidd8e9382012-05-29 14:42:56 +0300907 if (use_dma && ep->has_dma)
908 usb_gadget_map_request(&udc->gadget, &req->req,
909 (ep->bEndpointAddress & USB_DIR_IN));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 VDBG("%s queue req %p, len %d buf %p\n",
912 ep->ep.name, _req, _req->length, _req->buf);
913
914 spin_lock_irqsave(&udc->lock, flags);
915
916 req->req.status = -EINPROGRESS;
917 req->req.actual = 0;
918
919 /* maybe kickstart non-iso i/o queues */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300920 if (is_iso) {
921 u16 w;
922
923 w = omap_readw(UDC_IRQ_EN);
924 w |= UDC_SOF_IE;
925 omap_writew(w, UDC_IRQ_EN);
926 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int is_in;
928
929 if (ep->bEndpointAddress == 0) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300930 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 spin_unlock_irqrestore(&udc->lock, flags);
932 return -EL2HLT;
933 }
934
935 /* empty DATA stage? */
936 is_in = udc->ep0_in;
937 if (!req->req.length) {
938
939 /* chip became CONFIGURED or ADDRESSED
940 * earlier; drivers may already have queued
941 * requests to non-control endpoints
942 */
943 if (udc->ep0_set_config) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300944 u16 irq_en = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
947 if (!udc->ep0_reset_config)
948 irq_en |= UDC_EPN_RX_IE
949 | UDC_EPN_TX_IE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300950 omap_writew(irq_en, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952
David Brownell313980c2005-04-11 15:38:25 -0700953 /* STATUS for zero length DATA stages is
954 * always an IN ... even for IN transfers,
Joe Perchesdc0d5c12007-12-17 11:40:18 -0800955 * a weird case which seem to stall OMAP.
David Brownell313980c2005-04-11 15:38:25 -0700956 */
Felipe Balbi80dd1352012-05-29 14:26:09 +0300957 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
958 UDC_EP_NUM);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300959 omap_writew(UDC_CLR_EP, UDC_CTRL);
960 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
961 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* cleanup */
964 udc->ep0_pending = 0;
965 done(ep, req, 0);
David Brownell313980c2005-04-11 15:38:25 -0700966 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /* non-empty DATA stage */
969 } else if (is_in) {
Felipe Balbi80dd1352012-05-29 14:26:09 +0300970 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
971 UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 } else {
973 if (udc->ep0_setup)
974 goto irq_wait;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300975 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
977 } else {
978 is_in = ep->bEndpointAddress & USB_DIR_IN;
979 if (!ep->has_dma)
980 use_ep(ep, UDC_EP_SEL);
981 /* if ISO: SOF IRQs must be enabled/disabled! */
982 }
983
984 if (ep->has_dma)
985 (is_in ? next_in_dma : next_out_dma)(ep, req);
986 else if (req) {
987 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
David Brownell313980c2005-04-11 15:38:25 -0700988 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 deselect_ep();
990 if (!is_in) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300991 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 ep->ackwait = 1 + ep->double_buf;
993 }
994 /* IN: 6 wait states before it'll tx */
995 }
996 }
997
998irq_wait:
999 /* irq handler advances the queue */
David Brownell313980c2005-04-11 15:38:25 -07001000 if (req != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 list_add_tail(&req->queue, &ep->queue);
1002 spin_unlock_irqrestore(&udc->lock, flags);
1003
1004 return 0;
1005}
1006
1007static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1008{
1009 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1010 struct omap_req *req;
1011 unsigned long flags;
1012
1013 if (!_ep || !_req)
1014 return -EINVAL;
1015
1016 spin_lock_irqsave(&ep->udc->lock, flags);
1017
1018 /* make sure it's actually queued on this endpoint */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001019 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (&req->req == _req)
1021 break;
1022 }
1023 if (&req->req != _req) {
1024 spin_unlock_irqrestore(&ep->udc->lock, flags);
1025 return -EINVAL;
1026 }
1027
1028 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1029 int channel = ep->dma_channel;
1030
1031 /* releasing the channel cancels the request,
1032 * reclaiming the channel restarts the queue
1033 */
1034 dma_channel_release(ep);
1035 dma_channel_claim(ep, channel);
David Brownelle6a6e472006-12-10 11:47:04 -08001036 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 done(ep, req, -ECONNRESET);
1038 spin_unlock_irqrestore(&ep->udc->lock, flags);
1039 return 0;
1040}
1041
1042/*-------------------------------------------------------------------------*/
1043
1044static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1045{
1046 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1047 unsigned long flags;
1048 int status = -EOPNOTSUPP;
1049
1050 spin_lock_irqsave(&ep->udc->lock, flags);
1051
1052 /* just use protocol stalls for ep0; real halts are annoying */
1053 if (ep->bEndpointAddress == 0) {
1054 if (!ep->udc->ep0_pending)
1055 status = -EINVAL;
1056 else if (value) {
1057 if (ep->udc->ep0_set_config) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07001058 WARNING("error changing config?\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001059 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001061 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 ep->udc->ep0_pending = 0;
1063 status = 0;
1064 } else /* NOP */
1065 status = 0;
1066
1067 /* otherwise, all active non-ISO endpoints can halt */
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001068 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /* IN endpoints must already be idle */
1071 if ((ep->bEndpointAddress & USB_DIR_IN)
David Brownelle6a6e472006-12-10 11:47:04 -08001072 && !list_empty(&ep->queue)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 status = -EAGAIN;
1074 goto done;
1075 }
1076
1077 if (value) {
1078 int channel;
1079
1080 if (use_dma && ep->dma_channel
1081 && !list_empty(&ep->queue)) {
1082 channel = ep->dma_channel;
1083 dma_channel_release(ep);
1084 } else
1085 channel = 0;
1086
1087 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001088 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1089 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 status = 0;
1091 } else
1092 status = -EAGAIN;
1093 deselect_ep();
1094
1095 if (channel)
1096 dma_channel_claim(ep, channel);
1097 } else {
1098 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001099 omap_writew(ep->udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 ep->ackwait = 0;
1101 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001102 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ep->ackwait = 1 + ep->double_buf;
1104 }
1105 }
1106 }
1107done:
1108 VDBG("%s %s halt stat %d\n", ep->ep.name,
1109 value ? "set" : "clear", status);
1110
1111 spin_unlock_irqrestore(&ep->udc->lock, flags);
1112 return status;
1113}
1114
1115static struct usb_ep_ops omap_ep_ops = {
1116 .enable = omap_ep_enable,
1117 .disable = omap_ep_disable,
1118
1119 .alloc_request = omap_alloc_request,
1120 .free_request = omap_free_request,
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 .queue = omap_ep_queue,
1123 .dequeue = omap_ep_dequeue,
1124
1125 .set_halt = omap_ep_set_halt,
Felipe Balbi80dd1352012-05-29 14:26:09 +03001126 /* fifo_status ... report bytes in fifo */
1127 /* fifo_flush ... flush fifo */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128};
1129
1130/*-------------------------------------------------------------------------*/
1131
1132static int omap_get_frame(struct usb_gadget *gadget)
1133{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001134 u16 sof = omap_readw(UDC_SOF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1136}
1137
1138static int omap_wakeup(struct usb_gadget *gadget)
1139{
1140 struct omap_udc *udc;
1141 unsigned long flags;
1142 int retval = -EHOSTUNREACH;
1143
1144 udc = container_of(gadget, struct omap_udc, gadget);
1145
1146 spin_lock_irqsave(&udc->lock, flags);
1147 if (udc->devstat & UDC_SUS) {
1148 /* NOTE: OTG spec erratum says that OTG devices may
1149 * issue wakeups without host enable.
1150 */
1151 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1152 DBG("remote wakeup...\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001153 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 retval = 0;
1155 }
1156
1157 /* NOTE: non-OTG systems may use SRP TOO... */
1158 } else if (!(udc->devstat & UDC_ATT)) {
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301159 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001160 retval = otg_start_srp(udc->transceiver->otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162 spin_unlock_irqrestore(&udc->lock, flags);
1163
1164 return retval;
1165}
1166
1167static int
1168omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1169{
1170 struct omap_udc *udc;
1171 unsigned long flags;
1172 u16 syscon1;
1173
1174 udc = container_of(gadget, struct omap_udc, gadget);
1175 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001176 syscon1 = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (is_selfpowered)
1178 syscon1 |= UDC_SELF_PWR;
1179 else
1180 syscon1 &= ~UDC_SELF_PWR;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001181 omap_writew(syscon1, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 spin_unlock_irqrestore(&udc->lock, flags);
1183
1184 return 0;
1185}
1186
1187static int can_pullup(struct omap_udc *udc)
1188{
1189 return udc->driver && udc->softconnect && udc->vbus_active;
1190}
1191
1192static void pullup_enable(struct omap_udc *udc)
1193{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001194 u16 w;
1195
1196 w = omap_readw(UDC_SYSCON1);
1197 w |= UDC_PULLUP_EN;
1198 omap_writew(w, UDC_SYSCON1);
1199 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1200 u32 l;
1201
1202 l = omap_readl(OTG_CTRL);
1203 l |= OTG_BSESSVLD;
1204 omap_writel(l, OTG_CTRL);
1205 }
1206 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209static void pullup_disable(struct omap_udc *udc)
1210{
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001211 u16 w;
1212
1213 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1214 u32 l;
1215
1216 l = omap_readl(OTG_CTRL);
1217 l &= ~OTG_BSESSVLD;
1218 omap_writel(l, OTG_CTRL);
1219 }
1220 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1221 w = omap_readw(UDC_SYSCON1);
1222 w &= ~UDC_PULLUP_EN;
1223 omap_writew(w, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
David Brownelle6a6e472006-12-10 11:47:04 -08001226static struct omap_udc *udc;
1227
1228static void omap_udc_enable_clock(int enable)
1229{
1230 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1231 return;
1232
1233 if (enable) {
1234 clk_enable(udc->dc_clk);
1235 clk_enable(udc->hhc_clk);
1236 udelay(100);
1237 } else {
1238 clk_disable(udc->hhc_clk);
1239 clk_disable(udc->dc_clk);
1240 }
1241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/*
1244 * Called by whatever detects VBUS sessions: external transceiver
1245 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1246 */
1247static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1248{
1249 struct omap_udc *udc;
1250 unsigned long flags;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001251 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 udc = container_of(gadget, struct omap_udc, gadget);
1254 spin_lock_irqsave(&udc->lock, flags);
1255 VDBG("VBUS %s\n", is_active ? "on" : "off");
1256 udc->vbus_active = (is_active != 0);
1257 if (cpu_is_omap15xx()) {
1258 /* "software" detect, ignored if !VBUS_MODE_1510 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001259 l = omap_readl(FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (is_active)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001261 l |= VBUS_CTRL_1510;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001263 l &= ~VBUS_CTRL_1510;
1264 omap_writel(l, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
David Brownelle6a6e472006-12-10 11:47:04 -08001266 if (udc->dc_clk != NULL && is_active) {
1267 if (!udc->clk_requested) {
1268 omap_udc_enable_clock(1);
1269 udc->clk_requested = 1;
1270 }
1271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 if (can_pullup(udc))
1273 pullup_enable(udc);
1274 else
1275 pullup_disable(udc);
David Brownelle6a6e472006-12-10 11:47:04 -08001276 if (udc->dc_clk != NULL && !is_active) {
1277 if (udc->clk_requested) {
1278 omap_udc_enable_clock(0);
1279 udc->clk_requested = 0;
1280 }
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 spin_unlock_irqrestore(&udc->lock, flags);
1283 return 0;
1284}
1285
1286static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1287{
1288 struct omap_udc *udc;
1289
1290 udc = container_of(gadget, struct omap_udc, gadget);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301291 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001292 return usb_phy_set_power(udc->transceiver, mA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return -EOPNOTSUPP;
1294}
1295
1296static int omap_pullup(struct usb_gadget *gadget, int is_on)
1297{
1298 struct omap_udc *udc;
1299 unsigned long flags;
1300
1301 udc = container_of(gadget, struct omap_udc, gadget);
1302 spin_lock_irqsave(&udc->lock, flags);
1303 udc->softconnect = (is_on != 0);
1304 if (can_pullup(udc))
1305 pullup_enable(udc);
1306 else
1307 pullup_disable(udc);
1308 spin_unlock_irqrestore(&udc->lock, flags);
1309 return 0;
1310}
1311
Felipe Balbi1bf0cf62013-01-24 10:52:52 +02001312static int omap_udc_start(struct usb_gadget *g,
1313 struct usb_gadget_driver *driver)
1314static int omap_udc_stop(struct usb_gadget *g,
1315 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317static struct usb_gadget_ops omap_gadget_ops = {
1318 .get_frame = omap_get_frame,
1319 .wakeup = omap_wakeup,
1320 .set_selfpowered = omap_set_selfpowered,
1321 .vbus_session = omap_vbus_session,
1322 .vbus_draw = omap_vbus_draw,
1323 .pullup = omap_pullup,
Felipe Balbi1bf0cf62013-01-24 10:52:52 +02001324 .udc_start = omap_udc_start,
1325 .udc_stop = omap_udc_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326};
1327
1328/*-------------------------------------------------------------------------*/
1329
1330/* dequeue ALL requests; caller holds udc->lock */
1331static void nuke(struct omap_ep *ep, int status)
1332{
1333 struct omap_req *req;
1334
1335 ep->stopped = 1;
1336
1337 if (use_dma && ep->dma_channel)
1338 dma_channel_release(ep);
1339
1340 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001341 omap_writew(UDC_CLR_EP, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001343 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 while (!list_empty(&ep->queue)) {
1346 req = list_entry(ep->queue.next, struct omap_req, queue);
1347 done(ep, req, status);
1348 }
1349}
1350
1351/* caller holds udc->lock */
1352static void udc_quiesce(struct omap_udc *udc)
1353{
1354 struct omap_ep *ep;
1355
1356 udc->gadget.speed = USB_SPEED_UNKNOWN;
1357 nuke(&udc->ep[0], -ESHUTDOWN);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001358 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 nuke(ep, -ESHUTDOWN);
1360}
1361
1362/*-------------------------------------------------------------------------*/
1363
1364static void update_otg(struct omap_udc *udc)
1365{
1366 u16 devstat;
1367
David Brownell9cfbba72007-10-26 13:42:18 -07001368 if (!gadget_is_otg(&udc->gadget))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
1370
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001371 if (omap_readl(OTG_CTRL) & OTG_ID)
1372 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 else
1374 devstat = 0;
1375
1376 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1377 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1378 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1379
1380 /* Enable HNP early, avoiding races on suspend irq path.
1381 * ASSUMES OTG state machine B_BUS_REQ input is true.
1382 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001383 if (udc->gadget.b_hnp_enable) {
1384 u32 l;
1385
1386 l = omap_readl(OTG_CTRL);
1387 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1388 l &= ~OTG_PULLUP;
1389 omap_writel(l, OTG_CTRL);
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1394{
1395 struct omap_ep *ep0 = &udc->ep[0];
David Brownell313980c2005-04-11 15:38:25 -07001396 struct omap_req *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 ep0->irqs++;
1399
1400 /* Clear any pending requests and then scrub any rx/tx state
1401 * before starting to handle the SETUP request.
1402 */
1403 if (irq_src & UDC_SETUP) {
1404 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1405
1406 nuke(ep0, 0);
1407 if (ack) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001408 omap_writew(ack, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 irq_src = UDC_SETUP;
1410 }
1411 }
1412
David Brownelle6a6e472006-12-10 11:47:04 -08001413 /* IN/OUT packets mean we're in the DATA or STATUS stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * This driver uses only uses protocol stalls (ep0 never halts),
1415 * and if we got this far the gadget driver already had a
1416 * chance to stall. Tries to be forgiving of host oddities.
1417 *
1418 * NOTE: the last chance gadget drivers have to stall control
1419 * requests is during their request completion callback.
1420 */
1421 if (!list_empty(&ep0->queue))
1422 req = container_of(ep0->queue.next, struct omap_req, queue);
1423
1424 /* IN == TX to host */
1425 if (irq_src & UDC_EP0_TX) {
1426 int stat;
1427
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001428 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1429 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1430 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (stat & UDC_ACK) {
1432 if (udc->ep0_in) {
1433 /* write next IN packet from response,
1434 * or set up the status stage.
1435 */
1436 if (req)
1437 stat = write_fifo(ep0, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001438 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!req && udc->ep0_pending) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001440 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1441 omap_writew(UDC_CLR_EP, UDC_CTRL);
1442 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1443 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 udc->ep0_pending = 0;
1445 } /* else: 6 wait states before it'll tx */
1446 } else {
1447 /* ack status stage of OUT transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001448 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (req)
1450 done(ep0, req, 0);
1451 }
David Brownell313980c2005-04-11 15:38:25 -07001452 req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001454 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1455 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001457 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 }
1460
1461 /* OUT == RX from host */
1462 if (irq_src & UDC_EP0_RX) {
1463 int stat;
1464
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001465 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1466 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1467 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (stat & UDC_ACK) {
1469 if (!udc->ep0_in) {
1470 stat = 0;
1471 /* read next OUT packet of request, maybe
1472 * reactiviting the fifo; stall on errors.
1473 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001474 stat = read_fifo(ep0, req);
1475 if (!req || stat < 0) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001476 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 udc->ep0_pending = 0;
1478 stat = 0;
1479 } else if (stat == 0)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001480 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1481 omap_writew(0, UDC_EP_NUM);
David Brownelle6a6e472006-12-10 11:47:04 -08001482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* activate status stage */
1484 if (stat == 1) {
1485 done(ep0, req, 0);
1486 /* that may have STALLed ep0... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001487 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1488 UDC_EP_NUM);
1489 omap_writew(UDC_CLR_EP, UDC_CTRL);
1490 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1491 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 udc->ep0_pending = 0;
1493 }
1494 } else {
1495 /* ack status stage of IN transfer */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001496 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (req)
1498 done(ep0, req, 0);
1499 }
1500 } else if (stat & UDC_STALL) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001501 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1502 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001504 omap_writew(0, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 }
1507
1508 /* SETUP starts all control transfers */
1509 if (irq_src & UDC_SETUP) {
1510 union u {
1511 u16 word[4];
1512 struct usb_ctrlrequest r;
1513 } u;
1514 int status = -EINVAL;
1515 struct omap_ep *ep;
1516
1517 /* read the (latest) SETUP message */
1518 do {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001519 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 /* two bytes at a time */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001521 u.word[0] = omap_readw(UDC_DATA);
1522 u.word[1] = omap_readw(UDC_DATA);
1523 u.word[2] = omap_readw(UDC_DATA);
1524 u.word[3] = omap_readw(UDC_DATA);
1525 omap_writew(0, UDC_EP_NUM);
1526 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
David Brownell01ee7d72007-05-25 20:40:14 -07001528#define w_value le16_to_cpu(u.r.wValue)
1529#define w_index le16_to_cpu(u.r.wIndex)
1530#define w_length le16_to_cpu(u.r.wLength)
David Brownell65111082005-04-28 13:52:31 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 /* Delegate almost all control requests to the gadget driver,
1533 * except for a handful of ch9 status/feature requests that
1534 * hardware doesn't autodecode _and_ the gadget API hides.
1535 */
1536 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1537 udc->ep0_set_config = 0;
1538 udc->ep0_pending = 1;
1539 ep0->stopped = 0;
1540 ep0->ackwait = 0;
1541 switch (u.r.bRequest) {
1542 case USB_REQ_SET_CONFIGURATION:
1543 /* udc needs to know when ep != 0 is valid */
1544 if (u.r.bRequestType != USB_RECIP_DEVICE)
1545 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001546 if (w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 goto do_stall;
1548 udc->ep0_set_config = 1;
David Brownell65111082005-04-28 13:52:31 -07001549 udc->ep0_reset_config = (w_value == 0);
1550 VDBG("set config %d\n", w_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 /* update udc NOW since gadget driver may start
1553 * queueing requests immediately; clear config
1554 * later if it fails the request.
1555 */
1556 if (udc->ep0_reset_config)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001557 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001559 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 update_otg(udc);
1561 goto delegate;
1562 case USB_REQ_CLEAR_FEATURE:
1563 /* clear endpoint halt */
1564 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1565 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001566 if (w_value != USB_ENDPOINT_HALT
1567 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001569 ep = &udc->ep[w_index & 0xf];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (ep != ep0) {
David Brownell65111082005-04-28 13:52:31 -07001571 if (w_index & USB_DIR_IN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 ep += 16;
1573 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001574 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 goto do_stall;
1576 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001577 omap_writew(udc->clr_halt, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ep->ackwait = 0;
1579 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001580 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 ep->ackwait = 1 + ep->double_buf;
1582 }
David Brownell313980c2005-04-11 15:38:25 -07001583 /* NOTE: assumes the host behaves sanely,
1584 * only clearing real halts. Else we may
1585 * need to kill pending transfers and then
1586 * restart the queue... very messy for DMA!
1587 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589 VDBG("%s halt cleared by host\n", ep->name);
1590 goto ep0out_status_stage;
1591 case USB_REQ_SET_FEATURE:
1592 /* set endpoint halt */
1593 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1594 goto delegate;
David Brownell65111082005-04-28 13:52:31 -07001595 if (w_value != USB_ENDPOINT_HALT
1596 || w_length != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto do_stall;
David Brownell65111082005-04-28 13:52:31 -07001598 ep = &udc->ep[w_index & 0xf];
1599 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 == ep0 || !ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 goto do_stall;
1604 if (use_dma && ep->has_dma) {
1605 /* this has rude side-effects (aborts) and
1606 * can't really work if DMA-IN is active
1607 */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001608 DBG("%s host set_halt, NYET\n", ep->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 goto do_stall;
1610 }
1611 use_ep(ep, 0);
1612 /* can't halt if fifo isn't empty... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001613 omap_writew(UDC_CLR_EP, UDC_CTRL);
1614 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 VDBG("%s halted by host\n", ep->name);
1616ep0out_status_stage:
1617 status = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001618 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1619 omap_writew(UDC_CLR_EP, UDC_CTRL);
1620 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1621 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 udc->ep0_pending = 0;
1623 break;
1624 case USB_REQ_GET_STATUS:
David Brownell8a3c1f52007-03-21 12:26:32 -07001625 /* USB_ENDPOINT_HALT status? */
1626 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1627 goto intf_status;
1628
1629 /* ep0 never stalls */
1630 if (!(w_index & 0xf))
1631 goto zero_status;
1632
1633 /* only active endpoints count */
1634 ep = &udc->ep[w_index & 0xf];
1635 if (w_index & USB_DIR_IN)
1636 ep += 16;
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02001637 if (!ep->ep.desc)
David Brownell8a3c1f52007-03-21 12:26:32 -07001638 goto do_stall;
1639
1640 /* iso never stalls */
1641 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1642 goto zero_status;
1643
1644 /* FIXME don't assume non-halted endpoints!! */
1645 ERR("%s status, can't report\n", ep->ep.name);
1646 goto do_stall;
1647
1648intf_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* return interface status. if we were pedantic,
1650 * we'd detect non-existent interfaces, and stall.
1651 */
1652 if (u.r.bRequestType
1653 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1654 goto delegate;
David Brownell8a3c1f52007-03-21 12:26:32 -07001655
1656zero_status:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /* return two zero bytes */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001658 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1659 omap_writew(0, UDC_DATA);
1660 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1661 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 status = 0;
David Brownell65111082005-04-28 13:52:31 -07001663 VDBG("GET_STATUS, interface %d\n", w_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* next, status stage */
1665 break;
1666 default:
1667delegate:
1668 /* activate the ep0out fifo right away */
David Brownell65111082005-04-28 13:52:31 -07001669 if (!udc->ep0_in && w_length) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001670 omap_writew(0, UDC_EP_NUM);
1671 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 /* gadget drivers see class/vendor specific requests,
1675 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1676 * and more
1677 */
1678 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1679 u.r.bRequestType, u.r.bRequest,
David Brownell65111082005-04-28 13:52:31 -07001680 w_value, w_index, w_length);
1681
1682#undef w_value
1683#undef w_index
1684#undef w_length
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 /* The gadget driver may return an error here,
1687 * causing an immediate protocol stall.
1688 *
1689 * Else it must issue a response, either queueing a
1690 * response buffer for the DATA stage, or halting ep0
1691 * (causing a protocol stall, not a real halt). A
1692 * zero length buffer means no DATA stage.
1693 *
1694 * It's fine to issue that response after the setup()
1695 * call returns, and this IRQ was handled.
1696 */
1697 udc->ep0_setup = 1;
1698 spin_unlock(&udc->lock);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001699 status = udc->driver->setup(&udc->gadget, &u.r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 spin_lock(&udc->lock);
1701 udc->ep0_setup = 0;
1702 }
1703
1704 if (status < 0) {
1705do_stall:
1706 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1707 u.r.bRequestType, u.r.bRequest, status);
1708 if (udc->ep0_set_config) {
1709 if (udc->ep0_reset_config)
Arjan van de Venb6c63932008-07-25 01:45:52 -07001710 WARNING("error resetting config?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001712 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001714 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 udc->ep0_pending = 0;
1716 }
1717 }
1718}
1719
1720/*-------------------------------------------------------------------------*/
1721
1722#define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1723
1724static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1725{
1726 u16 devstat, change;
1727
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001728 devstat = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 change = devstat ^ udc->devstat;
1730 udc->devstat = devstat;
1731
1732 if (change & (UDC_USB_RESET|UDC_ATT)) {
1733 udc_quiesce(udc);
1734
1735 if (change & UDC_ATT) {
1736 /* driver for any external transceiver will
1737 * have called omap_vbus_session() already
1738 */
1739 if (devstat & UDC_ATT) {
1740 udc->gadget.speed = USB_SPEED_FULL;
1741 VDBG("connect\n");
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301742 if (IS_ERR_OR_NULL(udc->transceiver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 pullup_enable(udc);
Felipe Balbi80dd1352012-05-29 14:26:09 +03001744 /* if (driver->connect) call it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1746 udc->gadget.speed = USB_SPEED_UNKNOWN;
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301747 if (IS_ERR_OR_NULL(udc->transceiver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 pullup_disable(udc);
1749 DBG("disconnect, gadget %s\n",
1750 udc->driver->driver.name);
1751 if (udc->driver->disconnect) {
1752 spin_unlock(&udc->lock);
1753 udc->driver->disconnect(&udc->gadget);
1754 spin_lock(&udc->lock);
1755 }
1756 }
1757 change &= ~UDC_ATT;
1758 }
1759
1760 if (change & UDC_USB_RESET) {
1761 if (devstat & UDC_USB_RESET) {
1762 VDBG("RESET=1\n");
1763 } else {
1764 udc->gadget.speed = USB_SPEED_FULL;
1765 INFO("USB reset done, gadget %s\n",
1766 udc->driver->driver.name);
1767 /* ep0 traffic is legal from now on */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001768 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1769 UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 change &= ~UDC_USB_RESET;
1772 }
1773 }
1774 if (change & UDC_SUS) {
1775 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03001776 /* FIXME tell isp1301 to suspend/resume (?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (devstat & UDC_SUS) {
1778 VDBG("suspend\n");
1779 update_otg(udc);
1780 /* HNP could be under way already */
1781 if (udc->gadget.speed == USB_SPEED_FULL
1782 && udc->driver->suspend) {
1783 spin_unlock(&udc->lock);
1784 udc->driver->suspend(&udc->gadget);
1785 spin_lock(&udc->lock);
1786 }
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301787 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001788 usb_phy_set_suspend(
1789 udc->transceiver, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 } else {
1791 VDBG("resume\n");
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05301792 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001793 usb_phy_set_suspend(
1794 udc->transceiver, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (udc->gadget.speed == USB_SPEED_FULL
1796 && udc->driver->resume) {
1797 spin_unlock(&udc->lock);
1798 udc->driver->resume(&udc->gadget);
1799 spin_lock(&udc->lock);
1800 }
1801 }
1802 }
1803 change &= ~UDC_SUS;
1804 }
1805 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1806 update_otg(udc);
1807 change &= ~OTG_FLAGS;
1808 }
1809
1810 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1811 if (change)
1812 VDBG("devstat %03x, ignore change %03x\n",
1813 devstat, change);
1814
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001815 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
David Howells7d12e782006-10-05 14:55:46 +01001818static irqreturn_t omap_udc_irq(int irq, void *_udc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 struct omap_udc *udc = _udc;
1821 u16 irq_src;
1822 irqreturn_t status = IRQ_NONE;
1823 unsigned long flags;
1824
1825 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001826 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 /* Device state change (usb ch9 stuff) */
1829 if (irq_src & UDC_DS_CHG) {
1830 devstate_irq(_udc, irq_src);
1831 status = IRQ_HANDLED;
1832 irq_src &= ~UDC_DS_CHG;
1833 }
1834
1835 /* EP0 control transfers */
1836 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1837 ep0_irq(_udc, irq_src);
1838 status = IRQ_HANDLED;
1839 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1840 }
1841
1842 /* DMA transfer completion */
1843 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1844 dma_irq(_udc, irq_src);
1845 status = IRQ_HANDLED;
1846 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1847 }
1848
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001849 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (irq_src)
1851 DBG("udc_irq, unhandled %03x\n", irq_src);
1852 spin_unlock_irqrestore(&udc->lock, flags);
1853
1854 return status;
1855}
1856
1857/* workaround for seemingly-lost IRQs for RX ACKs... */
1858#define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1859#define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1860
1861static void pio_out_timer(unsigned long _ep)
1862{
1863 struct omap_ep *ep = (void *) _ep;
1864 unsigned long flags;
1865 u16 stat_flg;
1866
1867 spin_lock_irqsave(&ep->udc->lock, flags);
1868 if (!list_empty(&ep->queue) && ep->ackwait) {
David Brownelle6a6e472006-12-10 11:47:04 -08001869 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001870 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1873 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1874 struct omap_req *req;
1875
1876 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1877 req = container_of(ep->queue.next,
1878 struct omap_req, queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 (void) read_fifo(ep, req);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001880 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1881 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 ep->ackwait = 1 + ep->double_buf;
David Brownelle6a6e472006-12-10 11:47:04 -08001883 } else
1884 deselect_ep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1887 spin_unlock_irqrestore(&ep->udc->lock, flags);
1888}
1889
David Howells7d12e782006-10-05 14:55:46 +01001890static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 u16 epn_stat, irq_src;
1893 irqreturn_t status = IRQ_NONE;
1894 struct omap_ep *ep;
1895 int epnum;
1896 struct omap_udc *udc = _dev;
1897 struct omap_req *req;
1898 unsigned long flags;
1899
1900 spin_lock_irqsave(&udc->lock, flags);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001901 epn_stat = omap_readw(UDC_EPN_STAT);
1902 irq_src = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* handle OUT first, to avoid some wasteful NAKs */
1905 if (irq_src & UDC_EPN_RX) {
1906 epnum = (epn_stat >> 8) & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001907 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 status = IRQ_HANDLED;
1909 ep = &udc->ep[epnum];
1910 ep->irqs++;
1911
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001912 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 ep->fnf = 0;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001914 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 ep->ackwait--;
1916 if (!list_empty(&ep->queue)) {
1917 int stat;
1918 req = container_of(ep->queue.next,
1919 struct omap_req, queue);
1920 stat = read_fifo(ep, req);
1921 if (!ep->double_buf)
1922 ep->fnf = 1;
1923 }
1924 }
1925 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001926 epn_stat = omap_readw(UDC_EPN_STAT);
1927 epn_stat = omap_readw(UDC_EPN_STAT);
1928 omap_writew(epnum, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* enabling fifo _after_ clearing ACK, contrary to docs,
1931 * reduces lossage; timer still needed though (sigh).
1932 */
1933 if (ep->fnf) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001934 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 ep->ackwait = 1 + ep->double_buf;
1936 }
1937 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1938 }
1939
1940 /* then IN transfers */
1941 else if (irq_src & UDC_EPN_TX) {
1942 epnum = epn_stat & 0x0f;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001943 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 status = IRQ_HANDLED;
1945 ep = &udc->ep[16 + epnum];
1946 ep->irqs++;
1947
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001948 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1949 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 ep->ackwait = 0;
1951 if (!list_empty(&ep->queue)) {
1952 req = container_of(ep->queue.next,
1953 struct omap_req, queue);
1954 (void) write_fifo(ep, req);
1955 }
1956 }
1957 /* min 6 clock delay before clearing EP_SEL ... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001958 epn_stat = omap_readw(UDC_EPN_STAT);
1959 epn_stat = omap_readw(UDC_EPN_STAT);
1960 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* then 6 clocks before it'd tx */
1962 }
1963
1964 spin_unlock_irqrestore(&udc->lock, flags);
1965 return status;
1966}
1967
1968#ifdef USE_ISO
David Howells7d12e782006-10-05 14:55:46 +01001969static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct omap_udc *udc = _dev;
1972 struct omap_ep *ep;
1973 int pending = 0;
1974 unsigned long flags;
1975
1976 spin_lock_irqsave(&udc->lock, flags);
1977
1978 /* handle all non-DMA ISO transfers */
Felipe Balbi80dd1352012-05-29 14:26:09 +03001979 list_for_each_entry(ep, &udc->iso, iso) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 u16 stat;
1981 struct omap_req *req;
1982
1983 if (ep->has_dma || list_empty(&ep->queue))
1984 continue;
1985 req = list_entry(ep->queue.next, struct omap_req, queue);
1986
1987 use_ep(ep, UDC_EP_SEL);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001988 stat = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /* NOTE: like the other controller drivers, this isn't
1991 * currently reporting lost or damaged frames.
1992 */
1993 if (ep->bEndpointAddress & USB_DIR_IN) {
1994 if (stat & UDC_MISS_IN)
1995 /* done(ep, req, -EPROTO) */;
1996 else
1997 write_fifo(ep, req);
1998 } else {
1999 int status = 0;
2000
2001 if (stat & UDC_NO_RXPACKET)
2002 status = -EREMOTEIO;
2003 else if (stat & UDC_ISO_ERR)
2004 status = -EILSEQ;
2005 else if (stat & UDC_DATA_FLUSH)
2006 status = -ENOSR;
2007
2008 if (status)
2009 /* done(ep, req, status) */;
2010 else
2011 read_fifo(ep, req);
2012 }
2013 deselect_ep();
2014 /* 6 wait states before next EP */
2015
2016 ep->irqs++;
2017 if (!list_empty(&ep->queue))
2018 pending = 1;
2019 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002020 if (!pending) {
2021 u16 w;
2022
2023 w = omap_readw(UDC_IRQ_EN);
2024 w &= ~UDC_SOF_IE;
2025 omap_writew(w, UDC_IRQ_EN);
2026 }
2027 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 spin_unlock_irqrestore(&udc->lock, flags);
2030 return IRQ_HANDLED;
2031}
2032#endif
2033
2034/*-------------------------------------------------------------------------*/
2035
David Brownell8a3c1f52007-03-21 12:26:32 -07002036static inline int machine_without_vbus_sense(void)
David Brownelle6a6e472006-12-10 11:47:04 -08002037{
Felipe Balbi80dd1352012-05-29 14:26:09 +03002038 return machine_is_omap_innovator()
David Brownelle6a6e472006-12-10 11:47:04 -08002039 || machine_is_omap_osk()
David Brownelle6a6e472006-12-10 11:47:04 -08002040 || machine_is_sx1()
Felipe Balbi80dd1352012-05-29 14:26:09 +03002041 /* No known omap7xx boards with vbus sense */
2042 || cpu_is_omap7xx();
David Brownelle6a6e472006-12-10 11:47:04 -08002043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Felipe Balbi1bf0cf62013-01-24 10:52:52 +02002045static int omap_udc_start(struct usb_gadget *g,
2046 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 int status = -ENODEV;
2049 struct omap_ep *ep;
2050 unsigned long flags;
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 spin_lock_irqsave(&udc->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /* reset state */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002055 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ep->irqs = 0;
2057 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2058 continue;
2059 use_ep(ep, 0);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002060 omap_writew(UDC_SET_HALT, UDC_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 udc->ep0_pending = 0;
2063 udc->ep[0].irqs = 0;
2064 udc->softconnect = 1;
2065
2066 /* hook up the driver */
David Brownell313980c2005-04-11 15:38:25 -07002067 driver->driver.bus = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 udc->driver = driver;
2069 udc->gadget.dev.driver = &driver->driver;
2070 spin_unlock_irqrestore(&udc->lock, flags);
2071
David Brownelle6a6e472006-12-10 11:47:04 -08002072 if (udc->dc_clk != NULL)
2073 omap_udc_enable_clock(1);
2074
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002075 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* connect to bus through transceiver */
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302078 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002079 status = otg_set_peripheral(udc->transceiver->otg,
2080 &udc->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (status < 0) {
2082 ERR("can't bind to transceiver\n");
David Brownell6bea4762006-12-05 03:15:33 -08002083 if (driver->unbind) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002084 driver->unbind(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002085 udc->gadget.dev.driver = NULL;
2086 udc->driver = NULL;
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto done;
2089 }
2090 } else {
2091 if (can_pullup(udc))
Felipe Balbi80dd1352012-05-29 14:26:09 +03002092 pullup_enable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002094 pullup_disable(udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
2097 /* boards that don't have VBUS sensing can't autogate 48MHz;
2098 * can't enter deep sleep while a gadget driver is active.
2099 */
David Brownell8a3c1f52007-03-21 12:26:32 -07002100 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 omap_vbus_session(&udc->gadget, 1);
2102
2103done:
David Brownelle6a6e472006-12-10 11:47:04 -08002104 if (udc->dc_clk != NULL)
2105 omap_udc_enable_clock(0);
Felipe Balbi1bf0cf62013-01-24 10:52:52 +02002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return status;
2108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Felipe Balbi1bf0cf62013-01-24 10:52:52 +02002110static int omap_udc_stop(struct usb_gadget *g,
2111 struct usb_gadget_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 unsigned long flags;
2114 int status = -ENODEV;
2115
David Brownelle6a6e472006-12-10 11:47:04 -08002116 if (udc->dc_clk != NULL)
2117 omap_udc_enable_clock(1);
2118
David Brownell8a3c1f52007-03-21 12:26:32 -07002119 if (machine_without_vbus_sense())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 omap_vbus_session(&udc->gadget, 0);
2121
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302122 if (!IS_ERR_OR_NULL(udc->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002123 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 else
2125 pullup_disable(udc);
2126
2127 spin_lock_irqsave(&udc->lock, flags);
2128 udc_quiesce(udc);
2129 spin_unlock_irqrestore(&udc->lock, flags);
2130
David Brownell313980c2005-04-11 15:38:25 -07002131 udc->gadget.dev.driver = NULL;
2132 udc->driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David Brownelle6a6e472006-12-10 11:47:04 -08002134 if (udc->dc_clk != NULL)
2135 omap_udc_enable_clock(0);
Felipe Balbi1bf0cf62013-01-24 10:52:52 +02002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return status;
2138}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140/*-------------------------------------------------------------------------*/
2141
2142#ifdef CONFIG_USB_GADGET_DEBUG_FILES
2143
2144#include <linux/seq_file.h>
2145
2146static const char proc_filename[] = "driver/udc";
2147
2148#define FOURBITS "%s%s%s%s"
Felipe Balbi80dd1352012-05-29 14:26:09 +03002149#define EIGHTBITS "%s%s%s%s%s%s%s%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2152{
2153 u16 stat_flg;
2154 struct omap_req *req;
2155 char buf[20];
2156
2157 use_ep(ep, 0);
2158
2159 if (use_dma && ep->has_dma)
2160 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2161 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2162 ep->dma_channel - 1, ep->lch);
2163 else
2164 buf[0] = 0;
2165
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002166 stat_flg = omap_readw(UDC_STAT_FLG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 seq_printf(s,
2168 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2169 ep->name, buf,
2170 ep->double_buf ? "dbuf " : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002171 ({ char *s;
2172 switch (ep->ackwait) {
2173 case 0:
2174 s = "";
2175 break;
2176 case 1:
2177 s = "(ackw) ";
2178 break;
2179 case 2:
2180 s = "(ackw2) ";
2181 break;
2182 default:
2183 s = "(?) ";
2184 break;
2185 } s; }),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 ep->irqs, stat_flg,
2187 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2188 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2189 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2190 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2191 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2192 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2193 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2194 (stat_flg & UDC_STALL) ? "STALL " : "",
2195 (stat_flg & UDC_NAK) ? "NAK " : "",
2196 (stat_flg & UDC_ACK) ? "ACK " : "",
2197 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2198 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2199 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2200
Felipe Balbi80dd1352012-05-29 14:26:09 +03002201 if (list_empty(&ep->queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 seq_printf(s, "\t(queue empty)\n");
2203 else
Felipe Balbi80dd1352012-05-29 14:26:09 +03002204 list_for_each_entry(req, &ep->queue, queue) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 unsigned length = req->req.actual;
2206
2207 if (use_dma && buf[0]) {
2208 length += ((ep->bEndpointAddress & USB_DIR_IN)
2209 ? dma_src_len : dma_dest_len)
2210 (ep, req->req.dma + length);
2211 buf[0] = 0;
2212 }
2213 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2214 &req->req, length,
2215 req->req.length, req->req.buf);
2216 }
2217}
2218
2219static char *trx_mode(unsigned m, int enabled)
2220{
2221 switch (m) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002222 case 0:
2223 return enabled ? "*6wire" : "unused";
2224 case 1:
2225 return "4wire";
2226 case 2:
2227 return "3wire";
2228 case 3:
2229 return "6wire";
2230 default:
2231 return "unknown";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233}
2234
2235static int proc_otg_show(struct seq_file *s)
2236{
2237 u32 tmp;
Paul Walmsley4814ced2010-10-08 11:40:20 -06002238 u32 trans = 0;
2239 char *ctrl_name = "(UNKNOWN)";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Dmitry Baryshkove12cc342008-08-07 16:29:25 +04002241 tmp = omap_readl(OTG_REV);
Tony Lindgrenae372572012-05-21 12:01:11 -07002242 ctrl_name = "tranceiver_ctrl";
2243 trans = omap_readw(USB_TRANSCEIVER_CTRL);
David Brownelle6a6e472006-12-10 11:47:04 -08002244 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2245 tmp >> 4, tmp & 0xf, ctrl_name, trans);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002246 tmp = omap_readw(OTG_SYSCON_1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2248 FOURBITS "\n", tmp,
2249 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2250 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
David Brownell65111082005-04-28 13:52:31 -07002251 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 ? "internal"
2253 : trx_mode(USB0_TRX_MODE(tmp), 1),
2254 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2255 (tmp & HST_IDLE_EN) ? " !host" : "",
2256 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2257 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002258 tmp = omap_readl(OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2260 " b_ase_brst=%d hmc=%d\n", tmp,
2261 (tmp & OTG_EN) ? " otg_en" : "",
2262 (tmp & USBX_SYNCHRO) ? " synchro" : "",
Felipe Balbi80dd1352012-05-29 14:26:09 +03002263 /* much more SRP stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 (tmp & SRP_DATA) ? " srp_data" : "",
2265 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2266 (tmp & OTG_PADEN) ? " otg_paden" : "",
2267 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2268 (tmp & UHOST_EN) ? " uhost_en" : "",
2269 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2270 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2271 B_ASE_BRST(tmp),
2272 OTG_HMC(tmp));
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002273 tmp = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2275 (tmp & OTG_ASESSVLD) ? " asess" : "",
2276 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2277 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2278 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2279 (tmp & OTG_ID) ? " id" : "",
2280 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2281 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2282 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2283 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2284 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2285 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2286 (tmp & OTG_PULLDOWN) ? " down" : "",
2287 (tmp & OTG_PULLUP) ? " up" : "",
2288 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2289 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2290 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2291 (tmp & OTG_PU_ID) ? " pu_id" : ""
2292 );
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002293 tmp = omap_readw(OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002295 tmp = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002297 tmp = omap_readw(OTG_OUTCTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002299 tmp = omap_readw(OTG_TEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 seq_printf(s, "otg_test %04x" "\n", tmp);
David Brownell313980c2005-04-11 15:38:25 -07002301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304static int proc_udc_show(struct seq_file *s, void *_)
2305{
2306 u32 tmp;
2307 struct omap_ep *ep;
2308 unsigned long flags;
2309
2310 spin_lock_irqsave(&udc->lock, flags);
2311
2312 seq_printf(s, "%s, version: " DRIVER_VERSION
2313#ifdef USE_ISO
2314 " (iso)"
2315#endif
2316 "%s\n",
2317 driver_desc,
2318 use_dma ? " (dma)" : "");
2319
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002320 tmp = omap_readw(UDC_REV) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 seq_printf(s,
2322 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2323 "hmc %d, transceiver %s\n",
2324 tmp >> 4, tmp & 0xf,
2325 fifo_mode,
2326 udc->driver ? udc->driver->driver.name : "(none)",
2327 HMC,
David Brownelle6a6e472006-12-10 11:47:04 -08002328 udc->transceiver
2329 ? udc->transceiver->label
Tony Lindgrenae372572012-05-21 12:01:11 -07002330 : (cpu_is_omap1710()
David Brownelle6a6e472006-12-10 11:47:04 -08002331 ? "external" : "(none)"));
Tony Lindgrenae372572012-05-21 12:01:11 -07002332 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2333 omap_readw(ULPD_CLOCK_CTRL),
2334 omap_readw(ULPD_SOFT_REQ),
2335 omap_readw(ULPD_STATUS_REQ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 /* OTG controller registers */
2338 if (!cpu_is_omap15xx())
2339 proc_otg_show(s);
2340
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002341 tmp = omap_readw(UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2343 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2344 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2345 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2346 (tmp & UDC_NAK_EN) ? " nak" : "",
2347 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2348 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2349 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2350 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
Felipe Balbi80dd1352012-05-29 14:26:09 +03002351 /* syscon2 is write-only */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /* UDC controller registers */
2354 if (!(tmp & UDC_PULLUP_EN)) {
2355 seq_printf(s, "(suspended)\n");
2356 spin_unlock_irqrestore(&udc->lock, flags);
2357 return 0;
2358 }
2359
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002360 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2362 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2363 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2364 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2365 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2366 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2367 (tmp & UDC_SUS) ? " SUS" : "",
2368 (tmp & UDC_CFG) ? " CFG" : "",
2369 (tmp & UDC_ADD) ? " ADD" : "",
2370 (tmp & UDC_DEF) ? " DEF" : "",
2371 (tmp & UDC_ATT) ? " ATT" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002372 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2373 tmp = omap_readw(UDC_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2375 (tmp & UDC_SOF_IE) ? " sof" : "",
2376 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2377 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2378 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2379 (tmp & UDC_EP0_IE) ? " ep0" : "");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002380 tmp = omap_readw(UDC_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2382 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2383 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2384 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002385 (tmp & UDC_IRQ_SOF) ? " sof" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2387 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2388 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2389 (tmp & UDC_SETUP) ? " setup" : "",
2390 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2391 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2392 if (use_dma) {
2393 unsigned i;
2394
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002395 tmp = omap_readw(UDC_DMA_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2397 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2398 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2399 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2400
2401 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2402 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2403 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2404
2405 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2406 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2407 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2408
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002409 tmp = omap_readw(UDC_RXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2411 if (tmp) {
2412 for (i = 0; i < 3; i++) {
2413 if ((tmp & (0x0f << (i * 4))) == 0)
2414 continue;
2415 seq_printf(s, "rxdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002416 omap_readw(UDC_RXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
2418 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002419 tmp = omap_readw(UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 seq_printf(s, "txdma_cfg %04x\n", tmp);
2421 if (tmp) {
2422 for (i = 0; i < 3; i++) {
2423 if (!(tmp & (0x0f << (i * 4))))
2424 continue;
2425 seq_printf(s, "txdma[%d] %04x\n", i,
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002426 omap_readw(UDC_TXDMA(i + 1)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428 }
2429 }
2430
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002431 tmp = omap_readw(UDC_DEVSTAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (tmp & UDC_ATT) {
2433 proc_ep_show(s, &udc->ep[0]);
2434 if (tmp & UDC_ADD) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002435 list_for_each_entry(ep, &udc->gadget.ep_list,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 ep.ep_list) {
Ido Shayevitzf8bdae02012-03-12 20:25:35 +02002437 if (ep->ep.desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 proc_ep_show(s, ep);
2439 }
2440 }
2441 }
2442 spin_unlock_irqrestore(&udc->lock, flags);
2443 return 0;
2444}
2445
2446static int proc_udc_open(struct inode *inode, struct file *file)
2447{
David Brownell313980c2005-04-11 15:38:25 -07002448 return single_open(file, proc_udc_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -03002451static const struct file_operations proc_ops = {
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002452 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 .open = proc_udc_open,
2454 .read = seq_read,
2455 .llseek = seq_lseek,
2456 .release = single_release,
2457};
2458
2459static void create_proc_file(void)
2460{
Denis V. Lunevcdefa182008-04-29 01:02:19 -07002461 proc_create(proc_filename, 0, NULL, &proc_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
2464static void remove_proc_file(void)
2465{
David Brownell313980c2005-04-11 15:38:25 -07002466 remove_proc_entry(proc_filename, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
2469#else
2470
2471static inline void create_proc_file(void) {}
2472static inline void remove_proc_file(void) {}
2473
2474#endif
2475
2476/*-------------------------------------------------------------------------*/
2477
2478/* Before this controller can enumerate, we need to pick an endpoint
2479 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2480 * buffer space among the endpoints we'll be operating.
David Brownell65111082005-04-28 13:52:31 -07002481 *
2482 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002483 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
David Brownell65111082005-04-28 13:52:31 -07002484 * capability yet though.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002486static unsigned
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487omap_ep_setup(char *name, u8 addr, u8 type,
2488 unsigned buf, unsigned maxp, int dbuf)
2489{
2490 struct omap_ep *ep;
2491 u16 epn_rxtx = 0;
2492
2493 /* OUT endpoints first, then IN */
2494 ep = &udc->ep[addr & 0xf];
2495 if (addr & USB_DIR_IN)
2496 ep += 16;
2497
2498 /* in case of ep init table bugs */
2499 BUG_ON(ep->name[0]);
2500
2501 /* chip setup ... bit values are same for IN, OUT */
2502 if (type == USB_ENDPOINT_XFER_ISOC) {
2503 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002504 case 8:
2505 epn_rxtx = 0 << 12;
2506 break;
2507 case 16:
2508 epn_rxtx = 1 << 12;
2509 break;
2510 case 32:
2511 epn_rxtx = 2 << 12;
2512 break;
2513 case 64:
2514 epn_rxtx = 3 << 12;
2515 break;
2516 case 128:
2517 epn_rxtx = 4 << 12;
2518 break;
2519 case 256:
2520 epn_rxtx = 5 << 12;
2521 break;
2522 case 512:
2523 epn_rxtx = 6 << 12;
2524 break;
2525 default:
2526 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528 epn_rxtx |= UDC_EPN_RX_ISO;
2529 dbuf = 1;
2530 } else {
2531 /* double-buffering "not supported" on 15xx,
David Brownelle6a6e472006-12-10 11:47:04 -08002532 * and ignored for PIO-IN on newer chips
2533 * (for more reliable behavior)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 */
Tony Lindgrenae372572012-05-21 12:01:11 -07002535 if (!use_dma || cpu_is_omap15xx())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 dbuf = 0;
2537
2538 switch (maxp) {
Felipe Balbi80dd1352012-05-29 14:26:09 +03002539 case 8:
2540 epn_rxtx = 0 << 12;
2541 break;
2542 case 16:
2543 epn_rxtx = 1 << 12;
2544 break;
2545 case 32:
2546 epn_rxtx = 2 << 12;
2547 break;
2548 case 64:
2549 epn_rxtx = 3 << 12;
2550 break;
2551 default:
2552 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554 if (dbuf && addr)
2555 epn_rxtx |= UDC_EPN_RX_DB;
2556 init_timer(&ep->timer);
2557 ep->timer.function = pio_out_timer;
2558 ep->timer.data = (unsigned long) ep;
2559 }
2560 if (addr)
2561 epn_rxtx |= UDC_EPN_RX_VALID;
2562 BUG_ON(buf & 0x07);
2563 epn_rxtx |= buf >> 3;
2564
2565 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2566 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2567
2568 if (addr & USB_DIR_IN)
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002569 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 else
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002571 omap_writew(epn_rxtx, UDC_EP_RX(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 /* next endpoint's buffer starts after this one's */
2574 buf += maxp;
2575 if (dbuf)
2576 buf += maxp;
2577 BUG_ON(buf > 2048);
2578
2579 /* set up driver data structures */
2580 BUG_ON(strlen(name) >= sizeof ep->name);
2581 strlcpy(ep->name, name, sizeof ep->name);
2582 INIT_LIST_HEAD(&ep->queue);
2583 INIT_LIST_HEAD(&ep->iso);
2584 ep->bEndpointAddress = addr;
2585 ep->bmAttributes = type;
2586 ep->double_buf = dbuf;
David Brownelle6a6e472006-12-10 11:47:04 -08002587 ep->udc = udc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 ep->ep.name = ep->name;
2590 ep->ep.ops = &omap_ep_ops;
2591 ep->ep.maxpacket = ep->maxpacket = maxp;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002592 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 return buf;
2595}
2596
2597static void omap_udc_release(struct device *dev)
2598{
2599 complete(udc->done);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002600 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002601 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002604static int
Heikki Krogerus86753812012-02-13 13:24:02 +02002605omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 unsigned tmp, buf;
2608
2609 /* abolish any previous hardware state */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002610 omap_writew(0, UDC_SYSCON1);
2611 omap_writew(0, UDC_IRQ_EN);
2612 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2613 omap_writew(0, UDC_DMA_IRQ_EN);
2614 omap_writew(0, UDC_RXDMA_CFG);
2615 omap_writew(0, UDC_TXDMA_CFG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 /* UDC_PULLUP_EN gates the chip clock */
Felipe Balbi80dd1352012-05-29 14:26:09 +03002618 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Christoph Lametere94b1762006-12-06 20:33:17 -08002620 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (!udc)
2622 return -ENOMEM;
2623
Felipe Balbi80dd1352012-05-29 14:26:09 +03002624 spin_lock_init(&udc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 udc->gadget.ops = &omap_gadget_ops;
2627 udc->gadget.ep0 = &udc->ep[0].ep;
2628 INIT_LIST_HEAD(&udc->gadget.ep_list);
2629 INIT_LIST_HEAD(&udc->iso);
2630 udc->gadget.speed = USB_SPEED_UNKNOWN;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002631 udc->gadget.max_speed = USB_SPEED_FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 udc->gadget.name = driver_name;
2633
2634 device_initialize(&udc->gadget.dev);
Kay Sievers0031a062008-05-02 06:02:41 +02002635 dev_set_name(&udc->gadget.dev, "gadget");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 udc->gadget.dev.release = omap_udc_release;
2637 udc->gadget.dev.parent = &odev->dev;
2638 if (use_dma)
2639 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2640
2641 udc->transceiver = xceiv;
2642
2643 /* ep0 is special; put it right after the SETUP buffer */
2644 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2645 8 /* after SETUP */, 64 /* maxpacket */, 0);
2646 list_del_init(&udc->ep[0].ep.ep_list);
2647
2648 /* initially disable all non-ep0 endpoints */
2649 for (tmp = 1; tmp < 15; tmp++) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002650 omap_writew(0, UDC_EP_RX(tmp));
2651 omap_writew(0, UDC_EP_TX(tmp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653
Felipe Balbi80dd1352012-05-29 14:26:09 +03002654#define OMAP_BULK_EP(name, addr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 buf = omap_ep_setup(name "-bulk", addr, \
2656 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002657#define OMAP_INT_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 buf = omap_ep_setup(name "-int", addr, \
2659 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002660#define OMAP_ISO_EP(name, addr, maxp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 buf = omap_ep_setup(name "-iso", addr, \
2662 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2663
2664 switch (fifo_mode) {
2665 case 0:
2666 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2667 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2668 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2669 break;
2670 case 1:
2671 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2672 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
David Brownell313980c2005-04-11 15:38:25 -07002673 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2676 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
David Brownell313980c2005-04-11 15:38:25 -07002677 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2680 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
David Brownell313980c2005-04-11 15:38:25 -07002681 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2684 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
David Brownell313980c2005-04-11 15:38:25 -07002685 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2688 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
David Brownell313980c2005-04-11 15:38:25 -07002689 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2690 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2693 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
David Brownell313980c2005-04-11 15:38:25 -07002694 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2695 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
David Brownell313980c2005-04-11 15:38:25 -07002697 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2698 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 break;
2701
2702#ifdef USE_ISO
2703 case 2: /* mixed iso/bulk */
2704 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2705 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2706 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2707 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2708
2709 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2710
2711 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2712 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2713 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2714 break;
2715 case 3: /* mixed bulk/iso */
2716 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2717 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2718 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2719
2720 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2721 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2722 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2723
2724 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2725 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2726 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2727 break;
2728#endif
2729
2730 /* add more modes as needed */
2731
2732 default:
2733 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2734 return -ENODEV;
2735 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002736 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2738 return 0;
2739}
2740
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002741static int omap_udc_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 int status = -ENODEV;
2744 int hmc;
Heikki Krogerus86753812012-02-13 13:24:02 +02002745 struct usb_phy *xceiv = NULL;
David Brownell313980c2005-04-11 15:38:25 -07002746 const char *type = NULL;
Russell King3ae5eae2005-11-09 22:32:44 +00002747 struct omap_usb_config *config = pdev->dev.platform_data;
Tony Lindgrenae372572012-05-21 12:01:11 -07002748 struct clk *dc_clk = NULL;
2749 struct clk *hhc_clk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Felipe Balbi5b6d84b2012-05-29 14:27:52 +03002751 if (cpu_is_omap7xx())
2752 use_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /* NOTE: "knows" the order of the resources! */
David Brownelle6a6e472006-12-10 11:47:04 -08002755 if (!request_mem_region(pdev->resource[0].start,
Russell King3ae5eae2005-11-09 22:32:44 +00002756 pdev->resource[0].end - pdev->resource[0].start + 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 driver_name)) {
2758 DBG("request_mem_region failed\n");
2759 return -EBUSY;
2760 }
2761
David Brownelle6a6e472006-12-10 11:47:04 -08002762 if (cpu_is_omap16xx()) {
2763 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2764 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2765 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2766 /* can't use omap_udc_enable_clock yet */
2767 clk_enable(dc_clk);
2768 clk_enable(hhc_clk);
2769 udelay(100);
2770 }
2771
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002772 if (cpu_is_omap7xx()) {
2773 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2774 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2775 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2776 /* can't use omap_udc_enable_clock yet */
2777 clk_enable(dc_clk);
2778 clk_enable(hhc_clk);
2779 udelay(100);
2780 }
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 INFO("OMAP UDC rev %d.%d%s\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002783 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 config->otg ? ", Mini-AB" : "");
2785
2786 /* use the mode given to us by board init code */
2787 if (cpu_is_omap15xx()) {
2788 hmc = HMC_1510;
2789 type = "(unknown)";
2790
David Brownell8a3c1f52007-03-21 12:26:32 -07002791 if (machine_without_vbus_sense()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /* just set up software VBUS detect, and then
2793 * later rig it so we always report VBUS.
2794 * FIXME without really sensing VBUS, we can't
2795 * know when to turn PULLUP_EN on/off; and that
2796 * means we always "need" the 48MHz clock.
2797 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002798 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2799 tmp &= ~VBUS_CTRL_1510;
2800 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 tmp |= VBUS_MODE_1510;
2802 tmp &= ~VBUS_CTRL_1510;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002803 omap_writel(tmp, FUNC_MUX_CTRL_0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805 } else {
David Brownell65111082005-04-28 13:52:31 -07002806 /* The transceiver may package some GPIO logic or handle
2807 * loopback and/or transceiverless setup; if we find one,
2808 * use it. Except for OTG, we don't _need_ to talk to one;
2809 * but not having one probably means no VBUS detection.
2810 */
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05302811 xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302812 if (!IS_ERR_OR_NULL(xceiv))
David Brownell65111082005-04-28 13:52:31 -07002813 type = xceiv->label;
2814 else if (config->otg) {
2815 DBG("OTG requires external transceiver!\n");
2816 goto cleanup0;
2817 }
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 hmc = HMC_1610;
David Brownelle6a6e472006-12-10 11:47:04 -08002820
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 switch (hmc) {
David Brownell313980c2005-04-11 15:38:25 -07002822 case 0: /* POWERUP DEFAULT == 0 */
2823 case 4:
2824 case 12:
2825 case 20:
2826 if (!cpu_is_omap1710()) {
2827 type = "integrated";
2828 break;
2829 }
2830 /* FALL THROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 case 3:
2832 case 11:
2833 case 16:
2834 case 19:
2835 case 25:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302836 if (IS_ERR_OR_NULL(xceiv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 DBG("external transceiver not registered!\n");
David Brownell313980c2005-04-11 15:38:25 -07002838 type = "unknown";
David Brownell65111082005-04-28 13:52:31 -07002839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 case 21: /* internal loopback */
David Brownell313980c2005-04-11 15:38:25 -07002842 type = "loopback";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 break;
2844 case 14: /* transceiverless */
David Brownell65111082005-04-28 13:52:31 -07002845 if (cpu_is_omap1710())
2846 goto bad_on_1710;
2847 /* FALL THROUGH */
2848 case 13:
2849 case 15:
David Brownell313980c2005-04-11 15:38:25 -07002850 type = "no";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 break;
2852
2853 default:
David Brownell65111082005-04-28 13:52:31 -07002854bad_on_1710:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 ERR("unrecognized UDC HMC mode %d\n", hmc);
David Brownell65111082005-04-28 13:52:31 -07002856 goto cleanup0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
2858 }
Tony Lindgrenae372572012-05-21 12:01:11 -07002859
David Brownell313980c2005-04-11 15:38:25 -07002860 INFO("hmc mode %d, %s transceiver\n", hmc, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 /* a "gadget" abstracts/virtualizes the controller */
Russell King3ae5eae2005-11-09 22:32:44 +00002863 status = omap_udc_setup(pdev, xceiv);
Felipe Balbi80dd1352012-05-29 14:26:09 +03002864 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 goto cleanup0;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002866
David Brownell313980c2005-04-11 15:38:25 -07002867 xceiv = NULL;
Felipe Balbi80dd1352012-05-29 14:26:09 +03002868 /* "udc" is now valid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 pullup_disable(udc);
2870#if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2871 udc->gadget.is_otg = (config->otg != 0);
2872#endif
2873
David Brownell65111082005-04-28 13:52:31 -07002874 /* starting with omap1710 es2.0, clear toggle is a separate bit */
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002875 if (omap_readw(UDC_REV) >= 0x61)
David Brownell65111082005-04-28 13:52:31 -07002876 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2877 else
2878 udc->clr_halt = UDC_RESET_EP;
2879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 /* USB general purpose IRQ: ep0, state changes, dma, etc */
Russell King3ae5eae2005-11-09 22:32:44 +00002881 status = request_irq(pdev->resource[1].start, omap_udc_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002882 0, driver_name, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002884 ERR("can't get irq %d, err %d\n",
2885 (int) pdev->resource[1].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 goto cleanup1;
2887 }
2888
2889 /* USB "non-iso" IRQ (PIO for all but ep0) */
Russell King3ae5eae2005-11-09 22:32:44 +00002890 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
Felipe Balbi80dd1352012-05-29 14:26:09 +03002891 0, "omap_udc pio", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002893 ERR("can't get irq %d, err %d\n",
2894 (int) pdev->resource[2].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 goto cleanup2;
2896 }
2897#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002898 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +08002899 0, "omap_udc iso", udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (status != 0) {
David Brownelle6a6e472006-12-10 11:47:04 -08002901 ERR("can't get irq %d, err %d\n",
2902 (int) pdev->resource[3].start, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 goto cleanup3;
2904 }
2905#endif
Cory Maccarrone45f780a2009-11-22 10:10:52 -08002906 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002907 udc->dc_clk = dc_clk;
2908 udc->hhc_clk = hhc_clk;
2909 clk_disable(hhc_clk);
2910 clk_disable(dc_clk);
2911 }
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 create_proc_file();
David Brownelle6a6e472006-12-10 11:47:04 -08002914 status = device_add(&udc->gadget.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002915 if (status)
2916 goto cleanup4;
2917
2918 status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
David Brownelle6a6e472006-12-10 11:47:04 -08002919 if (!status)
2920 return status;
2921 /* If fail, fall through */
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002922cleanup4:
2923 remove_proc_file();
2924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925#ifdef USE_ISO
2926cleanup3:
Russell King3ae5eae2005-11-09 22:32:44 +00002927 free_irq(pdev->resource[2].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928#endif
2929
2930cleanup2:
Russell King3ae5eae2005-11-09 22:32:44 +00002931 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933cleanup1:
Felipe Balbi80dd1352012-05-29 14:26:09 +03002934 kfree(udc);
David Brownell313980c2005-04-11 15:38:25 -07002935 udc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937cleanup0:
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302938 if (!IS_ERR_OR_NULL(xceiv))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05302939 usb_put_phy(xceiv);
David Brownelle6a6e472006-12-10 11:47:04 -08002940
Tony Lindgrenae372572012-05-21 12:01:11 -07002941 if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
David Brownelle6a6e472006-12-10 11:47:04 -08002942 clk_disable(hhc_clk);
2943 clk_disable(dc_clk);
2944 clk_put(hhc_clk);
2945 clk_put(dc_clk);
2946 }
2947
Russell King3ae5eae2005-11-09 22:32:44 +00002948 release_mem_region(pdev->resource[0].start,
2949 pdev->resource[0].end - pdev->resource[0].start + 1);
David Brownelle6a6e472006-12-10 11:47:04 -08002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return status;
2952}
2953
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05002954static int omap_udc_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -07002956 DECLARE_COMPLETION_ONSTACK(done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 if (!udc)
2959 return -ENODEV;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002960
2961 usb_del_gadget_udc(&udc->gadget);
David Brownell6bea4762006-12-05 03:15:33 -08002962 if (udc->driver)
2963 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 udc->done = &done;
2966
2967 pullup_disable(udc);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +05302968 if (!IS_ERR_OR_NULL(udc->transceiver)) {
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +05302969 usb_put_phy(udc->transceiver);
David Brownell313980c2005-04-11 15:38:25 -07002970 udc->transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03002972 omap_writew(0, UDC_SYSCON1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 remove_proc_file();
2975
2976#ifdef USE_ISO
Russell King3ae5eae2005-11-09 22:32:44 +00002977 free_irq(pdev->resource[3].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978#endif
Russell King3ae5eae2005-11-09 22:32:44 +00002979 free_irq(pdev->resource[2].start, udc);
2980 free_irq(pdev->resource[1].start, udc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
David Brownelle6a6e472006-12-10 11:47:04 -08002982 if (udc->dc_clk) {
2983 if (udc->clk_requested)
2984 omap_udc_enable_clock(0);
2985 clk_put(udc->hhc_clk);
2986 clk_put(udc->dc_clk);
2987 }
2988
Russell King3ae5eae2005-11-09 22:32:44 +00002989 release_mem_region(pdev->resource[0].start,
2990 pdev->resource[0].end - pdev->resource[0].start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 device_unregister(&udc->gadget.dev);
2993 wait_for_completion(&done);
2994
2995 return 0;
2996}
2997
David Brownell313980c2005-04-11 15:38:25 -07002998/* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2999 * system is forced into deep sleep
3000 *
3001 * REVISIT we should probably reject suspend requests when there's a host
3002 * session active, rather than disconnecting, at least on boards that can
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003003 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
David Brownell313980c2005-04-11 15:38:25 -07003004 * make host resumes and VBUS detection trigger OMAP wakeup events; that
3005 * may involve talking to an external transceiver (e.g. isp1301).
3006 */
david-b@pacbell.net1d7beee2005-06-29 07:00:56 -07003007
Russell King3ae5eae2005-11-09 22:32:44 +00003008static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
David Brownell313980c2005-04-11 15:38:25 -07003010 u32 devstat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Tony Lindgrenf35ae632008-07-03 12:24:43 +03003012 devstat = omap_readw(UDC_DEVSTAT);
David Brownell313980c2005-04-11 15:38:25 -07003013
3014 /* we're requesting 48 MHz clock if the pullup is enabled
3015 * (== we're attached to the host) and we're not suspended,
3016 * which would prevent entry to deep sleep...
3017 */
3018 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
Arjan van de Venb6c63932008-07-25 01:45:52 -07003019 WARNING("session active; suspend requires disconnect\n");
David Brownell313980c2005-04-11 15:38:25 -07003020 omap_pullup(&udc->gadget, 0);
3021 }
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return 0;
3024}
3025
Russell King3ae5eae2005-11-09 22:32:44 +00003026static int omap_udc_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 DBG("resume + wakeup/SRP\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 omap_pullup(&udc->gadget, 1);
3030
3031 /* maybe the host would enumerate us if we nudged it */
3032 msleep(100);
3033 return omap_wakeup(&udc->gadget);
3034}
3035
3036/*-------------------------------------------------------------------------*/
3037
Russell King3ae5eae2005-11-09 22:32:44 +00003038static struct platform_driver udc_driver = {
Felipe Balbidc1737c2012-05-29 14:33:30 +03003039 .probe = omap_udc_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05003040 .remove = omap_udc_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 .suspend = omap_udc_suspend,
3042 .resume = omap_udc_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00003043 .driver = {
3044 .owner = THIS_MODULE,
3045 .name = (char *) driver_name,
3046 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047};
3048
Felipe Balbidc1737c2012-05-29 14:33:30 +03003049module_platform_driver(udc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051MODULE_DESCRIPTION(DRIVER_DESC);
3052MODULE_LICENSE("GPL");
Kay Sieversf34c32f2008-04-10 21:29:21 -07003053MODULE_ALIAS("platform:omap_udc");