2 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
5 * Copyright (C) 2003 Robert Schwebel, Pengutronix
6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
7 * Copyright (C) 2003 David Brownell
8 * Copyright (C) 2003 Joshua Wise
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 /* #define VERBOSE_DEBUG */
18 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/ioport.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/err.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/timer.h>
29 #include <linux/list.h>
30 #include <linux/interrupt.h>
32 #include <linux/platform_data/pxa2xx_udc.h>
33 #include <linux/platform_device.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/irq.h>
36 #include <linux/clk.h>
37 #include <linux/seq_file.h>
38 #include <linux/debugfs.h>
40 #include <linux/prefetch.h>
42 #include <asm/byteorder.h>
45 #include <asm/mach-types.h>
46 #include <asm/unaligned.h>
48 #include <linux/usb/ch9.h>
49 #include <linux/usb/gadget.h>
50 #include <linux/usb/otg.h>
53 * This driver is PXA25x only. Grab the right register definitions.
55 #ifdef CONFIG_ARCH_PXA
56 #include <mach/pxa25x-udc.h>
59 #ifdef CONFIG_ARCH_LUBBOCK
60 #include <mach/lubbock.h>
64 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
65 * series processors. The UDC for the IXP 4xx series is very similar.
66 * There are fifteen endpoints, in addition to ep0.
68 * Such controller drivers work with a gadget driver. The gadget driver
69 * returns descriptors, implements configuration and data protocols used
70 * by the host to interact with this device, and allocates endpoints to
71 * the different protocol interfaces. The controller driver virtualizes
72 * usb hardware so that the gadget drivers will be more portable.
74 * This UDC hardware wants to implement a bit too much USB protocol, so
75 * it constrains the sorts of USB configuration change events that work.
76 * The errata for these chips are misleading; some "fixed" bugs from
77 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
79 * Note that the UDC hardware supports DMA (except on IXP) but that's
80 * not used here. IN-DMA (to host) is simple enough, when the data is
81 * suitably aligned (16 bytes) ... the network stack doesn't do that,
82 * other software can. OUT-DMA is buggy in most chip versions, as well
83 * as poorly designed (data toggle not automatic). So this driver won't
84 * bother using DMA. (Mostly-working IN-DMA support was available in
85 * kernels before 2.6.23, but was never enabled or well tested.)
88 #define DRIVER_VERSION "30-June-2007"
89 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
92 static const char driver_name [] = "pxa25x_udc";
94 static const char ep0name [] = "ep0";
97 #ifdef CONFIG_ARCH_IXP4XX
99 /* cpu-specific register addresses are compiled in to this code */
100 #ifdef CONFIG_ARCH_PXA
101 #error "Can't configure both IXP and PXA"
104 /* IXP doesn't yet support <linux/clk.h> */
105 #define clk_get(dev,name) NULL
106 #define clk_enable(clk) do { } while (0)
107 #define clk_disable(clk) do { } while (0)
108 #define clk_put(clk) do { } while (0)
112 #include "pxa25x_udc.h"
115 #ifdef CONFIG_USB_PXA25X_SMALL
116 #define SIZE_STR " (small)"
121 /* ---------------------------------------------------------------------------
122 * endpoint related parts of the api to the usb controller hardware,
123 * used by gadget driver; and the inner talker-to-hardware core.
124 * ---------------------------------------------------------------------------
127 static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
128 static void nuke (struct pxa25x_ep *, int status);
130 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
131 static void pullup_off(void)
133 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
134 int off_level = mach->gpio_pullup_inverted;
136 if (gpio_is_valid(mach->gpio_pullup))
137 gpio_set_value(mach->gpio_pullup, off_level);
138 else if (mach->udc_command)
139 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
142 static void pullup_on(void)
144 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
145 int on_level = !mach->gpio_pullup_inverted;
147 if (gpio_is_valid(mach->gpio_pullup))
148 gpio_set_value(mach->gpio_pullup, on_level);
149 else if (mach->udc_command)
150 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
153 static void pio_irq_enable(int bEndpointAddress)
155 bEndpointAddress &= 0xf;
156 if (bEndpointAddress < 8)
157 UICR0 &= ~(1 << bEndpointAddress);
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;
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 pxa25x
202 * endpoint configurations are fixed, and are pretty much always enabled,
203 * there's not a lot to manage here.
205 * because pxa25x 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 pxa25x_ep_enable (struct usb_ep *_ep,
213 const struct usb_endpoint_descriptor *desc)
215 struct pxa25x_ep *ep;
216 struct pxa25x_udc *dev;
218 ep = container_of (_ep, struct pxa25x_ep, ep);
219 if (!_ep || !desc || _ep->name == ep0name
220 || desc->bDescriptorType != USB_DT_ENDPOINT
221 || ep->bEndpointAddress != desc->bEndpointAddress
222 || ep->fifo_size < usb_endpoint_maxp (desc)) {
223 DMSG("%s, bad ep or descriptor\n", __func__);
227 /* xfer types must match, except that interrupt ~= bulk */
228 if (ep->bmAttributes != desc->bmAttributes
229 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
230 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
231 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
235 /* hardware _could_ do smaller, but driver doesn't */
236 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
237 && usb_endpoint_maxp (desc)
239 || !desc->wMaxPacketSize) {
240 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
245 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
246 DMSG("%s, bogus device state\n", __func__);
253 ep->ep.maxpacket = usb_endpoint_maxp (desc);
255 /* flush fifo (mostly for OUT buffers) */
256 pxa25x_ep_fifo_flush (_ep);
258 /* ... reset halt state too, if we could ... */
260 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
264 static int pxa25x_ep_disable (struct usb_ep *_ep)
266 struct pxa25x_ep *ep;
269 ep = container_of (_ep, struct pxa25x_ep, ep);
270 if (!_ep || !ep->ep.desc) {
271 DMSG("%s, %s not enabled\n", __func__,
272 _ep ? ep->ep.name : NULL);
275 local_irq_save(flags);
277 nuke (ep, -ESHUTDOWN);
279 /* flush fifo (mostly for IN buffers) */
280 pxa25x_ep_fifo_flush (_ep);
285 local_irq_restore(flags);
286 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
290 /*-------------------------------------------------------------------------*/
292 /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
293 * must still pass correctly initialized endpoints, since other controller
294 * drivers may care about how it's currently set up (dma issues etc).
298 * pxa25x_ep_alloc_request - allocate a request data structure
300 static struct usb_request *
301 pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
303 struct pxa25x_request *req;
305 req = kzalloc(sizeof(*req), gfp_flags);
309 INIT_LIST_HEAD (&req->queue);
315 * pxa25x_ep_free_request - deallocate a request data structure
318 pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
320 struct pxa25x_request *req;
322 req = container_of (_req, struct pxa25x_request, req);
323 WARN_ON(!list_empty (&req->queue));
327 /*-------------------------------------------------------------------------*/
330 * done - retire a request; caller blocked irqs
332 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
334 unsigned stopped = ep->stopped;
336 list_del_init(&req->queue);
338 if (likely (req->req.status == -EINPROGRESS))
339 req->req.status = status;
341 status = req->req.status;
343 if (status && status != -ESHUTDOWN)
344 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
345 ep->ep.name, &req->req, status,
346 req->req.actual, req->req.length);
348 /* don't modify queue heads during completion callback */
350 req->req.complete(&ep->ep, &req->req);
351 ep->stopped = stopped;
355 static inline void ep0_idle (struct pxa25x_udc *dev)
357 dev->ep0state = EP0_IDLE;
361 write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
364 unsigned length, count;
366 buf = req->req.buf + req->req.actual;
369 /* how big will this packet be? */
370 length = min(req->req.length - req->req.actual, max);
371 req->req.actual += length;
374 while (likely(count--))
381 * write to an IN endpoint fifo, as many packets as possible.
382 * irqs will use this to write the rest later.
383 * caller guarantees at least one packet buffer is ready (or a zlp).
386 write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
390 max = usb_endpoint_maxp(ep->ep.desc);
393 int is_last, is_short;
395 count = write_packet(ep->reg_uddr, req, max);
397 /* last packet is usually short (or a zlp) */
398 if (unlikely (count != max))
399 is_last = is_short = 1;
401 if (likely(req->req.length != req->req.actual)
406 /* interrupt/iso maxpacket may not fill the fifo */
407 is_short = unlikely (max < ep->fifo_size);
410 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
412 is_last ? "/L" : "", is_short ? "/S" : "",
413 req->req.length - req->req.actual, req);
415 /* let loose that packet. maybe try writing another one,
416 * double buffering might work. TSP, TPC, and TFS
417 * bit values are the same for all normal IN endpoints.
419 *ep->reg_udccs = UDCCS_BI_TPC;
421 *ep->reg_udccs = UDCCS_BI_TSP;
423 /* requests complete when all IN data is in the FIFO */
426 if (list_empty(&ep->queue))
427 pio_irq_disable (ep->bEndpointAddress);
431 // TODO experiment: how robust can fifo mode tweaking be?
432 // double buffering is off in the default fifo mode, which
433 // prevents TFS from being set here.
435 } while (*ep->reg_udccs & UDCCS_BI_TFS);
439 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
440 * ep0 data stage. these chips want very simple state transitions.
443 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
445 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
447 dev->req_pending = 0;
448 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
449 __func__, tag, UDCCS0, flags);
453 write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
458 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
459 ep->dev->stats.write.bytes += count;
461 /* last packet "must be" short (or a zlp) */
462 is_short = (count != EP0_FIFO_SIZE);
464 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
465 req->req.length - req->req.actual, req);
467 if (unlikely (is_short)) {
468 if (ep->dev->req_pending)
469 ep0start(ep->dev, UDCCS0_IPR, "short IN");
473 count = req->req.length;
476 #ifndef CONFIG_ARCH_IXP4XX
478 /* This seems to get rid of lost status irqs in some cases:
479 * host responds quickly, or next request involves config
480 * change automagic, or should have been hidden, or ...
482 * FIXME get rid of all udelays possible...
484 if (count >= EP0_FIFO_SIZE) {
487 if ((UDCCS0 & UDCCS0_OPR) != 0) {
488 /* clear OPR, generate ack */
498 } else if (ep->dev->req_pending)
499 ep0start(ep->dev, 0, "IN");
505 * read_fifo - unload packet(s) from the fifo we use for usb OUT
506 * transfers and put them into the request. caller should have made
507 * sure there's at least one packet ready.
509 * returns true if the request completed because of short packet or the
510 * request buffer having filled (and maybe overran till end-of-packet).
513 read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
518 unsigned bufferspace, count, is_short;
520 /* make sure there's a packet in the FIFO.
521 * UDCCS_{BO,IO}_RPC are all the same bit value.
522 * UDCCS_{BO,IO}_RNE are all the same bit value.
524 udccs = *ep->reg_udccs;
525 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
527 buf = req->req.buf + req->req.actual;
529 bufferspace = req->req.length - req->req.actual;
531 /* read all bytes from this packet */
532 if (likely (udccs & UDCCS_BO_RNE)) {
533 count = 1 + (0x0ff & *ep->reg_ubcr);
534 req->req.actual += min (count, bufferspace);
537 is_short = (count < ep->ep.maxpacket);
538 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
539 ep->ep.name, udccs, count,
540 is_short ? "/S" : "",
541 req, req->req.actual, req->req.length);
542 while (likely (count-- != 0)) {
543 u8 byte = (u8) *ep->reg_uddr;
545 if (unlikely (bufferspace == 0)) {
546 /* this happens when the driver's buffer
547 * is smaller than what the host sent.
548 * discard the extra data.
550 if (req->req.status != -EOVERFLOW)
551 DMSG("%s overflow %d\n",
553 req->req.status = -EOVERFLOW;
559 *ep->reg_udccs = UDCCS_BO_RPC;
560 /* RPC/RSP/RNE could now reflect the other packet buffer */
562 /* iso is one request per packet */
563 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
564 if (udccs & UDCCS_IO_ROF)
565 req->req.status = -EHOSTUNREACH;
566 /* more like "is_done" */
571 if (is_short || req->req.actual == req->req.length) {
573 if (list_empty(&ep->queue))
574 pio_irq_disable (ep->bEndpointAddress);
578 /* finished that packet. the next one may be waiting... */
584 * special ep0 version of the above. no UBCR0 or double buffering; status
585 * handshaking is magic. most device protocols don't need control-OUT.
586 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
587 * protocols do use them.
590 read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
593 unsigned bufferspace;
595 buf = req->req.buf + req->req.actual;
596 bufferspace = req->req.length - req->req.actual;
598 while (UDCCS0 & UDCCS0_RNE) {
601 if (unlikely (bufferspace == 0)) {
602 /* this happens when the driver's buffer
603 * is smaller than what the host sent.
604 * discard the extra data.
606 if (req->req.status != -EOVERFLOW)
607 DMSG("%s overflow\n", ep->ep.name);
608 req->req.status = -EOVERFLOW;
616 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
619 if (req->req.actual >= req->req.length)
622 /* finished that packet. the next one may be waiting... */
626 /*-------------------------------------------------------------------------*/
629 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
631 struct pxa25x_request *req;
632 struct pxa25x_ep *ep;
633 struct pxa25x_udc *dev;
636 req = container_of(_req, struct pxa25x_request, req);
637 if (unlikely (!_req || !_req->complete || !_req->buf
638 || !list_empty(&req->queue))) {
639 DMSG("%s, bad params\n", __func__);
643 ep = container_of(_ep, struct pxa25x_ep, ep);
644 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
645 DMSG("%s, bad ep\n", __func__);
650 if (unlikely (!dev->driver
651 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
652 DMSG("%s, bogus device state\n", __func__);
656 /* iso is always one packet per request, that's the only way
657 * we can report per-packet status. that also helps with dma.
659 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
660 && req->req.length > usb_endpoint_maxp(ep->ep.desc)))
663 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
664 _ep->name, _req, _req->length, _req->buf);
666 local_irq_save(flags);
668 _req->status = -EINPROGRESS;
671 /* kickstart this i/o queue? */
672 if (list_empty(&ep->queue) && !ep->stopped) {
673 if (ep->ep.desc == NULL/* ep0 */) {
674 unsigned length = _req->length;
676 switch (dev->ep0state) {
677 case EP0_IN_DATA_PHASE:
678 dev->stats.write.ops++;
679 if (write_ep0_fifo(ep, req))
683 case EP0_OUT_DATA_PHASE:
684 dev->stats.read.ops++;
686 if (dev->req_config) {
687 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
688 dev->has_cfr ? "" : " raced");
690 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
693 dev->ep0state = EP0_END_XFER;
694 local_irq_restore (flags);
697 if (dev->req_pending)
698 ep0start(dev, UDCCS0_IPR, "OUT");
699 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
700 && read_ep0_fifo(ep, req))) {
708 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
709 local_irq_restore (flags);
712 /* can the FIFO can satisfy the request immediately? */
713 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
714 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
715 && write_fifo(ep, req))
717 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
718 && read_fifo(ep, req)) {
722 if (likely(req && ep->ep.desc))
723 pio_irq_enable(ep->bEndpointAddress);
726 /* pio or dma irq handler advances the queue. */
727 if (likely(req != NULL))
728 list_add_tail(&req->queue, &ep->queue);
729 local_irq_restore(flags);
736 * nuke - dequeue ALL requests
738 static void nuke(struct pxa25x_ep *ep, int status)
740 struct pxa25x_request *req;
742 /* called with irqs blocked */
743 while (!list_empty(&ep->queue)) {
744 req = list_entry(ep->queue.next,
745 struct pxa25x_request,
747 done(ep, req, status);
750 pio_irq_disable (ep->bEndpointAddress);
754 /* dequeue JUST ONE request */
755 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
757 struct pxa25x_ep *ep;
758 struct pxa25x_request *req;
761 ep = container_of(_ep, struct pxa25x_ep, ep);
762 if (!_ep || ep->ep.name == ep0name)
765 local_irq_save(flags);
767 /* make sure it's actually queued on this endpoint */
768 list_for_each_entry (req, &ep->queue, queue) {
769 if (&req->req == _req)
772 if (&req->req != _req) {
773 local_irq_restore(flags);
777 done(ep, req, -ECONNRESET);
779 local_irq_restore(flags);
783 /*-------------------------------------------------------------------------*/
785 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
787 struct pxa25x_ep *ep;
790 ep = container_of(_ep, struct pxa25x_ep, ep);
792 || (!ep->ep.desc && ep->ep.name != ep0name))
793 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
794 DMSG("%s, bad ep\n", __func__);
798 /* this path (reset toggle+halt) is needed to implement
799 * SET_INTERFACE on normal hardware. but it can't be
800 * done from software on the PXA UDC, and the hardware
801 * forgets to do it as part of SET_INTERFACE automagic.
803 DMSG("only host can clear %s halt\n", _ep->name);
807 local_irq_save(flags);
809 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
810 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
811 || !list_empty(&ep->queue))) {
812 local_irq_restore(flags);
816 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
817 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
819 /* ep0 needs special care */
821 start_watchdog(ep->dev);
822 ep->dev->req_pending = 0;
823 ep->dev->ep0state = EP0_STALL;
825 /* and bulk/intr endpoints like dropping stalls too */
828 for (i = 0; i < 1000; i += 20) {
829 if (*ep->reg_udccs & UDCCS_BI_SST)
834 local_irq_restore(flags);
836 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
840 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
842 struct pxa25x_ep *ep;
844 ep = container_of(_ep, struct pxa25x_ep, ep);
846 DMSG("%s, bad ep\n", __func__);
849 /* pxa can't report unclaimed bytes from IN fifos */
850 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
852 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
853 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
856 return (*ep->reg_ubcr & 0xfff) + 1;
859 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
861 struct pxa25x_ep *ep;
863 ep = container_of(_ep, struct pxa25x_ep, ep);
864 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
865 DMSG("%s, bad ep\n", __func__);
869 /* toggle and halt bits stay unchanged */
871 /* for OUT, just read and discard the FIFO contents. */
872 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
873 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
874 (void) *ep->reg_uddr;
878 /* most IN status is the same, but ISO can't stall */
879 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
880 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
885 static struct usb_ep_ops pxa25x_ep_ops = {
886 .enable = pxa25x_ep_enable,
887 .disable = pxa25x_ep_disable,
889 .alloc_request = pxa25x_ep_alloc_request,
890 .free_request = pxa25x_ep_free_request,
892 .queue = pxa25x_ep_queue,
893 .dequeue = pxa25x_ep_dequeue,
895 .set_halt = pxa25x_ep_set_halt,
896 .fifo_status = pxa25x_ep_fifo_status,
897 .fifo_flush = pxa25x_ep_fifo_flush,
901 /* ---------------------------------------------------------------------------
902 * device-scoped parts of the api to the usb controller hardware
903 * ---------------------------------------------------------------------------
906 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
908 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
911 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
913 /* host may not have enabled remote wakeup */
914 if ((UDCCS0 & UDCCS0_DRWF) == 0)
915 return -EHOSTUNREACH;
916 udc_set_mask_UDCCR(UDCCR_RSM);
920 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
921 static void udc_enable (struct pxa25x_udc *);
922 static void udc_disable(struct pxa25x_udc *);
924 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
927 static int pullup(struct pxa25x_udc *udc)
929 int is_active = udc->vbus && udc->pullup && !udc->suspended;
930 DMSG("%s\n", is_active ? "active" : "inactive");
934 /* Enable clock for USB device */
935 clk_enable(udc->clk);
940 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
941 DMSG("disconnect %s\n", udc->driver
942 ? udc->driver->driver.name
944 stop_activity(udc, udc->driver);
947 /* Disable clock for USB device */
948 clk_disable(udc->clk);
956 /* VBUS reporting logically comes from a transceiver */
957 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
959 struct pxa25x_udc *udc;
961 udc = container_of(_gadget, struct pxa25x_udc, gadget);
962 udc->vbus = is_active;
963 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
968 /* drivers may have software control over D+ pullup */
969 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
971 struct pxa25x_udc *udc;
973 udc = container_of(_gadget, struct pxa25x_udc, gadget);
975 /* not all boards support pullup control */
976 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
979 udc->pullup = (is_active != 0);
984 /* boards may consume current from VBUS, up to 100-500mA based on config.
985 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
988 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
990 struct pxa25x_udc *udc;
992 udc = container_of(_gadget, struct pxa25x_udc, gadget);
994 if (!IS_ERR_OR_NULL(udc->transceiver))
995 return usb_phy_set_power(udc->transceiver, mA);
999 static int pxa25x_start(struct usb_gadget_driver *driver,
1000 int (*bind)(struct usb_gadget *, struct usb_gadget_driver *));
1001 static int pxa25x_stop(struct usb_gadget_driver *driver);
1003 static const struct usb_gadget_ops pxa25x_udc_ops = {
1004 .get_frame = pxa25x_udc_get_frame,
1005 .wakeup = pxa25x_udc_wakeup,
1006 .vbus_session = pxa25x_udc_vbus_session,
1007 .pullup = pxa25x_udc_pullup,
1008 .vbus_draw = pxa25x_udc_vbus_draw,
1009 .start = pxa25x_start,
1010 .stop = pxa25x_stop,
1013 /*-------------------------------------------------------------------------*/
1015 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1018 udc_seq_show(struct seq_file *m, void *_d)
1020 struct pxa25x_udc *dev = m->private;
1021 unsigned long flags;
1025 local_irq_save(flags);
1027 /* basic device status */
1028 seq_printf(m, DRIVER_DESC "\n"
1029 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1030 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1031 dev->driver ? dev->driver->driver.name : "(none)",
1032 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
1034 /* registers for device and ep0 */
1036 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1037 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1041 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1042 (tmp & UDCCR_REM) ? " rem" : "",
1043 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1044 (tmp & UDCCR_SRM) ? " srm" : "",
1045 (tmp & UDCCR_SUSIR) ? " susir" : "",
1046 (tmp & UDCCR_RESIR) ? " resir" : "",
1047 (tmp & UDCCR_RSM) ? " rsm" : "",
1048 (tmp & UDCCR_UDA) ? " uda" : "",
1049 (tmp & UDCCR_UDE) ? " ude" : "");
1053 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1054 (tmp & UDCCS0_SA) ? " sa" : "",
1055 (tmp & UDCCS0_RNE) ? " rne" : "",
1056 (tmp & UDCCS0_FST) ? " fst" : "",
1057 (tmp & UDCCS0_SST) ? " sst" : "",
1058 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1059 (tmp & UDCCS0_FTF) ? " ftf" : "",
1060 (tmp & UDCCS0_IPR) ? " ipr" : "",
1061 (tmp & UDCCS0_OPR) ? " opr" : "");
1066 "udccfr %02X =%s%s\n", tmp,
1067 (tmp & UDCCFR_AREN) ? " aren" : "",
1068 (tmp & UDCCFR_ACM) ? " acm" : "");
1071 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
1074 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1075 dev->stats.write.bytes, dev->stats.write.ops,
1076 dev->stats.read.bytes, dev->stats.read.ops,
1079 /* dump endpoint queues */
1080 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1081 struct pxa25x_ep *ep = &dev->ep [i];
1082 struct pxa25x_request *req;
1085 const struct usb_endpoint_descriptor *desc;
1090 tmp = *dev->ep [i].reg_udccs;
1092 "%s max %d %s udccs %02x irqs %lu\n",
1093 ep->ep.name, usb_endpoint_maxp(desc),
1094 "pio", tmp, ep->pio_irqs);
1095 /* TODO translate all five groups of udccs bits! */
1097 } else /* ep0 should only have one transfer queued */
1098 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
1101 if (list_empty(&ep->queue)) {
1102 seq_printf(m, "\t(nothing queued)\n");
1105 list_for_each_entry(req, &ep->queue, queue) {
1107 "\treq %p len %d/%d buf %p\n",
1108 &req->req, req->req.actual,
1109 req->req.length, req->req.buf);
1114 local_irq_restore(flags);
1119 udc_debugfs_open(struct inode *inode, struct file *file)
1121 return single_open(file, udc_seq_show, inode->i_private);
1124 static const struct file_operations debug_fops = {
1125 .open = udc_debugfs_open,
1127 .llseek = seq_lseek,
1128 .release = single_release,
1129 .owner = THIS_MODULE,
1132 #define create_debug_files(dev) \
1134 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1135 S_IRUGO, NULL, dev, &debug_fops); \
1137 #define remove_debug_files(dev) \
1139 if (dev->debugfs_udc) \
1140 debugfs_remove(dev->debugfs_udc); \
1143 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1145 #define create_debug_files(dev) do {} while (0)
1146 #define remove_debug_files(dev) do {} while (0)
1148 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1150 /*-------------------------------------------------------------------------*/
1153 * udc_disable - disable USB device controller
1155 static void udc_disable(struct pxa25x_udc *dev)
1157 /* block all irqs */
1158 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1159 UICR0 = UICR1 = 0xff;
1162 /* if hardware supports it, disconnect from usb */
1165 udc_clear_mask_UDCCR(UDCCR_UDE);
1168 dev->gadget.speed = USB_SPEED_UNKNOWN;
1173 * udc_reinit - initialize software state
1175 static void udc_reinit(struct pxa25x_udc *dev)
1179 /* device/ep0 records init */
1180 INIT_LIST_HEAD (&dev->gadget.ep_list);
1181 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1182 dev->ep0state = EP0_IDLE;
1184 /* basic endpoint records init */
1185 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1186 struct pxa25x_ep *ep = &dev->ep[i];
1189 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1193 INIT_LIST_HEAD (&ep->queue);
1197 /* the rest was statically initialized, and is read-only */
1200 /* until it's enabled, this UDC should be completely invisible
1203 static void udc_enable (struct pxa25x_udc *dev)
1205 udc_clear_mask_UDCCR(UDCCR_UDE);
1207 /* try to clear these bits before we enable the udc */
1208 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1211 dev->gadget.speed = USB_SPEED_UNKNOWN;
1212 dev->stats.irqs = 0;
1215 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1217 * - if RESET is already in progress, ack interrupt
1218 * - unmask reset interrupt
1220 udc_set_mask_UDCCR(UDCCR_UDE);
1221 if (!(UDCCR & UDCCR_UDA))
1222 udc_ack_int_UDCCR(UDCCR_RSTIR);
1224 if (dev->has_cfr /* UDC_RES2 is defined */) {
1225 /* pxa255 (a0+) can avoid a set_config race that could
1226 * prevent gadget drivers from configuring correctly
1228 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1230 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1231 * which could result in missing packets and interrupts.
1232 * supposedly one bit per endpoint, controlling whether it
1233 * double buffers or not; ACM/AREN bits fit into the holes.
1234 * zero bits (like USIR0_IRx) disable double buffering.
1240 /* enable suspend/resume and reset irqs */
1241 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1243 /* enable ep0 irqs */
1244 UICR0 &= ~UICR0_IM0;
1246 /* if hardware supports it, pullup D+ and wait for reset */
1251 /* when a driver is successfully registered, it will receive
1252 * control requests including set_configuration(), which enables
1253 * non-control requests. then usb traffic follows until a
1254 * disconnect is reported. then a host may connect again, or
1255 * the driver might get unbound.
1257 static int pxa25x_start(struct usb_gadget_driver *driver,
1258 int (*bind)(struct usb_gadget *, struct usb_gadget_driver *))
1260 struct pxa25x_udc *dev = the_controller;
1264 || driver->max_speed < USB_SPEED_FULL
1266 || !driver->disconnect
1274 /* first hook up the driver ... */
1275 dev->driver = driver;
1276 dev->gadget.dev.driver = &driver->driver;
1279 retval = device_add (&dev->gadget.dev);
1283 dev->gadget.dev.driver = NULL;
1286 retval = bind(&dev->gadget, driver);
1288 DMSG("bind to driver %s --> error %d\n",
1289 driver->driver.name, retval);
1290 device_del (&dev->gadget.dev);
1294 /* ... then enable host detection and ep0; and we're ready
1295 * for set_configuration as well as eventual disconnect.
1297 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1299 /* connect to bus through transceiver */
1300 if (!IS_ERR_OR_NULL(dev->transceiver)) {
1301 retval = otg_set_peripheral(dev->transceiver->otg,
1304 DMSG("can't bind to transceiver\n");
1306 driver->unbind(&dev->gadget);
1319 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1323 /* don't disconnect drivers more than once */
1324 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1326 dev->gadget.speed = USB_SPEED_UNKNOWN;
1328 /* prevent new request submissions, kill any outstanding requests */
1329 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1330 struct pxa25x_ep *ep = &dev->ep[i];
1333 nuke(ep, -ESHUTDOWN);
1335 del_timer_sync(&dev->timer);
1337 /* report disconnect; the driver is already quiesced */
1339 driver->disconnect(&dev->gadget);
1341 /* re-init driver-visible data structures */
1345 static int pxa25x_stop(struct usb_gadget_driver *driver)
1347 struct pxa25x_udc *dev = the_controller;
1351 if (!driver || driver != dev->driver || !driver->unbind)
1354 local_irq_disable();
1357 stop_activity(dev, driver);
1360 if (!IS_ERR_OR_NULL(dev->transceiver))
1361 (void) otg_set_peripheral(dev->transceiver->otg, NULL);
1363 driver->unbind(&dev->gadget);
1364 dev->gadget.dev.driver = NULL;
1367 device_del (&dev->gadget.dev);
1369 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1374 /*-------------------------------------------------------------------------*/
1376 #ifdef CONFIG_ARCH_LUBBOCK
1378 /* Lubbock has separate connect and disconnect irqs. More typical designs
1379 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1383 lubbock_vbus_irq(int irq, void *_dev)
1385 struct pxa25x_udc *dev = _dev;
1390 case LUBBOCK_USB_IRQ:
1392 disable_irq(LUBBOCK_USB_IRQ);
1393 enable_irq(LUBBOCK_USB_DISC_IRQ);
1395 case LUBBOCK_USB_DISC_IRQ:
1397 disable_irq(LUBBOCK_USB_DISC_IRQ);
1398 enable_irq(LUBBOCK_USB_IRQ);
1404 pxa25x_udc_vbus_session(&dev->gadget, vbus);
1411 /*-------------------------------------------------------------------------*/
1413 static inline void clear_ep_state (struct pxa25x_udc *dev)
1417 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1418 * fifos, and pending transactions mustn't be continued in any case.
1420 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1421 nuke(&dev->ep[i], -ECONNABORTED);
1424 static void udc_watchdog(unsigned long _dev)
1426 struct pxa25x_udc *dev = (void *)_dev;
1428 local_irq_disable();
1429 if (dev->ep0state == EP0_STALL
1430 && (UDCCS0 & UDCCS0_FST) == 0
1431 && (UDCCS0 & UDCCS0_SST) == 0) {
1432 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1433 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1434 start_watchdog(dev);
1439 static void handle_ep0 (struct pxa25x_udc *dev)
1441 u32 udccs0 = UDCCS0;
1442 struct pxa25x_ep *ep = &dev->ep [0];
1443 struct pxa25x_request *req;
1445 struct usb_ctrlrequest r;
1450 if (list_empty(&ep->queue))
1453 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1455 /* clear stall status */
1456 if (udccs0 & UDCCS0_SST) {
1458 UDCCS0 = UDCCS0_SST;
1459 del_timer(&dev->timer);
1463 /* previous request unfinished? non-error iff back-to-back ... */
1464 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1466 del_timer(&dev->timer);
1470 switch (dev->ep0state) {
1472 /* late-breaking status? */
1475 /* start control request? */
1476 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1477 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1482 /* read SETUP packet */
1483 for (i = 0; i < 8; i++) {
1484 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1486 DMSG("SETUP %d!\n", i);
1489 u.raw [i] = (u8) UDDR0;
1491 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1495 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1496 u.r.bRequestType, u.r.bRequest,
1497 le16_to_cpu(u.r.wValue),
1498 le16_to_cpu(u.r.wIndex),
1499 le16_to_cpu(u.r.wLength));
1501 /* cope with automagic for some standard requests. */
1502 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1503 == USB_TYPE_STANDARD;
1504 dev->req_config = 0;
1505 dev->req_pending = 1;
1506 switch (u.r.bRequest) {
1507 /* hardware restricts gadget drivers here! */
1508 case USB_REQ_SET_CONFIGURATION:
1509 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1510 /* reflect hardware's automagic
1511 * up to the gadget driver.
1514 dev->req_config = 1;
1515 clear_ep_state(dev);
1516 /* if !has_cfr, there's no synch
1517 * else use AREN (later) not SA|OPR
1518 * USIR0_IR0 acts edge sensitive
1522 /* ... and here, even more ... */
1523 case USB_REQ_SET_INTERFACE:
1524 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1525 /* udc hardware is broken by design:
1526 * - altsetting may only be zero;
1527 * - hw resets all interfaces' eps;
1528 * - ep reset doesn't include halt(?).
1530 DMSG("broken set_interface (%d/%d)\n",
1531 le16_to_cpu(u.r.wIndex),
1532 le16_to_cpu(u.r.wValue));
1536 /* hardware was supposed to hide this */
1537 case USB_REQ_SET_ADDRESS:
1538 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1539 ep0start(dev, 0, "address");
1545 if (u.r.bRequestType & USB_DIR_IN)
1546 dev->ep0state = EP0_IN_DATA_PHASE;
1548 dev->ep0state = EP0_OUT_DATA_PHASE;
1550 i = dev->driver->setup(&dev->gadget, &u.r);
1552 /* hardware automagic preventing STALL... */
1553 if (dev->req_config) {
1554 /* hardware sometimes neglects to tell
1555 * tell us about config change events,
1556 * so later ones may fail...
1558 WARNING("config change %02x fail %d?\n",
1561 /* TODO experiment: if has_cfr,
1562 * hardware didn't ACK; maybe we
1563 * could actually STALL!
1566 DBG(DBG_VERBOSE, "protocol STALL, "
1567 "%02x err %d\n", UDCCS0, i);
1569 /* the watchdog timer helps deal with cases
1570 * where udc seems to clear FST wrongly, and
1571 * then NAKs instead of STALLing.
1573 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1574 start_watchdog(dev);
1575 dev->ep0state = EP0_STALL;
1577 /* deferred i/o == no response yet */
1578 } else if (dev->req_pending) {
1579 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1580 || dev->req_std || u.r.wLength))
1581 ep0start(dev, 0, "defer");
1583 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1586 /* expect at least one data or status stage irq */
1589 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1590 == (UDCCS0_OPR|UDCCS0_SA))) {
1593 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1594 * still observed on a pxa255 a0.
1596 DBG(DBG_VERBOSE, "e131\n");
1599 /* read SETUP data, but don't trust it too much */
1600 for (i = 0; i < 8; i++)
1601 u.raw [i] = (u8) UDDR0;
1602 if ((u.r.bRequestType & USB_RECIP_MASK)
1605 if (u.word [0] == 0 && u.word [1] == 0)
1609 /* some random early IRQ:
1612 * - OPR got set, without SA (likely status stage)
1614 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1617 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1618 if (udccs0 & UDCCS0_OPR) {
1619 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1620 DBG(DBG_VERBOSE, "ep0in premature status\n");
1624 } else /* irq was IPR clearing */ {
1626 /* this IN packet might finish the request */
1627 (void) write_ep0_fifo(ep, req);
1628 } /* else IN token before response was written */
1631 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1632 if (udccs0 & UDCCS0_OPR) {
1634 /* this OUT packet might finish the request */
1635 if (read_ep0_fifo(ep, req))
1637 /* else more OUT packets expected */
1638 } /* else OUT token before read was issued */
1639 } else /* irq was IPR clearing */ {
1640 DBG(DBG_VERBOSE, "ep0out premature status\n");
1649 /* ack control-IN status (maybe in-zlp was skipped)
1650 * also appears after some config change events.
1652 if (udccs0 & UDCCS0_OPR)
1653 UDCCS0 = UDCCS0_OPR;
1657 UDCCS0 = UDCCS0_FST;
1663 static void handle_ep(struct pxa25x_ep *ep)
1665 struct pxa25x_request *req;
1666 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1672 if (likely (!list_empty(&ep->queue)))
1673 req = list_entry(ep->queue.next,
1674 struct pxa25x_request, queue);
1678 // TODO check FST handling
1680 udccs = *ep->reg_udccs;
1681 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1683 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1684 tmp |= UDCCS_BI_SST;
1687 *ep->reg_udccs = tmp;
1688 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1689 completed = write_fifo(ep, req);
1691 } else { /* irq from RPC (or for ISO, ROF) */
1692 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1693 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1695 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1698 *ep->reg_udccs = tmp;
1700 /* fifos can hold packets, ready for reading... */
1702 completed = read_fifo(ep, req);
1704 pio_irq_disable (ep->bEndpointAddress);
1707 } while (completed);
1711 * pxa25x_udc_irq - interrupt handler
1713 * avoid delays in ep0 processing. the control handshaking isn't always
1714 * under software control (pxa250c0 and the pxa255 are better), and delays
1715 * could cause usb protocol errors.
1718 pxa25x_udc_irq(int irq, void *_dev)
1720 struct pxa25x_udc *dev = _dev;
1729 /* SUSpend Interrupt Request */
1730 if (unlikely(udccr & UDCCR_SUSIR)) {
1731 udc_ack_int_UDCCR(UDCCR_SUSIR);
1733 DBG(DBG_VERBOSE, "USB suspend\n");
1735 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1737 && dev->driver->suspend)
1738 dev->driver->suspend(&dev->gadget);
1742 /* RESume Interrupt Request */
1743 if (unlikely(udccr & UDCCR_RESIR)) {
1744 udc_ack_int_UDCCR(UDCCR_RESIR);
1746 DBG(DBG_VERBOSE, "USB resume\n");
1748 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1750 && dev->driver->resume)
1751 dev->driver->resume(&dev->gadget);
1754 /* ReSeT Interrupt Request - USB reset */
1755 if (unlikely(udccr & UDCCR_RSTIR)) {
1756 udc_ack_int_UDCCR(UDCCR_RSTIR);
1759 if ((UDCCR & UDCCR_UDA) == 0) {
1760 DBG(DBG_VERBOSE, "USB reset start\n");
1762 /* reset driver and endpoints,
1763 * in case that's not yet done
1765 stop_activity (dev, dev->driver);
1768 DBG(DBG_VERBOSE, "USB reset end\n");
1769 dev->gadget.speed = USB_SPEED_FULL;
1770 memset(&dev->stats, 0, sizeof dev->stats);
1771 /* driver and endpoints are still reset */
1775 u32 usir0 = USIR0 & ~UICR0;
1776 u32 usir1 = USIR1 & ~UICR1;
1779 if (unlikely (!usir0 && !usir1))
1782 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1784 /* control traffic */
1785 if (usir0 & USIR0_IR0) {
1786 dev->ep[0].pio_irqs++;
1791 /* endpoint data transfers */
1792 for (i = 0; i < 8; i++) {
1795 if (i && (usir0 & tmp)) {
1796 handle_ep(&dev->ep[i]);
1800 #ifndef CONFIG_USB_PXA25X_SMALL
1802 handle_ep(&dev->ep[i+8]);
1810 /* we could also ask for 1 msec SOF (SIR) interrupts */
1816 /*-------------------------------------------------------------------------*/
1818 static void nop_release (struct device *dev)
1820 DMSG("%s %s\n", __func__, dev_name(dev));
1823 /* this uses load-time allocation and initialization (instead of
1824 * doing it at run-time) to save code, eliminate fault paths, and
1825 * be more obviously correct.
1827 static struct pxa25x_udc memory = {
1829 .ops = &pxa25x_udc_ops,
1830 .ep0 = &memory.ep[0].ep,
1831 .name = driver_name,
1833 .init_name = "gadget",
1834 .release = nop_release,
1838 /* control endpoint */
1842 .ops = &pxa25x_ep_ops,
1843 .maxpacket = EP0_FIFO_SIZE,
1846 .reg_udccs = &UDCCS0,
1850 /* first group of endpoints */
1853 .name = "ep1in-bulk",
1854 .ops = &pxa25x_ep_ops,
1855 .maxpacket = BULK_FIFO_SIZE,
1858 .fifo_size = BULK_FIFO_SIZE,
1859 .bEndpointAddress = USB_DIR_IN | 1,
1860 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1861 .reg_udccs = &UDCCS1,
1866 .name = "ep2out-bulk",
1867 .ops = &pxa25x_ep_ops,
1868 .maxpacket = BULK_FIFO_SIZE,
1871 .fifo_size = BULK_FIFO_SIZE,
1872 .bEndpointAddress = 2,
1873 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1874 .reg_udccs = &UDCCS2,
1878 #ifndef CONFIG_USB_PXA25X_SMALL
1881 .name = "ep3in-iso",
1882 .ops = &pxa25x_ep_ops,
1883 .maxpacket = ISO_FIFO_SIZE,
1886 .fifo_size = ISO_FIFO_SIZE,
1887 .bEndpointAddress = USB_DIR_IN | 3,
1888 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1889 .reg_udccs = &UDCCS3,
1894 .name = "ep4out-iso",
1895 .ops = &pxa25x_ep_ops,
1896 .maxpacket = ISO_FIFO_SIZE,
1899 .fifo_size = ISO_FIFO_SIZE,
1900 .bEndpointAddress = 4,
1901 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1902 .reg_udccs = &UDCCS4,
1908 .name = "ep5in-int",
1909 .ops = &pxa25x_ep_ops,
1910 .maxpacket = INT_FIFO_SIZE,
1913 .fifo_size = INT_FIFO_SIZE,
1914 .bEndpointAddress = USB_DIR_IN | 5,
1915 .bmAttributes = USB_ENDPOINT_XFER_INT,
1916 .reg_udccs = &UDCCS5,
1920 /* second group of endpoints */
1923 .name = "ep6in-bulk",
1924 .ops = &pxa25x_ep_ops,
1925 .maxpacket = BULK_FIFO_SIZE,
1928 .fifo_size = BULK_FIFO_SIZE,
1929 .bEndpointAddress = USB_DIR_IN | 6,
1930 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1931 .reg_udccs = &UDCCS6,
1936 .name = "ep7out-bulk",
1937 .ops = &pxa25x_ep_ops,
1938 .maxpacket = BULK_FIFO_SIZE,
1941 .fifo_size = BULK_FIFO_SIZE,
1942 .bEndpointAddress = 7,
1943 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1944 .reg_udccs = &UDCCS7,
1950 .name = "ep8in-iso",
1951 .ops = &pxa25x_ep_ops,
1952 .maxpacket = ISO_FIFO_SIZE,
1955 .fifo_size = ISO_FIFO_SIZE,
1956 .bEndpointAddress = USB_DIR_IN | 8,
1957 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1958 .reg_udccs = &UDCCS8,
1963 .name = "ep9out-iso",
1964 .ops = &pxa25x_ep_ops,
1965 .maxpacket = ISO_FIFO_SIZE,
1968 .fifo_size = ISO_FIFO_SIZE,
1969 .bEndpointAddress = 9,
1970 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1971 .reg_udccs = &UDCCS9,
1977 .name = "ep10in-int",
1978 .ops = &pxa25x_ep_ops,
1979 .maxpacket = INT_FIFO_SIZE,
1982 .fifo_size = INT_FIFO_SIZE,
1983 .bEndpointAddress = USB_DIR_IN | 10,
1984 .bmAttributes = USB_ENDPOINT_XFER_INT,
1985 .reg_udccs = &UDCCS10,
1986 .reg_uddr = &UDDR10,
1989 /* third group of endpoints */
1992 .name = "ep11in-bulk",
1993 .ops = &pxa25x_ep_ops,
1994 .maxpacket = BULK_FIFO_SIZE,
1997 .fifo_size = BULK_FIFO_SIZE,
1998 .bEndpointAddress = USB_DIR_IN | 11,
1999 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2000 .reg_udccs = &UDCCS11,
2001 .reg_uddr = &UDDR11,
2005 .name = "ep12out-bulk",
2006 .ops = &pxa25x_ep_ops,
2007 .maxpacket = BULK_FIFO_SIZE,
2010 .fifo_size = BULK_FIFO_SIZE,
2011 .bEndpointAddress = 12,
2012 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2013 .reg_udccs = &UDCCS12,
2014 .reg_ubcr = &UBCR12,
2015 .reg_uddr = &UDDR12,
2019 .name = "ep13in-iso",
2020 .ops = &pxa25x_ep_ops,
2021 .maxpacket = ISO_FIFO_SIZE,
2024 .fifo_size = ISO_FIFO_SIZE,
2025 .bEndpointAddress = USB_DIR_IN | 13,
2026 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2027 .reg_udccs = &UDCCS13,
2028 .reg_uddr = &UDDR13,
2032 .name = "ep14out-iso",
2033 .ops = &pxa25x_ep_ops,
2034 .maxpacket = ISO_FIFO_SIZE,
2037 .fifo_size = ISO_FIFO_SIZE,
2038 .bEndpointAddress = 14,
2039 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2040 .reg_udccs = &UDCCS14,
2041 .reg_ubcr = &UBCR14,
2042 .reg_uddr = &UDDR14,
2046 .name = "ep15in-int",
2047 .ops = &pxa25x_ep_ops,
2048 .maxpacket = INT_FIFO_SIZE,
2051 .fifo_size = INT_FIFO_SIZE,
2052 .bEndpointAddress = USB_DIR_IN | 15,
2053 .bmAttributes = USB_ENDPOINT_XFER_INT,
2054 .reg_udccs = &UDCCS15,
2055 .reg_uddr = &UDDR15,
2057 #endif /* !CONFIG_USB_PXA25X_SMALL */
2060 #define CP15R0_VENDOR_MASK 0xffffe000
2062 #if defined(CONFIG_ARCH_PXA)
2063 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2065 #elif defined(CONFIG_ARCH_IXP4XX)
2066 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2070 #define CP15R0_PROD_MASK 0x000003f0
2071 #define PXA25x 0x00000100 /* and PXA26x */
2072 #define PXA210 0x00000120
2074 #define CP15R0_REV_MASK 0x0000000f
2076 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2078 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2079 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2080 #define PXA250_B2 0x00000104
2081 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2082 #define PXA250_B0 0x00000102
2083 #define PXA250_A1 0x00000101
2084 #define PXA250_A0 0x00000100
2086 #define PXA210_C0 0x00000125
2087 #define PXA210_B2 0x00000124
2088 #define PXA210_B1 0x00000123
2089 #define PXA210_B0 0x00000122
2090 #define IXP425_A0 0x000001c1
2091 #define IXP425_B0 0x000001f1
2092 #define IXP465_AD 0x00000200
2095 * probe - binds to the platform device
2097 static int __init pxa25x_udc_probe(struct platform_device *pdev)
2099 struct pxa25x_udc *dev = &memory;
2103 /* insist on Intel/ARM/XScale */
2104 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2105 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2106 pr_err("%s: not XScale!\n", driver_name);
2110 /* trigger chiprev-specific logic */
2111 switch (chiprev & CP15R0_PRODREV_MASK) {
2112 #if defined(CONFIG_ARCH_PXA)
2118 /* A0/A1 "not released"; ep 13, 15 unusable */
2120 case PXA250_B2: case PXA210_B2:
2121 case PXA250_B1: case PXA210_B1:
2122 case PXA250_B0: case PXA210_B0:
2123 /* OUT-DMA is broken ... */
2125 case PXA250_C0: case PXA210_C0:
2127 #elif defined(CONFIG_ARCH_IXP4XX)
2135 pr_err("%s: unrecognized processor: %08x\n",
2136 driver_name, chiprev);
2137 /* iop3xx, ixp4xx, ... */
2141 irq = platform_get_irq(pdev, 0);
2145 dev->clk = clk_get(&pdev->dev, NULL);
2146 if (IS_ERR(dev->clk)) {
2147 retval = PTR_ERR(dev->clk);
2151 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
2152 dev->has_cfr ? "" : " (!cfr)",
2156 /* other non-static parts of init */
2157 dev->dev = &pdev->dev;
2158 dev->mach = pdev->dev.platform_data;
2160 dev->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
2162 if (gpio_is_valid(dev->mach->gpio_pullup)) {
2163 if ((retval = gpio_request(dev->mach->gpio_pullup,
2164 "pca25x_udc GPIO PULLUP"))) {
2166 "can't get pullup gpio %d, err: %d\n",
2167 dev->mach->gpio_pullup, retval);
2168 goto err_gpio_pullup;
2170 gpio_direction_output(dev->mach->gpio_pullup, 0);
2173 init_timer(&dev->timer);
2174 dev->timer.function = udc_watchdog;
2175 dev->timer.data = (unsigned long) dev;
2177 device_initialize(&dev->gadget.dev);
2178 dev->gadget.dev.parent = &pdev->dev;
2179 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2181 the_controller = dev;
2182 platform_set_drvdata(pdev, dev);
2189 /* irq setup after old hardware state is cleaned up */
2190 retval = request_irq(irq, pxa25x_udc_irq,
2191 0, driver_name, dev);
2193 pr_err("%s: can't get irq %d, err %d\n",
2194 driver_name, irq, retval);
2199 #ifdef CONFIG_ARCH_LUBBOCK
2200 if (machine_is_lubbock()) {
2201 retval = request_irq(LUBBOCK_USB_DISC_IRQ, lubbock_vbus_irq,
2202 0, driver_name, dev);
2204 pr_err("%s: can't get irq %i, err %d\n",
2205 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2208 retval = request_irq(LUBBOCK_USB_IRQ, lubbock_vbus_irq,
2209 0, driver_name, dev);
2211 pr_err("%s: can't get irq %i, err %d\n",
2212 driver_name, LUBBOCK_USB_IRQ, retval);
2217 create_debug_files(dev);
2219 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2223 remove_debug_files(dev);
2224 #ifdef CONFIG_ARCH_LUBBOCK
2226 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2231 if (gpio_is_valid(dev->mach->gpio_pullup))
2232 gpio_free(dev->mach->gpio_pullup);
2234 if (!IS_ERR_OR_NULL(dev->transceiver)) {
2235 usb_put_phy(dev->transceiver);
2236 dev->transceiver = NULL;
2243 static void pxa25x_udc_shutdown(struct platform_device *_dev)
2248 static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2250 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
2252 usb_del_gadget_udc(&dev->gadget);
2259 remove_debug_files(dev);
2262 free_irq(platform_get_irq(pdev, 0), dev);
2265 #ifdef CONFIG_ARCH_LUBBOCK
2266 if (machine_is_lubbock()) {
2267 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2268 free_irq(LUBBOCK_USB_IRQ, dev);
2271 if (gpio_is_valid(dev->mach->gpio_pullup))
2272 gpio_free(dev->mach->gpio_pullup);
2276 if (!IS_ERR_OR_NULL(dev->transceiver)) {
2277 usb_put_phy(dev->transceiver);
2278 dev->transceiver = NULL;
2281 platform_set_drvdata(pdev, NULL);
2282 the_controller = NULL;
2286 /*-------------------------------------------------------------------------*/
2290 /* USB suspend (controlled by the host) and system suspend (controlled
2291 * by the PXA) don't necessarily work well together. If USB is active,
2292 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2293 * mode, or any deeper PM saving state.
2295 * For now, we punt and forcibly disconnect from the USB host when PXA
2296 * enters any suspend state. While we're disconnected, we always disable
2297 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2298 * Boards without software pullup control shouldn't use those states.
2299 * VBUS IRQs should probably be ignored so that the PXA device just acts
2300 * "dead" to USB hosts until system resume.
2302 static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
2304 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2305 unsigned long flags;
2307 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
2308 WARNING("USB host won't detect disconnect!\n");
2311 local_irq_save(flags);
2313 local_irq_restore(flags);
2318 static int pxa25x_udc_resume(struct platform_device *dev)
2320 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2321 unsigned long flags;
2324 local_irq_save(flags);
2326 local_irq_restore(flags);
2332 #define pxa25x_udc_suspend NULL
2333 #define pxa25x_udc_resume NULL
2336 /*-------------------------------------------------------------------------*/
2338 static struct platform_driver udc_driver = {
2339 .shutdown = pxa25x_udc_shutdown,
2340 .remove = __exit_p(pxa25x_udc_remove),
2341 .suspend = pxa25x_udc_suspend,
2342 .resume = pxa25x_udc_resume,
2344 .owner = THIS_MODULE,
2345 .name = "pxa25x-udc",
2349 static int __init udc_init(void)
2351 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
2352 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
2354 module_init(udc_init);
2356 static void __exit udc_exit(void)
2358 platform_driver_unregister(&udc_driver);
2360 module_exit(udc_exit);
2362 MODULE_DESCRIPTION(DRIVER_DESC);
2363 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2364 MODULE_LICENSE("GPL");
2365 MODULE_ALIAS("platform:pxa25x-udc");