2 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
5 * Copyright (C) 2003 Robert Schwebel, Pengutronix
6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
7 * Copyright (C) 2003 David Brownell
8 * Copyright (C) 2003 Joshua Wise
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 /* #define VERBOSE_DEBUG */
18 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/ioport.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/timer.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
31 #include <linux/platform_device.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/irq.h>
34 #include <linux/clk.h>
35 #include <linux/err.h>
36 #include <linux/seq_file.h>
37 #include <linux/debugfs.h>
39 #include <linux/prefetch.h>
41 #include <asm/byteorder.h>
44 #include <asm/mach-types.h>
45 #include <asm/unaligned.h>
47 #include <linux/usb/ch9.h>
48 #include <linux/usb/gadget.h>
49 #include <linux/usb/otg.h>
52 * This driver is PXA25x only. Grab the right register definitions.
54 #ifdef CONFIG_ARCH_PXA
55 #include <mach/pxa25x-udc.h>
58 #ifdef CONFIG_ARCH_LUBBOCK
59 #include <mach/lubbock.h>
62 #include <asm/mach/udc_pxa2xx.h>
66 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
67 * series processors. The UDC for the IXP 4xx series is very similar.
68 * There are fifteen endpoints, in addition to ep0.
70 * Such controller drivers work with a gadget driver. The gadget driver
71 * returns descriptors, implements configuration and data protocols used
72 * by the host to interact with this device, and allocates endpoints to
73 * the different protocol interfaces. The controller driver virtualizes
74 * usb hardware so that the gadget drivers will be more portable.
76 * This UDC hardware wants to implement a bit too much USB protocol, so
77 * it constrains the sorts of USB configuration change events that work.
78 * The errata for these chips are misleading; some "fixed" bugs from
79 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
81 * Note that the UDC hardware supports DMA (except on IXP) but that's
82 * not used here. IN-DMA (to host) is simple enough, when the data is
83 * suitably aligned (16 bytes) ... the network stack doesn't do that,
84 * other software can. OUT-DMA is buggy in most chip versions, as well
85 * as poorly designed (data toggle not automatic). So this driver won't
86 * bother using DMA. (Mostly-working IN-DMA support was available in
87 * kernels before 2.6.23, but was never enabled or well tested.)
90 #define DRIVER_VERSION "30-June-2007"
91 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
94 static const char driver_name [] = "pxa25x_udc";
96 static const char ep0name [] = "ep0";
99 #ifdef CONFIG_ARCH_IXP4XX
101 /* cpu-specific register addresses are compiled in to this code */
102 #ifdef CONFIG_ARCH_PXA
103 #error "Can't configure both IXP and PXA"
106 /* IXP doesn't yet support <linux/clk.h> */
107 #define clk_get(dev,name) NULL
108 #define clk_enable(clk) do { } while (0)
109 #define clk_disable(clk) do { } while (0)
110 #define clk_put(clk) do { } while (0)
114 #include "pxa25x_udc.h"
117 #ifdef CONFIG_USB_PXA25X_SMALL
118 #define SIZE_STR " (small)"
123 /* ---------------------------------------------------------------------------
124 * endpoint related parts of the api to the usb controller hardware,
125 * used by gadget driver; and the inner talker-to-hardware core.
126 * ---------------------------------------------------------------------------
129 static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
130 static void nuke (struct pxa25x_ep *, int status);
132 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
133 static void pullup_off(void)
135 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
136 int off_level = mach->gpio_pullup_inverted;
138 if (gpio_is_valid(mach->gpio_pullup))
139 gpio_set_value(mach->gpio_pullup, off_level);
140 else if (mach->udc_command)
141 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
144 static void pullup_on(void)
146 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
147 int on_level = !mach->gpio_pullup_inverted;
149 if (gpio_is_valid(mach->gpio_pullup))
150 gpio_set_value(mach->gpio_pullup, on_level);
151 else if (mach->udc_command)
152 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
155 static void pio_irq_enable(int bEndpointAddress)
157 bEndpointAddress &= 0xf;
158 if (bEndpointAddress < 8)
159 UICR0 &= ~(1 << bEndpointAddress);
161 bEndpointAddress -= 8;
162 UICR1 &= ~(1 << bEndpointAddress);
166 static void pio_irq_disable(int bEndpointAddress)
168 bEndpointAddress &= 0xf;
169 if (bEndpointAddress < 8)
170 UICR0 |= 1 << bEndpointAddress;
172 bEndpointAddress -= 8;
173 UICR1 |= 1 << bEndpointAddress;
177 /* The UDCCR reg contains mask and interrupt status bits,
178 * so using '|=' isn't safe as it may ack an interrupt.
180 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
182 static inline void udc_set_mask_UDCCR(int mask)
184 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
187 static inline void udc_clear_mask_UDCCR(int mask)
189 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
192 static inline void udc_ack_int_UDCCR(int mask)
194 /* udccr contains the bits we dont want to change */
195 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
197 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
201 * endpoint enable/disable
203 * we need to verify the descriptors used to enable endpoints. since pxa25x
204 * endpoint configurations are fixed, and are pretty much always enabled,
205 * there's not a lot to manage here.
207 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
208 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
209 * for a single interface (with only the default altsetting) and for gadget
210 * drivers that don't halt endpoints (not reset by set_interface). that also
211 * means that if you use ISO, you must violate the USB spec rule that all
212 * iso endpoints must be in non-default altsettings.
214 static int pxa25x_ep_enable (struct usb_ep *_ep,
215 const struct usb_endpoint_descriptor *desc)
217 struct pxa25x_ep *ep;
218 struct pxa25x_udc *dev;
220 ep = container_of (_ep, struct pxa25x_ep, ep);
221 if (!_ep || !desc || ep->ep.desc || _ep->name == ep0name
222 || desc->bDescriptorType != USB_DT_ENDPOINT
223 || ep->bEndpointAddress != desc->bEndpointAddress
224 || ep->fifo_size < usb_endpoint_maxp (desc)) {
225 DMSG("%s, bad ep or descriptor\n", __func__);
229 /* xfer types must match, except that interrupt ~= bulk */
230 if (ep->bmAttributes != desc->bmAttributes
231 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
232 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
233 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
237 /* hardware _could_ do smaller, but driver doesn't */
238 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
239 && usb_endpoint_maxp (desc)
241 || !desc->wMaxPacketSize) {
242 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
247 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
248 DMSG("%s, bogus device state\n", __func__);
255 ep->ep.maxpacket = usb_endpoint_maxp (desc);
257 /* flush fifo (mostly for OUT buffers) */
258 pxa25x_ep_fifo_flush (_ep);
260 /* ... reset halt state too, if we could ... */
262 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
266 static int pxa25x_ep_disable (struct usb_ep *_ep)
268 struct pxa25x_ep *ep;
271 ep = container_of (_ep, struct pxa25x_ep, ep);
272 if (!_ep || !ep->ep.desc) {
273 DMSG("%s, %s not enabled\n", __func__,
274 _ep ? ep->ep.name : NULL);
277 local_irq_save(flags);
279 nuke (ep, -ESHUTDOWN);
281 /* flush fifo (mostly for IN buffers) */
282 pxa25x_ep_fifo_flush (_ep);
287 local_irq_restore(flags);
288 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
292 /*-------------------------------------------------------------------------*/
294 /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
295 * must still pass correctly initialized endpoints, since other controller
296 * drivers may care about how it's currently set up (dma issues etc).
300 * pxa25x_ep_alloc_request - allocate a request data structure
302 static struct usb_request *
303 pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
305 struct pxa25x_request *req;
307 req = kzalloc(sizeof(*req), gfp_flags);
311 INIT_LIST_HEAD (&req->queue);
317 * pxa25x_ep_free_request - deallocate a request data structure
320 pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
322 struct pxa25x_request *req;
324 req = container_of (_req, struct pxa25x_request, req);
325 WARN_ON(!list_empty (&req->queue));
329 /*-------------------------------------------------------------------------*/
332 * done - retire a request; caller blocked irqs
334 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
336 unsigned stopped = ep->stopped;
338 list_del_init(&req->queue);
340 if (likely (req->req.status == -EINPROGRESS))
341 req->req.status = status;
343 status = req->req.status;
345 if (status && status != -ESHUTDOWN)
346 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
347 ep->ep.name, &req->req, status,
348 req->req.actual, req->req.length);
350 /* don't modify queue heads during completion callback */
352 req->req.complete(&ep->ep, &req->req);
353 ep->stopped = stopped;
357 static inline void ep0_idle (struct pxa25x_udc *dev)
359 dev->ep0state = EP0_IDLE;
363 write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
366 unsigned length, count;
368 buf = req->req.buf + req->req.actual;
371 /* how big will this packet be? */
372 length = min(req->req.length - req->req.actual, max);
373 req->req.actual += length;
376 while (likely(count--))
383 * write to an IN endpoint fifo, as many packets as possible.
384 * irqs will use this to write the rest later.
385 * caller guarantees at least one packet buffer is ready (or a zlp).
388 write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
392 max = usb_endpoint_maxp(ep->ep.desc);
395 int is_last, is_short;
397 count = write_packet(ep->reg_uddr, req, max);
399 /* last packet is usually short (or a zlp) */
400 if (unlikely (count != max))
401 is_last = is_short = 1;
403 if (likely(req->req.length != req->req.actual)
408 /* interrupt/iso maxpacket may not fill the fifo */
409 is_short = unlikely (max < ep->fifo_size);
412 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
414 is_last ? "/L" : "", is_short ? "/S" : "",
415 req->req.length - req->req.actual, req);
417 /* let loose that packet. maybe try writing another one,
418 * double buffering might work. TSP, TPC, and TFS
419 * bit values are the same for all normal IN endpoints.
421 *ep->reg_udccs = UDCCS_BI_TPC;
423 *ep->reg_udccs = UDCCS_BI_TSP;
425 /* requests complete when all IN data is in the FIFO */
428 if (list_empty(&ep->queue))
429 pio_irq_disable (ep->bEndpointAddress);
433 // TODO experiment: how robust can fifo mode tweaking be?
434 // double buffering is off in the default fifo mode, which
435 // prevents TFS from being set here.
437 } while (*ep->reg_udccs & UDCCS_BI_TFS);
441 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
442 * ep0 data stage. these chips want very simple state transitions.
445 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
447 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
449 dev->req_pending = 0;
450 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
451 __func__, tag, UDCCS0, flags);
455 write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
460 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
461 ep->dev->stats.write.bytes += count;
463 /* last packet "must be" short (or a zlp) */
464 is_short = (count != EP0_FIFO_SIZE);
466 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
467 req->req.length - req->req.actual, req);
469 if (unlikely (is_short)) {
470 if (ep->dev->req_pending)
471 ep0start(ep->dev, UDCCS0_IPR, "short IN");
475 count = req->req.length;
478 #ifndef CONFIG_ARCH_IXP4XX
480 /* This seems to get rid of lost status irqs in some cases:
481 * host responds quickly, or next request involves config
482 * change automagic, or should have been hidden, or ...
484 * FIXME get rid of all udelays possible...
486 if (count >= EP0_FIFO_SIZE) {
489 if ((UDCCS0 & UDCCS0_OPR) != 0) {
490 /* clear OPR, generate ack */
500 } else if (ep->dev->req_pending)
501 ep0start(ep->dev, 0, "IN");
507 * read_fifo - unload packet(s) from the fifo we use for usb OUT
508 * transfers and put them into the request. caller should have made
509 * sure there's at least one packet ready.
511 * returns true if the request completed because of short packet or the
512 * request buffer having filled (and maybe overran till end-of-packet).
515 read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
520 unsigned bufferspace, count, is_short;
522 /* make sure there's a packet in the FIFO.
523 * UDCCS_{BO,IO}_RPC are all the same bit value.
524 * UDCCS_{BO,IO}_RNE are all the same bit value.
526 udccs = *ep->reg_udccs;
527 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
529 buf = req->req.buf + req->req.actual;
531 bufferspace = req->req.length - req->req.actual;
533 /* read all bytes from this packet */
534 if (likely (udccs & UDCCS_BO_RNE)) {
535 count = 1 + (0x0ff & *ep->reg_ubcr);
536 req->req.actual += min (count, bufferspace);
539 is_short = (count < ep->ep.maxpacket);
540 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
541 ep->ep.name, udccs, count,
542 is_short ? "/S" : "",
543 req, req->req.actual, req->req.length);
544 while (likely (count-- != 0)) {
545 u8 byte = (u8) *ep->reg_uddr;
547 if (unlikely (bufferspace == 0)) {
548 /* this happens when the driver's buffer
549 * is smaller than what the host sent.
550 * discard the extra data.
552 if (req->req.status != -EOVERFLOW)
553 DMSG("%s overflow %d\n",
555 req->req.status = -EOVERFLOW;
561 *ep->reg_udccs = UDCCS_BO_RPC;
562 /* RPC/RSP/RNE could now reflect the other packet buffer */
564 /* iso is one request per packet */
565 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
566 if (udccs & UDCCS_IO_ROF)
567 req->req.status = -EHOSTUNREACH;
568 /* more like "is_done" */
573 if (is_short || req->req.actual == req->req.length) {
575 if (list_empty(&ep->queue))
576 pio_irq_disable (ep->bEndpointAddress);
580 /* finished that packet. the next one may be waiting... */
586 * special ep0 version of the above. no UBCR0 or double buffering; status
587 * handshaking is magic. most device protocols don't need control-OUT.
588 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
589 * protocols do use them.
592 read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
595 unsigned bufferspace;
597 buf = req->req.buf + req->req.actual;
598 bufferspace = req->req.length - req->req.actual;
600 while (UDCCS0 & UDCCS0_RNE) {
603 if (unlikely (bufferspace == 0)) {
604 /* this happens when the driver's buffer
605 * is smaller than what the host sent.
606 * discard the extra data.
608 if (req->req.status != -EOVERFLOW)
609 DMSG("%s overflow\n", ep->ep.name);
610 req->req.status = -EOVERFLOW;
618 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
621 if (req->req.actual >= req->req.length)
624 /* finished that packet. the next one may be waiting... */
628 /*-------------------------------------------------------------------------*/
631 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
633 struct pxa25x_request *req;
634 struct pxa25x_ep *ep;
635 struct pxa25x_udc *dev;
638 req = container_of(_req, struct pxa25x_request, req);
639 if (unlikely (!_req || !_req->complete || !_req->buf
640 || !list_empty(&req->queue))) {
641 DMSG("%s, bad params\n", __func__);
645 ep = container_of(_ep, struct pxa25x_ep, ep);
646 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) {
647 DMSG("%s, bad ep\n", __func__);
652 if (unlikely (!dev->driver
653 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
654 DMSG("%s, bogus device state\n", __func__);
658 /* iso is always one packet per request, that's the only way
659 * we can report per-packet status. that also helps with dma.
661 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
662 && req->req.length > usb_endpoint_maxp(ep->ep.desc)))
665 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
666 _ep->name, _req, _req->length, _req->buf);
668 local_irq_save(flags);
670 _req->status = -EINPROGRESS;
673 /* kickstart this i/o queue? */
674 if (list_empty(&ep->queue) && !ep->stopped) {
675 if (ep->ep.desc == NULL/* ep0 */) {
676 unsigned length = _req->length;
678 switch (dev->ep0state) {
679 case EP0_IN_DATA_PHASE:
680 dev->stats.write.ops++;
681 if (write_ep0_fifo(ep, req))
685 case EP0_OUT_DATA_PHASE:
686 dev->stats.read.ops++;
688 if (dev->req_config) {
689 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
690 dev->has_cfr ? "" : " raced");
692 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
695 dev->ep0state = EP0_END_XFER;
696 local_irq_restore (flags);
699 if (dev->req_pending)
700 ep0start(dev, UDCCS0_IPR, "OUT");
701 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
702 && read_ep0_fifo(ep, req))) {
710 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
711 local_irq_restore (flags);
714 /* can the FIFO can satisfy the request immediately? */
715 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
716 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
717 && write_fifo(ep, req))
719 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
720 && read_fifo(ep, req)) {
724 if (likely(req && ep->ep.desc))
725 pio_irq_enable(ep->bEndpointAddress);
728 /* pio or dma irq handler advances the queue. */
729 if (likely(req != NULL))
730 list_add_tail(&req->queue, &ep->queue);
731 local_irq_restore(flags);
738 * nuke - dequeue ALL requests
740 static void nuke(struct pxa25x_ep *ep, int status)
742 struct pxa25x_request *req;
744 /* called with irqs blocked */
745 while (!list_empty(&ep->queue)) {
746 req = list_entry(ep->queue.next,
747 struct pxa25x_request,
749 done(ep, req, status);
752 pio_irq_disable (ep->bEndpointAddress);
756 /* dequeue JUST ONE request */
757 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
759 struct pxa25x_ep *ep;
760 struct pxa25x_request *req;
763 ep = container_of(_ep, struct pxa25x_ep, ep);
764 if (!_ep || ep->ep.name == ep0name)
767 local_irq_save(flags);
769 /* make sure it's actually queued on this endpoint */
770 list_for_each_entry (req, &ep->queue, queue) {
771 if (&req->req == _req)
774 if (&req->req != _req) {
775 local_irq_restore(flags);
779 done(ep, req, -ECONNRESET);
781 local_irq_restore(flags);
785 /*-------------------------------------------------------------------------*/
787 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
789 struct pxa25x_ep *ep;
792 ep = container_of(_ep, struct pxa25x_ep, ep);
794 || (!ep->ep.desc && ep->ep.name != ep0name))
795 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
796 DMSG("%s, bad ep\n", __func__);
800 /* this path (reset toggle+halt) is needed to implement
801 * SET_INTERFACE on normal hardware. but it can't be
802 * done from software on the PXA UDC, and the hardware
803 * forgets to do it as part of SET_INTERFACE automagic.
805 DMSG("only host can clear %s halt\n", _ep->name);
809 local_irq_save(flags);
811 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
812 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
813 || !list_empty(&ep->queue))) {
814 local_irq_restore(flags);
818 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
819 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
821 /* ep0 needs special care */
823 start_watchdog(ep->dev);
824 ep->dev->req_pending = 0;
825 ep->dev->ep0state = EP0_STALL;
827 /* and bulk/intr endpoints like dropping stalls too */
830 for (i = 0; i < 1000; i += 20) {
831 if (*ep->reg_udccs & UDCCS_BI_SST)
836 local_irq_restore(flags);
838 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
842 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
844 struct pxa25x_ep *ep;
846 ep = container_of(_ep, struct pxa25x_ep, ep);
848 DMSG("%s, bad ep\n", __func__);
851 /* pxa can't report unclaimed bytes from IN fifos */
852 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
854 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
855 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
858 return (*ep->reg_ubcr & 0xfff) + 1;
861 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
863 struct pxa25x_ep *ep;
865 ep = container_of(_ep, struct pxa25x_ep, ep);
866 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
867 DMSG("%s, bad ep\n", __func__);
871 /* toggle and halt bits stay unchanged */
873 /* for OUT, just read and discard the FIFO contents. */
874 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
875 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
876 (void) *ep->reg_uddr;
880 /* most IN status is the same, but ISO can't stall */
881 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
882 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
887 static struct usb_ep_ops pxa25x_ep_ops = {
888 .enable = pxa25x_ep_enable,
889 .disable = pxa25x_ep_disable,
891 .alloc_request = pxa25x_ep_alloc_request,
892 .free_request = pxa25x_ep_free_request,
894 .queue = pxa25x_ep_queue,
895 .dequeue = pxa25x_ep_dequeue,
897 .set_halt = pxa25x_ep_set_halt,
898 .fifo_status = pxa25x_ep_fifo_status,
899 .fifo_flush = pxa25x_ep_fifo_flush,
903 /* ---------------------------------------------------------------------------
904 * device-scoped parts of the api to the usb controller hardware
905 * ---------------------------------------------------------------------------
908 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
910 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
913 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
915 /* host may not have enabled remote wakeup */
916 if ((UDCCS0 & UDCCS0_DRWF) == 0)
917 return -EHOSTUNREACH;
918 udc_set_mask_UDCCR(UDCCR_RSM);
922 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
923 static void udc_enable (struct pxa25x_udc *);
924 static void udc_disable(struct pxa25x_udc *);
926 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
929 static int pullup(struct pxa25x_udc *udc)
931 int is_active = udc->vbus && udc->pullup && !udc->suspended;
932 DMSG("%s\n", is_active ? "active" : "inactive");
936 /* Enable clock for USB device */
937 clk_enable(udc->clk);
942 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
943 DMSG("disconnect %s\n", udc->driver
944 ? udc->driver->driver.name
946 stop_activity(udc, udc->driver);
949 /* Disable clock for USB device */
950 clk_disable(udc->clk);
958 /* VBUS reporting logically comes from a transceiver */
959 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
961 struct pxa25x_udc *udc;
963 udc = container_of(_gadget, struct pxa25x_udc, gadget);
964 udc->vbus = is_active;
965 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
970 /* drivers may have software control over D+ pullup */
971 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
973 struct pxa25x_udc *udc;
975 udc = container_of(_gadget, struct pxa25x_udc, gadget);
977 /* not all boards support pullup control */
978 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
981 udc->pullup = (is_active != 0);
986 /* boards may consume current from VBUS, up to 100-500mA based on config.
987 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
990 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
992 struct pxa25x_udc *udc;
994 udc = container_of(_gadget, struct pxa25x_udc, gadget);
996 if (udc->transceiver)
997 return usb_phy_set_power(udc->transceiver, mA);
1001 static int pxa25x_start(struct usb_gadget_driver *driver,
1002 int (*bind)(struct usb_gadget *));
1003 static int pxa25x_stop(struct usb_gadget_driver *driver);
1005 static const struct usb_gadget_ops pxa25x_udc_ops = {
1006 .get_frame = pxa25x_udc_get_frame,
1007 .wakeup = pxa25x_udc_wakeup,
1008 .vbus_session = pxa25x_udc_vbus_session,
1009 .pullup = pxa25x_udc_pullup,
1010 .vbus_draw = pxa25x_udc_vbus_draw,
1011 .start = pxa25x_start,
1012 .stop = pxa25x_stop,
1015 /*-------------------------------------------------------------------------*/
1017 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1020 udc_seq_show(struct seq_file *m, void *_d)
1022 struct pxa25x_udc *dev = m->private;
1023 unsigned long flags;
1027 local_irq_save(flags);
1029 /* basic device status */
1030 seq_printf(m, DRIVER_DESC "\n"
1031 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1032 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1033 dev->driver ? dev->driver->driver.name : "(none)",
1034 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected");
1036 /* registers for device and ep0 */
1038 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1039 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1043 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1044 (tmp & UDCCR_REM) ? " rem" : "",
1045 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1046 (tmp & UDCCR_SRM) ? " srm" : "",
1047 (tmp & UDCCR_SUSIR) ? " susir" : "",
1048 (tmp & UDCCR_RESIR) ? " resir" : "",
1049 (tmp & UDCCR_RSM) ? " rsm" : "",
1050 (tmp & UDCCR_UDA) ? " uda" : "",
1051 (tmp & UDCCR_UDE) ? " ude" : "");
1055 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1056 (tmp & UDCCS0_SA) ? " sa" : "",
1057 (tmp & UDCCS0_RNE) ? " rne" : "",
1058 (tmp & UDCCS0_FST) ? " fst" : "",
1059 (tmp & UDCCS0_SST) ? " sst" : "",
1060 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1061 (tmp & UDCCS0_FTF) ? " ftf" : "",
1062 (tmp & UDCCS0_IPR) ? " ipr" : "",
1063 (tmp & UDCCS0_OPR) ? " opr" : "");
1068 "udccfr %02X =%s%s\n", tmp,
1069 (tmp & UDCCFR_AREN) ? " aren" : "",
1070 (tmp & UDCCFR_ACM) ? " acm" : "");
1073 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver)
1076 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1077 dev->stats.write.bytes, dev->stats.write.ops,
1078 dev->stats.read.bytes, dev->stats.read.ops,
1081 /* dump endpoint queues */
1082 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1083 struct pxa25x_ep *ep = &dev->ep [i];
1084 struct pxa25x_request *req;
1087 const struct usb_endpoint_descriptor *desc;
1092 tmp = *dev->ep [i].reg_udccs;
1094 "%s max %d %s udccs %02x irqs %lu\n",
1095 ep->ep.name, usb_endpoint_maxp(desc),
1096 "pio", tmp, ep->pio_irqs);
1097 /* TODO translate all five groups of udccs bits! */
1099 } else /* ep0 should only have one transfer queued */
1100 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
1103 if (list_empty(&ep->queue)) {
1104 seq_printf(m, "\t(nothing queued)\n");
1107 list_for_each_entry(req, &ep->queue, queue) {
1109 "\treq %p len %d/%d buf %p\n",
1110 &req->req, req->req.actual,
1111 req->req.length, req->req.buf);
1116 local_irq_restore(flags);
1121 udc_debugfs_open(struct inode *inode, struct file *file)
1123 return single_open(file, udc_seq_show, inode->i_private);
1126 static const struct file_operations debug_fops = {
1127 .open = udc_debugfs_open,
1129 .llseek = seq_lseek,
1130 .release = single_release,
1131 .owner = THIS_MODULE,
1134 #define create_debug_files(dev) \
1136 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1137 S_IRUGO, NULL, dev, &debug_fops); \
1139 #define remove_debug_files(dev) \
1141 if (dev->debugfs_udc) \
1142 debugfs_remove(dev->debugfs_udc); \
1145 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1147 #define create_debug_files(dev) do {} while (0)
1148 #define remove_debug_files(dev) do {} while (0)
1150 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1152 /*-------------------------------------------------------------------------*/
1155 * udc_disable - disable USB device controller
1157 static void udc_disable(struct pxa25x_udc *dev)
1159 /* block all irqs */
1160 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1161 UICR0 = UICR1 = 0xff;
1164 /* if hardware supports it, disconnect from usb */
1167 udc_clear_mask_UDCCR(UDCCR_UDE);
1170 dev->gadget.speed = USB_SPEED_UNKNOWN;
1175 * udc_reinit - initialize software state
1177 static void udc_reinit(struct pxa25x_udc *dev)
1181 /* device/ep0 records init */
1182 INIT_LIST_HEAD (&dev->gadget.ep_list);
1183 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1184 dev->ep0state = EP0_IDLE;
1186 /* basic endpoint records init */
1187 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1188 struct pxa25x_ep *ep = &dev->ep[i];
1191 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1195 INIT_LIST_HEAD (&ep->queue);
1199 /* the rest was statically initialized, and is read-only */
1202 /* until it's enabled, this UDC should be completely invisible
1205 static void udc_enable (struct pxa25x_udc *dev)
1207 udc_clear_mask_UDCCR(UDCCR_UDE);
1209 /* try to clear these bits before we enable the udc */
1210 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1213 dev->gadget.speed = USB_SPEED_UNKNOWN;
1214 dev->stats.irqs = 0;
1217 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1219 * - if RESET is already in progress, ack interrupt
1220 * - unmask reset interrupt
1222 udc_set_mask_UDCCR(UDCCR_UDE);
1223 if (!(UDCCR & UDCCR_UDA))
1224 udc_ack_int_UDCCR(UDCCR_RSTIR);
1226 if (dev->has_cfr /* UDC_RES2 is defined */) {
1227 /* pxa255 (a0+) can avoid a set_config race that could
1228 * prevent gadget drivers from configuring correctly
1230 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1232 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1233 * which could result in missing packets and interrupts.
1234 * supposedly one bit per endpoint, controlling whether it
1235 * double buffers or not; ACM/AREN bits fit into the holes.
1236 * zero bits (like USIR0_IRx) disable double buffering.
1242 /* enable suspend/resume and reset irqs */
1243 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1245 /* enable ep0 irqs */
1246 UICR0 &= ~UICR0_IM0;
1248 /* if hardware supports it, pullup D+ and wait for reset */
1253 /* when a driver is successfully registered, it will receive
1254 * control requests including set_configuration(), which enables
1255 * non-control requests. then usb traffic follows until a
1256 * disconnect is reported. then a host may connect again, or
1257 * the driver might get unbound.
1259 static int pxa25x_start(struct usb_gadget_driver *driver,
1260 int (*bind)(struct usb_gadget *))
1262 struct pxa25x_udc *dev = the_controller;
1266 || driver->max_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 = 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);
1301 /* connect to bus through transceiver */
1302 if (dev->transceiver) {
1303 retval = otg_set_peripheral(dev->transceiver->otg,
1306 DMSG("can't bind to transceiver\n");
1308 driver->unbind(&dev->gadget);
1321 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1325 /* don't disconnect drivers more than once */
1326 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1328 dev->gadget.speed = USB_SPEED_UNKNOWN;
1330 /* prevent new request submissions, kill any outstanding requests */
1331 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1332 struct pxa25x_ep *ep = &dev->ep[i];
1335 nuke(ep, -ESHUTDOWN);
1337 del_timer_sync(&dev->timer);
1339 /* report disconnect; the driver is already quiesced */
1341 driver->disconnect(&dev->gadget);
1343 /* re-init driver-visible data structures */
1347 static int pxa25x_stop(struct usb_gadget_driver *driver)
1349 struct pxa25x_udc *dev = the_controller;
1353 if (!driver || driver != dev->driver || !driver->unbind)
1356 local_irq_disable();
1359 stop_activity(dev, driver);
1362 if (dev->transceiver)
1363 (void) otg_set_peripheral(dev->transceiver->otg, NULL);
1365 driver->unbind(&dev->gadget);
1366 dev->gadget.dev.driver = NULL;
1369 device_del (&dev->gadget.dev);
1371 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1376 /*-------------------------------------------------------------------------*/
1378 #ifdef CONFIG_ARCH_LUBBOCK
1380 /* Lubbock has separate connect and disconnect irqs. More typical designs
1381 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1385 lubbock_vbus_irq(int irq, void *_dev)
1387 struct pxa25x_udc *dev = _dev;
1392 case LUBBOCK_USB_IRQ:
1394 disable_irq(LUBBOCK_USB_IRQ);
1395 enable_irq(LUBBOCK_USB_DISC_IRQ);
1397 case LUBBOCK_USB_DISC_IRQ:
1399 disable_irq(LUBBOCK_USB_DISC_IRQ);
1400 enable_irq(LUBBOCK_USB_IRQ);
1406 pxa25x_udc_vbus_session(&dev->gadget, vbus);
1413 /*-------------------------------------------------------------------------*/
1415 static inline void clear_ep_state (struct pxa25x_udc *dev)
1419 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1420 * fifos, and pending transactions mustn't be continued in any case.
1422 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1423 nuke(&dev->ep[i], -ECONNABORTED);
1426 static void udc_watchdog(unsigned long _dev)
1428 struct pxa25x_udc *dev = (void *)_dev;
1430 local_irq_disable();
1431 if (dev->ep0state == EP0_STALL
1432 && (UDCCS0 & UDCCS0_FST) == 0
1433 && (UDCCS0 & UDCCS0_SST) == 0) {
1434 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1435 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1436 start_watchdog(dev);
1441 static void handle_ep0 (struct pxa25x_udc *dev)
1443 u32 udccs0 = UDCCS0;
1444 struct pxa25x_ep *ep = &dev->ep [0];
1445 struct pxa25x_request *req;
1447 struct usb_ctrlrequest r;
1452 if (list_empty(&ep->queue))
1455 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1457 /* clear stall status */
1458 if (udccs0 & UDCCS0_SST) {
1460 UDCCS0 = UDCCS0_SST;
1461 del_timer(&dev->timer);
1465 /* previous request unfinished? non-error iff back-to-back ... */
1466 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1468 del_timer(&dev->timer);
1472 switch (dev->ep0state) {
1474 /* late-breaking status? */
1477 /* start control request? */
1478 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1479 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1484 /* read SETUP packet */
1485 for (i = 0; i < 8; i++) {
1486 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1488 DMSG("SETUP %d!\n", i);
1491 u.raw [i] = (u8) UDDR0;
1493 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1497 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1498 u.r.bRequestType, u.r.bRequest,
1499 le16_to_cpu(u.r.wValue),
1500 le16_to_cpu(u.r.wIndex),
1501 le16_to_cpu(u.r.wLength));
1503 /* cope with automagic for some standard requests. */
1504 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1505 == USB_TYPE_STANDARD;
1506 dev->req_config = 0;
1507 dev->req_pending = 1;
1508 switch (u.r.bRequest) {
1509 /* hardware restricts gadget drivers here! */
1510 case USB_REQ_SET_CONFIGURATION:
1511 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1512 /* reflect hardware's automagic
1513 * up to the gadget driver.
1516 dev->req_config = 1;
1517 clear_ep_state(dev);
1518 /* if !has_cfr, there's no synch
1519 * else use AREN (later) not SA|OPR
1520 * USIR0_IR0 acts edge sensitive
1524 /* ... and here, even more ... */
1525 case USB_REQ_SET_INTERFACE:
1526 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1527 /* udc hardware is broken by design:
1528 * - altsetting may only be zero;
1529 * - hw resets all interfaces' eps;
1530 * - ep reset doesn't include halt(?).
1532 DMSG("broken set_interface (%d/%d)\n",
1533 le16_to_cpu(u.r.wIndex),
1534 le16_to_cpu(u.r.wValue));
1538 /* hardware was supposed to hide this */
1539 case USB_REQ_SET_ADDRESS:
1540 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1541 ep0start(dev, 0, "address");
1547 if (u.r.bRequestType & USB_DIR_IN)
1548 dev->ep0state = EP0_IN_DATA_PHASE;
1550 dev->ep0state = EP0_OUT_DATA_PHASE;
1552 i = dev->driver->setup(&dev->gadget, &u.r);
1554 /* hardware automagic preventing STALL... */
1555 if (dev->req_config) {
1556 /* hardware sometimes neglects to tell
1557 * tell us about config change events,
1558 * so later ones may fail...
1560 WARNING("config change %02x fail %d?\n",
1563 /* TODO experiment: if has_cfr,
1564 * hardware didn't ACK; maybe we
1565 * could actually STALL!
1568 DBG(DBG_VERBOSE, "protocol STALL, "
1569 "%02x err %d\n", UDCCS0, i);
1571 /* the watchdog timer helps deal with cases
1572 * where udc seems to clear FST wrongly, and
1573 * then NAKs instead of STALLing.
1575 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1576 start_watchdog(dev);
1577 dev->ep0state = EP0_STALL;
1579 /* deferred i/o == no response yet */
1580 } else if (dev->req_pending) {
1581 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1582 || dev->req_std || u.r.wLength))
1583 ep0start(dev, 0, "defer");
1585 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1588 /* expect at least one data or status stage irq */
1591 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1592 == (UDCCS0_OPR|UDCCS0_SA))) {
1595 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1596 * still observed on a pxa255 a0.
1598 DBG(DBG_VERBOSE, "e131\n");
1601 /* read SETUP data, but don't trust it too much */
1602 for (i = 0; i < 8; i++)
1603 u.raw [i] = (u8) UDDR0;
1604 if ((u.r.bRequestType & USB_RECIP_MASK)
1607 if (u.word [0] == 0 && u.word [1] == 0)
1611 /* some random early IRQ:
1614 * - OPR got set, without SA (likely status stage)
1616 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1619 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1620 if (udccs0 & UDCCS0_OPR) {
1621 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1622 DBG(DBG_VERBOSE, "ep0in premature status\n");
1626 } else /* irq was IPR clearing */ {
1628 /* this IN packet might finish the request */
1629 (void) write_ep0_fifo(ep, req);
1630 } /* else IN token before response was written */
1633 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1634 if (udccs0 & UDCCS0_OPR) {
1636 /* this OUT packet might finish the request */
1637 if (read_ep0_fifo(ep, req))
1639 /* else more OUT packets expected */
1640 } /* else OUT token before read was issued */
1641 } else /* irq was IPR clearing */ {
1642 DBG(DBG_VERBOSE, "ep0out premature status\n");
1651 /* ack control-IN status (maybe in-zlp was skipped)
1652 * also appears after some config change events.
1654 if (udccs0 & UDCCS0_OPR)
1655 UDCCS0 = UDCCS0_OPR;
1659 UDCCS0 = UDCCS0_FST;
1665 static void handle_ep(struct pxa25x_ep *ep)
1667 struct pxa25x_request *req;
1668 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1674 if (likely (!list_empty(&ep->queue)))
1675 req = list_entry(ep->queue.next,
1676 struct pxa25x_request, queue);
1680 // TODO check FST handling
1682 udccs = *ep->reg_udccs;
1683 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1685 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1686 tmp |= UDCCS_BI_SST;
1689 *ep->reg_udccs = tmp;
1690 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1691 completed = write_fifo(ep, req);
1693 } else { /* irq from RPC (or for ISO, ROF) */
1694 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1695 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1697 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1700 *ep->reg_udccs = tmp;
1702 /* fifos can hold packets, ready for reading... */
1704 completed = read_fifo(ep, req);
1706 pio_irq_disable (ep->bEndpointAddress);
1709 } while (completed);
1713 * pxa25x_udc_irq - interrupt handler
1715 * avoid delays in ep0 processing. the control handshaking isn't always
1716 * under software control (pxa250c0 and the pxa255 are better), and delays
1717 * could cause usb protocol errors.
1720 pxa25x_udc_irq(int irq, void *_dev)
1722 struct pxa25x_udc *dev = _dev;
1731 /* SUSpend Interrupt Request */
1732 if (unlikely(udccr & UDCCR_SUSIR)) {
1733 udc_ack_int_UDCCR(UDCCR_SUSIR);
1735 DBG(DBG_VERBOSE, "USB suspend\n");
1737 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1739 && dev->driver->suspend)
1740 dev->driver->suspend(&dev->gadget);
1744 /* RESume Interrupt Request */
1745 if (unlikely(udccr & UDCCR_RESIR)) {
1746 udc_ack_int_UDCCR(UDCCR_RESIR);
1748 DBG(DBG_VERBOSE, "USB resume\n");
1750 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1752 && dev->driver->resume)
1753 dev->driver->resume(&dev->gadget);
1756 /* ReSeT Interrupt Request - USB reset */
1757 if (unlikely(udccr & UDCCR_RSTIR)) {
1758 udc_ack_int_UDCCR(UDCCR_RSTIR);
1761 if ((UDCCR & UDCCR_UDA) == 0) {
1762 DBG(DBG_VERBOSE, "USB reset start\n");
1764 /* reset driver and endpoints,
1765 * in case that's not yet done
1767 stop_activity (dev, dev->driver);
1770 DBG(DBG_VERBOSE, "USB reset end\n");
1771 dev->gadget.speed = USB_SPEED_FULL;
1772 memset(&dev->stats, 0, sizeof dev->stats);
1773 /* driver and endpoints are still reset */
1777 u32 usir0 = USIR0 & ~UICR0;
1778 u32 usir1 = USIR1 & ~UICR1;
1781 if (unlikely (!usir0 && !usir1))
1784 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1786 /* control traffic */
1787 if (usir0 & USIR0_IR0) {
1788 dev->ep[0].pio_irqs++;
1793 /* endpoint data transfers */
1794 for (i = 0; i < 8; i++) {
1797 if (i && (usir0 & tmp)) {
1798 handle_ep(&dev->ep[i]);
1802 #ifndef CONFIG_USB_PXA25X_SMALL
1804 handle_ep(&dev->ep[i+8]);
1812 /* we could also ask for 1 msec SOF (SIR) interrupts */
1818 /*-------------------------------------------------------------------------*/
1820 static void nop_release (struct device *dev)
1822 DMSG("%s %s\n", __func__, dev_name(dev));
1825 /* this uses load-time allocation and initialization (instead of
1826 * doing it at run-time) to save code, eliminate fault paths, and
1827 * be more obviously correct.
1829 static struct pxa25x_udc memory = {
1831 .ops = &pxa25x_udc_ops,
1832 .ep0 = &memory.ep[0].ep,
1833 .name = driver_name,
1835 .init_name = "gadget",
1836 .release = nop_release,
1840 /* control endpoint */
1844 .ops = &pxa25x_ep_ops,
1845 .maxpacket = EP0_FIFO_SIZE,
1848 .reg_udccs = &UDCCS0,
1852 /* first group of endpoints */
1855 .name = "ep1in-bulk",
1856 .ops = &pxa25x_ep_ops,
1857 .maxpacket = BULK_FIFO_SIZE,
1860 .fifo_size = BULK_FIFO_SIZE,
1861 .bEndpointAddress = USB_DIR_IN | 1,
1862 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1863 .reg_udccs = &UDCCS1,
1868 .name = "ep2out-bulk",
1869 .ops = &pxa25x_ep_ops,
1870 .maxpacket = BULK_FIFO_SIZE,
1873 .fifo_size = BULK_FIFO_SIZE,
1874 .bEndpointAddress = 2,
1875 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1876 .reg_udccs = &UDCCS2,
1880 #ifndef CONFIG_USB_PXA25X_SMALL
1883 .name = "ep3in-iso",
1884 .ops = &pxa25x_ep_ops,
1885 .maxpacket = ISO_FIFO_SIZE,
1888 .fifo_size = ISO_FIFO_SIZE,
1889 .bEndpointAddress = USB_DIR_IN | 3,
1890 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1891 .reg_udccs = &UDCCS3,
1896 .name = "ep4out-iso",
1897 .ops = &pxa25x_ep_ops,
1898 .maxpacket = ISO_FIFO_SIZE,
1901 .fifo_size = ISO_FIFO_SIZE,
1902 .bEndpointAddress = 4,
1903 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1904 .reg_udccs = &UDCCS4,
1910 .name = "ep5in-int",
1911 .ops = &pxa25x_ep_ops,
1912 .maxpacket = INT_FIFO_SIZE,
1915 .fifo_size = INT_FIFO_SIZE,
1916 .bEndpointAddress = USB_DIR_IN | 5,
1917 .bmAttributes = USB_ENDPOINT_XFER_INT,
1918 .reg_udccs = &UDCCS5,
1922 /* second group of endpoints */
1925 .name = "ep6in-bulk",
1926 .ops = &pxa25x_ep_ops,
1927 .maxpacket = BULK_FIFO_SIZE,
1930 .fifo_size = BULK_FIFO_SIZE,
1931 .bEndpointAddress = USB_DIR_IN | 6,
1932 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1933 .reg_udccs = &UDCCS6,
1938 .name = "ep7out-bulk",
1939 .ops = &pxa25x_ep_ops,
1940 .maxpacket = BULK_FIFO_SIZE,
1943 .fifo_size = BULK_FIFO_SIZE,
1944 .bEndpointAddress = 7,
1945 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1946 .reg_udccs = &UDCCS7,
1952 .name = "ep8in-iso",
1953 .ops = &pxa25x_ep_ops,
1954 .maxpacket = ISO_FIFO_SIZE,
1957 .fifo_size = ISO_FIFO_SIZE,
1958 .bEndpointAddress = USB_DIR_IN | 8,
1959 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1960 .reg_udccs = &UDCCS8,
1965 .name = "ep9out-iso",
1966 .ops = &pxa25x_ep_ops,
1967 .maxpacket = ISO_FIFO_SIZE,
1970 .fifo_size = ISO_FIFO_SIZE,
1971 .bEndpointAddress = 9,
1972 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1973 .reg_udccs = &UDCCS9,
1979 .name = "ep10in-int",
1980 .ops = &pxa25x_ep_ops,
1981 .maxpacket = INT_FIFO_SIZE,
1984 .fifo_size = INT_FIFO_SIZE,
1985 .bEndpointAddress = USB_DIR_IN | 10,
1986 .bmAttributes = USB_ENDPOINT_XFER_INT,
1987 .reg_udccs = &UDCCS10,
1988 .reg_uddr = &UDDR10,
1991 /* third group of endpoints */
1994 .name = "ep11in-bulk",
1995 .ops = &pxa25x_ep_ops,
1996 .maxpacket = BULK_FIFO_SIZE,
1999 .fifo_size = BULK_FIFO_SIZE,
2000 .bEndpointAddress = USB_DIR_IN | 11,
2001 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2002 .reg_udccs = &UDCCS11,
2003 .reg_uddr = &UDDR11,
2007 .name = "ep12out-bulk",
2008 .ops = &pxa25x_ep_ops,
2009 .maxpacket = BULK_FIFO_SIZE,
2012 .fifo_size = BULK_FIFO_SIZE,
2013 .bEndpointAddress = 12,
2014 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2015 .reg_udccs = &UDCCS12,
2016 .reg_ubcr = &UBCR12,
2017 .reg_uddr = &UDDR12,
2021 .name = "ep13in-iso",
2022 .ops = &pxa25x_ep_ops,
2023 .maxpacket = ISO_FIFO_SIZE,
2026 .fifo_size = ISO_FIFO_SIZE,
2027 .bEndpointAddress = USB_DIR_IN | 13,
2028 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2029 .reg_udccs = &UDCCS13,
2030 .reg_uddr = &UDDR13,
2034 .name = "ep14out-iso",
2035 .ops = &pxa25x_ep_ops,
2036 .maxpacket = ISO_FIFO_SIZE,
2039 .fifo_size = ISO_FIFO_SIZE,
2040 .bEndpointAddress = 14,
2041 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2042 .reg_udccs = &UDCCS14,
2043 .reg_ubcr = &UBCR14,
2044 .reg_uddr = &UDDR14,
2048 .name = "ep15in-int",
2049 .ops = &pxa25x_ep_ops,
2050 .maxpacket = INT_FIFO_SIZE,
2053 .fifo_size = INT_FIFO_SIZE,
2054 .bEndpointAddress = USB_DIR_IN | 15,
2055 .bmAttributes = USB_ENDPOINT_XFER_INT,
2056 .reg_udccs = &UDCCS15,
2057 .reg_uddr = &UDDR15,
2059 #endif /* !CONFIG_USB_PXA25X_SMALL */
2062 #define CP15R0_VENDOR_MASK 0xffffe000
2064 #if defined(CONFIG_ARCH_PXA)
2065 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2067 #elif defined(CONFIG_ARCH_IXP4XX)
2068 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2072 #define CP15R0_PROD_MASK 0x000003f0
2073 #define PXA25x 0x00000100 /* and PXA26x */
2074 #define PXA210 0x00000120
2076 #define CP15R0_REV_MASK 0x0000000f
2078 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2080 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2081 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2082 #define PXA250_B2 0x00000104
2083 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2084 #define PXA250_B0 0x00000102
2085 #define PXA250_A1 0x00000101
2086 #define PXA250_A0 0x00000100
2088 #define PXA210_C0 0x00000125
2089 #define PXA210_B2 0x00000124
2090 #define PXA210_B1 0x00000123
2091 #define PXA210_B0 0x00000122
2092 #define IXP425_A0 0x000001c1
2093 #define IXP425_B0 0x000001f1
2094 #define IXP465_AD 0x00000200
2097 * probe - binds to the platform device
2099 static int __init pxa25x_udc_probe(struct platform_device *pdev)
2101 struct pxa25x_udc *dev = &memory;
2105 /* insist on Intel/ARM/XScale */
2106 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2107 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2108 pr_err("%s: not XScale!\n", driver_name);
2112 /* trigger chiprev-specific logic */
2113 switch (chiprev & CP15R0_PRODREV_MASK) {
2114 #if defined(CONFIG_ARCH_PXA)
2120 /* A0/A1 "not released"; ep 13, 15 unusable */
2122 case PXA250_B2: case PXA210_B2:
2123 case PXA250_B1: case PXA210_B1:
2124 case PXA250_B0: case PXA210_B0:
2125 /* OUT-DMA is broken ... */
2127 case PXA250_C0: case PXA210_C0:
2129 #elif defined(CONFIG_ARCH_IXP4XX)
2137 pr_err("%s: unrecognized processor: %08x\n",
2138 driver_name, chiprev);
2139 /* iop3xx, ixp4xx, ... */
2143 irq = platform_get_irq(pdev, 0);
2147 dev->clk = clk_get(&pdev->dev, NULL);
2148 if (IS_ERR(dev->clk)) {
2149 retval = PTR_ERR(dev->clk);
2153 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
2154 dev->has_cfr ? "" : " (!cfr)",
2158 /* other non-static parts of init */
2159 dev->dev = &pdev->dev;
2160 dev->mach = pdev->dev.platform_data;
2162 dev->transceiver = usb_get_transceiver();
2164 if (gpio_is_valid(dev->mach->gpio_pullup)) {
2165 if ((retval = gpio_request(dev->mach->gpio_pullup,
2166 "pca25x_udc GPIO PULLUP"))) {
2168 "can't get pullup gpio %d, err: %d\n",
2169 dev->mach->gpio_pullup, retval);
2170 goto err_gpio_pullup;
2172 gpio_direction_output(dev->mach->gpio_pullup, 0);
2175 init_timer(&dev->timer);
2176 dev->timer.function = udc_watchdog;
2177 dev->timer.data = (unsigned long) dev;
2179 device_initialize(&dev->gadget.dev);
2180 dev->gadget.dev.parent = &pdev->dev;
2181 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2183 the_controller = dev;
2184 platform_set_drvdata(pdev, dev);
2191 /* irq setup after old hardware state is cleaned up */
2192 retval = request_irq(irq, pxa25x_udc_irq,
2193 0, driver_name, dev);
2195 pr_err("%s: can't get irq %d, err %d\n",
2196 driver_name, irq, retval);
2201 #ifdef CONFIG_ARCH_LUBBOCK
2202 if (machine_is_lubbock()) {
2203 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2208 pr_err("%s: can't get irq %i, err %d\n",
2209 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2212 retval = request_irq(LUBBOCK_USB_IRQ,
2217 pr_err("%s: can't get irq %i, err %d\n",
2218 driver_name, LUBBOCK_USB_IRQ, retval);
2223 create_debug_files(dev);
2225 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget);
2229 remove_debug_files(dev);
2230 #ifdef CONFIG_ARCH_LUBBOCK
2232 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2237 if (gpio_is_valid(dev->mach->gpio_pullup))
2238 gpio_free(dev->mach->gpio_pullup);
2240 if (dev->transceiver) {
2241 usb_put_transceiver(dev->transceiver);
2242 dev->transceiver = NULL;
2249 static void pxa25x_udc_shutdown(struct platform_device *_dev)
2254 static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2256 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
2258 usb_del_gadget_udc(&dev->gadget);
2265 remove_debug_files(dev);
2268 free_irq(platform_get_irq(pdev, 0), dev);
2271 #ifdef CONFIG_ARCH_LUBBOCK
2272 if (machine_is_lubbock()) {
2273 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2274 free_irq(LUBBOCK_USB_IRQ, dev);
2277 if (gpio_is_valid(dev->mach->gpio_pullup))
2278 gpio_free(dev->mach->gpio_pullup);
2282 if (dev->transceiver) {
2283 usb_put_transceiver(dev->transceiver);
2284 dev->transceiver = NULL;
2287 platform_set_drvdata(pdev, NULL);
2288 the_controller = NULL;
2292 /*-------------------------------------------------------------------------*/
2296 /* USB suspend (controlled by the host) and system suspend (controlled
2297 * by the PXA) don't necessarily work well together. If USB is active,
2298 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2299 * mode, or any deeper PM saving state.
2301 * For now, we punt and forcibly disconnect from the USB host when PXA
2302 * enters any suspend state. While we're disconnected, we always disable
2303 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2304 * Boards without software pullup control shouldn't use those states.
2305 * VBUS IRQs should probably be ignored so that the PXA device just acts
2306 * "dead" to USB hosts until system resume.
2308 static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
2310 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2311 unsigned long flags;
2313 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command)
2314 WARNING("USB host won't detect disconnect!\n");
2317 local_irq_save(flags);
2319 local_irq_restore(flags);
2324 static int pxa25x_udc_resume(struct platform_device *dev)
2326 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2327 unsigned long flags;
2330 local_irq_save(flags);
2332 local_irq_restore(flags);
2338 #define pxa25x_udc_suspend NULL
2339 #define pxa25x_udc_resume NULL
2342 /*-------------------------------------------------------------------------*/
2344 static struct platform_driver udc_driver = {
2345 .shutdown = pxa25x_udc_shutdown,
2346 .remove = __exit_p(pxa25x_udc_remove),
2347 .suspend = pxa25x_udc_suspend,
2348 .resume = pxa25x_udc_resume,
2350 .owner = THIS_MODULE,
2351 .name = "pxa25x-udc",
2355 static int __init udc_init(void)
2357 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
2358 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
2360 module_init(udc_init);
2362 static void __exit udc_exit(void)
2364 platform_driver_unregister(&udc_driver);
2366 module_exit(udc_exit);
2368 MODULE_DESCRIPTION(DRIVER_DESC);
2369 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2370 MODULE_LICENSE("GPL");
2371 MODULE_ALIAS("platform:pxa25x-udc");