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>
46 #include <linux/clk.h>
47 #include <linux/err.h>
49 #include <asm/byteorder.h>
53 #include <asm/system.h>
54 #include <asm/mach-types.h>
55 #include <asm/unaligned.h>
56 #include <asm/hardware.h>
58 #include <linux/usb/ch9.h>
59 #include <linux/usb/gadget.h>
61 #include <asm/mach/udc_pxa2xx.h>
65 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
66 * series processors. The UDC for the IXP 4xx series is very similar.
67 * There are fifteen endpoints, in addition to ep0.
69 * Such controller drivers work with a gadget driver. The gadget driver
70 * returns descriptors, implements configuration and data protocols used
71 * by the host to interact with this device, and allocates endpoints to
72 * the different protocol interfaces. The controller driver virtualizes
73 * usb hardware so that the gadget drivers will be more portable.
75 * This UDC hardware wants to implement a bit too much USB protocol, so
76 * it constrains the sorts of USB configuration change events that work.
77 * The errata for these chips are misleading; some "fixed" bugs from
78 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
80 * Note that the UDC hardware supports DMA (except on IXP) but that's
81 * not used here. IN-DMA (to host) is simple enough, when the data is
82 * suitably aligned (16 bytes) ... the network stack doesn't do that,
83 * other software can. OUT-DMA is buggy in most chip versions, as well
84 * as poorly designed (data toggle not automatic). So this driver won't
85 * bother using DMA. (Mostly-working IN-DMA support was available in
86 * kernels before 2.6.23, but was never enabled or well tested.)
89 #define DRIVER_VERSION "30-June-2007"
90 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
93 static const char driver_name [] = "pxa2xx_udc";
95 static const char ep0name [] = "ep0";
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 /* ---------------------------------------------------------------------------
117 * endpoint related parts of the api to the usb controller hardware,
118 * used by gadget driver; and the inner talker-to-hardware core.
119 * ---------------------------------------------------------------------------
122 static void pxa2xx_ep_fifo_flush (struct usb_ep *ep);
123 static void nuke (struct pxa2xx_ep *, int status);
125 /* one GPIO should be used to detect VBUS from the host */
126 static int is_vbus_present(void)
128 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
131 return gpio_get_value(mach->gpio_vbus);
132 if (mach->udc_is_connected)
133 return mach->udc_is_connected();
137 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
138 static void pullup_off(void)
140 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
142 if (mach->gpio_pullup)
143 gpio_set_value(mach->gpio_pullup, 0);
144 else if (mach->udc_command)
145 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
148 static void pullup_on(void)
150 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
152 if (mach->gpio_pullup)
153 gpio_set_value(mach->gpio_pullup, 1);
154 else if (mach->udc_command)
155 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
158 static void pio_irq_enable(int bEndpointAddress)
160 bEndpointAddress &= 0xf;
161 if (bEndpointAddress < 8)
162 UICR0 &= ~(1 << bEndpointAddress);
164 bEndpointAddress -= 8;
165 UICR1 &= ~(1 << bEndpointAddress);
169 static void pio_irq_disable(int bEndpointAddress)
171 bEndpointAddress &= 0xf;
172 if (bEndpointAddress < 8)
173 UICR0 |= 1 << bEndpointAddress;
175 bEndpointAddress -= 8;
176 UICR1 |= 1 << bEndpointAddress;
180 /* The UDCCR reg contains mask and interrupt status bits,
181 * so using '|=' isn't safe as it may ack an interrupt.
183 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
185 static inline void udc_set_mask_UDCCR(int mask)
187 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
190 static inline void udc_clear_mask_UDCCR(int mask)
192 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
195 static inline void udc_ack_int_UDCCR(int mask)
197 /* udccr contains the bits we dont want to change */
198 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
200 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
204 * endpoint enable/disable
206 * we need to verify the descriptors used to enable endpoints. since pxa2xx
207 * endpoint configurations are fixed, and are pretty much always enabled,
208 * there's not a lot to manage here.
210 * because pxa2xx can't selectively initialize bulk (or interrupt) endpoints,
211 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
212 * for a single interface (with only the default altsetting) and for gadget
213 * drivers that don't halt endpoints (not reset by set_interface). that also
214 * means that if you use ISO, you must violate the USB spec rule that all
215 * iso endpoints must be in non-default altsettings.
217 static int pxa2xx_ep_enable (struct usb_ep *_ep,
218 const struct usb_endpoint_descriptor *desc)
220 struct pxa2xx_ep *ep;
221 struct pxa2xx_udc *dev;
223 ep = container_of (_ep, struct pxa2xx_ep, ep);
224 if (!_ep || !desc || ep->desc || _ep->name == ep0name
225 || desc->bDescriptorType != USB_DT_ENDPOINT
226 || ep->bEndpointAddress != desc->bEndpointAddress
227 || ep->fifo_size < le16_to_cpu
228 (desc->wMaxPacketSize)) {
229 DMSG("%s, bad ep or descriptor\n", __FUNCTION__);
233 /* xfer types must match, except that interrupt ~= bulk */
234 if (ep->bmAttributes != desc->bmAttributes
235 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
236 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
237 DMSG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
241 /* hardware _could_ do smaller, but driver doesn't */
242 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
243 && le16_to_cpu (desc->wMaxPacketSize)
245 || !desc->wMaxPacketSize) {
246 DMSG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
251 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
252 DMSG("%s, bogus device state\n", __FUNCTION__);
259 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
261 /* flush fifo (mostly for OUT buffers) */
262 pxa2xx_ep_fifo_flush (_ep);
264 /* ... reset halt state too, if we could ... */
266 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
270 static int pxa2xx_ep_disable (struct usb_ep *_ep)
272 struct pxa2xx_ep *ep;
275 ep = container_of (_ep, struct pxa2xx_ep, ep);
276 if (!_ep || !ep->desc) {
277 DMSG("%s, %s not enabled\n", __FUNCTION__,
278 _ep ? ep->ep.name : NULL);
281 local_irq_save(flags);
283 nuke (ep, -ESHUTDOWN);
285 /* flush fifo (mostly for IN buffers) */
286 pxa2xx_ep_fifo_flush (_ep);
291 local_irq_restore(flags);
292 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
296 /*-------------------------------------------------------------------------*/
298 /* for the pxa2xx, these can just wrap kmalloc/kfree. gadget drivers
299 * must still pass correctly initialized endpoints, since other controller
300 * drivers may care about how it's currently set up (dma issues etc).
304 * pxa2xx_ep_alloc_request - allocate a request data structure
306 static struct usb_request *
307 pxa2xx_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
309 struct pxa2xx_request *req;
311 req = kzalloc(sizeof(*req), gfp_flags);
315 INIT_LIST_HEAD (&req->queue);
321 * pxa2xx_ep_free_request - deallocate a request data structure
324 pxa2xx_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
326 struct pxa2xx_request *req;
328 req = container_of (_req, struct pxa2xx_request, req);
329 WARN_ON (!list_empty (&req->queue));
333 /*-------------------------------------------------------------------------*/
336 * done - retire a request; caller blocked irqs
338 static void done(struct pxa2xx_ep *ep, struct pxa2xx_request *req, int status)
340 unsigned stopped = ep->stopped;
342 list_del_init(&req->queue);
344 if (likely (req->req.status == -EINPROGRESS))
345 req->req.status = status;
347 status = req->req.status;
349 if (status && status != -ESHUTDOWN)
350 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
351 ep->ep.name, &req->req, status,
352 req->req.actual, req->req.length);
354 /* don't modify queue heads during completion callback */
356 req->req.complete(&ep->ep, &req->req);
357 ep->stopped = stopped;
361 static inline void ep0_idle (struct pxa2xx_udc *dev)
363 dev->ep0state = EP0_IDLE;
367 write_packet(volatile u32 *uddr, struct pxa2xx_request *req, unsigned max)
370 unsigned length, count;
372 buf = req->req.buf + req->req.actual;
375 /* how big will this packet be? */
376 length = min(req->req.length - req->req.actual, max);
377 req->req.actual += length;
380 while (likely(count--))
387 * write to an IN endpoint fifo, as many packets as possible.
388 * irqs will use this to write the rest later.
389 * caller guarantees at least one packet buffer is ready (or a zlp).
392 write_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
396 max = le16_to_cpu(ep->desc->wMaxPacketSize);
399 int is_last, is_short;
401 count = write_packet(ep->reg_uddr, req, max);
403 /* last packet is usually short (or a zlp) */
404 if (unlikely (count != max))
405 is_last = is_short = 1;
407 if (likely(req->req.length != req->req.actual)
412 /* interrupt/iso maxpacket may not fill the fifo */
413 is_short = unlikely (max < ep->fifo_size);
416 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
418 is_last ? "/L" : "", is_short ? "/S" : "",
419 req->req.length - req->req.actual, req);
421 /* let loose that packet. maybe try writing another one,
422 * double buffering might work. TSP, TPC, and TFS
423 * bit values are the same for all normal IN endpoints.
425 *ep->reg_udccs = UDCCS_BI_TPC;
427 *ep->reg_udccs = UDCCS_BI_TSP;
429 /* requests complete when all IN data is in the FIFO */
432 if (list_empty(&ep->queue))
433 pio_irq_disable (ep->bEndpointAddress);
437 // TODO experiment: how robust can fifo mode tweaking be?
438 // double buffering is off in the default fifo mode, which
439 // prevents TFS from being set here.
441 } while (*ep->reg_udccs & UDCCS_BI_TFS);
445 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
446 * ep0 data stage. these chips want very simple state transitions.
449 void ep0start(struct pxa2xx_udc *dev, u32 flags, const char *tag)
451 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
453 dev->req_pending = 0;
454 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
455 __FUNCTION__, tag, UDCCS0, flags);
459 write_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
464 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
465 ep->dev->stats.write.bytes += count;
467 /* last packet "must be" short (or a zlp) */
468 is_short = (count != EP0_FIFO_SIZE);
470 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
471 req->req.length - req->req.actual, req);
473 if (unlikely (is_short)) {
474 if (ep->dev->req_pending)
475 ep0start(ep->dev, UDCCS0_IPR, "short IN");
479 count = req->req.length;
482 #ifndef CONFIG_ARCH_IXP4XX
484 /* This seems to get rid of lost status irqs in some cases:
485 * host responds quickly, or next request involves config
486 * change automagic, or should have been hidden, or ...
488 * FIXME get rid of all udelays possible...
490 if (count >= EP0_FIFO_SIZE) {
493 if ((UDCCS0 & UDCCS0_OPR) != 0) {
494 /* clear OPR, generate ack */
504 } else if (ep->dev->req_pending)
505 ep0start(ep->dev, 0, "IN");
511 * read_fifo - unload packet(s) from the fifo we use for usb OUT
512 * transfers and put them into the request. caller should have made
513 * sure there's at least one packet ready.
515 * returns true if the request completed because of short packet or the
516 * request buffer having filled (and maybe overran till end-of-packet).
519 read_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
524 unsigned bufferspace, count, is_short;
526 /* make sure there's a packet in the FIFO.
527 * UDCCS_{BO,IO}_RPC are all the same bit value.
528 * UDCCS_{BO,IO}_RNE are all the same bit value.
530 udccs = *ep->reg_udccs;
531 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
533 buf = req->req.buf + req->req.actual;
535 bufferspace = req->req.length - req->req.actual;
537 /* read all bytes from this packet */
538 if (likely (udccs & UDCCS_BO_RNE)) {
539 count = 1 + (0x0ff & *ep->reg_ubcr);
540 req->req.actual += min (count, bufferspace);
543 is_short = (count < ep->ep.maxpacket);
544 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
545 ep->ep.name, udccs, count,
546 is_short ? "/S" : "",
547 req, req->req.actual, req->req.length);
548 while (likely (count-- != 0)) {
549 u8 byte = (u8) *ep->reg_uddr;
551 if (unlikely (bufferspace == 0)) {
552 /* this happens when the driver's buffer
553 * is smaller than what the host sent.
554 * discard the extra data.
556 if (req->req.status != -EOVERFLOW)
557 DMSG("%s overflow %d\n",
559 req->req.status = -EOVERFLOW;
565 *ep->reg_udccs = UDCCS_BO_RPC;
566 /* RPC/RSP/RNE could now reflect the other packet buffer */
568 /* iso is one request per packet */
569 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
570 if (udccs & UDCCS_IO_ROF)
571 req->req.status = -EHOSTUNREACH;
572 /* more like "is_done" */
577 if (is_short || req->req.actual == req->req.length) {
579 if (list_empty(&ep->queue))
580 pio_irq_disable (ep->bEndpointAddress);
584 /* finished that packet. the next one may be waiting... */
590 * special ep0 version of the above. no UBCR0 or double buffering; status
591 * handshaking is magic. most device protocols don't need control-OUT.
592 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
593 * protocols do use them.
596 read_ep0_fifo (struct pxa2xx_ep *ep, struct pxa2xx_request *req)
599 unsigned bufferspace;
601 buf = req->req.buf + req->req.actual;
602 bufferspace = req->req.length - req->req.actual;
604 while (UDCCS0 & UDCCS0_RNE) {
607 if (unlikely (bufferspace == 0)) {
608 /* this happens when the driver's buffer
609 * is smaller than what the host sent.
610 * discard the extra data.
612 if (req->req.status != -EOVERFLOW)
613 DMSG("%s overflow\n", ep->ep.name);
614 req->req.status = -EOVERFLOW;
622 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
625 if (req->req.actual >= req->req.length)
628 /* finished that packet. the next one may be waiting... */
632 /*-------------------------------------------------------------------------*/
635 pxa2xx_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
637 struct pxa2xx_request *req;
638 struct pxa2xx_ep *ep;
639 struct pxa2xx_udc *dev;
642 req = container_of(_req, struct pxa2xx_request, req);
643 if (unlikely (!_req || !_req->complete || !_req->buf
644 || !list_empty(&req->queue))) {
645 DMSG("%s, bad params\n", __FUNCTION__);
649 ep = container_of(_ep, struct pxa2xx_ep, ep);
650 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
651 DMSG("%s, bad ep\n", __FUNCTION__);
656 if (unlikely (!dev->driver
657 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
658 DMSG("%s, bogus device state\n", __FUNCTION__);
662 /* iso is always one packet per request, that's the only way
663 * we can report per-packet status. that also helps with dma.
665 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
666 && req->req.length > le16_to_cpu
667 (ep->desc->wMaxPacketSize)))
670 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
671 _ep->name, _req, _req->length, _req->buf);
673 local_irq_save(flags);
675 _req->status = -EINPROGRESS;
678 /* kickstart this i/o queue? */
679 if (list_empty(&ep->queue) && !ep->stopped) {
680 if (ep->desc == 0 /* ep0 */) {
681 unsigned length = _req->length;
683 switch (dev->ep0state) {
684 case EP0_IN_DATA_PHASE:
685 dev->stats.write.ops++;
686 if (write_ep0_fifo(ep, req))
690 case EP0_OUT_DATA_PHASE:
691 dev->stats.read.ops++;
693 if (dev->req_config) {
694 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
695 dev->has_cfr ? "" : " raced");
697 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
700 dev->ep0state = EP0_END_XFER;
701 local_irq_restore (flags);
704 if (dev->req_pending)
705 ep0start(dev, UDCCS0_IPR, "OUT");
706 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
707 && read_ep0_fifo(ep, req))) {
715 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
716 local_irq_restore (flags);
719 /* can the FIFO can satisfy the request immediately? */
720 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
721 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
722 && write_fifo(ep, req))
724 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
725 && read_fifo(ep, req)) {
729 if (likely (req && ep->desc))
730 pio_irq_enable(ep->bEndpointAddress);
733 /* pio or dma irq handler advances the queue. */
734 if (likely (req != 0))
735 list_add_tail(&req->queue, &ep->queue);
736 local_irq_restore(flags);
743 * nuke - dequeue ALL requests
745 static void nuke(struct pxa2xx_ep *ep, int status)
747 struct pxa2xx_request *req;
749 /* called with irqs blocked */
750 while (!list_empty(&ep->queue)) {
751 req = list_entry(ep->queue.next,
752 struct pxa2xx_request,
754 done(ep, req, status);
757 pio_irq_disable (ep->bEndpointAddress);
761 /* dequeue JUST ONE request */
762 static int pxa2xx_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
764 struct pxa2xx_ep *ep;
765 struct pxa2xx_request *req;
768 ep = container_of(_ep, struct pxa2xx_ep, ep);
769 if (!_ep || ep->ep.name == ep0name)
772 local_irq_save(flags);
774 /* make sure it's actually queued on this endpoint */
775 list_for_each_entry (req, &ep->queue, queue) {
776 if (&req->req == _req)
779 if (&req->req != _req) {
780 local_irq_restore(flags);
784 done(ep, req, -ECONNRESET);
786 local_irq_restore(flags);
790 /*-------------------------------------------------------------------------*/
792 static int pxa2xx_ep_set_halt(struct usb_ep *_ep, int value)
794 struct pxa2xx_ep *ep;
797 ep = container_of(_ep, struct pxa2xx_ep, ep);
799 || (!ep->desc && ep->ep.name != ep0name))
800 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
801 DMSG("%s, bad ep\n", __FUNCTION__);
805 /* this path (reset toggle+halt) is needed to implement
806 * SET_INTERFACE on normal hardware. but it can't be
807 * done from software on the PXA UDC, and the hardware
808 * forgets to do it as part of SET_INTERFACE automagic.
810 DMSG("only host can clear %s halt\n", _ep->name);
814 local_irq_save(flags);
816 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
817 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
818 || !list_empty(&ep->queue))) {
819 local_irq_restore(flags);
823 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
824 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
826 /* ep0 needs special care */
828 start_watchdog(ep->dev);
829 ep->dev->req_pending = 0;
830 ep->dev->ep0state = EP0_STALL;
832 /* and bulk/intr endpoints like dropping stalls too */
835 for (i = 0; i < 1000; i += 20) {
836 if (*ep->reg_udccs & UDCCS_BI_SST)
841 local_irq_restore(flags);
843 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
847 static int pxa2xx_ep_fifo_status(struct usb_ep *_ep)
849 struct pxa2xx_ep *ep;
851 ep = container_of(_ep, struct pxa2xx_ep, ep);
853 DMSG("%s, bad ep\n", __FUNCTION__);
856 /* pxa can't report unclaimed bytes from IN fifos */
857 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
859 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
860 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
863 return (*ep->reg_ubcr & 0xfff) + 1;
866 static void pxa2xx_ep_fifo_flush(struct usb_ep *_ep)
868 struct pxa2xx_ep *ep;
870 ep = container_of(_ep, struct pxa2xx_ep, ep);
871 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
872 DMSG("%s, bad ep\n", __FUNCTION__);
876 /* toggle and halt bits stay unchanged */
878 /* for OUT, just read and discard the FIFO contents. */
879 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
880 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
881 (void) *ep->reg_uddr;
885 /* most IN status is the same, but ISO can't stall */
886 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
887 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
892 static struct usb_ep_ops pxa2xx_ep_ops = {
893 .enable = pxa2xx_ep_enable,
894 .disable = pxa2xx_ep_disable,
896 .alloc_request = pxa2xx_ep_alloc_request,
897 .free_request = pxa2xx_ep_free_request,
899 .queue = pxa2xx_ep_queue,
900 .dequeue = pxa2xx_ep_dequeue,
902 .set_halt = pxa2xx_ep_set_halt,
903 .fifo_status = pxa2xx_ep_fifo_status,
904 .fifo_flush = pxa2xx_ep_fifo_flush,
908 /* ---------------------------------------------------------------------------
909 * device-scoped parts of the api to the usb controller hardware
910 * ---------------------------------------------------------------------------
913 static int pxa2xx_udc_get_frame(struct usb_gadget *_gadget)
915 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
918 static int pxa2xx_udc_wakeup(struct usb_gadget *_gadget)
920 /* host may not have enabled remote wakeup */
921 if ((UDCCS0 & UDCCS0_DRWF) == 0)
922 return -EHOSTUNREACH;
923 udc_set_mask_UDCCR(UDCCR_RSM);
927 static void stop_activity(struct pxa2xx_udc *, struct usb_gadget_driver *);
928 static void udc_enable (struct pxa2xx_udc *);
929 static void udc_disable(struct pxa2xx_udc *);
931 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
934 static int pullup(struct pxa2xx_udc *udc, int is_active)
936 is_active = is_active && udc->vbus && udc->pullup;
937 DMSG("%s\n", is_active ? "active" : "inactive");
941 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
942 DMSG("disconnect %s\n", udc->driver
943 ? udc->driver->driver.name
945 stop_activity(udc, udc->driver);
952 /* VBUS reporting logically comes from a transceiver */
953 static int pxa2xx_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
955 struct pxa2xx_udc *udc;
957 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
958 udc->vbus = is_active = (is_active != 0);
959 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
960 pullup(udc, is_active);
964 /* drivers may have software control over D+ pullup */
965 static int pxa2xx_udc_pullup(struct usb_gadget *_gadget, int is_active)
967 struct pxa2xx_udc *udc;
969 udc = container_of(_gadget, struct pxa2xx_udc, gadget);
971 /* not all boards support pullup control */
972 if (!udc->mach->gpio_pullup && !udc->mach->udc_command)
975 is_active = (is_active != 0);
976 udc->pullup = is_active;
977 pullup(udc, is_active);
981 static const struct usb_gadget_ops pxa2xx_udc_ops = {
982 .get_frame = pxa2xx_udc_get_frame,
983 .wakeup = pxa2xx_udc_wakeup,
984 .vbus_session = pxa2xx_udc_vbus_session,
985 .pullup = pxa2xx_udc_pullup,
987 // .vbus_draw ... boards may consume current from VBUS, up to
988 // 100-500mA based on config. the 500uA suspend ceiling means
989 // that exclusively vbus-powered PXA designs violate USB specs.
992 /*-------------------------------------------------------------------------*/
994 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
996 static const char proc_node_name [] = "driver/udc";
999 udc_proc_read(char *page, char **start, off_t off, int count,
1000 int *eof, void *_dev)
1003 struct pxa2xx_udc *dev = _dev;
1005 unsigned size = count;
1006 unsigned long flags;
1013 local_irq_save(flags);
1015 /* basic device status */
1016 t = scnprintf(next, size, DRIVER_DESC "\n"
1017 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1018 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1019 dev->driver ? dev->driver->driver.name : "(none)",
1020 is_vbus_present() ? "full speed" : "disconnected");
1024 /* registers for device and ep0 */
1025 t = scnprintf(next, size,
1026 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1027 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1032 t = scnprintf(next, size,
1033 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1034 (tmp & UDCCR_REM) ? " rem" : "",
1035 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1036 (tmp & UDCCR_SRM) ? " srm" : "",
1037 (tmp & UDCCR_SUSIR) ? " susir" : "",
1038 (tmp & UDCCR_RESIR) ? " resir" : "",
1039 (tmp & UDCCR_RSM) ? " rsm" : "",
1040 (tmp & UDCCR_UDA) ? " uda" : "",
1041 (tmp & UDCCR_UDE) ? " ude" : "");
1046 t = scnprintf(next, size,
1047 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1048 (tmp & UDCCS0_SA) ? " sa" : "",
1049 (tmp & UDCCS0_RNE) ? " rne" : "",
1050 (tmp & UDCCS0_FST) ? " fst" : "",
1051 (tmp & UDCCS0_SST) ? " sst" : "",
1052 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1053 (tmp & UDCCS0_FTF) ? " ftf" : "",
1054 (tmp & UDCCS0_IPR) ? " ipr" : "",
1055 (tmp & UDCCS0_OPR) ? " opr" : "");
1061 t = scnprintf(next, size,
1062 "udccfr %02X =%s%s\n", tmp,
1063 (tmp & UDCCFR_AREN) ? " aren" : "",
1064 (tmp & UDCCFR_ACM) ? " acm" : "");
1069 if (!is_vbus_present() || !dev->driver)
1072 t = scnprintf(next, size, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1073 dev->stats.write.bytes, dev->stats.write.ops,
1074 dev->stats.read.bytes, dev->stats.read.ops,
1079 /* dump endpoint queues */
1080 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1081 struct pxa2xx_ep *ep = &dev->ep [i];
1082 struct pxa2xx_request *req;
1085 const struct usb_endpoint_descriptor *d;
1090 tmp = *dev->ep [i].reg_udccs;
1091 t = scnprintf(next, size,
1092 "%s max %d %s udccs %02x irqs %lu\n",
1093 ep->ep.name, le16_to_cpu (d->wMaxPacketSize),
1094 "pio", tmp, ep->pio_irqs);
1095 /* TODO translate all five groups of udccs bits! */
1097 } else /* ep0 should only have one transfer queued */
1098 t = scnprintf(next, size, "ep0 max 16 pio irqs %lu\n",
1100 if (t <= 0 || t > size)
1105 if (list_empty(&ep->queue)) {
1106 t = scnprintf(next, size, "\t(nothing queued)\n");
1107 if (t <= 0 || t > size)
1113 list_for_each_entry(req, &ep->queue, queue) {
1114 t = scnprintf(next, size,
1115 "\treq %p len %d/%d buf %p\n",
1116 &req->req, req->req.actual,
1117 req->req.length, req->req.buf);
1118 if (t <= 0 || t > size)
1126 local_irq_restore(flags);
1128 return count - size;
1131 #define create_proc_files() \
1132 create_proc_read_entry(proc_node_name, 0, NULL, udc_proc_read, dev)
1133 #define remove_proc_files() \
1134 remove_proc_entry(proc_node_name, NULL)
1136 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1138 #define create_proc_files() do {} while (0)
1139 #define remove_proc_files() do {} while (0)
1141 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1143 /*-------------------------------------------------------------------------*/
1146 * udc_disable - disable USB device controller
1148 static void udc_disable(struct pxa2xx_udc *dev)
1150 /* block all irqs */
1151 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1152 UICR0 = UICR1 = 0xff;
1155 /* if hardware supports it, disconnect from usb */
1158 udc_clear_mask_UDCCR(UDCCR_UDE);
1160 #ifdef CONFIG_ARCH_PXA
1161 /* Disable clock for USB device */
1162 clk_disable(dev->clk);
1166 dev->gadget.speed = USB_SPEED_UNKNOWN;
1171 * udc_reinit - initialize software state
1173 static void udc_reinit(struct pxa2xx_udc *dev)
1177 /* device/ep0 records init */
1178 INIT_LIST_HEAD (&dev->gadget.ep_list);
1179 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1180 dev->ep0state = EP0_IDLE;
1182 /* basic endpoint records init */
1183 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1184 struct pxa2xx_ep *ep = &dev->ep[i];
1187 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1191 INIT_LIST_HEAD (&ep->queue);
1195 /* the rest was statically initialized, and is read-only */
1198 /* until it's enabled, this UDC should be completely invisible
1201 static void udc_enable (struct pxa2xx_udc *dev)
1203 udc_clear_mask_UDCCR(UDCCR_UDE);
1205 #ifdef CONFIG_ARCH_PXA
1206 /* Enable clock for USB device */
1207 clk_enable(dev->clk);
1210 /* try to clear these bits before we enable the udc */
1211 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1214 dev->gadget.speed = USB_SPEED_UNKNOWN;
1215 dev->stats.irqs = 0;
1218 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1220 * - if RESET is already in progress, ack interrupt
1221 * - unmask reset interrupt
1223 udc_set_mask_UDCCR(UDCCR_UDE);
1224 if (!(UDCCR & UDCCR_UDA))
1225 udc_ack_int_UDCCR(UDCCR_RSTIR);
1227 if (dev->has_cfr /* UDC_RES2 is defined */) {
1228 /* pxa255 (a0+) can avoid a set_config race that could
1229 * prevent gadget drivers from configuring correctly
1231 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1233 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1234 * which could result in missing packets and interrupts.
1235 * supposedly one bit per endpoint, controlling whether it
1236 * double buffers or not; ACM/AREN bits fit into the holes.
1237 * zero bits (like USIR0_IRx) disable double buffering.
1243 /* enable suspend/resume and reset irqs */
1244 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1246 /* enable ep0 irqs */
1247 UICR0 &= ~UICR0_IM0;
1249 /* if hardware supports it, pullup D+ and wait for reset */
1254 /* when a driver is successfully registered, it will receive
1255 * control requests including set_configuration(), which enables
1256 * non-control requests. then usb traffic follows until a
1257 * disconnect is reported. then a host may connect again, or
1258 * the driver might get unbound.
1260 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1262 struct pxa2xx_udc *dev = the_controller;
1266 || driver->speed < USB_SPEED_FULL
1268 || !driver->disconnect
1276 /* first hook up the driver ... */
1277 dev->driver = driver;
1278 dev->gadget.dev.driver = &driver->driver;
1281 retval = device_add (&dev->gadget.dev);
1285 dev->gadget.dev.driver = NULL;
1288 retval = driver->bind(&dev->gadget);
1290 DMSG("bind to driver %s --> error %d\n",
1291 driver->driver.name, retval);
1292 device_del (&dev->gadget.dev);
1296 /* ... then enable host detection and ep0; and we're ready
1297 * for set_configuration as well as eventual disconnect.
1299 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1304 EXPORT_SYMBOL(usb_gadget_register_driver);
1307 stop_activity(struct pxa2xx_udc *dev, struct usb_gadget_driver *driver)
1311 /* don't disconnect drivers more than once */
1312 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1314 dev->gadget.speed = USB_SPEED_UNKNOWN;
1316 /* prevent new request submissions, kill any outstanding requests */
1317 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1318 struct pxa2xx_ep *ep = &dev->ep[i];
1321 nuke(ep, -ESHUTDOWN);
1323 del_timer_sync(&dev->timer);
1325 /* report disconnect; the driver is already quiesced */
1327 driver->disconnect(&dev->gadget);
1329 /* re-init driver-visible data structures */
1333 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1335 struct pxa2xx_udc *dev = the_controller;
1339 if (!driver || driver != dev->driver || !driver->unbind)
1342 local_irq_disable();
1344 stop_activity(dev, driver);
1347 driver->unbind(&dev->gadget);
1350 device_del (&dev->gadget.dev);
1352 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1356 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1359 /*-------------------------------------------------------------------------*/
1361 #ifdef CONFIG_ARCH_LUBBOCK
1363 /* Lubbock has separate connect and disconnect irqs. More typical designs
1364 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1368 lubbock_vbus_irq(int irq, void *_dev)
1370 struct pxa2xx_udc *dev = _dev;
1375 case LUBBOCK_USB_IRQ:
1377 disable_irq(LUBBOCK_USB_IRQ);
1378 enable_irq(LUBBOCK_USB_DISC_IRQ);
1380 case LUBBOCK_USB_DISC_IRQ:
1382 disable_irq(LUBBOCK_USB_DISC_IRQ);
1383 enable_irq(LUBBOCK_USB_IRQ);
1389 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1395 static irqreturn_t udc_vbus_irq(int irq, void *_dev)
1397 struct pxa2xx_udc *dev = _dev;
1398 int vbus = gpio_get_value(dev->mach->gpio_vbus);
1400 pxa2xx_udc_vbus_session(&dev->gadget, vbus);
1405 /*-------------------------------------------------------------------------*/
1407 static inline void clear_ep_state (struct pxa2xx_udc *dev)
1411 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1412 * fifos, and pending transactions mustn't be continued in any case.
1414 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1415 nuke(&dev->ep[i], -ECONNABORTED);
1418 static void udc_watchdog(unsigned long _dev)
1420 struct pxa2xx_udc *dev = (void *)_dev;
1422 local_irq_disable();
1423 if (dev->ep0state == EP0_STALL
1424 && (UDCCS0 & UDCCS0_FST) == 0
1425 && (UDCCS0 & UDCCS0_SST) == 0) {
1426 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1427 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1428 start_watchdog(dev);
1433 static void handle_ep0 (struct pxa2xx_udc *dev)
1435 u32 udccs0 = UDCCS0;
1436 struct pxa2xx_ep *ep = &dev->ep [0];
1437 struct pxa2xx_request *req;
1439 struct usb_ctrlrequest r;
1444 if (list_empty(&ep->queue))
1447 req = list_entry(ep->queue.next, struct pxa2xx_request, queue);
1449 /* clear stall status */
1450 if (udccs0 & UDCCS0_SST) {
1452 UDCCS0 = UDCCS0_SST;
1453 del_timer(&dev->timer);
1457 /* previous request unfinished? non-error iff back-to-back ... */
1458 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1460 del_timer(&dev->timer);
1464 switch (dev->ep0state) {
1466 /* late-breaking status? */
1469 /* start control request? */
1470 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1471 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1476 /* read SETUP packet */
1477 for (i = 0; i < 8; i++) {
1478 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1480 DMSG("SETUP %d!\n", i);
1483 u.raw [i] = (u8) UDDR0;
1485 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1489 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1490 u.r.bRequestType, u.r.bRequest,
1491 le16_to_cpu(u.r.wValue),
1492 le16_to_cpu(u.r.wIndex),
1493 le16_to_cpu(u.r.wLength));
1495 /* cope with automagic for some standard requests. */
1496 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1497 == USB_TYPE_STANDARD;
1498 dev->req_config = 0;
1499 dev->req_pending = 1;
1500 switch (u.r.bRequest) {
1501 /* hardware restricts gadget drivers here! */
1502 case USB_REQ_SET_CONFIGURATION:
1503 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1504 /* reflect hardware's automagic
1505 * up to the gadget driver.
1508 dev->req_config = 1;
1509 clear_ep_state(dev);
1510 /* if !has_cfr, there's no synch
1511 * else use AREN (later) not SA|OPR
1512 * USIR0_IR0 acts edge sensitive
1516 /* ... and here, even more ... */
1517 case USB_REQ_SET_INTERFACE:
1518 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1519 /* udc hardware is broken by design:
1520 * - altsetting may only be zero;
1521 * - hw resets all interfaces' eps;
1522 * - ep reset doesn't include halt(?).
1524 DMSG("broken set_interface (%d/%d)\n",
1525 le16_to_cpu(u.r.wIndex),
1526 le16_to_cpu(u.r.wValue));
1530 /* hardware was supposed to hide this */
1531 case USB_REQ_SET_ADDRESS:
1532 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1533 ep0start(dev, 0, "address");
1539 if (u.r.bRequestType & USB_DIR_IN)
1540 dev->ep0state = EP0_IN_DATA_PHASE;
1542 dev->ep0state = EP0_OUT_DATA_PHASE;
1544 i = dev->driver->setup(&dev->gadget, &u.r);
1546 /* hardware automagic preventing STALL... */
1547 if (dev->req_config) {
1548 /* hardware sometimes neglects to tell
1549 * tell us about config change events,
1550 * so later ones may fail...
1552 WARN("config change %02x fail %d?\n",
1555 /* TODO experiment: if has_cfr,
1556 * hardware didn't ACK; maybe we
1557 * could actually STALL!
1560 DBG(DBG_VERBOSE, "protocol STALL, "
1561 "%02x err %d\n", UDCCS0, i);
1563 /* the watchdog timer helps deal with cases
1564 * where udc seems to clear FST wrongly, and
1565 * then NAKs instead of STALLing.
1567 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1568 start_watchdog(dev);
1569 dev->ep0state = EP0_STALL;
1571 /* deferred i/o == no response yet */
1572 } else if (dev->req_pending) {
1573 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1574 || dev->req_std || u.r.wLength))
1575 ep0start(dev, 0, "defer");
1577 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1580 /* expect at least one data or status stage irq */
1583 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1584 == (UDCCS0_OPR|UDCCS0_SA))) {
1587 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1588 * still observed on a pxa255 a0.
1590 DBG(DBG_VERBOSE, "e131\n");
1593 /* read SETUP data, but don't trust it too much */
1594 for (i = 0; i < 8; i++)
1595 u.raw [i] = (u8) UDDR0;
1596 if ((u.r.bRequestType & USB_RECIP_MASK)
1599 if (u.word [0] == 0 && u.word [1] == 0)
1603 /* some random early IRQ:
1606 * - OPR got set, without SA (likely status stage)
1608 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1611 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1612 if (udccs0 & UDCCS0_OPR) {
1613 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1614 DBG(DBG_VERBOSE, "ep0in premature status\n");
1618 } else /* irq was IPR clearing */ {
1620 /* this IN packet might finish the request */
1621 (void) write_ep0_fifo(ep, req);
1622 } /* else IN token before response was written */
1625 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1626 if (udccs0 & UDCCS0_OPR) {
1628 /* this OUT packet might finish the request */
1629 if (read_ep0_fifo(ep, req))
1631 /* else more OUT packets expected */
1632 } /* else OUT token before read was issued */
1633 } else /* irq was IPR clearing */ {
1634 DBG(DBG_VERBOSE, "ep0out premature status\n");
1643 /* ack control-IN status (maybe in-zlp was skipped)
1644 * also appears after some config change events.
1646 if (udccs0 & UDCCS0_OPR)
1647 UDCCS0 = UDCCS0_OPR;
1651 UDCCS0 = UDCCS0_FST;
1657 static void handle_ep(struct pxa2xx_ep *ep)
1659 struct pxa2xx_request *req;
1660 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1666 if (likely (!list_empty(&ep->queue)))
1667 req = list_entry(ep->queue.next,
1668 struct pxa2xx_request, queue);
1672 // TODO check FST handling
1674 udccs = *ep->reg_udccs;
1675 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1677 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1678 tmp |= UDCCS_BI_SST;
1681 *ep->reg_udccs = tmp;
1682 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1683 completed = write_fifo(ep, req);
1685 } else { /* irq from RPC (or for ISO, ROF) */
1686 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1687 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1689 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1692 *ep->reg_udccs = tmp;
1694 /* fifos can hold packets, ready for reading... */
1696 completed = read_fifo(ep, req);
1698 pio_irq_disable (ep->bEndpointAddress);
1701 } while (completed);
1705 * pxa2xx_udc_irq - interrupt handler
1707 * avoid delays in ep0 processing. the control handshaking isn't always
1708 * under software control (pxa250c0 and the pxa255 are better), and delays
1709 * could cause usb protocol errors.
1712 pxa2xx_udc_irq(int irq, void *_dev)
1714 struct pxa2xx_udc *dev = _dev;
1723 /* SUSpend Interrupt Request */
1724 if (unlikely(udccr & UDCCR_SUSIR)) {
1725 udc_ack_int_UDCCR(UDCCR_SUSIR);
1727 DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present()
1728 ? "" : "+disconnect");
1730 if (!is_vbus_present())
1731 stop_activity(dev, dev->driver);
1732 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
1734 && dev->driver->suspend)
1735 dev->driver->suspend(&dev->gadget);
1739 /* RESume Interrupt Request */
1740 if (unlikely(udccr & UDCCR_RESIR)) {
1741 udc_ack_int_UDCCR(UDCCR_RESIR);
1743 DBG(DBG_VERBOSE, "USB resume\n");
1745 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1747 && dev->driver->resume
1748 && is_vbus_present())
1749 dev->driver->resume(&dev->gadget);
1752 /* ReSeT Interrupt Request - USB reset */
1753 if (unlikely(udccr & UDCCR_RSTIR)) {
1754 udc_ack_int_UDCCR(UDCCR_RSTIR);
1757 if ((UDCCR & UDCCR_UDA) == 0) {
1758 DBG(DBG_VERBOSE, "USB reset start\n");
1760 /* reset driver and endpoints,
1761 * in case that's not yet done
1763 stop_activity (dev, dev->driver);
1766 DBG(DBG_VERBOSE, "USB reset end\n");
1767 dev->gadget.speed = USB_SPEED_FULL;
1768 memset(&dev->stats, 0, sizeof dev->stats);
1769 /* driver and endpoints are still reset */
1773 u32 usir0 = USIR0 & ~UICR0;
1774 u32 usir1 = USIR1 & ~UICR1;
1777 if (unlikely (!usir0 && !usir1))
1780 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1782 /* control traffic */
1783 if (usir0 & USIR0_IR0) {
1784 dev->ep[0].pio_irqs++;
1789 /* endpoint data transfers */
1790 for (i = 0; i < 8; i++) {
1793 if (i && (usir0 & tmp)) {
1794 handle_ep(&dev->ep[i]);
1799 handle_ep(&dev->ep[i+8]);
1806 /* we could also ask for 1 msec SOF (SIR) interrupts */
1812 /*-------------------------------------------------------------------------*/
1814 static void nop_release (struct device *dev)
1816 DMSG("%s %s\n", __FUNCTION__, dev->bus_id);
1819 /* this uses load-time allocation and initialization (instead of
1820 * doing it at run-time) to save code, eliminate fault paths, and
1821 * be more obviously correct.
1823 static struct pxa2xx_udc memory = {
1825 .ops = &pxa2xx_udc_ops,
1826 .ep0 = &memory.ep[0].ep,
1827 .name = driver_name,
1830 .release = nop_release,
1834 /* control endpoint */
1838 .ops = &pxa2xx_ep_ops,
1839 .maxpacket = EP0_FIFO_SIZE,
1842 .reg_udccs = &UDCCS0,
1846 /* first group of endpoints */
1849 .name = "ep1in-bulk",
1850 .ops = &pxa2xx_ep_ops,
1851 .maxpacket = BULK_FIFO_SIZE,
1854 .fifo_size = BULK_FIFO_SIZE,
1855 .bEndpointAddress = USB_DIR_IN | 1,
1856 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1857 .reg_udccs = &UDCCS1,
1862 .name = "ep2out-bulk",
1863 .ops = &pxa2xx_ep_ops,
1864 .maxpacket = BULK_FIFO_SIZE,
1867 .fifo_size = BULK_FIFO_SIZE,
1868 .bEndpointAddress = 2,
1869 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1870 .reg_udccs = &UDCCS2,
1874 #ifndef CONFIG_USB_PXA2XX_SMALL
1877 .name = "ep3in-iso",
1878 .ops = &pxa2xx_ep_ops,
1879 .maxpacket = ISO_FIFO_SIZE,
1882 .fifo_size = ISO_FIFO_SIZE,
1883 .bEndpointAddress = USB_DIR_IN | 3,
1884 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1885 .reg_udccs = &UDCCS3,
1890 .name = "ep4out-iso",
1891 .ops = &pxa2xx_ep_ops,
1892 .maxpacket = ISO_FIFO_SIZE,
1895 .fifo_size = ISO_FIFO_SIZE,
1896 .bEndpointAddress = 4,
1897 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1898 .reg_udccs = &UDCCS4,
1904 .name = "ep5in-int",
1905 .ops = &pxa2xx_ep_ops,
1906 .maxpacket = INT_FIFO_SIZE,
1909 .fifo_size = INT_FIFO_SIZE,
1910 .bEndpointAddress = USB_DIR_IN | 5,
1911 .bmAttributes = USB_ENDPOINT_XFER_INT,
1912 .reg_udccs = &UDCCS5,
1916 /* second group of endpoints */
1919 .name = "ep6in-bulk",
1920 .ops = &pxa2xx_ep_ops,
1921 .maxpacket = BULK_FIFO_SIZE,
1924 .fifo_size = BULK_FIFO_SIZE,
1925 .bEndpointAddress = USB_DIR_IN | 6,
1926 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1927 .reg_udccs = &UDCCS6,
1932 .name = "ep7out-bulk",
1933 .ops = &pxa2xx_ep_ops,
1934 .maxpacket = BULK_FIFO_SIZE,
1937 .fifo_size = BULK_FIFO_SIZE,
1938 .bEndpointAddress = 7,
1939 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1940 .reg_udccs = &UDCCS7,
1946 .name = "ep8in-iso",
1947 .ops = &pxa2xx_ep_ops,
1948 .maxpacket = ISO_FIFO_SIZE,
1951 .fifo_size = ISO_FIFO_SIZE,
1952 .bEndpointAddress = USB_DIR_IN | 8,
1953 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1954 .reg_udccs = &UDCCS8,
1959 .name = "ep9out-iso",
1960 .ops = &pxa2xx_ep_ops,
1961 .maxpacket = ISO_FIFO_SIZE,
1964 .fifo_size = ISO_FIFO_SIZE,
1965 .bEndpointAddress = 9,
1966 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1967 .reg_udccs = &UDCCS9,
1973 .name = "ep10in-int",
1974 .ops = &pxa2xx_ep_ops,
1975 .maxpacket = INT_FIFO_SIZE,
1978 .fifo_size = INT_FIFO_SIZE,
1979 .bEndpointAddress = USB_DIR_IN | 10,
1980 .bmAttributes = USB_ENDPOINT_XFER_INT,
1981 .reg_udccs = &UDCCS10,
1982 .reg_uddr = &UDDR10,
1985 /* third group of endpoints */
1988 .name = "ep11in-bulk",
1989 .ops = &pxa2xx_ep_ops,
1990 .maxpacket = BULK_FIFO_SIZE,
1993 .fifo_size = BULK_FIFO_SIZE,
1994 .bEndpointAddress = USB_DIR_IN | 11,
1995 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1996 .reg_udccs = &UDCCS11,
1997 .reg_uddr = &UDDR11,
2001 .name = "ep12out-bulk",
2002 .ops = &pxa2xx_ep_ops,
2003 .maxpacket = BULK_FIFO_SIZE,
2006 .fifo_size = BULK_FIFO_SIZE,
2007 .bEndpointAddress = 12,
2008 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2009 .reg_udccs = &UDCCS12,
2010 .reg_ubcr = &UBCR12,
2011 .reg_uddr = &UDDR12,
2015 .name = "ep13in-iso",
2016 .ops = &pxa2xx_ep_ops,
2017 .maxpacket = ISO_FIFO_SIZE,
2020 .fifo_size = ISO_FIFO_SIZE,
2021 .bEndpointAddress = USB_DIR_IN | 13,
2022 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2023 .reg_udccs = &UDCCS13,
2024 .reg_uddr = &UDDR13,
2028 .name = "ep14out-iso",
2029 .ops = &pxa2xx_ep_ops,
2030 .maxpacket = ISO_FIFO_SIZE,
2033 .fifo_size = ISO_FIFO_SIZE,
2034 .bEndpointAddress = 14,
2035 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2036 .reg_udccs = &UDCCS14,
2037 .reg_ubcr = &UBCR14,
2038 .reg_uddr = &UDDR14,
2042 .name = "ep15in-int",
2043 .ops = &pxa2xx_ep_ops,
2044 .maxpacket = INT_FIFO_SIZE,
2047 .fifo_size = INT_FIFO_SIZE,
2048 .bEndpointAddress = USB_DIR_IN | 15,
2049 .bmAttributes = USB_ENDPOINT_XFER_INT,
2050 .reg_udccs = &UDCCS15,
2051 .reg_uddr = &UDDR15,
2053 #endif /* !CONFIG_USB_PXA2XX_SMALL */
2056 #define CP15R0_VENDOR_MASK 0xffffe000
2058 #if defined(CONFIG_ARCH_PXA)
2059 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2061 #elif defined(CONFIG_ARCH_IXP4XX)
2062 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2066 #define CP15R0_PROD_MASK 0x000003f0
2067 #define PXA25x 0x00000100 /* and PXA26x */
2068 #define PXA210 0x00000120
2070 #define CP15R0_REV_MASK 0x0000000f
2072 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2074 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2075 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2076 #define PXA250_B2 0x00000104
2077 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2078 #define PXA250_B0 0x00000102
2079 #define PXA250_A1 0x00000101
2080 #define PXA250_A0 0x00000100
2082 #define PXA210_C0 0x00000125
2083 #define PXA210_B2 0x00000124
2084 #define PXA210_B1 0x00000123
2085 #define PXA210_B0 0x00000122
2086 #define IXP425_A0 0x000001c1
2087 #define IXP425_B0 0x000001f1
2088 #define IXP465_AD 0x00000200
2091 * probe - binds to the platform device
2093 static int __init pxa2xx_udc_probe(struct platform_device *pdev)
2095 struct pxa2xx_udc *dev = &memory;
2096 int retval, vbus_irq, irq;
2099 /* insist on Intel/ARM/XScale */
2100 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2101 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2102 printk(KERN_ERR "%s: not XScale!\n", driver_name);
2106 /* trigger chiprev-specific logic */
2107 switch (chiprev & CP15R0_PRODREV_MASK) {
2108 #if defined(CONFIG_ARCH_PXA)
2114 /* A0/A1 "not released"; ep 13, 15 unusable */
2116 case PXA250_B2: case PXA210_B2:
2117 case PXA250_B1: case PXA210_B1:
2118 case PXA250_B0: case PXA210_B0:
2119 /* OUT-DMA is broken ... */
2121 case PXA250_C0: case PXA210_C0:
2123 #elif defined(CONFIG_ARCH_IXP4XX)
2131 printk(KERN_ERR "%s: unrecognized processor: %08x\n",
2132 driver_name, chiprev);
2133 /* iop3xx, ixp4xx, ... */
2137 irq = platform_get_irq(pdev, 0);
2141 #ifdef CONFIG_ARCH_PXA
2142 dev->clk = clk_get(&pdev->dev, "UDCCLK");
2143 if (IS_ERR(dev->clk)) {
2144 retval = PTR_ERR(dev->clk);
2149 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
2150 dev->has_cfr ? "" : " (!cfr)",
2154 /* other non-static parts of init */
2155 dev->dev = &pdev->dev;
2156 dev->mach = pdev->dev.platform_data;
2158 if (dev->mach->gpio_vbus) {
2159 if ((retval = gpio_request(dev->mach->gpio_vbus,
2160 "pxa2xx_udc GPIO VBUS"))) {
2162 "can't get vbus gpio %d, err: %d\n",
2163 dev->mach->gpio_vbus, retval);
2166 gpio_direction_input(dev->mach->gpio_vbus);
2167 vbus_irq = gpio_to_irq(dev->mach->gpio_vbus);
2171 if (dev->mach->gpio_pullup) {
2172 if ((retval = gpio_request(dev->mach->gpio_pullup,
2173 "pca2xx_udc GPIO PULLUP"))) {
2175 "can't get pullup gpio %d, err: %d\n",
2176 dev->mach->gpio_pullup, retval);
2177 goto err_gpio_pullup;
2179 gpio_direction_output(dev->mach->gpio_pullup, 0);
2182 init_timer(&dev->timer);
2183 dev->timer.function = udc_watchdog;
2184 dev->timer.data = (unsigned long) dev;
2186 device_initialize(&dev->gadget.dev);
2187 dev->gadget.dev.parent = &pdev->dev;
2188 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2190 the_controller = dev;
2191 platform_set_drvdata(pdev, dev);
2196 dev->vbus = is_vbus_present();
2198 /* irq setup after old hardware state is cleaned up */
2199 retval = request_irq(irq, pxa2xx_udc_irq,
2200 IRQF_DISABLED, driver_name, dev);
2202 printk(KERN_ERR "%s: can't get irq %d, err %d\n",
2203 driver_name, irq, retval);
2208 #ifdef CONFIG_ARCH_LUBBOCK
2209 if (machine_is_lubbock()) {
2210 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2212 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2215 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2216 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2220 retval = request_irq(LUBBOCK_USB_IRQ,
2222 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2225 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2226 driver_name, LUBBOCK_USB_IRQ, retval);
2227 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2233 retval = request_irq(vbus_irq, udc_vbus_irq,
2234 IRQF_DISABLED | IRQF_SAMPLE_RANDOM |
2235 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2238 printk(KERN_ERR "%s: can't get irq %i, err %d\n",
2239 driver_name, vbus_irq, retval);
2243 create_proc_files();
2248 #ifdef CONFIG_ARCH_LUBBOCK
2249 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2254 if (dev->mach->gpio_pullup)
2255 gpio_free(dev->mach->gpio_pullup);
2257 if (dev->mach->gpio_vbus)
2258 gpio_free(dev->mach->gpio_vbus);
2260 #ifdef CONFIG_ARCH_PXA
2267 static void pxa2xx_udc_shutdown(struct platform_device *_dev)
2272 static int __exit pxa2xx_udc_remove(struct platform_device *pdev)
2274 struct pxa2xx_udc *dev = platform_get_drvdata(pdev);
2280 remove_proc_files();
2283 free_irq(platform_get_irq(pdev, 0), dev);
2286 #ifdef CONFIG_ARCH_LUBBOCK
2287 if (machine_is_lubbock()) {
2288 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2289 free_irq(LUBBOCK_USB_IRQ, dev);
2292 if (dev->mach->gpio_vbus) {
2293 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev);
2294 gpio_free(dev->mach->gpio_vbus);
2296 if (dev->mach->gpio_pullup)
2297 gpio_free(dev->mach->gpio_pullup);
2299 #ifdef CONFIG_ARCH_PXA
2303 platform_set_drvdata(pdev, NULL);
2304 the_controller = NULL;
2308 /*-------------------------------------------------------------------------*/
2312 /* USB suspend (controlled by the host) and system suspend (controlled
2313 * by the PXA) don't necessarily work well together. If USB is active,
2314 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2315 * mode, or any deeper PM saving state.
2317 * For now, we punt and forcibly disconnect from the USB host when PXA
2318 * enters any suspend state. While we're disconnected, we always disable
2319 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2320 * Boards without software pullup control shouldn't use those states.
2321 * VBUS IRQs should probably be ignored so that the PXA device just acts
2322 * "dead" to USB hosts until system resume.
2324 static int pxa2xx_udc_suspend(struct platform_device *dev, pm_message_t state)
2326 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
2328 if (!udc->mach->gpio_pullup && !udc->mach->udc_command)
2329 WARN("USB host won't detect disconnect!\n");
2335 static int pxa2xx_udc_resume(struct platform_device *dev)
2337 struct pxa2xx_udc *udc = platform_get_drvdata(dev);
2345 #define pxa2xx_udc_suspend NULL
2346 #define pxa2xx_udc_resume NULL
2349 /*-------------------------------------------------------------------------*/
2351 static struct platform_driver udc_driver = {
2352 .shutdown = pxa2xx_udc_shutdown,
2353 .remove = __exit_p(pxa2xx_udc_remove),
2354 .suspend = pxa2xx_udc_suspend,
2355 .resume = pxa2xx_udc_resume,
2357 .owner = THIS_MODULE,
2358 .name = "pxa2xx-udc",
2362 static int __init udc_init(void)
2364 printk(KERN_INFO "%s: version %s\n", driver_name, DRIVER_VERSION);
2365 return platform_driver_probe(&udc_driver, pxa2xx_udc_probe);
2367 module_init(udc_init);
2369 static void __exit udc_exit(void)
2371 platform_driver_unregister(&udc_driver);
2373 module_exit(udc_exit);
2375 MODULE_DESCRIPTION(DRIVER_DESC);
2376 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2377 MODULE_LICENSE("GPL");