2 * linux/drivers/usb/gadget/pxa2xx_udc.c
3 * Intel PXA2xx and IXP4xx on-chip full speed USB device controllers
5 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
6 * Copyright (C) 2003 Robert Schwebel, Pengutronix
7 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
8 * Copyright (C) 2003 David Brownell
9 * Copyright (C) 2003 Joshua Wise
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 // #define VERBOSE DBG_VERBOSE
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/ioport.h>
34 #include <linux/types.h>
35 #include <linux/version.h>
36 #include <linux/errno.h>
37 #include <linux/delay.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/timer.h>
42 #include <linux/list.h>
43 #include <linux/interrupt.h>
44 #include <linux/proc_fs.h>
46 #include <linux/device.h>
47 #include <linux/dma-mapping.h>
49 #include <asm/byteorder.h>
53 #include <asm/system.h>
54 #include <asm/mach-types.h>
55 #include <asm/unaligned.h>
56 #include <asm/hardware.h>
57 #include <asm/arch/pxa-regs.h>
59 #include <linux/usb_ch9.h>
60 #include <linux/usb_gadget.h>
62 #include <asm/arch/udc.h>
66 * This driver handles the USB Device Controller (UDC) in Intel's PXA 2xx
67 * series processors. The UDC for the IXP 4xx series is very similar.
68 * There are fifteen endpoints, in addition to ep0.
70 * Such controller drivers work with a gadget driver. The gadget driver
71 * returns descriptors, implements configuration and data protocols used
72 * by the host to interact with this device, and allocates endpoints to
73 * the different protocol interfaces. The controller driver virtualizes
74 * usb hardware so that the gadget drivers will be more portable.
76 * This UDC hardware wants to implement a bit too much USB protocol, so
77 * it constrains the sorts of USB configuration change events that work.
78 * The errata for these chips are misleading; some "fixed" bugs from
79 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
82 #define DRIVER_VERSION "14-Dec-2003"
83 #define DRIVER_DESC "PXA 2xx USB Device Controller driver"
86 static const char driver_name
[] = "pxa2xx_udc";
88 static const char ep0name
[] = "ep0";
92 // #define USE_OUT_DMA
93 // #define DISABLE_TEST_MODE
95 #ifdef CONFIG_ARCH_IXP4XX
98 /* cpu-specific register addresses are compiled in to this code */
99 #ifdef CONFIG_ARCH_PXA
100 #error "Can't configure both IXP and PXA"
105 #include "pxa2xx_udc.h"
109 static int use_dma
= 1;
110 module_param(use_dma
, bool, 0);
111 MODULE_PARM_DESC (use_dma
, "true to use dma");
113 static void dma_nodesc_handler (int dmach
, void *_ep
, struct pt_regs
*r
);
114 static void kick_dma(struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
);
117 #define DMASTR " (dma support)"
119 #define DMASTR " (dma in)"
123 #define DMASTR " (pio only)"
127 #ifdef CONFIG_USB_PXA2XX_SMALL
128 #define SIZE_STR " (small)"
133 #ifdef DISABLE_TEST_MODE
134 /* (mode == 0) == no undocumented chip tweaks
135 * (mode & 1) == double buffer bulk IN
136 * (mode & 2) == double buffer bulk OUT
137 * ... so mode = 3 (or 7, 15, etc) does it for both
139 static ushort fifo_mode
= 0;
140 module_param(fifo_mode
, ushort
, 0);
141 MODULE_PARM_DESC (fifo_mode
, "pxa2xx udc fifo mode");
144 /* ---------------------------------------------------------------------------
145 * endpoint related parts of the api to the usb controller hardware,
146 * used by gadget driver; and the inner talker-to-hardware core.
147 * ---------------------------------------------------------------------------
150 static void pxa2xx_ep_fifo_flush (struct usb_ep
*ep
);
151 static void nuke (struct pxa2xx_ep
*, int status
);
153 static void pio_irq_enable(int bEndpointAddress
)
155 bEndpointAddress
&= 0xf;
156 if (bEndpointAddress
< 8)
157 UICR0
&= ~(1 << bEndpointAddress
);
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 pxa2xx
202 * endpoint configurations are fixed, and are pretty much always enabled,
203 * there's not a lot to manage here.
205 * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
206 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
207 * for a single interface (with only the default altsetting) and for gadget
208 * drivers that don't halt endpoints (not reset by set_interface). that also
209 * means that if you use ISO, you must violate the USB spec rule that all
210 * iso endpoints must be in non-default altsettings.
212 static int pxa2xx_ep_enable (struct usb_ep
*_ep
,
213 const struct usb_endpoint_descriptor
*desc
)
215 struct pxa2xx_ep
*ep
;
216 struct pxa2xx_udc
*dev
;
218 ep
= container_of (_ep
, struct pxa2xx_ep
, ep
);
219 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
220 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
221 || ep
->bEndpointAddress
!= desc
->bEndpointAddress
222 || ep
->fifo_size
< le16_to_cpu
223 (desc
->wMaxPacketSize
)) {
224 DMSG("%s, bad ep or descriptor\n", __FUNCTION__
);
228 /* xfer types must match, except that interrupt ~= bulk */
229 if (ep
->bmAttributes
!= desc
->bmAttributes
230 && ep
->bmAttributes
!= USB_ENDPOINT_XFER_BULK
231 && desc
->bmAttributes
!= USB_ENDPOINT_XFER_INT
) {
232 DMSG("%s, %s type mismatch\n", __FUNCTION__
, _ep
->name
);
236 /* hardware _could_ do smaller, but driver doesn't */
237 if ((desc
->bmAttributes
== USB_ENDPOINT_XFER_BULK
238 && le16_to_cpu (desc
->wMaxPacketSize
)
240 || !desc
->wMaxPacketSize
) {
241 DMSG("%s, bad %s maxpacket\n", __FUNCTION__
, _ep
->name
);
246 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
247 DMSG("%s, bogus device state\n", __FUNCTION__
);
254 ep
->pio_irqs
= ep
->dma_irqs
= 0;
255 ep
->ep
.maxpacket
= le16_to_cpu (desc
->wMaxPacketSize
);
257 /* flush fifo (mostly for OUT buffers) */
258 pxa2xx_ep_fifo_flush (_ep
);
260 /* ... reset halt state too, if we could ... */
263 /* for (some) bulk and ISO endpoints, try to get a DMA channel and
264 * bind it to the endpoint. otherwise use PIO.
266 switch (ep
->bmAttributes
) {
267 case USB_ENDPOINT_XFER_ISOC
:
268 if (le16_to_cpu(desc
->wMaxPacketSize
) % 32)
271 case USB_ENDPOINT_XFER_BULK
:
272 if (!use_dma
|| !ep
->reg_drcmr
)
274 ep
->dma
= pxa_request_dma ((char *)_ep
->name
,
275 (le16_to_cpu (desc
->wMaxPacketSize
) > 64)
276 ? DMA_PRIO_MEDIUM
/* some iso */
278 dma_nodesc_handler
, ep
);
280 *ep
->reg_drcmr
= DRCMR_MAPVLD
| ep
->dma
;
281 DMSG("%s using dma%d\n", _ep
->name
, ep
->dma
);
286 DBG(DBG_VERBOSE
, "enabled %s\n", _ep
->name
);
290 static int pxa2xx_ep_disable (struct usb_ep
*_ep
)
292 struct pxa2xx_ep
*ep
;
294 ep
= container_of (_ep
, struct pxa2xx_ep
, ep
);
295 if (!_ep
|| !ep
->desc
) {
296 DMSG("%s, %s not enabled\n", __FUNCTION__
,
297 _ep
? ep
->ep
.name
: NULL
);
300 nuke (ep
, -ESHUTDOWN
);
305 pxa_free_dma (ep
->dma
);
310 /* flush fifo (mostly for IN buffers) */
311 pxa2xx_ep_fifo_flush (_ep
);
316 DBG(DBG_VERBOSE
, "%s disabled\n", _ep
->name
);
320 /*-------------------------------------------------------------------------*/
322 /* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
323 * must still pass correctly initialized endpoints, since other controller
324 * drivers may care about how it's currently set up (dma issues etc).
328 * pxa2xx_ep_alloc_request - allocate a request data structure
330 static struct usb_request
*
331 pxa2xx_ep_alloc_request (struct usb_ep
*_ep
, int gfp_flags
)
333 struct pxa2xx_request
*req
;
335 req
= kmalloc (sizeof *req
, gfp_flags
);
339 memset (req
, 0, sizeof *req
);
340 INIT_LIST_HEAD (&req
->queue
);
346 * pxa2xx_ep_free_request - deallocate a request data structure
349 pxa2xx_ep_free_request (struct usb_ep
*_ep
, struct usb_request
*_req
)
351 struct pxa2xx_request
*req
;
353 req
= container_of (_req
, struct pxa2xx_request
, req
);
354 WARN_ON (!list_empty (&req
->queue
));
359 /* PXA cache needs flushing with DMA I/O (it's dma-incoherent), but there's
360 * no device-affinity and the heap works perfectly well for i/o buffers.
361 * It wastes much less memory than dma_alloc_coherent() would, and even
362 * prevents cacheline (32 bytes wide) sharing problems.
365 pxa2xx_ep_alloc_buffer(struct usb_ep
*_ep
, unsigned bytes
,
366 dma_addr_t
*dma
, int gfp_flags
)
370 retval
= kmalloc (bytes
, gfp_flags
& ~(__GFP_DMA
|__GFP_HIGHMEM
));
373 *dma
= virt_to_bus (retval
);
375 *dma
= (dma_addr_t
)~0;
381 pxa2xx_ep_free_buffer(struct usb_ep
*_ep
, void *buf
, dma_addr_t dma
,
387 /*-------------------------------------------------------------------------*/
390 * done - retire a request; caller blocked irqs
392 static void done(struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
, int status
)
394 unsigned stopped
= ep
->stopped
;
396 list_del_init(&req
->queue
);
398 if (likely (req
->req
.status
== -EINPROGRESS
))
399 req
->req
.status
= status
;
401 status
= req
->req
.status
;
403 if (status
&& status
!= -ESHUTDOWN
)
404 DBG(DBG_VERBOSE
, "complete %s req %p stat %d len %u/%u\n",
405 ep
->ep
.name
, &req
->req
, status
,
406 req
->req
.actual
, req
->req
.length
);
408 /* don't modify queue heads during completion callback */
410 req
->req
.complete(&ep
->ep
, &req
->req
);
411 ep
->stopped
= stopped
;
415 static inline void ep0_idle (struct pxa2xx_udc
*dev
)
417 dev
->ep0state
= EP0_IDLE
;
421 write_packet(volatile u32
*uddr
, struct pxa2xx_request
*req
, unsigned max
)
424 unsigned length
, count
;
426 buf
= req
->req
.buf
+ req
->req
.actual
;
429 /* how big will this packet be? */
430 length
= min(req
->req
.length
- req
->req
.actual
, max
);
431 req
->req
.actual
+= length
;
434 while (likely(count
--))
441 * write to an IN endpoint fifo, as many packets as possible.
442 * irqs will use this to write the rest later.
443 * caller guarantees at least one packet buffer is ready (or a zlp).
446 write_fifo (struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
)
450 max
= le16_to_cpu(ep
->desc
->wMaxPacketSize
);
453 int is_last
, is_short
;
455 count
= write_packet(ep
->reg_uddr
, req
, max
);
457 /* last packet is usually short (or a zlp) */
458 if (unlikely (count
!= max
))
459 is_last
= is_short
= 1;
461 if (likely(req
->req
.length
!= req
->req
.actual
)
466 /* interrupt/iso maxpacket may not fill the fifo */
467 is_short
= unlikely (max
< ep
->fifo_size
);
470 DBG(DBG_VERY_NOISY
, "wrote %s %d bytes%s%s %d left %p\n",
472 is_last
? "/L" : "", is_short
? "/S" : "",
473 req
->req
.length
- req
->req
.actual
, req
);
475 /* let loose that packet. maybe try writing another one,
476 * double buffering might work. TSP, TPC, and TFS
477 * bit values are the same for all normal IN endpoints.
479 *ep
->reg_udccs
= UDCCS_BI_TPC
;
481 *ep
->reg_udccs
= UDCCS_BI_TSP
;
483 /* requests complete when all IN data is in the FIFO */
486 if (list_empty(&ep
->queue
) || unlikely(ep
->dma
>= 0)) {
487 pio_irq_disable (ep
->bEndpointAddress
);
489 /* unaligned data and zlps couldn't use dma */
490 if (unlikely(!list_empty(&ep
->queue
))) {
491 req
= list_entry(ep
->queue
.next
,
492 struct pxa2xx_request
, queue
);
501 // TODO experiment: how robust can fifo mode tweaking be?
502 // double buffering is off in the default fifo mode, which
503 // prevents TFS from being set here.
505 } while (*ep
->reg_udccs
& UDCCS_BI_TFS
);
509 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
510 * ep0 data stage. these chips want very simple state transitions.
513 void ep0start(struct pxa2xx_udc
*dev
, u32 flags
, const char *tag
)
515 UDCCS0
= flags
|UDCCS0_SA
|UDCCS0_OPR
;
517 dev
->req_pending
= 0;
518 DBG(DBG_VERY_NOISY
, "%s %s, %02x/%02x\n",
519 __FUNCTION__
, tag
, UDCCS0
, flags
);
523 write_ep0_fifo (struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
)
528 count
= write_packet(&UDDR0
, req
, EP0_FIFO_SIZE
);
529 ep
->dev
->stats
.write
.bytes
+= count
;
531 /* last packet "must be" short (or a zlp) */
532 is_short
= (count
!= EP0_FIFO_SIZE
);
534 DBG(DBG_VERY_NOISY
, "ep0in %d bytes %d left %p\n", count
,
535 req
->req
.length
- req
->req
.actual
, req
);
537 if (unlikely (is_short
)) {
538 if (ep
->dev
->req_pending
)
539 ep0start(ep
->dev
, UDCCS0_IPR
, "short IN");
543 count
= req
->req
.length
;
547 /* This seems to get rid of lost status irqs in some cases:
548 * host responds quickly, or next request involves config
549 * change automagic, or should have been hidden, or ...
551 * FIXME get rid of all udelays possible...
553 if (count
>= EP0_FIFO_SIZE
) {
556 if ((UDCCS0
& UDCCS0_OPR
) != 0) {
557 /* clear OPR, generate ack */
566 } else if (ep
->dev
->req_pending
)
567 ep0start(ep
->dev
, 0, "IN");
573 * read_fifo - unload packet(s) from the fifo we use for usb OUT
574 * transfers and put them into the request. caller should have made
575 * sure there's at least one packet ready.
577 * returns true if the request completed because of short packet or the
578 * request buffer having filled (and maybe overran till end-of-packet).
581 read_fifo (struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
)
586 unsigned bufferspace
, count
, is_short
;
588 /* make sure there's a packet in the FIFO.
589 * UDCCS_{BO,IO}_RPC are all the same bit value.
590 * UDCCS_{BO,IO}_RNE are all the same bit value.
592 udccs
= *ep
->reg_udccs
;
593 if (unlikely ((udccs
& UDCCS_BO_RPC
) == 0))
595 buf
= req
->req
.buf
+ req
->req
.actual
;
597 bufferspace
= req
->req
.length
- req
->req
.actual
;
599 /* read all bytes from this packet */
600 if (likely (udccs
& UDCCS_BO_RNE
)) {
601 count
= 1 + (0x0ff & *ep
->reg_ubcr
);
602 req
->req
.actual
+= min (count
, bufferspace
);
605 is_short
= (count
< ep
->ep
.maxpacket
);
606 DBG(DBG_VERY_NOISY
, "read %s %02x, %d bytes%s req %p %d/%d\n",
607 ep
->ep
.name
, udccs
, count
,
608 is_short
? "/S" : "",
609 req
, req
->req
.actual
, req
->req
.length
);
610 while (likely (count
-- != 0)) {
611 u8 byte
= (u8
) *ep
->reg_uddr
;
613 if (unlikely (bufferspace
== 0)) {
614 /* this happens when the driver's buffer
615 * is smaller than what the host sent.
616 * discard the extra data.
618 if (req
->req
.status
!= -EOVERFLOW
)
619 DMSG("%s overflow %d\n",
621 req
->req
.status
= -EOVERFLOW
;
627 *ep
->reg_udccs
= UDCCS_BO_RPC
;
628 /* RPC/RSP/RNE could now reflect the other packet buffer */
630 /* iso is one request per packet */
631 if (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
632 if (udccs
& UDCCS_IO_ROF
)
633 req
->req
.status
= -EHOSTUNREACH
;
634 /* more like "is_done" */
639 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
641 if (list_empty(&ep
->queue
))
642 pio_irq_disable (ep
->bEndpointAddress
);
646 /* finished that packet. the next one may be waiting... */
652 * special ep0 version of the above. no UBCR0 or double buffering; status
653 * handshaking is magic. most device protocols don't need control-OUT.
654 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
655 * protocols do use them.
658 read_ep0_fifo (struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
)
661 unsigned bufferspace
;
663 buf
= req
->req
.buf
+ req
->req
.actual
;
664 bufferspace
= req
->req
.length
- req
->req
.actual
;
666 while (UDCCS0
& UDCCS0_RNE
) {
669 if (unlikely (bufferspace
== 0)) {
670 /* this happens when the driver's buffer
671 * is smaller than what the host sent.
672 * discard the extra data.
674 if (req
->req
.status
!= -EOVERFLOW
)
675 DMSG("%s overflow\n", ep
->ep
.name
);
676 req
->req
.status
= -EOVERFLOW
;
684 UDCCS0
= UDCCS0_OPR
| UDCCS0_IPR
;
687 if (req
->req
.actual
>= req
->req
.length
)
690 /* finished that packet. the next one may be waiting... */
696 #define MAX_IN_DMA ((DCMD_LENGTH + 1) - BULK_FIFO_SIZE)
699 start_dma_nodesc(struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
, int is_in
)
701 u32 dcmd
= req
->req
.length
;
702 u32 buf
= req
->req
.dma
;
703 u32 fifo
= io_v2p ((u32
)ep
->reg_uddr
);
705 /* caller guarantees there's a packet or more remaining
706 * - IN may end with a short packet (TSP set separately),
707 * - OUT is always full length
709 buf
+= req
->req
.actual
;
710 dcmd
-= req
->req
.actual
;
713 /* no-descriptor mode can be simple for bulk-in, iso-in, iso-out */
714 DCSR(ep
->dma
) = DCSR_NODESC
;
716 DSADR(ep
->dma
) = buf
;
717 DTADR(ep
->dma
) = fifo
;
718 if (dcmd
> MAX_IN_DMA
)
721 ep
->dma_fixup
= (dcmd
% ep
->ep
.maxpacket
) != 0;
722 dcmd
|= DCMD_BURST32
| DCMD_WIDTH1
723 | DCMD_FLOWTRG
| DCMD_INCSRCADDR
;
726 DSADR(ep
->dma
) = fifo
;
727 DTADR(ep
->dma
) = buf
;
728 if (ep
->bmAttributes
!= USB_ENDPOINT_XFER_ISOC
)
729 dcmd
= ep
->ep
.maxpacket
;
730 dcmd
|= DCMD_BURST32
| DCMD_WIDTH1
731 | DCMD_FLOWSRC
| DCMD_INCTRGADDR
;
734 DCMD(ep
->dma
) = dcmd
;
735 DCSR(ep
->dma
) = DCSR_RUN
| DCSR_NODESC
737 ? DCSR_STOPIRQEN
/* use dma_nodesc_handler() */
738 : 0); /* use handle_ep() */
741 static void kick_dma(struct pxa2xx_ep
*ep
, struct pxa2xx_request
*req
)
743 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
746 /* unaligned tx buffers and zlps only work with PIO */
747 if ((req
->req
.dma
& 0x0f) != 0
748 || unlikely((req
->req
.length
- req
->req
.actual
)
750 pio_irq_enable(ep
->bEndpointAddress
);
751 if ((*ep
->reg_udccs
& UDCCS_BI_TFS
) != 0)
752 (void) write_fifo(ep
, req
);
754 start_dma_nodesc(ep
, req
, USB_DIR_IN
);
757 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
) {
758 DMSG("%s short dma read...\n", ep
->ep
.name
);
759 /* we're always set up for pio out */
762 *ep
->reg_udccs
= UDCCS_BO_DME
763 | (*ep
->reg_udccs
& UDCCS_BO_FST
);
764 start_dma_nodesc(ep
, req
, USB_DIR_OUT
);
769 static void cancel_dma(struct pxa2xx_ep
*ep
)
771 struct pxa2xx_request
*req
;
774 if (DCSR(ep
->dma
) == 0 || list_empty(&ep
->queue
))
778 while ((DCSR(ep
->dma
) & DCSR_STOPSTATE
) == 0)
781 req
= list_entry(ep
->queue
.next
, struct pxa2xx_request
, queue
);
782 tmp
= DCMD(ep
->dma
) & DCMD_LENGTH
;
783 req
->req
.actual
= req
->req
.length
- (tmp
& DCMD_LENGTH
);
785 /* the last tx packet may be incomplete, so flush the fifo.
786 * FIXME correct req.actual if we can
788 if (ep
->bEndpointAddress
& USB_DIR_IN
)
789 *ep
->reg_udccs
= UDCCS_BI_FTF
;
792 /* dma channel stopped ... normal tx end (IN), or on error (IN/OUT) */
793 static void dma_nodesc_handler(int dmach
, void *_ep
, struct pt_regs
*r
)
795 struct pxa2xx_ep
*ep
= _ep
;
796 struct pxa2xx_request
*req
;
801 req
= list_entry(ep
->queue
.next
, struct pxa2xx_request
, queue
);
804 ep
->dev
->stats
.irqs
++;
805 HEX_DISPLAY(ep
->dev
->stats
.irqs
);
810 if ((tmp
& DCSR_STOPSTATE
) == 0
811 || (DDADR(ep
->dma
) & DDADR_STOP
) != 0) {
812 DBG(DBG_VERBOSE
, "%s, dcsr %08x ddadr %08x\n",
813 ep
->ep
.name
, DCSR(ep
->dma
), DDADR(ep
->dma
));
816 DCSR(ep
->dma
) = 0; /* clear DCSR_STOPSTATE */
818 /* update transfer status */
819 completed
= tmp
& DCSR_BUSERR
;
820 if (ep
->bEndpointAddress
& USB_DIR_IN
)
821 tmp
= DSADR(ep
->dma
);
823 tmp
= DTADR(ep
->dma
);
824 req
->req
.actual
= tmp
- req
->req
.dma
;
826 /* FIXME seems we sometimes see partial transfers... */
828 if (unlikely(completed
!= 0))
829 req
->req
.status
= -EIO
;
830 else if (req
->req
.actual
) {
831 /* these registers have zeroes in low bits; they miscount
832 * some (end-of-transfer) short packets: tx 14 as tx 12
835 req
->req
.actual
= min(req
->req
.actual
+ 3,
838 tmp
= (req
->req
.length
- req
->req
.actual
);
839 completed
= (tmp
== 0);
840 if (completed
&& (ep
->bEndpointAddress
& USB_DIR_IN
)) {
842 /* maybe validate final short packet ... */
843 if ((req
->req
.actual
% ep
->ep
.maxpacket
) != 0)
844 *ep
->reg_udccs
= UDCCS_BI_TSP
/*|UDCCS_BI_TPC*/;
846 /* ... or zlp, using pio fallback */
847 else if (ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
849 DMSG("%s zlp terminate ...\n", ep
->ep
.name
);
855 if (likely(completed
)) {
858 /* maybe re-activate after completion */
859 if (ep
->stopped
|| list_empty(&ep
->queue
))
861 req
= list_entry(ep
->queue
.next
, struct pxa2xx_request
, queue
);
870 /*-------------------------------------------------------------------------*/
873 pxa2xx_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, int gfp_flags
)
875 struct pxa2xx_request
*req
;
876 struct pxa2xx_ep
*ep
;
877 struct pxa2xx_udc
*dev
;
880 req
= container_of(_req
, struct pxa2xx_request
, req
);
881 if (unlikely (!_req
|| !_req
->complete
|| !_req
->buf
882 || !list_empty(&req
->queue
))) {
883 DMSG("%s, bad params\n", __FUNCTION__
);
887 ep
= container_of(_ep
, struct pxa2xx_ep
, ep
);
888 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
889 DMSG("%s, bad ep\n", __FUNCTION__
);
894 if (unlikely (!dev
->driver
895 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
896 DMSG("%s, bogus device state\n", __FUNCTION__
);
900 /* iso is always one packet per request, that's the only way
901 * we can report per-packet status. that also helps with dma.
903 if (unlikely (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
904 && req
->req
.length
> le16_to_cpu
905 (ep
->desc
->wMaxPacketSize
)))
909 // FIXME caller may already have done the dma mapping
911 _req
->dma
= dma_map_single(dev
->dev
,
912 _req
->buf
, _req
->length
,
913 ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0)
919 DBG(DBG_NOISY
, "%s queue req %p, len %d buf %p\n",
920 _ep
->name
, _req
, _req
->length
, _req
->buf
);
922 local_irq_save(flags
);
924 _req
->status
= -EINPROGRESS
;
927 /* kickstart this i/o queue? */
928 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
929 if (ep
->desc
== 0 /* ep0 */) {
930 unsigned length
= _req
->length
;
932 switch (dev
->ep0state
) {
933 case EP0_IN_DATA_PHASE
:
934 dev
->stats
.write
.ops
++;
935 if (write_ep0_fifo(ep
, req
))
939 case EP0_OUT_DATA_PHASE
:
940 dev
->stats
.read
.ops
++;
942 if (dev
->req_config
) {
943 DBG(DBG_VERBOSE
, "ep0 config ack%s\n",
944 dev
->has_cfr
? "" : " raced");
946 UDCCFR
= UDCCFR_AREN
|UDCCFR_ACM
949 dev
->ep0state
= EP0_END_XFER
;
950 local_irq_restore (flags
);
953 if (dev
->req_pending
)
954 ep0start(dev
, UDCCS0_IPR
, "OUT");
955 if (length
== 0 || ((UDCCS0
& UDCCS0_RNE
) != 0
956 && read_ep0_fifo(ep
, req
))) {
964 DMSG("ep0 i/o, odd state %d\n", dev
->ep0state
);
965 local_irq_restore (flags
);
969 /* either start dma or prime pio pump */
970 } else if (ep
->dma
>= 0) {
973 /* can the FIFO can satisfy the request immediately? */
974 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
975 && (*ep
->reg_udccs
& UDCCS_BI_TFS
) != 0
976 && write_fifo(ep
, req
)) {
978 } else if ((*ep
->reg_udccs
& UDCCS_BO_RFS
) != 0
979 && read_fifo(ep
, req
)) {
983 if (likely (req
&& ep
->desc
) && ep
->dma
< 0)
984 pio_irq_enable(ep
->bEndpointAddress
);
987 /* pio or dma irq handler advances the queue. */
988 if (likely (req
!= 0))
989 list_add_tail(&req
->queue
, &ep
->queue
);
990 local_irq_restore(flags
);
997 * nuke - dequeue ALL requests
999 static void nuke(struct pxa2xx_ep
*ep
, int status
)
1001 struct pxa2xx_request
*req
;
1003 /* called with irqs blocked */
1005 if (ep
->dma
>= 0 && !ep
->stopped
)
1008 while (!list_empty(&ep
->queue
)) {
1009 req
= list_entry(ep
->queue
.next
,
1010 struct pxa2xx_request
,
1012 done(ep
, req
, status
);
1015 pio_irq_disable (ep
->bEndpointAddress
);
1019 /* dequeue JUST ONE request */
1020 static int pxa2xx_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1022 struct pxa2xx_ep
*ep
;
1023 struct pxa2xx_request
*req
;
1024 unsigned long flags
;
1026 ep
= container_of(_ep
, struct pxa2xx_ep
, ep
);
1027 if (!_ep
|| ep
->ep
.name
== ep0name
)
1030 local_irq_save(flags
);
1032 /* make sure it's actually queued on this endpoint */
1033 list_for_each_entry (req
, &ep
->queue
, queue
) {
1034 if (&req
->req
== _req
)
1037 if (&req
->req
!= _req
) {
1038 local_irq_restore(flags
);
1043 if (ep
->dma
>= 0 && ep
->queue
.next
== &req
->queue
&& !ep
->stopped
) {
1045 done(ep
, req
, -ECONNRESET
);
1047 if (!list_empty(&ep
->queue
)) {
1048 req
= list_entry(ep
->queue
.next
,
1049 struct pxa2xx_request
, queue
);
1054 done(ep
, req
, -ECONNRESET
);
1056 local_irq_restore(flags
);
1060 /*-------------------------------------------------------------------------*/
1062 static int pxa2xx_ep_set_halt(struct usb_ep
*_ep
, int value
)
1064 struct pxa2xx_ep
*ep
;
1065 unsigned long flags
;
1067 ep
= container_of(_ep
, struct pxa2xx_ep
, ep
);
1069 || (!ep
->desc
&& ep
->ep
.name
!= ep0name
))
1070 || ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
1071 DMSG("%s, bad ep\n", __FUNCTION__
);
1075 /* this path (reset toggle+halt) is needed to implement
1076 * SET_INTERFACE on normal hardware. but it can't be
1077 * done from software on the PXA UDC, and the hardware
1078 * forgets to do it as part of SET_INTERFACE automagic.
1080 DMSG("only host can clear %s halt\n", _ep
->name
);
1084 local_irq_save(flags
);
1086 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
1087 && ((*ep
->reg_udccs
& UDCCS_BI_TFS
) == 0
1088 || !list_empty(&ep
->queue
))) {
1089 local_irq_restore(flags
);
1093 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
1094 *ep
->reg_udccs
= UDCCS_BI_FST
|UDCCS_BI_FTF
;
1096 /* ep0 needs special care */
1098 start_watchdog(ep
->dev
);
1099 ep
->dev
->req_pending
= 0;
1100 ep
->dev
->ep0state
= EP0_STALL
;
1102 /* and bulk/intr endpoints like dropping stalls too */
1105 for (i
= 0; i
< 1000; i
+= 20) {
1106 if (*ep
->reg_udccs
& UDCCS_BI_SST
)
1111 local_irq_restore(flags
);
1113 DBG(DBG_VERBOSE
, "%s halt\n", _ep
->name
);
1117 static int pxa2xx_ep_fifo_status(struct usb_ep
*_ep
)
1119 struct pxa2xx_ep
*ep
;
1121 ep
= container_of(_ep
, struct pxa2xx_ep
, ep
);
1123 DMSG("%s, bad ep\n", __FUNCTION__
);
1126 /* pxa can't report unclaimed bytes from IN fifos */
1127 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0)
1129 if (ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
1130 || (*ep
->reg_udccs
& UDCCS_BO_RFS
) == 0)
1133 return (*ep
->reg_ubcr
& 0xfff) + 1;
1136 static void pxa2xx_ep_fifo_flush(struct usb_ep
*_ep
)
1138 struct pxa2xx_ep
*ep
;
1140 ep
= container_of(_ep
, struct pxa2xx_ep
, ep
);
1141 if (!_ep
|| ep
->ep
.name
== ep0name
|| !list_empty(&ep
->queue
)) {
1142 DMSG("%s, bad ep\n", __FUNCTION__
);
1146 /* toggle and halt bits stay unchanged */
1148 /* for OUT, just read and discard the FIFO contents. */
1149 if ((ep
->bEndpointAddress
& USB_DIR_IN
) == 0) {
1150 while (((*ep
->reg_udccs
) & UDCCS_BO_RNE
) != 0)
1151 (void) *ep
->reg_uddr
;
1155 /* most IN status is the same, but ISO can't stall */
1156 *ep
->reg_udccs
= UDCCS_BI_TPC
|UDCCS_BI_FTF
|UDCCS_BI_TUR
1157 | (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
)
1162 static struct usb_ep_ops pxa2xx_ep_ops
= {
1163 .enable
= pxa2xx_ep_enable
,
1164 .disable
= pxa2xx_ep_disable
,
1166 .alloc_request
= pxa2xx_ep_alloc_request
,
1167 .free_request
= pxa2xx_ep_free_request
,
1169 .alloc_buffer
= pxa2xx_ep_alloc_buffer
,
1170 .free_buffer
= pxa2xx_ep_free_buffer
,
1172 .queue
= pxa2xx_ep_queue
,
1173 .dequeue
= pxa2xx_ep_dequeue
,
1175 .set_halt
= pxa2xx_ep_set_halt
,
1176 .fifo_status
= pxa2xx_ep_fifo_status
,
1177 .fifo_flush
= pxa2xx_ep_fifo_flush
,
1181 /* ---------------------------------------------------------------------------
1182 * device-scoped parts of the api to the usb controller hardware
1183 * ---------------------------------------------------------------------------
1186 static int pxa2xx_udc_get_frame(struct usb_gadget
*_gadget
)
1188 return ((UFNRH
& 0x07) << 8) | (UFNRL
& 0xff);
1191 static int pxa2xx_udc_wakeup(struct usb_gadget
*_gadget
)
1193 /* host may not have enabled remote wakeup */
1194 if ((UDCCS0
& UDCCS0_DRWF
) == 0)
1195 return -EHOSTUNREACH
;
1196 udc_set_mask_UDCCR(UDCCR_RSM
);
1200 static void stop_activity(struct pxa2xx_udc
*, struct usb_gadget_driver
*);
1201 static void udc_enable (struct pxa2xx_udc
*);
1202 static void udc_disable(struct pxa2xx_udc
*);
1204 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
1207 static int pullup(struct pxa2xx_udc
*udc
, int is_active
)
1209 is_active
= is_active
&& udc
->vbus
&& udc
->pullup
;
1210 DMSG("%s\n", is_active
? "active" : "inactive");
1214 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
1215 DMSG("disconnect %s\n", udc
->driver
1216 ? udc
->driver
->driver
.name
1218 stop_activity(udc
, udc
->driver
);
1225 /* VBUS reporting logically comes from a transceiver */
1226 static int pxa2xx_udc_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
1228 struct pxa2xx_udc
*udc
;
1230 udc
= container_of(_gadget
, struct pxa2xx_udc
, gadget
);
1231 udc
->vbus
= is_active
= (is_active
!= 0);
1232 DMSG("vbus %s\n", is_active
? "supplied" : "inactive");
1233 pullup(udc
, is_active
);
1237 /* drivers may have software control over D+ pullup */
1238 static int pxa2xx_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
1240 struct pxa2xx_udc
*udc
;
1242 udc
= container_of(_gadget
, struct pxa2xx_udc
, gadget
);
1244 /* not all boards support pullup control */
1245 if (!udc
->mach
->udc_command
)
1248 is_active
= (is_active
!= 0);
1249 udc
->pullup
= is_active
;
1250 pullup(udc
, is_active
);
1254 static const struct usb_gadget_ops pxa2xx_udc_ops
= {
1255 .get_frame
= pxa2xx_udc_get_frame
,
1256 .wakeup
= pxa2xx_udc_wakeup
,
1257 .vbus_session
= pxa2xx_udc_vbus_session
,
1258 .pullup
= pxa2xx_udc_pullup
,
1260 // .vbus_draw ... boards may consume current from VBUS, up to
1261 // 100-500mA based on config. the 500uA suspend ceiling means
1262 // that exclusively vbus-powered PXA designs violate USB specs.
1265 /*-------------------------------------------------------------------------*/
1267 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1269 static const char proc_node_name
[] = "driver/udc";
1272 udc_proc_read(char *page
, char **start
, off_t off
, int count
,
1273 int *eof
, void *_dev
)
1276 struct pxa2xx_udc
*dev
= _dev
;
1278 unsigned size
= count
;
1279 unsigned long flags
;
1286 local_irq_save(flags
);
1288 /* basic device status */
1289 t
= scnprintf(next
, size
, DRIVER_DESC
"\n"
1290 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1291 driver_name
, DRIVER_VERSION SIZE_STR DMASTR
,
1292 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1293 is_usb_connected() ? "full speed" : "disconnected");
1297 /* registers for device and ep0 */
1298 t
= scnprintf(next
, size
,
1299 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1300 UICR1
, UICR0
, USIR1
, USIR0
, UFNRH
, UFNRL
);
1305 t
= scnprintf(next
, size
,
1306 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp
,
1307 (tmp
& UDCCR_REM
) ? " rem" : "",
1308 (tmp
& UDCCR_RSTIR
) ? " rstir" : "",
1309 (tmp
& UDCCR_SRM
) ? " srm" : "",
1310 (tmp
& UDCCR_SUSIR
) ? " susir" : "",
1311 (tmp
& UDCCR_RESIR
) ? " resir" : "",
1312 (tmp
& UDCCR_RSM
) ? " rsm" : "",
1313 (tmp
& UDCCR_UDA
) ? " uda" : "",
1314 (tmp
& UDCCR_UDE
) ? " ude" : "");
1319 t
= scnprintf(next
, size
,
1320 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp
,
1321 (tmp
& UDCCS0_SA
) ? " sa" : "",
1322 (tmp
& UDCCS0_RNE
) ? " rne" : "",
1323 (tmp
& UDCCS0_FST
) ? " fst" : "",
1324 (tmp
& UDCCS0_SST
) ? " sst" : "",
1325 (tmp
& UDCCS0_DRWF
) ? " dwrf" : "",
1326 (tmp
& UDCCS0_FTF
) ? " ftf" : "",
1327 (tmp
& UDCCS0_IPR
) ? " ipr" : "",
1328 (tmp
& UDCCS0_OPR
) ? " opr" : "");
1334 t
= scnprintf(next
, size
,
1335 "udccfr %02X =%s%s\n", tmp
,
1336 (tmp
& UDCCFR_AREN
) ? " aren" : "",
1337 (tmp
& UDCCFR_ACM
) ? " acm" : "");
1342 if (!is_usb_connected() || !dev
->driver
)
1345 t
= scnprintf(next
, size
, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1346 dev
->stats
.write
.bytes
, dev
->stats
.write
.ops
,
1347 dev
->stats
.read
.bytes
, dev
->stats
.read
.ops
,
1352 /* dump endpoint queues */
1353 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1354 struct pxa2xx_ep
*ep
= &dev
->ep
[i
];
1355 struct pxa2xx_request
*req
;
1359 const struct usb_endpoint_descriptor
*d
;
1364 tmp
= *dev
->ep
[i
].reg_udccs
;
1365 t
= scnprintf(next
, size
,
1366 "%s max %d %s udccs %02x irqs %lu/%lu\n",
1367 ep
->ep
.name
, le16_to_cpu (d
->wMaxPacketSize
),
1368 (ep
->dma
>= 0) ? "dma" : "pio", tmp
,
1369 ep
->pio_irqs
, ep
->dma_irqs
);
1370 /* TODO translate all five groups of udccs bits! */
1372 } else /* ep0 should only have one transfer queued */
1373 t
= scnprintf(next
, size
, "ep0 max 16 pio irqs %lu\n",
1375 if (t
<= 0 || t
> size
)
1380 if (list_empty(&ep
->queue
)) {
1381 t
= scnprintf(next
, size
, "\t(nothing queued)\n");
1382 if (t
<= 0 || t
> size
)
1388 list_for_each_entry(req
, &ep
->queue
, queue
) {
1390 if (ep
->dma
>= 0 && req
->queue
.prev
== &ep
->queue
)
1391 t
= scnprintf(next
, size
,
1392 "\treq %p len %d/%d "
1393 "buf %p (dma%d dcmd %08x)\n",
1394 &req
->req
, req
->req
.actual
,
1395 req
->req
.length
, req
->req
.buf
,
1396 ep
->dma
, DCMD(ep
->dma
)
1397 // low 13 bits == bytes-to-go
1401 t
= scnprintf(next
, size
,
1402 "\treq %p len %d/%d buf %p\n",
1403 &req
->req
, req
->req
.actual
,
1404 req
->req
.length
, req
->req
.buf
);
1405 if (t
<= 0 || t
> size
)
1413 local_irq_restore(flags
);
1415 return count
- size
;
1418 #define create_proc_files() \
1419 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1420 #define remove_proc_files() \
1421 remove_proc_entry(proc_node_name, NULL)
1423 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1425 #define create_proc_files() do {} while (0)
1426 #define remove_proc_files() do {} while (0)
1428 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1430 /* "function" sysfs attribute */
1432 show_function (struct device
*_dev
, char *buf
)
1434 struct pxa2xx_udc
*dev
= dev_get_drvdata (_dev
);
1437 || !dev
->driver
->function
1438 || strlen (dev
->driver
->function
) > PAGE_SIZE
)
1440 return scnprintf (buf
, PAGE_SIZE
, "%s\n", dev
->driver
->function
);
1442 static DEVICE_ATTR (function
, S_IRUGO
, show_function
, NULL
);
1444 /*-------------------------------------------------------------------------*/
1447 * udc_disable - disable USB device controller
1449 static void udc_disable(struct pxa2xx_udc
*dev
)
1451 /* block all irqs */
1452 udc_set_mask_UDCCR(UDCCR_SRM
|UDCCR_REM
);
1453 UICR0
= UICR1
= 0xff;
1456 /* if hardware supports it, disconnect from usb */
1457 make_usb_disappear();
1459 udc_clear_mask_UDCCR(UDCCR_UDE
);
1461 #ifdef CONFIG_ARCH_PXA
1462 /* Disable clock for USB device */
1463 pxa_set_cken(CKEN11_USB
, 0);
1467 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1473 * udc_reinit - initialize software state
1475 static void udc_reinit(struct pxa2xx_udc
*dev
)
1479 /* device/ep0 records init */
1480 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1481 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1482 dev
->ep0state
= EP0_IDLE
;
1484 /* basic endpoint records init */
1485 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1486 struct pxa2xx_ep
*ep
= &dev
->ep
[i
];
1489 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1493 INIT_LIST_HEAD (&ep
->queue
);
1494 ep
->pio_irqs
= ep
->dma_irqs
= 0;
1497 /* the rest was statically initialized, and is read-only */
1500 /* until it's enabled, this UDC should be completely invisible
1503 static void udc_enable (struct pxa2xx_udc
*dev
)
1505 udc_clear_mask_UDCCR(UDCCR_UDE
);
1507 #ifdef CONFIG_ARCH_PXA
1508 /* Enable clock for USB device */
1509 pxa_set_cken(CKEN11_USB
, 1);
1513 /* try to clear these bits before we enable the udc */
1514 udc_ack_int_UDCCR(UDCCR_SUSIR
|/*UDCCR_RSTIR|*/UDCCR_RESIR
);
1517 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1518 dev
->stats
.irqs
= 0;
1521 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1523 * - if RESET is already in progress, ack interrupt
1524 * - unmask reset interrupt
1526 udc_set_mask_UDCCR(UDCCR_UDE
);
1527 if (!(UDCCR
& UDCCR_UDA
))
1528 udc_ack_int_UDCCR(UDCCR_RSTIR
);
1530 if (dev
->has_cfr
/* UDC_RES2 is defined */) {
1531 /* pxa255 (a0+) can avoid a set_config race that could
1532 * prevent gadget drivers from configuring correctly
1534 UDCCFR
= UDCCFR_ACM
| UDCCFR_MB1
;
1536 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1537 * which could result in missing packets and interrupts.
1538 * supposedly one bit per endpoint, controlling whether it
1539 * double buffers or not; ACM/AREN bits fit into the holes.
1540 * zero bits (like USIR0_IRx) disable double buffering.
1546 #ifdef DISABLE_TEST_MODE
1547 /* "test mode" seems to have become the default in later chip
1548 * revs, preventing double buffering (and invalidating docs).
1549 * this EXPERIMENT enables it for bulk endpoints by tweaking
1550 * undefined/reserved register bits (that other drivers clear).
1551 * Belcarra code comments noted this usage.
1553 if (fifo_mode
& 1) { /* IN endpoints */
1554 UDC_RES1
|= USIR0_IR1
|USIR0_IR6
;
1555 UDC_RES2
|= USIR1_IR11
;
1557 if (fifo_mode
& 2) { /* OUT endpoints */
1558 UDC_RES1
|= USIR0_IR2
|USIR0_IR7
;
1559 UDC_RES2
|= USIR1_IR12
;
1563 /* enable suspend/resume and reset irqs */
1564 udc_clear_mask_UDCCR(UDCCR_SRM
| UDCCR_REM
);
1566 /* enable ep0 irqs */
1567 UICR0
&= ~UICR0_IM0
;
1569 /* if hardware supports it, pullup D+ and wait for reset */
1574 /* when a driver is successfully registered, it will receive
1575 * control requests including set_configuration(), which enables
1576 * non-control requests. then usb traffic follows until a
1577 * disconnect is reported. then a host may connect again, or
1578 * the driver might get unbound.
1580 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1582 struct pxa2xx_udc
*dev
= the_controller
;
1586 || driver
->speed
!= USB_SPEED_FULL
1589 || !driver
->disconnect
1597 /* first hook up the driver ... */
1598 dev
->driver
= driver
;
1599 dev
->gadget
.dev
.driver
= &driver
->driver
;
1602 device_add (&dev
->gadget
.dev
);
1603 retval
= driver
->bind(&dev
->gadget
);
1605 DMSG("bind to driver %s --> error %d\n",
1606 driver
->driver
.name
, retval
);
1607 device_del (&dev
->gadget
.dev
);
1610 dev
->gadget
.dev
.driver
= NULL
;
1613 device_create_file(dev
->dev
, &dev_attr_function
);
1615 /* ... then enable host detection and ep0; and we're ready
1616 * for set_configuration as well as eventual disconnect.
1618 DMSG("registered gadget driver '%s'\n", driver
->driver
.name
);
1623 EXPORT_SYMBOL(usb_gadget_register_driver
);
1626 stop_activity(struct pxa2xx_udc
*dev
, struct usb_gadget_driver
*driver
)
1630 /* don't disconnect drivers more than once */
1631 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1633 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1635 /* prevent new request submissions, kill any outstanding requests */
1636 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1637 struct pxa2xx_ep
*ep
= &dev
->ep
[i
];
1640 nuke(ep
, -ESHUTDOWN
);
1642 del_timer_sync(&dev
->timer
);
1644 /* report disconnect; the driver is already quiesced */
1647 driver
->disconnect(&dev
->gadget
);
1649 /* re-init driver-visible data structures */
1653 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1655 struct pxa2xx_udc
*dev
= the_controller
;
1659 if (!driver
|| driver
!= dev
->driver
)
1662 local_irq_disable();
1664 stop_activity(dev
, driver
);
1667 driver
->unbind(&dev
->gadget
);
1670 device_del (&dev
->gadget
.dev
);
1671 device_remove_file(dev
->dev
, &dev_attr_function
);
1673 DMSG("unregistered gadget driver '%s'\n", driver
->driver
.name
);
1677 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1680 /*-------------------------------------------------------------------------*/
1682 #ifdef CONFIG_ARCH_LUBBOCK
1684 /* Lubbock has separate connect and disconnect irqs. More typical designs
1685 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1689 lubbock_vbus_irq(int irq
, void *_dev
, struct pt_regs
*r
)
1691 struct pxa2xx_udc
*dev
= _dev
;
1695 HEX_DISPLAY(dev
->stats
.irqs
);
1697 case LUBBOCK_USB_IRQ
:
1700 disable_irq(LUBBOCK_USB_IRQ
);
1701 enable_irq(LUBBOCK_USB_DISC_IRQ
);
1703 case LUBBOCK_USB_DISC_IRQ
:
1706 disable_irq(LUBBOCK_USB_DISC_IRQ
);
1707 enable_irq(LUBBOCK_USB_IRQ
);
1713 pxa2xx_udc_vbus_session(&dev
->gadget
, vbus
);
1720 /*-------------------------------------------------------------------------*/
1722 static inline void clear_ep_state (struct pxa2xx_udc
*dev
)
1726 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1727 * fifos, and pending transactions mustn't be continued in any case.
1729 for (i
= 1; i
< PXA_UDC_NUM_ENDPOINTS
; i
++)
1730 nuke(&dev
->ep
[i
], -ECONNABORTED
);
1733 static void udc_watchdog(unsigned long _dev
)
1735 struct pxa2xx_udc
*dev
= (void *)_dev
;
1737 local_irq_disable();
1738 if (dev
->ep0state
== EP0_STALL
1739 && (UDCCS0
& UDCCS0_FST
) == 0
1740 && (UDCCS0
& UDCCS0_SST
) == 0) {
1741 UDCCS0
= UDCCS0_FST
|UDCCS0_FTF
;
1742 DBG(DBG_VERBOSE
, "ep0 re-stall\n");
1743 start_watchdog(dev
);
1748 static void handle_ep0 (struct pxa2xx_udc
*dev
)
1750 u32 udccs0
= UDCCS0
;
1751 struct pxa2xx_ep
*ep
= &dev
->ep
[0];
1752 struct pxa2xx_request
*req
;
1754 struct usb_ctrlrequest r
;
1759 if (list_empty(&ep
->queue
))
1762 req
= list_entry(ep
->queue
.next
, struct pxa2xx_request
, queue
);
1764 /* clear stall status */
1765 if (udccs0
& UDCCS0_SST
) {
1767 UDCCS0
= UDCCS0_SST
;
1768 del_timer(&dev
->timer
);
1772 /* previous request unfinished? non-error iff back-to-back ... */
1773 if ((udccs0
& UDCCS0_SA
) != 0 && dev
->ep0state
!= EP0_IDLE
) {
1775 del_timer(&dev
->timer
);
1779 switch (dev
->ep0state
) {
1781 /* late-breaking status? */
1784 /* start control request? */
1785 if (likely((udccs0
& (UDCCS0_OPR
|UDCCS0_SA
|UDCCS0_RNE
))
1786 == (UDCCS0_OPR
|UDCCS0_SA
|UDCCS0_RNE
))) {
1791 /* read SETUP packet */
1792 for (i
= 0; i
< 8; i
++) {
1793 if (unlikely(!(UDCCS0
& UDCCS0_RNE
))) {
1795 DMSG("SETUP %d!\n", i
);
1798 u
.raw
[i
] = (u8
) UDDR0
;
1800 if (unlikely((UDCCS0
& UDCCS0_RNE
) != 0))
1804 DBG(DBG_VERBOSE
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1805 u
.r
.bRequestType
, u
.r
.bRequest
,
1806 le16_to_cpu(u
.r
.wValue
),
1807 le16_to_cpu(u
.r
.wIndex
),
1808 le16_to_cpu(u
.r
.wLength
));
1810 /* cope with automagic for some standard requests. */
1811 dev
->req_std
= (u
.r
.bRequestType
& USB_TYPE_MASK
)
1812 == USB_TYPE_STANDARD
;
1813 dev
->req_config
= 0;
1814 dev
->req_pending
= 1;
1815 switch (u
.r
.bRequest
) {
1816 /* hardware restricts gadget drivers here! */
1817 case USB_REQ_SET_CONFIGURATION
:
1818 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1819 /* reflect hardware's automagic
1820 * up to the gadget driver.
1823 dev
->req_config
= 1;
1824 clear_ep_state(dev
);
1825 /* if !has_cfr, there's no synch
1826 * else use AREN (later) not SA|OPR
1827 * USIR0_IR0 acts edge sensitive
1831 /* ... and here, even more ... */
1832 case USB_REQ_SET_INTERFACE
:
1833 if (u
.r
.bRequestType
== USB_RECIP_INTERFACE
) {
1834 /* udc hardware is broken by design:
1835 * - altsetting may only be zero;
1836 * - hw resets all interfaces' eps;
1837 * - ep reset doesn't include halt(?).
1839 DMSG("broken set_interface (%d/%d)\n",
1840 le16_to_cpu(u
.r
.wIndex
),
1841 le16_to_cpu(u
.r
.wValue
));
1845 /* hardware was supposed to hide this */
1846 case USB_REQ_SET_ADDRESS
:
1847 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1848 ep0start(dev
, 0, "address");
1854 if (u
.r
.bRequestType
& USB_DIR_IN
)
1855 dev
->ep0state
= EP0_IN_DATA_PHASE
;
1857 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
1859 i
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
1861 /* hardware automagic preventing STALL... */
1862 if (dev
->req_config
) {
1863 /* hardware sometimes neglects to tell
1864 * tell us about config change events,
1865 * so later ones may fail...
1867 WARN("config change %02x fail %d?\n",
1870 /* TODO experiment: if has_cfr,
1871 * hardware didn't ACK; maybe we
1872 * could actually STALL!
1875 DBG(DBG_VERBOSE
, "protocol STALL, "
1876 "%02x err %d\n", UDCCS0
, i
);
1878 /* the watchdog timer helps deal with cases
1879 * where udc seems to clear FST wrongly, and
1880 * then NAKs instead of STALLing.
1882 ep0start(dev
, UDCCS0_FST
|UDCCS0_FTF
, "stall");
1883 start_watchdog(dev
);
1884 dev
->ep0state
= EP0_STALL
;
1886 /* deferred i/o == no response yet */
1887 } else if (dev
->req_pending
) {
1888 if (likely(dev
->ep0state
== EP0_IN_DATA_PHASE
1889 || dev
->req_std
|| u
.r
.wLength
))
1890 ep0start(dev
, 0, "defer");
1892 ep0start(dev
, UDCCS0_IPR
, "defer/IPR");
1895 /* expect at least one data or status stage irq */
1898 } else if (likely((udccs0
& (UDCCS0_OPR
|UDCCS0_SA
))
1899 == (UDCCS0_OPR
|UDCCS0_SA
))) {
1902 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1903 * still observed on a pxa255 a0.
1905 DBG(DBG_VERBOSE
, "e131\n");
1908 /* read SETUP data, but don't trust it too much */
1909 for (i
= 0; i
< 8; i
++)
1910 u
.raw
[i
] = (u8
) UDDR0
;
1911 if ((u
.r
.bRequestType
& USB_RECIP_MASK
)
1914 if (u
.word
[0] == 0 && u
.word
[1] == 0)
1918 /* some random early IRQ:
1921 * - OPR got set, without SA (likely status stage)
1923 UDCCS0
= udccs0
& (UDCCS0_SA
|UDCCS0_OPR
);
1926 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
1927 if (udccs0
& UDCCS0_OPR
) {
1928 UDCCS0
= UDCCS0_OPR
|UDCCS0_FTF
;
1929 DBG(DBG_VERBOSE
, "ep0in premature status\n");
1933 } else /* irq was IPR clearing */ {
1935 /* this IN packet might finish the request */
1936 (void) write_ep0_fifo(ep
, req
);
1937 } /* else IN token before response was written */
1940 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
1941 if (udccs0
& UDCCS0_OPR
) {
1943 /* this OUT packet might finish the request */
1944 if (read_ep0_fifo(ep
, req
))
1946 /* else more OUT packets expected */
1947 } /* else OUT token before read was issued */
1948 } else /* irq was IPR clearing */ {
1949 DBG(DBG_VERBOSE
, "ep0out premature status\n");
1958 /* ack control-IN status (maybe in-zlp was skipped)
1959 * also appears after some config change events.
1961 if (udccs0
& UDCCS0_OPR
)
1962 UDCCS0
= UDCCS0_OPR
;
1966 UDCCS0
= UDCCS0_FST
;
1972 static void handle_ep(struct pxa2xx_ep
*ep
)
1974 struct pxa2xx_request
*req
;
1975 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
1981 if (likely (!list_empty(&ep
->queue
)))
1982 req
= list_entry(ep
->queue
.next
,
1983 struct pxa2xx_request
, queue
);
1987 // TODO check FST handling
1989 udccs
= *ep
->reg_udccs
;
1990 if (unlikely(is_in
)) { /* irq from TPC, SST, or (ISO) TUR */
1992 if (likely(ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
))
1993 tmp
|= UDCCS_BI_SST
;
1996 *ep
->reg_udccs
= tmp
;
1997 if (req
&& likely ((udccs
& UDCCS_BI_TFS
) != 0))
1998 completed
= write_fifo(ep
, req
);
2000 } else { /* irq from RPC (or for ISO, ROF) */
2001 if (likely(ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
))
2002 tmp
= UDCCS_BO_SST
| UDCCS_BO_DME
;
2004 tmp
= UDCCS_IO_ROF
| UDCCS_IO_DME
;
2007 *ep
->reg_udccs
= tmp
;
2009 /* fifos can hold packets, ready for reading... */
2012 // TODO didn't yet debug out-dma. this approach assumes
2013 // the worst about short packets and RPC; it might be better.
2015 if (likely(ep
->dma
>= 0)) {
2016 if (!(udccs
& UDCCS_BO_RSP
)) {
2017 *ep
->reg_udccs
= UDCCS_BO_RPC
;
2023 completed
= read_fifo(ep
, req
);
2025 pio_irq_disable (ep
->bEndpointAddress
);
2028 } while (completed
);
2032 * pxa2xx_udc_irq - interrupt handler
2034 * avoid delays in ep0 processing. the control handshaking isn't always
2035 * under software control (pxa250c0 and the pxa255 are better), and delays
2036 * could cause usb protocol errors.
2039 pxa2xx_udc_irq(int irq
, void *_dev
, struct pt_regs
*r
)
2041 struct pxa2xx_udc
*dev
= _dev
;
2045 HEX_DISPLAY(dev
->stats
.irqs
);
2051 /* SUSpend Interrupt Request */
2052 if (unlikely(udccr
& UDCCR_SUSIR
)) {
2053 udc_ack_int_UDCCR(UDCCR_SUSIR
);
2055 DBG(DBG_VERBOSE
, "USB suspend%s\n", is_usb_connected()
2056 ? "" : "+disconnect");
2058 if (!is_usb_connected())
2059 stop_activity(dev
, dev
->driver
);
2060 else if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
2062 && dev
->driver
->suspend
)
2063 dev
->driver
->suspend(&dev
->gadget
);
2067 /* RESume Interrupt Request */
2068 if (unlikely(udccr
& UDCCR_RESIR
)) {
2069 udc_ack_int_UDCCR(UDCCR_RESIR
);
2071 DBG(DBG_VERBOSE
, "USB resume\n");
2073 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
2075 && dev
->driver
->resume
2076 && is_usb_connected())
2077 dev
->driver
->resume(&dev
->gadget
);
2080 /* ReSeT Interrupt Request - USB reset */
2081 if (unlikely(udccr
& UDCCR_RSTIR
)) {
2082 udc_ack_int_UDCCR(UDCCR_RSTIR
);
2085 if ((UDCCR
& UDCCR_UDA
) == 0) {
2086 DBG(DBG_VERBOSE
, "USB reset start\n");
2088 /* reset driver and endpoints,
2089 * in case that's not yet done
2091 stop_activity (dev
, dev
->driver
);
2094 DBG(DBG_VERBOSE
, "USB reset end\n");
2095 dev
->gadget
.speed
= USB_SPEED_FULL
;
2097 memset(&dev
->stats
, 0, sizeof dev
->stats
);
2098 /* driver and endpoints are still reset */
2102 u32 usir0
= USIR0
& ~UICR0
;
2103 u32 usir1
= USIR1
& ~UICR1
;
2106 if (unlikely (!usir0
&& !usir1
))
2109 DBG(DBG_VERY_NOISY
, "irq %02x.%02x\n", usir1
, usir0
);
2111 /* control traffic */
2112 if (usir0
& USIR0_IR0
) {
2113 dev
->ep
[0].pio_irqs
++;
2118 /* endpoint data transfers */
2119 for (i
= 0; i
< 8; i
++) {
2122 if (i
&& (usir0
& tmp
)) {
2123 handle_ep(&dev
->ep
[i
]);
2128 handle_ep(&dev
->ep
[i
+8]);
2135 /* we could also ask for 1 msec SOF (SIR) interrupts */
2141 /*-------------------------------------------------------------------------*/
2143 static void nop_release (struct device
*dev
)
2145 DMSG("%s %s\n", __FUNCTION__
, dev
->bus_id
);
2148 /* this uses load-time allocation and initialization (instead of
2149 * doing it at run-time) to save code, eliminate fault paths, and
2150 * be more obviously correct.
2152 static struct pxa2xx_udc memory
= {
2154 .ops
= &pxa2xx_udc_ops
,
2155 .ep0
= &memory
.ep
[0].ep
,
2156 .name
= driver_name
,
2159 .release
= nop_release
,
2163 /* control endpoint */
2167 .ops
= &pxa2xx_ep_ops
,
2168 .maxpacket
= EP0_FIFO_SIZE
,
2171 .reg_udccs
= &UDCCS0
,
2175 /* first group of endpoints */
2178 .name
= "ep1in-bulk",
2179 .ops
= &pxa2xx_ep_ops
,
2180 .maxpacket
= BULK_FIFO_SIZE
,
2183 .fifo_size
= BULK_FIFO_SIZE
,
2184 .bEndpointAddress
= USB_DIR_IN
| 1,
2185 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2186 .reg_udccs
= &UDCCS1
,
2192 .name
= "ep2out-bulk",
2193 .ops
= &pxa2xx_ep_ops
,
2194 .maxpacket
= BULK_FIFO_SIZE
,
2197 .fifo_size
= BULK_FIFO_SIZE
,
2198 .bEndpointAddress
= 2,
2199 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2200 .reg_udccs
= &UDCCS2
,
2205 #ifndef CONFIG_USB_PXA2XX_SMALL
2208 .name
= "ep3in-iso",
2209 .ops
= &pxa2xx_ep_ops
,
2210 .maxpacket
= ISO_FIFO_SIZE
,
2213 .fifo_size
= ISO_FIFO_SIZE
,
2214 .bEndpointAddress
= USB_DIR_IN
| 3,
2215 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2216 .reg_udccs
= &UDCCS3
,
2222 .name
= "ep4out-iso",
2223 .ops
= &pxa2xx_ep_ops
,
2224 .maxpacket
= ISO_FIFO_SIZE
,
2227 .fifo_size
= ISO_FIFO_SIZE
,
2228 .bEndpointAddress
= 4,
2229 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2230 .reg_udccs
= &UDCCS4
,
2237 .name
= "ep5in-int",
2238 .ops
= &pxa2xx_ep_ops
,
2239 .maxpacket
= INT_FIFO_SIZE
,
2242 .fifo_size
= INT_FIFO_SIZE
,
2243 .bEndpointAddress
= USB_DIR_IN
| 5,
2244 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2245 .reg_udccs
= &UDCCS5
,
2249 /* second group of endpoints */
2252 .name
= "ep6in-bulk",
2253 .ops
= &pxa2xx_ep_ops
,
2254 .maxpacket
= BULK_FIFO_SIZE
,
2257 .fifo_size
= BULK_FIFO_SIZE
,
2258 .bEndpointAddress
= USB_DIR_IN
| 6,
2259 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2260 .reg_udccs
= &UDCCS6
,
2266 .name
= "ep7out-bulk",
2267 .ops
= &pxa2xx_ep_ops
,
2268 .maxpacket
= BULK_FIFO_SIZE
,
2271 .fifo_size
= BULK_FIFO_SIZE
,
2272 .bEndpointAddress
= 7,
2273 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2274 .reg_udccs
= &UDCCS7
,
2281 .name
= "ep8in-iso",
2282 .ops
= &pxa2xx_ep_ops
,
2283 .maxpacket
= ISO_FIFO_SIZE
,
2286 .fifo_size
= ISO_FIFO_SIZE
,
2287 .bEndpointAddress
= USB_DIR_IN
| 8,
2288 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2289 .reg_udccs
= &UDCCS8
,
2295 .name
= "ep9out-iso",
2296 .ops
= &pxa2xx_ep_ops
,
2297 .maxpacket
= ISO_FIFO_SIZE
,
2300 .fifo_size
= ISO_FIFO_SIZE
,
2301 .bEndpointAddress
= 9,
2302 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2303 .reg_udccs
= &UDCCS9
,
2310 .name
= "ep10in-int",
2311 .ops
= &pxa2xx_ep_ops
,
2312 .maxpacket
= INT_FIFO_SIZE
,
2315 .fifo_size
= INT_FIFO_SIZE
,
2316 .bEndpointAddress
= USB_DIR_IN
| 10,
2317 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2318 .reg_udccs
= &UDCCS10
,
2319 .reg_uddr
= &UDDR10
,
2322 /* third group of endpoints */
2325 .name
= "ep11in-bulk",
2326 .ops
= &pxa2xx_ep_ops
,
2327 .maxpacket
= BULK_FIFO_SIZE
,
2330 .fifo_size
= BULK_FIFO_SIZE
,
2331 .bEndpointAddress
= USB_DIR_IN
| 11,
2332 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2333 .reg_udccs
= &UDCCS11
,
2334 .reg_uddr
= &UDDR11
,
2339 .name
= "ep12out-bulk",
2340 .ops
= &pxa2xx_ep_ops
,
2341 .maxpacket
= BULK_FIFO_SIZE
,
2344 .fifo_size
= BULK_FIFO_SIZE
,
2345 .bEndpointAddress
= 12,
2346 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2347 .reg_udccs
= &UDCCS12
,
2348 .reg_ubcr
= &UBCR12
,
2349 .reg_uddr
= &UDDR12
,
2354 .name
= "ep13in-iso",
2355 .ops
= &pxa2xx_ep_ops
,
2356 .maxpacket
= ISO_FIFO_SIZE
,
2359 .fifo_size
= ISO_FIFO_SIZE
,
2360 .bEndpointAddress
= USB_DIR_IN
| 13,
2361 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2362 .reg_udccs
= &UDCCS13
,
2363 .reg_uddr
= &UDDR13
,
2368 .name
= "ep14out-iso",
2369 .ops
= &pxa2xx_ep_ops
,
2370 .maxpacket
= ISO_FIFO_SIZE
,
2373 .fifo_size
= ISO_FIFO_SIZE
,
2374 .bEndpointAddress
= 14,
2375 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2376 .reg_udccs
= &UDCCS14
,
2377 .reg_ubcr
= &UBCR14
,
2378 .reg_uddr
= &UDDR14
,
2383 .name
= "ep15in-int",
2384 .ops
= &pxa2xx_ep_ops
,
2385 .maxpacket
= INT_FIFO_SIZE
,
2388 .fifo_size
= INT_FIFO_SIZE
,
2389 .bEndpointAddress
= USB_DIR_IN
| 15,
2390 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2391 .reg_udccs
= &UDCCS15
,
2392 .reg_uddr
= &UDDR15
,
2394 #endif /* !CONFIG_USB_PXA2XX_SMALL */
2397 #define CP15R0_VENDOR_MASK 0xffffe000
2399 #if defined(CONFIG_ARCH_PXA)
2400 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2402 #elif defined(CONFIG_ARCH_IXP4XX)
2403 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2407 #define CP15R0_PROD_MASK 0x000003f0
2408 #define PXA25x 0x00000100 /* and PXA26x */
2409 #define PXA210 0x00000120
2411 #define CP15R0_REV_MASK 0x0000000f
2413 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2415 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2416 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2417 #define PXA250_B2 0x00000104
2418 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2419 #define PXA250_B0 0x00000102
2420 #define PXA250_A1 0x00000101
2421 #define PXA250_A0 0x00000100
2423 #define PXA210_C0 0x00000125
2424 #define PXA210_B2 0x00000124
2425 #define PXA210_B1 0x00000123
2426 #define PXA210_B0 0x00000122
2427 #define IXP425_A0 0x000001c1
2430 * probe - binds to the platform device
2432 static int __init
pxa2xx_udc_probe(struct device
*_dev
)
2434 struct pxa2xx_udc
*dev
= &memory
;
2435 int retval
, out_dma
= 1;
2438 /* insist on Intel/ARM/XScale */
2439 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev
));
2440 if ((chiprev
& CP15R0_VENDOR_MASK
) != CP15R0_XSCALE_VALUE
) {
2441 printk(KERN_ERR
"%s: not XScale!\n", driver_name
);
2445 /* trigger chiprev-specific logic */
2446 switch (chiprev
& CP15R0_PRODREV_MASK
) {
2447 #if defined(CONFIG_ARCH_PXA)
2453 /* A0/A1 "not released"; ep 13, 15 unusable */
2455 case PXA250_B2
: case PXA210_B2
:
2456 case PXA250_B1
: case PXA210_B1
:
2457 case PXA250_B0
: case PXA210_B0
:
2460 case PXA250_C0
: case PXA210_C0
:
2462 #elif defined(CONFIG_ARCH_IXP4XX)
2469 printk(KERN_ERR
"%s: unrecognized processor: %08x\n",
2470 driver_name
, chiprev
);
2471 /* iop3xx, ixp4xx, ... */
2475 pr_debug("%s: IRQ %d%s%s%s\n", driver_name
, IRQ_USB
,
2476 dev
->has_cfr
? "" : " (!cfr)",
2477 out_dma
? "" : " (broken dma-out)",
2485 /* pxa 250 erratum 130 prevents using OUT dma (fixed C0) */
2487 DMSG("disabled OUT dma\n");
2488 dev
->ep
[ 2].reg_drcmr
= dev
->ep
[ 4].reg_drcmr
= 0;
2489 dev
->ep
[ 7].reg_drcmr
= dev
->ep
[ 9].reg_drcmr
= 0;
2490 dev
->ep
[12].reg_drcmr
= dev
->ep
[14].reg_drcmr
= 0;
2494 /* other non-static parts of init */
2496 dev
->mach
= _dev
->platform_data
;
2498 init_timer(&dev
->timer
);
2499 dev
->timer
.function
= udc_watchdog
;
2500 dev
->timer
.data
= (unsigned long) dev
;
2502 device_initialize(&dev
->gadget
.dev
);
2503 dev
->gadget
.dev
.parent
= _dev
;
2504 dev
->gadget
.dev
.dma_mask
= _dev
->dma_mask
;
2506 the_controller
= dev
;
2507 dev_set_drvdata(_dev
, dev
);
2512 dev
->vbus
= is_usb_connected();
2514 /* irq setup after old hardware state is cleaned up */
2515 retval
= request_irq(IRQ_USB
, pxa2xx_udc_irq
,
2516 SA_INTERRUPT
, driver_name
, dev
);
2518 printk(KERN_ERR
"%s: can't get irq %i, err %d\n",
2519 driver_name
, IRQ_USB
, retval
);
2524 #ifdef CONFIG_ARCH_LUBBOCK
2525 if (machine_is_lubbock()) {
2526 retval
= request_irq(LUBBOCK_USB_DISC_IRQ
,
2528 SA_INTERRUPT
| SA_SAMPLE_RANDOM
,
2531 printk(KERN_ERR
"%s: can't get irq %i, err %d\n",
2532 driver_name
, LUBBOCK_USB_DISC_IRQ
, retval
);
2534 free_irq(IRQ_USB
, dev
);
2537 retval
= request_irq(LUBBOCK_USB_IRQ
,
2539 SA_INTERRUPT
| SA_SAMPLE_RANDOM
,
2542 printk(KERN_ERR
"%s: can't get irq %i, err %d\n",
2543 driver_name
, LUBBOCK_USB_IRQ
, retval
);
2544 free_irq(LUBBOCK_USB_DISC_IRQ
, dev
);
2548 /* with U-Boot (but not BLOB), hex is off by default */
2549 HEX_DISPLAY(dev
->stats
.irqs
);
2550 LUB_DISC_BLNK_LED
&= 0xff;
2554 create_proc_files();
2558 static int __exit
pxa2xx_udc_remove(struct device
*_dev
)
2560 struct pxa2xx_udc
*dev
= dev_get_drvdata(_dev
);
2563 remove_proc_files();
2564 usb_gadget_unregister_driver(dev
->driver
);
2567 free_irq(IRQ_USB
, dev
);
2570 if (machine_is_lubbock()) {
2571 free_irq(LUBBOCK_USB_DISC_IRQ
, dev
);
2572 free_irq(LUBBOCK_USB_IRQ
, dev
);
2574 dev_set_drvdata(_dev
, NULL
);
2575 the_controller
= NULL
;
2579 /*-------------------------------------------------------------------------*/
2583 /* USB suspend (controlled by the host) and system suspend (controlled
2584 * by the PXA) don't necessarily work well together. If USB is active,
2585 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2586 * mode, or any deeper PM saving state.
2588 * For now, we punt and forcibly disconnect from the USB host when PXA
2589 * enters any suspend state. While we're disconnected, we always disable
2590 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2591 * Boards without software pullup control shouldn't use those states.
2592 * VBUS IRQs should probably be ignored so that the PXA device just acts
2593 * "dead" to USB hosts until system resume.
2595 static int pxa2xx_udc_suspend(struct device
*dev
, u32 state
, u32 level
)
2597 struct pxa2xx_udc
*udc
= dev_get_drvdata(dev
);
2599 if (level
== SUSPEND_POWER_DOWN
) {
2600 if (!udc
->mach
->udc_command
)
2601 WARN("USB host won't detect disconnect!\n");
2607 static int pxa2xx_udc_resume(struct device
*dev
, u32 level
)
2609 struct pxa2xx_udc
*udc
= dev_get_drvdata(dev
);
2611 if (level
== RESUME_POWER_ON
)
2617 #define pxa2xx_udc_suspend NULL
2618 #define pxa2xx_udc_resume NULL
2621 /*-------------------------------------------------------------------------*/
2623 static struct device_driver udc_driver
= {
2624 .name
= "pxa2xx-udc",
2625 .bus
= &platform_bus_type
,
2626 .probe
= pxa2xx_udc_probe
,
2627 .remove
= __exit_p(pxa2xx_udc_remove
),
2628 .suspend
= pxa2xx_udc_suspend
,
2629 .resume
= pxa2xx_udc_resume
,
2632 static int __init
udc_init(void)
2634 printk(KERN_INFO
"%s: version %s\n", driver_name
, DRIVER_VERSION
);
2635 return driver_register(&udc_driver
);
2637 module_init(udc_init
);
2639 static void __exit
udc_exit(void)
2641 driver_unregister(&udc_driver
);
2643 module_exit(udc_exit
);
2645 MODULE_DESCRIPTION(DRIVER_DESC
);
2646 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2647 MODULE_LICENSE("GPL");