]> Pileus Git - ~andy/linux/blob - drivers/usb/gadget/omap_udc.c
Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[~andy/linux] / drivers / usb / gadget / omap_udc.c
1 /*
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  *
7  * OMAP2 & DMA support by Kyungmin Park <kyungmin.park@samsung.com>
8  *
9  * 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.
13  */
14
15 #undef  DEBUG
16 #undef  VERBOSE
17
18 #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>
24 #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>
32 #include <linux/platform_device.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
35 #include <linux/usb/otg.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/clk.h>
38 #include <linux/prefetch.h>
39
40 #include <asm/byteorder.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/system.h>
44 #include <asm/unaligned.h>
45 #include <asm/mach-types.h>
46
47 #include <plat/dma.h>
48 #include <plat/usb.h>
49
50 #include "omap_udc.h"
51
52 #undef  USB_TRACE
53
54 /* bulk DMA seems to be behaving for both IN and OUT */
55 #define USE_DMA
56
57 /* ISO too */
58 #define USE_ISO
59
60 #define DRIVER_DESC     "OMAP UDC driver"
61 #define DRIVER_VERSION  "4 October 2004"
62
63 #define DMA_ADDR_INVALID        (~(dma_addr_t)0)
64
65 #define OMAP2_DMA_CH(ch)        (((ch) - 1) << 1)
66 #define OMAP24XX_DMA(name, ch)  (OMAP24XX_DMA_##name + OMAP2_DMA_CH(ch))
67
68 /*
69  * The OMAP UDC needs _very_ early endpoint setup:  before enabling the
70  * D+ pullup to allow enumeration.  That's too early for the gadget
71  * framework to use from usb_endpoint_enable(), which happens after
72  * enumeration as part of activating an interface.  (But if we add an
73  * optional new "UDC not yet running" state to the gadget driver model,
74  * even just during driver binding, the endpoint autoconfig logic is the
75  * natural spot to manufacture new endpoints.)
76  *
77  * So instead of using endpoint enable calls to control the hardware setup,
78  * this driver defines a "fifo mode" parameter.  It's used during driver
79  * initialization to choose among a set of pre-defined endpoint configs.
80  * See omap_udc_setup() for available modes, or to add others.  That code
81  * lives in an init section, so use this driver as a module if you need
82  * to change the fifo mode after the kernel boots.
83  *
84  * Gadget drivers normally ignore endpoints they don't care about, and
85  * won't include them in configuration descriptors.  That means only
86  * misbehaving hosts would even notice they exist.
87  */
88 #ifdef  USE_ISO
89 static unsigned fifo_mode = 3;
90 #else
91 static unsigned fifo_mode = 0;
92 #endif
93
94 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
95  * boot parameter "omap_udc:fifo_mode=42"
96  */
97 module_param (fifo_mode, uint, 0);
98 MODULE_PARM_DESC (fifo_mode, "endpoint configuration");
99
100 #ifdef  USE_DMA
101 static bool use_dma = 1;
102
103 /* "modprobe omap_udc use_dma=y", or else as a kernel
104  * boot parameter "omap_udc:use_dma=y"
105  */
106 module_param (use_dma, bool, 0);
107 MODULE_PARM_DESC (use_dma, "enable/disable DMA");
108 #else   /* !USE_DMA */
109
110 /* save a bit of code */
111 #define use_dma         0
112 #endif  /* !USE_DMA */
113
114
115 static const char driver_name [] = "omap_udc";
116 static const char driver_desc [] = DRIVER_DESC;
117
118 /*-------------------------------------------------------------------------*/
119
120 /* there's a notion of "current endpoint" for modifying endpoint
121  * state, and PIO access to its FIFO.
122  */
123
124 static void use_ep(struct omap_ep *ep, u16 select)
125 {
126         u16     num = ep->bEndpointAddress & 0x0f;
127
128         if (ep->bEndpointAddress & USB_DIR_IN)
129                 num |= UDC_EP_DIR;
130         omap_writew(num | select, UDC_EP_NUM);
131         /* when select, MUST deselect later !! */
132 }
133
134 static inline void deselect_ep(void)
135 {
136         u16 w;
137
138         w = omap_readw(UDC_EP_NUM);
139         w &= ~UDC_EP_SEL;
140         omap_writew(w, UDC_EP_NUM);
141         /* 6 wait states before TX will happen */
142 }
143
144 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
145
146 /*-------------------------------------------------------------------------*/
147
148 static int omap_ep_enable(struct usb_ep *_ep,
149                 const struct usb_endpoint_descriptor *desc)
150 {
151         struct omap_ep  *ep = container_of(_ep, struct omap_ep, ep);
152         struct omap_udc *udc;
153         unsigned long   flags;
154         u16             maxp;
155
156         /* catch various bogus parameters */
157         if (!_ep || !desc || ep->desc
158                         || desc->bDescriptorType != USB_DT_ENDPOINT
159                         || ep->bEndpointAddress != desc->bEndpointAddress
160                         || ep->maxpacket < usb_endpoint_maxp(desc)) {
161                 DBG("%s, bad ep or descriptor\n", __func__);
162                 return -EINVAL;
163         }
164         maxp = usb_endpoint_maxp(desc);
165         if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
166                                 && maxp != ep->maxpacket)
167                         || usb_endpoint_maxp(desc) > ep->maxpacket
168                         || !desc->wMaxPacketSize) {
169                 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
170                 return -ERANGE;
171         }
172
173 #ifdef  USE_ISO
174         if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
175                                 && desc->bInterval != 1)) {
176                 /* hardware wants period = 1; USB allows 2^(Interval-1) */
177                 DBG("%s, unsupported ISO period %dms\n", _ep->name,
178                                 1 << (desc->bInterval - 1));
179                 return -EDOM;
180         }
181 #else
182         if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
183                 DBG("%s, ISO nyet\n", _ep->name);
184                 return -EDOM;
185         }
186 #endif
187
188         /* xfer types must match, except that interrupt ~= bulk */
189         if (ep->bmAttributes != desc->bmAttributes
190                         && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
191                         && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
192                 DBG("%s, %s type mismatch\n", __func__, _ep->name);
193                 return -EINVAL;
194         }
195
196         udc = ep->udc;
197         if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
198                 DBG("%s, bogus device state\n", __func__);
199                 return -ESHUTDOWN;
200         }
201
202         spin_lock_irqsave(&udc->lock, flags);
203
204         ep->desc = desc;
205         ep->irqs = 0;
206         ep->stopped = 0;
207         ep->ep.maxpacket = maxp;
208
209         /* set endpoint to initial state */
210         ep->dma_channel = 0;
211         ep->has_dma = 0;
212         ep->lch = -1;
213         use_ep(ep, UDC_EP_SEL);
214         omap_writew(udc->clr_halt, UDC_CTRL);
215         ep->ackwait = 0;
216         deselect_ep();
217
218         if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
219                 list_add(&ep->iso, &udc->iso);
220
221         /* maybe assign a DMA channel to this endpoint */
222         if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
223                 /* FIXME ISO can dma, but prefers first channel */
224                 dma_channel_claim(ep, 0);
225
226         /* PIO OUT may RX packets */
227         if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
228                         && !ep->has_dma
229                         && !(ep->bEndpointAddress & USB_DIR_IN)) {
230                 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
231                 ep->ackwait = 1 + ep->double_buf;
232         }
233
234         spin_unlock_irqrestore(&udc->lock, flags);
235         VDBG("%s enabled\n", _ep->name);
236         return 0;
237 }
238
239 static void nuke(struct omap_ep *, int status);
240
241 static int omap_ep_disable(struct usb_ep *_ep)
242 {
243         struct omap_ep  *ep = container_of(_ep, struct omap_ep, ep);
244         unsigned long   flags;
245
246         if (!_ep || !ep->desc) {
247                 DBG("%s, %s not enabled\n", __func__,
248                         _ep ? ep->ep.name : NULL);
249                 return -EINVAL;
250         }
251
252         spin_lock_irqsave(&ep->udc->lock, flags);
253         ep->desc = NULL;
254         ep->ep.desc = NULL;
255         nuke (ep, -ESHUTDOWN);
256         ep->ep.maxpacket = ep->maxpacket;
257         ep->has_dma = 0;
258         omap_writew(UDC_SET_HALT, UDC_CTRL);
259         list_del_init(&ep->iso);
260         del_timer(&ep->timer);
261
262         spin_unlock_irqrestore(&ep->udc->lock, flags);
263
264         VDBG("%s disabled\n", _ep->name);
265         return 0;
266 }
267
268 /*-------------------------------------------------------------------------*/
269
270 static struct usb_request *
271 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
272 {
273         struct omap_req *req;
274
275         req = kzalloc(sizeof(*req), gfp_flags);
276         if (req) {
277                 req->req.dma = DMA_ADDR_INVALID;
278                 INIT_LIST_HEAD (&req->queue);
279         }
280         return &req->req;
281 }
282
283 static void
284 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
285 {
286         struct omap_req *req = container_of(_req, struct omap_req, req);
287
288         if (_req)
289                 kfree (req);
290 }
291
292 /*-------------------------------------------------------------------------*/
293
294 static void
295 done(struct omap_ep *ep, struct omap_req *req, int status)
296 {
297         unsigned                stopped = ep->stopped;
298
299         list_del_init(&req->queue);
300
301         if (req->req.status == -EINPROGRESS)
302                 req->req.status = status;
303         else
304                 status = req->req.status;
305
306         if (use_dma && ep->has_dma) {
307                 if (req->mapped) {
308                         dma_unmap_single(ep->udc->gadget.dev.parent,
309                                 req->req.dma, req->req.length,
310                                 (ep->bEndpointAddress & USB_DIR_IN)
311                                         ? DMA_TO_DEVICE
312                                         : DMA_FROM_DEVICE);
313                         req->req.dma = DMA_ADDR_INVALID;
314                         req->mapped = 0;
315                 } else
316                         dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
317                                 req->req.dma, req->req.length,
318                                 (ep->bEndpointAddress & USB_DIR_IN)
319                                         ? DMA_TO_DEVICE
320                                         : DMA_FROM_DEVICE);
321         }
322
323 #ifndef USB_TRACE
324         if (status && status != -ESHUTDOWN)
325 #endif
326                 VDBG("complete %s req %p stat %d len %u/%u\n",
327                         ep->ep.name, &req->req, status,
328                         req->req.actual, req->req.length);
329
330         /* don't modify queue heads during completion callback */
331         ep->stopped = 1;
332         spin_unlock(&ep->udc->lock);
333         req->req.complete(&ep->ep, &req->req);
334         spin_lock(&ep->udc->lock);
335         ep->stopped = stopped;
336 }
337
338 /*-------------------------------------------------------------------------*/
339
340 #define UDC_FIFO_FULL           (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
341 #define UDC_FIFO_UNWRITABLE     (UDC_EP_HALTED | UDC_FIFO_FULL)
342
343 #define FIFO_EMPTY      (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
344 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
345
346 static inline int
347 write_packet(u8 *buf, struct omap_req *req, unsigned max)
348 {
349         unsigned        len;
350         u16             *wp;
351
352         len = min(req->req.length - req->req.actual, max);
353         req->req.actual += len;
354
355         max = len;
356         if (likely((((int)buf) & 1) == 0)) {
357                 wp = (u16 *)buf;
358                 while (max >= 2) {
359                         omap_writew(*wp++, UDC_DATA);
360                         max -= 2;
361                 }
362                 buf = (u8 *)wp;
363         }
364         while (max--)
365                 omap_writeb(*buf++, UDC_DATA);
366         return len;
367 }
368
369 // FIXME change r/w fifo calling convention
370
371
372 // return:  0 = still running, 1 = completed, negative = errno
373 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
374 {
375         u8              *buf;
376         unsigned        count;
377         int             is_last;
378         u16             ep_stat;
379
380         buf = req->req.buf + req->req.actual;
381         prefetch(buf);
382
383         /* PIO-IN isn't double buffered except for iso */
384         ep_stat = omap_readw(UDC_STAT_FLG);
385         if (ep_stat & UDC_FIFO_UNWRITABLE)
386                 return 0;
387
388         count = ep->ep.maxpacket;
389         count = write_packet(buf, req, count);
390         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
391         ep->ackwait = 1;
392
393         /* last packet is often short (sometimes a zlp) */
394         if (count != ep->ep.maxpacket)
395                 is_last = 1;
396         else if (req->req.length == req->req.actual
397                         && !req->req.zero)
398                 is_last = 1;
399         else
400                 is_last = 0;
401
402         /* NOTE:  requests complete when all IN data is in a
403          * FIFO (or sometimes later, if a zlp was needed).
404          * Use usb_ep_fifo_status() where needed.
405          */
406         if (is_last)
407                 done(ep, req, 0);
408         return is_last;
409 }
410
411 static inline int
412 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
413 {
414         unsigned        len;
415         u16             *wp;
416
417         len = min(req->req.length - req->req.actual, avail);
418         req->req.actual += len;
419         avail = len;
420
421         if (likely((((int)buf) & 1) == 0)) {
422                 wp = (u16 *)buf;
423                 while (avail >= 2) {
424                         *wp++ = omap_readw(UDC_DATA);
425                         avail -= 2;
426                 }
427                 buf = (u8 *)wp;
428         }
429         while (avail--)
430                 *buf++ = omap_readb(UDC_DATA);
431         return len;
432 }
433
434 // return:  0 = still running, 1 = queue empty, negative = errno
435 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
436 {
437         u8              *buf;
438         unsigned        count, avail;
439         int             is_last;
440
441         buf = req->req.buf + req->req.actual;
442         prefetchw(buf);
443
444         for (;;) {
445                 u16     ep_stat = omap_readw(UDC_STAT_FLG);
446
447                 is_last = 0;
448                 if (ep_stat & FIFO_EMPTY) {
449                         if (!ep->double_buf)
450                                 break;
451                         ep->fnf = 1;
452                 }
453                 if (ep_stat & UDC_EP_HALTED)
454                         break;
455
456                 if (ep_stat & UDC_FIFO_FULL)
457                         avail = ep->ep.maxpacket;
458                 else  {
459                         avail = omap_readw(UDC_RXFSTAT);
460                         ep->fnf = ep->double_buf;
461                 }
462                 count = read_packet(buf, req, avail);
463
464                 /* partial packet reads may not be errors */
465                 if (count < ep->ep.maxpacket) {
466                         is_last = 1;
467                         /* overflowed this request?  flush extra data */
468                         if (count != avail) {
469                                 req->req.status = -EOVERFLOW;
470                                 avail -= count;
471                                 while (avail--)
472                                         omap_readw(UDC_DATA);
473                         }
474                 } else if (req->req.length == req->req.actual)
475                         is_last = 1;
476                 else
477                         is_last = 0;
478
479                 if (!ep->bEndpointAddress)
480                         break;
481                 if (is_last)
482                         done(ep, req, 0);
483                 break;
484         }
485         return is_last;
486 }
487
488 /*-------------------------------------------------------------------------*/
489
490 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
491 {
492         dma_addr_t      end;
493
494         /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
495          * the last transfer's bytecount by more than a FIFO's worth.
496          */
497         if (cpu_is_omap15xx())
498                 return 0;
499
500         end = omap_get_dma_src_pos(ep->lch);
501         if (end == ep->dma_counter)
502                 return 0;
503
504         end |= start & (0xffff << 16);
505         if (end < start)
506                 end += 0x10000;
507         return end - start;
508 }
509
510 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
511 {
512         dma_addr_t      end;
513
514         end = omap_get_dma_dst_pos(ep->lch);
515         if (end == ep->dma_counter)
516                 return 0;
517
518         end |= start & (0xffff << 16);
519         if (cpu_is_omap15xx())
520                 end++;
521         if (end < start)
522                 end += 0x10000;
523         return end - start;
524 }
525
526
527 /* Each USB transfer request using DMA maps to one or more DMA transfers.
528  * When DMA completion isn't request completion, the UDC continues with
529  * the next DMA transfer for that USB transfer.
530  */
531
532 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
533 {
534         u16             txdma_ctrl, w;
535         unsigned        length = req->req.length - req->req.actual;
536         const int       sync_mode = cpu_is_omap15xx()
537                                 ? OMAP_DMA_SYNC_FRAME
538                                 : OMAP_DMA_SYNC_ELEMENT;
539         int             dma_trigger = 0;
540
541         if (cpu_is_omap24xx())
542                 dma_trigger = OMAP24XX_DMA(USB_W2FC_TX0, ep->dma_channel);
543
544         /* measure length in either bytes or packets */
545         if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
546                         || (cpu_is_omap24xx() && length < ep->maxpacket)
547                         || (cpu_is_omap15xx() && length < ep->maxpacket)) {
548                 txdma_ctrl = UDC_TXN_EOT | length;
549                 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
550                                 length, 1, sync_mode, dma_trigger, 0);
551         } else {
552                 length = min(length / ep->maxpacket,
553                                 (unsigned) UDC_TXN_TSC + 1);
554                 txdma_ctrl = length;
555                 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
556                                 ep->ep.maxpacket >> 1, length, sync_mode,
557                                 dma_trigger, 0);
558                 length *= ep->maxpacket;
559         }
560         omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
561                 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
562                 0, 0);
563
564         omap_start_dma(ep->lch);
565         ep->dma_counter = omap_get_dma_src_pos(ep->lch);
566         w = omap_readw(UDC_DMA_IRQ_EN);
567         w |= UDC_TX_DONE_IE(ep->dma_channel);
568         omap_writew(w, UDC_DMA_IRQ_EN);
569         omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
570         req->dma_bytes = length;
571 }
572
573 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
574 {
575         u16 w;
576
577         if (status == 0) {
578                 req->req.actual += req->dma_bytes;
579
580                 /* return if this request needs to send data or zlp */
581                 if (req->req.actual < req->req.length)
582                         return;
583                 if (req->req.zero
584                                 && req->dma_bytes != 0
585                                 && (req->req.actual % ep->maxpacket) == 0)
586                         return;
587         } else
588                 req->req.actual += dma_src_len(ep, req->req.dma
589                                                         + req->req.actual);
590
591         /* tx completion */
592         omap_stop_dma(ep->lch);
593         w = omap_readw(UDC_DMA_IRQ_EN);
594         w &= ~UDC_TX_DONE_IE(ep->dma_channel);
595         omap_writew(w, UDC_DMA_IRQ_EN);
596         done(ep, req, status);
597 }
598
599 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
600 {
601         unsigned packets = req->req.length - req->req.actual;
602         int dma_trigger = 0;
603         u16 w;
604
605         if (cpu_is_omap24xx())
606                 dma_trigger = OMAP24XX_DMA(USB_W2FC_RX0, ep->dma_channel);
607
608         /* NOTE:  we filtered out "short reads" before, so we know
609          * the buffer has only whole numbers of packets.
610          * except MODE SELECT(6) sent the 24 bytes data in OMAP24XX DMA mode
611          */
612         if (cpu_is_omap24xx() && packets < ep->maxpacket) {
613                 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
614                                 packets, 1, OMAP_DMA_SYNC_ELEMENT,
615                                 dma_trigger, 0);
616                 req->dma_bytes = packets;
617         } else {
618                 /* set up this DMA transfer, enable the fifo, start */
619                 packets /= ep->ep.maxpacket;
620                 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
621                 req->dma_bytes = packets * ep->ep.maxpacket;
622                 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
623                                 ep->ep.maxpacket >> 1, packets,
624                                 OMAP_DMA_SYNC_ELEMENT,
625                                 dma_trigger, 0);
626         }
627         omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
628                 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
629                 0, 0);
630         ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
631
632         omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
633         w = omap_readw(UDC_DMA_IRQ_EN);
634         w |= UDC_RX_EOT_IE(ep->dma_channel);
635         omap_writew(w, UDC_DMA_IRQ_EN);
636         omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
637         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
638
639         omap_start_dma(ep->lch);
640 }
641
642 static void
643 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
644 {
645         u16     count, w;
646
647         if (status == 0)
648                 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
649         count = dma_dest_len(ep, req->req.dma + req->req.actual);
650         count += req->req.actual;
651         if (one)
652                 count--;
653         if (count <= req->req.length)
654                 req->req.actual = count;
655
656         if (count != req->dma_bytes || status)
657                 omap_stop_dma(ep->lch);
658
659         /* if this wasn't short, request may need another transfer */
660         else if (req->req.actual < req->req.length)
661                 return;
662
663         /* rx completion */
664         w = omap_readw(UDC_DMA_IRQ_EN);
665         w &= ~UDC_RX_EOT_IE(ep->dma_channel);
666         omap_writew(w, UDC_DMA_IRQ_EN);
667         done(ep, req, status);
668 }
669
670 static void dma_irq(struct omap_udc *udc, u16 irq_src)
671 {
672         u16             dman_stat = omap_readw(UDC_DMAN_STAT);
673         struct omap_ep  *ep;
674         struct omap_req *req;
675
676         /* IN dma: tx to host */
677         if (irq_src & UDC_TXN_DONE) {
678                 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
679                 ep->irqs++;
680                 /* can see TXN_DONE after dma abort */
681                 if (!list_empty(&ep->queue)) {
682                         req = container_of(ep->queue.next,
683                                                 struct omap_req, queue);
684                         finish_in_dma(ep, req, 0);
685                 }
686                 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
687
688                 if (!list_empty (&ep->queue)) {
689                         req = container_of(ep->queue.next,
690                                         struct omap_req, queue);
691                         next_in_dma(ep, req);
692                 }
693         }
694
695         /* OUT dma: rx from host */
696         if (irq_src & UDC_RXN_EOT) {
697                 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
698                 ep->irqs++;
699                 /* can see RXN_EOT after dma abort */
700                 if (!list_empty(&ep->queue)) {
701                         req = container_of(ep->queue.next,
702                                         struct omap_req, queue);
703                         finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
704                 }
705                 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
706
707                 if (!list_empty (&ep->queue)) {
708                         req = container_of(ep->queue.next,
709                                         struct omap_req, queue);
710                         next_out_dma(ep, req);
711                 }
712         }
713
714         if (irq_src & UDC_RXN_CNT) {
715                 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
716                 ep->irqs++;
717                 /* omap15xx does this unasked... */
718                 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
719                 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
720         }
721 }
722
723 static void dma_error(int lch, u16 ch_status, void *data)
724 {
725         struct omap_ep  *ep = data;
726
727         /* if ch_status & OMAP_DMA_DROP_IRQ ... */
728         /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
729         ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
730
731         /* complete current transfer ... */
732 }
733
734 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
735 {
736         u16     reg;
737         int     status, restart, is_in;
738         int     dma_channel;
739
740         is_in = ep->bEndpointAddress & USB_DIR_IN;
741         if (is_in)
742                 reg = omap_readw(UDC_TXDMA_CFG);
743         else
744                 reg = omap_readw(UDC_RXDMA_CFG);
745         reg |= UDC_DMA_REQ;             /* "pulse" activated */
746
747         ep->dma_channel = 0;
748         ep->lch = -1;
749         if (channel == 0 || channel > 3) {
750                 if ((reg & 0x0f00) == 0)
751                         channel = 3;
752                 else if ((reg & 0x00f0) == 0)
753                         channel = 2;
754                 else if ((reg & 0x000f) == 0)   /* preferred for ISO */
755                         channel = 1;
756                 else {
757                         status = -EMLINK;
758                         goto just_restart;
759                 }
760         }
761         reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
762         ep->dma_channel = channel;
763
764         if (is_in) {
765                 if (cpu_is_omap24xx())
766                         dma_channel = OMAP24XX_DMA(USB_W2FC_TX0, channel);
767                 else
768                         dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
769                 status = omap_request_dma(dma_channel,
770                         ep->ep.name, dma_error, ep, &ep->lch);
771                 if (status == 0) {
772                         omap_writew(reg, UDC_TXDMA_CFG);
773                         /* EMIFF or SDRC */
774                         omap_set_dma_src_burst_mode(ep->lch,
775                                                 OMAP_DMA_DATA_BURST_4);
776                         omap_set_dma_src_data_pack(ep->lch, 1);
777                         /* TIPB */
778                         omap_set_dma_dest_params(ep->lch,
779                                 OMAP_DMA_PORT_TIPB,
780                                 OMAP_DMA_AMODE_CONSTANT,
781                                 UDC_DATA_DMA,
782                                 0, 0);
783                 }
784         } else {
785                 if (cpu_is_omap24xx())
786                         dma_channel = OMAP24XX_DMA(USB_W2FC_RX0, channel);
787                 else
788                         dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
789
790                 status = omap_request_dma(dma_channel,
791                         ep->ep.name, dma_error, ep, &ep->lch);
792                 if (status == 0) {
793                         omap_writew(reg, UDC_RXDMA_CFG);
794                         /* TIPB */
795                         omap_set_dma_src_params(ep->lch,
796                                 OMAP_DMA_PORT_TIPB,
797                                 OMAP_DMA_AMODE_CONSTANT,
798                                 UDC_DATA_DMA,
799                                 0, 0);
800                         /* EMIFF or SDRC */
801                         omap_set_dma_dest_burst_mode(ep->lch,
802                                                 OMAP_DMA_DATA_BURST_4);
803                         omap_set_dma_dest_data_pack(ep->lch, 1);
804                 }
805         }
806         if (status)
807                 ep->dma_channel = 0;
808         else {
809                 ep->has_dma = 1;
810                 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
811
812                 /* channel type P: hw synch (fifo) */
813                 if (cpu_class_is_omap1() && !cpu_is_omap15xx())
814                         omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
815         }
816
817 just_restart:
818         /* restart any queue, even if the claim failed  */
819         restart = !ep->stopped && !list_empty(&ep->queue);
820
821         if (status)
822                 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
823                         restart ? " (restart)" : "");
824         else
825                 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
826                         is_in ? 't' : 'r',
827                         ep->dma_channel - 1, ep->lch,
828                         restart ? " (restart)" : "");
829
830         if (restart) {
831                 struct omap_req *req;
832                 req = container_of(ep->queue.next, struct omap_req, queue);
833                 if (ep->has_dma)
834                         (is_in ? next_in_dma : next_out_dma)(ep, req);
835                 else {
836                         use_ep(ep, UDC_EP_SEL);
837                         (is_in ? write_fifo : read_fifo)(ep, req);
838                         deselect_ep();
839                         if (!is_in) {
840                                 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
841                                 ep->ackwait = 1 + ep->double_buf;
842                         }
843                         /* IN: 6 wait states before it'll tx */
844                 }
845         }
846 }
847
848 static void dma_channel_release(struct omap_ep *ep)
849 {
850         int             shift = 4 * (ep->dma_channel - 1);
851         u16             mask = 0x0f << shift;
852         struct omap_req *req;
853         int             active;
854
855         /* abort any active usb transfer request */
856         if (!list_empty(&ep->queue))
857                 req = container_of(ep->queue.next, struct omap_req, queue);
858         else
859                 req = NULL;
860
861         active = omap_get_dma_active_status(ep->lch);
862
863         DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
864                         active ? "active" : "idle",
865                         (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
866                         ep->dma_channel - 1, req);
867
868         /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
869          * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
870          */
871
872         /* wait till current packet DMA finishes, and fifo empties */
873         if (ep->bEndpointAddress & USB_DIR_IN) {
874                 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
875                                         UDC_TXDMA_CFG);
876
877                 if (req) {
878                         finish_in_dma(ep, req, -ECONNRESET);
879
880                         /* clear FIFO; hosts probably won't empty it */
881                         use_ep(ep, UDC_EP_SEL);
882                         omap_writew(UDC_CLR_EP, UDC_CTRL);
883                         deselect_ep();
884                 }
885                 while (omap_readw(UDC_TXDMA_CFG) & mask)
886                         udelay(10);
887         } else {
888                 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
889                                         UDC_RXDMA_CFG);
890
891                 /* dma empties the fifo */
892                 while (omap_readw(UDC_RXDMA_CFG) & mask)
893                         udelay(10);
894                 if (req)
895                         finish_out_dma(ep, req, -ECONNRESET, 0);
896         }
897         omap_free_dma(ep->lch);
898         ep->dma_channel = 0;
899         ep->lch = -1;
900         /* has_dma still set, till endpoint is fully quiesced */
901 }
902
903
904 /*-------------------------------------------------------------------------*/
905
906 static int
907 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
908 {
909         struct omap_ep  *ep = container_of(_ep, struct omap_ep, ep);
910         struct omap_req *req = container_of(_req, struct omap_req, req);
911         struct omap_udc *udc;
912         unsigned long   flags;
913         int             is_iso = 0;
914
915         /* catch various bogus parameters */
916         if (!_req || !req->req.complete || !req->req.buf
917                         || !list_empty(&req->queue)) {
918                 DBG("%s, bad params\n", __func__);
919                 return -EINVAL;
920         }
921         if (!_ep || (!ep->desc && ep->bEndpointAddress)) {
922                 DBG("%s, bad ep\n", __func__);
923                 return -EINVAL;
924         }
925         if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
926                 if (req->req.length > ep->ep.maxpacket)
927                         return -EMSGSIZE;
928                 is_iso = 1;
929         }
930
931         /* this isn't bogus, but OMAP DMA isn't the only hardware to
932          * have a hard time with partial packet reads...  reject it.
933          * Except OMAP2 can handle the small packets.
934          */
935         if (use_dma
936                         && ep->has_dma
937                         && ep->bEndpointAddress != 0
938                         && (ep->bEndpointAddress & USB_DIR_IN) == 0
939                         && !cpu_class_is_omap2()
940                         && (req->req.length % ep->ep.maxpacket) != 0) {
941                 DBG("%s, no partial packet OUT reads\n", __func__);
942                 return -EMSGSIZE;
943         }
944
945         udc = ep->udc;
946         if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
947                 return -ESHUTDOWN;
948
949         if (use_dma && ep->has_dma) {
950                 if (req->req.dma == DMA_ADDR_INVALID) {
951                         req->req.dma = dma_map_single(
952                                 ep->udc->gadget.dev.parent,
953                                 req->req.buf,
954                                 req->req.length,
955                                 (ep->bEndpointAddress & USB_DIR_IN)
956                                         ? DMA_TO_DEVICE
957                                         : DMA_FROM_DEVICE);
958                         req->mapped = 1;
959                 } else {
960                         dma_sync_single_for_device(
961                                 ep->udc->gadget.dev.parent,
962                                 req->req.dma, req->req.length,
963                                 (ep->bEndpointAddress & USB_DIR_IN)
964                                         ? DMA_TO_DEVICE
965                                         : DMA_FROM_DEVICE);
966                         req->mapped = 0;
967                 }
968         }
969
970         VDBG("%s queue req %p, len %d buf %p\n",
971                 ep->ep.name, _req, _req->length, _req->buf);
972
973         spin_lock_irqsave(&udc->lock, flags);
974
975         req->req.status = -EINPROGRESS;
976         req->req.actual = 0;
977
978         /* maybe kickstart non-iso i/o queues */
979         if (is_iso) {
980                 u16 w;
981
982                 w = omap_readw(UDC_IRQ_EN);
983                 w |= UDC_SOF_IE;
984                 omap_writew(w, UDC_IRQ_EN);
985         } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
986                 int     is_in;
987
988                 if (ep->bEndpointAddress == 0) {
989                         if (!udc->ep0_pending || !list_empty (&ep->queue)) {
990                                 spin_unlock_irqrestore(&udc->lock, flags);
991                                 return -EL2HLT;
992                         }
993
994                         /* empty DATA stage? */
995                         is_in = udc->ep0_in;
996                         if (!req->req.length) {
997
998                                 /* chip became CONFIGURED or ADDRESSED
999                                  * earlier; drivers may already have queued
1000                                  * requests to non-control endpoints
1001                                  */
1002                                 if (udc->ep0_set_config) {
1003                                         u16     irq_en = omap_readw(UDC_IRQ_EN);
1004
1005                                         irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
1006                                         if (!udc->ep0_reset_config)
1007                                                 irq_en |= UDC_EPN_RX_IE
1008                                                         | UDC_EPN_TX_IE;
1009                                         omap_writew(irq_en, UDC_IRQ_EN);
1010                                 }
1011
1012                                 /* STATUS for zero length DATA stages is
1013                                  * always an IN ... even for IN transfers,
1014                                  * a weird case which seem to stall OMAP.
1015                                  */
1016                                 omap_writew(UDC_EP_SEL | UDC_EP_DIR, UDC_EP_NUM);
1017                                 omap_writew(UDC_CLR_EP, UDC_CTRL);
1018                                 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1019                                 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1020
1021                                 /* cleanup */
1022                                 udc->ep0_pending = 0;
1023                                 done(ep, req, 0);
1024                                 req = NULL;
1025
1026                         /* non-empty DATA stage */
1027                         } else if (is_in) {
1028                                 omap_writew(UDC_EP_SEL | UDC_EP_DIR, UDC_EP_NUM);
1029                         } else {
1030                                 if (udc->ep0_setup)
1031                                         goto irq_wait;
1032                                 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1033                         }
1034                 } else {
1035                         is_in = ep->bEndpointAddress & USB_DIR_IN;
1036                         if (!ep->has_dma)
1037                                 use_ep(ep, UDC_EP_SEL);
1038                         /* if ISO: SOF IRQs must be enabled/disabled! */
1039                 }
1040
1041                 if (ep->has_dma)
1042                         (is_in ? next_in_dma : next_out_dma)(ep, req);
1043                 else if (req) {
1044                         if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
1045                                 req = NULL;
1046                         deselect_ep();
1047                         if (!is_in) {
1048                                 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1049                                 ep->ackwait = 1 + ep->double_buf;
1050                         }
1051                         /* IN: 6 wait states before it'll tx */
1052                 }
1053         }
1054
1055 irq_wait:
1056         /* irq handler advances the queue */
1057         if (req != NULL)
1058                 list_add_tail(&req->queue, &ep->queue);
1059         spin_unlock_irqrestore(&udc->lock, flags);
1060
1061         return 0;
1062 }
1063
1064 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1065 {
1066         struct omap_ep  *ep = container_of(_ep, struct omap_ep, ep);
1067         struct omap_req *req;
1068         unsigned long   flags;
1069
1070         if (!_ep || !_req)
1071                 return -EINVAL;
1072
1073         spin_lock_irqsave(&ep->udc->lock, flags);
1074
1075         /* make sure it's actually queued on this endpoint */
1076         list_for_each_entry (req, &ep->queue, queue) {
1077                 if (&req->req == _req)
1078                         break;
1079         }
1080         if (&req->req != _req) {
1081                 spin_unlock_irqrestore(&ep->udc->lock, flags);
1082                 return -EINVAL;
1083         }
1084
1085         if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1086                 int channel = ep->dma_channel;
1087
1088                 /* releasing the channel cancels the request,
1089                  * reclaiming the channel restarts the queue
1090                  */
1091                 dma_channel_release(ep);
1092                 dma_channel_claim(ep, channel);
1093         } else
1094                 done(ep, req, -ECONNRESET);
1095         spin_unlock_irqrestore(&ep->udc->lock, flags);
1096         return 0;
1097 }
1098
1099 /*-------------------------------------------------------------------------*/
1100
1101 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1102 {
1103         struct omap_ep  *ep = container_of(_ep, struct omap_ep, ep);
1104         unsigned long   flags;
1105         int             status = -EOPNOTSUPP;
1106
1107         spin_lock_irqsave(&ep->udc->lock, flags);
1108
1109         /* just use protocol stalls for ep0; real halts are annoying */
1110         if (ep->bEndpointAddress == 0) {
1111                 if (!ep->udc->ep0_pending)
1112                         status = -EINVAL;
1113                 else if (value) {
1114                         if (ep->udc->ep0_set_config) {
1115                                 WARNING("error changing config?\n");
1116                                 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1117                         }
1118                         omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1119                         ep->udc->ep0_pending = 0;
1120                         status = 0;
1121                 } else /* NOP */
1122                         status = 0;
1123
1124         /* otherwise, all active non-ISO endpoints can halt */
1125         } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->desc) {
1126
1127                 /* IN endpoints must already be idle */
1128                 if ((ep->bEndpointAddress & USB_DIR_IN)
1129                                 && !list_empty(&ep->queue)) {
1130                         status = -EAGAIN;
1131                         goto done;
1132                 }
1133
1134                 if (value) {
1135                         int     channel;
1136
1137                         if (use_dma && ep->dma_channel
1138                                         && !list_empty(&ep->queue)) {
1139                                 channel = ep->dma_channel;
1140                                 dma_channel_release(ep);
1141                         } else
1142                                 channel = 0;
1143
1144                         use_ep(ep, UDC_EP_SEL);
1145                         if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1146                                 omap_writew(UDC_SET_HALT, UDC_CTRL);
1147                                 status = 0;
1148                         } else
1149                                 status = -EAGAIN;
1150                         deselect_ep();
1151
1152                         if (channel)
1153                                 dma_channel_claim(ep, channel);
1154                 } else {
1155                         use_ep(ep, 0);
1156                         omap_writew(ep->udc->clr_halt, UDC_CTRL);
1157                         ep->ackwait = 0;
1158                         if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1159                                 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1160                                 ep->ackwait = 1 + ep->double_buf;
1161                         }
1162                 }
1163         }
1164 done:
1165         VDBG("%s %s halt stat %d\n", ep->ep.name,
1166                 value ? "set" : "clear", status);
1167
1168         spin_unlock_irqrestore(&ep->udc->lock, flags);
1169         return status;
1170 }
1171
1172 static struct usb_ep_ops omap_ep_ops = {
1173         .enable         = omap_ep_enable,
1174         .disable        = omap_ep_disable,
1175
1176         .alloc_request  = omap_alloc_request,
1177         .free_request   = omap_free_request,
1178
1179         .queue          = omap_ep_queue,
1180         .dequeue        = omap_ep_dequeue,
1181
1182         .set_halt       = omap_ep_set_halt,
1183         // fifo_status ... report bytes in fifo
1184         // fifo_flush ... flush fifo
1185 };
1186
1187 /*-------------------------------------------------------------------------*/
1188
1189 static int omap_get_frame(struct usb_gadget *gadget)
1190 {
1191         u16     sof = omap_readw(UDC_SOF);
1192         return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1193 }
1194
1195 static int omap_wakeup(struct usb_gadget *gadget)
1196 {
1197         struct omap_udc *udc;
1198         unsigned long   flags;
1199         int             retval = -EHOSTUNREACH;
1200
1201         udc = container_of(gadget, struct omap_udc, gadget);
1202
1203         spin_lock_irqsave(&udc->lock, flags);
1204         if (udc->devstat & UDC_SUS) {
1205                 /* NOTE:  OTG spec erratum says that OTG devices may
1206                  * issue wakeups without host enable.
1207                  */
1208                 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1209                         DBG("remote wakeup...\n");
1210                         omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
1211                         retval = 0;
1212                 }
1213
1214         /* NOTE:  non-OTG systems may use SRP TOO... */
1215         } else if (!(udc->devstat & UDC_ATT)) {
1216                 if (udc->transceiver)
1217                         retval = otg_start_srp(udc->transceiver->otg);
1218         }
1219         spin_unlock_irqrestore(&udc->lock, flags);
1220
1221         return retval;
1222 }
1223
1224 static int
1225 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1226 {
1227         struct omap_udc *udc;
1228         unsigned long   flags;
1229         u16             syscon1;
1230
1231         udc = container_of(gadget, struct omap_udc, gadget);
1232         spin_lock_irqsave(&udc->lock, flags);
1233         syscon1 = omap_readw(UDC_SYSCON1);
1234         if (is_selfpowered)
1235                 syscon1 |= UDC_SELF_PWR;
1236         else
1237                 syscon1 &= ~UDC_SELF_PWR;
1238         omap_writew(syscon1, UDC_SYSCON1);
1239         spin_unlock_irqrestore(&udc->lock, flags);
1240
1241         return 0;
1242 }
1243
1244 static int can_pullup(struct omap_udc *udc)
1245 {
1246         return udc->driver && udc->softconnect && udc->vbus_active;
1247 }
1248
1249 static void pullup_enable(struct omap_udc *udc)
1250 {
1251         u16 w;
1252
1253         w = omap_readw(UDC_SYSCON1);
1254         w |= UDC_PULLUP_EN;
1255         omap_writew(w, UDC_SYSCON1);
1256         if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1257                 u32 l;
1258
1259                 l = omap_readl(OTG_CTRL);
1260                 l |= OTG_BSESSVLD;
1261                 omap_writel(l, OTG_CTRL);
1262         }
1263         omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1264 }
1265
1266 static void pullup_disable(struct omap_udc *udc)
1267 {
1268         u16 w;
1269
1270         if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1271                 u32 l;
1272
1273                 l = omap_readl(OTG_CTRL);
1274                 l &= ~OTG_BSESSVLD;
1275                 omap_writel(l, OTG_CTRL);
1276         }
1277         omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1278         w = omap_readw(UDC_SYSCON1);
1279         w &= ~UDC_PULLUP_EN;
1280         omap_writew(w, UDC_SYSCON1);
1281 }
1282
1283 static struct omap_udc *udc;
1284
1285 static void omap_udc_enable_clock(int enable)
1286 {
1287         if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1288                 return;
1289
1290         if (enable) {
1291                 clk_enable(udc->dc_clk);
1292                 clk_enable(udc->hhc_clk);
1293                 udelay(100);
1294         } else {
1295                 clk_disable(udc->hhc_clk);
1296                 clk_disable(udc->dc_clk);
1297         }
1298 }
1299
1300 /*
1301  * Called by whatever detects VBUS sessions:  external transceiver
1302  * driver, or maybe GPIO0 VBUS IRQ.  May request 48 MHz clock.
1303  */
1304 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1305 {
1306         struct omap_udc *udc;
1307         unsigned long   flags;
1308         u32 l;
1309
1310         udc = container_of(gadget, struct omap_udc, gadget);
1311         spin_lock_irqsave(&udc->lock, flags);
1312         VDBG("VBUS %s\n", is_active ? "on" : "off");
1313         udc->vbus_active = (is_active != 0);
1314         if (cpu_is_omap15xx()) {
1315                 /* "software" detect, ignored if !VBUS_MODE_1510 */
1316                 l = omap_readl(FUNC_MUX_CTRL_0);
1317                 if (is_active)
1318                         l |= VBUS_CTRL_1510;
1319                 else
1320                         l &= ~VBUS_CTRL_1510;
1321                 omap_writel(l, FUNC_MUX_CTRL_0);
1322         }
1323         if (udc->dc_clk != NULL && is_active) {
1324                 if (!udc->clk_requested) {
1325                         omap_udc_enable_clock(1);
1326                         udc->clk_requested = 1;
1327                 }
1328         }
1329         if (can_pullup(udc))
1330                 pullup_enable(udc);
1331         else
1332                 pullup_disable(udc);
1333         if (udc->dc_clk != NULL && !is_active) {
1334                 if (udc->clk_requested) {
1335                         omap_udc_enable_clock(0);
1336                         udc->clk_requested = 0;
1337                 }
1338         }
1339         spin_unlock_irqrestore(&udc->lock, flags);
1340         return 0;
1341 }
1342
1343 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1344 {
1345         struct omap_udc *udc;
1346
1347         udc = container_of(gadget, struct omap_udc, gadget);
1348         if (udc->transceiver)
1349                 return usb_phy_set_power(udc->transceiver, mA);
1350         return -EOPNOTSUPP;
1351 }
1352
1353 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1354 {
1355         struct omap_udc *udc;
1356         unsigned long   flags;
1357
1358         udc = container_of(gadget, struct omap_udc, gadget);
1359         spin_lock_irqsave(&udc->lock, flags);
1360         udc->softconnect = (is_on != 0);
1361         if (can_pullup(udc))
1362                 pullup_enable(udc);
1363         else
1364                 pullup_disable(udc);
1365         spin_unlock_irqrestore(&udc->lock, flags);
1366         return 0;
1367 }
1368
1369 static int omap_udc_start(struct usb_gadget_driver *driver,
1370                 int (*bind)(struct usb_gadget *));
1371 static int omap_udc_stop(struct usb_gadget_driver *driver);
1372
1373 static struct usb_gadget_ops omap_gadget_ops = {
1374         .get_frame              = omap_get_frame,
1375         .wakeup                 = omap_wakeup,
1376         .set_selfpowered        = omap_set_selfpowered,
1377         .vbus_session           = omap_vbus_session,
1378         .vbus_draw              = omap_vbus_draw,
1379         .pullup                 = omap_pullup,
1380         .start                  = omap_udc_start,
1381         .stop                   = omap_udc_stop,
1382 };
1383
1384 /*-------------------------------------------------------------------------*/
1385
1386 /* dequeue ALL requests; caller holds udc->lock */
1387 static void nuke(struct omap_ep *ep, int status)
1388 {
1389         struct omap_req *req;
1390
1391         ep->stopped = 1;
1392
1393         if (use_dma && ep->dma_channel)
1394                 dma_channel_release(ep);
1395
1396         use_ep(ep, 0);
1397         omap_writew(UDC_CLR_EP, UDC_CTRL);
1398         if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1399                 omap_writew(UDC_SET_HALT, UDC_CTRL);
1400
1401         while (!list_empty(&ep->queue)) {
1402                 req = list_entry(ep->queue.next, struct omap_req, queue);
1403                 done(ep, req, status);
1404         }
1405 }
1406
1407 /* caller holds udc->lock */
1408 static void udc_quiesce(struct omap_udc *udc)
1409 {
1410         struct omap_ep  *ep;
1411
1412         udc->gadget.speed = USB_SPEED_UNKNOWN;
1413         nuke(&udc->ep[0], -ESHUTDOWN);
1414         list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
1415                 nuke(ep, -ESHUTDOWN);
1416 }
1417
1418 /*-------------------------------------------------------------------------*/
1419
1420 static void update_otg(struct omap_udc *udc)
1421 {
1422         u16     devstat;
1423
1424         if (!gadget_is_otg(&udc->gadget))
1425                 return;
1426
1427         if (omap_readl(OTG_CTRL) & OTG_ID)
1428                 devstat = omap_readw(UDC_DEVSTAT);
1429         else
1430                 devstat = 0;
1431
1432         udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1433         udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1434         udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1435
1436         /* Enable HNP early, avoiding races on suspend irq path.
1437          * ASSUMES OTG state machine B_BUS_REQ input is true.
1438          */
1439         if (udc->gadget.b_hnp_enable) {
1440                 u32 l;
1441
1442                 l = omap_readl(OTG_CTRL);
1443                 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1444                 l &= ~OTG_PULLUP;
1445                 omap_writel(l, OTG_CTRL);
1446         }
1447 }
1448
1449 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1450 {
1451         struct omap_ep  *ep0 = &udc->ep[0];
1452         struct omap_req *req = NULL;
1453
1454         ep0->irqs++;
1455
1456         /* Clear any pending requests and then scrub any rx/tx state
1457          * before starting to handle the SETUP request.
1458          */
1459         if (irq_src & UDC_SETUP) {
1460                 u16     ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1461
1462                 nuke(ep0, 0);
1463                 if (ack) {
1464                         omap_writew(ack, UDC_IRQ_SRC);
1465                         irq_src = UDC_SETUP;
1466                 }
1467         }
1468
1469         /* IN/OUT packets mean we're in the DATA or STATUS stage.
1470          * This driver uses only uses protocol stalls (ep0 never halts),
1471          * and if we got this far the gadget driver already had a
1472          * chance to stall.  Tries to be forgiving of host oddities.
1473          *
1474          * NOTE:  the last chance gadget drivers have to stall control
1475          * requests is during their request completion callback.
1476          */
1477         if (!list_empty(&ep0->queue))
1478                 req = container_of(ep0->queue.next, struct omap_req, queue);
1479
1480         /* IN == TX to host */
1481         if (irq_src & UDC_EP0_TX) {
1482                 int     stat;
1483
1484                 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1485                 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1486                 stat = omap_readw(UDC_STAT_FLG);
1487                 if (stat & UDC_ACK) {
1488                         if (udc->ep0_in) {
1489                                 /* write next IN packet from response,
1490                                  * or set up the status stage.
1491                                  */
1492                                 if (req)
1493                                         stat = write_fifo(ep0, req);
1494                                 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1495                                 if (!req && udc->ep0_pending) {
1496                                         omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1497                                         omap_writew(UDC_CLR_EP, UDC_CTRL);
1498                                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1499                                         omap_writew(0, UDC_EP_NUM);
1500                                         udc->ep0_pending = 0;
1501                                 } /* else:  6 wait states before it'll tx */
1502                         } else {
1503                                 /* ack status stage of OUT transfer */
1504                                 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1505                                 if (req)
1506                                         done(ep0, req, 0);
1507                         }
1508                         req = NULL;
1509                 } else if (stat & UDC_STALL) {
1510                         omap_writew(UDC_CLR_HALT, UDC_CTRL);
1511                         omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1512                 } else {
1513                         omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1514                 }
1515         }
1516
1517         /* OUT == RX from host */
1518         if (irq_src & UDC_EP0_RX) {
1519                 int     stat;
1520
1521                 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1522                 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1523                 stat = omap_readw(UDC_STAT_FLG);
1524                 if (stat & UDC_ACK) {
1525                         if (!udc->ep0_in) {
1526                                 stat = 0;
1527                                 /* read next OUT packet of request, maybe
1528                                  * reactiviting the fifo; stall on errors.
1529                                  */
1530                                 if (!req || (stat = read_fifo(ep0, req)) < 0) {
1531                                         omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1532                                         udc->ep0_pending = 0;
1533                                         stat = 0;
1534                                 } else if (stat == 0)
1535                                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1536                                 omap_writew(0, UDC_EP_NUM);
1537
1538                                 /* activate status stage */
1539                                 if (stat == 1) {
1540                                         done(ep0, req, 0);
1541                                         /* that may have STALLed ep0... */
1542                                         omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1543                                                         UDC_EP_NUM);
1544                                         omap_writew(UDC_CLR_EP, UDC_CTRL);
1545                                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1546                                         omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1547                                         udc->ep0_pending = 0;
1548                                 }
1549                         } else {
1550                                 /* ack status stage of IN transfer */
1551                                 omap_writew(0, UDC_EP_NUM);
1552                                 if (req)
1553                                         done(ep0, req, 0);
1554                         }
1555                 } else if (stat & UDC_STALL) {
1556                         omap_writew(UDC_CLR_HALT, UDC_CTRL);
1557                         omap_writew(0, UDC_EP_NUM);
1558                 } else {
1559                         omap_writew(0, UDC_EP_NUM);
1560                 }
1561         }
1562
1563         /* SETUP starts all control transfers */
1564         if (irq_src & UDC_SETUP) {
1565                 union u {
1566                         u16                     word[4];
1567                         struct usb_ctrlrequest  r;
1568                 } u;
1569                 int                     status = -EINVAL;
1570                 struct omap_ep          *ep;
1571
1572                 /* read the (latest) SETUP message */
1573                 do {
1574                         omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
1575                         /* two bytes at a time */
1576                         u.word[0] = omap_readw(UDC_DATA);
1577                         u.word[1] = omap_readw(UDC_DATA);
1578                         u.word[2] = omap_readw(UDC_DATA);
1579                         u.word[3] = omap_readw(UDC_DATA);
1580                         omap_writew(0, UDC_EP_NUM);
1581                 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
1582
1583 #define w_value         le16_to_cpu(u.r.wValue)
1584 #define w_index         le16_to_cpu(u.r.wIndex)
1585 #define w_length        le16_to_cpu(u.r.wLength)
1586
1587                 /* Delegate almost all control requests to the gadget driver,
1588                  * except for a handful of ch9 status/feature requests that
1589                  * hardware doesn't autodecode _and_ the gadget API hides.
1590                  */
1591                 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1592                 udc->ep0_set_config = 0;
1593                 udc->ep0_pending = 1;
1594                 ep0->stopped = 0;
1595                 ep0->ackwait = 0;
1596                 switch (u.r.bRequest) {
1597                 case USB_REQ_SET_CONFIGURATION:
1598                         /* udc needs to know when ep != 0 is valid */
1599                         if (u.r.bRequestType != USB_RECIP_DEVICE)
1600                                 goto delegate;
1601                         if (w_length != 0)
1602                                 goto do_stall;
1603                         udc->ep0_set_config = 1;
1604                         udc->ep0_reset_config = (w_value == 0);
1605                         VDBG("set config %d\n", w_value);
1606
1607                         /* update udc NOW since gadget driver may start
1608                          * queueing requests immediately; clear config
1609                          * later if it fails the request.
1610                          */
1611                         if (udc->ep0_reset_config)
1612                                 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1613                         else
1614                                 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
1615                         update_otg(udc);
1616                         goto delegate;
1617                 case USB_REQ_CLEAR_FEATURE:
1618                         /* clear endpoint halt */
1619                         if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1620                                 goto delegate;
1621                         if (w_value != USB_ENDPOINT_HALT
1622                                         || w_length != 0)
1623                                 goto do_stall;
1624                         ep = &udc->ep[w_index & 0xf];
1625                         if (ep != ep0) {
1626                                 if (w_index & USB_DIR_IN)
1627                                         ep += 16;
1628                                 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1629                                                 || !ep->desc)
1630                                         goto do_stall;
1631                                 use_ep(ep, 0);
1632                                 omap_writew(udc->clr_halt, UDC_CTRL);
1633                                 ep->ackwait = 0;
1634                                 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1635                                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1636                                         ep->ackwait = 1 + ep->double_buf;
1637                                 }
1638                                 /* NOTE:  assumes the host behaves sanely,
1639                                  * only clearing real halts.  Else we may
1640                                  * need to kill pending transfers and then
1641                                  * restart the queue... very messy for DMA!
1642                                  */
1643                         }
1644                         VDBG("%s halt cleared by host\n", ep->name);
1645                         goto ep0out_status_stage;
1646                 case USB_REQ_SET_FEATURE:
1647                         /* set endpoint halt */
1648                         if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1649                                 goto delegate;
1650                         if (w_value != USB_ENDPOINT_HALT
1651                                         || w_length != 0)
1652                                 goto do_stall;
1653                         ep = &udc->ep[w_index & 0xf];
1654                         if (w_index & USB_DIR_IN)
1655                                 ep += 16;
1656                         if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1657                                         || ep == ep0 || !ep->desc)
1658                                 goto do_stall;
1659                         if (use_dma && ep->has_dma) {
1660                                 /* this has rude side-effects (aborts) and
1661                                  * can't really work if DMA-IN is active
1662                                  */
1663                                 DBG("%s host set_halt, NYET \n", ep->name);
1664                                 goto do_stall;
1665                         }
1666                         use_ep(ep, 0);
1667                         /* can't halt if fifo isn't empty... */
1668                         omap_writew(UDC_CLR_EP, UDC_CTRL);
1669                         omap_writew(UDC_SET_HALT, UDC_CTRL);
1670                         VDBG("%s halted by host\n", ep->name);
1671 ep0out_status_stage:
1672                         status = 0;
1673                         omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1674                         omap_writew(UDC_CLR_EP, UDC_CTRL);
1675                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1676                         omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1677                         udc->ep0_pending = 0;
1678                         break;
1679                 case USB_REQ_GET_STATUS:
1680                         /* USB_ENDPOINT_HALT status? */
1681                         if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1682                                 goto intf_status;
1683
1684                         /* ep0 never stalls */
1685                         if (!(w_index & 0xf))
1686                                 goto zero_status;
1687
1688                         /* only active endpoints count */
1689                         ep = &udc->ep[w_index & 0xf];
1690                         if (w_index & USB_DIR_IN)
1691                                 ep += 16;
1692                         if (!ep->desc)
1693                                 goto do_stall;
1694
1695                         /* iso never stalls */
1696                         if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1697                                 goto zero_status;
1698
1699                         /* FIXME don't assume non-halted endpoints!! */
1700                         ERR("%s status, can't report\n", ep->ep.name);
1701                         goto do_stall;
1702
1703 intf_status:
1704                         /* return interface status.  if we were pedantic,
1705                          * we'd detect non-existent interfaces, and stall.
1706                          */
1707                         if (u.r.bRequestType
1708                                         != (USB_DIR_IN|USB_RECIP_INTERFACE))
1709                                 goto delegate;
1710
1711 zero_status:
1712                         /* return two zero bytes */
1713                         omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1714                         omap_writew(0, UDC_DATA);
1715                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1716                         omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1717                         status = 0;
1718                         VDBG("GET_STATUS, interface %d\n", w_index);
1719                         /* next, status stage */
1720                         break;
1721                 default:
1722 delegate:
1723                         /* activate the ep0out fifo right away */
1724                         if (!udc->ep0_in && w_length) {
1725                                 omap_writew(0, UDC_EP_NUM);
1726                                 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1727                         }
1728
1729                         /* gadget drivers see class/vendor specific requests,
1730                          * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1731                          * and more
1732                          */
1733                         VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1734                                 u.r.bRequestType, u.r.bRequest,
1735                                 w_value, w_index, w_length);
1736
1737 #undef  w_value
1738 #undef  w_index
1739 #undef  w_length
1740
1741                         /* The gadget driver may return an error here,
1742                          * causing an immediate protocol stall.
1743                          *
1744                          * Else it must issue a response, either queueing a
1745                          * response buffer for the DATA stage, or halting ep0
1746                          * (causing a protocol stall, not a real halt).  A
1747                          * zero length buffer means no DATA stage.
1748                          *
1749                          * It's fine to issue that response after the setup()
1750                          * call returns, and this IRQ was handled.
1751                          */
1752                         udc->ep0_setup = 1;
1753                         spin_unlock(&udc->lock);
1754                         status = udc->driver->setup (&udc->gadget, &u.r);
1755                         spin_lock(&udc->lock);
1756                         udc->ep0_setup = 0;
1757                 }
1758
1759                 if (status < 0) {
1760 do_stall:
1761                         VDBG("req %02x.%02x protocol STALL; stat %d\n",
1762                                         u.r.bRequestType, u.r.bRequest, status);
1763                         if (udc->ep0_set_config) {
1764                                 if (udc->ep0_reset_config)
1765                                         WARNING("error resetting config?\n");
1766                                 else
1767                                         omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1768                         }
1769                         omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1770                         udc->ep0_pending = 0;
1771                 }
1772         }
1773 }
1774
1775 /*-------------------------------------------------------------------------*/
1776
1777 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1778
1779 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1780 {
1781         u16     devstat, change;
1782
1783         devstat = omap_readw(UDC_DEVSTAT);
1784         change = devstat ^ udc->devstat;
1785         udc->devstat = devstat;
1786
1787         if (change & (UDC_USB_RESET|UDC_ATT)) {
1788                 udc_quiesce(udc);
1789
1790                 if (change & UDC_ATT) {
1791                         /* driver for any external transceiver will
1792                          * have called omap_vbus_session() already
1793                          */
1794                         if (devstat & UDC_ATT) {
1795                                 udc->gadget.speed = USB_SPEED_FULL;
1796                                 VDBG("connect\n");
1797                                 if (!udc->transceiver)
1798                                         pullup_enable(udc);
1799                                 // if (driver->connect) call it
1800                         } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1801                                 udc->gadget.speed = USB_SPEED_UNKNOWN;
1802                                 if (!udc->transceiver)
1803                                         pullup_disable(udc);
1804                                 DBG("disconnect, gadget %s\n",
1805                                         udc->driver->driver.name);
1806                                 if (udc->driver->disconnect) {
1807                                         spin_unlock(&udc->lock);
1808                                         udc->driver->disconnect(&udc->gadget);
1809                                         spin_lock(&udc->lock);
1810                                 }
1811                         }
1812                         change &= ~UDC_ATT;
1813                 }
1814
1815                 if (change & UDC_USB_RESET) {
1816                         if (devstat & UDC_USB_RESET) {
1817                                 VDBG("RESET=1\n");
1818                         } else {
1819                                 udc->gadget.speed = USB_SPEED_FULL;
1820                                 INFO("USB reset done, gadget %s\n",
1821                                         udc->driver->driver.name);
1822                                 /* ep0 traffic is legal from now on */
1823                                 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1824                                                 UDC_IRQ_EN);
1825                         }
1826                         change &= ~UDC_USB_RESET;
1827                 }
1828         }
1829         if (change & UDC_SUS) {
1830                 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1831                         // FIXME tell isp1301 to suspend/resume (?)
1832                         if (devstat & UDC_SUS) {
1833                                 VDBG("suspend\n");
1834                                 update_otg(udc);
1835                                 /* HNP could be under way already */
1836                                 if (udc->gadget.speed == USB_SPEED_FULL
1837                                                 && udc->driver->suspend) {
1838                                         spin_unlock(&udc->lock);
1839                                         udc->driver->suspend(&udc->gadget);
1840                                         spin_lock(&udc->lock);
1841                                 }
1842                                 if (udc->transceiver)
1843                                         usb_phy_set_suspend(
1844                                                         udc->transceiver, 1);
1845                         } else {
1846                                 VDBG("resume\n");
1847                                 if (udc->transceiver)
1848                                         usb_phy_set_suspend(
1849                                                         udc->transceiver, 0);
1850                                 if (udc->gadget.speed == USB_SPEED_FULL
1851                                                 && udc->driver->resume) {
1852                                         spin_unlock(&udc->lock);
1853                                         udc->driver->resume(&udc->gadget);
1854                                         spin_lock(&udc->lock);
1855                                 }
1856                         }
1857                 }
1858                 change &= ~UDC_SUS;
1859         }
1860         if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1861                 update_otg(udc);
1862                 change &= ~OTG_FLAGS;
1863         }
1864
1865         change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1866         if (change)
1867                 VDBG("devstat %03x, ignore change %03x\n",
1868                         devstat,  change);
1869
1870         omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
1871 }
1872
1873 static irqreturn_t omap_udc_irq(int irq, void *_udc)
1874 {
1875         struct omap_udc *udc = _udc;
1876         u16             irq_src;
1877         irqreturn_t     status = IRQ_NONE;
1878         unsigned long   flags;
1879
1880         spin_lock_irqsave(&udc->lock, flags);
1881         irq_src = omap_readw(UDC_IRQ_SRC);
1882
1883         /* Device state change (usb ch9 stuff) */
1884         if (irq_src & UDC_DS_CHG) {
1885                 devstate_irq(_udc, irq_src);
1886                 status = IRQ_HANDLED;
1887                 irq_src &= ~UDC_DS_CHG;
1888         }
1889
1890         /* EP0 control transfers */
1891         if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1892                 ep0_irq(_udc, irq_src);
1893                 status = IRQ_HANDLED;
1894                 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1895         }
1896
1897         /* DMA transfer completion */
1898         if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1899                 dma_irq(_udc, irq_src);
1900                 status = IRQ_HANDLED;
1901                 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1902         }
1903
1904         irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
1905         if (irq_src)
1906                 DBG("udc_irq, unhandled %03x\n", irq_src);
1907         spin_unlock_irqrestore(&udc->lock, flags);
1908
1909         return status;
1910 }
1911
1912 /* workaround for seemingly-lost IRQs for RX ACKs... */
1913 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1914 #define HALF_FULL(f)    (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1915
1916 static void pio_out_timer(unsigned long _ep)
1917 {
1918         struct omap_ep  *ep = (void *) _ep;
1919         unsigned long   flags;
1920         u16             stat_flg;
1921
1922         spin_lock_irqsave(&ep->udc->lock, flags);
1923         if (!list_empty(&ep->queue) && ep->ackwait) {
1924                 use_ep(ep, UDC_EP_SEL);
1925                 stat_flg = omap_readw(UDC_STAT_FLG);
1926
1927                 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1928                                 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1929                         struct omap_req *req;
1930
1931                         VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1932                         req = container_of(ep->queue.next,
1933                                         struct omap_req, queue);
1934                         (void) read_fifo(ep, req);
1935                         omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1936                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1937                         ep->ackwait = 1 + ep->double_buf;
1938                 } else
1939                         deselect_ep();
1940         }
1941         mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1942         spin_unlock_irqrestore(&ep->udc->lock, flags);
1943 }
1944
1945 static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
1946 {
1947         u16             epn_stat, irq_src;
1948         irqreturn_t     status = IRQ_NONE;
1949         struct omap_ep  *ep;
1950         int             epnum;
1951         struct omap_udc *udc = _dev;
1952         struct omap_req *req;
1953         unsigned long   flags;
1954
1955         spin_lock_irqsave(&udc->lock, flags);
1956         epn_stat = omap_readw(UDC_EPN_STAT);
1957         irq_src = omap_readw(UDC_IRQ_SRC);
1958
1959         /* handle OUT first, to avoid some wasteful NAKs */
1960         if (irq_src & UDC_EPN_RX) {
1961                 epnum = (epn_stat >> 8) & 0x0f;
1962                 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
1963                 status = IRQ_HANDLED;
1964                 ep = &udc->ep[epnum];
1965                 ep->irqs++;
1966
1967                 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
1968                 ep->fnf = 0;
1969                 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1970                         ep->ackwait--;
1971                         if (!list_empty(&ep->queue)) {
1972                                 int stat;
1973                                 req = container_of(ep->queue.next,
1974                                                 struct omap_req, queue);
1975                                 stat = read_fifo(ep, req);
1976                                 if (!ep->double_buf)
1977                                         ep->fnf = 1;
1978                         }
1979                 }
1980                 /* min 6 clock delay before clearing EP_SEL ... */
1981                 epn_stat = omap_readw(UDC_EPN_STAT);
1982                 epn_stat = omap_readw(UDC_EPN_STAT);
1983                 omap_writew(epnum, UDC_EP_NUM);
1984
1985                 /* enabling fifo _after_ clearing ACK, contrary to docs,
1986                  * reduces lossage; timer still needed though (sigh).
1987                  */
1988                 if (ep->fnf) {
1989                         omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1990                         ep->ackwait = 1 + ep->double_buf;
1991                 }
1992                 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1993         }
1994
1995         /* then IN transfers */
1996         else if (irq_src & UDC_EPN_TX) {
1997                 epnum = epn_stat & 0x0f;
1998                 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
1999                 status = IRQ_HANDLED;
2000                 ep = &udc->ep[16 + epnum];
2001                 ep->irqs++;
2002
2003                 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
2004                 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
2005                         ep->ackwait = 0;
2006                         if (!list_empty(&ep->queue)) {
2007                                 req = container_of(ep->queue.next,
2008                                                 struct omap_req, queue);
2009                                 (void) write_fifo(ep, req);
2010                         }
2011                 }
2012                 /* min 6 clock delay before clearing EP_SEL ... */
2013                 epn_stat = omap_readw(UDC_EPN_STAT);
2014                 epn_stat = omap_readw(UDC_EPN_STAT);
2015                 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
2016                 /* then 6 clocks before it'd tx */
2017         }
2018
2019         spin_unlock_irqrestore(&udc->lock, flags);
2020         return status;
2021 }
2022
2023 #ifdef  USE_ISO
2024 static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
2025 {
2026         struct omap_udc *udc = _dev;
2027         struct omap_ep  *ep;
2028         int             pending = 0;
2029         unsigned long   flags;
2030
2031         spin_lock_irqsave(&udc->lock, flags);
2032
2033         /* handle all non-DMA ISO transfers */
2034         list_for_each_entry (ep, &udc->iso, iso) {
2035                 u16             stat;
2036                 struct omap_req *req;
2037
2038                 if (ep->has_dma || list_empty(&ep->queue))
2039                         continue;
2040                 req = list_entry(ep->queue.next, struct omap_req, queue);
2041
2042                 use_ep(ep, UDC_EP_SEL);
2043                 stat = omap_readw(UDC_STAT_FLG);
2044
2045                 /* NOTE: like the other controller drivers, this isn't
2046                  * currently reporting lost or damaged frames.
2047                  */
2048                 if (ep->bEndpointAddress & USB_DIR_IN) {
2049                         if (stat & UDC_MISS_IN)
2050                                 /* done(ep, req, -EPROTO) */;
2051                         else
2052                                 write_fifo(ep, req);
2053                 } else {
2054                         int     status = 0;
2055
2056                         if (stat & UDC_NO_RXPACKET)
2057                                 status = -EREMOTEIO;
2058                         else if (stat & UDC_ISO_ERR)
2059                                 status = -EILSEQ;
2060                         else if (stat & UDC_DATA_FLUSH)
2061                                 status = -ENOSR;
2062
2063                         if (status)
2064                                 /* done(ep, req, status) */;
2065                         else
2066                                 read_fifo(ep, req);
2067                 }
2068                 deselect_ep();
2069                 /* 6 wait states before next EP */
2070
2071                 ep->irqs++;
2072                 if (!list_empty(&ep->queue))
2073                         pending = 1;
2074         }
2075         if (!pending) {
2076                 u16 w;
2077
2078                 w = omap_readw(UDC_IRQ_EN);
2079                 w &= ~UDC_SOF_IE;
2080                 omap_writew(w, UDC_IRQ_EN);
2081         }
2082         omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
2083
2084         spin_unlock_irqrestore(&udc->lock, flags);
2085         return IRQ_HANDLED;
2086 }
2087 #endif
2088
2089 /*-------------------------------------------------------------------------*/
2090
2091 static inline int machine_without_vbus_sense(void)
2092 {
2093         return (machine_is_omap_innovator()
2094                 || machine_is_omap_osk()
2095                 || machine_is_omap_apollon()
2096 #ifndef CONFIG_MACH_OMAP_H4_OTG
2097                 || machine_is_omap_h4()
2098 #endif
2099                 || machine_is_sx1()
2100                 || cpu_is_omap7xx() /* No known omap7xx boards with vbus sense */
2101                 );
2102 }
2103
2104 static int omap_udc_start(struct usb_gadget_driver *driver,
2105                 int (*bind)(struct usb_gadget *))
2106 {
2107         int             status = -ENODEV;
2108         struct omap_ep  *ep;
2109         unsigned long   flags;
2110
2111         /* basic sanity tests */
2112         if (!udc)
2113                 return -ENODEV;
2114         if (!driver
2115                         // FIXME if otg, check:  driver->is_otg
2116                         || driver->max_speed < USB_SPEED_FULL
2117                         || !bind || !driver->setup)
2118                 return -EINVAL;
2119
2120         spin_lock_irqsave(&udc->lock, flags);
2121         if (udc->driver) {
2122                 spin_unlock_irqrestore(&udc->lock, flags);
2123                 return -EBUSY;
2124         }
2125
2126         /* reset state */
2127         list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
2128                 ep->irqs = 0;
2129                 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2130                         continue;
2131                 use_ep(ep, 0);
2132                 omap_writew(UDC_SET_HALT, UDC_CTRL);
2133         }
2134         udc->ep0_pending = 0;
2135         udc->ep[0].irqs = 0;
2136         udc->softconnect = 1;
2137
2138         /* hook up the driver */
2139         driver->driver.bus = NULL;
2140         udc->driver = driver;
2141         udc->gadget.dev.driver = &driver->driver;
2142         spin_unlock_irqrestore(&udc->lock, flags);
2143
2144         if (udc->dc_clk != NULL)
2145                 omap_udc_enable_clock(1);
2146
2147         status = bind(&udc->gadget);
2148         if (status) {
2149                 DBG("bind to %s --> %d\n", driver->driver.name, status);
2150                 udc->gadget.dev.driver = NULL;
2151                 udc->driver = NULL;
2152                 goto done;
2153         }
2154         DBG("bound to driver %s\n", driver->driver.name);
2155
2156         omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2157
2158         /* connect to bus through transceiver */
2159         if (udc->transceiver) {
2160                 status = otg_set_peripheral(udc->transceiver->otg,
2161                                                 &udc->gadget);
2162                 if (status < 0) {
2163                         ERR("can't bind to transceiver\n");
2164                         if (driver->unbind) {
2165                                 driver->unbind (&udc->gadget);
2166                                 udc->gadget.dev.driver = NULL;
2167                                 udc->driver = NULL;
2168                         }
2169                         goto done;
2170                 }
2171         } else {
2172                 if (can_pullup(udc))
2173                         pullup_enable (udc);
2174                 else
2175                         pullup_disable (udc);
2176         }
2177
2178         /* boards that don't have VBUS sensing can't autogate 48MHz;
2179          * can't enter deep sleep while a gadget driver is active.
2180          */
2181         if (machine_without_vbus_sense())
2182                 omap_vbus_session(&udc->gadget, 1);
2183
2184 done:
2185         if (udc->dc_clk != NULL)
2186                 omap_udc_enable_clock(0);
2187         return status;
2188 }
2189
2190 static int omap_udc_stop(struct usb_gadget_driver *driver)
2191 {
2192         unsigned long   flags;
2193         int             status = -ENODEV;
2194
2195         if (!udc)
2196                 return -ENODEV;
2197         if (!driver || driver != udc->driver || !driver->unbind)
2198                 return -EINVAL;
2199
2200         if (udc->dc_clk != NULL)
2201                 omap_udc_enable_clock(1);
2202
2203         if (machine_without_vbus_sense())
2204                 omap_vbus_session(&udc->gadget, 0);
2205
2206         if (udc->transceiver)
2207                 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
2208         else
2209                 pullup_disable(udc);
2210
2211         spin_lock_irqsave(&udc->lock, flags);
2212         udc_quiesce(udc);
2213         spin_unlock_irqrestore(&udc->lock, flags);
2214
2215         driver->unbind(&udc->gadget);
2216         udc->gadget.dev.driver = NULL;
2217         udc->driver = NULL;
2218
2219         if (udc->dc_clk != NULL)
2220                 omap_udc_enable_clock(0);
2221         DBG("unregistered driver '%s'\n", driver->driver.name);
2222         return status;
2223 }
2224
2225 /*-------------------------------------------------------------------------*/
2226
2227 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2228
2229 #include <linux/seq_file.h>
2230
2231 static const char proc_filename[] = "driver/udc";
2232
2233 #define FOURBITS "%s%s%s%s"
2234 #define EIGHTBITS FOURBITS FOURBITS
2235
2236 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2237 {
2238         u16             stat_flg;
2239         struct omap_req *req;
2240         char            buf[20];
2241
2242         use_ep(ep, 0);
2243
2244         if (use_dma && ep->has_dma)
2245                 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2246                         (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2247                         ep->dma_channel - 1, ep->lch);
2248         else
2249                 buf[0] = 0;
2250
2251         stat_flg = omap_readw(UDC_STAT_FLG);
2252         seq_printf(s,
2253                 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2254                 ep->name, buf,
2255                 ep->double_buf ? "dbuf " : "",
2256                 ({char *s; switch(ep->ackwait){
2257                 case 0: s = ""; break;
2258                 case 1: s = "(ackw) "; break;
2259                 case 2: s = "(ackw2) "; break;
2260                 default: s = "(?) "; break;
2261                 } s;}),
2262                 ep->irqs, stat_flg,
2263                 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2264                 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2265                 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2266                 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2267                 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2268                 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2269                 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2270                 (stat_flg & UDC_STALL) ? "STALL " : "",
2271                 (stat_flg & UDC_NAK) ? "NAK " : "",
2272                 (stat_flg & UDC_ACK) ? "ACK " : "",
2273                 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2274                 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2275                 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2276
2277         if (list_empty (&ep->queue))
2278                 seq_printf(s, "\t(queue empty)\n");
2279         else
2280                 list_for_each_entry (req, &ep->queue, queue) {
2281                         unsigned        length = req->req.actual;
2282
2283                         if (use_dma && buf[0]) {
2284                                 length += ((ep->bEndpointAddress & USB_DIR_IN)
2285                                                 ? dma_src_len : dma_dest_len)
2286                                         (ep, req->req.dma + length);
2287                                 buf[0] = 0;
2288                         }
2289                         seq_printf(s, "\treq %p len %d/%d buf %p\n",
2290                                         &req->req, length,
2291                                         req->req.length, req->req.buf);
2292                 }
2293 }
2294
2295 static char *trx_mode(unsigned m, int enabled)
2296 {
2297         switch (m) {
2298         case 0:         return enabled ? "*6wire" : "unused";
2299         case 1:         return "4wire";
2300         case 2:         return "3wire";
2301         case 3:         return "6wire";
2302         default:        return "unknown";
2303         }
2304 }
2305
2306 static int proc_otg_show(struct seq_file *s)
2307 {
2308         u32             tmp;
2309         u32             trans = 0;
2310         char            *ctrl_name = "(UNKNOWN)";
2311
2312         /* XXX This needs major revision for OMAP2+ */
2313         tmp = omap_readl(OTG_REV);
2314         if (cpu_class_is_omap1()) {
2315                 ctrl_name = "tranceiver_ctrl";
2316                 trans = omap_readw(USB_TRANSCEIVER_CTRL);
2317         }
2318         seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2319                 tmp >> 4, tmp & 0xf, ctrl_name, trans);
2320         tmp = omap_readw(OTG_SYSCON_1);
2321         seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2322                         FOURBITS "\n", tmp,
2323                 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2324                 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2325                 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2326                         ? "internal"
2327                         : trx_mode(USB0_TRX_MODE(tmp), 1),
2328                 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2329                 (tmp & HST_IDLE_EN) ? " !host" : "",
2330                 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2331                 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2332         tmp = omap_readl(OTG_SYSCON_2);
2333         seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2334                         " b_ase_brst=%d hmc=%d\n", tmp,
2335                 (tmp & OTG_EN) ? " otg_en" : "",
2336                 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2337                 // much more SRP stuff
2338                 (tmp & SRP_DATA) ? " srp_data" : "",
2339                 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2340                 (tmp & OTG_PADEN) ? " otg_paden" : "",
2341                 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2342                 (tmp & UHOST_EN) ? " uhost_en" : "",
2343                 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2344                 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2345                 B_ASE_BRST(tmp),
2346                 OTG_HMC(tmp));
2347         tmp = omap_readl(OTG_CTRL);
2348         seq_printf(s, "otg_ctrl    %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2349                 (tmp & OTG_ASESSVLD) ? " asess" : "",
2350                 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2351                 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2352                 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2353                 (tmp & OTG_ID) ? " id" : "",
2354                 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2355                 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2356                 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2357                 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2358                 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2359                 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2360                 (tmp & OTG_PULLDOWN) ? " down" : "",
2361                 (tmp & OTG_PULLUP) ? " up" : "",
2362                 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2363                 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2364                 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2365                 (tmp & OTG_PU_ID) ? " pu_id" : ""
2366                 );
2367         tmp = omap_readw(OTG_IRQ_EN);
2368         seq_printf(s, "otg_irq_en  %04x" "\n", tmp);
2369         tmp = omap_readw(OTG_IRQ_SRC);
2370         seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2371         tmp = omap_readw(OTG_OUTCTRL);
2372         seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2373         tmp = omap_readw(OTG_TEST);
2374         seq_printf(s, "otg_test    %04x" "\n", tmp);
2375         return 0;
2376 }
2377
2378 static int proc_udc_show(struct seq_file *s, void *_)
2379 {
2380         u32             tmp;
2381         struct omap_ep  *ep;
2382         unsigned long   flags;
2383
2384         spin_lock_irqsave(&udc->lock, flags);
2385
2386         seq_printf(s, "%s, version: " DRIVER_VERSION
2387 #ifdef  USE_ISO
2388                 " (iso)"
2389 #endif
2390                 "%s\n",
2391                 driver_desc,
2392                 use_dma ?  " (dma)" : "");
2393
2394         tmp = omap_readw(UDC_REV) & 0xff;
2395         seq_printf(s,
2396                 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2397                 "hmc %d, transceiver %s\n",
2398                 tmp >> 4, tmp & 0xf,
2399                 fifo_mode,
2400                 udc->driver ? udc->driver->driver.name : "(none)",
2401                 HMC,
2402                 udc->transceiver
2403                         ? udc->transceiver->label
2404                         : ((cpu_is_omap1710() || cpu_is_omap24xx())
2405                                 ? "external" : "(none)"));
2406         if (cpu_class_is_omap1()) {
2407                 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2408                         omap_readw(ULPD_CLOCK_CTRL),
2409                         omap_readw(ULPD_SOFT_REQ),
2410                         omap_readw(ULPD_STATUS_REQ));
2411         }
2412
2413         /* OTG controller registers */
2414         if (!cpu_is_omap15xx())
2415                 proc_otg_show(s);
2416
2417         tmp = omap_readw(UDC_SYSCON1);
2418         seq_printf(s, "\nsyscon1     %04x" EIGHTBITS "\n", tmp,
2419                 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2420                 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2421                 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2422                 (tmp & UDC_NAK_EN) ? " nak" : "",
2423                 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2424                 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2425                 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2426                 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2427         // syscon2 is write-only
2428
2429         /* UDC controller registers */
2430         if (!(tmp & UDC_PULLUP_EN)) {
2431                 seq_printf(s, "(suspended)\n");
2432                 spin_unlock_irqrestore(&udc->lock, flags);
2433                 return 0;
2434         }
2435
2436         tmp = omap_readw(UDC_DEVSTAT);
2437         seq_printf(s, "devstat     %04x" EIGHTBITS "%s%s\n", tmp,
2438                 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2439                 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2440                 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2441                 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2442                 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2443                 (tmp & UDC_SUS) ? " SUS" : "",
2444                 (tmp & UDC_CFG) ? " CFG" : "",
2445                 (tmp & UDC_ADD) ? " ADD" : "",
2446                 (tmp & UDC_DEF) ? " DEF" : "",
2447                 (tmp & UDC_ATT) ? " ATT" : "");
2448         seq_printf(s, "sof         %04x\n", omap_readw(UDC_SOF));
2449         tmp = omap_readw(UDC_IRQ_EN);
2450         seq_printf(s, "irq_en      %04x" FOURBITS "%s\n", tmp,
2451                 (tmp & UDC_SOF_IE) ? " sof" : "",
2452                 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2453                 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2454                 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2455                 (tmp & UDC_EP0_IE) ? " ep0" : "");
2456         tmp = omap_readw(UDC_IRQ_SRC);
2457         seq_printf(s, "irq_src     %04x" EIGHTBITS "%s%s\n", tmp,
2458                 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2459                 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2460                 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2461                 (tmp & UDC_IRQ_SOF) ? " sof" : "",
2462                 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2463                 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2464                 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2465                 (tmp & UDC_SETUP) ? " setup" : "",
2466                 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2467                 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2468         if (use_dma) {
2469                 unsigned i;
2470
2471                 tmp = omap_readw(UDC_DMA_IRQ_EN);
2472                 seq_printf(s, "dma_irq_en  %04x%s" EIGHTBITS "\n", tmp,
2473                         (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2474                         (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2475                         (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2476
2477                         (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2478                         (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2479                         (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2480
2481                         (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2482                         (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2483                         (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2484
2485                 tmp = omap_readw(UDC_RXDMA_CFG);
2486                 seq_printf(s, "rxdma_cfg   %04x\n", tmp);
2487                 if (tmp) {
2488                         for (i = 0; i < 3; i++) {
2489                                 if ((tmp & (0x0f << (i * 4))) == 0)
2490                                         continue;
2491                                 seq_printf(s, "rxdma[%d]    %04x\n", i,
2492                                                 omap_readw(UDC_RXDMA(i + 1)));
2493                         }
2494                 }
2495                 tmp = omap_readw(UDC_TXDMA_CFG);
2496                 seq_printf(s, "txdma_cfg   %04x\n", tmp);
2497                 if (tmp) {
2498                         for (i = 0; i < 3; i++) {
2499                                 if (!(tmp & (0x0f << (i * 4))))
2500                                         continue;
2501                                 seq_printf(s, "txdma[%d]    %04x\n", i,
2502                                                 omap_readw(UDC_TXDMA(i + 1)));
2503                         }
2504                 }
2505         }
2506
2507         tmp = omap_readw(UDC_DEVSTAT);
2508         if (tmp & UDC_ATT) {
2509                 proc_ep_show(s, &udc->ep[0]);
2510                 if (tmp & UDC_ADD) {
2511                         list_for_each_entry (ep, &udc->gadget.ep_list,
2512                                         ep.ep_list) {
2513                                 if (ep->desc)
2514                                         proc_ep_show(s, ep);
2515                         }
2516                 }
2517         }
2518         spin_unlock_irqrestore(&udc->lock, flags);
2519         return 0;
2520 }
2521
2522 static int proc_udc_open(struct inode *inode, struct file *file)
2523 {
2524         return single_open(file, proc_udc_show, NULL);
2525 }
2526
2527 static const struct file_operations proc_ops = {
2528         .owner          = THIS_MODULE,
2529         .open           = proc_udc_open,
2530         .read           = seq_read,
2531         .llseek         = seq_lseek,
2532         .release        = single_release,
2533 };
2534
2535 static void create_proc_file(void)
2536 {
2537         proc_create(proc_filename, 0, NULL, &proc_ops);
2538 }
2539
2540 static void remove_proc_file(void)
2541 {
2542         remove_proc_entry(proc_filename, NULL);
2543 }
2544
2545 #else
2546
2547 static inline void create_proc_file(void) {}
2548 static inline void remove_proc_file(void) {}
2549
2550 #endif
2551
2552 /*-------------------------------------------------------------------------*/
2553
2554 /* Before this controller can enumerate, we need to pick an endpoint
2555  * configuration, or "fifo_mode"  That involves allocating 2KB of packet
2556  * buffer space among the endpoints we'll be operating.
2557  *
2558  * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2559  * UDC_SYSCON_1.CFG_LOCK is set can now work.  We won't use that
2560  * capability yet though.
2561  */
2562 static unsigned __init
2563 omap_ep_setup(char *name, u8 addr, u8 type,
2564                 unsigned buf, unsigned maxp, int dbuf)
2565 {
2566         struct omap_ep  *ep;
2567         u16             epn_rxtx = 0;
2568
2569         /* OUT endpoints first, then IN */
2570         ep = &udc->ep[addr & 0xf];
2571         if (addr & USB_DIR_IN)
2572                 ep += 16;
2573
2574         /* in case of ep init table bugs */
2575         BUG_ON(ep->name[0]);
2576
2577         /* chip setup ... bit values are same for IN, OUT */
2578         if (type == USB_ENDPOINT_XFER_ISOC) {
2579                 switch (maxp) {
2580                 case 8:         epn_rxtx = 0 << 12; break;
2581                 case 16:        epn_rxtx = 1 << 12; break;
2582                 case 32:        epn_rxtx = 2 << 12; break;
2583                 case 64:        epn_rxtx = 3 << 12; break;
2584                 case 128:       epn_rxtx = 4 << 12; break;
2585                 case 256:       epn_rxtx = 5 << 12; break;
2586                 case 512:       epn_rxtx = 6 << 12; break;
2587                 default:        BUG();
2588                 }
2589                 epn_rxtx |= UDC_EPN_RX_ISO;
2590                 dbuf = 1;
2591         } else {
2592                 /* double-buffering "not supported" on 15xx,
2593                  * and ignored for PIO-IN on newer chips
2594                  * (for more reliable behavior)
2595                  */
2596                 if (!use_dma || cpu_is_omap15xx() || cpu_is_omap24xx())
2597                         dbuf = 0;
2598
2599                 switch (maxp) {
2600                 case 8:         epn_rxtx = 0 << 12; break;
2601                 case 16:        epn_rxtx = 1 << 12; break;
2602                 case 32:        epn_rxtx = 2 << 12; break;
2603                 case 64:        epn_rxtx = 3 << 12; break;
2604                 default:        BUG();
2605                 }
2606                 if (dbuf && addr)
2607                         epn_rxtx |= UDC_EPN_RX_DB;
2608                 init_timer(&ep->timer);
2609                 ep->timer.function = pio_out_timer;
2610                 ep->timer.data = (unsigned long) ep;
2611         }
2612         if (addr)
2613                 epn_rxtx |= UDC_EPN_RX_VALID;
2614         BUG_ON(buf & 0x07);
2615         epn_rxtx |= buf >> 3;
2616
2617         DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2618                 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2619
2620         if (addr & USB_DIR_IN)
2621                 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
2622         else
2623                 omap_writew(epn_rxtx, UDC_EP_RX(addr));
2624
2625         /* next endpoint's buffer starts after this one's */
2626         buf += maxp;
2627         if (dbuf)
2628                 buf += maxp;
2629         BUG_ON(buf > 2048);
2630
2631         /* set up driver data structures */
2632         BUG_ON(strlen(name) >= sizeof ep->name);
2633         strlcpy(ep->name, name, sizeof ep->name);
2634         INIT_LIST_HEAD(&ep->queue);
2635         INIT_LIST_HEAD(&ep->iso);
2636         ep->bEndpointAddress = addr;
2637         ep->bmAttributes = type;
2638         ep->double_buf = dbuf;
2639         ep->udc = udc;
2640
2641         ep->ep.name = ep->name;
2642         ep->ep.ops = &omap_ep_ops;
2643         ep->ep.maxpacket = ep->maxpacket = maxp;
2644         list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
2645
2646         return buf;
2647 }
2648
2649 static void omap_udc_release(struct device *dev)
2650 {
2651         complete(udc->done);
2652         kfree (udc);
2653         udc = NULL;
2654 }
2655
2656 static int __init
2657 omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2658 {
2659         unsigned        tmp, buf;
2660
2661         /* abolish any previous hardware state */
2662         omap_writew(0, UDC_SYSCON1);
2663         omap_writew(0, UDC_IRQ_EN);
2664         omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2665         omap_writew(0, UDC_DMA_IRQ_EN);
2666         omap_writew(0, UDC_RXDMA_CFG);
2667         omap_writew(0, UDC_TXDMA_CFG);
2668
2669         /* UDC_PULLUP_EN gates the chip clock */
2670         // OTG_SYSCON_1 |= DEV_IDLE_EN;
2671
2672         udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2673         if (!udc)
2674                 return -ENOMEM;
2675
2676         spin_lock_init (&udc->lock);
2677
2678         udc->gadget.ops = &omap_gadget_ops;
2679         udc->gadget.ep0 = &udc->ep[0].ep;
2680         INIT_LIST_HEAD(&udc->gadget.ep_list);
2681         INIT_LIST_HEAD(&udc->iso);
2682         udc->gadget.speed = USB_SPEED_UNKNOWN;
2683         udc->gadget.max_speed = USB_SPEED_FULL;
2684         udc->gadget.name = driver_name;
2685
2686         device_initialize(&udc->gadget.dev);
2687         dev_set_name(&udc->gadget.dev, "gadget");
2688         udc->gadget.dev.release = omap_udc_release;
2689         udc->gadget.dev.parent = &odev->dev;
2690         if (use_dma)
2691                 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2692
2693         udc->transceiver = xceiv;
2694
2695         /* ep0 is special; put it right after the SETUP buffer */
2696         buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2697                         8 /* after SETUP */, 64 /* maxpacket */, 0);
2698         list_del_init(&udc->ep[0].ep.ep_list);
2699
2700         /* initially disable all non-ep0 endpoints */
2701         for (tmp = 1; tmp < 15; tmp++) {
2702                 omap_writew(0, UDC_EP_RX(tmp));
2703                 omap_writew(0, UDC_EP_TX(tmp));
2704         }
2705
2706 #define OMAP_BULK_EP(name,addr) \
2707         buf = omap_ep_setup(name "-bulk", addr, \
2708                         USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2709 #define OMAP_INT_EP(name,addr, maxp) \
2710         buf = omap_ep_setup(name "-int", addr, \
2711                         USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2712 #define OMAP_ISO_EP(name,addr, maxp) \
2713         buf = omap_ep_setup(name "-iso", addr, \
2714                         USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2715
2716         switch (fifo_mode) {
2717         case 0:
2718                 OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
2719                 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2720                 OMAP_INT_EP("ep3in",   USB_DIR_IN  | 3, 16);
2721                 break;
2722         case 1:
2723                 OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
2724                 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2725                 OMAP_INT_EP("ep9in",   USB_DIR_IN  | 9, 16);
2726
2727                 OMAP_BULK_EP("ep3in",  USB_DIR_IN  | 3);
2728                 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2729                 OMAP_INT_EP("ep10in",  USB_DIR_IN  | 10, 16);
2730
2731                 OMAP_BULK_EP("ep5in",  USB_DIR_IN  | 5);
2732                 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2733                 OMAP_INT_EP("ep11in",  USB_DIR_IN  | 11, 16);
2734
2735                 OMAP_BULK_EP("ep6in",  USB_DIR_IN  | 6);
2736                 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2737                 OMAP_INT_EP("ep12in",  USB_DIR_IN  | 12, 16);
2738
2739                 OMAP_BULK_EP("ep7in",  USB_DIR_IN  | 7);
2740                 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2741                 OMAP_INT_EP("ep13in",  USB_DIR_IN  | 13, 16);
2742                 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2743
2744                 OMAP_BULK_EP("ep8in",  USB_DIR_IN  | 8);
2745                 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2746                 OMAP_INT_EP("ep14in",  USB_DIR_IN  | 14, 16);
2747                 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2748
2749                 OMAP_BULK_EP("ep15in",  USB_DIR_IN  | 15);
2750                 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2751
2752                 break;
2753
2754 #ifdef  USE_ISO
2755         case 2:                 /* mixed iso/bulk */
2756                 OMAP_ISO_EP("ep1in",   USB_DIR_IN  | 1, 256);
2757                 OMAP_ISO_EP("ep2out",  USB_DIR_OUT | 2, 256);
2758                 OMAP_ISO_EP("ep3in",   USB_DIR_IN  | 3, 128);
2759                 OMAP_ISO_EP("ep4out",  USB_DIR_OUT | 4, 128);
2760
2761                 OMAP_INT_EP("ep5in",   USB_DIR_IN  | 5, 16);
2762
2763                 OMAP_BULK_EP("ep6in",  USB_DIR_IN  | 6);
2764                 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2765                 OMAP_INT_EP("ep8in",   USB_DIR_IN  | 8, 16);
2766                 break;
2767         case 3:                 /* mixed bulk/iso */
2768                 OMAP_BULK_EP("ep1in",  USB_DIR_IN  | 1);
2769                 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2770                 OMAP_INT_EP("ep3in",   USB_DIR_IN  | 3, 16);
2771
2772                 OMAP_BULK_EP("ep4in",  USB_DIR_IN  | 4);
2773                 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2774                 OMAP_INT_EP("ep6in",   USB_DIR_IN  | 6, 16);
2775
2776                 OMAP_ISO_EP("ep7in",   USB_DIR_IN  | 7, 256);
2777                 OMAP_ISO_EP("ep8out",  USB_DIR_OUT | 8, 256);
2778                 OMAP_INT_EP("ep9in",   USB_DIR_IN  | 9, 16);
2779                 break;
2780 #endif
2781
2782         /* add more modes as needed */
2783
2784         default:
2785                 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2786                 return -ENODEV;
2787         }
2788         omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
2789         INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2790         return 0;
2791 }
2792
2793 static int __init omap_udc_probe(struct platform_device *pdev)
2794 {
2795         int                     status = -ENODEV;
2796         int                     hmc;
2797         struct usb_phy          *xceiv = NULL;
2798         const char              *type = NULL;
2799         struct omap_usb_config  *config = pdev->dev.platform_data;
2800         struct clk              *dc_clk;
2801         struct clk              *hhc_clk;
2802
2803         /* NOTE:  "knows" the order of the resources! */
2804         if (!request_mem_region(pdev->resource[0].start,
2805                         pdev->resource[0].end - pdev->resource[0].start + 1,
2806                         driver_name)) {
2807                 DBG("request_mem_region failed\n");
2808                 return -EBUSY;
2809         }
2810
2811         if (cpu_is_omap16xx()) {
2812                 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2813                 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2814                 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2815                 /* can't use omap_udc_enable_clock yet */
2816                 clk_enable(dc_clk);
2817                 clk_enable(hhc_clk);
2818                 udelay(100);
2819         }
2820
2821         if (cpu_is_omap24xx()) {
2822                 dc_clk = clk_get(&pdev->dev, "usb_fck");
2823                 hhc_clk = clk_get(&pdev->dev, "usb_l4_ick");
2824                 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2825                 /* can't use omap_udc_enable_clock yet */
2826                 clk_enable(dc_clk);
2827                 clk_enable(hhc_clk);
2828                 udelay(100);
2829         }
2830
2831         if (cpu_is_omap7xx()) {
2832                 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2833                 hhc_clk = clk_get(&pdev->dev, "l3_ocpi_ck");
2834                 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2835                 /* can't use omap_udc_enable_clock yet */
2836                 clk_enable(dc_clk);
2837                 clk_enable(hhc_clk);
2838                 udelay(100);
2839         }
2840
2841         INFO("OMAP UDC rev %d.%d%s\n",
2842                 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
2843                 config->otg ? ", Mini-AB" : "");
2844
2845         /* use the mode given to us by board init code */
2846         if (cpu_is_omap15xx()) {
2847                 hmc = HMC_1510;
2848                 type = "(unknown)";
2849
2850                 if (machine_without_vbus_sense()) {
2851                         /* just set up software VBUS detect, and then
2852                          * later rig it so we always report VBUS.
2853                          * FIXME without really sensing VBUS, we can't
2854                          * know when to turn PULLUP_EN on/off; and that
2855                          * means we always "need" the 48MHz clock.
2856                          */
2857                         u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2858                         tmp &= ~VBUS_CTRL_1510;
2859                         omap_writel(tmp, FUNC_MUX_CTRL_0);
2860                         tmp |= VBUS_MODE_1510;
2861                         tmp &= ~VBUS_CTRL_1510;
2862                         omap_writel(tmp, FUNC_MUX_CTRL_0);
2863                 }
2864         } else {
2865                 /* The transceiver may package some GPIO logic or handle
2866                  * loopback and/or transceiverless setup; if we find one,
2867                  * use it.  Except for OTG, we don't _need_ to talk to one;
2868                  * but not having one probably means no VBUS detection.
2869                  */
2870                 xceiv = usb_get_transceiver();
2871                 if (xceiv)
2872                         type = xceiv->label;
2873                 else if (config->otg) {
2874                         DBG("OTG requires external transceiver!\n");
2875                         goto cleanup0;
2876                 }
2877
2878                 hmc = HMC_1610;
2879
2880                 if (cpu_is_omap24xx()) {
2881                         /* this could be transceiverless in one of the
2882                          * "we don't need to know" modes.
2883                          */
2884                         type = "external";
2885                         goto known;
2886                 }
2887
2888                 switch (hmc) {
2889                 case 0:                 /* POWERUP DEFAULT == 0 */
2890                 case 4:
2891                 case 12:
2892                 case 20:
2893                         if (!cpu_is_omap1710()) {
2894                                 type = "integrated";
2895                                 break;
2896                         }
2897                         /* FALL THROUGH */
2898                 case 3:
2899                 case 11:
2900                 case 16:
2901                 case 19:
2902                 case 25:
2903                         if (!xceiv) {
2904                                 DBG("external transceiver not registered!\n");
2905                                 type = "unknown";
2906                         }
2907                         break;
2908                 case 21:                        /* internal loopback */
2909                         type = "loopback";
2910                         break;
2911                 case 14:                        /* transceiverless */
2912                         if (cpu_is_omap1710())
2913                                 goto bad_on_1710;
2914                         /* FALL THROUGH */
2915                 case 13:
2916                 case 15:
2917                         type = "no";
2918                         break;
2919
2920                 default:
2921 bad_on_1710:
2922                         ERR("unrecognized UDC HMC mode %d\n", hmc);
2923                         goto cleanup0;
2924                 }
2925         }
2926 known:
2927         INFO("hmc mode %d, %s transceiver\n", hmc, type);
2928
2929         /* a "gadget" abstracts/virtualizes the controller */
2930         status = omap_udc_setup(pdev, xceiv);
2931         if (status) {
2932                 goto cleanup0;
2933         }
2934         xceiv = NULL;
2935         // "udc" is now valid
2936         pullup_disable(udc);
2937 #if     defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2938         udc->gadget.is_otg = (config->otg != 0);
2939 #endif
2940
2941         /* starting with omap1710 es2.0, clear toggle is a separate bit */
2942         if (omap_readw(UDC_REV) >= 0x61)
2943                 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2944         else
2945                 udc->clr_halt = UDC_RESET_EP;
2946
2947         /* USB general purpose IRQ:  ep0, state changes, dma, etc */
2948         status = request_irq(pdev->resource[1].start, omap_udc_irq,
2949                         IRQF_SAMPLE_RANDOM, driver_name, udc);
2950         if (status != 0) {
2951                 ERR("can't get irq %d, err %d\n",
2952                         (int) pdev->resource[1].start, status);
2953                 goto cleanup1;
2954         }
2955
2956         /* USB "non-iso" IRQ (PIO for all but ep0) */
2957         status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
2958                         IRQF_SAMPLE_RANDOM, "omap_udc pio", udc);
2959         if (status != 0) {
2960                 ERR("can't get irq %d, err %d\n",
2961                         (int) pdev->resource[2].start, status);
2962                 goto cleanup2;
2963         }
2964 #ifdef  USE_ISO
2965         status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
2966                         0, "omap_udc iso", udc);
2967         if (status != 0) {
2968                 ERR("can't get irq %d, err %d\n",
2969                         (int) pdev->resource[3].start, status);
2970                 goto cleanup3;
2971         }
2972 #endif
2973         if (cpu_is_omap16xx() || cpu_is_omap7xx()) {
2974                 udc->dc_clk = dc_clk;
2975                 udc->hhc_clk = hhc_clk;
2976                 clk_disable(hhc_clk);
2977                 clk_disable(dc_clk);
2978         }
2979
2980         if (cpu_is_omap24xx()) {
2981                 udc->dc_clk = dc_clk;
2982                 udc->hhc_clk = hhc_clk;
2983                 /* FIXME OMAP2 don't release hhc & dc clock */
2984 #if 0
2985                 clk_disable(hhc_clk);
2986                 clk_disable(dc_clk);
2987 #endif
2988         }
2989
2990         create_proc_file();
2991         status = device_add(&udc->gadget.dev);
2992         if (status)
2993                 goto cleanup4;
2994
2995         status = usb_add_gadget_udc(&pdev->dev, &udc->gadget);
2996         if (!status)
2997                 return status;
2998         /* If fail, fall through */
2999 cleanup4:
3000         remove_proc_file();
3001
3002 #ifdef  USE_ISO
3003 cleanup3:
3004         free_irq(pdev->resource[2].start, udc);
3005 #endif
3006
3007 cleanup2:
3008         free_irq(pdev->resource[1].start, udc);
3009
3010 cleanup1:
3011         kfree (udc);
3012         udc = NULL;
3013
3014 cleanup0:
3015         if (xceiv)
3016                 usb_put_transceiver(xceiv);
3017
3018         if (cpu_is_omap16xx() || cpu_is_omap24xx() || cpu_is_omap7xx()) {
3019                 clk_disable(hhc_clk);
3020                 clk_disable(dc_clk);
3021                 clk_put(hhc_clk);
3022                 clk_put(dc_clk);
3023         }
3024
3025         release_mem_region(pdev->resource[0].start,
3026                         pdev->resource[0].end - pdev->resource[0].start + 1);
3027
3028         return status;
3029 }
3030
3031 static int __exit omap_udc_remove(struct platform_device *pdev)
3032 {
3033         DECLARE_COMPLETION_ONSTACK(done);
3034
3035         if (!udc)
3036                 return -ENODEV;
3037
3038         usb_del_gadget_udc(&udc->gadget);
3039         if (udc->driver)
3040                 return -EBUSY;
3041
3042         udc->done = &done;
3043
3044         pullup_disable(udc);
3045         if (udc->transceiver) {
3046                 usb_put_transceiver(udc->transceiver);
3047                 udc->transceiver = NULL;
3048         }
3049         omap_writew(0, UDC_SYSCON1);
3050
3051         remove_proc_file();
3052
3053 #ifdef  USE_ISO
3054         free_irq(pdev->resource[3].start, udc);
3055 #endif
3056         free_irq(pdev->resource[2].start, udc);
3057         free_irq(pdev->resource[1].start, udc);
3058
3059         if (udc->dc_clk) {
3060                 if (udc->clk_requested)
3061                         omap_udc_enable_clock(0);
3062                 clk_put(udc->hhc_clk);
3063                 clk_put(udc->dc_clk);
3064         }
3065
3066         release_mem_region(pdev->resource[0].start,
3067                         pdev->resource[0].end - pdev->resource[0].start + 1);
3068
3069         device_unregister(&udc->gadget.dev);
3070         wait_for_completion(&done);
3071
3072         return 0;
3073 }
3074
3075 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
3076  * system is forced into deep sleep
3077  *
3078  * REVISIT we should probably reject suspend requests when there's a host
3079  * session active, rather than disconnecting, at least on boards that can
3080  * report VBUS irqs (UDC_DEVSTAT.UDC_ATT).  And in any case, we need to
3081  * make host resumes and VBUS detection trigger OMAP wakeup events; that
3082  * may involve talking to an external transceiver (e.g. isp1301).
3083  */
3084
3085 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
3086 {
3087         u32     devstat;
3088
3089         devstat = omap_readw(UDC_DEVSTAT);
3090
3091         /* we're requesting 48 MHz clock if the pullup is enabled
3092          * (== we're attached to the host) and we're not suspended,
3093          * which would prevent entry to deep sleep...
3094          */
3095         if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
3096                 WARNING("session active; suspend requires disconnect\n");
3097                 omap_pullup(&udc->gadget, 0);
3098         }
3099
3100         return 0;
3101 }
3102
3103 static int omap_udc_resume(struct platform_device *dev)
3104 {
3105         DBG("resume + wakeup/SRP\n");
3106         omap_pullup(&udc->gadget, 1);
3107
3108         /* maybe the host would enumerate us if we nudged it */
3109         msleep(100);
3110         return omap_wakeup(&udc->gadget);
3111 }
3112
3113 /*-------------------------------------------------------------------------*/
3114
3115 static struct platform_driver udc_driver = {
3116         .remove         = __exit_p(omap_udc_remove),
3117         .suspend        = omap_udc_suspend,
3118         .resume         = omap_udc_resume,
3119         .driver         = {
3120                 .owner  = THIS_MODULE,
3121                 .name   = (char *) driver_name,
3122         },
3123 };
3124
3125 static int __init udc_init(void)
3126 {
3127         /* Disable DMA for omap7xx -- it doesn't work right. */
3128         if (cpu_is_omap7xx())
3129                 use_dma = 0;
3130
3131         INFO("%s, version: " DRIVER_VERSION
3132 #ifdef  USE_ISO
3133                 " (iso)"
3134 #endif
3135                 "%s\n", driver_desc,
3136                 use_dma ?  " (dma)" : "");
3137         return platform_driver_probe(&udc_driver, omap_udc_probe);
3138 }
3139 module_init(udc_init);
3140
3141 static void __exit udc_exit(void)
3142 {
3143         platform_driver_unregister(&udc_driver);
3144 }
3145 module_exit(udc_exit);
3146
3147 MODULE_DESCRIPTION(DRIVER_DESC);
3148 MODULE_LICENSE("GPL");
3149 MODULE_ALIAS("platform:omap_udc");