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.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* #define VERBOSE_DEBUG */
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/ioport.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <linux/init.h>
37 #include <linux/timer.h>
38 #include <linux/list.h>
39 #include <linux/interrupt.h>
41 #include <linux/platform_device.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/irq.h>
44 #include <linux/clk.h>
45 #include <linux/err.h>
46 #include <linux/seq_file.h>
47 #include <linux/debugfs.h>
49 #include <linux/prefetch.h>
51 #include <asm/byteorder.h>
54 #include <asm/system.h>
55 #include <asm/mach-types.h>
56 #include <asm/unaligned.h>
58 #include <linux/usb/ch9.h>
59 #include <linux/usb/gadget.h>
60 #include <linux/usb/otg.h>
63 * This driver is PXA25x only. Grab the right register definitions.
65 #ifdef CONFIG_ARCH_PXA
66 #include <mach/pxa25x-udc.h>
69 #ifdef CONFIG_ARCH_LUBBOCK
70 #include <mach/lubbock.h>
73 #include <asm/mach/udc_pxa2xx.h>
77 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
78 * series processors. The UDC for the IXP 4xx series is very similar.
79 * There are fifteen endpoints, in addition to ep0.
81 * Such controller drivers work with a gadget driver. The gadget driver
82 * returns descriptors, implements configuration and data protocols used
83 * by the host to interact with this device, and allocates endpoints to
84 * the different protocol interfaces. The controller driver virtualizes
85 * usb hardware so that the gadget drivers will be more portable.
87 * This UDC hardware wants to implement a bit too much USB protocol, so
88 * it constrains the sorts of USB configuration change events that work.
89 * The errata for these chips are misleading; some "fixed" bugs from
90 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
92 * Note that the UDC hardware supports DMA (except on IXP) but that's
93 * not used here. IN-DMA (to host) is simple enough, when the data is
94 * suitably aligned (16 bytes) ... the network stack doesn't do that,
95 * other software can. OUT-DMA is buggy in most chip versions, as well
96 * as poorly designed (data toggle not automatic). So this driver won't
97 * bother using DMA. (Mostly-working IN-DMA support was available in
98 * kernels before 2.6.23, but was never enabled or well tested.)
101 #define DRIVER_VERSION "30-June-2007"
102 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
105 static const char driver_name
[] = "pxa25x_udc";
107 static const char ep0name
[] = "ep0";
110 #ifdef CONFIG_ARCH_IXP4XX
112 /* cpu-specific register addresses are compiled in to this code */
113 #ifdef CONFIG_ARCH_PXA
114 #error "Can't configure both IXP and PXA"
117 /* IXP doesn't yet support <linux/clk.h> */
118 #define clk_get(dev,name) NULL
119 #define clk_enable(clk) do { } while (0)
120 #define clk_disable(clk) do { } while (0)
121 #define clk_put(clk) do { } while (0)
125 #include "pxa25x_udc.h"
128 #ifdef CONFIG_USB_PXA25X_SMALL
129 #define SIZE_STR " (small)"
134 /* ---------------------------------------------------------------------------
135 * endpoint related parts of the api to the usb controller hardware,
136 * used by gadget driver; and the inner talker-to-hardware core.
137 * ---------------------------------------------------------------------------
140 static void pxa25x_ep_fifo_flush (struct usb_ep
*ep
);
141 static void nuke (struct pxa25x_ep
*, int status
);
143 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
144 static void pullup_off(void)
146 struct pxa2xx_udc_mach_info
*mach
= the_controller
->mach
;
147 int off_level
= mach
->gpio_pullup_inverted
;
149 if (gpio_is_valid(mach
->gpio_pullup
))
150 gpio_set_value(mach
->gpio_pullup
, off_level
);
151 else if (mach
->udc_command
)
152 mach
->udc_command(PXA2XX_UDC_CMD_DISCONNECT
);
155 static void pullup_on(void)
157 struct pxa2xx_udc_mach_info
*mach
= the_controller
->mach
;
158 int on_level
= !mach
->gpio_pullup_inverted
;
160 if (gpio_is_valid(mach
->gpio_pullup
))
161 gpio_set_value(mach
->gpio_pullup
, on_level
);
162 else if (mach
->udc_command
)
163 mach
->udc_command(PXA2XX_UDC_CMD_CONNECT
);
166 static void pio_irq_enable(int bEndpointAddress
)
168 bEndpointAddress
&= 0xf;
169 if (bEndpointAddress
< 8)
170 UICR0
&= ~(1 << bEndpointAddress
);
172 bEndpointAddress
-= 8;
173 UICR1
&= ~(1 << bEndpointAddress
);
177 static void pio_irq_disable(int bEndpointAddress
)
179 bEndpointAddress
&= 0xf;
180 if (bEndpointAddress
< 8)
181 UICR0
|= 1 << bEndpointAddress
;
183 bEndpointAddress
-= 8;
184 UICR1
|= 1 << bEndpointAddress
;
188 /* The UDCCR reg contains mask and interrupt status bits,
189 * so using '|=' isn't safe as it may ack an interrupt.
191 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
193 static inline void udc_set_mask_UDCCR(int mask
)
195 UDCCR
= (UDCCR
& UDCCR_MASK_BITS
) | (mask
& UDCCR_MASK_BITS
);
198 static inline void udc_clear_mask_UDCCR(int mask
)
200 UDCCR
= (UDCCR
& UDCCR_MASK_BITS
) & ~(mask
& UDCCR_MASK_BITS
);
203 static inline void udc_ack_int_UDCCR(int mask
)
205 /* udccr contains the bits we dont want to change */
206 __u32 udccr
= UDCCR
& UDCCR_MASK_BITS
;
208 UDCCR
= udccr
| (mask
& ~UDCCR_MASK_BITS
);
212 * endpoint enable/disable
214 * we need to verify the descriptors used to enable endpoints. since pxa25x
215 * endpoint configurations are fixed, and are pretty much always enabled,
216 * there's not a lot to manage here.
218 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
219 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
220 * for a single interface (with only the default altsetting) and for gadget
221 * drivers that don't halt endpoints (not reset by set_interface). that also
222 * means that if you use ISO, you must violate the USB spec rule that all
223 * iso endpoints must be in non-default altsettings.
225 static int pxa25x_ep_enable (struct usb_ep
*_ep
,
226 const struct usb_endpoint_descriptor
*desc
)
228 struct pxa25x_ep
*ep
;
229 struct pxa25x_udc
*dev
;
231 ep
= container_of (_ep
, struct pxa25x_ep
, ep
);
232 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep0name
233 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
234 || ep
->bEndpointAddress
!= desc
->bEndpointAddress
235 || ep
->fifo_size
< usb_endpoint_maxp (desc
)) {
236 DMSG("%s, bad ep or descriptor\n", __func__
);
240 /* xfer types must match, except that interrupt ~= bulk */
241 if (ep
->bmAttributes
!= desc
->bmAttributes
242 && ep
->bmAttributes
!= USB_ENDPOINT_XFER_BULK
243 && desc
->bmAttributes
!= USB_ENDPOINT_XFER_INT
) {
244 DMSG("%s, %s type mismatch\n", __func__
, _ep
->name
);
248 /* hardware _could_ do smaller, but driver doesn't */
249 if ((desc
->bmAttributes
== USB_ENDPOINT_XFER_BULK
250 && usb_endpoint_maxp (desc
)
252 || !desc
->wMaxPacketSize
) {
253 DMSG("%s, bad %s maxpacket\n", __func__
, _ep
->name
);
258 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
259 DMSG("%s, bogus device state\n", __func__
);
266 ep
->ep
.maxpacket
= usb_endpoint_maxp (desc
);
268 /* flush fifo (mostly for OUT buffers) */
269 pxa25x_ep_fifo_flush (_ep
);
271 /* ... reset halt state too, if we could ... */
273 DBG(DBG_VERBOSE
, "enabled %s\n", _ep
->name
);
277 static int pxa25x_ep_disable (struct usb_ep
*_ep
)
279 struct pxa25x_ep
*ep
;
282 ep
= container_of (_ep
, struct pxa25x_ep
, ep
);
283 if (!_ep
|| !ep
->desc
) {
284 DMSG("%s, %s not enabled\n", __func__
,
285 _ep
? ep
->ep
.name
: NULL
);
288 local_irq_save(flags
);
290 nuke (ep
, -ESHUTDOWN
);
292 /* flush fifo (mostly for IN buffers) */
293 pxa25x_ep_fifo_flush (_ep
);
298 local_irq_restore(flags
);
299 DBG(DBG_VERBOSE
, "%s disabled\n", _ep
->name
);
303 /*-------------------------------------------------------------------------*/
305 /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
306 * must still pass correctly initialized endpoints, since other controller
307 * drivers may care about how it's currently set up (dma issues etc).
311 * pxa25x_ep_alloc_request - allocate a request data structure
313 static struct usb_request
*
314 pxa25x_ep_alloc_request (struct usb_ep
*_ep
, gfp_t gfp_flags
)
316 struct pxa25x_request
*req
;
318 req
= kzalloc(sizeof(*req
), gfp_flags
);
322 INIT_LIST_HEAD (&req
->queue
);
328 * pxa25x_ep_free_request - deallocate a request data structure
331 pxa25x_ep_free_request (struct usb_ep
*_ep
, struct usb_request
*_req
)
333 struct pxa25x_request
*req
;
335 req
= container_of (_req
, struct pxa25x_request
, req
);
336 WARN_ON(!list_empty (&req
->queue
));
340 /*-------------------------------------------------------------------------*/
343 * done - retire a request; caller blocked irqs
345 static void done(struct pxa25x_ep
*ep
, struct pxa25x_request
*req
, int status
)
347 unsigned stopped
= ep
->stopped
;
349 list_del_init(&req
->queue
);
351 if (likely (req
->req
.status
== -EINPROGRESS
))
352 req
->req
.status
= status
;
354 status
= req
->req
.status
;
356 if (status
&& status
!= -ESHUTDOWN
)
357 DBG(DBG_VERBOSE
, "complete %s req %p stat %d len %u/%u\n",
358 ep
->ep
.name
, &req
->req
, status
,
359 req
->req
.actual
, req
->req
.length
);
361 /* don't modify queue heads during completion callback */
363 req
->req
.complete(&ep
->ep
, &req
->req
);
364 ep
->stopped
= stopped
;
368 static inline void ep0_idle (struct pxa25x_udc
*dev
)
370 dev
->ep0state
= EP0_IDLE
;
374 write_packet(volatile u32
*uddr
, struct pxa25x_request
*req
, unsigned max
)
377 unsigned length
, count
;
379 buf
= req
->req
.buf
+ req
->req
.actual
;
382 /* how big will this packet be? */
383 length
= min(req
->req
.length
- req
->req
.actual
, max
);
384 req
->req
.actual
+= length
;
387 while (likely(count
--))
394 * write to an IN endpoint fifo, as many packets as possible.
395 * irqs will use this to write the rest later.
396 * caller guarantees at least one packet buffer is ready (or a zlp).
399 write_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
403 max
= usb_endpoint_maxp(ep
->desc
);
406 int is_last
, is_short
;
408 count
= write_packet(ep
->reg_uddr
, req
, max
);
410 /* last packet is usually short (or a zlp) */
411 if (unlikely (count
!= max
))
412 is_last
= is_short
= 1;
414 if (likely(req
->req
.length
!= req
->req
.actual
)
419 /* interrupt/iso maxpacket may not fill the fifo */
420 is_short
= unlikely (max
< ep
->fifo_size
);
423 DBG(DBG_VERY_NOISY
, "wrote %s %d bytes%s%s %d left %p\n",
425 is_last
? "/L" : "", is_short
? "/S" : "",
426 req
->req
.length
- req
->req
.actual
, req
);
428 /* let loose that packet. maybe try writing another one,
429 * double buffering might work. TSP, TPC, and TFS
430 * bit values are the same for all normal IN endpoints.
432 *ep
->reg_udccs
= UDCCS_BI_TPC
;
434 *ep
->reg_udccs
= UDCCS_BI_TSP
;
436 /* requests complete when all IN data is in the FIFO */
439 if (list_empty(&ep
->queue
))
440 pio_irq_disable (ep
->bEndpointAddress
);
444 // TODO experiment: how robust can fifo mode tweaking be?
445 // double buffering is off in the default fifo mode, which
446 // prevents TFS from being set here.
448 } while (*ep
->reg_udccs
& UDCCS_BI_TFS
);
452 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
453 * ep0 data stage. these chips want very simple state transitions.
456 void ep0start(struct pxa25x_udc
*dev
, u32 flags
, const char *tag
)
458 UDCCS0
= flags
|UDCCS0_SA
|UDCCS0_OPR
;
460 dev
->req_pending
= 0;
461 DBG(DBG_VERY_NOISY
, "%s %s, %02x/%02x\n",
462 __func__
, tag
, UDCCS0
, flags
);
466 write_ep0_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
471 count
= write_packet(&UDDR0
, req
, EP0_FIFO_SIZE
);
472 ep
->dev
->stats
.write
.bytes
+= count
;
474 /* last packet "must be" short (or a zlp) */
475 is_short
= (count
!= EP0_FIFO_SIZE
);
477 DBG(DBG_VERY_NOISY
, "ep0in %d bytes %d left %p\n", count
,
478 req
->req
.length
- req
->req
.actual
, req
);
480 if (unlikely (is_short
)) {
481 if (ep
->dev
->req_pending
)
482 ep0start(ep
->dev
, UDCCS0_IPR
, "short IN");
486 count
= req
->req
.length
;
489 #ifndef CONFIG_ARCH_IXP4XX
491 /* This seems to get rid of lost status irqs in some cases:
492 * host responds quickly, or next request involves config
493 * change automagic, or should have been hidden, or ...
495 * FIXME get rid of all udelays possible...
497 if (count
>= EP0_FIFO_SIZE
) {
500 if ((UDCCS0
& UDCCS0_OPR
) != 0) {
501 /* clear OPR, generate ack */
511 } else if (ep
->dev
->req_pending
)
512 ep0start(ep
->dev
, 0, "IN");
518 * read_fifo - unload packet(s) from the fifo we use for usb OUT
519 * transfers and put them into the request. caller should have made
520 * sure there's at least one packet ready.
522 * returns true if the request completed because of short packet or the
523 * request buffer having filled (and maybe overran till end-of-packet).
526 read_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
531 unsigned bufferspace
, count
, is_short
;
533 /* make sure there's a packet in the FIFO.
534 * UDCCS_{BO,IO}_RPC are all the same bit value.
535 * UDCCS_{BO,IO}_RNE are all the same bit value.
537 udccs
= *ep
->reg_udccs
;
538 if (unlikely ((udccs
& UDCCS_BO_RPC
) == 0))
540 buf
= req
->req
.buf
+ req
->req
.actual
;
542 bufferspace
= req
->req
.length
- req
->req
.actual
;
544 /* read all bytes from this packet */
545 if (likely (udccs
& UDCCS_BO_RNE
)) {
546 count
= 1 + (0x0ff & *ep
->reg_ubcr
);
547 req
->req
.actual
+= min (count
, bufferspace
);
550 is_short
= (count
< ep
->ep
.maxpacket
);
551 DBG(DBG_VERY_NOISY
, "read %s %02x, %d bytes%s req %p %d/%d\n",
552 ep
->ep
.name
, udccs
, count
,
553 is_short
? "/S" : "",
554 req
, req
->req
.actual
, req
->req
.length
);
555 while (likely (count
-- != 0)) {
556 u8 byte
= (u8
) *ep
->reg_uddr
;
558 if (unlikely (bufferspace
== 0)) {
559 /* this happens when the driver's buffer
560 * is smaller than what the host sent.
561 * discard the extra data.
563 if (req
->req
.status
!= -EOVERFLOW
)
564 DMSG("%s overflow %d\n",
566 req
->req
.status
= -EOVERFLOW
;
572 *ep
->reg_udccs
= UDCCS_BO_RPC
;
573 /* RPC/RSP/RNE could now reflect the other packet buffer */
575 /* iso is one request per packet */
576 if (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
577 if (udccs
& UDCCS_IO_ROF
)
578 req
->req
.status
= -EHOSTUNREACH
;
579 /* more like "is_done" */
584 if (is_short
|| req
->req
.actual
== req
->req
.length
) {
586 if (list_empty(&ep
->queue
))
587 pio_irq_disable (ep
->bEndpointAddress
);
591 /* finished that packet. the next one may be waiting... */
597 * special ep0 version of the above. no UBCR0 or double buffering; status
598 * handshaking is magic. most device protocols don't need control-OUT.
599 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
600 * protocols do use them.
603 read_ep0_fifo (struct pxa25x_ep
*ep
, struct pxa25x_request
*req
)
606 unsigned bufferspace
;
608 buf
= req
->req
.buf
+ req
->req
.actual
;
609 bufferspace
= req
->req
.length
- req
->req
.actual
;
611 while (UDCCS0
& UDCCS0_RNE
) {
614 if (unlikely (bufferspace
== 0)) {
615 /* this happens when the driver's buffer
616 * is smaller than what the host sent.
617 * discard the extra data.
619 if (req
->req
.status
!= -EOVERFLOW
)
620 DMSG("%s overflow\n", ep
->ep
.name
);
621 req
->req
.status
= -EOVERFLOW
;
629 UDCCS0
= UDCCS0_OPR
| UDCCS0_IPR
;
632 if (req
->req
.actual
>= req
->req
.length
)
635 /* finished that packet. the next one may be waiting... */
639 /*-------------------------------------------------------------------------*/
642 pxa25x_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
, gfp_t gfp_flags
)
644 struct pxa25x_request
*req
;
645 struct pxa25x_ep
*ep
;
646 struct pxa25x_udc
*dev
;
649 req
= container_of(_req
, struct pxa25x_request
, req
);
650 if (unlikely (!_req
|| !_req
->complete
|| !_req
->buf
651 || !list_empty(&req
->queue
))) {
652 DMSG("%s, bad params\n", __func__
);
656 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
657 if (unlikely (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
))) {
658 DMSG("%s, bad ep\n", __func__
);
663 if (unlikely (!dev
->driver
664 || dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)) {
665 DMSG("%s, bogus device state\n", __func__
);
669 /* iso is always one packet per request, that's the only way
670 * we can report per-packet status. that also helps with dma.
672 if (unlikely (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
673 && req
->req
.length
> usb_endpoint_maxp (ep
->desc
)))
676 DBG(DBG_NOISY
, "%s queue req %p, len %d buf %p\n",
677 _ep
->name
, _req
, _req
->length
, _req
->buf
);
679 local_irq_save(flags
);
681 _req
->status
= -EINPROGRESS
;
684 /* kickstart this i/o queue? */
685 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
686 if (ep
->desc
== NULL
/* ep0 */) {
687 unsigned length
= _req
->length
;
689 switch (dev
->ep0state
) {
690 case EP0_IN_DATA_PHASE
:
691 dev
->stats
.write
.ops
++;
692 if (write_ep0_fifo(ep
, req
))
696 case EP0_OUT_DATA_PHASE
:
697 dev
->stats
.read
.ops
++;
699 if (dev
->req_config
) {
700 DBG(DBG_VERBOSE
, "ep0 config ack%s\n",
701 dev
->has_cfr
? "" : " raced");
703 UDCCFR
= UDCCFR_AREN
|UDCCFR_ACM
706 dev
->ep0state
= EP0_END_XFER
;
707 local_irq_restore (flags
);
710 if (dev
->req_pending
)
711 ep0start(dev
, UDCCS0_IPR
, "OUT");
712 if (length
== 0 || ((UDCCS0
& UDCCS0_RNE
) != 0
713 && read_ep0_fifo(ep
, req
))) {
721 DMSG("ep0 i/o, odd state %d\n", dev
->ep0state
);
722 local_irq_restore (flags
);
725 /* can the FIFO can satisfy the request immediately? */
726 } else if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0) {
727 if ((*ep
->reg_udccs
& UDCCS_BI_TFS
) != 0
728 && write_fifo(ep
, req
))
730 } else if ((*ep
->reg_udccs
& UDCCS_BO_RFS
) != 0
731 && read_fifo(ep
, req
)) {
735 if (likely (req
&& ep
->desc
))
736 pio_irq_enable(ep
->bEndpointAddress
);
739 /* pio or dma irq handler advances the queue. */
740 if (likely(req
!= NULL
))
741 list_add_tail(&req
->queue
, &ep
->queue
);
742 local_irq_restore(flags
);
749 * nuke - dequeue ALL requests
751 static void nuke(struct pxa25x_ep
*ep
, int status
)
753 struct pxa25x_request
*req
;
755 /* called with irqs blocked */
756 while (!list_empty(&ep
->queue
)) {
757 req
= list_entry(ep
->queue
.next
,
758 struct pxa25x_request
,
760 done(ep
, req
, status
);
763 pio_irq_disable (ep
->bEndpointAddress
);
767 /* dequeue JUST ONE request */
768 static int pxa25x_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
770 struct pxa25x_ep
*ep
;
771 struct pxa25x_request
*req
;
774 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
775 if (!_ep
|| ep
->ep
.name
== ep0name
)
778 local_irq_save(flags
);
780 /* make sure it's actually queued on this endpoint */
781 list_for_each_entry (req
, &ep
->queue
, queue
) {
782 if (&req
->req
== _req
)
785 if (&req
->req
!= _req
) {
786 local_irq_restore(flags
);
790 done(ep
, req
, -ECONNRESET
);
792 local_irq_restore(flags
);
796 /*-------------------------------------------------------------------------*/
798 static int pxa25x_ep_set_halt(struct usb_ep
*_ep
, int value
)
800 struct pxa25x_ep
*ep
;
803 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
805 || (!ep
->desc
&& ep
->ep
.name
!= ep0name
))
806 || ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
) {
807 DMSG("%s, bad ep\n", __func__
);
811 /* this path (reset toggle+halt) is needed to implement
812 * SET_INTERFACE on normal hardware. but it can't be
813 * done from software on the PXA UDC, and the hardware
814 * forgets to do it as part of SET_INTERFACE automagic.
816 DMSG("only host can clear %s halt\n", _ep
->name
);
820 local_irq_save(flags
);
822 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0
823 && ((*ep
->reg_udccs
& UDCCS_BI_TFS
) == 0
824 || !list_empty(&ep
->queue
))) {
825 local_irq_restore(flags
);
829 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
830 *ep
->reg_udccs
= UDCCS_BI_FST
|UDCCS_BI_FTF
;
832 /* ep0 needs special care */
834 start_watchdog(ep
->dev
);
835 ep
->dev
->req_pending
= 0;
836 ep
->dev
->ep0state
= EP0_STALL
;
838 /* and bulk/intr endpoints like dropping stalls too */
841 for (i
= 0; i
< 1000; i
+= 20) {
842 if (*ep
->reg_udccs
& UDCCS_BI_SST
)
847 local_irq_restore(flags
);
849 DBG(DBG_VERBOSE
, "%s halt\n", _ep
->name
);
853 static int pxa25x_ep_fifo_status(struct usb_ep
*_ep
)
855 struct pxa25x_ep
*ep
;
857 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
859 DMSG("%s, bad ep\n", __func__
);
862 /* pxa can't report unclaimed bytes from IN fifos */
863 if ((ep
->bEndpointAddress
& USB_DIR_IN
) != 0)
865 if (ep
->dev
->gadget
.speed
== USB_SPEED_UNKNOWN
866 || (*ep
->reg_udccs
& UDCCS_BO_RFS
) == 0)
869 return (*ep
->reg_ubcr
& 0xfff) + 1;
872 static void pxa25x_ep_fifo_flush(struct usb_ep
*_ep
)
874 struct pxa25x_ep
*ep
;
876 ep
= container_of(_ep
, struct pxa25x_ep
, ep
);
877 if (!_ep
|| ep
->ep
.name
== ep0name
|| !list_empty(&ep
->queue
)) {
878 DMSG("%s, bad ep\n", __func__
);
882 /* toggle and halt bits stay unchanged */
884 /* for OUT, just read and discard the FIFO contents. */
885 if ((ep
->bEndpointAddress
& USB_DIR_IN
) == 0) {
886 while (((*ep
->reg_udccs
) & UDCCS_BO_RNE
) != 0)
887 (void) *ep
->reg_uddr
;
891 /* most IN status is the same, but ISO can't stall */
892 *ep
->reg_udccs
= UDCCS_BI_TPC
|UDCCS_BI_FTF
|UDCCS_BI_TUR
893 | (ep
->bmAttributes
== USB_ENDPOINT_XFER_ISOC
898 static struct usb_ep_ops pxa25x_ep_ops
= {
899 .enable
= pxa25x_ep_enable
,
900 .disable
= pxa25x_ep_disable
,
902 .alloc_request
= pxa25x_ep_alloc_request
,
903 .free_request
= pxa25x_ep_free_request
,
905 .queue
= pxa25x_ep_queue
,
906 .dequeue
= pxa25x_ep_dequeue
,
908 .set_halt
= pxa25x_ep_set_halt
,
909 .fifo_status
= pxa25x_ep_fifo_status
,
910 .fifo_flush
= pxa25x_ep_fifo_flush
,
914 /* ---------------------------------------------------------------------------
915 * device-scoped parts of the api to the usb controller hardware
916 * ---------------------------------------------------------------------------
919 static int pxa25x_udc_get_frame(struct usb_gadget
*_gadget
)
921 return ((UFNRH
& 0x07) << 8) | (UFNRL
& 0xff);
924 static int pxa25x_udc_wakeup(struct usb_gadget
*_gadget
)
926 /* host may not have enabled remote wakeup */
927 if ((UDCCS0
& UDCCS0_DRWF
) == 0)
928 return -EHOSTUNREACH
;
929 udc_set_mask_UDCCR(UDCCR_RSM
);
933 static void stop_activity(struct pxa25x_udc
*, struct usb_gadget_driver
*);
934 static void udc_enable (struct pxa25x_udc
*);
935 static void udc_disable(struct pxa25x_udc
*);
937 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
940 static int pullup(struct pxa25x_udc
*udc
)
942 int is_active
= udc
->vbus
&& udc
->pullup
&& !udc
->suspended
;
943 DMSG("%s\n", is_active
? "active" : "inactive");
947 /* Enable clock for USB device */
948 clk_enable(udc
->clk
);
953 if (udc
->gadget
.speed
!= USB_SPEED_UNKNOWN
) {
954 DMSG("disconnect %s\n", udc
->driver
955 ? udc
->driver
->driver
.name
957 stop_activity(udc
, udc
->driver
);
960 /* Disable clock for USB device */
961 clk_disable(udc
->clk
);
969 /* VBUS reporting logically comes from a transceiver */
970 static int pxa25x_udc_vbus_session(struct usb_gadget
*_gadget
, int is_active
)
972 struct pxa25x_udc
*udc
;
974 udc
= container_of(_gadget
, struct pxa25x_udc
, gadget
);
975 udc
->vbus
= is_active
;
976 DMSG("vbus %s\n", is_active
? "supplied" : "inactive");
981 /* drivers may have software control over D+ pullup */
982 static int pxa25x_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
984 struct pxa25x_udc
*udc
;
986 udc
= container_of(_gadget
, struct pxa25x_udc
, gadget
);
988 /* not all boards support pullup control */
989 if (!gpio_is_valid(udc
->mach
->gpio_pullup
) && !udc
->mach
->udc_command
)
992 udc
->pullup
= (is_active
!= 0);
997 /* boards may consume current from VBUS, up to 100-500mA based on config.
998 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
1001 static int pxa25x_udc_vbus_draw(struct usb_gadget
*_gadget
, unsigned mA
)
1003 struct pxa25x_udc
*udc
;
1005 udc
= container_of(_gadget
, struct pxa25x_udc
, gadget
);
1007 if (udc
->transceiver
)
1008 return otg_set_power(udc
->transceiver
, mA
);
1012 static int pxa25x_start(struct usb_gadget_driver
*driver
,
1013 int (*bind
)(struct usb_gadget
*));
1014 static int pxa25x_stop(struct usb_gadget_driver
*driver
);
1016 static const struct usb_gadget_ops pxa25x_udc_ops
= {
1017 .get_frame
= pxa25x_udc_get_frame
,
1018 .wakeup
= pxa25x_udc_wakeup
,
1019 .vbus_session
= pxa25x_udc_vbus_session
,
1020 .pullup
= pxa25x_udc_pullup
,
1021 .vbus_draw
= pxa25x_udc_vbus_draw
,
1022 .start
= pxa25x_start
,
1023 .stop
= pxa25x_stop
,
1026 /*-------------------------------------------------------------------------*/
1028 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1031 udc_seq_show(struct seq_file
*m
, void *_d
)
1033 struct pxa25x_udc
*dev
= m
->private;
1034 unsigned long flags
;
1038 local_irq_save(flags
);
1040 /* basic device status */
1041 seq_printf(m
, DRIVER_DESC
"\n"
1042 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1043 driver_name
, DRIVER_VERSION SIZE_STR
"(pio)",
1044 dev
->driver
? dev
->driver
->driver
.name
: "(none)",
1045 dev
->gadget
.speed
== USB_SPEED_FULL
? "full speed" : "disconnected");
1047 /* registers for device and ep0 */
1049 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1050 UICR1
, UICR0
, USIR1
, USIR0
, UFNRH
, UFNRL
);
1054 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp
,
1055 (tmp
& UDCCR_REM
) ? " rem" : "",
1056 (tmp
& UDCCR_RSTIR
) ? " rstir" : "",
1057 (tmp
& UDCCR_SRM
) ? " srm" : "",
1058 (tmp
& UDCCR_SUSIR
) ? " susir" : "",
1059 (tmp
& UDCCR_RESIR
) ? " resir" : "",
1060 (tmp
& UDCCR_RSM
) ? " rsm" : "",
1061 (tmp
& UDCCR_UDA
) ? " uda" : "",
1062 (tmp
& UDCCR_UDE
) ? " ude" : "");
1066 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp
,
1067 (tmp
& UDCCS0_SA
) ? " sa" : "",
1068 (tmp
& UDCCS0_RNE
) ? " rne" : "",
1069 (tmp
& UDCCS0_FST
) ? " fst" : "",
1070 (tmp
& UDCCS0_SST
) ? " sst" : "",
1071 (tmp
& UDCCS0_DRWF
) ? " dwrf" : "",
1072 (tmp
& UDCCS0_FTF
) ? " ftf" : "",
1073 (tmp
& UDCCS0_IPR
) ? " ipr" : "",
1074 (tmp
& UDCCS0_OPR
) ? " opr" : "");
1079 "udccfr %02X =%s%s\n", tmp
,
1080 (tmp
& UDCCFR_AREN
) ? " aren" : "",
1081 (tmp
& UDCCFR_ACM
) ? " acm" : "");
1084 if (dev
->gadget
.speed
!= USB_SPEED_FULL
|| !dev
->driver
)
1087 seq_printf(m
, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1088 dev
->stats
.write
.bytes
, dev
->stats
.write
.ops
,
1089 dev
->stats
.read
.bytes
, dev
->stats
.read
.ops
,
1092 /* dump endpoint queues */
1093 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1094 struct pxa25x_ep
*ep
= &dev
->ep
[i
];
1095 struct pxa25x_request
*req
;
1098 const struct usb_endpoint_descriptor
*desc
;
1103 tmp
= *dev
->ep
[i
].reg_udccs
;
1105 "%s max %d %s udccs %02x irqs %lu\n",
1106 ep
->ep
.name
, usb_endpoint_maxp(desc
),
1107 "pio", tmp
, ep
->pio_irqs
);
1108 /* TODO translate all five groups of udccs bits! */
1110 } else /* ep0 should only have one transfer queued */
1111 seq_printf(m
, "ep0 max 16 pio irqs %lu\n",
1114 if (list_empty(&ep
->queue
)) {
1115 seq_printf(m
, "\t(nothing queued)\n");
1118 list_for_each_entry(req
, &ep
->queue
, queue
) {
1120 "\treq %p len %d/%d buf %p\n",
1121 &req
->req
, req
->req
.actual
,
1122 req
->req
.length
, req
->req
.buf
);
1127 local_irq_restore(flags
);
1132 udc_debugfs_open(struct inode
*inode
, struct file
*file
)
1134 return single_open(file
, udc_seq_show
, inode
->i_private
);
1137 static const struct file_operations debug_fops
= {
1138 .open
= udc_debugfs_open
,
1140 .llseek
= seq_lseek
,
1141 .release
= single_release
,
1142 .owner
= THIS_MODULE
,
1145 #define create_debug_files(dev) \
1147 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1148 S_IRUGO, NULL, dev, &debug_fops); \
1150 #define remove_debug_files(dev) \
1152 if (dev->debugfs_udc) \
1153 debugfs_remove(dev->debugfs_udc); \
1156 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1158 #define create_debug_files(dev) do {} while (0)
1159 #define remove_debug_files(dev) do {} while (0)
1161 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1163 /*-------------------------------------------------------------------------*/
1166 * udc_disable - disable USB device controller
1168 static void udc_disable(struct pxa25x_udc
*dev
)
1170 /* block all irqs */
1171 udc_set_mask_UDCCR(UDCCR_SRM
|UDCCR_REM
);
1172 UICR0
= UICR1
= 0xff;
1175 /* if hardware supports it, disconnect from usb */
1178 udc_clear_mask_UDCCR(UDCCR_UDE
);
1181 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1186 * udc_reinit - initialize software state
1188 static void udc_reinit(struct pxa25x_udc
*dev
)
1192 /* device/ep0 records init */
1193 INIT_LIST_HEAD (&dev
->gadget
.ep_list
);
1194 INIT_LIST_HEAD (&dev
->gadget
.ep0
->ep_list
);
1195 dev
->ep0state
= EP0_IDLE
;
1197 /* basic endpoint records init */
1198 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1199 struct pxa25x_ep
*ep
= &dev
->ep
[i
];
1202 list_add_tail (&ep
->ep
.ep_list
, &dev
->gadget
.ep_list
);
1206 INIT_LIST_HEAD (&ep
->queue
);
1210 /* the rest was statically initialized, and is read-only */
1213 /* until it's enabled, this UDC should be completely invisible
1216 static void udc_enable (struct pxa25x_udc
*dev
)
1218 udc_clear_mask_UDCCR(UDCCR_UDE
);
1220 /* try to clear these bits before we enable the udc */
1221 udc_ack_int_UDCCR(UDCCR_SUSIR
|/*UDCCR_RSTIR|*/UDCCR_RESIR
);
1224 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1225 dev
->stats
.irqs
= 0;
1228 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1230 * - if RESET is already in progress, ack interrupt
1231 * - unmask reset interrupt
1233 udc_set_mask_UDCCR(UDCCR_UDE
);
1234 if (!(UDCCR
& UDCCR_UDA
))
1235 udc_ack_int_UDCCR(UDCCR_RSTIR
);
1237 if (dev
->has_cfr
/* UDC_RES2 is defined */) {
1238 /* pxa255 (a0+) can avoid a set_config race that could
1239 * prevent gadget drivers from configuring correctly
1241 UDCCFR
= UDCCFR_ACM
| UDCCFR_MB1
;
1243 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1244 * which could result in missing packets and interrupts.
1245 * supposedly one bit per endpoint, controlling whether it
1246 * double buffers or not; ACM/AREN bits fit into the holes.
1247 * zero bits (like USIR0_IRx) disable double buffering.
1253 /* enable suspend/resume and reset irqs */
1254 udc_clear_mask_UDCCR(UDCCR_SRM
| UDCCR_REM
);
1256 /* enable ep0 irqs */
1257 UICR0
&= ~UICR0_IM0
;
1259 /* if hardware supports it, pullup D+ and wait for reset */
1264 /* when a driver is successfully registered, it will receive
1265 * control requests including set_configuration(), which enables
1266 * non-control requests. then usb traffic follows until a
1267 * disconnect is reported. then a host may connect again, or
1268 * the driver might get unbound.
1270 static int pxa25x_start(struct usb_gadget_driver
*driver
,
1271 int (*bind
)(struct usb_gadget
*))
1273 struct pxa25x_udc
*dev
= the_controller
;
1277 || driver
->speed
< USB_SPEED_FULL
1279 || !driver
->disconnect
1287 /* first hook up the driver ... */
1288 dev
->driver
= driver
;
1289 dev
->gadget
.dev
.driver
= &driver
->driver
;
1292 retval
= device_add (&dev
->gadget
.dev
);
1296 dev
->gadget
.dev
.driver
= NULL
;
1299 retval
= bind(&dev
->gadget
);
1301 DMSG("bind to driver %s --> error %d\n",
1302 driver
->driver
.name
, retval
);
1303 device_del (&dev
->gadget
.dev
);
1307 /* ... then enable host detection and ep0; and we're ready
1308 * for set_configuration as well as eventual disconnect.
1310 DMSG("registered gadget driver '%s'\n", driver
->driver
.name
);
1312 /* connect to bus through transceiver */
1313 if (dev
->transceiver
) {
1314 retval
= otg_set_peripheral(dev
->transceiver
, &dev
->gadget
);
1316 DMSG("can't bind to transceiver\n");
1318 driver
->unbind(&dev
->gadget
);
1331 stop_activity(struct pxa25x_udc
*dev
, struct usb_gadget_driver
*driver
)
1335 /* don't disconnect drivers more than once */
1336 if (dev
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1338 dev
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1340 /* prevent new request submissions, kill any outstanding requests */
1341 for (i
= 0; i
< PXA_UDC_NUM_ENDPOINTS
; i
++) {
1342 struct pxa25x_ep
*ep
= &dev
->ep
[i
];
1345 nuke(ep
, -ESHUTDOWN
);
1347 del_timer_sync(&dev
->timer
);
1349 /* report disconnect; the driver is already quiesced */
1351 driver
->disconnect(&dev
->gadget
);
1353 /* re-init driver-visible data structures */
1357 static int pxa25x_stop(struct usb_gadget_driver
*driver
)
1359 struct pxa25x_udc
*dev
= the_controller
;
1363 if (!driver
|| driver
!= dev
->driver
|| !driver
->unbind
)
1366 local_irq_disable();
1369 stop_activity(dev
, driver
);
1372 if (dev
->transceiver
)
1373 (void) otg_set_peripheral(dev
->transceiver
, NULL
);
1375 driver
->unbind(&dev
->gadget
);
1376 dev
->gadget
.dev
.driver
= NULL
;
1379 device_del (&dev
->gadget
.dev
);
1381 DMSG("unregistered gadget driver '%s'\n", driver
->driver
.name
);
1386 /*-------------------------------------------------------------------------*/
1388 #ifdef CONFIG_ARCH_LUBBOCK
1390 /* Lubbock has separate connect and disconnect irqs. More typical designs
1391 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1395 lubbock_vbus_irq(int irq
, void *_dev
)
1397 struct pxa25x_udc
*dev
= _dev
;
1402 case LUBBOCK_USB_IRQ
:
1404 disable_irq(LUBBOCK_USB_IRQ
);
1405 enable_irq(LUBBOCK_USB_DISC_IRQ
);
1407 case LUBBOCK_USB_DISC_IRQ
:
1409 disable_irq(LUBBOCK_USB_DISC_IRQ
);
1410 enable_irq(LUBBOCK_USB_IRQ
);
1416 pxa25x_udc_vbus_session(&dev
->gadget
, vbus
);
1423 /*-------------------------------------------------------------------------*/
1425 static inline void clear_ep_state (struct pxa25x_udc
*dev
)
1429 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1430 * fifos, and pending transactions mustn't be continued in any case.
1432 for (i
= 1; i
< PXA_UDC_NUM_ENDPOINTS
; i
++)
1433 nuke(&dev
->ep
[i
], -ECONNABORTED
);
1436 static void udc_watchdog(unsigned long _dev
)
1438 struct pxa25x_udc
*dev
= (void *)_dev
;
1440 local_irq_disable();
1441 if (dev
->ep0state
== EP0_STALL
1442 && (UDCCS0
& UDCCS0_FST
) == 0
1443 && (UDCCS0
& UDCCS0_SST
) == 0) {
1444 UDCCS0
= UDCCS0_FST
|UDCCS0_FTF
;
1445 DBG(DBG_VERBOSE
, "ep0 re-stall\n");
1446 start_watchdog(dev
);
1451 static void handle_ep0 (struct pxa25x_udc
*dev
)
1453 u32 udccs0
= UDCCS0
;
1454 struct pxa25x_ep
*ep
= &dev
->ep
[0];
1455 struct pxa25x_request
*req
;
1457 struct usb_ctrlrequest r
;
1462 if (list_empty(&ep
->queue
))
1465 req
= list_entry(ep
->queue
.next
, struct pxa25x_request
, queue
);
1467 /* clear stall status */
1468 if (udccs0
& UDCCS0_SST
) {
1470 UDCCS0
= UDCCS0_SST
;
1471 del_timer(&dev
->timer
);
1475 /* previous request unfinished? non-error iff back-to-back ... */
1476 if ((udccs0
& UDCCS0_SA
) != 0 && dev
->ep0state
!= EP0_IDLE
) {
1478 del_timer(&dev
->timer
);
1482 switch (dev
->ep0state
) {
1484 /* late-breaking status? */
1487 /* start control request? */
1488 if (likely((udccs0
& (UDCCS0_OPR
|UDCCS0_SA
|UDCCS0_RNE
))
1489 == (UDCCS0_OPR
|UDCCS0_SA
|UDCCS0_RNE
))) {
1494 /* read SETUP packet */
1495 for (i
= 0; i
< 8; i
++) {
1496 if (unlikely(!(UDCCS0
& UDCCS0_RNE
))) {
1498 DMSG("SETUP %d!\n", i
);
1501 u
.raw
[i
] = (u8
) UDDR0
;
1503 if (unlikely((UDCCS0
& UDCCS0_RNE
) != 0))
1507 DBG(DBG_VERBOSE
, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1508 u
.r
.bRequestType
, u
.r
.bRequest
,
1509 le16_to_cpu(u
.r
.wValue
),
1510 le16_to_cpu(u
.r
.wIndex
),
1511 le16_to_cpu(u
.r
.wLength
));
1513 /* cope with automagic for some standard requests. */
1514 dev
->req_std
= (u
.r
.bRequestType
& USB_TYPE_MASK
)
1515 == USB_TYPE_STANDARD
;
1516 dev
->req_config
= 0;
1517 dev
->req_pending
= 1;
1518 switch (u
.r
.bRequest
) {
1519 /* hardware restricts gadget drivers here! */
1520 case USB_REQ_SET_CONFIGURATION
:
1521 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1522 /* reflect hardware's automagic
1523 * up to the gadget driver.
1526 dev
->req_config
= 1;
1527 clear_ep_state(dev
);
1528 /* if !has_cfr, there's no synch
1529 * else use AREN (later) not SA|OPR
1530 * USIR0_IR0 acts edge sensitive
1534 /* ... and here, even more ... */
1535 case USB_REQ_SET_INTERFACE
:
1536 if (u
.r
.bRequestType
== USB_RECIP_INTERFACE
) {
1537 /* udc hardware is broken by design:
1538 * - altsetting may only be zero;
1539 * - hw resets all interfaces' eps;
1540 * - ep reset doesn't include halt(?).
1542 DMSG("broken set_interface (%d/%d)\n",
1543 le16_to_cpu(u
.r
.wIndex
),
1544 le16_to_cpu(u
.r
.wValue
));
1548 /* hardware was supposed to hide this */
1549 case USB_REQ_SET_ADDRESS
:
1550 if (u
.r
.bRequestType
== USB_RECIP_DEVICE
) {
1551 ep0start(dev
, 0, "address");
1557 if (u
.r
.bRequestType
& USB_DIR_IN
)
1558 dev
->ep0state
= EP0_IN_DATA_PHASE
;
1560 dev
->ep0state
= EP0_OUT_DATA_PHASE
;
1562 i
= dev
->driver
->setup(&dev
->gadget
, &u
.r
);
1564 /* hardware automagic preventing STALL... */
1565 if (dev
->req_config
) {
1566 /* hardware sometimes neglects to tell
1567 * tell us about config change events,
1568 * so later ones may fail...
1570 WARNING("config change %02x fail %d?\n",
1573 /* TODO experiment: if has_cfr,
1574 * hardware didn't ACK; maybe we
1575 * could actually STALL!
1578 DBG(DBG_VERBOSE
, "protocol STALL, "
1579 "%02x err %d\n", UDCCS0
, i
);
1581 /* the watchdog timer helps deal with cases
1582 * where udc seems to clear FST wrongly, and
1583 * then NAKs instead of STALLing.
1585 ep0start(dev
, UDCCS0_FST
|UDCCS0_FTF
, "stall");
1586 start_watchdog(dev
);
1587 dev
->ep0state
= EP0_STALL
;
1589 /* deferred i/o == no response yet */
1590 } else if (dev
->req_pending
) {
1591 if (likely(dev
->ep0state
== EP0_IN_DATA_PHASE
1592 || dev
->req_std
|| u
.r
.wLength
))
1593 ep0start(dev
, 0, "defer");
1595 ep0start(dev
, UDCCS0_IPR
, "defer/IPR");
1598 /* expect at least one data or status stage irq */
1601 } else if (likely((udccs0
& (UDCCS0_OPR
|UDCCS0_SA
))
1602 == (UDCCS0_OPR
|UDCCS0_SA
))) {
1605 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1606 * still observed on a pxa255 a0.
1608 DBG(DBG_VERBOSE
, "e131\n");
1611 /* read SETUP data, but don't trust it too much */
1612 for (i
= 0; i
< 8; i
++)
1613 u
.raw
[i
] = (u8
) UDDR0
;
1614 if ((u
.r
.bRequestType
& USB_RECIP_MASK
)
1617 if (u
.word
[0] == 0 && u
.word
[1] == 0)
1621 /* some random early IRQ:
1624 * - OPR got set, without SA (likely status stage)
1626 UDCCS0
= udccs0
& (UDCCS0_SA
|UDCCS0_OPR
);
1629 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR etc */
1630 if (udccs0
& UDCCS0_OPR
) {
1631 UDCCS0
= UDCCS0_OPR
|UDCCS0_FTF
;
1632 DBG(DBG_VERBOSE
, "ep0in premature status\n");
1636 } else /* irq was IPR clearing */ {
1638 /* this IN packet might finish the request */
1639 (void) write_ep0_fifo(ep
, req
);
1640 } /* else IN token before response was written */
1643 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR etc */
1644 if (udccs0
& UDCCS0_OPR
) {
1646 /* this OUT packet might finish the request */
1647 if (read_ep0_fifo(ep
, req
))
1649 /* else more OUT packets expected */
1650 } /* else OUT token before read was issued */
1651 } else /* irq was IPR clearing */ {
1652 DBG(DBG_VERBOSE
, "ep0out premature status\n");
1661 /* ack control-IN status (maybe in-zlp was skipped)
1662 * also appears after some config change events.
1664 if (udccs0
& UDCCS0_OPR
)
1665 UDCCS0
= UDCCS0_OPR
;
1669 UDCCS0
= UDCCS0_FST
;
1675 static void handle_ep(struct pxa25x_ep
*ep
)
1677 struct pxa25x_request
*req
;
1678 int is_in
= ep
->bEndpointAddress
& USB_DIR_IN
;
1684 if (likely (!list_empty(&ep
->queue
)))
1685 req
= list_entry(ep
->queue
.next
,
1686 struct pxa25x_request
, queue
);
1690 // TODO check FST handling
1692 udccs
= *ep
->reg_udccs
;
1693 if (unlikely(is_in
)) { /* irq from TPC, SST, or (ISO) TUR */
1695 if (likely(ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
))
1696 tmp
|= UDCCS_BI_SST
;
1699 *ep
->reg_udccs
= tmp
;
1700 if (req
&& likely ((udccs
& UDCCS_BI_TFS
) != 0))
1701 completed
= write_fifo(ep
, req
);
1703 } else { /* irq from RPC (or for ISO, ROF) */
1704 if (likely(ep
->bmAttributes
== USB_ENDPOINT_XFER_BULK
))
1705 tmp
= UDCCS_BO_SST
| UDCCS_BO_DME
;
1707 tmp
= UDCCS_IO_ROF
| UDCCS_IO_DME
;
1710 *ep
->reg_udccs
= tmp
;
1712 /* fifos can hold packets, ready for reading... */
1714 completed
= read_fifo(ep
, req
);
1716 pio_irq_disable (ep
->bEndpointAddress
);
1719 } while (completed
);
1723 * pxa25x_udc_irq - interrupt handler
1725 * avoid delays in ep0 processing. the control handshaking isn't always
1726 * under software control (pxa250c0 and the pxa255 are better), and delays
1727 * could cause usb protocol errors.
1730 pxa25x_udc_irq(int irq
, void *_dev
)
1732 struct pxa25x_udc
*dev
= _dev
;
1741 /* SUSpend Interrupt Request */
1742 if (unlikely(udccr
& UDCCR_SUSIR
)) {
1743 udc_ack_int_UDCCR(UDCCR_SUSIR
);
1745 DBG(DBG_VERBOSE
, "USB suspend\n");
1747 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1749 && dev
->driver
->suspend
)
1750 dev
->driver
->suspend(&dev
->gadget
);
1754 /* RESume Interrupt Request */
1755 if (unlikely(udccr
& UDCCR_RESIR
)) {
1756 udc_ack_int_UDCCR(UDCCR_RESIR
);
1758 DBG(DBG_VERBOSE
, "USB resume\n");
1760 if (dev
->gadget
.speed
!= USB_SPEED_UNKNOWN
1762 && dev
->driver
->resume
)
1763 dev
->driver
->resume(&dev
->gadget
);
1766 /* ReSeT Interrupt Request - USB reset */
1767 if (unlikely(udccr
& UDCCR_RSTIR
)) {
1768 udc_ack_int_UDCCR(UDCCR_RSTIR
);
1771 if ((UDCCR
& UDCCR_UDA
) == 0) {
1772 DBG(DBG_VERBOSE
, "USB reset start\n");
1774 /* reset driver and endpoints,
1775 * in case that's not yet done
1777 stop_activity (dev
, dev
->driver
);
1780 DBG(DBG_VERBOSE
, "USB reset end\n");
1781 dev
->gadget
.speed
= USB_SPEED_FULL
;
1782 memset(&dev
->stats
, 0, sizeof dev
->stats
);
1783 /* driver and endpoints are still reset */
1787 u32 usir0
= USIR0
& ~UICR0
;
1788 u32 usir1
= USIR1
& ~UICR1
;
1791 if (unlikely (!usir0
&& !usir1
))
1794 DBG(DBG_VERY_NOISY
, "irq %02x.%02x\n", usir1
, usir0
);
1796 /* control traffic */
1797 if (usir0
& USIR0_IR0
) {
1798 dev
->ep
[0].pio_irqs
++;
1803 /* endpoint data transfers */
1804 for (i
= 0; i
< 8; i
++) {
1807 if (i
&& (usir0
& tmp
)) {
1808 handle_ep(&dev
->ep
[i
]);
1812 #ifndef CONFIG_USB_PXA25X_SMALL
1814 handle_ep(&dev
->ep
[i
+8]);
1822 /* we could also ask for 1 msec SOF (SIR) interrupts */
1828 /*-------------------------------------------------------------------------*/
1830 static void nop_release (struct device
*dev
)
1832 DMSG("%s %s\n", __func__
, dev_name(dev
));
1835 /* this uses load-time allocation and initialization (instead of
1836 * doing it at run-time) to save code, eliminate fault paths, and
1837 * be more obviously correct.
1839 static struct pxa25x_udc memory
= {
1841 .ops
= &pxa25x_udc_ops
,
1842 .ep0
= &memory
.ep
[0].ep
,
1843 .name
= driver_name
,
1845 .init_name
= "gadget",
1846 .release
= nop_release
,
1850 /* control endpoint */
1854 .ops
= &pxa25x_ep_ops
,
1855 .maxpacket
= EP0_FIFO_SIZE
,
1858 .reg_udccs
= &UDCCS0
,
1862 /* first group of endpoints */
1865 .name
= "ep1in-bulk",
1866 .ops
= &pxa25x_ep_ops
,
1867 .maxpacket
= BULK_FIFO_SIZE
,
1870 .fifo_size
= BULK_FIFO_SIZE
,
1871 .bEndpointAddress
= USB_DIR_IN
| 1,
1872 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1873 .reg_udccs
= &UDCCS1
,
1878 .name
= "ep2out-bulk",
1879 .ops
= &pxa25x_ep_ops
,
1880 .maxpacket
= BULK_FIFO_SIZE
,
1883 .fifo_size
= BULK_FIFO_SIZE
,
1884 .bEndpointAddress
= 2,
1885 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1886 .reg_udccs
= &UDCCS2
,
1890 #ifndef CONFIG_USB_PXA25X_SMALL
1893 .name
= "ep3in-iso",
1894 .ops
= &pxa25x_ep_ops
,
1895 .maxpacket
= ISO_FIFO_SIZE
,
1898 .fifo_size
= ISO_FIFO_SIZE
,
1899 .bEndpointAddress
= USB_DIR_IN
| 3,
1900 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1901 .reg_udccs
= &UDCCS3
,
1906 .name
= "ep4out-iso",
1907 .ops
= &pxa25x_ep_ops
,
1908 .maxpacket
= ISO_FIFO_SIZE
,
1911 .fifo_size
= ISO_FIFO_SIZE
,
1912 .bEndpointAddress
= 4,
1913 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1914 .reg_udccs
= &UDCCS4
,
1920 .name
= "ep5in-int",
1921 .ops
= &pxa25x_ep_ops
,
1922 .maxpacket
= INT_FIFO_SIZE
,
1925 .fifo_size
= INT_FIFO_SIZE
,
1926 .bEndpointAddress
= USB_DIR_IN
| 5,
1927 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
1928 .reg_udccs
= &UDCCS5
,
1932 /* second group of endpoints */
1935 .name
= "ep6in-bulk",
1936 .ops
= &pxa25x_ep_ops
,
1937 .maxpacket
= BULK_FIFO_SIZE
,
1940 .fifo_size
= BULK_FIFO_SIZE
,
1941 .bEndpointAddress
= USB_DIR_IN
| 6,
1942 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1943 .reg_udccs
= &UDCCS6
,
1948 .name
= "ep7out-bulk",
1949 .ops
= &pxa25x_ep_ops
,
1950 .maxpacket
= BULK_FIFO_SIZE
,
1953 .fifo_size
= BULK_FIFO_SIZE
,
1954 .bEndpointAddress
= 7,
1955 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1956 .reg_udccs
= &UDCCS7
,
1962 .name
= "ep8in-iso",
1963 .ops
= &pxa25x_ep_ops
,
1964 .maxpacket
= ISO_FIFO_SIZE
,
1967 .fifo_size
= ISO_FIFO_SIZE
,
1968 .bEndpointAddress
= USB_DIR_IN
| 8,
1969 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1970 .reg_udccs
= &UDCCS8
,
1975 .name
= "ep9out-iso",
1976 .ops
= &pxa25x_ep_ops
,
1977 .maxpacket
= ISO_FIFO_SIZE
,
1980 .fifo_size
= ISO_FIFO_SIZE
,
1981 .bEndpointAddress
= 9,
1982 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
1983 .reg_udccs
= &UDCCS9
,
1989 .name
= "ep10in-int",
1990 .ops
= &pxa25x_ep_ops
,
1991 .maxpacket
= INT_FIFO_SIZE
,
1994 .fifo_size
= INT_FIFO_SIZE
,
1995 .bEndpointAddress
= USB_DIR_IN
| 10,
1996 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
1997 .reg_udccs
= &UDCCS10
,
1998 .reg_uddr
= &UDDR10
,
2001 /* third group of endpoints */
2004 .name
= "ep11in-bulk",
2005 .ops
= &pxa25x_ep_ops
,
2006 .maxpacket
= BULK_FIFO_SIZE
,
2009 .fifo_size
= BULK_FIFO_SIZE
,
2010 .bEndpointAddress
= USB_DIR_IN
| 11,
2011 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2012 .reg_udccs
= &UDCCS11
,
2013 .reg_uddr
= &UDDR11
,
2017 .name
= "ep12out-bulk",
2018 .ops
= &pxa25x_ep_ops
,
2019 .maxpacket
= BULK_FIFO_SIZE
,
2022 .fifo_size
= BULK_FIFO_SIZE
,
2023 .bEndpointAddress
= 12,
2024 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
2025 .reg_udccs
= &UDCCS12
,
2026 .reg_ubcr
= &UBCR12
,
2027 .reg_uddr
= &UDDR12
,
2031 .name
= "ep13in-iso",
2032 .ops
= &pxa25x_ep_ops
,
2033 .maxpacket
= ISO_FIFO_SIZE
,
2036 .fifo_size
= ISO_FIFO_SIZE
,
2037 .bEndpointAddress
= USB_DIR_IN
| 13,
2038 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2039 .reg_udccs
= &UDCCS13
,
2040 .reg_uddr
= &UDDR13
,
2044 .name
= "ep14out-iso",
2045 .ops
= &pxa25x_ep_ops
,
2046 .maxpacket
= ISO_FIFO_SIZE
,
2049 .fifo_size
= ISO_FIFO_SIZE
,
2050 .bEndpointAddress
= 14,
2051 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
2052 .reg_udccs
= &UDCCS14
,
2053 .reg_ubcr
= &UBCR14
,
2054 .reg_uddr
= &UDDR14
,
2058 .name
= "ep15in-int",
2059 .ops
= &pxa25x_ep_ops
,
2060 .maxpacket
= INT_FIFO_SIZE
,
2063 .fifo_size
= INT_FIFO_SIZE
,
2064 .bEndpointAddress
= USB_DIR_IN
| 15,
2065 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
2066 .reg_udccs
= &UDCCS15
,
2067 .reg_uddr
= &UDDR15
,
2069 #endif /* !CONFIG_USB_PXA25X_SMALL */
2072 #define CP15R0_VENDOR_MASK 0xffffe000
2074 #if defined(CONFIG_ARCH_PXA)
2075 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2077 #elif defined(CONFIG_ARCH_IXP4XX)
2078 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2082 #define CP15R0_PROD_MASK 0x000003f0
2083 #define PXA25x 0x00000100 /* and PXA26x */
2084 #define PXA210 0x00000120
2086 #define CP15R0_REV_MASK 0x0000000f
2088 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2090 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2091 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2092 #define PXA250_B2 0x00000104
2093 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2094 #define PXA250_B0 0x00000102
2095 #define PXA250_A1 0x00000101
2096 #define PXA250_A0 0x00000100
2098 #define PXA210_C0 0x00000125
2099 #define PXA210_B2 0x00000124
2100 #define PXA210_B1 0x00000123
2101 #define PXA210_B0 0x00000122
2102 #define IXP425_A0 0x000001c1
2103 #define IXP425_B0 0x000001f1
2104 #define IXP465_AD 0x00000200
2107 * probe - binds to the platform device
2109 static int __init
pxa25x_udc_probe(struct platform_device
*pdev
)
2111 struct pxa25x_udc
*dev
= &memory
;
2115 /* insist on Intel/ARM/XScale */
2116 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev
));
2117 if ((chiprev
& CP15R0_VENDOR_MASK
) != CP15R0_XSCALE_VALUE
) {
2118 pr_err("%s: not XScale!\n", driver_name
);
2122 /* trigger chiprev-specific logic */
2123 switch (chiprev
& CP15R0_PRODREV_MASK
) {
2124 #if defined(CONFIG_ARCH_PXA)
2130 /* A0/A1 "not released"; ep 13, 15 unusable */
2132 case PXA250_B2
: case PXA210_B2
:
2133 case PXA250_B1
: case PXA210_B1
:
2134 case PXA250_B0
: case PXA210_B0
:
2135 /* OUT-DMA is broken ... */
2137 case PXA250_C0
: case PXA210_C0
:
2139 #elif defined(CONFIG_ARCH_IXP4XX)
2147 pr_err("%s: unrecognized processor: %08x\n",
2148 driver_name
, chiprev
);
2149 /* iop3xx, ixp4xx, ... */
2153 irq
= platform_get_irq(pdev
, 0);
2157 dev
->clk
= clk_get(&pdev
->dev
, NULL
);
2158 if (IS_ERR(dev
->clk
)) {
2159 retval
= PTR_ERR(dev
->clk
);
2163 pr_debug("%s: IRQ %d%s%s\n", driver_name
, irq
,
2164 dev
->has_cfr
? "" : " (!cfr)",
2168 /* other non-static parts of init */
2169 dev
->dev
= &pdev
->dev
;
2170 dev
->mach
= pdev
->dev
.platform_data
;
2172 dev
->transceiver
= otg_get_transceiver();
2174 if (gpio_is_valid(dev
->mach
->gpio_pullup
)) {
2175 if ((retval
= gpio_request(dev
->mach
->gpio_pullup
,
2176 "pca25x_udc GPIO PULLUP"))) {
2178 "can't get pullup gpio %d, err: %d\n",
2179 dev
->mach
->gpio_pullup
, retval
);
2180 goto err_gpio_pullup
;
2182 gpio_direction_output(dev
->mach
->gpio_pullup
, 0);
2185 init_timer(&dev
->timer
);
2186 dev
->timer
.function
= udc_watchdog
;
2187 dev
->timer
.data
= (unsigned long) dev
;
2189 device_initialize(&dev
->gadget
.dev
);
2190 dev
->gadget
.dev
.parent
= &pdev
->dev
;
2191 dev
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
2193 the_controller
= dev
;
2194 platform_set_drvdata(pdev
, dev
);
2201 /* irq setup after old hardware state is cleaned up */
2202 retval
= request_irq(irq
, pxa25x_udc_irq
,
2203 IRQF_DISABLED
, driver_name
, dev
);
2205 pr_err("%s: can't get irq %d, err %d\n",
2206 driver_name
, irq
, retval
);
2211 #ifdef CONFIG_ARCH_LUBBOCK
2212 if (machine_is_lubbock()) {
2213 retval
= request_irq(LUBBOCK_USB_DISC_IRQ
,
2215 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
2218 pr_err("%s: can't get irq %i, err %d\n",
2219 driver_name
, LUBBOCK_USB_DISC_IRQ
, retval
);
2222 retval
= request_irq(LUBBOCK_USB_IRQ
,
2224 IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
2227 pr_err("%s: can't get irq %i, err %d\n",
2228 driver_name
, LUBBOCK_USB_IRQ
, retval
);
2233 create_debug_files(dev
);
2235 retval
= usb_add_gadget_udc(&pdev
->dev
, &dev
->gadget
);
2239 remove_debug_files(dev
);
2240 #ifdef CONFIG_ARCH_LUBBOCK
2242 free_irq(LUBBOCK_USB_DISC_IRQ
, dev
);
2247 if (gpio_is_valid(dev
->mach
->gpio_pullup
))
2248 gpio_free(dev
->mach
->gpio_pullup
);
2250 if (dev
->transceiver
) {
2251 otg_put_transceiver(dev
->transceiver
);
2252 dev
->transceiver
= NULL
;
2259 static void pxa25x_udc_shutdown(struct platform_device
*_dev
)
2264 static int __exit
pxa25x_udc_remove(struct platform_device
*pdev
)
2266 struct pxa25x_udc
*dev
= platform_get_drvdata(pdev
);
2268 usb_del_gadget_udc(&dev
->gadget
);
2275 remove_debug_files(dev
);
2278 free_irq(platform_get_irq(pdev
, 0), dev
);
2281 #ifdef CONFIG_ARCH_LUBBOCK
2282 if (machine_is_lubbock()) {
2283 free_irq(LUBBOCK_USB_DISC_IRQ
, dev
);
2284 free_irq(LUBBOCK_USB_IRQ
, dev
);
2287 if (gpio_is_valid(dev
->mach
->gpio_pullup
))
2288 gpio_free(dev
->mach
->gpio_pullup
);
2292 if (dev
->transceiver
) {
2293 otg_put_transceiver(dev
->transceiver
);
2294 dev
->transceiver
= NULL
;
2297 platform_set_drvdata(pdev
, NULL
);
2298 the_controller
= NULL
;
2302 /*-------------------------------------------------------------------------*/
2306 /* USB suspend (controlled by the host) and system suspend (controlled
2307 * by the PXA) don't necessarily work well together. If USB is active,
2308 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2309 * mode, or any deeper PM saving state.
2311 * For now, we punt and forcibly disconnect from the USB host when PXA
2312 * enters any suspend state. While we're disconnected, we always disable
2313 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2314 * Boards without software pullup control shouldn't use those states.
2315 * VBUS IRQs should probably be ignored so that the PXA device just acts
2316 * "dead" to USB hosts until system resume.
2318 static int pxa25x_udc_suspend(struct platform_device
*dev
, pm_message_t state
)
2320 struct pxa25x_udc
*udc
= platform_get_drvdata(dev
);
2321 unsigned long flags
;
2323 if (!gpio_is_valid(udc
->mach
->gpio_pullup
) && !udc
->mach
->udc_command
)
2324 WARNING("USB host won't detect disconnect!\n");
2327 local_irq_save(flags
);
2329 local_irq_restore(flags
);
2334 static int pxa25x_udc_resume(struct platform_device
*dev
)
2336 struct pxa25x_udc
*udc
= platform_get_drvdata(dev
);
2337 unsigned long flags
;
2340 local_irq_save(flags
);
2342 local_irq_restore(flags
);
2348 #define pxa25x_udc_suspend NULL
2349 #define pxa25x_udc_resume NULL
2352 /*-------------------------------------------------------------------------*/
2354 static struct platform_driver udc_driver
= {
2355 .shutdown
= pxa25x_udc_shutdown
,
2356 .remove
= __exit_p(pxa25x_udc_remove
),
2357 .suspend
= pxa25x_udc_suspend
,
2358 .resume
= pxa25x_udc_resume
,
2360 .owner
= THIS_MODULE
,
2361 .name
= "pxa25x-udc",
2365 static int __init
udc_init(void)
2367 pr_info("%s: version %s\n", driver_name
, DRIVER_VERSION
);
2368 return platform_driver_probe(&udc_driver
, pxa25x_udc_probe
);
2370 module_init(udc_init
);
2372 static void __exit
udc_exit(void)
2374 platform_driver_unregister(&udc_driver
);
2376 module_exit(udc_exit
);
2378 MODULE_DESCRIPTION(DRIVER_DESC
);
2379 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2380 MODULE_LICENSE("GPL");
2381 MODULE_ALIAS("platform:pxa25x-udc");