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/delay.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/timer.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
31 #include <linux/platform_device.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/irq.h>
34 #include <linux/clk.h>
35 #include <linux/err.h>
36 #include <linux/seq_file.h>
37 #include <linux/debugfs.h>
39 #include <linux/prefetch.h>
41 #include <asm/byteorder.h>
44 #include <asm/system.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>
63 #include <asm/mach/udc_pxa2xx.h>
67 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
68 * series processors. The UDC for the IXP 4xx series is very similar.
69 * There are fifteen endpoints, in addition to ep0.
71 * Such controller drivers work with a gadget driver. The gadget driver
72 * returns descriptors, implements configuration and data protocols used
73 * by the host to interact with this device, and allocates endpoints to
74 * the different protocol interfaces. The controller driver virtualizes
75 * usb hardware so that the gadget drivers will be more portable.
77 * This UDC hardware wants to implement a bit too much USB protocol, so
78 * it constrains the sorts of USB configuration change events that work.
79 * The errata for these chips are misleading; some "fixed" bugs from
80 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
82 * Note that the UDC hardware supports DMA (except on IXP) but that's
83 * not used here. IN-DMA (to host) is simple enough, when the data is
84 * suitably aligned (16 bytes) ... the network stack doesn't do that,
85 * other software can. OUT-DMA is buggy in most chip versions, as well
86 * as poorly designed (data toggle not automatic). So this driver won't
87 * bother using DMA. (Mostly-working IN-DMA support was available in
88 * kernels before 2.6.23, but was never enabled or well tested.)
91 #define DRIVER_VERSION "30-June-2007"
92 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
95 static const char driver_name
[] = "pxa25x_udc";
97 static const char ep0name
[] = "ep0";
100 #ifdef CONFIG_ARCH_IXP4XX
102 /* cpu-specific register addresses are compiled in to this code */
103 #ifdef CONFIG_ARCH_PXA
104 #error "Can't configure both IXP and PXA"
107 /* IXP doesn't yet support <linux/clk.h> */
108 #define clk_get(dev,name) NULL
109 #define clk_enable(clk) do { } while (0)
110 #define clk_disable(clk) do { } while (0)
111 #define clk_put(clk) do { } while (0)
115 #include "pxa25x_udc.h"
118 #ifdef CONFIG_USB_PXA25X_SMALL
119 #define SIZE_STR " (small)"
124 /* ---------------------------------------------------------------------------
125 * endpoint related parts of the api to the usb controller hardware,
126 * used by gadget driver; and the inner talker-to-hardware core.
127 * ---------------------------------------------------------------------------
130 static void pxa25x_ep_fifo_flush (struct usb_ep
*ep
);
131 static void nuke (struct pxa25x_ep
*, int status
);
133 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
134 static void pullup_off(void)
136 struct pxa2xx_udc_mach_info
*mach
= the_controller
->mach
;
137 int off_level
= mach
->gpio_pullup_inverted
;
139 if (gpio_is_valid(mach
->gpio_pullup
))
140 gpio_set_value(mach
->gpio_pullup
, off_level
);
141 else if (mach
->udc_command
)
142 mach
->udc_command(PXA2XX_UDC_CMD_DISCONNECT
);
145 static void pullup_on(void)
147 struct pxa2xx_udc_mach_info
*mach
= the_controller
->mach
;
148 int on_level
= !mach
->gpio_pullup_inverted
;
150 if (gpio_is_valid(mach
->gpio_pullup
))
151 gpio_set_value(mach
->gpio_pullup
, on_level
);
152 else if (mach
->udc_command
)
153 mach
->udc_command(PXA2XX_UDC_CMD_CONNECT
);
156 static void pio_irq_enable(int bEndpointAddress
)
158 bEndpointAddress
&= 0xf;
159 if (bEndpointAddress
< 8)
160 UICR0
&= ~(1 << bEndpointAddress
);
162 bEndpointAddress
-= 8;
163 UICR1
&= ~(1 << bEndpointAddress
);
167 static void pio_irq_disable(int bEndpointAddress
)
169 bEndpointAddress
&= 0xf;
170 if (bEndpointAddress
< 8)
171 UICR0
|= 1 << bEndpointAddress
;
173 bEndpointAddress
-= 8;
174 UICR1
|= 1 << bEndpointAddress
;
178 /* The UDCCR reg contains mask and interrupt status bits,
179 * so using '|=' isn't safe as it may ack an interrupt.
181 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
183 static inline void udc_set_mask_UDCCR(int mask
)
185 UDCCR
= (UDCCR
& UDCCR_MASK_BITS
) | (mask
& UDCCR_MASK_BITS
);
188 static inline void udc_clear_mask_UDCCR(int mask
)
190 UDCCR
= (UDCCR
& UDCCR_MASK_BITS
) & ~(mask
& UDCCR_MASK_BITS
);
193 static inline void udc_ack_int_UDCCR(int mask
)
195 /* udccr contains the bits we dont want to change */
196 __u32 udccr
= UDCCR
& UDCCR_MASK_BITS
;
198 UDCCR
= udccr
| (mask
& ~UDCCR_MASK_BITS
);
202 * endpoint enable/disable
204 * we need to verify the descriptors used to enable endpoints. since pxa25x
205 * endpoint configurations are fixed, and are pretty much always enabled,
206 * there's not a lot to manage here.
208 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
209 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
210 * for a single interface (with only the default altsetting) and for gadget
211 * drivers that don't halt endpoints (not reset by set_interface). that also
212 * means that if you use ISO, you must violate the USB spec rule that all
213 * iso endpoints must be in non-default altsettings.
215 static int pxa25x_ep_enable (struct usb_ep
*_ep
,
216 const struct usb_endpoint_descriptor
*desc
)
218 struct pxa25x_ep
*ep
;
219 struct pxa25x_udc
*dev
;
221 ep
= container_of (_ep
, struct pxa25x_ep
, ep
);
222 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
223 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
224 || ep
->bEndpointAddress
!= desc
->bEndpointAddress
225 || ep
->fifo_size
< usb_endpoint_maxp (desc
)) {
226 DMSG("%s, bad ep or descriptor\n", __func__
);
230 /* xfer types must match, except that interrupt ~= bulk */
231 if (ep
->bmAttributes
!= desc
->bmAttributes
232 && ep
->bmAttributes
!= USB_ENDPOINT_XFER_BULK
233 && desc
->bmAttributes
!= USB_ENDPOINT_XFER_INT
) {
234 DMSG("%s, %s type mismatch\n", __func__
, _ep
->name
);
238 /* hardware _could_ do smaller, but driver doesn't */
239 if ((desc
->bmAttributes
== USB_ENDPOINT_XFER_BULK
240 && usb_endpoint_maxp (desc
)
242 || !desc
->wMaxPacketSize
) {
243 DMSG("%s, bad %s maxpacket\n", __func__
, _ep
->name
);
248 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
249 DMSG("%s, bogus device state\n", __func__
);
256 ep
->ep
.maxpacket
= usb_endpoint_maxp (desc
);
258 /* flush fifo (mostly for OUT buffers) */
259 pxa25x_ep_fifo_flush (_ep
);
261 /* ... reset halt state too, if we could ... */
263 DBG(DBG_VERBOSE
, "enabled %s\n", _ep
->name
);
267 static int pxa25x_ep_disable (struct usb_ep
*_ep
)
269 struct pxa25x_ep
*ep
;
272 ep
= container_of (_ep
, struct pxa25x_ep
, ep
);
273 if (!_ep
|| !ep
->desc
) {
274 DMSG("%s, %s not enabled\n", __func__
,
275 _ep
? ep
->ep
.name
: NULL
);
278 local_irq_save(flags
);
280 nuke (ep
, -ESHUTDOWN
);
282 /* flush fifo (mostly for IN buffers) */
283 pxa25x_ep_fifo_flush (_ep
);
289 local_irq_restore(flags
);
290 DBG(DBG_VERBOSE
, "%s disabled\n", _ep
->name
);
294 /*-------------------------------------------------------------------------*/
296 /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
297 * must still pass correctly initialized endpoints, since other controller
298 * drivers may care about how it's currently set up (dma issues etc).
302 * pxa25x_ep_alloc_request - allocate a request data structure
304 static struct usb_request
*
305 pxa25x_ep_alloc_request (struct usb_ep
*_ep
, gfp_t gfp_flags
)
307 struct pxa25x_request
*req
;
309 req
= kzalloc(sizeof(*req
), gfp_flags
);
313 INIT_LIST_HEAD (&req
->queue
);
319 * pxa25x_ep_free_request - deallocate a request data structure
322 pxa25x_ep_free_request (struct usb_ep
*_ep
, struct usb_request
*_req
)
324 struct pxa25x_request
*req
;
326 req
= container_of (_req
, struct pxa25x_request
, req
);
327 WARN_ON(!list_empty (&req
->queue
));
331 /*-------------------------------------------------------------------------*/
334 * done - retire a request; caller blocked irqs
336 static void done(struct pxa25x_ep
*ep
, struct pxa25x_request
*req
, int status
)
338 unsigned stopped
= ep
->stopped
;
340 list_del_init(&req
->queue
);
342 if (likely (req
->req
.status
== -EINPROGRESS
))
343 req
->req
.status
= status
;
345 status
= req
->req
.status
;
347 if (status
&& status
!= -ESHUTDOWN
)
348 DBG(DBG_VERBOSE
, "complete %s req %p stat %d len %u/%u\n",
349 ep
->ep
.name
, &req
->req
, status
,
350 req
->req
.actual
, req
->req
.length
);
352 /* don't modify queue heads during completion callback */
354 req
->req
.complete(&ep
->ep
, &req
->req
);
355 ep
->stopped
= stopped
;
359 static inline void ep0_idle (struct pxa25x_udc
*dev
)
361 dev
->ep0state
= EP0_IDLE
;
365 write_packet(volatile u32
*uddr
, struct pxa25x_request
*req
, unsigned max
)
368 unsigned length
, count
;
370 buf
= req
->req
.buf
+ req
->req
.actual
;
373 /* how big will this packet be? */
374 length
= min(req
->req
.length
- req
->req
.actual
, max
);
375 req
->req
.actual
+= length
;
378 while (likely(count
--))
385 * write to an IN endpoint fifo, as many packets as possible.
386 * irqs will use this to write the rest later.
387 * caller guarantees at least one packet buffer is ready (or a zlp).
390 write_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
394 max
= usb_endpoint_maxp(ep
->desc
);
397 int is_last
, is_short
;
399 count
= write_packet(ep
->reg_uddr
, req
, max
);
401 /* last packet is usually short (or a zlp) */
402 if (unlikely (count
!= max
))
403 is_last
= is_short
= 1;
405 if (likely(req
->req
.length
!= req
->req
.actual
)
410 /* interrupt/iso maxpacket may not fill the fifo */
411 is_short
= unlikely (max
< ep
->fifo_size
);
414 DBG(DBG_VERY_NOISY
, "wrote %s %d bytes%s%s %d left %p\n",
416 is_last
? "/L" : "", is_short
? "/S" : "",
417 req
->req
.length
- req
->req
.actual
, req
);
419 /* let loose that packet. maybe try writing another one,
420 * double buffering might work. TSP, TPC, and TFS
421 * bit values are the same for all normal IN endpoints.
423 *ep
->reg_udccs
= UDCCS_BI_TPC
;
425 *ep
->reg_udccs
= UDCCS_BI_TSP
;
427 /* requests complete when all IN data is in the FIFO */
430 if (list_empty(&ep
->queue
))
431 pio_irq_disable (ep
->bEndpointAddress
);
435 // TODO experiment: how robust can fifo mode tweaking be?
436 // double buffering is off in the default fifo mode, which
437 // prevents TFS from being set here.
439 } while (*ep
->reg_udccs
& UDCCS_BI_TFS
);
443 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
444 * ep0 data stage. these chips want very simple state transitions.
447 void ep0start(struct pxa25x_udc
*dev
, u32 flags
, const char *tag
)
449 UDCCS0
= flags
|UDCCS0_SA
|UDCCS0_OPR
;
451 dev
->req_pending
= 0;
452 DBG(DBG_VERY_NOISY
, "%s %s, %02x/%02x\n",
453 __func__
, tag
, UDCCS0
, flags
);
457 write_ep0_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
462 count
= write_packet(&UDDR0
, req
, EP0_FIFO_SIZE
);
463 ep
->dev
->stats
.write
.bytes
+= count
;
465 /* last packet "must be" short (or a zlp) */
466 is_short
= (count
!= EP0_FIFO_SIZE
);
468 DBG(DBG_VERY_NOISY
, "ep0in %d bytes %d left %p\n", count
,
469 req
->req
.length
- req
->req
.actual
, req
);
471 if (unlikely (is_short
)) {
472 if (ep
->dev
->req_pending
)
473 ep0start(ep
->dev
, UDCCS0_IPR
, "short IN");
477 count
= req
->req
.length
;
480 #ifndef CONFIG_ARCH_IXP4XX
482 /* This seems to get rid of lost status irqs in some cases:
483 * host responds quickly, or next request involves config
484 * change automagic, or should have been hidden, or ...
486 * FIXME get rid of all udelays possible...
488 if (count
>= EP0_FIFO_SIZE
) {
491 if ((UDCCS0
& UDCCS0_OPR
) != 0) {
492 /* clear OPR, generate ack */
502 } else if (ep
->dev
->req_pending
)
503 ep0start(ep
->dev
, 0, "IN");
509 * read_fifo - unload packet(s) from the fifo we use for usb OUT
510 * transfers and put them into the request. caller should have made
511 * sure there's at least one packet ready.
513 * returns true if the request completed because of short packet or the
514 * request buffer having filled (and maybe overran till end-of-packet).
517 read_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
522 unsigned bufferspace
, count
, is_short
;
524 /* make sure there's a packet in the FIFO.
525 * UDCCS_{BO,IO}_RPC are all the same bit value.
526 * UDCCS_{BO,IO}_RNE are all the same bit value.
528 udccs
= *ep
->reg_udccs
;
529 if (unlikely ((udccs
& UDCCS_BO_RPC
) == 0))
531 buf
= req
->req
.buf
+ req
->req
.actual
;
533 bufferspace
= req
->req
.length
- req
->req
.actual
;
535 /* read all bytes from this packet */
536 if (likely (udccs
& UDCCS_BO_RNE
)) {
537 count
= 1 + (0x0ff & *ep
->reg_ubcr
);
538 req
->req
.actual
+= min (count
, bufferspace
);
541 is_short
= (count
< ep
->ep
.maxpacket
);
542 DBG(DBG_VERY_NOISY
, "read %s %02x, %d bytes%s req %p %d/%d\n",
543 ep
->ep
.name
, udccs
, count
,
544 is_short
? "/S" : "",
545 req
, req
->req
.actual
, req
->req
.length
);
546 while (likely (count
-- != 0)) {
547 u8 byte
= (u8
) *ep
->reg_uddr
;
549 if (unlikely (bufferspace
== 0)) {
550 /* this happens when the driver's buffer
551 * is smaller than what the host sent.
552 * discard the extra data.
554 if (req
->req
.status
!= -EOVERFLOW
)
555 DMSG("%s overflow %d\n",
557 req
->req
.status
= -EOVERFLOW
;
563 *ep
->reg_udccs
= UDCCS_BO_RPC
;
564 /* RPC/RSP/RNE could now reflect the other packet buffer */
566 /* iso is one request per packet */
567 if (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
568 if (udccs
& UDCCS_IO_ROF
)
569 req
->req
.status
= -EHOSTUNREACH
;
570 /* more like "is_done" */
575 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
577 if (list_empty(&ep
->queue
))
578 pio_irq_disable (ep
->bEndpointAddress
);
582 /* finished that packet. the next one may be waiting... */
588 * special ep0 version of the above. no UBCR0 or double buffering; status
589 * handshaking is magic. most device protocols don't need control-OUT.
590 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
591 * protocols do use them.
594 read_ep0_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
597 unsigned bufferspace
;
599 buf
= req
->req
.buf
+ req
->req
.actual
;
600 bufferspace
= req
->req
.length
- req
->req
.actual
;
602 while (UDCCS0
& UDCCS0_RNE
) {
605 if (unlikely (bufferspace
== 0)) {
606 /* this happens when the driver's buffer
607 * is smaller than what the host sent.
608 * discard the extra data.
610 if (req
->req
.status
!= -EOVERFLOW
)
611 DMSG("%s overflow\n", ep
->ep
.name
);
612 req
->req
.status
= -EOVERFLOW
;
620 UDCCS0
= UDCCS0_OPR
| UDCCS0_IPR
;
623 if (req
->req
.actual
>= req
->req
.length
)
626 /* finished that packet. the next one may be waiting... */
630 /*-------------------------------------------------------------------------*/
633 pxa25x_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
635 struct pxa25x_request
*req
;
636 struct pxa25x_ep
*ep
;
637 struct pxa25x_udc
*dev
;
640 req
= container_of(_req
, struct pxa25x_request
, req
);
641 if (unlikely (!_req
|| !_req
->complete
|| !_req
->buf
642 || !list_empty(&req
->queue
))) {
643 DMSG("%s, bad params\n", __func__
);
647 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
648 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
649 DMSG("%s, bad ep\n", __func__
);
654 if (unlikely (!dev
->driver
655 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
656 DMSG("%s, bogus device state\n", __func__
);
660 /* iso is always one packet per request, that's the only way
661 * we can report per-packet status. that also helps with dma.
663 if (unlikely (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
664 && req
->req
.length
> usb_endpoint_maxp (ep
->desc
)))
667 DBG(DBG_NOISY
, "%s queue req %p, len %d buf %p\n",
668 _ep
->name
, _req
, _req
->length
, _req
->buf
);
670 local_irq_save(flags
);
672 _req
->status
= -EINPROGRESS
;
675 /* kickstart this i/o queue? */
676 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
677 if (ep
->desc
== NULL
/* ep0 */) {
678 unsigned length
= _req
->length
;
680 switch (dev
->ep0state
) {
681 case EP0_IN_DATA_PHASE
:
682 dev
->stats
.write
.ops
++;
683 if (write_ep0_fifo(ep
, req
))
687 case EP0_OUT_DATA_PHASE
:
688 dev
->stats
.read
.ops
++;
690 if (dev
->req_config
) {
691 DBG(DBG_VERBOSE
, "ep0 config ack%s\n",
692 dev
->has_cfr
? "" : " raced");
694 UDCCFR
= UDCCFR_AREN
|UDCCFR_ACM
697 dev
->ep0state
= EP0_END_XFER
;
698 local_irq_restore (flags
);
701 if (dev
->req_pending
)
702 ep0start(dev
, UDCCS0_IPR
, "OUT");
703 if (length
== 0 || ((UDCCS0
& UDCCS0_RNE
) != 0
704 && read_ep0_fifo(ep
, req
))) {
712 DMSG("ep0 i/o, odd state %d\n", dev
->ep0state
);
713 local_irq_restore (flags
);
716 /* can the FIFO can satisfy the request immediately? */
717 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
718 if ((*ep
->reg_udccs
& UDCCS_BI_TFS
) != 0
719 && write_fifo(ep
, req
))
721 } else if ((*ep
->reg_udccs
& UDCCS_BO_RFS
) != 0
722 && read_fifo(ep
, req
)) {
726 if (likely (req
&& ep
->desc
))
727 pio_irq_enable(ep
->bEndpointAddress
);
730 /* pio or dma irq handler advances the queue. */
731 if (likely(req
!= NULL
))
732 list_add_tail(&req
->queue
, &ep
->queue
);
733 local_irq_restore(flags
);
740 * nuke - dequeue ALL requests
742 static void nuke(struct pxa25x_ep
*ep
, int status
)
744 struct pxa25x_request
*req
;
746 /* called with irqs blocked */
747 while (!list_empty(&ep
->queue
)) {
748 req
= list_entry(ep
->queue
.next
,
749 struct pxa25x_request
,
751 done(ep
, req
, status
);
754 pio_irq_disable (ep
->bEndpointAddress
);
758 /* dequeue JUST ONE request */
759 static int pxa25x_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
761 struct pxa25x_ep
*ep
;
762 struct pxa25x_request
*req
;
765 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
766 if (!_ep
|| ep
->ep
.name
== ep0name
)
769 local_irq_save(flags
);
771 /* make sure it's actually queued on this endpoint */
772 list_for_each_entry (req
, &ep
->queue
, queue
) {
773 if (&req
->req
== _req
)
776 if (&req
->req
!= _req
) {
777 local_irq_restore(flags
);
781 done(ep
, req
, -ECONNRESET
);
783 local_irq_restore(flags
);
787 /*-------------------------------------------------------------------------*/
789 static int pxa25x_ep_set_halt(struct usb_ep
*_ep
, int value
)
791 struct pxa25x_ep
*ep
;
794 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
796 || (!ep
->desc
&& ep
->ep
.name
!= ep0name
))
797 || ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
798 DMSG("%s, bad ep\n", __func__
);
802 /* this path (reset toggle+halt) is needed to implement
803 * SET_INTERFACE on normal hardware. but it can't be
804 * done from software on the PXA UDC, and the hardware
805 * forgets to do it as part of SET_INTERFACE automagic.
807 DMSG("only host can clear %s halt\n", _ep
->name
);
811 local_irq_save(flags
);
813 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
814 && ((*ep
->reg_udccs
& UDCCS_BI_TFS
) == 0
815 || !list_empty(&ep
->queue
))) {
816 local_irq_restore(flags
);
820 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
821 *ep
->reg_udccs
= UDCCS_BI_FST
|UDCCS_BI_FTF
;
823 /* ep0 needs special care */
825 start_watchdog(ep
->dev
);
826 ep
->dev
->req_pending
= 0;
827 ep
->dev
->ep0state
= EP0_STALL
;
829 /* and bulk/intr endpoints like dropping stalls too */
832 for (i
= 0; i
< 1000; i
+= 20) {
833 if (*ep
->reg_udccs
& UDCCS_BI_SST
)
838 local_irq_restore(flags
);
840 DBG(DBG_VERBOSE
, "%s halt\n", _ep
->name
);
844 static int pxa25x_ep_fifo_status(struct usb_ep
*_ep
)
846 struct pxa25x_ep
*ep
;
848 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
850 DMSG("%s, bad ep\n", __func__
);
853 /* pxa can't report unclaimed bytes from IN fifos */
854 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0)
856 if (ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
857 || (*ep
->reg_udccs
& UDCCS_BO_RFS
) == 0)
860 return (*ep
->reg_ubcr
& 0xfff) + 1;
863 static void pxa25x_ep_fifo_flush(struct usb_ep
*_ep
)
865 struct pxa25x_ep
*ep
;
867 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
868 if (!_ep
|| ep
->ep
.name
== ep0name
|| !list_empty(&ep
->queue
)) {
869 DMSG("%s, bad ep\n", __func__
);
873 /* toggle and halt bits stay unchanged */
875 /* for OUT, just read and discard the FIFO contents. */
876 if ((ep
->bEndpointAddress
& USB_DIR_IN
) == 0) {
877 while (((*ep
->reg_udccs
) & UDCCS_BO_RNE
) != 0)
878 (void) *ep
->reg_uddr
;
882 /* most IN status is the same, but ISO can't stall */
883 *ep
->reg_udccs
= UDCCS_BI_TPC
|UDCCS_BI_FTF
|UDCCS_BI_TUR
884 | (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
889 static struct usb_ep_ops pxa25x_ep_ops
= {
890 .enable
= pxa25x_ep_enable
,
891 .disable
= pxa25x_ep_disable
,
893 .alloc_request
= pxa25x_ep_alloc_request
,
894 .free_request
= pxa25x_ep_free_request
,
896 .queue
= pxa25x_ep_queue
,
897 .dequeue
= pxa25x_ep_dequeue
,
899 .set_halt
= pxa25x_ep_set_halt
,
900 .fifo_status
= pxa25x_ep_fifo_status
,
901 .fifo_flush
= pxa25x_ep_fifo_flush
,
905 /* ---------------------------------------------------------------------------
906 * device-scoped parts of the api to the usb controller hardware
907 * ---------------------------------------------------------------------------
910 static int pxa25x_udc_get_frame(struct usb_gadget
*_gadget
)
912 return ((UFNRH
& 0x07) << 8) | (UFNRL
& 0xff);
915 static int pxa25x_udc_wakeup(struct usb_gadget
*_gadget
)
917 /* host may not have enabled remote wakeup */
918 if ((UDCCS0
& UDCCS0_DRWF
) == 0)
919 return -EHOSTUNREACH
;
920 udc_set_mask_UDCCR(UDCCR_RSM
);
924 static void stop_activity(struct pxa25x_udc
*, struct usb_gadget_driver
*);
925 static void udc_enable (struct pxa25x_udc
*);
926 static void udc_disable(struct pxa25x_udc
*);
928 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
931 static int pullup(struct pxa25x_udc
*udc
)
933 int is_active
= udc
->vbus
&& udc
->pullup
&& !udc
->suspended
;
934 DMSG("%s\n", is_active
? "active" : "inactive");
938 /* Enable clock for USB device */
939 clk_enable(udc
->clk
);
944 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
945 DMSG("disconnect %s\n", udc
->driver
946 ? udc
->driver
->driver
.name
948 stop_activity(udc
, udc
->driver
);
951 /* Disable clock for USB device */
952 clk_disable(udc
->clk
);
960 /* VBUS reporting logically comes from a transceiver */
961 static int pxa25x_udc_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
963 struct pxa25x_udc
*udc
;
965 udc
= container_of(_gadget
, struct pxa25x_udc
, gadget
);
966 udc
->vbus
= is_active
;
967 DMSG("vbus %s\n", is_active
? "supplied" : "inactive");
972 /* drivers may have software control over D+ pullup */
973 static int pxa25x_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
975 struct pxa25x_udc
*udc
;
977 udc
= container_of(_gadget
, struct pxa25x_udc
, gadget
);
979 /* not all boards support pullup control */
980 if (!gpio_is_valid(udc
->mach
->gpio_pullup
) && !udc
->mach
->udc_command
)
983 udc
->pullup
= (is_active
!= 0);
988 /* boards may consume current from VBUS, up to 100-500mA based on config.
989 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
992 static int pxa25x_udc_vbus_draw(struct usb_gadget
*_gadget
, unsigned mA
)
994 struct pxa25x_udc
*udc
;
996 udc
= container_of(_gadget
, struct pxa25x_udc
, gadget
);
998 if (udc
->transceiver
)
999 return otg_set_power(udc
->transceiver
, mA
);
1003 static int pxa25x_start(struct usb_gadget_driver
*driver
,
1004 int (*bind
)(struct usb_gadget
*));
1005 static int pxa25x_stop(struct usb_gadget_driver
*driver
);
1007 static const struct usb_gadget_ops pxa25x_udc_ops
= {
1008 .get_frame
= pxa25x_udc_get_frame
,
1009 .wakeup
= pxa25x_udc_wakeup
,
1010 .vbus_session
= pxa25x_udc_vbus_session
,
1011 .pullup
= pxa25x_udc_pullup
,
1012 .vbus_draw
= pxa25x_udc_vbus_draw
,
1013 .start
= pxa25x_start
,
1014 .stop
= pxa25x_stop
,
1017 /*-------------------------------------------------------------------------*/
1019 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1022 udc_seq_show(struct seq_file
*m
, void *_d
)
1024 struct pxa25x_udc
*dev
= m
->private;
1025 unsigned long flags
;
1029 local_irq_save(flags
);
1031 /* basic device status */
1032 seq_printf(m
, DRIVER_DESC
"\n"
1033 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1034 driver_name
, DRIVER_VERSION SIZE_STR
"(pio)",
1035 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1036 dev
->gadget
.speed
== USB_SPEED_FULL
? "full speed" : "disconnected");
1038 /* registers for device and ep0 */
1040 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1041 UICR1
, UICR0
, USIR1
, USIR0
, UFNRH
, UFNRL
);
1045 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp
,
1046 (tmp
& UDCCR_REM
) ? " rem" : "",
1047 (tmp
& UDCCR_RSTIR
) ? " rstir" : "",
1048 (tmp
& UDCCR_SRM
) ? " srm" : "",
1049 (tmp
& UDCCR_SUSIR
) ? " susir" : "",
1050 (tmp
& UDCCR_RESIR
) ? " resir" : "",
1051 (tmp
& UDCCR_RSM
) ? " rsm" : "",
1052 (tmp
& UDCCR_UDA
) ? " uda" : "",
1053 (tmp
& UDCCR_UDE
) ? " ude" : "");
1057 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp
,
1058 (tmp
& UDCCS0_SA
) ? " sa" : "",
1059 (tmp
& UDCCS0_RNE
) ? " rne" : "",
1060 (tmp
& UDCCS0_FST
) ? " fst" : "",
1061 (tmp
& UDCCS0_SST
) ? " sst" : "",
1062 (tmp
& UDCCS0_DRWF
) ? " dwrf" : "",
1063 (tmp
& UDCCS0_FTF
) ? " ftf" : "",
1064 (tmp
& UDCCS0_IPR
) ? " ipr" : "",
1065 (tmp
& UDCCS0_OPR
) ? " opr" : "");
1070 "udccfr %02X =%s%s\n", tmp
,
1071 (tmp
& UDCCFR_AREN
) ? " aren" : "",
1072 (tmp
& UDCCFR_ACM
) ? " acm" : "");
1075 if (dev
->gadget
.speed
!= USB_SPEED_FULL
|| !dev
->driver
)
1078 seq_printf(m
, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1079 dev
->stats
.write
.bytes
, dev
->stats
.write
.ops
,
1080 dev
->stats
.read
.bytes
, dev
->stats
.read
.ops
,
1083 /* dump endpoint queues */
1084 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1085 struct pxa25x_ep
*ep
= &dev
->ep
[i
];
1086 struct pxa25x_request
*req
;
1089 const struct usb_endpoint_descriptor
*desc
;
1094 tmp
= *dev
->ep
[i
].reg_udccs
;
1096 "%s max %d %s udccs %02x irqs %lu\n",
1097 ep
->ep
.name
, usb_endpoint_maxp(desc
),
1098 "pio", tmp
, ep
->pio_irqs
);
1099 /* TODO translate all five groups of udccs bits! */
1101 } else /* ep0 should only have one transfer queued */
1102 seq_printf(m
, "ep0 max 16 pio irqs %lu\n",
1105 if (list_empty(&ep
->queue
)) {
1106 seq_printf(m
, "\t(nothing queued)\n");
1109 list_for_each_entry(req
, &ep
->queue
, queue
) {
1111 "\treq %p len %d/%d buf %p\n",
1112 &req
->req
, req
->req
.actual
,
1113 req
->req
.length
, req
->req
.buf
);
1118 local_irq_restore(flags
);
1123 udc_debugfs_open(struct inode
*inode
, struct file
*file
)
1125 return single_open(file
, udc_seq_show
, inode
->i_private
);
1128 static const struct file_operations debug_fops
= {
1129 .open
= udc_debugfs_open
,
1131 .llseek
= seq_lseek
,
1132 .release
= single_release
,
1133 .owner
= THIS_MODULE
,
1136 #define create_debug_files(dev) \
1138 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1139 S_IRUGO, NULL, dev, &debug_fops); \
1141 #define remove_debug_files(dev) \
1143 if (dev->debugfs_udc) \
1144 debugfs_remove(dev->debugfs_udc); \
1147 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1149 #define create_debug_files(dev) do {} while (0)
1150 #define remove_debug_files(dev) do {} while (0)
1152 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1154 /*-------------------------------------------------------------------------*/
1157 * udc_disable - disable USB device controller
1159 static void udc_disable(struct pxa25x_udc
*dev
)
1161 /* block all irqs */
1162 udc_set_mask_UDCCR(UDCCR_SRM
|UDCCR_REM
);
1163 UICR0
= UICR1
= 0xff;
1166 /* if hardware supports it, disconnect from usb */
1169 udc_clear_mask_UDCCR(UDCCR_UDE
);
1172 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1177 * udc_reinit - initialize software state
1179 static void udc_reinit(struct pxa25x_udc
*dev
)
1183 /* device/ep0 records init */
1184 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1185 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1186 dev
->ep0state
= EP0_IDLE
;
1188 /* basic endpoint records init */
1189 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1190 struct pxa25x_ep
*ep
= &dev
->ep
[i
];
1193 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1198 INIT_LIST_HEAD (&ep
->queue
);
1202 /* the rest was statically initialized, and is read-only */
1205 /* until it's enabled, this UDC should be completely invisible
1208 static void udc_enable (struct pxa25x_udc
*dev
)
1210 udc_clear_mask_UDCCR(UDCCR_UDE
);
1212 /* try to clear these bits before we enable the udc */
1213 udc_ack_int_UDCCR(UDCCR_SUSIR
|/*UDCCR_RSTIR|*/UDCCR_RESIR
);
1216 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1217 dev
->stats
.irqs
= 0;
1220 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1222 * - if RESET is already in progress, ack interrupt
1223 * - unmask reset interrupt
1225 udc_set_mask_UDCCR(UDCCR_UDE
);
1226 if (!(UDCCR
& UDCCR_UDA
))
1227 udc_ack_int_UDCCR(UDCCR_RSTIR
);
1229 if (dev
->has_cfr
/* UDC_RES2 is defined */) {
1230 /* pxa255 (a0+) can avoid a set_config race that could
1231 * prevent gadget drivers from configuring correctly
1233 UDCCFR
= UDCCFR_ACM
| UDCCFR_MB1
;
1235 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1236 * which could result in missing packets and interrupts.
1237 * supposedly one bit per endpoint, controlling whether it
1238 * double buffers or not; ACM/AREN bits fit into the holes.
1239 * zero bits (like USIR0_IRx) disable double buffering.
1245 /* enable suspend/resume and reset irqs */
1246 udc_clear_mask_UDCCR(UDCCR_SRM
| UDCCR_REM
);
1248 /* enable ep0 irqs */
1249 UICR0
&= ~UICR0_IM0
;
1251 /* if hardware supports it, pullup D+ and wait for reset */
1256 /* when a driver is successfully registered, it will receive
1257 * control requests including set_configuration(), which enables
1258 * non-control requests. then usb traffic follows until a
1259 * disconnect is reported. then a host may connect again, or
1260 * the driver might get unbound.
1262 static int pxa25x_start(struct usb_gadget_driver
*driver
,
1263 int (*bind
)(struct usb_gadget
*))
1265 struct pxa25x_udc
*dev
= the_controller
;
1269 || driver
->max_speed
< USB_SPEED_FULL
1271 || !driver
->disconnect
1279 /* first hook up the driver ... */
1280 dev
->driver
= driver
;
1281 dev
->gadget
.dev
.driver
= &driver
->driver
;
1284 retval
= device_add (&dev
->gadget
.dev
);
1288 dev
->gadget
.dev
.driver
= NULL
;
1291 retval
= bind(&dev
->gadget
);
1293 DMSG("bind to driver %s --> error %d\n",
1294 driver
->driver
.name
, retval
);
1295 device_del (&dev
->gadget
.dev
);
1299 /* ... then enable host detection and ep0; and we're ready
1300 * for set_configuration as well as eventual disconnect.
1302 DMSG("registered gadget driver '%s'\n", driver
->driver
.name
);
1304 /* connect to bus through transceiver */
1305 if (dev
->transceiver
) {
1306 retval
= otg_set_peripheral(dev
->transceiver
, &dev
->gadget
);
1308 DMSG("can't bind to transceiver\n");
1310 driver
->unbind(&dev
->gadget
);
1323 stop_activity(struct pxa25x_udc
*dev
, struct usb_gadget_driver
*driver
)
1327 /* don't disconnect drivers more than once */
1328 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1330 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1332 /* prevent new request submissions, kill any outstanding requests */
1333 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1334 struct pxa25x_ep
*ep
= &dev
->ep
[i
];
1337 nuke(ep
, -ESHUTDOWN
);
1339 del_timer_sync(&dev
->timer
);
1341 /* report disconnect; the driver is already quiesced */
1343 driver
->disconnect(&dev
->gadget
);
1345 /* re-init driver-visible data structures */
1349 static int pxa25x_stop(struct usb_gadget_driver
*driver
)
1351 struct pxa25x_udc
*dev
= the_controller
;
1355 if (!driver
|| driver
!= dev
->driver
|| !driver
->unbind
)
1358 local_irq_disable();
1361 stop_activity(dev
, driver
);
1364 if (dev
->transceiver
)
1365 (void) otg_set_peripheral(dev
->transceiver
, NULL
);
1367 driver
->unbind(&dev
->gadget
);
1368 dev
->gadget
.dev
.driver
= NULL
;
1371 device_del (&dev
->gadget
.dev
);
1373 DMSG("unregistered gadget driver '%s'\n", driver
->driver
.name
);
1378 /*-------------------------------------------------------------------------*/
1380 #ifdef CONFIG_ARCH_LUBBOCK
1382 /* Lubbock has separate connect and disconnect irqs. More typical designs
1383 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1387 lubbock_vbus_irq(int irq
, void *_dev
)
1389 struct pxa25x_udc
*dev
= _dev
;
1394 case LUBBOCK_USB_IRQ
:
1396 disable_irq(LUBBOCK_USB_IRQ
);
1397 enable_irq(LUBBOCK_USB_DISC_IRQ
);
1399 case LUBBOCK_USB_DISC_IRQ
:
1401 disable_irq(LUBBOCK_USB_DISC_IRQ
);
1402 enable_irq(LUBBOCK_USB_IRQ
);
1408 pxa25x_udc_vbus_session(&dev
->gadget
, vbus
);
1415 /*-------------------------------------------------------------------------*/
1417 static inline void clear_ep_state (struct pxa25x_udc
*dev
)
1421 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1422 * fifos, and pending transactions mustn't be continued in any case.
1424 for (i
= 1; i
< PXA_UDC_NUM_ENDPOINTS
; i
++)
1425 nuke(&dev
->ep
[i
], -ECONNABORTED
);
1428 static void udc_watchdog(unsigned long _dev
)
1430 struct pxa25x_udc
*dev
= (void *)_dev
;
1432 local_irq_disable();
1433 if (dev
->ep0state
== EP0_STALL
1434 && (UDCCS0
& UDCCS0_FST
) == 0
1435 && (UDCCS0
& UDCCS0_SST
) == 0) {
1436 UDCCS0
= UDCCS0_FST
|UDCCS0_FTF
;
1437 DBG(DBG_VERBOSE
, "ep0 re-stall\n");
1438 start_watchdog(dev
);
1443 static void handle_ep0 (struct pxa25x_udc
*dev
)
1445 u32 udccs0
= UDCCS0
;
1446 struct pxa25x_ep
*ep
= &dev
->ep
[0];
1447 struct pxa25x_request
*req
;
1449 struct usb_ctrlrequest r
;
1454 if (list_empty(&ep
->queue
))
1457 req
= list_entry(ep
->queue
.next
, struct pxa25x_request
, queue
);
1459 /* clear stall status */
1460 if (udccs0
& UDCCS0_SST
) {
1462 UDCCS0
= UDCCS0_SST
;
1463 del_timer(&dev
->timer
);
1467 /* previous request unfinished? non-error iff back-to-back ... */
1468 if ((udccs0
& UDCCS0_SA
) != 0 && dev
->ep0state
!= EP0_IDLE
) {
1470 del_timer(&dev
->timer
);
1474 switch (dev
->ep0state
) {
1476 /* late-breaking status? */
1479 /* start control request? */
1480 if (likely((udccs0
& (UDCCS0_OPR
|UDCCS0_SA
|UDCCS0_RNE
))
1481 == (UDCCS0_OPR
|UDCCS0_SA
|UDCCS0_RNE
))) {
1486 /* read SETUP packet */
1487 for (i
= 0; i
< 8; i
++) {
1488 if (unlikely(!(UDCCS0
& UDCCS0_RNE
))) {
1490 DMSG("SETUP %d!\n", i
);
1493 u
.raw
[i
] = (u8
) UDDR0
;
1495 if (unlikely((UDCCS0
& UDCCS0_RNE
) != 0))
1499 DBG(DBG_VERBOSE
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1500 u
.r
.bRequestType
, u
.r
.bRequest
,
1501 le16_to_cpu(u
.r
.wValue
),
1502 le16_to_cpu(u
.r
.wIndex
),
1503 le16_to_cpu(u
.r
.wLength
));
1505 /* cope with automagic for some standard requests. */
1506 dev
->req_std
= (u
.r
.bRequestType
& USB_TYPE_MASK
)
1507 == USB_TYPE_STANDARD
;
1508 dev
->req_config
= 0;
1509 dev
->req_pending
= 1;
1510 switch (u
.r
.bRequest
) {
1511 /* hardware restricts gadget drivers here! */
1512 case USB_REQ_SET_CONFIGURATION
:
1513 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1514 /* reflect hardware's automagic
1515 * up to the gadget driver.
1518 dev
->req_config
= 1;
1519 clear_ep_state(dev
);
1520 /* if !has_cfr, there's no synch
1521 * else use AREN (later) not SA|OPR
1522 * USIR0_IR0 acts edge sensitive
1526 /* ... and here, even more ... */
1527 case USB_REQ_SET_INTERFACE
:
1528 if (u
.r
.bRequestType
== USB_RECIP_INTERFACE
) {
1529 /* udc hardware is broken by design:
1530 * - altsetting may only be zero;
1531 * - hw resets all interfaces' eps;
1532 * - ep reset doesn't include halt(?).
1534 DMSG("broken set_interface (%d/%d)\n",
1535 le16_to_cpu(u
.r
.wIndex
),
1536 le16_to_cpu(u
.r
.wValue
));
1540 /* hardware was supposed to hide this */
1541 case USB_REQ_SET_ADDRESS
:
1542 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1543 ep0start(dev
, 0, "address");
1549 if (u
.r
.bRequestType
& USB_DIR_IN
)
1550 dev
->ep0state
= EP0_IN_DATA_PHASE
;
1552 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
1554 i
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
1556 /* hardware automagic preventing STALL... */
1557 if (dev
->req_config
) {
1558 /* hardware sometimes neglects to tell
1559 * tell us about config change events,
1560 * so later ones may fail...
1562 WARNING("config change %02x fail %d?\n",
1565 /* TODO experiment: if has_cfr,
1566 * hardware didn't ACK; maybe we
1567 * could actually STALL!
1570 DBG(DBG_VERBOSE
, "protocol STALL, "
1571 "%02x err %d\n", UDCCS0
, i
);
1573 /* the watchdog timer helps deal with cases
1574 * where udc seems to clear FST wrongly, and
1575 * then NAKs instead of STALLing.
1577 ep0start(dev
, UDCCS0_FST
|UDCCS0_FTF
, "stall");
1578 start_watchdog(dev
);
1579 dev
->ep0state
= EP0_STALL
;
1581 /* deferred i/o == no response yet */
1582 } else if (dev
->req_pending
) {
1583 if (likely(dev
->ep0state
== EP0_IN_DATA_PHASE
1584 || dev
->req_std
|| u
.r
.wLength
))
1585 ep0start(dev
, 0, "defer");
1587 ep0start(dev
, UDCCS0_IPR
, "defer/IPR");
1590 /* expect at least one data or status stage irq */
1593 } else if (likely((udccs0
& (UDCCS0_OPR
|UDCCS0_SA
))
1594 == (UDCCS0_OPR
|UDCCS0_SA
))) {
1597 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1598 * still observed on a pxa255 a0.
1600 DBG(DBG_VERBOSE
, "e131\n");
1603 /* read SETUP data, but don't trust it too much */
1604 for (i
= 0; i
< 8; i
++)
1605 u
.raw
[i
] = (u8
) UDDR0
;
1606 if ((u
.r
.bRequestType
& USB_RECIP_MASK
)
1609 if (u
.word
[0] == 0 && u
.word
[1] == 0)
1613 /* some random early IRQ:
1616 * - OPR got set, without SA (likely status stage)
1618 UDCCS0
= udccs0
& (UDCCS0_SA
|UDCCS0_OPR
);
1621 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
1622 if (udccs0
& UDCCS0_OPR
) {
1623 UDCCS0
= UDCCS0_OPR
|UDCCS0_FTF
;
1624 DBG(DBG_VERBOSE
, "ep0in premature status\n");
1628 } else /* irq was IPR clearing */ {
1630 /* this IN packet might finish the request */
1631 (void) write_ep0_fifo(ep
, req
);
1632 } /* else IN token before response was written */
1635 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
1636 if (udccs0
& UDCCS0_OPR
) {
1638 /* this OUT packet might finish the request */
1639 if (read_ep0_fifo(ep
, req
))
1641 /* else more OUT packets expected */
1642 } /* else OUT token before read was issued */
1643 } else /* irq was IPR clearing */ {
1644 DBG(DBG_VERBOSE
, "ep0out premature status\n");
1653 /* ack control-IN status (maybe in-zlp was skipped)
1654 * also appears after some config change events.
1656 if (udccs0
& UDCCS0_OPR
)
1657 UDCCS0
= UDCCS0_OPR
;
1661 UDCCS0
= UDCCS0_FST
;
1667 static void handle_ep(struct pxa25x_ep
*ep
)
1669 struct pxa25x_request
*req
;
1670 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
1676 if (likely (!list_empty(&ep
->queue
)))
1677 req
= list_entry(ep
->queue
.next
,
1678 struct pxa25x_request
, queue
);
1682 // TODO check FST handling
1684 udccs
= *ep
->reg_udccs
;
1685 if (unlikely(is_in
)) { /* irq from TPC, SST, or (ISO) TUR */
1687 if (likely(ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
))
1688 tmp
|= UDCCS_BI_SST
;
1691 *ep
->reg_udccs
= tmp
;
1692 if (req
&& likely ((udccs
& UDCCS_BI_TFS
) != 0))
1693 completed
= write_fifo(ep
, req
);
1695 } else { /* irq from RPC (or for ISO, ROF) */
1696 if (likely(ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
))
1697 tmp
= UDCCS_BO_SST
| UDCCS_BO_DME
;
1699 tmp
= UDCCS_IO_ROF
| UDCCS_IO_DME
;
1702 *ep
->reg_udccs
= tmp
;
1704 /* fifos can hold packets, ready for reading... */
1706 completed
= read_fifo(ep
, req
);
1708 pio_irq_disable (ep
->bEndpointAddress
);
1711 } while (completed
);
1715 * pxa25x_udc_irq - interrupt handler
1717 * avoid delays in ep0 processing. the control handshaking isn't always
1718 * under software control (pxa250c0 and the pxa255 are better), and delays
1719 * could cause usb protocol errors.
1722 pxa25x_udc_irq(int irq
, void *_dev
)
1724 struct pxa25x_udc
*dev
= _dev
;
1733 /* SUSpend Interrupt Request */
1734 if (unlikely(udccr
& UDCCR_SUSIR
)) {
1735 udc_ack_int_UDCCR(UDCCR_SUSIR
);
1737 DBG(DBG_VERBOSE
, "USB suspend\n");
1739 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1741 && dev
->driver
->suspend
)
1742 dev
->driver
->suspend(&dev
->gadget
);
1746 /* RESume Interrupt Request */
1747 if (unlikely(udccr
& UDCCR_RESIR
)) {
1748 udc_ack_int_UDCCR(UDCCR_RESIR
);
1750 DBG(DBG_VERBOSE
, "USB resume\n");
1752 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1754 && dev
->driver
->resume
)
1755 dev
->driver
->resume(&dev
->gadget
);
1758 /* ReSeT Interrupt Request - USB reset */
1759 if (unlikely(udccr
& UDCCR_RSTIR
)) {
1760 udc_ack_int_UDCCR(UDCCR_RSTIR
);
1763 if ((UDCCR
& UDCCR_UDA
) == 0) {
1764 DBG(DBG_VERBOSE
, "USB reset start\n");
1766 /* reset driver and endpoints,
1767 * in case that's not yet done
1769 stop_activity (dev
, dev
->driver
);
1772 DBG(DBG_VERBOSE
, "USB reset end\n");
1773 dev
->gadget
.speed
= USB_SPEED_FULL
;
1774 memset(&dev
->stats
, 0, sizeof dev
->stats
);
1775 /* driver and endpoints are still reset */
1779 u32 usir0
= USIR0
& ~UICR0
;
1780 u32 usir1
= USIR1
& ~UICR1
;
1783 if (unlikely (!usir0
&& !usir1
))
1786 DBG(DBG_VERY_NOISY
, "irq %02x.%02x\n", usir1
, usir0
);
1788 /* control traffic */
1789 if (usir0
& USIR0_IR0
) {
1790 dev
->ep
[0].pio_irqs
++;
1795 /* endpoint data transfers */
1796 for (i
= 0; i
< 8; i
++) {
1799 if (i
&& (usir0
& tmp
)) {
1800 handle_ep(&dev
->ep
[i
]);
1804 #ifndef CONFIG_USB_PXA25X_SMALL
1806 handle_ep(&dev
->ep
[i
+8]);
1814 /* we could also ask for 1 msec SOF (SIR) interrupts */
1820 /*-------------------------------------------------------------------------*/
1822 static void nop_release (struct device
*dev
)
1824 DMSG("%s %s\n", __func__
, dev_name(dev
));
1827 /* this uses load-time allocation and initialization (instead of
1828 * doing it at run-time) to save code, eliminate fault paths, and
1829 * be more obviously correct.
1831 static struct pxa25x_udc memory
= {
1833 .ops
= &pxa25x_udc_ops
,
1834 .ep0
= &memory
.ep
[0].ep
,
1835 .name
= driver_name
,
1837 .init_name
= "gadget",
1838 .release
= nop_release
,
1842 /* control endpoint */
1846 .ops
= &pxa25x_ep_ops
,
1847 .maxpacket
= EP0_FIFO_SIZE
,
1850 .reg_udccs
= &UDCCS0
,
1854 /* first group of endpoints */
1857 .name
= "ep1in-bulk",
1858 .ops
= &pxa25x_ep_ops
,
1859 .maxpacket
= BULK_FIFO_SIZE
,
1862 .fifo_size
= BULK_FIFO_SIZE
,
1863 .bEndpointAddress
= USB_DIR_IN
| 1,
1864 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1865 .reg_udccs
= &UDCCS1
,
1870 .name
= "ep2out-bulk",
1871 .ops
= &pxa25x_ep_ops
,
1872 .maxpacket
= BULK_FIFO_SIZE
,
1875 .fifo_size
= BULK_FIFO_SIZE
,
1876 .bEndpointAddress
= 2,
1877 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1878 .reg_udccs
= &UDCCS2
,
1882 #ifndef CONFIG_USB_PXA25X_SMALL
1885 .name
= "ep3in-iso",
1886 .ops
= &pxa25x_ep_ops
,
1887 .maxpacket
= ISO_FIFO_SIZE
,
1890 .fifo_size
= ISO_FIFO_SIZE
,
1891 .bEndpointAddress
= USB_DIR_IN
| 3,
1892 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1893 .reg_udccs
= &UDCCS3
,
1898 .name
= "ep4out-iso",
1899 .ops
= &pxa25x_ep_ops
,
1900 .maxpacket
= ISO_FIFO_SIZE
,
1903 .fifo_size
= ISO_FIFO_SIZE
,
1904 .bEndpointAddress
= 4,
1905 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1906 .reg_udccs
= &UDCCS4
,
1912 .name
= "ep5in-int",
1913 .ops
= &pxa25x_ep_ops
,
1914 .maxpacket
= INT_FIFO_SIZE
,
1917 .fifo_size
= INT_FIFO_SIZE
,
1918 .bEndpointAddress
= USB_DIR_IN
| 5,
1919 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
1920 .reg_udccs
= &UDCCS5
,
1924 /* second group of endpoints */
1927 .name
= "ep6in-bulk",
1928 .ops
= &pxa25x_ep_ops
,
1929 .maxpacket
= BULK_FIFO_SIZE
,
1932 .fifo_size
= BULK_FIFO_SIZE
,
1933 .bEndpointAddress
= USB_DIR_IN
| 6,
1934 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1935 .reg_udccs
= &UDCCS6
,
1940 .name
= "ep7out-bulk",
1941 .ops
= &pxa25x_ep_ops
,
1942 .maxpacket
= BULK_FIFO_SIZE
,
1945 .fifo_size
= BULK_FIFO_SIZE
,
1946 .bEndpointAddress
= 7,
1947 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1948 .reg_udccs
= &UDCCS7
,
1954 .name
= "ep8in-iso",
1955 .ops
= &pxa25x_ep_ops
,
1956 .maxpacket
= ISO_FIFO_SIZE
,
1959 .fifo_size
= ISO_FIFO_SIZE
,
1960 .bEndpointAddress
= USB_DIR_IN
| 8,
1961 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1962 .reg_udccs
= &UDCCS8
,
1967 .name
= "ep9out-iso",
1968 .ops
= &pxa25x_ep_ops
,
1969 .maxpacket
= ISO_FIFO_SIZE
,
1972 .fifo_size
= ISO_FIFO_SIZE
,
1973 .bEndpointAddress
= 9,
1974 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1975 .reg_udccs
= &UDCCS9
,
1981 .name
= "ep10in-int",
1982 .ops
= &pxa25x_ep_ops
,
1983 .maxpacket
= INT_FIFO_SIZE
,
1986 .fifo_size
= INT_FIFO_SIZE
,
1987 .bEndpointAddress
= USB_DIR_IN
| 10,
1988 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
1989 .reg_udccs
= &UDCCS10
,
1990 .reg_uddr
= &UDDR10
,
1993 /* third group of endpoints */
1996 .name
= "ep11in-bulk",
1997 .ops
= &pxa25x_ep_ops
,
1998 .maxpacket
= BULK_FIFO_SIZE
,
2001 .fifo_size
= BULK_FIFO_SIZE
,
2002 .bEndpointAddress
= USB_DIR_IN
| 11,
2003 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2004 .reg_udccs
= &UDCCS11
,
2005 .reg_uddr
= &UDDR11
,
2009 .name
= "ep12out-bulk",
2010 .ops
= &pxa25x_ep_ops
,
2011 .maxpacket
= BULK_FIFO_SIZE
,
2014 .fifo_size
= BULK_FIFO_SIZE
,
2015 .bEndpointAddress
= 12,
2016 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2017 .reg_udccs
= &UDCCS12
,
2018 .reg_ubcr
= &UBCR12
,
2019 .reg_uddr
= &UDDR12
,
2023 .name
= "ep13in-iso",
2024 .ops
= &pxa25x_ep_ops
,
2025 .maxpacket
= ISO_FIFO_SIZE
,
2028 .fifo_size
= ISO_FIFO_SIZE
,
2029 .bEndpointAddress
= USB_DIR_IN
| 13,
2030 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2031 .reg_udccs
= &UDCCS13
,
2032 .reg_uddr
= &UDDR13
,
2036 .name
= "ep14out-iso",
2037 .ops
= &pxa25x_ep_ops
,
2038 .maxpacket
= ISO_FIFO_SIZE
,
2041 .fifo_size
= ISO_FIFO_SIZE
,
2042 .bEndpointAddress
= 14,
2043 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2044 .reg_udccs
= &UDCCS14
,
2045 .reg_ubcr
= &UBCR14
,
2046 .reg_uddr
= &UDDR14
,
2050 .name
= "ep15in-int",
2051 .ops
= &pxa25x_ep_ops
,
2052 .maxpacket
= INT_FIFO_SIZE
,
2055 .fifo_size
= INT_FIFO_SIZE
,
2056 .bEndpointAddress
= USB_DIR_IN
| 15,
2057 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2058 .reg_udccs
= &UDCCS15
,
2059 .reg_uddr
= &UDDR15
,
2061 #endif /* !CONFIG_USB_PXA25X_SMALL */
2064 #define CP15R0_VENDOR_MASK 0xffffe000
2066 #if defined(CONFIG_ARCH_PXA)
2067 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2069 #elif defined(CONFIG_ARCH_IXP4XX)
2070 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2074 #define CP15R0_PROD_MASK 0x000003f0
2075 #define PXA25x 0x00000100 /* and PXA26x */
2076 #define PXA210 0x00000120
2078 #define CP15R0_REV_MASK 0x0000000f
2080 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2082 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2083 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2084 #define PXA250_B2 0x00000104
2085 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2086 #define PXA250_B0 0x00000102
2087 #define PXA250_A1 0x00000101
2088 #define PXA250_A0 0x00000100
2090 #define PXA210_C0 0x00000125
2091 #define PXA210_B2 0x00000124
2092 #define PXA210_B1 0x00000123
2093 #define PXA210_B0 0x00000122
2094 #define IXP425_A0 0x000001c1
2095 #define IXP425_B0 0x000001f1
2096 #define IXP465_AD 0x00000200
2099 * probe - binds to the platform device
2101 static int __init
pxa25x_udc_probe(struct platform_device
*pdev
)
2103 struct pxa25x_udc
*dev
= &memory
;
2107 /* insist on Intel/ARM/XScale */
2108 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev
));
2109 if ((chiprev
& CP15R0_VENDOR_MASK
) != CP15R0_XSCALE_VALUE
) {
2110 pr_err("%s: not XScale!\n", driver_name
);
2114 /* trigger chiprev-specific logic */
2115 switch (chiprev
& CP15R0_PRODREV_MASK
) {
2116 #if defined(CONFIG_ARCH_PXA)
2122 /* A0/A1 "not released"; ep 13, 15 unusable */
2124 case PXA250_B2
: case PXA210_B2
:
2125 case PXA250_B1
: case PXA210_B1
:
2126 case PXA250_B0
: case PXA210_B0
:
2127 /* OUT-DMA is broken ... */
2129 case PXA250_C0
: case PXA210_C0
:
2131 #elif defined(CONFIG_ARCH_IXP4XX)
2139 pr_err("%s: unrecognized processor: %08x\n",
2140 driver_name
, chiprev
);
2141 /* iop3xx, ixp4xx, ... */
2145 irq
= platform_get_irq(pdev
, 0);
2149 dev
->clk
= clk_get(&pdev
->dev
, NULL
);
2150 if (IS_ERR(dev
->clk
)) {
2151 retval
= PTR_ERR(dev
->clk
);
2155 pr_debug("%s: IRQ %d%s%s\n", driver_name
, irq
,
2156 dev
->has_cfr
? "" : " (!cfr)",
2160 /* other non-static parts of init */
2161 dev
->dev
= &pdev
->dev
;
2162 dev
->mach
= pdev
->dev
.platform_data
;
2164 dev
->transceiver
= otg_get_transceiver();
2166 if (gpio_is_valid(dev
->mach
->gpio_pullup
)) {
2167 if ((retval
= gpio_request(dev
->mach
->gpio_pullup
,
2168 "pca25x_udc GPIO PULLUP"))) {
2170 "can't get pullup gpio %d, err: %d\n",
2171 dev
->mach
->gpio_pullup
, retval
);
2172 goto err_gpio_pullup
;
2174 gpio_direction_output(dev
->mach
->gpio_pullup
, 0);
2177 init_timer(&dev
->timer
);
2178 dev
->timer
.function
= udc_watchdog
;
2179 dev
->timer
.data
= (unsigned long) dev
;
2181 device_initialize(&dev
->gadget
.dev
);
2182 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2183 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2185 the_controller
= dev
;
2186 platform_set_drvdata(pdev
, dev
);
2193 /* irq setup after old hardware state is cleaned up */
2194 retval
= request_irq(irq
, pxa25x_udc_irq
,
2195 0, driver_name
, dev
);
2197 pr_err("%s: can't get irq %d, err %d\n",
2198 driver_name
, irq
, retval
);
2203 #ifdef CONFIG_ARCH_LUBBOCK
2204 if (machine_is_lubbock()) {
2205 retval
= request_irq(LUBBOCK_USB_DISC_IRQ
,
2210 pr_err("%s: can't get irq %i, err %d\n",
2211 driver_name
, LUBBOCK_USB_DISC_IRQ
, retval
);
2214 retval
= request_irq(LUBBOCK_USB_IRQ
,
2219 pr_err("%s: can't get irq %i, err %d\n",
2220 driver_name
, LUBBOCK_USB_IRQ
, retval
);
2225 create_debug_files(dev
);
2227 retval
= usb_add_gadget_udc(&pdev
->dev
, &dev
->gadget
);
2231 remove_debug_files(dev
);
2232 #ifdef CONFIG_ARCH_LUBBOCK
2234 free_irq(LUBBOCK_USB_DISC_IRQ
, dev
);
2239 if (gpio_is_valid(dev
->mach
->gpio_pullup
))
2240 gpio_free(dev
->mach
->gpio_pullup
);
2242 if (dev
->transceiver
) {
2243 otg_put_transceiver(dev
->transceiver
);
2244 dev
->transceiver
= NULL
;
2251 static void pxa25x_udc_shutdown(struct platform_device
*_dev
)
2256 static int __exit
pxa25x_udc_remove(struct platform_device
*pdev
)
2258 struct pxa25x_udc
*dev
= platform_get_drvdata(pdev
);
2260 usb_del_gadget_udc(&dev
->gadget
);
2267 remove_debug_files(dev
);
2270 free_irq(platform_get_irq(pdev
, 0), dev
);
2273 #ifdef CONFIG_ARCH_LUBBOCK
2274 if (machine_is_lubbock()) {
2275 free_irq(LUBBOCK_USB_DISC_IRQ
, dev
);
2276 free_irq(LUBBOCK_USB_IRQ
, dev
);
2279 if (gpio_is_valid(dev
->mach
->gpio_pullup
))
2280 gpio_free(dev
->mach
->gpio_pullup
);
2284 if (dev
->transceiver
) {
2285 otg_put_transceiver(dev
->transceiver
);
2286 dev
->transceiver
= NULL
;
2289 platform_set_drvdata(pdev
, NULL
);
2290 the_controller
= NULL
;
2294 /*-------------------------------------------------------------------------*/
2298 /* USB suspend (controlled by the host) and system suspend (controlled
2299 * by the PXA) don't necessarily work well together. If USB is active,
2300 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2301 * mode, or any deeper PM saving state.
2303 * For now, we punt and forcibly disconnect from the USB host when PXA
2304 * enters any suspend state. While we're disconnected, we always disable
2305 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2306 * Boards without software pullup control shouldn't use those states.
2307 * VBUS IRQs should probably be ignored so that the PXA device just acts
2308 * "dead" to USB hosts until system resume.
2310 static int pxa25x_udc_suspend(struct platform_device
*dev
, pm_message_t state
)
2312 struct pxa25x_udc
*udc
= platform_get_drvdata(dev
);
2313 unsigned long flags
;
2315 if (!gpio_is_valid(udc
->mach
->gpio_pullup
) && !udc
->mach
->udc_command
)
2316 WARNING("USB host won't detect disconnect!\n");
2319 local_irq_save(flags
);
2321 local_irq_restore(flags
);
2326 static int pxa25x_udc_resume(struct platform_device
*dev
)
2328 struct pxa25x_udc
*udc
= platform_get_drvdata(dev
);
2329 unsigned long flags
;
2332 local_irq_save(flags
);
2334 local_irq_restore(flags
);
2340 #define pxa25x_udc_suspend NULL
2341 #define pxa25x_udc_resume NULL
2344 /*-------------------------------------------------------------------------*/
2346 static struct platform_driver udc_driver
= {
2347 .shutdown
= pxa25x_udc_shutdown
,
2348 .remove
= __exit_p(pxa25x_udc_remove
),
2349 .suspend
= pxa25x_udc_suspend
,
2350 .resume
= pxa25x_udc_resume
,
2352 .owner
= THIS_MODULE
,
2353 .name
= "pxa25x-udc",
2357 static int __init
udc_init(void)
2359 pr_info("%s: version %s\n", driver_name
, DRIVER_VERSION
);
2360 return platform_driver_probe(&udc_driver
, pxa25x_udc_probe
);
2362 module_init(udc_init
);
2364 static void __exit
udc_exit(void)
2366 platform_driver_unregister(&udc_driver
);
2368 module_exit(udc_exit
);
2370 MODULE_DESCRIPTION(DRIVER_DESC
);
2371 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2372 MODULE_LICENSE("GPL");
2373 MODULE_ALIAS("platform:pxa25x-udc");