Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / pxa2xx_udc.c
blob6390c5726d81ce50eecfd9c2791064ed564fecae
1 /*
2 * linux/drivers/usb/gadget/pxa2xx_udc.c
3 * Intel PXA2xx and IXP4xx on-chip full speed USB device controllers
5 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
6 * Copyright (C) 2003 Robert Schwebel, Pengutronix
7 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
8 * Copyright (C) 2003 David Brownell
9 * Copyright (C) 2003 Joshua Wise
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #undef DEBUG
28 // #define VERBOSE DBG_VERBOSE
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/ioport.h>
34 #include <linux/types.h>
35 #include <linux/version.h>
36 #include <linux/errno.h>
37 #include <linux/delay.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/timer.h>
42 #include <linux/list.h>
43 #include <linux/interrupt.h>
44 #include <linux/proc_fs.h>
45 #include <linux/mm.h>
46 #include <linux/device.h>
47 #include <linux/dma-mapping.h>
49 #include <asm/byteorder.h>
50 #include <asm/dma.h>
51 #include <asm/io.h>
52 #include <asm/irq.h>
53 #include <asm/system.h>
54 #include <asm/mach-types.h>
55 #include <asm/unaligned.h>
56 #include <asm/hardware.h>
57 #include <asm/arch/pxa-regs.h>
59 #include <linux/usb_ch9.h>
60 #include <linux/usb_gadget.h>
62 #include <asm/arch/udc.h>
66 * This driver handles the USB Device Controller (UDC) in Intel's PXA 2xx
67 * series processors. The UDC for the IXP 4xx series is very similar.
68 * There are fifteen endpoints, in addition to ep0.
70 * Such controller drivers work with a gadget driver. The gadget driver
71 * returns descriptors, implements configuration and data protocols used
72 * by the host to interact with this device, and allocates endpoints to
73 * the different protocol interfaces. The controller driver virtualizes
74 * usb hardware so that the gadget drivers will be more portable.
76 * This UDC hardware wants to implement a bit too much USB protocol, so
77 * it constrains the sorts of USB configuration change events that work.
78 * The errata for these chips are misleading; some "fixed" bugs from
79 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
82 #define DRIVER_VERSION "14-Dec-2003"
83 #define DRIVER_DESC "PXA 2xx USB Device Controller driver"
86 static const char driver_name [] = "pxa2xx_udc";
88 static const char ep0name [] = "ep0";
91 // #define USE_DMA
92 // #define USE_OUT_DMA
93 // #define DISABLE_TEST_MODE
95 #ifdef CONFIG_ARCH_IXP4XX
96 #undef USE_DMA
98 /* cpu-specific register addresses are compiled in to this code */
99 #ifdef CONFIG_ARCH_PXA
100 #error "Can't configure both IXP and PXA"
101 #endif
103 #endif
105 #include "pxa2xx_udc.h"
108 #ifdef USE_DMA
109 static int use_dma = 1;
110 module_param(use_dma, bool, 0);
111 MODULE_PARM_DESC (use_dma, "true to use dma");
113 static void dma_nodesc_handler (int dmach, void *_ep, struct pt_regs *r);
114 static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req);
116 #ifdef USE_OUT_DMA
117 #define DMASTR " (dma support)"
118 #else
119 #define DMASTR " (dma in)"
120 #endif
122 #else /* !USE_DMA */
123 #define DMASTR " (pio only)"
124 #undef USE_OUT_DMA
125 #endif
127 #ifdef CONFIG_USB_PXA2XX_SMALL
128 #define SIZE_STR " (small)"
129 #else
130 #define SIZE_STR ""
131 #endif
133 #ifdef DISABLE_TEST_MODE
134 /* (mode == 0) == no undocumented chip tweaks
135 * (mode & 1) == double buffer bulk IN
136 * (mode & 2) == double buffer bulk OUT
137 * ... so mode = 3 (or 7, 15, etc) does it for both
139 static ushort fifo_mode = 0;
140 module_param(fifo_mode, ushort, 0);
141 MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
142 #endif
144 /* ---------------------------------------------------------------------------
145 * endpoint related parts of the api to the usb controller hardware,
146 * used by gadget driver; and the inner talker-to-hardware core.
147 * ---------------------------------------------------------------------------
150 static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
151 static void nuke (struct pxa2xx_ep *, int status);
153 static void pio_irq_enable(int bEndpointAddress)
155 bEndpointAddress &= 0xf;
156 if (bEndpointAddress < 8)
157 UICR0 &= ~(1 << bEndpointAddress);
158 else {
159 bEndpointAddress -= 8;
160 UICR1 &= ~(1 << bEndpointAddress);
164 static void pio_irq_disable(int bEndpointAddress)
166 bEndpointAddress &= 0xf;
167 if (bEndpointAddress < 8)
168 UICR0 |= 1 << bEndpointAddress;
169 else {
170 bEndpointAddress -= 8;
171 UICR1 |= 1 << bEndpointAddress;
175 /* The UDCCR reg contains mask and interrupt status bits,
176 * so using '|=' isn't safe as it may ack an interrupt.
178 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
180 static inline void udc_set_mask_UDCCR(int mask)
182 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
185 static inline void udc_clear_mask_UDCCR(int mask)
187 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
190 static inline void udc_ack_int_UDCCR(int mask)
192 /* udccr contains the bits we dont want to change */
193 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
195 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
199 * endpoint enable/disable
201 * we need to verify the descriptors used to enable endpoints. since pxa2xx
202 * endpoint configurations are fixed, and are pretty much always enabled,
203 * there's not a lot to manage here.
205 * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
206 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
207 * for a single interface (with only the default altsetting) and for gadget
208 * drivers that don't halt endpoints (not reset by set_interface). that also
209 * means that if you use ISO, you must violate the USB spec rule that all
210 * iso endpoints must be in non-default altsettings.
212 static int pxa2xx_ep_enable (struct usb_ep *_ep,
213 const struct usb_endpoint_descriptor *desc)
215 struct pxa2xx_ep *ep;
216 struct pxa2xx_udc *dev;
218 ep = container_of (_ep, struct pxa2xx_ep, ep);
219 if (!_ep || !desc || ep->desc || _ep->name == ep0name
220 || desc->bDescriptorType != USB_DT_ENDPOINT
221 || ep->bEndpointAddress != desc->bEndpointAddress
222 || ep->fifo_size < le16_to_cpu
223 (desc->wMaxPacketSize)) {
224 DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
225 return -EINVAL;
228 /* xfer types must match, except that interrupt ~= bulk */
229 if (ep->bmAttributes != desc->bmAttributes
230 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
231 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
232 DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
233 return -EINVAL;
236 /* hardware _could_ do smaller, but driver doesn't */
237 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
238 && le16_to_cpu (desc->wMaxPacketSize)
239 != BULK_FIFO_SIZE)
240 || !desc->wMaxPacketSize) {
241 DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
242 return -ERANGE;
245 dev = ep->dev;
246 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
247 DMSG("%s, bogus device state\n", __FUNCTION__);
248 return -ESHUTDOWN;
251 ep->desc = desc;
252 ep->dma = -1;
253 ep->stopped = 0;
254 ep->pio_irqs = ep->dma_irqs = 0;
255 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
257 /* flush fifo (mostly for OUT buffers) */
258 pxa2xx_ep_fifo_flush (_ep);
260 /* ... reset halt state too, if we could ... */
262 #ifdef USE_DMA
263 /* for (some) bulk and ISO endpoints, try to get a DMA channel and
264 * bind it to the endpoint. otherwise use PIO.
266 switch (ep->bmAttributes) {
267 case USB_ENDPOINT_XFER_ISOC:
268 if (le16_to_cpu(desc->wMaxPacketSize) % 32)
269 break;
270 // fall through
271 case USB_ENDPOINT_XFER_BULK:
272 if (!use_dma || !ep->reg_drcmr)
273 break;
274 ep->dma = pxa_request_dma ((char *)_ep->name,
275 (le16_to_cpu (desc->wMaxPacketSize) > 64)
276 ? DMA_PRIO_MEDIUM /* some iso */
277 : DMA_PRIO_LOW,
278 dma_nodesc_handler, ep);
279 if (ep->dma >= 0) {
280 *ep->reg_drcmr = DRCMR_MAPVLD | ep->dma;
281 DMSG("%s using dma%d\n", _ep->name, ep->dma);
284 #endif
286 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
287 return 0;
290 static int pxa2xx_ep_disable (struct usb_ep *_ep)
292 struct pxa2xx_ep *ep;
294 ep = container_of (_ep, struct pxa2xx_ep, ep);
295 if (!_ep || !ep->desc) {
296 DMSG("%s, %s not enabled\n", __FUNCTION__,
297 _ep ? ep->ep.name : NULL);
298 return -EINVAL;
300 nuke (ep, -ESHUTDOWN);
302 #ifdef USE_DMA
303 if (ep->dma >= 0) {
304 *ep->reg_drcmr = 0;
305 pxa_free_dma (ep->dma);
306 ep->dma = -1;
308 #endif
310 /* flush fifo (mostly for IN buffers) */
311 pxa2xx_ep_fifo_flush (_ep);
313 ep->desc = NULL;
314 ep->stopped = 1;
316 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
317 return 0;
320 /*-------------------------------------------------------------------------*/
322 /* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
323 * must still pass correctly initialized endpoints, since other controller
324 * drivers may care about how it's currently set up (dma issues etc).
328 * pxa2xx_ep_alloc_request - allocate a request data structure
330 static struct usb_request *
331 pxa2xx_ep_alloc_request (struct usb_ep *_ep, int gfp_flags)
333 struct pxa2xx_request *req;
335 req = kmalloc (sizeof *req, gfp_flags);
336 if (!req)
337 return NULL;
339 memset (req, 0, sizeof *req);
340 INIT_LIST_HEAD (&req->queue);
341 return &req->req;
346 * pxa2xx_ep_free_request - deallocate a request data structure
348 static void
349 pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
351 struct pxa2xx_request *req;
353 req = container_of (_req, struct pxa2xx_request, req);
354 WARN_ON (!list_empty (&req->queue));
355 kfree(req);
359 /* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
360 * no device-affinity and the heap works perfectly well for i/o buffers.
361 * It wastes much less memory than dma_alloc_coherent() would, and even
362 * prevents cacheline (32 bytes wide) sharing problems.
364 static void *
365 pxa2xx_ep_alloc_buffer(struct usb_ep *_ep, unsigned bytes,
366 dma_addr_t *dma, int gfp_flags)
368 char *retval;
370 retval = kmalloc (bytes, gfp_flags & ~(__GFP_DMA|__GFP_HIGHMEM));
371 if (retval)
372 #ifdef USE_DMA
373 *dma = virt_to_bus (retval);
374 #else
375 *dma = (dma_addr_t)~0;
376 #endif
377 return retval;
380 static void
381 pxa2xx_ep_free_buffer(struct usb_ep *_ep, void *buf, dma_addr_t dma,
382 unsigned bytes)
384 kfree (buf);
387 /*-------------------------------------------------------------------------*/
390 * done - retire a request; caller blocked irqs
392 static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
394 unsigned stopped = ep->stopped;
396 list_del_init(&req->queue);
398 if (likely (req->req.status == -EINPROGRESS))
399 req->req.status = status;
400 else
401 status = req->req.status;
403 if (status && status != -ESHUTDOWN)
404 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
405 ep->ep.name, &req->req, status,
406 req->req.actual, req->req.length);
408 /* don't modify queue heads during completion callback */
409 ep->stopped = 1;
410 req->req.complete(&ep->ep, &req->req);
411 ep->stopped = stopped;
415 static inline void ep0_idle (struct pxa2xx_udc *dev)
417 dev->ep0state = EP0_IDLE;
420 static int
421 write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
423 u8 *buf;
424 unsigned length, count;
426 buf = req->req.buf + req->req.actual;
427 prefetch(buf);
429 /* how big will this packet be? */
430 length = min(req->req.length - req->req.actual, max);
431 req->req.actual += length;
433 count = length;
434 while (likely(count--))
435 *uddr = *buf++;
437 return length;
441 * write to an IN endpoint fifo, as many packets as possible.
442 * irqs will use this to write the rest later.
443 * caller guarantees at least one packet buffer is ready (or a zlp).
445 static int
446 write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
448 unsigned max;
450 max = le16_to_cpu(ep->desc->wMaxPacketSize);
451 do {
452 unsigned count;
453 int is_last, is_short;
455 count = write_packet(ep->reg_uddr, req, max);
457 /* last packet is usually short (or a zlp) */
458 if (unlikely (count != max))
459 is_last = is_short = 1;
460 else {
461 if (likely(req->req.length != req->req.actual)
462 || req->req.zero)
463 is_last = 0;
464 else
465 is_last = 1;
466 /* interrupt/iso maxpacket may not fill the fifo */
467 is_short = unlikely (max < ep->fifo_size);
470 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
471 ep->ep.name, count,
472 is_last ? "/L" : "", is_short ? "/S" : "",
473 req->req.length - req->req.actual, req);
475 /* let loose that packet. maybe try writing another one,
476 * double buffering might work. TSP, TPC, and TFS
477 * bit values are the same for all normal IN endpoints.
479 *ep->reg_udccs = UDCCS_BI_TPC;
480 if (is_short)
481 *ep->reg_udccs = UDCCS_BI_TSP;
483 /* requests complete when all IN data is in the FIFO */
484 if (is_last) {
485 done (ep, req, 0);
486 if (list_empty(&ep->queue) || unlikely(ep->dma >= 0)) {
487 pio_irq_disable (ep->bEndpointAddress);
488 #ifdef USE_DMA
489 /* unaligned data and zlps couldn't use dma */
490 if (unlikely(!list_empty(&ep->queue))) {
491 req = list_entry(ep->queue.next,
492 struct pxa2xx_request, queue);
493 kick_dma(ep,req);
494 return 0;
496 #endif
498 return 1;
501 // TODO experiment: how robust can fifo mode tweaking be?
502 // double buffering is off in the default fifo mode, which
503 // prevents TFS from being set here.
505 } while (*ep->reg_udccs & UDCCS_BI_TFS);
506 return 0;
509 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
510 * ep0 data stage. these chips want very simple state transitions.
512 static inline
513 void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
515 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
516 USIR0 = USIR0_IR0;
517 dev->req_pending = 0;
518 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
519 __FUNCTION__, tag, UDCCS0, flags);
522 static int
523 write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
525 unsigned count;
526 int is_short;
528 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
529 ep->dev->stats.write.bytes += count;
531 /* last packet "must be" short (or a zlp) */
532 is_short = (count != EP0_FIFO_SIZE);
534 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
535 req->req.length - req->req.actual, req);
537 if (unlikely (is_short)) {
538 if (ep->dev->req_pending)
539 ep0start(ep->dev, UDCCS0_IPR, "short IN");
540 else
541 UDCCS0 = UDCCS0_IPR;
543 count = req->req.length;
544 done (ep, req, 0);
545 ep0_idle(ep->dev);
546 #if 1
547 /* This seems to get rid of lost status irqs in some cases:
548 * host responds quickly, or next request involves config
549 * change automagic, or should have been hidden, or ...
551 * FIXME get rid of all udelays possible...
553 if (count >= EP0_FIFO_SIZE) {
554 count = 100;
555 do {
556 if ((UDCCS0 & UDCCS0_OPR) != 0) {
557 /* clear OPR, generate ack */
558 UDCCS0 = UDCCS0_OPR;
559 break;
561 count--;
562 udelay(1);
563 } while (count);
565 #endif
566 } else if (ep->dev->req_pending)
567 ep0start(ep->dev, 0, "IN");
568 return is_short;
573 * read_fifo - unload packet(s) from the fifo we use for usb OUT
574 * transfers and put them into the request. caller should have made
575 * sure there's at least one packet ready.
577 * returns true if the request completed because of short packet or the
578 * request buffer having filled (and maybe overran till end-of-packet).
580 static int
581 read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
583 for (;;) {
584 u32 udccs;
585 u8 *buf;
586 unsigned bufferspace, count, is_short;
588 /* make sure there's a packet in the FIFO.
589 * UDCCS_{BO,IO}_RPC are all the same bit value.
590 * UDCCS_{BO,IO}_RNE are all the same bit value.
592 udccs = *ep->reg_udccs;
593 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
594 break;
595 buf = req->req.buf + req->req.actual;
596 prefetchw(buf);
597 bufferspace = req->req.length - req->req.actual;
599 /* read all bytes from this packet */
600 if (likely (udccs & UDCCS_BO_RNE)) {
601 count = 1 + (0x0ff & *ep->reg_ubcr);
602 req->req.actual += min (count, bufferspace);
603 } else /* zlp */
604 count = 0;
605 is_short = (count < ep->ep.maxpacket);
606 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
607 ep->ep.name, udccs, count,
608 is_short ? "/S" : "",
609 req, req->req.actual, req->req.length);
610 while (likely (count-- != 0)) {
611 u8 byte = (u8) *ep->reg_uddr;
613 if (unlikely (bufferspace == 0)) {
614 /* this happens when the driver's buffer
615 * is smaller than what the host sent.
616 * discard the extra data.
618 if (req->req.status != -EOVERFLOW)
619 DMSG("%s overflow %d\n",
620 ep->ep.name, count);
621 req->req.status = -EOVERFLOW;
622 } else {
623 *buf++ = byte;
624 bufferspace--;
627 *ep->reg_udccs = UDCCS_BO_RPC;
628 /* RPC/RSP/RNE could now reflect the other packet buffer */
630 /* iso is one request per packet */
631 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
632 if (udccs & UDCCS_IO_ROF)
633 req->req.status = -EHOSTUNREACH;
634 /* more like "is_done" */
635 is_short = 1;
638 /* completion */
639 if (is_short || req->req.actual == req->req.length) {
640 done (ep, req, 0);
641 if (list_empty(&ep->queue))
642 pio_irq_disable (ep->bEndpointAddress);
643 return 1;
646 /* finished that packet. the next one may be waiting... */
648 return 0;
652 * special ep0 version of the above. no UBCR0 or double buffering; status
653 * handshaking is magic. most device protocols don't need control-OUT.
654 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
655 * protocols do use them.
657 static int
658 read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
660 u8 *buf, byte;
661 unsigned bufferspace;
663 buf = req->req.buf + req->req.actual;
664 bufferspace = req->req.length - req->req.actual;
666 while (UDCCS0 & UDCCS0_RNE) {
667 byte = (u8) UDDR0;
669 if (unlikely (bufferspace == 0)) {
670 /* this happens when the driver's buffer
671 * is smaller than what the host sent.
672 * discard the extra data.
674 if (req->req.status != -EOVERFLOW)
675 DMSG("%s overflow\n", ep->ep.name);
676 req->req.status = -EOVERFLOW;
677 } else {
678 *buf++ = byte;
679 req->req.actual++;
680 bufferspace--;
684 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
686 /* completion */
687 if (req->req.actual >= req->req.length)
688 return 1;
690 /* finished that packet. the next one may be waiting... */
691 return 0;
694 #ifdef USE_DMA
696 #define MAX_IN_DMA ((DCMD_LENGTH + 1) - BULK_FIFO_SIZE)
698 static void
699 start_dma_nodesc(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int is_in)
701 u32 dcmd = req->req.length;
702 u32 buf = req->req.dma;
703 u32 fifo = io_v2p ((u32)ep->reg_uddr);
705 /* caller guarantees there's a packet or more remaining
706 * - IN may end with a short packet (TSP set separately),
707 * - OUT is always full length
709 buf += req->req.actual;
710 dcmd -= req->req.actual;
711 ep->dma_fixup = 0;
713 /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
714 DCSR(ep->dma) = DCSR_NODESC;
715 if (is_in) {
716 DSADR(ep->dma) = buf;
717 DTADR(ep->dma) = fifo;
718 if (dcmd > MAX_IN_DMA)
719 dcmd = MAX_IN_DMA;
720 else
721 ep->dma_fixup = (dcmd % ep->ep.maxpacket) != 0;
722 dcmd |= DCMD_BURST32 | DCMD_WIDTH1
723 | DCMD_FLOWTRG | DCMD_INCSRCADDR;
724 } else {
725 #ifdef USE_OUT_DMA
726 DSADR(ep->dma) = fifo;
727 DTADR(ep->dma) = buf;
728 if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
729 dcmd = ep->ep.maxpacket;
730 dcmd |= DCMD_BURST32 | DCMD_WIDTH1
731 | DCMD_FLOWSRC | DCMD_INCTRGADDR;
732 #endif
734 DCMD(ep->dma) = dcmd;
735 DCSR(ep->dma) = DCSR_RUN | DCSR_NODESC
736 | (unlikely(is_in)
737 ? DCSR_STOPIRQEN /* use dma_nodesc_handler() */
738 : 0); /* use handle_ep() */
741 static void kick_dma(struct pxa2xx_ep *ep, struct pxa2xx_request *req)
743 int is_in = ep->bEndpointAddress & USB_DIR_IN;
745 if (is_in) {
746 /* unaligned tx buffers and zlps only work with PIO */
747 if ((req->req.dma & 0x0f) != 0
748 || unlikely((req->req.length - req->req.actual)
749 == 0)) {
750 pio_irq_enable(ep->bEndpointAddress);
751 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0)
752 (void) write_fifo(ep, req);
753 } else {
754 start_dma_nodesc(ep, req, USB_DIR_IN);
756 } else {
757 if ((req->req.length - req->req.actual) < ep->ep.maxpacket) {
758 DMSG("%s short dma read...\n", ep->ep.name);
759 /* we're always set up for pio out */
760 read_fifo (ep, req);
761 } else {
762 *ep->reg_udccs = UDCCS_BO_DME
763 | (*ep->reg_udccs & UDCCS_BO_FST);
764 start_dma_nodesc(ep, req, USB_DIR_OUT);
769 static void cancel_dma(struct pxa2xx_ep *ep)
771 struct pxa2xx_request *req;
772 u32 tmp;
774 if (DCSR(ep->dma) == 0 || list_empty(&ep->queue))
775 return;
777 DCSR(ep->dma) = 0;
778 while ((DCSR(ep->dma) & DCSR_STOPSTATE) == 0)
779 cpu_relax();
781 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
782 tmp = DCMD(ep->dma) & DCMD_LENGTH;
783 req->req.actual = req->req.length - (tmp & DCMD_LENGTH);
785 /* the last tx packet may be incomplete, so flush the fifo.
786 * FIXME correct req.actual if we can
788 if (ep->bEndpointAddress & USB_DIR_IN)
789 *ep->reg_udccs = UDCCS_BI_FTF;
792 /* dma channel stopped ... normal tx end (IN), or on error (IN/OUT) */
793 static void dma_nodesc_handler(int dmach, void *_ep, struct pt_regs *r)
795 struct pxa2xx_ep *ep = _ep;
796 struct pxa2xx_request *req;
797 u32 tmp, completed;
799 local_irq_disable();
801 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
803 ep->dma_irqs++;
804 ep->dev->stats.irqs++;
805 HEX_DISPLAY(ep->dev->stats.irqs);
807 /* ack/clear */
808 tmp = DCSR(ep->dma);
809 DCSR(ep->dma) = tmp;
810 if ((tmp & DCSR_STOPSTATE) == 0
811 || (DDADR(ep->dma) & DDADR_STOP) != 0) {
812 DBG(DBG_VERBOSE, "%s, dcsr %08x ddadr %08x\n",
813 ep->ep.name, DCSR(ep->dma), DDADR(ep->dma));
814 goto done;
816 DCSR(ep->dma) = 0; /* clear DCSR_STOPSTATE */
818 /* update transfer status */
819 completed = tmp & DCSR_BUSERR;
820 if (ep->bEndpointAddress & USB_DIR_IN)
821 tmp = DSADR(ep->dma);
822 else
823 tmp = DTADR(ep->dma);
824 req->req.actual = tmp - req->req.dma;
826 /* FIXME seems we sometimes see partial transfers... */
828 if (unlikely(completed != 0))
829 req->req.status = -EIO;
830 else if (req->req.actual) {
831 /* these registers have zeroes in low bits; they miscount
832 * some (end-of-transfer) short packets: tx 14 as tx 12
834 if (ep->dma_fixup)
835 req->req.actual = min(req->req.actual + 3,
836 req->req.length);
838 tmp = (req->req.length - req->req.actual);
839 completed = (tmp == 0);
840 if (completed && (ep->bEndpointAddress & USB_DIR_IN)) {
842 /* maybe validate final short packet ... */
843 if ((req->req.actual % ep->ep.maxpacket) != 0)
844 *ep->reg_udccs = UDCCS_BI_TSP/*|UDCCS_BI_TPC*/;
846 /* ... or zlp, using pio fallback */
847 else if (ep->bmAttributes == USB_ENDPOINT_XFER_BULK
848 && req->req.zero) {
849 DMSG("%s zlp terminate ...\n", ep->ep.name);
850 completed = 0;
855 if (likely(completed)) {
856 done(ep, req, 0);
858 /* maybe re-activate after completion */
859 if (ep->stopped || list_empty(&ep->queue))
860 goto done;
861 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
863 kick_dma(ep, req);
864 done:
865 local_irq_enable();
868 #endif
870 /*-------------------------------------------------------------------------*/
872 static int
873 pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
875 struct pxa2xx_request *req;
876 struct pxa2xx_ep *ep;
877 struct pxa2xx_udc *dev;
878 unsigned long flags;
880 req = container_of(_req, struct pxa2xx_request, req);
881 if (unlikely (!_req || !_req->complete || !_req->buf
882 || !list_empty(&req->queue))) {
883 DMSG("%s, bad params\n", __FUNCTION__);
884 return -EINVAL;
887 ep = container_of(_ep, struct pxa2xx_ep, ep);
888 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
889 DMSG("%s, bad ep\n", __FUNCTION__);
890 return -EINVAL;
893 dev = ep->dev;
894 if (unlikely (!dev->driver
895 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
896 DMSG("%s, bogus device state\n", __FUNCTION__);
897 return -ESHUTDOWN;
900 /* iso is always one packet per request, that's the only way
901 * we can report per-packet status. that also helps with dma.
903 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
904 && req->req.length > le16_to_cpu
905 (ep->desc->wMaxPacketSize)))
906 return -EMSGSIZE;
908 #ifdef USE_DMA
909 // FIXME caller may already have done the dma mapping
910 if (ep->dma >= 0) {
911 _req->dma = dma_map_single(dev->dev,
912 _req->buf, _req->length,
913 ((ep->bEndpointAddress & USB_DIR_IN) != 0)
914 ? DMA_TO_DEVICE
915 : DMA_FROM_DEVICE);
917 #endif
919 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
920 _ep->name, _req, _req->length, _req->buf);
922 local_irq_save(flags);
924 _req->status = -EINPROGRESS;
925 _req->actual = 0;
927 /* kickstart this i/o queue? */
928 if (list_empty(&ep->queue) && !ep->stopped) {
929 if (ep->desc == 0 /* ep0 */) {
930 unsigned length = _req->length;
932 switch (dev->ep0state) {
933 case EP0_IN_DATA_PHASE:
934 dev->stats.write.ops++;
935 if (write_ep0_fifo(ep, req))
936 req = NULL;
937 break;
939 case EP0_OUT_DATA_PHASE:
940 dev->stats.read.ops++;
941 /* messy ... */
942 if (dev->req_config) {
943 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
944 dev->has_cfr ? "" : " raced");
945 if (dev->has_cfr)
946 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
947 |UDCCFR_MB1;
948 done(ep, req, 0);
949 dev->ep0state = EP0_END_XFER;
950 local_irq_restore (flags);
951 return 0;
953 if (dev->req_pending)
954 ep0start(dev, UDCCS0_IPR, "OUT");
955 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
956 && read_ep0_fifo(ep, req))) {
957 ep0_idle(dev);
958 done(ep, req, 0);
959 req = NULL;
961 break;
963 default:
964 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
965 local_irq_restore (flags);
966 return -EL2HLT;
968 #ifdef USE_DMA
969 /* either start dma or prime pio pump */
970 } else if (ep->dma >= 0) {
971 kick_dma(ep, req);
972 #endif
973 /* can the FIFO can satisfy the request immediately? */
974 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0
975 && (*ep->reg_udccs & UDCCS_BI_TFS) != 0
976 && write_fifo(ep, req)) {
977 req = NULL;
978 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
979 && read_fifo(ep, req)) {
980 req = NULL;
983 if (likely (req && ep->desc) && ep->dma < 0)
984 pio_irq_enable(ep->bEndpointAddress);
987 /* pio or dma irq handler advances the queue. */
988 if (likely (req != 0))
989 list_add_tail(&req->queue, &ep->queue);
990 local_irq_restore(flags);
992 return 0;
997 * nuke - dequeue ALL requests
999 static void nuke(struct pxa2xx_ep *ep, int status)
1001 struct pxa2xx_request *req;
1003 /* called with irqs blocked */
1004 #ifdef USE_DMA
1005 if (ep->dma >= 0 && !ep->stopped)
1006 cancel_dma(ep);
1007 #endif
1008 while (!list_empty(&ep->queue)) {
1009 req = list_entry(ep->queue.next,
1010 struct pxa2xx_request,
1011 queue);
1012 done(ep, req, status);
1014 if (ep->desc)
1015 pio_irq_disable (ep->bEndpointAddress);
1019 /* dequeue JUST ONE request */
1020 static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1022 struct pxa2xx_ep *ep;
1023 struct pxa2xx_request *req;
1024 unsigned long flags;
1026 ep = container_of(_ep, struct pxa2xx_ep, ep);
1027 if (!_ep || ep->ep.name == ep0name)
1028 return -EINVAL;
1030 local_irq_save(flags);
1032 /* make sure it's actually queued on this endpoint */
1033 list_for_each_entry (req, &ep->queue, queue) {
1034 if (&req->req == _req)
1035 break;
1037 if (&req->req != _req) {
1038 local_irq_restore(flags);
1039 return -EINVAL;
1042 #ifdef USE_DMA
1043 if (ep->dma >= 0 && ep->queue.next == &req->queue && !ep->stopped) {
1044 cancel_dma(ep);
1045 done(ep, req, -ECONNRESET);
1046 /* restart i/o */
1047 if (!list_empty(&ep->queue)) {
1048 req = list_entry(ep->queue.next,
1049 struct pxa2xx_request, queue);
1050 kick_dma(ep, req);
1052 } else
1053 #endif
1054 done(ep, req, -ECONNRESET);
1056 local_irq_restore(flags);
1057 return 0;
1060 /*-------------------------------------------------------------------------*/
1062 static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
1064 struct pxa2xx_ep *ep;
1065 unsigned long flags;
1067 ep = container_of(_ep, struct pxa2xx_ep, ep);
1068 if (unlikely (!_ep
1069 || (!ep->desc && ep->ep.name != ep0name))
1070 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
1071 DMSG("%s, bad ep\n", __FUNCTION__);
1072 return -EINVAL;
1074 if (value == 0) {
1075 /* this path (reset toggle+halt) is needed to implement
1076 * SET_INTERFACE on normal hardware. but it can't be
1077 * done from software on the PXA UDC, and the hardware
1078 * forgets to do it as part of SET_INTERFACE automagic.
1080 DMSG("only host can clear %s halt\n", _ep->name);
1081 return -EROFS;
1084 local_irq_save(flags);
1086 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
1087 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
1088 || !list_empty(&ep->queue))) {
1089 local_irq_restore(flags);
1090 return -EAGAIN;
1093 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
1094 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
1096 /* ep0 needs special care */
1097 if (!ep->desc) {
1098 start_watchdog(ep->dev);
1099 ep->dev->req_pending = 0;
1100 ep->dev->ep0state = EP0_STALL;
1102 /* and bulk/intr endpoints like dropping stalls too */
1103 } else {
1104 unsigned i;
1105 for (i = 0; i < 1000; i += 20) {
1106 if (*ep->reg_udccs & UDCCS_BI_SST)
1107 break;
1108 udelay(20);
1111 local_irq_restore(flags);
1113 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
1114 return 0;
1117 static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
1119 struct pxa2xx_ep *ep;
1121 ep = container_of(_ep, struct pxa2xx_ep, ep);
1122 if (!_ep) {
1123 DMSG("%s, bad ep\n", __FUNCTION__);
1124 return -ENODEV;
1126 /* pxa can't report unclaimed bytes from IN fifos */
1127 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
1128 return -EOPNOTSUPP;
1129 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
1130 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
1131 return 0;
1132 else
1133 return (*ep->reg_ubcr & 0xfff) + 1;
1136 static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
1138 struct pxa2xx_ep *ep;
1140 ep = container_of(_ep, struct pxa2xx_ep, ep);
1141 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
1142 DMSG("%s, bad ep\n", __FUNCTION__);
1143 return;
1146 /* toggle and halt bits stay unchanged */
1148 /* for OUT, just read and discard the FIFO contents. */
1149 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
1150 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
1151 (void) *ep->reg_uddr;
1152 return;
1155 /* most IN status is the same, but ISO can't stall */
1156 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
1157 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1158 ? 0 : UDCCS_BI_SST;
1162 static struct usb_ep_ops pxa2xx_ep_ops = {
1163 .enable = pxa2xx_ep_enable,
1164 .disable = pxa2xx_ep_disable,
1166 .alloc_request = pxa2xx_ep_alloc_request,
1167 .free_request = pxa2xx_ep_free_request,
1169 .alloc_buffer = pxa2xx_ep_alloc_buffer,
1170 .free_buffer = pxa2xx_ep_free_buffer,
1172 .queue = pxa2xx_ep_queue,
1173 .dequeue = pxa2xx_ep_dequeue,
1175 .set_halt = pxa2xx_ep_set_halt,
1176 .fifo_status = pxa2xx_ep_fifo_status,
1177 .fifo_flush = pxa2xx_ep_fifo_flush,
1181 /* ---------------------------------------------------------------------------
1182 * device-scoped parts of the api to the usb controller hardware
1183 * ---------------------------------------------------------------------------
1186 static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
1188 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
1191 static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
1193 /* host may not have enabled remote wakeup */
1194 if ((UDCCS0 & UDCCS0_DRWF) == 0)
1195 return -EHOSTUNREACH;
1196 udc_set_mask_UDCCR(UDCCR_RSM);
1197 return 0;
1200 static void stop_activity(struct pxa2xx_udc *, struct usb_gadget_driver *);
1201 static void udc_enable (struct pxa2xx_udc *);
1202 static void udc_disable(struct pxa2xx_udc *);
1204 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
1205 * in active use.
1207 static int pullup(struct pxa2xx_udc *udc, int is_active)
1209 is_active = is_active && udc->vbus && udc->pullup;
1210 DMSG("%s\n", is_active ? "active" : "inactive");
1211 if (is_active)
1212 udc_enable(udc);
1213 else {
1214 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1215 DMSG("disconnect %s\n", udc->driver
1216 ? udc->driver->driver.name
1217 : "(no driver)");
1218 stop_activity(udc, udc->driver);
1220 udc_disable(udc);
1222 return 0;
1225 /* VBUS reporting logically comes from a transceiver */
1226 static int pxa2xx_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1228 struct pxa2xx_udc *udc;
1230 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
1231 udc->vbus = is_active = (is_active != 0);
1232 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
1233 pullup(udc, is_active);
1234 return 0;
1237 /* drivers may have software control over D+ pullup */
1238 static int pxa2xx_udc_pullup(struct usb_gadget *_gadget, int is_active)
1240 struct pxa2xx_udc *udc;
1242 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
1244 /* not all boards support pullup control */
1245 if (!udc->mach->udc_command)
1246 return -EOPNOTSUPP;
1248 is_active = (is_active != 0);
1249 udc->pullup = is_active;
1250 pullup(udc, is_active);
1251 return 0;
1254 static const struct usb_gadget_ops pxa2xx_udc_ops = {
1255 .get_frame = pxa2xx_udc_get_frame,
1256 .wakeup = pxa2xx_udc_wakeup,
1257 .vbus_session = pxa2xx_udc_vbus_session,
1258 .pullup = pxa2xx_udc_pullup,
1260 // .vbus_draw ... boards may consume current from VBUS, up to
1261 // 100-500mA based on config. the 500uA suspend ceiling means
1262 // that exclusively vbus-powered PXA designs violate USB specs.
1265 /*-------------------------------------------------------------------------*/
1267 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1269 static const char proc_node_name [] = "driver/udc";
1271 static int
1272 udc_proc_read(char *page, char **start, off_t off, int count,
1273 int *eof, void *_dev)
1275 char *buf = page;
1276 struct pxa2xx_udc *dev = _dev;
1277 char *next = buf;
1278 unsigned size = count;
1279 unsigned long flags;
1280 int i, t;
1281 u32 tmp;
1283 if (off != 0)
1284 return 0;
1286 local_irq_save(flags);
1288 /* basic device status */
1289 t = scnprintf(next, size, DRIVER_DESC "\n"
1290 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1291 driver_name, DRIVER_VERSION SIZE_STR DMASTR,
1292 dev->driver ? dev->driver->driver.name : "(none)",
1293 is_usb_connected() ? "full speed" : "disconnected");
1294 size -= t;
1295 next += t;
1297 /* registers for device and ep0 */
1298 t = scnprintf(next, size,
1299 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1300 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1301 size -= t;
1302 next += t;
1304 tmp = UDCCR;
1305 t = scnprintf(next, size,
1306 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1307 (tmp & UDCCR_REM) ? " rem" : "",
1308 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1309 (tmp & UDCCR_SRM) ? " srm" : "",
1310 (tmp & UDCCR_SUSIR) ? " susir" : "",
1311 (tmp & UDCCR_RESIR) ? " resir" : "",
1312 (tmp & UDCCR_RSM) ? " rsm" : "",
1313 (tmp & UDCCR_UDA) ? " uda" : "",
1314 (tmp & UDCCR_UDE) ? " ude" : "");
1315 size -= t;
1316 next += t;
1318 tmp = UDCCS0;
1319 t = scnprintf(next, size,
1320 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1321 (tmp & UDCCS0_SA) ? " sa" : "",
1322 (tmp & UDCCS0_RNE) ? " rne" : "",
1323 (tmp & UDCCS0_FST) ? " fst" : "",
1324 (tmp & UDCCS0_SST) ? " sst" : "",
1325 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1326 (tmp & UDCCS0_FTF) ? " ftf" : "",
1327 (tmp & UDCCS0_IPR) ? " ipr" : "",
1328 (tmp & UDCCS0_OPR) ? " opr" : "");
1329 size -= t;
1330 next += t;
1332 if (dev->has_cfr) {
1333 tmp = UDCCFR;
1334 t = scnprintf(next, size,
1335 "udccfr %02X =%s%s\n", tmp,
1336 (tmp & UDCCFR_AREN) ? " aren" : "",
1337 (tmp & UDCCFR_ACM) ? " acm" : "");
1338 size -= t;
1339 next += t;
1342 if (!is_usb_connected() || !dev->driver)
1343 goto done;
1345 t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1346 dev->stats.write.bytes, dev->stats.write.ops,
1347 dev->stats.read.bytes, dev->stats.read.ops,
1348 dev->stats.irqs);
1349 size -= t;
1350 next += t;
1352 /* dump endpoint queues */
1353 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1354 struct pxa2xx_ep *ep = &dev->ep [i];
1355 struct pxa2xx_request *req;
1356 int t;
1358 if (i != 0) {
1359 const struct usb_endpoint_descriptor *d;
1361 d = ep->desc;
1362 if (!d)
1363 continue;
1364 tmp = *dev->ep [i].reg_udccs;
1365 t = scnprintf(next, size,
1366 "%s max %d %s udccs %02x irqs %lu/%lu\n",
1367 ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
1368 (ep->dma >= 0) ? "dma" : "pio", tmp,
1369 ep->pio_irqs, ep->dma_irqs);
1370 /* TODO translate all five groups of udccs bits! */
1372 } else /* ep0 should only have one transfer queued */
1373 t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1374 ep->pio_irqs);
1375 if (t <= 0 || t > size)
1376 goto done;
1377 size -= t;
1378 next += t;
1380 if (list_empty(&ep->queue)) {
1381 t = scnprintf(next, size, "\t(nothing queued)\n");
1382 if (t <= 0 || t > size)
1383 goto done;
1384 size -= t;
1385 next += t;
1386 continue;
1388 list_for_each_entry(req, &ep->queue, queue) {
1389 #ifdef USE_DMA
1390 if (ep->dma >= 0 && req->queue.prev == &ep->queue)
1391 t = scnprintf(next, size,
1392 "\treq %p len %d/%d "
1393 "buf %p (dma%d dcmd %08x)\n",
1394 &req->req, req->req.actual,
1395 req->req.length, req->req.buf,
1396 ep->dma, DCMD(ep->dma)
1397 // low 13 bits == bytes-to-go
1399 else
1400 #endif
1401 t = scnprintf(next, size,
1402 "\treq %p len %d/%d buf %p\n",
1403 &req->req, req->req.actual,
1404 req->req.length, req->req.buf);
1405 if (t <= 0 || t > size)
1406 goto done;
1407 size -= t;
1408 next += t;
1412 done:
1413 local_irq_restore(flags);
1414 *eof = 1;
1415 return count - size;
1418 #define create_proc_files() \
1419 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1420 #define remove_proc_files() \
1421 remove_proc_entry(proc_node_name, NULL)
1423 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1425 #define create_proc_files() do {} while (0)
1426 #define remove_proc_files() do {} while (0)
1428 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1430 /* "function" sysfs attribute */
1431 static ssize_t
1432 show_function (struct device *_dev, char *buf)
1434 struct pxa2xx_udc *dev = dev_get_drvdata (_dev);
1436 if (!dev->driver
1437 || !dev->driver->function
1438 || strlen (dev->driver->function) > PAGE_SIZE)
1439 return 0;
1440 return scnprintf (buf, PAGE_SIZE, "%s\n", dev->driver->function);
1442 static DEVICE_ATTR (function, S_IRUGO, show_function, NULL);
1444 /*-------------------------------------------------------------------------*/
1447 * udc_disable - disable USB device controller
1449 static void udc_disable(struct pxa2xx_udc *dev)
1451 /* block all irqs */
1452 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1453 UICR0 = UICR1 = 0xff;
1454 UFNRH = UFNRH_SIM;
1456 /* if hardware supports it, disconnect from usb */
1457 make_usb_disappear();
1459 udc_clear_mask_UDCCR(UDCCR_UDE);
1461 #ifdef CONFIG_ARCH_PXA
1462 /* Disable clock for USB device */
1463 pxa_set_cken(CKEN11_USB, 0);
1464 #endif
1466 ep0_idle (dev);
1467 dev->gadget.speed = USB_SPEED_UNKNOWN;
1468 LED_CONNECTED_OFF;
1473 * udc_reinit - initialize software state
1475 static void udc_reinit(struct pxa2xx_udc *dev)
1477 u32 i;
1479 /* device/ep0 records init */
1480 INIT_LIST_HEAD (&dev->gadget.ep_list);
1481 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1482 dev->ep0state = EP0_IDLE;
1484 /* basic endpoint records init */
1485 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1486 struct pxa2xx_ep *ep = &dev->ep[i];
1488 if (i != 0)
1489 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1491 ep->desc = NULL;
1492 ep->stopped = 0;
1493 INIT_LIST_HEAD (&ep->queue);
1494 ep->pio_irqs = ep->dma_irqs = 0;
1497 /* the rest was statically initialized, and is read-only */
1500 /* until it's enabled, this UDC should be completely invisible
1501 * to any USB host.
1503 static void udc_enable (struct pxa2xx_udc *dev)
1505 udc_clear_mask_UDCCR(UDCCR_UDE);
1507 #ifdef CONFIG_ARCH_PXA
1508 /* Enable clock for USB device */
1509 pxa_set_cken(CKEN11_USB, 1);
1510 udelay(5);
1511 #endif
1513 /* try to clear these bits before we enable the udc */
1514 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1516 ep0_idle(dev);
1517 dev->gadget.speed = USB_SPEED_UNKNOWN;
1518 dev->stats.irqs = 0;
1521 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1522 * - enable UDC
1523 * - if RESET is already in progress, ack interrupt
1524 * - unmask reset interrupt
1526 udc_set_mask_UDCCR(UDCCR_UDE);
1527 if (!(UDCCR & UDCCR_UDA))
1528 udc_ack_int_UDCCR(UDCCR_RSTIR);
1530 if (dev->has_cfr /* UDC_RES2 is defined */) {
1531 /* pxa255 (a0+) can avoid a set_config race that could
1532 * prevent gadget drivers from configuring correctly
1534 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1535 } else {
1536 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1537 * which could result in missing packets and interrupts.
1538 * supposedly one bit per endpoint, controlling whether it
1539 * double buffers or not; ACM/AREN bits fit into the holes.
1540 * zero bits (like USIR0_IRx) disable double buffering.
1542 UDC_RES1 = 0x00;
1543 UDC_RES2 = 0x00;
1546 #ifdef DISABLE_TEST_MODE
1547 /* "test mode" seems to have become the default in later chip
1548 * revs, preventing double buffering (and invalidating docs).
1549 * this EXPERIMENT enables it for bulk endpoints by tweaking
1550 * undefined/reserved register bits (that other drivers clear).
1551 * Belcarra code comments noted this usage.
1553 if (fifo_mode & 1) { /* IN endpoints */
1554 UDC_RES1 |= USIR0_IR1|USIR0_IR6;
1555 UDC_RES2 |= USIR1_IR11;
1557 if (fifo_mode & 2) { /* OUT endpoints */
1558 UDC_RES1 |= USIR0_IR2|USIR0_IR7;
1559 UDC_RES2 |= USIR1_IR12;
1561 #endif
1563 /* enable suspend/resume and reset irqs */
1564 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1566 /* enable ep0 irqs */
1567 UICR0 &= ~UICR0_IM0;
1569 /* if hardware supports it, pullup D+ and wait for reset */
1570 let_usb_appear();
1574 /* when a driver is successfully registered, it will receive
1575 * control requests including set_configuration(), which enables
1576 * non-control requests. then usb traffic follows until a
1577 * disconnect is reported. then a host may connect again, or
1578 * the driver might get unbound.
1580 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1582 struct pxa2xx_udc *dev = the_controller;
1583 int retval;
1585 if (!driver
1586 || driver->speed != USB_SPEED_FULL
1587 || !driver->bind
1588 || !driver->unbind
1589 || !driver->disconnect
1590 || !driver->setup)
1591 return -EINVAL;
1592 if (!dev)
1593 return -ENODEV;
1594 if (dev->driver)
1595 return -EBUSY;
1597 /* first hook up the driver ... */
1598 dev->driver = driver;
1599 dev->gadget.dev.driver = &driver->driver;
1600 dev->pullup = 1;
1602 device_add (&dev->gadget.dev);
1603 retval = driver->bind(&dev->gadget);
1604 if (retval) {
1605 DMSG("bind to driver %s --> error %d\n",
1606 driver->driver.name, retval);
1607 device_del (&dev->gadget.dev);
1609 dev->driver = NULL;
1610 dev->gadget.dev.driver = NULL;
1611 return retval;
1613 device_create_file(dev->dev, &dev_attr_function);
1615 /* ... then enable host detection and ep0; and we're ready
1616 * for set_configuration as well as eventual disconnect.
1618 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1619 pullup(dev, 1);
1620 dump_state(dev);
1621 return 0;
1623 EXPORT_SYMBOL(usb_gadget_register_driver);
1625 static void
1626 stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
1628 int i;
1630 /* don't disconnect drivers more than once */
1631 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1632 driver = NULL;
1633 dev->gadget.speed = USB_SPEED_UNKNOWN;
1635 /* prevent new request submissions, kill any outstanding requests */
1636 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1637 struct pxa2xx_ep *ep = &dev->ep[i];
1639 ep->stopped = 1;
1640 nuke(ep, -ESHUTDOWN);
1642 del_timer_sync(&dev->timer);
1644 /* report disconnect; the driver is already quiesced */
1645 LED_CONNECTED_OFF;
1646 if (driver)
1647 driver->disconnect(&dev->gadget);
1649 /* re-init driver-visible data structures */
1650 udc_reinit(dev);
1653 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1655 struct pxa2xx_udc *dev = the_controller;
1657 if (!dev)
1658 return -ENODEV;
1659 if (!driver || driver != dev->driver)
1660 return -EINVAL;
1662 local_irq_disable();
1663 pullup(dev, 0);
1664 stop_activity(dev, driver);
1665 local_irq_enable();
1667 driver->unbind(&dev->gadget);
1668 dev->driver = NULL;
1670 device_del (&dev->gadget.dev);
1671 device_remove_file(dev->dev, &dev_attr_function);
1673 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1674 dump_state(dev);
1675 return 0;
1677 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1680 /*-------------------------------------------------------------------------*/
1682 #ifdef CONFIG_ARCH_LUBBOCK
1684 /* Lubbock has separate connect and disconnect irqs. More typical designs
1685 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1688 static irqreturn_t
1689 lubbock_vbus_irq(int irq, void *_dev, struct pt_regs *r)
1691 struct pxa2xx_udc *dev = _dev;
1692 int vbus;
1694 dev->stats.irqs++;
1695 HEX_DISPLAY(dev->stats.irqs);
1696 switch (irq) {
1697 case LUBBOCK_USB_IRQ:
1698 LED_CONNECTED_ON;
1699 vbus = 1;
1700 disable_irq(LUBBOCK_USB_IRQ);
1701 enable_irq(LUBBOCK_USB_DISC_IRQ);
1702 break;
1703 case LUBBOCK_USB_DISC_IRQ:
1704 LED_CONNECTED_OFF;
1705 vbus = 0;
1706 disable_irq(LUBBOCK_USB_DISC_IRQ);
1707 enable_irq(LUBBOCK_USB_IRQ);
1708 break;
1709 default:
1710 return IRQ_NONE;
1713 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1714 return IRQ_HANDLED;
1717 #endif
1720 /*-------------------------------------------------------------------------*/
1722 static inline void clear_ep_state (struct pxa2xx_udc *dev)
1724 unsigned i;
1726 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1727 * fifos, and pending transactions mustn't be continued in any case.
1729 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1730 nuke(&dev->ep[i], -ECONNABORTED);
1733 static void udc_watchdog(unsigned long _dev)
1735 struct pxa2xx_udc *dev = (void *)_dev;
1737 local_irq_disable();
1738 if (dev->ep0state == EP0_STALL
1739 && (UDCCS0 & UDCCS0_FST) == 0
1740 && (UDCCS0 & UDCCS0_SST) == 0) {
1741 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1742 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1743 start_watchdog(dev);
1745 local_irq_enable();
1748 static void handle_ep0 (struct pxa2xx_udc *dev)
1750 u32 udccs0 = UDCCS0;
1751 struct pxa2xx_ep *ep = &dev->ep [0];
1752 struct pxa2xx_request *req;
1753 union {
1754 struct usb_ctrlrequest r;
1755 u8 raw [8];
1756 u32 word [2];
1757 } u;
1759 if (list_empty(&ep->queue))
1760 req = NULL;
1761 else
1762 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
1764 /* clear stall status */
1765 if (udccs0 & UDCCS0_SST) {
1766 nuke(ep, -EPIPE);
1767 UDCCS0 = UDCCS0_SST;
1768 del_timer(&dev->timer);
1769 ep0_idle(dev);
1772 /* previous request unfinished? non-error iff back-to-back ... */
1773 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1774 nuke(ep, 0);
1775 del_timer(&dev->timer);
1776 ep0_idle(dev);
1779 switch (dev->ep0state) {
1780 case EP0_IDLE:
1781 /* late-breaking status? */
1782 udccs0 = UDCCS0;
1784 /* start control request? */
1785 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1786 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1787 int i;
1789 nuke (ep, -EPROTO);
1791 /* read SETUP packet */
1792 for (i = 0; i < 8; i++) {
1793 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1794 bad_setup:
1795 DMSG("SETUP %d!\n", i);
1796 goto stall;
1798 u.raw [i] = (u8) UDDR0;
1800 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1801 goto bad_setup;
1803 got_setup:
1804 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1805 u.r.bRequestType, u.r.bRequest,
1806 le16_to_cpu(u.r.wValue),
1807 le16_to_cpu(u.r.wIndex),
1808 le16_to_cpu(u.r.wLength));
1810 /* cope with automagic for some standard requests. */
1811 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1812 == USB_TYPE_STANDARD;
1813 dev->req_config = 0;
1814 dev->req_pending = 1;
1815 switch (u.r.bRequest) {
1816 /* hardware restricts gadget drivers here! */
1817 case USB_REQ_SET_CONFIGURATION:
1818 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1819 /* reflect hardware's automagic
1820 * up to the gadget driver.
1822 config_change:
1823 dev->req_config = 1;
1824 clear_ep_state(dev);
1825 /* if !has_cfr, there's no synch
1826 * else use AREN (later) not SA|OPR
1827 * USIR0_IR0 acts edge sensitive
1830 break;
1831 /* ... and here, even more ... */
1832 case USB_REQ_SET_INTERFACE:
1833 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1834 /* udc hardware is broken by design:
1835 * - altsetting may only be zero;
1836 * - hw resets all interfaces' eps;
1837 * - ep reset doesn't include halt(?).
1839 DMSG("broken set_interface (%d/%d)\n",
1840 le16_to_cpu(u.r.wIndex),
1841 le16_to_cpu(u.r.wValue));
1842 goto config_change;
1844 break;
1845 /* hardware was supposed to hide this */
1846 case USB_REQ_SET_ADDRESS:
1847 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1848 ep0start(dev, 0, "address");
1849 return;
1851 break;
1854 if (u.r.bRequestType & USB_DIR_IN)
1855 dev->ep0state = EP0_IN_DATA_PHASE;
1856 else
1857 dev->ep0state = EP0_OUT_DATA_PHASE;
1859 i = dev->driver->setup(&dev->gadget, &u.r);
1860 if (i < 0) {
1861 /* hardware automagic preventing STALL... */
1862 if (dev->req_config) {
1863 /* hardware sometimes neglects to tell
1864 * tell us about config change events,
1865 * so later ones may fail...
1867 WARN("config change %02x fail %d?\n",
1868 u.r.bRequest, i);
1869 return;
1870 /* TODO experiment: if has_cfr,
1871 * hardware didn't ACK; maybe we
1872 * could actually STALL!
1875 DBG(DBG_VERBOSE, "protocol STALL, "
1876 "%02x err %d\n", UDCCS0, i);
1877 stall:
1878 /* the watchdog timer helps deal with cases
1879 * where udc seems to clear FST wrongly, and
1880 * then NAKs instead of STALLing.
1882 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1883 start_watchdog(dev);
1884 dev->ep0state = EP0_STALL;
1886 /* deferred i/o == no response yet */
1887 } else if (dev->req_pending) {
1888 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1889 || dev->req_std || u.r.wLength))
1890 ep0start(dev, 0, "defer");
1891 else
1892 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1895 /* expect at least one data or status stage irq */
1896 return;
1898 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1899 == (UDCCS0_OPR|UDCCS0_SA))) {
1900 unsigned i;
1902 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1903 * still observed on a pxa255 a0.
1905 DBG(DBG_VERBOSE, "e131\n");
1906 nuke(ep, -EPROTO);
1908 /* read SETUP data, but don't trust it too much */
1909 for (i = 0; i < 8; i++)
1910 u.raw [i] = (u8) UDDR0;
1911 if ((u.r.bRequestType & USB_RECIP_MASK)
1912 > USB_RECIP_OTHER)
1913 goto stall;
1914 if (u.word [0] == 0 && u.word [1] == 0)
1915 goto stall;
1916 goto got_setup;
1917 } else {
1918 /* some random early IRQ:
1919 * - we acked FST
1920 * - IPR cleared
1921 * - OPR got set, without SA (likely status stage)
1923 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1925 break;
1926 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1927 if (udccs0 & UDCCS0_OPR) {
1928 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1929 DBG(DBG_VERBOSE, "ep0in premature status\n");
1930 if (req)
1931 done(ep, req, 0);
1932 ep0_idle(dev);
1933 } else /* irq was IPR clearing */ {
1934 if (req) {
1935 /* this IN packet might finish the request */
1936 (void) write_ep0_fifo(ep, req);
1937 } /* else IN token before response was written */
1939 break;
1940 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1941 if (udccs0 & UDCCS0_OPR) {
1942 if (req) {
1943 /* this OUT packet might finish the request */
1944 if (read_ep0_fifo(ep, req))
1945 done(ep, req, 0);
1946 /* else more OUT packets expected */
1947 } /* else OUT token before read was issued */
1948 } else /* irq was IPR clearing */ {
1949 DBG(DBG_VERBOSE, "ep0out premature status\n");
1950 if (req)
1951 done(ep, req, 0);
1952 ep0_idle(dev);
1954 break;
1955 case EP0_END_XFER:
1956 if (req)
1957 done(ep, req, 0);
1958 /* ack control-IN status (maybe in-zlp was skipped)
1959 * also appears after some config change events.
1961 if (udccs0 & UDCCS0_OPR)
1962 UDCCS0 = UDCCS0_OPR;
1963 ep0_idle(dev);
1964 break;
1965 case EP0_STALL:
1966 UDCCS0 = UDCCS0_FST;
1967 break;
1969 USIR0 = USIR0_IR0;
1972 static void handle_ep(struct pxa2xx_ep *ep)
1974 struct pxa2xx_request *req;
1975 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1976 int completed;
1977 u32 udccs, tmp;
1979 do {
1980 completed = 0;
1981 if (likely (!list_empty(&ep->queue)))
1982 req = list_entry(ep->queue.next,
1983 struct pxa2xx_request, queue);
1984 else
1985 req = NULL;
1987 // TODO check FST handling
1989 udccs = *ep->reg_udccs;
1990 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1991 tmp = UDCCS_BI_TUR;
1992 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1993 tmp |= UDCCS_BI_SST;
1994 tmp &= udccs;
1995 if (likely (tmp))
1996 *ep->reg_udccs = tmp;
1997 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1998 completed = write_fifo(ep, req);
2000 } else { /* irq from RPC (or for ISO, ROF) */
2001 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
2002 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
2003 else
2004 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
2005 tmp &= udccs;
2006 if (likely(tmp))
2007 *ep->reg_udccs = tmp;
2009 /* fifos can hold packets, ready for reading... */
2010 if (likely(req)) {
2011 #ifdef USE_OUT_DMA
2012 // TODO didn't yet debug out-dma. this approach assumes
2013 // the worst about short packets and RPC; it might be better.
2015 if (likely(ep->dma >= 0)) {
2016 if (!(udccs & UDCCS_BO_RSP)) {
2017 *ep->reg_udccs = UDCCS_BO_RPC;
2018 ep->dma_irqs++;
2019 return;
2022 #endif
2023 completed = read_fifo(ep, req);
2024 } else
2025 pio_irq_disable (ep->bEndpointAddress);
2027 ep->pio_irqs++;
2028 } while (completed);
2032 * pxa2xx_udc_irq - interrupt handler
2034 * avoid delays in ep0 processing. the control handshaking isn't always
2035 * under software control (pxa250c0 and the pxa255 are better), and delays
2036 * could cause usb protocol errors.
2038 static irqreturn_t
2039 pxa2xx_udc_irq(int irq, void *_dev, struct pt_regs *r)
2041 struct pxa2xx_udc *dev = _dev;
2042 int handled;
2044 dev->stats.irqs++;
2045 HEX_DISPLAY(dev->stats.irqs);
2046 do {
2047 u32 udccr = UDCCR;
2049 handled = 0;
2051 /* SUSpend Interrupt Request */
2052 if (unlikely(udccr & UDCCR_SUSIR)) {
2053 udc_ack_int_UDCCR(UDCCR_SUSIR);
2054 handled = 1;
2055 DBG(DBG_VERBOSE, "USB suspend%s\n", is_usb_connected()
2056 ? "" : "+disconnect");
2058 if (!is_usb_connected())
2059 stop_activity(dev, dev->driver);
2060 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
2061 && dev->driver
2062 && dev->driver->suspend)
2063 dev->driver->suspend(&dev->gadget);
2064 ep0_idle (dev);
2067 /* RESume Interrupt Request */
2068 if (unlikely(udccr & UDCCR_RESIR)) {
2069 udc_ack_int_UDCCR(UDCCR_RESIR);
2070 handled = 1;
2071 DBG(DBG_VERBOSE, "USB resume\n");
2073 if (dev->gadget.speed != USB_SPEED_UNKNOWN
2074 && dev->driver
2075 && dev->driver->resume
2076 && is_usb_connected())
2077 dev->driver->resume(&dev->gadget);
2080 /* ReSeT Interrupt Request - USB reset */
2081 if (unlikely(udccr & UDCCR_RSTIR)) {
2082 udc_ack_int_UDCCR(UDCCR_RSTIR);
2083 handled = 1;
2085 if ((UDCCR & UDCCR_UDA) == 0) {
2086 DBG(DBG_VERBOSE, "USB reset start\n");
2088 /* reset driver and endpoints,
2089 * in case that's not yet done
2091 stop_activity (dev, dev->driver);
2093 } else {
2094 DBG(DBG_VERBOSE, "USB reset end\n");
2095 dev->gadget.speed = USB_SPEED_FULL;
2096 LED_CONNECTED_ON;
2097 memset(&dev->stats, 0, sizeof dev->stats);
2098 /* driver and endpoints are still reset */
2101 } else {
2102 u32 usir0 = USIR0 & ~UICR0;
2103 u32 usir1 = USIR1 & ~UICR1;
2104 int i;
2106 if (unlikely (!usir0 && !usir1))
2107 continue;
2109 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
2111 /* control traffic */
2112 if (usir0 & USIR0_IR0) {
2113 dev->ep[0].pio_irqs++;
2114 handle_ep0(dev);
2115 handled = 1;
2118 /* endpoint data transfers */
2119 for (i = 0; i < 8; i++) {
2120 u32 tmp = 1 << i;
2122 if (i && (usir0 & tmp)) {
2123 handle_ep(&dev->ep[i]);
2124 USIR0 |= tmp;
2125 handled = 1;
2127 if (usir1 & tmp) {
2128 handle_ep(&dev->ep[i+8]);
2129 USIR1 |= tmp;
2130 handled = 1;
2135 /* we could also ask for 1 msec SOF (SIR) interrupts */
2137 } while (handled);
2138 return IRQ_HANDLED;
2141 /*-------------------------------------------------------------------------*/
2143 static void nop_release (struct device *dev)
2145 DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
2148 /* this uses load-time allocation and initialization (instead of
2149 * doing it at run-time) to save code, eliminate fault paths, and
2150 * be more obviously correct.
2152 static struct pxa2xx_udc memory = {
2153 .gadget = {
2154 .ops = &pxa2xx_udc_ops,
2155 .ep0 = &memory.ep[0].ep,
2156 .name = driver_name,
2157 .dev = {
2158 .bus_id = "gadget",
2159 .release = nop_release,
2163 /* control endpoint */
2164 .ep[0] = {
2165 .ep = {
2166 .name = ep0name,
2167 .ops = &pxa2xx_ep_ops,
2168 .maxpacket = EP0_FIFO_SIZE,
2170 .dev = &memory,
2171 .reg_udccs = &UDCCS0,
2172 .reg_uddr = &UDDR0,
2175 /* first group of endpoints */
2176 .ep[1] = {
2177 .ep = {
2178 .name = "ep1in-bulk",
2179 .ops = &pxa2xx_ep_ops,
2180 .maxpacket = BULK_FIFO_SIZE,
2182 .dev = &memory,
2183 .fifo_size = BULK_FIFO_SIZE,
2184 .bEndpointAddress = USB_DIR_IN | 1,
2185 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2186 .reg_udccs = &UDCCS1,
2187 .reg_uddr = &UDDR1,
2188 drcmr (25)
2190 .ep[2] = {
2191 .ep = {
2192 .name = "ep2out-bulk",
2193 .ops = &pxa2xx_ep_ops,
2194 .maxpacket = BULK_FIFO_SIZE,
2196 .dev = &memory,
2197 .fifo_size = BULK_FIFO_SIZE,
2198 .bEndpointAddress = 2,
2199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2200 .reg_udccs = &UDCCS2,
2201 .reg_ubcr = &UBCR2,
2202 .reg_uddr = &UDDR2,
2203 drcmr (26)
2205 #ifndef CONFIG_USB_PXA2XX_SMALL
2206 .ep[3] = {
2207 .ep = {
2208 .name = "ep3in-iso",
2209 .ops = &pxa2xx_ep_ops,
2210 .maxpacket = ISO_FIFO_SIZE,
2212 .dev = &memory,
2213 .fifo_size = ISO_FIFO_SIZE,
2214 .bEndpointAddress = USB_DIR_IN | 3,
2215 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2216 .reg_udccs = &UDCCS3,
2217 .reg_uddr = &UDDR3,
2218 drcmr (27)
2220 .ep[4] = {
2221 .ep = {
2222 .name = "ep4out-iso",
2223 .ops = &pxa2xx_ep_ops,
2224 .maxpacket = ISO_FIFO_SIZE,
2226 .dev = &memory,
2227 .fifo_size = ISO_FIFO_SIZE,
2228 .bEndpointAddress = 4,
2229 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2230 .reg_udccs = &UDCCS4,
2231 .reg_ubcr = &UBCR4,
2232 .reg_uddr = &UDDR4,
2233 drcmr (28)
2235 .ep[5] = {
2236 .ep = {
2237 .name = "ep5in-int",
2238 .ops = &pxa2xx_ep_ops,
2239 .maxpacket = INT_FIFO_SIZE,
2241 .dev = &memory,
2242 .fifo_size = INT_FIFO_SIZE,
2243 .bEndpointAddress = USB_DIR_IN | 5,
2244 .bmAttributes = USB_ENDPOINT_XFER_INT,
2245 .reg_udccs = &UDCCS5,
2246 .reg_uddr = &UDDR5,
2249 /* second group of endpoints */
2250 .ep[6] = {
2251 .ep = {
2252 .name = "ep6in-bulk",
2253 .ops = &pxa2xx_ep_ops,
2254 .maxpacket = BULK_FIFO_SIZE,
2256 .dev = &memory,
2257 .fifo_size = BULK_FIFO_SIZE,
2258 .bEndpointAddress = USB_DIR_IN | 6,
2259 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2260 .reg_udccs = &UDCCS6,
2261 .reg_uddr = &UDDR6,
2262 drcmr (30)
2264 .ep[7] = {
2265 .ep = {
2266 .name = "ep7out-bulk",
2267 .ops = &pxa2xx_ep_ops,
2268 .maxpacket = BULK_FIFO_SIZE,
2270 .dev = &memory,
2271 .fifo_size = BULK_FIFO_SIZE,
2272 .bEndpointAddress = 7,
2273 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2274 .reg_udccs = &UDCCS7,
2275 .reg_ubcr = &UBCR7,
2276 .reg_uddr = &UDDR7,
2277 drcmr (31)
2279 .ep[8] = {
2280 .ep = {
2281 .name = "ep8in-iso",
2282 .ops = &pxa2xx_ep_ops,
2283 .maxpacket = ISO_FIFO_SIZE,
2285 .dev = &memory,
2286 .fifo_size = ISO_FIFO_SIZE,
2287 .bEndpointAddress = USB_DIR_IN | 8,
2288 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2289 .reg_udccs = &UDCCS8,
2290 .reg_uddr = &UDDR8,
2291 drcmr (32)
2293 .ep[9] = {
2294 .ep = {
2295 .name = "ep9out-iso",
2296 .ops = &pxa2xx_ep_ops,
2297 .maxpacket = ISO_FIFO_SIZE,
2299 .dev = &memory,
2300 .fifo_size = ISO_FIFO_SIZE,
2301 .bEndpointAddress = 9,
2302 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2303 .reg_udccs = &UDCCS9,
2304 .reg_ubcr = &UBCR9,
2305 .reg_uddr = &UDDR9,
2306 drcmr (33)
2308 .ep[10] = {
2309 .ep = {
2310 .name = "ep10in-int",
2311 .ops = &pxa2xx_ep_ops,
2312 .maxpacket = INT_FIFO_SIZE,
2314 .dev = &memory,
2315 .fifo_size = INT_FIFO_SIZE,
2316 .bEndpointAddress = USB_DIR_IN | 10,
2317 .bmAttributes = USB_ENDPOINT_XFER_INT,
2318 .reg_udccs = &UDCCS10,
2319 .reg_uddr = &UDDR10,
2322 /* third group of endpoints */
2323 .ep[11] = {
2324 .ep = {
2325 .name = "ep11in-bulk",
2326 .ops = &pxa2xx_ep_ops,
2327 .maxpacket = BULK_FIFO_SIZE,
2329 .dev = &memory,
2330 .fifo_size = BULK_FIFO_SIZE,
2331 .bEndpointAddress = USB_DIR_IN | 11,
2332 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2333 .reg_udccs = &UDCCS11,
2334 .reg_uddr = &UDDR11,
2335 drcmr (35)
2337 .ep[12] = {
2338 .ep = {
2339 .name = "ep12out-bulk",
2340 .ops = &pxa2xx_ep_ops,
2341 .maxpacket = BULK_FIFO_SIZE,
2343 .dev = &memory,
2344 .fifo_size = BULK_FIFO_SIZE,
2345 .bEndpointAddress = 12,
2346 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2347 .reg_udccs = &UDCCS12,
2348 .reg_ubcr = &UBCR12,
2349 .reg_uddr = &UDDR12,
2350 drcmr (36)
2352 .ep[13] = {
2353 .ep = {
2354 .name = "ep13in-iso",
2355 .ops = &pxa2xx_ep_ops,
2356 .maxpacket = ISO_FIFO_SIZE,
2358 .dev = &memory,
2359 .fifo_size = ISO_FIFO_SIZE,
2360 .bEndpointAddress = USB_DIR_IN | 13,
2361 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2362 .reg_udccs = &UDCCS13,
2363 .reg_uddr = &UDDR13,
2364 drcmr (37)
2366 .ep[14] = {
2367 .ep = {
2368 .name = "ep14out-iso",
2369 .ops = &pxa2xx_ep_ops,
2370 .maxpacket = ISO_FIFO_SIZE,
2372 .dev = &memory,
2373 .fifo_size = ISO_FIFO_SIZE,
2374 .bEndpointAddress = 14,
2375 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2376 .reg_udccs = &UDCCS14,
2377 .reg_ubcr = &UBCR14,
2378 .reg_uddr = &UDDR14,
2379 drcmr (38)
2381 .ep[15] = {
2382 .ep = {
2383 .name = "ep15in-int",
2384 .ops = &pxa2xx_ep_ops,
2385 .maxpacket = INT_FIFO_SIZE,
2387 .dev = &memory,
2388 .fifo_size = INT_FIFO_SIZE,
2389 .bEndpointAddress = USB_DIR_IN | 15,
2390 .bmAttributes = USB_ENDPOINT_XFER_INT,
2391 .reg_udccs = &UDCCS15,
2392 .reg_uddr = &UDDR15,
2394 #endif /* !CONFIG_USB_PXA2XX_SMALL */
2397 #define CP15R0_VENDOR_MASK 0xffffe000
2399 #if defined(CONFIG_ARCH_PXA)
2400 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2402 #elif defined(CONFIG_ARCH_IXP4XX)
2403 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2405 #endif
2407 #define CP15R0_PROD_MASK 0x000003f0
2408 #define PXA25x 0x00000100 /* and PXA26x */
2409 #define PXA210 0x00000120
2411 #define CP15R0_REV_MASK 0x0000000f
2413 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2415 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2416 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2417 #define PXA250_B2 0x00000104
2418 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2419 #define PXA250_B0 0x00000102
2420 #define PXA250_A1 0x00000101
2421 #define PXA250_A0 0x00000100
2423 #define PXA210_C0 0x00000125
2424 #define PXA210_B2 0x00000124
2425 #define PXA210_B1 0x00000123
2426 #define PXA210_B0 0x00000122
2427 #define IXP425_A0 0x000001c1
2430 * probe - binds to the platform device
2432 static int __init pxa2xx_udc_probe(struct device *_dev)
2434 struct pxa2xx_udc *dev = &memory;
2435 int retval, out_dma = 1;
2436 u32 chiprev;
2438 /* insist on Intel/ARM/XScale */
2439 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2440 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2441 printk(KERN_ERR "%s: not XScale!\n", driver_name);
2442 return -ENODEV;
2445 /* trigger chiprev-specific logic */
2446 switch (chiprev & CP15R0_PRODREV_MASK) {
2447 #if defined(CONFIG_ARCH_PXA)
2448 case PXA255_A0:
2449 dev->has_cfr = 1;
2450 break;
2451 case PXA250_A0:
2452 case PXA250_A1:
2453 /* A0/A1 "not released"; ep 13, 15 unusable */
2454 /* fall through */
2455 case PXA250_B2: case PXA210_B2:
2456 case PXA250_B1: case PXA210_B1:
2457 case PXA250_B0: case PXA210_B0:
2458 out_dma = 0;
2459 /* fall through */
2460 case PXA250_C0: case PXA210_C0:
2461 break;
2462 #elif defined(CONFIG_ARCH_IXP4XX)
2463 case IXP425_A0:
2464 out_dma = 0;
2465 break;
2466 #endif
2467 default:
2468 out_dma = 0;
2469 printk(KERN_ERR "%s: unrecognized processor: %08x\n",
2470 driver_name, chiprev);
2471 /* iop3xx, ixp4xx, ... */
2472 return -ENODEV;
2475 pr_debug("%s: IRQ %d%s%s%s\n", driver_name, IRQ_USB,
2476 dev->has_cfr ? "" : " (!cfr)",
2477 out_dma ? "" : " (broken dma-out)",
2478 SIZE_STR DMASTR
2481 #ifdef USE_DMA
2482 #ifndef USE_OUT_DMA
2483 out_dma = 0;
2484 #endif
2485 /* pxa 250 erratum 130 prevents using OUT dma (fixed C0) */
2486 if (!out_dma) {
2487 DMSG("disabled OUT dma\n");
2488 dev->ep[ 2].reg_drcmr = dev->ep[ 4].reg_drcmr = 0;
2489 dev->ep[ 7].reg_drcmr = dev->ep[ 9].reg_drcmr = 0;
2490 dev->ep[12].reg_drcmr = dev->ep[14].reg_drcmr = 0;
2492 #endif
2494 /* other non-static parts of init */
2495 dev->dev = _dev;
2496 dev->mach = _dev->platform_data;
2498 init_timer(&dev->timer);
2499 dev->timer.function = udc_watchdog;
2500 dev->timer.data = (unsigned long) dev;
2502 device_initialize(&dev->gadget.dev);
2503 dev->gadget.dev.parent = _dev;
2504 dev->gadget.dev.dma_mask = _dev->dma_mask;
2506 the_controller = dev;
2507 dev_set_drvdata(_dev, dev);
2509 udc_disable(dev);
2510 udc_reinit(dev);
2512 dev->vbus = is_usb_connected();
2514 /* irq setup after old hardware state is cleaned up */
2515 retval = request_irq(IRQ_USB, pxa2xx_udc_irq,
2516 SA_INTERRUPT, driver_name, dev);
2517 if (retval != 0) {
2518 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2519 driver_name, IRQ_USB, retval);
2520 return -EBUSY;
2522 dev->got_irq = 1;
2524 #ifdef CONFIG_ARCH_LUBBOCK
2525 if (machine_is_lubbock()) {
2526 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2527 lubbock_vbus_irq,
2528 SA_INTERRUPT | SA_SAMPLE_RANDOM,
2529 driver_name, dev);
2530 if (retval != 0) {
2531 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2532 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2533 lubbock_fail0:
2534 free_irq(IRQ_USB, dev);
2535 return -EBUSY;
2537 retval = request_irq(LUBBOCK_USB_IRQ,
2538 lubbock_vbus_irq,
2539 SA_INTERRUPT | SA_SAMPLE_RANDOM,
2540 driver_name, dev);
2541 if (retval != 0) {
2542 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2543 driver_name, LUBBOCK_USB_IRQ, retval);
2544 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2545 goto lubbock_fail0;
2547 #ifdef DEBUG
2548 /* with U-Boot (but not BLOB), hex is off by default */
2549 HEX_DISPLAY(dev->stats.irqs);
2550 LUB_DISC_BLNK_LED &= 0xff;
2551 #endif
2553 #endif
2554 create_proc_files();
2556 return 0;
2558 static int __exit pxa2xx_udc_remove(struct device *_dev)
2560 struct pxa2xx_udc *dev = dev_get_drvdata(_dev);
2562 udc_disable(dev);
2563 remove_proc_files();
2564 usb_gadget_unregister_driver(dev->driver);
2566 if (dev->got_irq) {
2567 free_irq(IRQ_USB, dev);
2568 dev->got_irq = 0;
2570 if (machine_is_lubbock()) {
2571 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2572 free_irq(LUBBOCK_USB_IRQ, dev);
2574 dev_set_drvdata(_dev, NULL);
2575 the_controller = NULL;
2576 return 0;
2579 /*-------------------------------------------------------------------------*/
2581 #ifdef CONFIG_PM
2583 /* USB suspend (controlled by the host) and system suspend (controlled
2584 * by the PXA) don't necessarily work well together. If USB is active,
2585 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2586 * mode, or any deeper PM saving state.
2588 * For now, we punt and forcibly disconnect from the USB host when PXA
2589 * enters any suspend state. While we're disconnected, we always disable
2590 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2591 * Boards without software pullup control shouldn't use those states.
2592 * VBUS IRQs should probably be ignored so that the PXA device just acts
2593 * "dead" to USB hosts until system resume.
2595 static int pxa2xx_udc_suspend(struct device *dev, u32 state, u32 level)
2597 struct pxa2xx_udc *udc = dev_get_drvdata(dev);
2599 if (level == SUSPEND_POWER_DOWN) {
2600 if (!udc->mach->udc_command)
2601 WARN("USB host won't detect disconnect!\n");
2602 pullup(udc, 0);
2604 return 0;
2607 static int pxa2xx_udc_resume(struct device *dev, u32 level)
2609 struct pxa2xx_udc *udc = dev_get_drvdata(dev);
2611 if (level == RESUME_POWER_ON)
2612 pullup(udc, 1);
2613 return 0;
2616 #else
2617 #define pxa2xx_udc_suspend NULL
2618 #define pxa2xx_udc_resume NULL
2619 #endif
2621 /*-------------------------------------------------------------------------*/
2623 static struct device_driver udc_driver = {
2624 .name = "pxa2xx-udc",
2625 .bus = &platform_bus_type,
2626 .probe = pxa2xx_udc_probe,
2627 .remove = __exit_p(pxa2xx_udc_remove),
2628 .suspend = pxa2xx_udc_suspend,
2629 .resume = pxa2xx_udc_resume,
2632 static int __init udc_init(void)
2634 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2635 return driver_register(&udc_driver);
2637 module_init(udc_init);
2639 static void __exit udc_exit(void)
2641 driver_unregister(&udc_driver);
2643 module_exit(udc_exit);
2645 MODULE_DESCRIPTION(DRIVER_DESC);
2646 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2647 MODULE_LICENSE("GPL");