2 * linux/drivers/usb/gadget/pxa2xx_udc.c
3 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
5 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
6 * Copyright (C) 2003 Robert Schwebel, Pengutronix
7 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
8 * Copyright (C) 2003 David Brownell
9 * Copyright (C) 2003 Joshua Wise
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 // #define VERBOSE DBG_VERBOSE
29 #include <linux/device.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/ioport.h>
33 #include <linux/types.h>
34 #include <linux/errno.h>
35 #include <linux/delay.h>
36 #include <linux/slab.h>
37 #include <linux/init.h>
38 #include <linux/timer.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/proc_fs.h>
43 #include <linux/platform_device.h>
44 #include <linux/dma-mapping.h>
45 #include <linux/irq.h>
47 #include <asm/byteorder.h>
51 #include <asm/system.h>
52 #include <asm/mach-types.h>
53 #include <asm/unaligned.h>
54 #include <asm/hardware.h>
56 #include <linux/usb/ch9.h>
57 #include <linux/usb_gadget.h>
59 #include <asm/mach/udc_pxa2xx.h>
63 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
64 * series processors. The UDC for the IXP 4xx series is very similar.
65 * There are fifteen endpoints, in addition to ep0.
67 * Such controller drivers work with a gadget driver. The gadget driver
68 * returns descriptors, implements configuration and data protocols used
69 * by the host to interact with this device, and allocates endpoints to
70 * the different protocol interfaces. The controller driver virtualizes
71 * usb hardware so that the gadget drivers will be more portable.
73 * This UDC hardware wants to implement a bit too much USB protocol, so
74 * it constrains the sorts of USB configuration change events that work.
75 * The errata for these chips are misleading; some "fixed" bugs from
76 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
78 * Note that the UDC hardware supports DMA (except on IXP) but that's
79 * not used here. IN-DMA (to host) is simple enough, when the data is
80 * suitably aligned (16 bytes) ... the network stack doesn't do that,
81 * other software can. OUT-DMA is buggy in most chip versions, as well
82 * as poorly designed (data toggle not automatic). So this driver won't
83 * bother using DMA. (Mostly-working IN-DMA support was available in
84 * kernels before 2.6.23, but was never enabled or well tested.)
87 #define DRIVER_VERSION "30-June-2007"
88 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
91 static const char driver_name [] = "pxa2xx_udc";
93 static const char ep0name [] = "ep0";
96 // #define DISABLE_TEST_MODE
98 #ifdef CONFIG_ARCH_IXP4XX
100 /* cpu-specific register addresses are compiled in to this code */
101 #ifdef CONFIG_ARCH_PXA
102 #error "Can't configure both IXP and PXA"
107 #include "pxa2xx_udc.h"
110 #ifdef CONFIG_USB_PXA2XX_SMALL
111 #define SIZE_STR " (small)"
116 #ifdef DISABLE_TEST_MODE
117 /* (mode == 0) == no undocumented chip tweaks
118 * (mode & 1) == double buffer bulk IN
119 * (mode & 2) == double buffer bulk OUT
120 * ... so mode = 3 (or 7, 15, etc) does it for both
122 static ushort fifo_mode = 0;
123 module_param(fifo_mode, ushort, 0);
124 MODULE_PARM_DESC (fifo_mode, "pxa2xx udc fifo mode");
127 /* ---------------------------------------------------------------------------
128 * endpoint related parts of the api to the usb controller hardware,
129 * used by gadget driver; and the inner talker-to-hardware core.
130 * ---------------------------------------------------------------------------
133 static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
134 static void nuke (struct pxa2xx_ep *, int status);
136 /* one GPIO should be used to detect VBUS from the host */
137 static int is_vbus_present(void)
139 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
142 return gpio_get_value(mach->gpio_vbus);
143 if (mach->udc_is_connected)
144 return mach->udc_is_connected();
148 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
149 static void pullup_off(void)
151 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
153 if (mach->gpio_pullup)
154 gpio_set_value(mach->gpio_pullup, 0);
155 else if (mach->udc_command)
156 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
159 static void pullup_on(void)
161 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
163 if (mach->gpio_pullup)
164 gpio_set_value(mach->gpio_pullup, 1);
165 else if (mach->udc_command)
166 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
169 static void pio_irq_enable(int bEndpointAddress)
171 bEndpointAddress &= 0xf;
172 if (bEndpointAddress < 8)
173 UICR0 &= ~(1 << bEndpointAddress);
175 bEndpointAddress -= 8;
176 UICR1 &= ~(1 << bEndpointAddress);
180 static void pio_irq_disable(int bEndpointAddress)
182 bEndpointAddress &= 0xf;
183 if (bEndpointAddress < 8)
184 UICR0 |= 1 << bEndpointAddress;
186 bEndpointAddress -= 8;
187 UICR1 |= 1 << bEndpointAddress;
191 /* The UDCCR reg contains mask and interrupt status bits,
192 * so using '|=' isn't safe as it may ack an interrupt.
194 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
196 static inline void udc_set_mask_UDCCR(int mask)
198 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
201 static inline void udc_clear_mask_UDCCR(int mask)
203 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
206 static inline void udc_ack_int_UDCCR(int mask)
208 /* udccr contains the bits we dont want to change */
209 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
211 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
215 * endpoint enable/disable
217 * we need to verify the descriptors used to enable endpoints. since pxa2xx
218 * endpoint configurations are fixed, and are pretty much always enabled,
219 * there's not a lot to manage here.
221 * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
222 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
223 * for a single interface (with only the default altsetting) and for gadget
224 * drivers that don't halt endpoints (not reset by set_interface). that also
225 * means that if you use ISO, you must violate the USB spec rule that all
226 * iso endpoints must be in non-default altsettings.
228 static int pxa2xx_ep_enable (struct usb_ep *_ep,
229 const struct usb_endpoint_descriptor *desc)
231 struct pxa2xx_ep *ep;
232 struct pxa2xx_udc *dev;
234 ep = container_of (_ep, struct pxa2xx_ep, ep);
235 if (!_ep || !desc || ep->desc || _ep->name == ep0name
236 || desc->bDescriptorType != USB_DT_ENDPOINT
237 || ep->bEndpointAddress != desc->bEndpointAddress
238 || ep->fifo_size < le16_to_cpu
239 (desc->wMaxPacketSize)) {
240 DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
244 /* xfer types must match, except that interrupt ~= bulk */
245 if (ep->bmAttributes != desc->bmAttributes
246 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
247 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
248 DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
252 /* hardware _could_ do smaller, but driver doesn't */
253 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
254 && le16_to_cpu (desc->wMaxPacketSize)
256 || !desc->wMaxPacketSize) {
257 DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
262 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
263 DMSG("%s, bogus device state\n", __FUNCTION__);
270 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
272 /* flush fifo (mostly for OUT buffers) */
273 pxa2xx_ep_fifo_flush (_ep);
275 /* ... reset halt state too, if we could ... */
277 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
281 static int pxa2xx_ep_disable (struct usb_ep *_ep)
283 struct pxa2xx_ep *ep;
286 ep = container_of (_ep, struct pxa2xx_ep, ep);
287 if (!_ep || !ep->desc) {
288 DMSG("%s, %s not enabled\n", __FUNCTION__,
289 _ep ? ep->ep.name : NULL);
292 local_irq_save(flags);
294 nuke (ep, -ESHUTDOWN);
296 /* flush fifo (mostly for IN buffers) */
297 pxa2xx_ep_fifo_flush (_ep);
302 local_irq_restore(flags);
303 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
307 /*-------------------------------------------------------------------------*/
309 /* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
310 * must still pass correctly initialized endpoints, since other controller
311 * drivers may care about how it's currently set up (dma issues etc).
315 * pxa2xx_ep_alloc_request - allocate a request data structure
317 static struct usb_request *
318 pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
320 struct pxa2xx_request *req;
322 req = kzalloc(sizeof(*req), gfp_flags);
326 INIT_LIST_HEAD (&req->queue);
332 * pxa2xx_ep_free_request - deallocate a request data structure
335 pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
337 struct pxa2xx_request *req;
339 req = container_of (_req, struct pxa2xx_request, req);
340 WARN_ON (!list_empty (&req->queue));
344 /*-------------------------------------------------------------------------*/
347 * done - retire a request; caller blocked irqs
349 static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
351 unsigned stopped = ep->stopped;
353 list_del_init(&req->queue);
355 if (likely (req->req.status == -EINPROGRESS))
356 req->req.status = status;
358 status = req->req.status;
360 if (status && status != -ESHUTDOWN)
361 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
362 ep->ep.name, &req->req, status,
363 req->req.actual, req->req.length);
365 /* don't modify queue heads during completion callback */
367 req->req.complete(&ep->ep, &req->req);
368 ep->stopped = stopped;
372 static inline void ep0_idle (struct pxa2xx_udc *dev)
374 dev->ep0state = EP0_IDLE;
378 write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
381 unsigned length, count;
383 buf = req->req.buf + req->req.actual;
386 /* how big will this packet be? */
387 length = min(req->req.length - req->req.actual, max);
388 req->req.actual += length;
391 while (likely(count--))
398 * write to an IN endpoint fifo, as many packets as possible.
399 * irqs will use this to write the rest later.
400 * caller guarantees at least one packet buffer is ready (or a zlp).
403 write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
407 max = le16_to_cpu(ep->desc->wMaxPacketSize);
410 int is_last, is_short;
412 count = write_packet(ep->reg_uddr, req, max);
414 /* last packet is usually short (or a zlp) */
415 if (unlikely (count != max))
416 is_last = is_short = 1;
418 if (likely(req->req.length != req->req.actual)
423 /* interrupt/iso maxpacket may not fill the fifo */
424 is_short = unlikely (max < ep->fifo_size);
427 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
429 is_last ? "/L" : "", is_short ? "/S" : "",
430 req->req.length - req->req.actual, req);
432 /* let loose that packet. maybe try writing another one,
433 * double buffering might work. TSP, TPC, and TFS
434 * bit values are the same for all normal IN endpoints.
436 *ep->reg_udccs = UDCCS_BI_TPC;
438 *ep->reg_udccs = UDCCS_BI_TSP;
440 /* requests complete when all IN data is in the FIFO */
443 if (list_empty(&ep->queue))
444 pio_irq_disable (ep->bEndpointAddress);
448 // TODO experiment: how robust can fifo mode tweaking be?
449 // double buffering is off in the default fifo mode, which
450 // prevents TFS from being set here.
452 } while (*ep->reg_udccs & UDCCS_BI_TFS);
456 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
457 * ep0 data stage. these chips want very simple state transitions.
460 void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
462 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
464 dev->req_pending = 0;
465 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
466 __FUNCTION__, tag, UDCCS0, flags);
470 write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
475 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
476 ep->dev->stats.write.bytes += count;
478 /* last packet "must be" short (or a zlp) */
479 is_short = (count != EP0_FIFO_SIZE);
481 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
482 req->req.length - req->req.actual, req);
484 if (unlikely (is_short)) {
485 if (ep->dev->req_pending)
486 ep0start(ep->dev, UDCCS0_IPR, "short IN");
490 count = req->req.length;
493 #ifndef CONFIG_ARCH_IXP4XX
495 /* This seems to get rid of lost status irqs in some cases:
496 * host responds quickly, or next request involves config
497 * change automagic, or should have been hidden, or ...
499 * FIXME get rid of all udelays possible...
501 if (count >= EP0_FIFO_SIZE) {
504 if ((UDCCS0 & UDCCS0_OPR) != 0) {
505 /* clear OPR, generate ack */
515 } else if (ep->dev->req_pending)
516 ep0start(ep->dev, 0, "IN");
522 * read_fifo - unload packet(s) from the fifo we use for usb OUT
523 * transfers and put them into the request. caller should have made
524 * sure there's at least one packet ready.
526 * returns true if the request completed because of short packet or the
527 * request buffer having filled (and maybe overran till end-of-packet).
530 read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
535 unsigned bufferspace, count, is_short;
537 /* make sure there's a packet in the FIFO.
538 * UDCCS_{BO,IO}_RPC are all the same bit value.
539 * UDCCS_{BO,IO}_RNE are all the same bit value.
541 udccs = *ep->reg_udccs;
542 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
544 buf = req->req.buf + req->req.actual;
546 bufferspace = req->req.length - req->req.actual;
548 /* read all bytes from this packet */
549 if (likely (udccs & UDCCS_BO_RNE)) {
550 count = 1 + (0x0ff & *ep->reg_ubcr);
551 req->req.actual += min (count, bufferspace);
554 is_short = (count < ep->ep.maxpacket);
555 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
556 ep->ep.name, udccs, count,
557 is_short ? "/S" : "",
558 req, req->req.actual, req->req.length);
559 while (likely (count-- != 0)) {
560 u8 byte = (u8) *ep->reg_uddr;
562 if (unlikely (bufferspace == 0)) {
563 /* this happens when the driver's buffer
564 * is smaller than what the host sent.
565 * discard the extra data.
567 if (req->req.status != -EOVERFLOW)
568 DMSG("%s overflow %d\n",
570 req->req.status = -EOVERFLOW;
576 *ep->reg_udccs = UDCCS_BO_RPC;
577 /* RPC/RSP/RNE could now reflect the other packet buffer */
579 /* iso is one request per packet */
580 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
581 if (udccs & UDCCS_IO_ROF)
582 req->req.status = -EHOSTUNREACH;
583 /* more like "is_done" */
588 if (is_short || req->req.actual == req->req.length) {
590 if (list_empty(&ep->queue))
591 pio_irq_disable (ep->bEndpointAddress);
595 /* finished that packet. the next one may be waiting... */
601 * special ep0 version of the above. no UBCR0 or double buffering; status
602 * handshaking is magic. most device protocols don't need control-OUT.
603 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
604 * protocols do use them.
607 read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
610 unsigned bufferspace;
612 buf = req->req.buf + req->req.actual;
613 bufferspace = req->req.length - req->req.actual;
615 while (UDCCS0 & UDCCS0_RNE) {
618 if (unlikely (bufferspace == 0)) {
619 /* this happens when the driver's buffer
620 * is smaller than what the host sent.
621 * discard the extra data.
623 if (req->req.status != -EOVERFLOW)
624 DMSG("%s overflow\n", ep->ep.name);
625 req->req.status = -EOVERFLOW;
633 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
636 if (req->req.actual >= req->req.length)
639 /* finished that packet. the next one may be waiting... */
643 /*-------------------------------------------------------------------------*/
646 pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
648 struct pxa2xx_request *req;
649 struct pxa2xx_ep *ep;
650 struct pxa2xx_udc *dev;
653 req = container_of(_req, struct pxa2xx_request, req);
654 if (unlikely (!_req || !_req->complete || !_req->buf
655 || !list_empty(&req->queue))) {
656 DMSG("%s, bad params\n", __FUNCTION__);
660 ep = container_of(_ep, struct pxa2xx_ep, ep);
661 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
662 DMSG("%s, bad ep\n", __FUNCTION__);
667 if (unlikely (!dev->driver
668 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
669 DMSG("%s, bogus device state\n", __FUNCTION__);
673 /* iso is always one packet per request, that's the only way
674 * we can report per-packet status. that also helps with dma.
676 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
677 && req->req.length > le16_to_cpu
678 (ep->desc->wMaxPacketSize)))
681 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
682 _ep->name, _req, _req->length, _req->buf);
684 local_irq_save(flags);
686 _req->status = -EINPROGRESS;
689 /* kickstart this i/o queue? */
690 if (list_empty(&ep->queue) && !ep->stopped) {
691 if (ep->desc == 0 /* ep0 */) {
692 unsigned length = _req->length;
694 switch (dev->ep0state) {
695 case EP0_IN_DATA_PHASE:
696 dev->stats.write.ops++;
697 if (write_ep0_fifo(ep, req))
701 case EP0_OUT_DATA_PHASE:
702 dev->stats.read.ops++;
704 if (dev->req_config) {
705 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
706 dev->has_cfr ? "" : " raced");
708 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
711 dev->ep0state = EP0_END_XFER;
712 local_irq_restore (flags);
715 if (dev->req_pending)
716 ep0start(dev, UDCCS0_IPR, "OUT");
717 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
718 && read_ep0_fifo(ep, req))) {
726 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
727 local_irq_restore (flags);
730 /* can the FIFO can satisfy the request immediately? */
731 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
732 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
733 && write_fifo(ep, req))
735 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
736 && read_fifo(ep, req)) {
740 if (likely (req && ep->desc))
741 pio_irq_enable(ep->bEndpointAddress);
744 /* pio or dma irq handler advances the queue. */
745 if (likely (req != 0))
746 list_add_tail(&req->queue, &ep->queue);
747 local_irq_restore(flags);
754 * nuke - dequeue ALL requests
756 static void nuke(struct pxa2xx_ep *ep, int status)
758 struct pxa2xx_request *req;
760 /* called with irqs blocked */
761 while (!list_empty(&ep->queue)) {
762 req = list_entry(ep->queue.next,
763 struct pxa2xx_request,
765 done(ep, req, status);
768 pio_irq_disable (ep->bEndpointAddress);
772 /* dequeue JUST ONE request */
773 static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
775 struct pxa2xx_ep *ep;
776 struct pxa2xx_request *req;
779 ep = container_of(_ep, struct pxa2xx_ep, ep);
780 if (!_ep || ep->ep.name == ep0name)
783 local_irq_save(flags);
785 /* make sure it's actually queued on this endpoint */
786 list_for_each_entry (req, &ep->queue, queue) {
787 if (&req->req == _req)
790 if (&req->req != _req) {
791 local_irq_restore(flags);
795 done(ep, req, -ECONNRESET);
797 local_irq_restore(flags);
801 /*-------------------------------------------------------------------------*/
803 static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
805 struct pxa2xx_ep *ep;
808 ep = container_of(_ep, struct pxa2xx_ep, ep);
810 || (!ep->desc && ep->ep.name != ep0name))
811 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
812 DMSG("%s, bad ep\n", __FUNCTION__);
816 /* this path (reset toggle+halt) is needed to implement
817 * SET_INTERFACE on normal hardware. but it can't be
818 * done from software on the PXA UDC, and the hardware
819 * forgets to do it as part of SET_INTERFACE automagic.
821 DMSG("only host can clear %s halt\n", _ep->name);
825 local_irq_save(flags);
827 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
828 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
829 || !list_empty(&ep->queue))) {
830 local_irq_restore(flags);
834 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
835 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
837 /* ep0 needs special care */
839 start_watchdog(ep->dev);
840 ep->dev->req_pending = 0;
841 ep->dev->ep0state = EP0_STALL;
843 /* and bulk/intr endpoints like dropping stalls too */
846 for (i = 0; i < 1000; i += 20) {
847 if (*ep->reg_udccs & UDCCS_BI_SST)
852 local_irq_restore(flags);
854 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
858 static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
860 struct pxa2xx_ep *ep;
862 ep = container_of(_ep, struct pxa2xx_ep, ep);
864 DMSG("%s, bad ep\n", __FUNCTION__);
867 /* pxa can't report unclaimed bytes from IN fifos */
868 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
870 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
871 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
874 return (*ep->reg_ubcr & 0xfff) + 1;
877 static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
879 struct pxa2xx_ep *ep;
881 ep = container_of(_ep, struct pxa2xx_ep, ep);
882 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
883 DMSG("%s, bad ep\n", __FUNCTION__);
887 /* toggle and halt bits stay unchanged */
889 /* for OUT, just read and discard the FIFO contents. */
890 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
891 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
892 (void) *ep->reg_uddr;
896 /* most IN status is the same, but ISO can't stall */
897 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
898 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
903 static struct usb_ep_ops pxa2xx_ep_ops = {
904 .enable = pxa2xx_ep_enable,
905 .disable = pxa2xx_ep_disable,
907 .alloc_request = pxa2xx_ep_alloc_request,
908 .free_request = pxa2xx_ep_free_request,
910 .queue = pxa2xx_ep_queue,
911 .dequeue = pxa2xx_ep_dequeue,
913 .set_halt = pxa2xx_ep_set_halt,
914 .fifo_status = pxa2xx_ep_fifo_status,
915 .fifo_flush = pxa2xx_ep_fifo_flush,
919 /* ---------------------------------------------------------------------------
920 * device-scoped parts of the api to the usb controller hardware
921 * ---------------------------------------------------------------------------
924 static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
926 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
929 static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
931 /* host may not have enabled remote wakeup */
932 if ((UDCCS0 & UDCCS0_DRWF) == 0)
933 return -EHOSTUNREACH;
934 udc_set_mask_UDCCR(UDCCR_RSM);
938 static void stop_activity(struct pxa2xx_udc *, struct usb_gadget_driver *);
939 static void udc_enable (struct pxa2xx_udc *);
940 static void udc_disable(struct pxa2xx_udc *);
942 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
945 static int pullup(struct pxa2xx_udc *udc, int is_active)
947 is_active = is_active && udc->vbus && udc->pullup;
948 DMSG("%s\n", is_active ? "active" : "inactive");
952 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
953 DMSG("disconnect %s\n", udc->driver
954 ? udc->driver->driver.name
956 stop_activity(udc, udc->driver);
963 /* VBUS reporting logically comes from a transceiver */
964 static int pxa2xx_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
966 struct pxa2xx_udc *udc;
968 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
969 udc->vbus = is_active = (is_active != 0);
970 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
971 pullup(udc, is_active);
975 /* drivers may have software control over D+ pullup */
976 static int pxa2xx_udc_pullup(struct usb_gadget *_gadget, int is_active)
978 struct pxa2xx_udc *udc;
980 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
982 /* not all boards support pullup control */
983 if (!udc->mach->udc_command)
986 is_active = (is_active != 0);
987 udc->pullup = is_active;
988 pullup(udc, is_active);
992 static const struct usb_gadget_ops pxa2xx_udc_ops = {
993 .get_frame = pxa2xx_udc_get_frame,
994 .wakeup = pxa2xx_udc_wakeup,
995 .vbus_session = pxa2xx_udc_vbus_session,
996 .pullup = pxa2xx_udc_pullup,
998 // .vbus_draw ... boards may consume current from VBUS, up to
999 // 100-500mA based on config. the 500uA suspend ceiling means
1000 // that exclusively vbus-powered PXA designs violate USB specs.
1003 /*-------------------------------------------------------------------------*/
1005 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
1007 static const char proc_node_name [] = "driver/udc";
1010 udc_proc_read(char *page, char **start, off_t off, int count,
1011 int *eof, void *_dev)
1014 struct pxa2xx_udc *dev = _dev;
1016 unsigned size = count;
1017 unsigned long flags;
1024 local_irq_save(flags);
1026 /* basic device status */
1027 t = scnprintf(next, size, DRIVER_DESC "\n"
1028 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1029 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1030 dev->driver ? dev->driver->driver.name : "(none)",
1031 is_vbus_present() ? "full speed" : "disconnected");
1035 /* registers for device and ep0 */
1036 t = scnprintf(next, size,
1037 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1038 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1043 t = scnprintf(next, size,
1044 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1045 (tmp & UDCCR_REM) ? " rem" : "",
1046 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1047 (tmp & UDCCR_SRM) ? " srm" : "",
1048 (tmp & UDCCR_SUSIR) ? " susir" : "",
1049 (tmp & UDCCR_RESIR) ? " resir" : "",
1050 (tmp & UDCCR_RSM) ? " rsm" : "",
1051 (tmp & UDCCR_UDA) ? " uda" : "",
1052 (tmp & UDCCR_UDE) ? " ude" : "");
1057 t = scnprintf(next, size,
1058 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1059 (tmp & UDCCS0_SA) ? " sa" : "",
1060 (tmp & UDCCS0_RNE) ? " rne" : "",
1061 (tmp & UDCCS0_FST) ? " fst" : "",
1062 (tmp & UDCCS0_SST) ? " sst" : "",
1063 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1064 (tmp & UDCCS0_FTF) ? " ftf" : "",
1065 (tmp & UDCCS0_IPR) ? " ipr" : "",
1066 (tmp & UDCCS0_OPR) ? " opr" : "");
1072 t = scnprintf(next, size,
1073 "udccfr %02X =%s%s\n", tmp,
1074 (tmp & UDCCFR_AREN) ? " aren" : "",
1075 (tmp & UDCCFR_ACM) ? " acm" : "");
1080 if (!is_vbus_present() || !dev->driver)
1083 t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1084 dev->stats.write.bytes, dev->stats.write.ops,
1085 dev->stats.read.bytes, dev->stats.read.ops,
1090 /* dump endpoint queues */
1091 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1092 struct pxa2xx_ep *ep = &dev->ep [i];
1093 struct pxa2xx_request *req;
1096 const struct usb_endpoint_descriptor *d;
1101 tmp = *dev->ep [i].reg_udccs;
1102 t = scnprintf(next, size,
1103 "%s max %d %s udccs %02x irqs %lu\n",
1104 ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
1105 "pio", tmp, ep->pio_irqs);
1106 /* TODO translate all five groups of udccs bits! */
1108 } else /* ep0 should only have one transfer queued */
1109 t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1111 if (t <= 0 || t > size)
1116 if (list_empty(&ep->queue)) {
1117 t = scnprintf(next, size, "\t(nothing queued)\n");
1118 if (t <= 0 || t > size)
1124 list_for_each_entry(req, &ep->queue, queue) {
1125 t = scnprintf(next, size,
1126 "\treq %p len %d/%d buf %p\n",
1127 &req->req, req->req.actual,
1128 req->req.length, req->req.buf);
1129 if (t <= 0 || t > size)
1137 local_irq_restore(flags);
1139 return count - size;
1142 #define create_proc_files() \
1143 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1144 #define remove_proc_files() \
1145 remove_proc_entry(proc_node_name, NULL)
1147 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1149 #define create_proc_files() do {} while (0)
1150 #define remove_proc_files() do {} while (0)
1152 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1154 /*-------------------------------------------------------------------------*/
1157 * udc_disable - disable USB device controller
1159 static void udc_disable(struct pxa2xx_udc *dev)
1161 /* block all irqs */
1162 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1163 UICR0 = UICR1 = 0xff;
1166 /* if hardware supports it, disconnect from usb */
1169 udc_clear_mask_UDCCR(UDCCR_UDE);
1171 #ifdef CONFIG_ARCH_PXA
1172 /* Disable clock for USB device */
1173 pxa_set_cken(CKEN_USB, 0);
1177 dev->gadget.speed = USB_SPEED_UNKNOWN;
1182 * udc_reinit - initialize software state
1184 static void udc_reinit(struct pxa2xx_udc *dev)
1188 /* device/ep0 records init */
1189 INIT_LIST_HEAD (&dev->gadget.ep_list);
1190 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1191 dev->ep0state = EP0_IDLE;
1193 /* basic endpoint records init */
1194 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1195 struct pxa2xx_ep *ep = &dev->ep[i];
1198 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1202 INIT_LIST_HEAD (&ep->queue);
1206 /* the rest was statically initialized, and is read-only */
1209 /* until it's enabled, this UDC should be completely invisible
1212 static void udc_enable (struct pxa2xx_udc *dev)
1214 udc_clear_mask_UDCCR(UDCCR_UDE);
1216 #ifdef CONFIG_ARCH_PXA
1217 /* Enable clock for USB device */
1218 pxa_set_cken(CKEN_USB, 1);
1222 /* try to clear these bits before we enable the udc */
1223 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1226 dev->gadget.speed = USB_SPEED_UNKNOWN;
1227 dev->stats.irqs = 0;
1230 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1232 * - if RESET is already in progress, ack interrupt
1233 * - unmask reset interrupt
1235 udc_set_mask_UDCCR(UDCCR_UDE);
1236 if (!(UDCCR & UDCCR_UDA))
1237 udc_ack_int_UDCCR(UDCCR_RSTIR);
1239 if (dev->has_cfr /* UDC_RES2 is defined */) {
1240 /* pxa255 (a0+) can avoid a set_config race that could
1241 * prevent gadget drivers from configuring correctly
1243 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1245 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1246 * which could result in missing packets and interrupts.
1247 * supposedly one bit per endpoint, controlling whether it
1248 * double buffers or not; ACM/AREN bits fit into the holes.
1249 * zero bits (like USIR0_IRx) disable double buffering.
1255 #ifdef DISABLE_TEST_MODE
1256 /* "test mode" seems to have become the default in later chip
1257 * revs, preventing double buffering (and invalidating docs).
1258 * this EXPERIMENT enables it for bulk endpoints by tweaking
1259 * undefined/reserved register bits (that other drivers clear).
1260 * Belcarra code comments noted this usage.
1262 if (fifo_mode & 1) { /* IN endpoints */
1263 UDC_RES1 |= USIR0_IR1|USIR0_IR6;
1264 UDC_RES2 |= USIR1_IR11;
1266 if (fifo_mode & 2) { /* OUT endpoints */
1267 UDC_RES1 |= USIR0_IR2|USIR0_IR7;
1268 UDC_RES2 |= USIR1_IR12;
1272 /* enable suspend/resume and reset irqs */
1273 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1275 /* enable ep0 irqs */
1276 UICR0 &= ~UICR0_IM0;
1278 /* if hardware supports it, pullup D+ and wait for reset */
1283 /* when a driver is successfully registered, it will receive
1284 * control requests including set_configuration(), which enables
1285 * non-control requests. then usb traffic follows until a
1286 * disconnect is reported. then a host may connect again, or
1287 * the driver might get unbound.
1289 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1291 struct pxa2xx_udc *dev = the_controller;
1295 || driver->speed < USB_SPEED_FULL
1297 || !driver->disconnect
1305 /* first hook up the driver ... */
1306 dev->driver = driver;
1307 dev->gadget.dev.driver = &driver->driver;
1310 retval = device_add (&dev->gadget.dev);
1314 dev->gadget.dev.driver = NULL;
1317 retval = driver->bind(&dev->gadget);
1319 DMSG("bind to driver %s --> error %d\n",
1320 driver->driver.name, retval);
1321 device_del (&dev->gadget.dev);
1325 /* ... then enable host detection and ep0; and we're ready
1326 * for set_configuration as well as eventual disconnect.
1328 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1333 EXPORT_SYMBOL(usb_gadget_register_driver);
1336 stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
1340 /* don't disconnect drivers more than once */
1341 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1343 dev->gadget.speed = USB_SPEED_UNKNOWN;
1345 /* prevent new request submissions, kill any outstanding requests */
1346 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1347 struct pxa2xx_ep *ep = &dev->ep[i];
1350 nuke(ep, -ESHUTDOWN);
1352 del_timer_sync(&dev->timer);
1354 /* report disconnect; the driver is already quiesced */
1356 driver->disconnect(&dev->gadget);
1358 /* re-init driver-visible data structures */
1362 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1364 struct pxa2xx_udc *dev = the_controller;
1368 if (!driver || driver != dev->driver || !driver->unbind)
1371 local_irq_disable();
1373 stop_activity(dev, driver);
1376 driver->unbind(&dev->gadget);
1379 device_del (&dev->gadget.dev);
1381 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1385 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1388 /*-------------------------------------------------------------------------*/
1390 #ifdef CONFIG_ARCH_LUBBOCK
1392 /* Lubbock has separate connect and disconnect irqs. More typical designs
1393 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1397 lubbock_vbus_irq(int irq, void *_dev)
1399 struct pxa2xx_udc *dev = _dev;
1404 case LUBBOCK_USB_IRQ:
1406 disable_irq(LUBBOCK_USB_IRQ);
1407 enable_irq(LUBBOCK_USB_DISC_IRQ);
1409 case LUBBOCK_USB_DISC_IRQ:
1411 disable_irq(LUBBOCK_USB_DISC_IRQ);
1412 enable_irq(LUBBOCK_USB_IRQ);
1418 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1424 static irqreturn_t udc_vbus_irq(int irq, void *_dev)
1426 struct pxa2xx_udc *dev = _dev;
1427 int vbus = gpio_get_value(dev->mach->gpio_vbus);
1429 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1434 /*-------------------------------------------------------------------------*/
1436 static inline void clear_ep_state (struct pxa2xx_udc *dev)
1440 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1441 * fifos, and pending transactions mustn't be continued in any case.
1443 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1444 nuke(&dev->ep[i], -ECONNABORTED);
1447 static void udc_watchdog(unsigned long _dev)
1449 struct pxa2xx_udc *dev = (void *)_dev;
1451 local_irq_disable();
1452 if (dev->ep0state == EP0_STALL
1453 && (UDCCS0 & UDCCS0_FST) == 0
1454 && (UDCCS0 & UDCCS0_SST) == 0) {
1455 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1456 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1457 start_watchdog(dev);
1462 static void handle_ep0 (struct pxa2xx_udc *dev)
1464 u32 udccs0 = UDCCS0;
1465 struct pxa2xx_ep *ep = &dev->ep [0];
1466 struct pxa2xx_request *req;
1468 struct usb_ctrlrequest r;
1473 if (list_empty(&ep->queue))
1476 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
1478 /* clear stall status */
1479 if (udccs0 & UDCCS0_SST) {
1481 UDCCS0 = UDCCS0_SST;
1482 del_timer(&dev->timer);
1486 /* previous request unfinished? non-error iff back-to-back ... */
1487 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1489 del_timer(&dev->timer);
1493 switch (dev->ep0state) {
1495 /* late-breaking status? */
1498 /* start control request? */
1499 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1500 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1505 /* read SETUP packet */
1506 for (i = 0; i < 8; i++) {
1507 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1509 DMSG("SETUP %d!\n", i);
1512 u.raw [i] = (u8) UDDR0;
1514 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1518 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1519 u.r.bRequestType, u.r.bRequest,
1520 le16_to_cpu(u.r.wValue),
1521 le16_to_cpu(u.r.wIndex),
1522 le16_to_cpu(u.r.wLength));
1524 /* cope with automagic for some standard requests. */
1525 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1526 == USB_TYPE_STANDARD;
1527 dev->req_config = 0;
1528 dev->req_pending = 1;
1529 switch (u.r.bRequest) {
1530 /* hardware restricts gadget drivers here! */
1531 case USB_REQ_SET_CONFIGURATION:
1532 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1533 /* reflect hardware's automagic
1534 * up to the gadget driver.
1537 dev->req_config = 1;
1538 clear_ep_state(dev);
1539 /* if !has_cfr, there's no synch
1540 * else use AREN (later) not SA|OPR
1541 * USIR0_IR0 acts edge sensitive
1545 /* ... and here, even more ... */
1546 case USB_REQ_SET_INTERFACE:
1547 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1548 /* udc hardware is broken by design:
1549 * - altsetting may only be zero;
1550 * - hw resets all interfaces' eps;
1551 * - ep reset doesn't include halt(?).
1553 DMSG("broken set_interface (%d/%d)\n",
1554 le16_to_cpu(u.r.wIndex),
1555 le16_to_cpu(u.r.wValue));
1559 /* hardware was supposed to hide this */
1560 case USB_REQ_SET_ADDRESS:
1561 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1562 ep0start(dev, 0, "address");
1568 if (u.r.bRequestType & USB_DIR_IN)
1569 dev->ep0state = EP0_IN_DATA_PHASE;
1571 dev->ep0state = EP0_OUT_DATA_PHASE;
1573 i = dev->driver->setup(&dev->gadget, &u.r);
1575 /* hardware automagic preventing STALL... */
1576 if (dev->req_config) {
1577 /* hardware sometimes neglects to tell
1578 * tell us about config change events,
1579 * so later ones may fail...
1581 WARN("config change %02x fail %d?\n",
1584 /* TODO experiment: if has_cfr,
1585 * hardware didn't ACK; maybe we
1586 * could actually STALL!
1589 DBG(DBG_VERBOSE, "protocol STALL, "
1590 "%02x err %d\n", UDCCS0, i);
1592 /* the watchdog timer helps deal with cases
1593 * where udc seems to clear FST wrongly, and
1594 * then NAKs instead of STALLing.
1596 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1597 start_watchdog(dev);
1598 dev->ep0state = EP0_STALL;
1600 /* deferred i/o == no response yet */
1601 } else if (dev->req_pending) {
1602 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1603 || dev->req_std || u.r.wLength))
1604 ep0start(dev, 0, "defer");
1606 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1609 /* expect at least one data or status stage irq */
1612 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1613 == (UDCCS0_OPR|UDCCS0_SA))) {
1616 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1617 * still observed on a pxa255 a0.
1619 DBG(DBG_VERBOSE, "e131\n");
1622 /* read SETUP data, but don't trust it too much */
1623 for (i = 0; i < 8; i++)
1624 u.raw [i] = (u8) UDDR0;
1625 if ((u.r.bRequestType & USB_RECIP_MASK)
1628 if (u.word [0] == 0 && u.word [1] == 0)
1632 /* some random early IRQ:
1635 * - OPR got set, without SA (likely status stage)
1637 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1640 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1641 if (udccs0 & UDCCS0_OPR) {
1642 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1643 DBG(DBG_VERBOSE, "ep0in premature status\n");
1647 } else /* irq was IPR clearing */ {
1649 /* this IN packet might finish the request */
1650 (void) write_ep0_fifo(ep, req);
1651 } /* else IN token before response was written */
1654 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1655 if (udccs0 & UDCCS0_OPR) {
1657 /* this OUT packet might finish the request */
1658 if (read_ep0_fifo(ep, req))
1660 /* else more OUT packets expected */
1661 } /* else OUT token before read was issued */
1662 } else /* irq was IPR clearing */ {
1663 DBG(DBG_VERBOSE, "ep0out premature status\n");
1672 /* ack control-IN status (maybe in-zlp was skipped)
1673 * also appears after some config change events.
1675 if (udccs0 & UDCCS0_OPR)
1676 UDCCS0 = UDCCS0_OPR;
1680 UDCCS0 = UDCCS0_FST;
1686 static void handle_ep(struct pxa2xx_ep *ep)
1688 struct pxa2xx_request *req;
1689 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1695 if (likely (!list_empty(&ep->queue)))
1696 req = list_entry(ep->queue.next,
1697 struct pxa2xx_request, queue);
1701 // TODO check FST handling
1703 udccs = *ep->reg_udccs;
1704 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1706 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1707 tmp |= UDCCS_BI_SST;
1710 *ep->reg_udccs = tmp;
1711 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1712 completed = write_fifo(ep, req);
1714 } else { /* irq from RPC (or for ISO, ROF) */
1715 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1716 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1718 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1721 *ep->reg_udccs = tmp;
1723 /* fifos can hold packets, ready for reading... */
1725 completed = read_fifo(ep, req);
1727 pio_irq_disable (ep->bEndpointAddress);
1730 } while (completed);
1734 * pxa2xx_udc_irq - interrupt handler
1736 * avoid delays in ep0 processing. the control handshaking isn't always
1737 * under software control (pxa250c0 and the pxa255 are better), and delays
1738 * could cause usb protocol errors.
1741 pxa2xx_udc_irq(int irq, void *_dev)
1743 struct pxa2xx_udc *dev = _dev;
1752 /* SUSpend Interrupt Request */
1753 if (unlikely(udccr & UDCCR_SUSIR)) {
1754 udc_ack_int_UDCCR(UDCCR_SUSIR);
1756 DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present()
1757 ? "" : "+disconnect");
1759 if (!is_vbus_present())
1760 stop_activity(dev, dev->driver);
1761 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
1763 && dev->driver->suspend)
1764 dev->driver->suspend(&dev->gadget);
1768 /* RESume Interrupt Request */
1769 if (unlikely(udccr & UDCCR_RESIR)) {
1770 udc_ack_int_UDCCR(UDCCR_RESIR);
1772 DBG(DBG_VERBOSE, "USB resume\n");
1774 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1776 && dev->driver->resume
1777 && is_vbus_present())
1778 dev->driver->resume(&dev->gadget);
1781 /* ReSeT Interrupt Request - USB reset */
1782 if (unlikely(udccr & UDCCR_RSTIR)) {
1783 udc_ack_int_UDCCR(UDCCR_RSTIR);
1786 if ((UDCCR & UDCCR_UDA) == 0) {
1787 DBG(DBG_VERBOSE, "USB reset start\n");
1789 /* reset driver and endpoints,
1790 * in case that's not yet done
1792 stop_activity (dev, dev->driver);
1795 DBG(DBG_VERBOSE, "USB reset end\n");
1796 dev->gadget.speed = USB_SPEED_FULL;
1797 memset(&dev->stats, 0, sizeof dev->stats);
1798 /* driver and endpoints are still reset */
1802 u32 usir0 = USIR0 & ~UICR0;
1803 u32 usir1 = USIR1 & ~UICR1;
1806 if (unlikely (!usir0 && !usir1))
1809 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1811 /* control traffic */
1812 if (usir0 & USIR0_IR0) {
1813 dev->ep[0].pio_irqs++;
1818 /* endpoint data transfers */
1819 for (i = 0; i < 8; i++) {
1822 if (i && (usir0 & tmp)) {
1823 handle_ep(&dev->ep[i]);
1828 handle_ep(&dev->ep[i+8]);
1835 /* we could also ask for 1 msec SOF (SIR) interrupts */
1841 /*-------------------------------------------------------------------------*/
1843 static void nop_release (struct device *dev)
1845 DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
1848 /* this uses load-time allocation and initialization (instead of
1849 * doing it at run-time) to save code, eliminate fault paths, and
1850 * be more obviously correct.
1852 static struct pxa2xx_udc memory = {
1854 .ops = &pxa2xx_udc_ops,
1855 .ep0 = &memory.ep[0].ep,
1856 .name = driver_name,
1859 .release = nop_release,
1863 /* control endpoint */
1867 .ops = &pxa2xx_ep_ops,
1868 .maxpacket = EP0_FIFO_SIZE,
1871 .reg_udccs = &UDCCS0,
1875 /* first group of endpoints */
1878 .name = "ep1in-bulk",
1879 .ops = &pxa2xx_ep_ops,
1880 .maxpacket = BULK_FIFO_SIZE,
1883 .fifo_size = BULK_FIFO_SIZE,
1884 .bEndpointAddress = USB_DIR_IN | 1,
1885 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1886 .reg_udccs = &UDCCS1,
1891 .name = "ep2out-bulk",
1892 .ops = &pxa2xx_ep_ops,
1893 .maxpacket = BULK_FIFO_SIZE,
1896 .fifo_size = BULK_FIFO_SIZE,
1897 .bEndpointAddress = 2,
1898 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1899 .reg_udccs = &UDCCS2,
1903 #ifndef CONFIG_USB_PXA2XX_SMALL
1906 .name = "ep3in-iso",
1907 .ops = &pxa2xx_ep_ops,
1908 .maxpacket = ISO_FIFO_SIZE,
1911 .fifo_size = ISO_FIFO_SIZE,
1912 .bEndpointAddress = USB_DIR_IN | 3,
1913 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1914 .reg_udccs = &UDCCS3,
1919 .name = "ep4out-iso",
1920 .ops = &pxa2xx_ep_ops,
1921 .maxpacket = ISO_FIFO_SIZE,
1924 .fifo_size = ISO_FIFO_SIZE,
1925 .bEndpointAddress = 4,
1926 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1927 .reg_udccs = &UDCCS4,
1933 .name = "ep5in-int",
1934 .ops = &pxa2xx_ep_ops,
1935 .maxpacket = INT_FIFO_SIZE,
1938 .fifo_size = INT_FIFO_SIZE,
1939 .bEndpointAddress = USB_DIR_IN | 5,
1940 .bmAttributes = USB_ENDPOINT_XFER_INT,
1941 .reg_udccs = &UDCCS5,
1945 /* second group of endpoints */
1948 .name = "ep6in-bulk",
1949 .ops = &pxa2xx_ep_ops,
1950 .maxpacket = BULK_FIFO_SIZE,
1953 .fifo_size = BULK_FIFO_SIZE,
1954 .bEndpointAddress = USB_DIR_IN | 6,
1955 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1956 .reg_udccs = &UDCCS6,
1961 .name = "ep7out-bulk",
1962 .ops = &pxa2xx_ep_ops,
1963 .maxpacket = BULK_FIFO_SIZE,
1966 .fifo_size = BULK_FIFO_SIZE,
1967 .bEndpointAddress = 7,
1968 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1969 .reg_udccs = &UDCCS7,
1975 .name = "ep8in-iso",
1976 .ops = &pxa2xx_ep_ops,
1977 .maxpacket = ISO_FIFO_SIZE,
1980 .fifo_size = ISO_FIFO_SIZE,
1981 .bEndpointAddress = USB_DIR_IN | 8,
1982 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1983 .reg_udccs = &UDCCS8,
1988 .name = "ep9out-iso",
1989 .ops = &pxa2xx_ep_ops,
1990 .maxpacket = ISO_FIFO_SIZE,
1993 .fifo_size = ISO_FIFO_SIZE,
1994 .bEndpointAddress = 9,
1995 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1996 .reg_udccs = &UDCCS9,
2002 .name = "ep10in-int",
2003 .ops = &pxa2xx_ep_ops,
2004 .maxpacket = INT_FIFO_SIZE,
2007 .fifo_size = INT_FIFO_SIZE,
2008 .bEndpointAddress = USB_DIR_IN | 10,
2009 .bmAttributes = USB_ENDPOINT_XFER_INT,
2010 .reg_udccs = &UDCCS10,
2011 .reg_uddr = &UDDR10,
2014 /* third group of endpoints */
2017 .name = "ep11in-bulk",
2018 .ops = &pxa2xx_ep_ops,
2019 .maxpacket = BULK_FIFO_SIZE,
2022 .fifo_size = BULK_FIFO_SIZE,
2023 .bEndpointAddress = USB_DIR_IN | 11,
2024 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2025 .reg_udccs = &UDCCS11,
2026 .reg_uddr = &UDDR11,
2030 .name = "ep12out-bulk",
2031 .ops = &pxa2xx_ep_ops,
2032 .maxpacket = BULK_FIFO_SIZE,
2035 .fifo_size = BULK_FIFO_SIZE,
2036 .bEndpointAddress = 12,
2037 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2038 .reg_udccs = &UDCCS12,
2039 .reg_ubcr = &UBCR12,
2040 .reg_uddr = &UDDR12,
2044 .name = "ep13in-iso",
2045 .ops = &pxa2xx_ep_ops,
2046 .maxpacket = ISO_FIFO_SIZE,
2049 .fifo_size = ISO_FIFO_SIZE,
2050 .bEndpointAddress = USB_DIR_IN | 13,
2051 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2052 .reg_udccs = &UDCCS13,
2053 .reg_uddr = &UDDR13,
2057 .name = "ep14out-iso",
2058 .ops = &pxa2xx_ep_ops,
2059 .maxpacket = ISO_FIFO_SIZE,
2062 .fifo_size = ISO_FIFO_SIZE,
2063 .bEndpointAddress = 14,
2064 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2065 .reg_udccs = &UDCCS14,
2066 .reg_ubcr = &UBCR14,
2067 .reg_uddr = &UDDR14,
2071 .name = "ep15in-int",
2072 .ops = &pxa2xx_ep_ops,
2073 .maxpacket = INT_FIFO_SIZE,
2076 .fifo_size = INT_FIFO_SIZE,
2077 .bEndpointAddress = USB_DIR_IN | 15,
2078 .bmAttributes = USB_ENDPOINT_XFER_INT,
2079 .reg_udccs = &UDCCS15,
2080 .reg_uddr = &UDDR15,
2082 #endif /* !CONFIG_USB_PXA2XX_SMALL */
2085 #define CP15R0_VENDOR_MASK 0xffffe000
2087 #if defined(CONFIG_ARCH_PXA)
2088 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2090 #elif defined(CONFIG_ARCH_IXP4XX)
2091 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2095 #define CP15R0_PROD_MASK 0x000003f0
2096 #define PXA25x 0x00000100 /* and PXA26x */
2097 #define PXA210 0x00000120
2099 #define CP15R0_REV_MASK 0x0000000f
2101 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2103 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2104 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2105 #define PXA250_B2 0x00000104
2106 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2107 #define PXA250_B0 0x00000102
2108 #define PXA250_A1 0x00000101
2109 #define PXA250_A0 0x00000100
2111 #define PXA210_C0 0x00000125
2112 #define PXA210_B2 0x00000124
2113 #define PXA210_B1 0x00000123
2114 #define PXA210_B0 0x00000122
2115 #define IXP425_A0 0x000001c1
2116 #define IXP425_B0 0x000001f1
2117 #define IXP465_AD 0x00000200
2120 * probe - binds to the platform device
2122 static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2124 struct pxa2xx_udc *dev = &memory;
2125 int retval, vbus_irq, irq;
2128 /* insist on Intel/ARM/XScale */
2129 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2130 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2131 printk(KERN_ERR "%s: not XScale!\n", driver_name);
2135 /* trigger chiprev-specific logic */
2136 switch (chiprev & CP15R0_PRODREV_MASK) {
2137 #if defined(CONFIG_ARCH_PXA)
2143 /* A0/A1 "not released"; ep 13, 15 unusable */
2145 case PXA250_B2: case PXA210_B2:
2146 case PXA250_B1: case PXA210_B1:
2147 case PXA250_B0: case PXA210_B0:
2148 /* OUT-DMA is broken ... */
2150 case PXA250_C0: case PXA210_C0:
2152 #elif defined(CONFIG_ARCH_IXP4XX)
2160 printk(KERN_ERR "%s: unrecognized processor: %08x\n",
2161 driver_name, chiprev);
2162 /* iop3xx, ixp4xx, ... */
2166 irq = platform_get_irq(pdev, 0);
2170 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
2171 dev->has_cfr ? "" : " (!cfr)",
2175 /* other non-static parts of init */
2176 dev->dev = &pdev->dev;
2177 dev->mach = pdev->dev.platform_data;
2179 if (dev->mach->gpio_vbus) {
2180 if ((retval = gpio_request(dev->mach->gpio_vbus,
2181 "pxa2xx_udc GPIO VBUS"))) {
2183 "can't get vbus gpio %d, err: %d\n",
2184 dev->mach->gpio_vbus, retval);
2187 gpio_direction_input(dev->mach->gpio_vbus);
2188 vbus_irq = gpio_to_irq(dev->mach->gpio_vbus);
2189 set_irq_type(vbus_irq, IRQT_BOTHEDGE);
2193 if (dev->mach->gpio_pullup) {
2194 if ((retval = gpio_request(dev->mach->gpio_pullup,
2195 "pca2xx_udc GPIO PULLUP"))) {
2197 "can't get pullup gpio %d, err: %d\n",
2198 dev->mach->gpio_pullup, retval);
2199 if (dev->mach->gpio_vbus)
2200 gpio_free(dev->mach->gpio_vbus);
2203 gpio_direction_output(dev->mach->gpio_pullup, 0);
2206 init_timer(&dev->timer);
2207 dev->timer.function = udc_watchdog;
2208 dev->timer.data = (unsigned long) dev;
2210 device_initialize(&dev->gadget.dev);
2211 dev->gadget.dev.parent = &pdev->dev;
2212 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2214 the_controller = dev;
2215 platform_set_drvdata(pdev, dev);
2220 dev->vbus = is_vbus_present();
2222 /* irq setup after old hardware state is cleaned up */
2223 retval = request_irq(irq, pxa2xx_udc_irq,
2224 IRQF_DISABLED, driver_name, dev);
2226 printk(KERN_ERR "%s: can't get irq %d, err %d\n",
2227 driver_name, irq, retval);
2228 if (dev->mach->gpio_pullup)
2229 gpio_free(dev->mach->gpio_pullup);
2230 if (dev->mach->gpio_vbus)
2231 gpio_free(dev->mach->gpio_vbus);
2236 #ifdef CONFIG_ARCH_LUBBOCK
2237 if (machine_is_lubbock()) {
2238 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2240 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2243 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2244 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2247 if (dev->mach->gpio_pullup)
2248 gpio_free(dev->mach->gpio_pullup);
2249 if (dev->mach->gpio_vbus)
2250 gpio_free(dev->mach->gpio_vbus);
2253 retval = request_irq(LUBBOCK_USB_IRQ,
2255 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2258 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2259 driver_name, LUBBOCK_USB_IRQ, retval);
2260 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2266 retval = request_irq(vbus_irq, udc_vbus_irq,
2267 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2270 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2271 driver_name, vbus_irq, retval);
2273 if (dev->mach->gpio_pullup)
2274 gpio_free(dev->mach->gpio_pullup);
2275 if (dev->mach->gpio_vbus)
2276 gpio_free(dev->mach->gpio_vbus);
2280 create_proc_files();
2285 static void pxa2xx_udc_shutdown(struct platform_device *_dev)
2290 static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
2292 struct pxa2xx_udc *dev = platform_get_drvdata(pdev);
2298 remove_proc_files();
2301 free_irq(platform_get_irq(pdev, 0), dev);
2304 #ifdef CONFIG_ARCH_LUBBOCK
2305 if (machine_is_lubbock()) {
2306 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2307 free_irq(LUBBOCK_USB_IRQ, dev);
2310 if (dev->mach->gpio_vbus) {
2311 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev);
2312 gpio_free(dev->mach->gpio_vbus);
2314 if (dev->mach->gpio_pullup)
2315 gpio_free(dev->mach->gpio_pullup);
2317 platform_set_drvdata(pdev, NULL);
2318 the_controller = NULL;
2322 /*-------------------------------------------------------------------------*/
2326 /* USB suspend (controlled by the host) and system suspend (controlled
2327 * by the PXA) don't necessarily work well together. If USB is active,
2328 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2329 * mode, or any deeper PM saving state.
2331 * For now, we punt and forcibly disconnect from the USB host when PXA
2332 * enters any suspend state. While we're disconnected, we always disable
2333 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2334 * Boards without software pullup control shouldn't use those states.
2335 * VBUS IRQs should probably be ignored so that the PXA device just acts
2336 * "dead" to USB hosts until system resume.
2338 static int pxa2xx_udc_suspend(struct platform_device *dev, pm_message_t state)
2340 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
2342 if (!udc->mach->udc_command)
2343 WARN("USB host won't detect disconnect!\n");
2349 static int pxa2xx_udc_resume(struct platform_device *dev)
2351 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
2359 #define pxa2xx_udc_suspend NULL
2360 #define pxa2xx_udc_resume NULL
2363 /*-------------------------------------------------------------------------*/
2365 static struct platform_driver udc_driver = {
2366 .shutdown = pxa2xx_udc_shutdown,
2367 .remove = __exit_p(pxa2xx_udc_remove),
2368 .suspend = pxa2xx_udc_suspend,
2369 .resume = pxa2xx_udc_resume,
2371 .owner = THIS_MODULE,
2372 .name = "pxa2xx-udc",
2376 static int __init udc_init(void)
2378 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2379 return platform_driver_probe(&udc_driver, pxa2xx_udc_probe);
2381 module_init(udc_init);
2383 static void __exit udc_exit(void)
2385 platform_driver_unregister(&udc_driver);
2387 module_exit(udc_exit);
2389 MODULE_DESCRIPTION(DRIVER_DESC);
2390 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2391 MODULE_LICENSE("GPL");