2 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
5 * Copyright (C) 2003 Robert Schwebel, Pengutronix
6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
7 * Copyright (C) 2003 David Brownell
8 * Copyright (C) 2003 Joshua Wise
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 /* #define VERBOSE_DEBUG */
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/ioport.h>
32 #include <linux/types.h>
33 #include <linux/errno.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <linux/init.h>
37 #include <linux/timer.h>
38 #include <linux/list.h>
39 #include <linux/interrupt.h>
41 #include <linux/platform_device.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/irq.h>
44 #include <linux/clk.h>
45 #include <linux/err.h>
46 #include <linux/seq_file.h>
47 #include <linux/debugfs.h>
50 #include <asm/byteorder.h>
53 #include <asm/system.h>
54 #include <asm/mach-types.h>
55 #include <asm/unaligned.h>
57 #include <linux/usb/ch9.h>
58 #include <linux/usb/gadget.h>
61 * This driver is PXA25x only. Grab the right register definitions.
63 #ifdef CONFIG_ARCH_PXA
64 #include <mach/pxa25x-udc.h>
67 #include <asm/mach/udc_pxa2xx.h>
71 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
72 * series processors. The UDC for the IXP 4xx series is very similar.
73 * There are fifteen endpoints, in addition to ep0.
75 * Such controller drivers work with a gadget driver. The gadget driver
76 * returns descriptors, implements configuration and data protocols used
77 * by the host to interact with this device, and allocates endpoints to
78 * the different protocol interfaces. The controller driver virtualizes
79 * usb hardware so that the gadget drivers will be more portable.
81 * This UDC hardware wants to implement a bit too much USB protocol, so
82 * it constrains the sorts of USB configuration change events that work.
83 * The errata for these chips are misleading; some "fixed" bugs from
84 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
86 * Note that the UDC hardware supports DMA (except on IXP) but that's
87 * not used here. IN-DMA (to host) is simple enough, when the data is
88 * suitably aligned (16 bytes) ... the network stack doesn't do that,
89 * other software can. OUT-DMA is buggy in most chip versions, as well
90 * as poorly designed (data toggle not automatic). So this driver won't
91 * bother using DMA. (Mostly-working IN-DMA support was available in
92 * kernels before 2.6.23, but was never enabled or well tested.)
95 #define DRIVER_VERSION "30-June-2007"
96 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
99 static const char driver_name [] = "pxa25x_udc";
101 static const char ep0name [] = "ep0";
104 #ifdef CONFIG_ARCH_IXP4XX
106 /* cpu-specific register addresses are compiled in to this code */
107 #ifdef CONFIG_ARCH_PXA
108 #error "Can't configure both IXP and PXA"
111 /* IXP doesn't yet support <linux/clk.h> */
112 #define clk_get(dev,name) NULL
113 #define clk_enable(clk) do { } while (0)
114 #define clk_disable(clk) do { } while (0)
115 #define clk_put(clk) do { } while (0)
119 #include "pxa25x_udc.h"
122 #ifdef CONFIG_USB_PXA25X_SMALL
123 #define SIZE_STR " (small)"
128 /* ---------------------------------------------------------------------------
129 * endpoint related parts of the api to the usb controller hardware,
130 * used by gadget driver; and the inner talker-to-hardware core.
131 * ---------------------------------------------------------------------------
134 static void pxa25x_ep_fifo_flush (struct usb_ep *ep);
135 static void nuke (struct pxa25x_ep *, int status);
137 /* one GPIO should be used to detect VBUS from the host */
138 static int is_vbus_present(void)
140 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
142 if (mach->gpio_vbus) {
143 int value = gpio_get_value(mach->gpio_vbus);
145 if (mach->gpio_vbus_inverted)
150 if (mach->udc_is_connected)
151 return mach->udc_is_connected();
155 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
156 static void pullup_off(void)
158 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
159 int off_level = mach->gpio_pullup_inverted;
161 if (mach->gpio_pullup)
162 gpio_set_value(mach->gpio_pullup, off_level);
163 else if (mach->udc_command)
164 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
167 static void pullup_on(void)
169 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
170 int on_level = !mach->gpio_pullup_inverted;
172 if (mach->gpio_pullup)
173 gpio_set_value(mach->gpio_pullup, on_level);
174 else if (mach->udc_command)
175 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
178 static void pio_irq_enable(int bEndpointAddress)
180 bEndpointAddress &= 0xf;
181 if (bEndpointAddress < 8)
182 UICR0 &= ~(1 << bEndpointAddress);
184 bEndpointAddress -= 8;
185 UICR1 &= ~(1 << bEndpointAddress);
189 static void pio_irq_disable(int bEndpointAddress)
191 bEndpointAddress &= 0xf;
192 if (bEndpointAddress < 8)
193 UICR0 |= 1 << bEndpointAddress;
195 bEndpointAddress -= 8;
196 UICR1 |= 1 << bEndpointAddress;
200 /* The UDCCR reg contains mask and interrupt status bits,
201 * so using '|=' isn't safe as it may ack an interrupt.
203 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE)
205 static inline void udc_set_mask_UDCCR(int mask)
207 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
210 static inline void udc_clear_mask_UDCCR(int mask)
212 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
215 static inline void udc_ack_int_UDCCR(int mask)
217 /* udccr contains the bits we dont want to change */
218 __u32 udccr = UDCCR & UDCCR_MASK_BITS;
220 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS);
224 * endpoint enable/disable
226 * we need to verify the descriptors used to enable endpoints. since pxa25x
227 * endpoint configurations are fixed, and are pretty much always enabled,
228 * there's not a lot to manage here.
230 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
231 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
232 * for a single interface (with only the default altsetting) and for gadget
233 * drivers that don't halt endpoints (not reset by set_interface). that also
234 * means that if you use ISO, you must violate the USB spec rule that all
235 * iso endpoints must be in non-default altsettings.
237 static int pxa25x_ep_enable (struct usb_ep *_ep,
238 const struct usb_endpoint_descriptor *desc)
240 struct pxa25x_ep *ep;
241 struct pxa25x_udc *dev;
243 ep = container_of (_ep, struct pxa25x_ep, ep);
244 if (!_ep || !desc || ep->desc || _ep->name == ep0name
245 || desc->bDescriptorType != USB_DT_ENDPOINT
246 || ep->bEndpointAddress != desc->bEndpointAddress
247 || ep->fifo_size < le16_to_cpu
248 (desc->wMaxPacketSize)) {
249 DMSG("%s, bad ep or descriptor\n", __func__);
253 /* xfer types must match, except that interrupt ~= bulk */
254 if (ep->bmAttributes != desc->bmAttributes
255 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
256 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
257 DMSG("%s, %s type mismatch\n", __func__, _ep->name);
261 /* hardware _could_ do smaller, but driver doesn't */
262 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
263 && le16_to_cpu (desc->wMaxPacketSize)
265 || !desc->wMaxPacketSize) {
266 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name);
271 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
272 DMSG("%s, bogus device state\n", __func__);
279 ep->ep.maxpacket = le16_to_cpu (desc->wMaxPacketSize);
281 /* flush fifo (mostly for OUT buffers) */
282 pxa25x_ep_fifo_flush (_ep);
284 /* ... reset halt state too, if we could ... */
286 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name);
290 static int pxa25x_ep_disable (struct usb_ep *_ep)
292 struct pxa25x_ep *ep;
295 ep = container_of (_ep, struct pxa25x_ep, ep);
296 if (!_ep || !ep->desc) {
297 DMSG("%s, %s not enabled\n", __func__,
298 _ep ? ep->ep.name : NULL);
301 local_irq_save(flags);
303 nuke (ep, -ESHUTDOWN);
305 /* flush fifo (mostly for IN buffers) */
306 pxa25x_ep_fifo_flush (_ep);
311 local_irq_restore(flags);
312 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name);
316 /*-------------------------------------------------------------------------*/
318 /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
319 * must still pass correctly initialized endpoints, since other controller
320 * drivers may care about how it's currently set up (dma issues etc).
324 * pxa25x_ep_alloc_request - allocate a request data structure
326 static struct usb_request *
327 pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags)
329 struct pxa25x_request *req;
331 req = kzalloc(sizeof(*req), gfp_flags);
335 INIT_LIST_HEAD (&req->queue);
341 * pxa25x_ep_free_request - deallocate a request data structure
344 pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req)
346 struct pxa25x_request *req;
348 req = container_of (_req, struct pxa25x_request, req);
349 WARN_ON(!list_empty (&req->queue));
353 /*-------------------------------------------------------------------------*/
356 * done - retire a request; caller blocked irqs
358 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
360 unsigned stopped = ep->stopped;
362 list_del_init(&req->queue);
364 if (likely (req->req.status == -EINPROGRESS))
365 req->req.status = status;
367 status = req->req.status;
369 if (status && status != -ESHUTDOWN)
370 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n",
371 ep->ep.name, &req->req, status,
372 req->req.actual, req->req.length);
374 /* don't modify queue heads during completion callback */
376 req->req.complete(&ep->ep, &req->req);
377 ep->stopped = stopped;
381 static inline void ep0_idle (struct pxa25x_udc *dev)
383 dev->ep0state = EP0_IDLE;
387 write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max)
390 unsigned length, count;
392 buf = req->req.buf + req->req.actual;
395 /* how big will this packet be? */
396 length = min(req->req.length - req->req.actual, max);
397 req->req.actual += length;
400 while (likely(count--))
407 * write to an IN endpoint fifo, as many packets as possible.
408 * irqs will use this to write the rest later.
409 * caller guarantees at least one packet buffer is ready (or a zlp).
412 write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
416 max = le16_to_cpu(ep->desc->wMaxPacketSize);
419 int is_last, is_short;
421 count = write_packet(ep->reg_uddr, req, max);
423 /* last packet is usually short (or a zlp) */
424 if (unlikely (count != max))
425 is_last = is_short = 1;
427 if (likely(req->req.length != req->req.actual)
432 /* interrupt/iso maxpacket may not fill the fifo */
433 is_short = unlikely (max < ep->fifo_size);
436 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n",
438 is_last ? "/L" : "", is_short ? "/S" : "",
439 req->req.length - req->req.actual, req);
441 /* let loose that packet. maybe try writing another one,
442 * double buffering might work. TSP, TPC, and TFS
443 * bit values are the same for all normal IN endpoints.
445 *ep->reg_udccs = UDCCS_BI_TPC;
447 *ep->reg_udccs = UDCCS_BI_TSP;
449 /* requests complete when all IN data is in the FIFO */
452 if (list_empty(&ep->queue))
453 pio_irq_disable (ep->bEndpointAddress);
457 // TODO experiment: how robust can fifo mode tweaking be?
458 // double buffering is off in the default fifo mode, which
459 // prevents TFS from being set here.
461 } while (*ep->reg_udccs & UDCCS_BI_TFS);
465 /* caller asserts req->pending (ep0 irq status nyet cleared); starts
466 * ep0 data stage. these chips want very simple state transitions.
469 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
471 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR;
473 dev->req_pending = 0;
474 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n",
475 __func__, tag, UDCCS0, flags);
479 write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
484 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE);
485 ep->dev->stats.write.bytes += count;
487 /* last packet "must be" short (or a zlp) */
488 is_short = (count != EP0_FIFO_SIZE);
490 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count,
491 req->req.length - req->req.actual, req);
493 if (unlikely (is_short)) {
494 if (ep->dev->req_pending)
495 ep0start(ep->dev, UDCCS0_IPR, "short IN");
499 count = req->req.length;
502 #ifndef CONFIG_ARCH_IXP4XX
504 /* This seems to get rid of lost status irqs in some cases:
505 * host responds quickly, or next request involves config
506 * change automagic, or should have been hidden, or ...
508 * FIXME get rid of all udelays possible...
510 if (count >= EP0_FIFO_SIZE) {
513 if ((UDCCS0 & UDCCS0_OPR) != 0) {
514 /* clear OPR, generate ack */
524 } else if (ep->dev->req_pending)
525 ep0start(ep->dev, 0, "IN");
531 * read_fifo - unload packet(s) from the fifo we use for usb OUT
532 * transfers and put them into the request. caller should have made
533 * sure there's at least one packet ready.
535 * returns true if the request completed because of short packet or the
536 * request buffer having filled (and maybe overran till end-of-packet).
539 read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
544 unsigned bufferspace, count, is_short;
546 /* make sure there's a packet in the FIFO.
547 * UDCCS_{BO,IO}_RPC are all the same bit value.
548 * UDCCS_{BO,IO}_RNE are all the same bit value.
550 udccs = *ep->reg_udccs;
551 if (unlikely ((udccs & UDCCS_BO_RPC) == 0))
553 buf = req->req.buf + req->req.actual;
555 bufferspace = req->req.length - req->req.actual;
557 /* read all bytes from this packet */
558 if (likely (udccs & UDCCS_BO_RNE)) {
559 count = 1 + (0x0ff & *ep->reg_ubcr);
560 req->req.actual += min (count, bufferspace);
563 is_short = (count < ep->ep.maxpacket);
564 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
565 ep->ep.name, udccs, count,
566 is_short ? "/S" : "",
567 req, req->req.actual, req->req.length);
568 while (likely (count-- != 0)) {
569 u8 byte = (u8) *ep->reg_uddr;
571 if (unlikely (bufferspace == 0)) {
572 /* this happens when the driver's buffer
573 * is smaller than what the host sent.
574 * discard the extra data.
576 if (req->req.status != -EOVERFLOW)
577 DMSG("%s overflow %d\n",
579 req->req.status = -EOVERFLOW;
585 *ep->reg_udccs = UDCCS_BO_RPC;
586 /* RPC/RSP/RNE could now reflect the other packet buffer */
588 /* iso is one request per packet */
589 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
590 if (udccs & UDCCS_IO_ROF)
591 req->req.status = -EHOSTUNREACH;
592 /* more like "is_done" */
597 if (is_short || req->req.actual == req->req.length) {
599 if (list_empty(&ep->queue))
600 pio_irq_disable (ep->bEndpointAddress);
604 /* finished that packet. the next one may be waiting... */
610 * special ep0 version of the above. no UBCR0 or double buffering; status
611 * handshaking is magic. most device protocols don't need control-OUT.
612 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
613 * protocols do use them.
616 read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req)
619 unsigned bufferspace;
621 buf = req->req.buf + req->req.actual;
622 bufferspace = req->req.length - req->req.actual;
624 while (UDCCS0 & UDCCS0_RNE) {
627 if (unlikely (bufferspace == 0)) {
628 /* this happens when the driver's buffer
629 * is smaller than what the host sent.
630 * discard the extra data.
632 if (req->req.status != -EOVERFLOW)
633 DMSG("%s overflow\n", ep->ep.name);
634 req->req.status = -EOVERFLOW;
642 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR;
645 if (req->req.actual >= req->req.length)
648 /* finished that packet. the next one may be waiting... */
652 /*-------------------------------------------------------------------------*/
655 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
657 struct pxa25x_request *req;
658 struct pxa25x_ep *ep;
659 struct pxa25x_udc *dev;
662 req = container_of(_req, struct pxa25x_request, req);
663 if (unlikely (!_req || !_req->complete || !_req->buf
664 || !list_empty(&req->queue))) {
665 DMSG("%s, bad params\n", __func__);
669 ep = container_of(_ep, struct pxa25x_ep, ep);
670 if (unlikely (!_ep || (!ep->desc && ep->ep.name != ep0name))) {
671 DMSG("%s, bad ep\n", __func__);
676 if (unlikely (!dev->driver
677 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
678 DMSG("%s, bogus device state\n", __func__);
682 /* iso is always one packet per request, that's the only way
683 * we can report per-packet status. that also helps with dma.
685 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
686 && req->req.length > le16_to_cpu
687 (ep->desc->wMaxPacketSize)))
690 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n",
691 _ep->name, _req, _req->length, _req->buf);
693 local_irq_save(flags);
695 _req->status = -EINPROGRESS;
698 /* kickstart this i/o queue? */
699 if (list_empty(&ep->queue) && !ep->stopped) {
700 if (ep->desc == NULL/* ep0 */) {
701 unsigned length = _req->length;
703 switch (dev->ep0state) {
704 case EP0_IN_DATA_PHASE:
705 dev->stats.write.ops++;
706 if (write_ep0_fifo(ep, req))
710 case EP0_OUT_DATA_PHASE:
711 dev->stats.read.ops++;
713 if (dev->req_config) {
714 DBG(DBG_VERBOSE, "ep0 config ack%s\n",
715 dev->has_cfr ? "" : " raced");
717 UDCCFR = UDCCFR_AREN|UDCCFR_ACM
720 dev->ep0state = EP0_END_XFER;
721 local_irq_restore (flags);
724 if (dev->req_pending)
725 ep0start(dev, UDCCS0_IPR, "OUT");
726 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0
727 && read_ep0_fifo(ep, req))) {
735 DMSG("ep0 i/o, odd state %d\n", dev->ep0state);
736 local_irq_restore (flags);
739 /* can the FIFO can satisfy the request immediately? */
740 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
741 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0
742 && write_fifo(ep, req))
744 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0
745 && read_fifo(ep, req)) {
749 if (likely (req && ep->desc))
750 pio_irq_enable(ep->bEndpointAddress);
753 /* pio or dma irq handler advances the queue. */
754 if (likely(req != NULL))
755 list_add_tail(&req->queue, &ep->queue);
756 local_irq_restore(flags);
763 * nuke - dequeue ALL requests
765 static void nuke(struct pxa25x_ep *ep, int status)
767 struct pxa25x_request *req;
769 /* called with irqs blocked */
770 while (!list_empty(&ep->queue)) {
771 req = list_entry(ep->queue.next,
772 struct pxa25x_request,
774 done(ep, req, status);
777 pio_irq_disable (ep->bEndpointAddress);
781 /* dequeue JUST ONE request */
782 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
784 struct pxa25x_ep *ep;
785 struct pxa25x_request *req;
788 ep = container_of(_ep, struct pxa25x_ep, ep);
789 if (!_ep || ep->ep.name == ep0name)
792 local_irq_save(flags);
794 /* make sure it's actually queued on this endpoint */
795 list_for_each_entry (req, &ep->queue, queue) {
796 if (&req->req == _req)
799 if (&req->req != _req) {
800 local_irq_restore(flags);
804 done(ep, req, -ECONNRESET);
806 local_irq_restore(flags);
810 /*-------------------------------------------------------------------------*/
812 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
814 struct pxa25x_ep *ep;
817 ep = container_of(_ep, struct pxa25x_ep, ep);
819 || (!ep->desc && ep->ep.name != ep0name))
820 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
821 DMSG("%s, bad ep\n", __func__);
825 /* this path (reset toggle+halt) is needed to implement
826 * SET_INTERFACE on normal hardware. but it can't be
827 * done from software on the PXA UDC, and the hardware
828 * forgets to do it as part of SET_INTERFACE automagic.
830 DMSG("only host can clear %s halt\n", _ep->name);
834 local_irq_save(flags);
836 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
837 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0
838 || !list_empty(&ep->queue))) {
839 local_irq_restore(flags);
843 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
844 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF;
846 /* ep0 needs special care */
848 start_watchdog(ep->dev);
849 ep->dev->req_pending = 0;
850 ep->dev->ep0state = EP0_STALL;
852 /* and bulk/intr endpoints like dropping stalls too */
855 for (i = 0; i < 1000; i += 20) {
856 if (*ep->reg_udccs & UDCCS_BI_SST)
861 local_irq_restore(flags);
863 DBG(DBG_VERBOSE, "%s halt\n", _ep->name);
867 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
869 struct pxa25x_ep *ep;
871 ep = container_of(_ep, struct pxa25x_ep, ep);
873 DMSG("%s, bad ep\n", __func__);
876 /* pxa can't report unclaimed bytes from IN fifos */
877 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
879 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
880 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0)
883 return (*ep->reg_ubcr & 0xfff) + 1;
886 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
888 struct pxa25x_ep *ep;
890 ep = container_of(_ep, struct pxa25x_ep, ep);
891 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
892 DMSG("%s, bad ep\n", __func__);
896 /* toggle and halt bits stay unchanged */
898 /* for OUT, just read and discard the FIFO contents. */
899 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
900 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0)
901 (void) *ep->reg_uddr;
905 /* most IN status is the same, but ISO can't stall */
906 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
907 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
912 static struct usb_ep_ops pxa25x_ep_ops = {
913 .enable = pxa25x_ep_enable,
914 .disable = pxa25x_ep_disable,
916 .alloc_request = pxa25x_ep_alloc_request,
917 .free_request = pxa25x_ep_free_request,
919 .queue = pxa25x_ep_queue,
920 .dequeue = pxa25x_ep_dequeue,
922 .set_halt = pxa25x_ep_set_halt,
923 .fifo_status = pxa25x_ep_fifo_status,
924 .fifo_flush = pxa25x_ep_fifo_flush,
928 /* ---------------------------------------------------------------------------
929 * device-scoped parts of the api to the usb controller hardware
930 * ---------------------------------------------------------------------------
933 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
935 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff);
938 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
940 /* host may not have enabled remote wakeup */
941 if ((UDCCS0 & UDCCS0_DRWF) == 0)
942 return -EHOSTUNREACH;
943 udc_set_mask_UDCCR(UDCCR_RSM);
947 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
948 static void udc_enable (struct pxa25x_udc *);
949 static void udc_disable(struct pxa25x_udc *);
951 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not
954 static int pullup(struct pxa25x_udc *udc)
956 int is_active = udc->vbus && udc->pullup && !udc->suspended;
957 DMSG("%s\n", is_active ? "active" : "inactive");
961 /* Enable clock for USB device */
962 clk_enable(udc->clk);
967 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
968 DMSG("disconnect %s\n", udc->driver
969 ? udc->driver->driver.name
971 stop_activity(udc, udc->driver);
974 /* Disable clock for USB device */
975 clk_disable(udc->clk);
983 /* VBUS reporting logically comes from a transceiver */
984 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
986 struct pxa25x_udc *udc;
988 udc = container_of(_gadget, struct pxa25x_udc, gadget);
989 udc->vbus = is_active;
990 DMSG("vbus %s\n", is_active ? "supplied" : "inactive");
995 /* drivers may have software control over D+ pullup */
996 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
998 struct pxa25x_udc *udc;
1000 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1002 /* not all boards support pullup control */
1003 if (!udc->mach->gpio_pullup && !udc->mach->udc_command)
1006 udc->pullup = (is_active != 0);
1011 static const struct usb_gadget_ops pxa25x_udc_ops = {
1012 .get_frame = pxa25x_udc_get_frame,
1013 .wakeup = pxa25x_udc_wakeup,
1014 .vbus_session = pxa25x_udc_vbus_session,
1015 .pullup = pxa25x_udc_pullup,
1017 // .vbus_draw ... boards may consume current from VBUS, up to
1018 // 100-500mA based on config. the 500uA suspend ceiling means
1019 // that exclusively vbus-powered PXA designs violate USB specs.
1022 /*-------------------------------------------------------------------------*/
1024 #ifdef CONFIG_USB_GADGET_DEBUG_FS
1027 udc_seq_show(struct seq_file *m, void *_d)
1029 struct pxa25x_udc *dev = m->private;
1030 unsigned long flags;
1034 local_irq_save(flags);
1036 /* basic device status */
1037 seq_printf(m, DRIVER_DESC "\n"
1038 "%s version: %s\nGadget driver: %s\nHost %s\n\n",
1039 driver_name, DRIVER_VERSION SIZE_STR "(pio)",
1040 dev->driver ? dev->driver->driver.name : "(none)",
1041 is_vbus_present() ? "full speed" : "disconnected");
1043 /* registers for device and ep0 */
1045 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
1046 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL);
1050 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp,
1051 (tmp & UDCCR_REM) ? " rem" : "",
1052 (tmp & UDCCR_RSTIR) ? " rstir" : "",
1053 (tmp & UDCCR_SRM) ? " srm" : "",
1054 (tmp & UDCCR_SUSIR) ? " susir" : "",
1055 (tmp & UDCCR_RESIR) ? " resir" : "",
1056 (tmp & UDCCR_RSM) ? " rsm" : "",
1057 (tmp & UDCCR_UDA) ? " uda" : "",
1058 (tmp & UDCCR_UDE) ? " ude" : "");
1062 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp,
1063 (tmp & UDCCS0_SA) ? " sa" : "",
1064 (tmp & UDCCS0_RNE) ? " rne" : "",
1065 (tmp & UDCCS0_FST) ? " fst" : "",
1066 (tmp & UDCCS0_SST) ? " sst" : "",
1067 (tmp & UDCCS0_DRWF) ? " dwrf" : "",
1068 (tmp & UDCCS0_FTF) ? " ftf" : "",
1069 (tmp & UDCCS0_IPR) ? " ipr" : "",
1070 (tmp & UDCCS0_OPR) ? " opr" : "");
1075 "udccfr %02X =%s%s\n", tmp,
1076 (tmp & UDCCFR_AREN) ? " aren" : "",
1077 (tmp & UDCCFR_ACM) ? " acm" : "");
1080 if (!is_vbus_present() || !dev->driver)
1083 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n",
1084 dev->stats.write.bytes, dev->stats.write.ops,
1085 dev->stats.read.bytes, dev->stats.read.ops,
1088 /* dump endpoint queues */
1089 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1090 struct pxa25x_ep *ep = &dev->ep [i];
1091 struct pxa25x_request *req;
1094 const struct usb_endpoint_descriptor *desc;
1099 tmp = *dev->ep [i].reg_udccs;
1101 "%s max %d %s udccs %02x irqs %lu\n",
1102 ep->ep.name, le16_to_cpu(desc->wMaxPacketSize),
1103 "pio", tmp, ep->pio_irqs);
1104 /* TODO translate all five groups of udccs bits! */
1106 } else /* ep0 should only have one transfer queued */
1107 seq_printf(m, "ep0 max 16 pio irqs %lu\n",
1110 if (list_empty(&ep->queue)) {
1111 seq_printf(m, "\t(nothing queued)\n");
1114 list_for_each_entry(req, &ep->queue, queue) {
1116 "\treq %p len %d/%d buf %p\n",
1117 &req->req, req->req.actual,
1118 req->req.length, req->req.buf);
1123 local_irq_restore(flags);
1128 udc_debugfs_open(struct inode *inode, struct file *file)
1130 return single_open(file, udc_seq_show, inode->i_private);
1133 static const struct file_operations debug_fops = {
1134 .open = udc_debugfs_open,
1136 .llseek = seq_lseek,
1137 .release = single_release,
1138 .owner = THIS_MODULE,
1141 #define create_debug_files(dev) \
1143 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \
1144 S_IRUGO, NULL, dev, &debug_fops); \
1146 #define remove_debug_files(dev) \
1148 if (dev->debugfs_udc) \
1149 debugfs_remove(dev->debugfs_udc); \
1152 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */
1154 #define create_debug_files(dev) do {} while (0)
1155 #define remove_debug_files(dev) do {} while (0)
1157 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */
1159 /*-------------------------------------------------------------------------*/
1162 * udc_disable - disable USB device controller
1164 static void udc_disable(struct pxa25x_udc *dev)
1166 /* block all irqs */
1167 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1168 UICR0 = UICR1 = 0xff;
1171 /* if hardware supports it, disconnect from usb */
1174 udc_clear_mask_UDCCR(UDCCR_UDE);
1177 dev->gadget.speed = USB_SPEED_UNKNOWN;
1182 * udc_reinit - initialize software state
1184 static void udc_reinit(struct pxa25x_udc *dev)
1188 /* device/ep0 records init */
1189 INIT_LIST_HEAD (&dev->gadget.ep_list);
1190 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list);
1191 dev->ep0state = EP0_IDLE;
1193 /* basic endpoint records init */
1194 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1195 struct pxa25x_ep *ep = &dev->ep[i];
1198 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list);
1202 INIT_LIST_HEAD (&ep->queue);
1206 /* the rest was statically initialized, and is read-only */
1209 /* until it's enabled, this UDC should be completely invisible
1212 static void udc_enable (struct pxa25x_udc *dev)
1214 udc_clear_mask_UDCCR(UDCCR_UDE);
1216 /* try to clear these bits before we enable the udc */
1217 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR);
1220 dev->gadget.speed = USB_SPEED_UNKNOWN;
1221 dev->stats.irqs = 0;
1224 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1226 * - if RESET is already in progress, ack interrupt
1227 * - unmask reset interrupt
1229 udc_set_mask_UDCCR(UDCCR_UDE);
1230 if (!(UDCCR & UDCCR_UDA))
1231 udc_ack_int_UDCCR(UDCCR_RSTIR);
1233 if (dev->has_cfr /* UDC_RES2 is defined */) {
1234 /* pxa255 (a0+) can avoid a set_config race that could
1235 * prevent gadget drivers from configuring correctly
1237 UDCCFR = UDCCFR_ACM | UDCCFR_MB1;
1239 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1)
1240 * which could result in missing packets and interrupts.
1241 * supposedly one bit per endpoint, controlling whether it
1242 * double buffers or not; ACM/AREN bits fit into the holes.
1243 * zero bits (like USIR0_IRx) disable double buffering.
1249 /* enable suspend/resume and reset irqs */
1250 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1252 /* enable ep0 irqs */
1253 UICR0 &= ~UICR0_IM0;
1255 /* if hardware supports it, pullup D+ and wait for reset */
1260 /* when a driver is successfully registered, it will receive
1261 * control requests including set_configuration(), which enables
1262 * non-control requests. then usb traffic follows until a
1263 * disconnect is reported. then a host may connect again, or
1264 * the driver might get unbound.
1266 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1268 struct pxa25x_udc *dev = the_controller;
1272 || driver->speed < USB_SPEED_FULL
1274 || !driver->disconnect
1282 /* first hook up the driver ... */
1283 dev->driver = driver;
1284 dev->gadget.dev.driver = &driver->driver;
1287 retval = device_add (&dev->gadget.dev);
1291 dev->gadget.dev.driver = NULL;
1294 retval = driver->bind(&dev->gadget);
1296 DMSG("bind to driver %s --> error %d\n",
1297 driver->driver.name, retval);
1298 device_del (&dev->gadget.dev);
1302 /* ... then enable host detection and ep0; and we're ready
1303 * for set_configuration as well as eventual disconnect.
1305 DMSG("registered gadget driver '%s'\n", driver->driver.name);
1310 EXPORT_SYMBOL(usb_gadget_register_driver);
1313 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1317 /* don't disconnect drivers more than once */
1318 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1320 dev->gadget.speed = USB_SPEED_UNKNOWN;
1322 /* prevent new request submissions, kill any outstanding requests */
1323 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1324 struct pxa25x_ep *ep = &dev->ep[i];
1327 nuke(ep, -ESHUTDOWN);
1329 del_timer_sync(&dev->timer);
1331 /* report disconnect; the driver is already quiesced */
1333 driver->disconnect(&dev->gadget);
1335 /* re-init driver-visible data structures */
1339 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1341 struct pxa25x_udc *dev = the_controller;
1345 if (!driver || driver != dev->driver || !driver->unbind)
1348 local_irq_disable();
1351 stop_activity(dev, driver);
1354 driver->unbind(&dev->gadget);
1355 dev->gadget.dev.driver = NULL;
1358 device_del (&dev->gadget.dev);
1360 DMSG("unregistered gadget driver '%s'\n", driver->driver.name);
1364 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1367 /*-------------------------------------------------------------------------*/
1369 #ifdef CONFIG_ARCH_LUBBOCK
1371 /* Lubbock has separate connect and disconnect irqs. More typical designs
1372 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup.
1376 lubbock_vbus_irq(int irq, void *_dev)
1378 struct pxa25x_udc *dev = _dev;
1383 case LUBBOCK_USB_IRQ:
1385 disable_irq(LUBBOCK_USB_IRQ);
1386 enable_irq(LUBBOCK_USB_DISC_IRQ);
1388 case LUBBOCK_USB_DISC_IRQ:
1390 disable_irq(LUBBOCK_USB_DISC_IRQ);
1391 enable_irq(LUBBOCK_USB_IRQ);
1397 pxa25x_udc_vbus_session(&dev->gadget, vbus);
1403 static irqreturn_t udc_vbus_irq(int irq, void *_dev)
1405 struct pxa25x_udc *dev = _dev;
1407 pxa25x_udc_vbus_session(&dev->gadget, is_vbus_present());
1412 /*-------------------------------------------------------------------------*/
1414 static inline void clear_ep_state (struct pxa25x_udc *dev)
1418 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1419 * fifos, and pending transactions mustn't be continued in any case.
1421 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1422 nuke(&dev->ep[i], -ECONNABORTED);
1425 static void udc_watchdog(unsigned long _dev)
1427 struct pxa25x_udc *dev = (void *)_dev;
1429 local_irq_disable();
1430 if (dev->ep0state == EP0_STALL
1431 && (UDCCS0 & UDCCS0_FST) == 0
1432 && (UDCCS0 & UDCCS0_SST) == 0) {
1433 UDCCS0 = UDCCS0_FST|UDCCS0_FTF;
1434 DBG(DBG_VERBOSE, "ep0 re-stall\n");
1435 start_watchdog(dev);
1440 static void handle_ep0 (struct pxa25x_udc *dev)
1442 u32 udccs0 = UDCCS0;
1443 struct pxa25x_ep *ep = &dev->ep [0];
1444 struct pxa25x_request *req;
1446 struct usb_ctrlrequest r;
1451 if (list_empty(&ep->queue))
1454 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1456 /* clear stall status */
1457 if (udccs0 & UDCCS0_SST) {
1459 UDCCS0 = UDCCS0_SST;
1460 del_timer(&dev->timer);
1464 /* previous request unfinished? non-error iff back-to-back ... */
1465 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1467 del_timer(&dev->timer);
1471 switch (dev->ep0state) {
1473 /* late-breaking status? */
1476 /* start control request? */
1477 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1478 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1483 /* read SETUP packet */
1484 for (i = 0; i < 8; i++) {
1485 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) {
1487 DMSG("SETUP %d!\n", i);
1490 u.raw [i] = (u8) UDDR0;
1492 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0))
1496 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n",
1497 u.r.bRequestType, u.r.bRequest,
1498 le16_to_cpu(u.r.wValue),
1499 le16_to_cpu(u.r.wIndex),
1500 le16_to_cpu(u.r.wLength));
1502 /* cope with automagic for some standard requests. */
1503 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1504 == USB_TYPE_STANDARD;
1505 dev->req_config = 0;
1506 dev->req_pending = 1;
1507 switch (u.r.bRequest) {
1508 /* hardware restricts gadget drivers here! */
1509 case USB_REQ_SET_CONFIGURATION:
1510 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1511 /* reflect hardware's automagic
1512 * up to the gadget driver.
1515 dev->req_config = 1;
1516 clear_ep_state(dev);
1517 /* if !has_cfr, there's no synch
1518 * else use AREN (later) not SA|OPR
1519 * USIR0_IR0 acts edge sensitive
1523 /* ... and here, even more ... */
1524 case USB_REQ_SET_INTERFACE:
1525 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1526 /* udc hardware is broken by design:
1527 * - altsetting may only be zero;
1528 * - hw resets all interfaces' eps;
1529 * - ep reset doesn't include halt(?).
1531 DMSG("broken set_interface (%d/%d)\n",
1532 le16_to_cpu(u.r.wIndex),
1533 le16_to_cpu(u.r.wValue));
1537 /* hardware was supposed to hide this */
1538 case USB_REQ_SET_ADDRESS:
1539 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1540 ep0start(dev, 0, "address");
1546 if (u.r.bRequestType & USB_DIR_IN)
1547 dev->ep0state = EP0_IN_DATA_PHASE;
1549 dev->ep0state = EP0_OUT_DATA_PHASE;
1551 i = dev->driver->setup(&dev->gadget, &u.r);
1553 /* hardware automagic preventing STALL... */
1554 if (dev->req_config) {
1555 /* hardware sometimes neglects to tell
1556 * tell us about config change events,
1557 * so later ones may fail...
1559 WARNING("config change %02x fail %d?\n",
1562 /* TODO experiment: if has_cfr,
1563 * hardware didn't ACK; maybe we
1564 * could actually STALL!
1567 DBG(DBG_VERBOSE, "protocol STALL, "
1568 "%02x err %d\n", UDCCS0, i);
1570 /* the watchdog timer helps deal with cases
1571 * where udc seems to clear FST wrongly, and
1572 * then NAKs instead of STALLing.
1574 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1575 start_watchdog(dev);
1576 dev->ep0state = EP0_STALL;
1578 /* deferred i/o == no response yet */
1579 } else if (dev->req_pending) {
1580 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1581 || dev->req_std || u.r.wLength))
1582 ep0start(dev, 0, "defer");
1584 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1587 /* expect at least one data or status stage irq */
1590 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1591 == (UDCCS0_OPR|UDCCS0_SA))) {
1594 /* pxa210/250 erratum 131 for B0/B1 says RNE lies.
1595 * still observed on a pxa255 a0.
1597 DBG(DBG_VERBOSE, "e131\n");
1600 /* read SETUP data, but don't trust it too much */
1601 for (i = 0; i < 8; i++)
1602 u.raw [i] = (u8) UDDR0;
1603 if ((u.r.bRequestType & USB_RECIP_MASK)
1606 if (u.word [0] == 0 && u.word [1] == 0)
1610 /* some random early IRQ:
1613 * - OPR got set, without SA (likely status stage)
1615 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR);
1618 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1619 if (udccs0 & UDCCS0_OPR) {
1620 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF;
1621 DBG(DBG_VERBOSE, "ep0in premature status\n");
1625 } else /* irq was IPR clearing */ {
1627 /* this IN packet might finish the request */
1628 (void) write_ep0_fifo(ep, req);
1629 } /* else IN token before response was written */
1632 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1633 if (udccs0 & UDCCS0_OPR) {
1635 /* this OUT packet might finish the request */
1636 if (read_ep0_fifo(ep, req))
1638 /* else more OUT packets expected */
1639 } /* else OUT token before read was issued */
1640 } else /* irq was IPR clearing */ {
1641 DBG(DBG_VERBOSE, "ep0out premature status\n");
1650 /* ack control-IN status (maybe in-zlp was skipped)
1651 * also appears after some config change events.
1653 if (udccs0 & UDCCS0_OPR)
1654 UDCCS0 = UDCCS0_OPR;
1658 UDCCS0 = UDCCS0_FST;
1664 static void handle_ep(struct pxa25x_ep *ep)
1666 struct pxa25x_request *req;
1667 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1673 if (likely (!list_empty(&ep->queue)))
1674 req = list_entry(ep->queue.next,
1675 struct pxa25x_request, queue);
1679 // TODO check FST handling
1681 udccs = *ep->reg_udccs;
1682 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1684 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1685 tmp |= UDCCS_BI_SST;
1688 *ep->reg_udccs = tmp;
1689 if (req && likely ((udccs & UDCCS_BI_TFS) != 0))
1690 completed = write_fifo(ep, req);
1692 } else { /* irq from RPC (or for ISO, ROF) */
1693 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1694 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1696 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1699 *ep->reg_udccs = tmp;
1701 /* fifos can hold packets, ready for reading... */
1703 completed = read_fifo(ep, req);
1705 pio_irq_disable (ep->bEndpointAddress);
1708 } while (completed);
1712 * pxa25x_udc_irq - interrupt handler
1714 * avoid delays in ep0 processing. the control handshaking isn't always
1715 * under software control (pxa250c0 and the pxa255 are better), and delays
1716 * could cause usb protocol errors.
1719 pxa25x_udc_irq(int irq, void *_dev)
1721 struct pxa25x_udc *dev = _dev;
1730 /* SUSpend Interrupt Request */
1731 if (unlikely(udccr & UDCCR_SUSIR)) {
1732 udc_ack_int_UDCCR(UDCCR_SUSIR);
1734 DBG(DBG_VERBOSE, "USB suspend%s\n", is_vbus_present()
1735 ? "" : "+disconnect");
1737 if (!is_vbus_present())
1738 stop_activity(dev, dev->driver);
1739 else if (dev->gadget.speed != USB_SPEED_UNKNOWN
1741 && dev->driver->suspend)
1742 dev->driver->suspend(&dev->gadget);
1746 /* RESume Interrupt Request */
1747 if (unlikely(udccr & UDCCR_RESIR)) {
1748 udc_ack_int_UDCCR(UDCCR_RESIR);
1750 DBG(DBG_VERBOSE, "USB resume\n");
1752 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1754 && dev->driver->resume
1755 && is_vbus_present())
1756 dev->driver->resume(&dev->gadget);
1759 /* ReSeT Interrupt Request - USB reset */
1760 if (unlikely(udccr & UDCCR_RSTIR)) {
1761 udc_ack_int_UDCCR(UDCCR_RSTIR);
1764 if ((UDCCR & UDCCR_UDA) == 0) {
1765 DBG(DBG_VERBOSE, "USB reset start\n");
1767 /* reset driver and endpoints,
1768 * in case that's not yet done
1770 stop_activity (dev, dev->driver);
1773 DBG(DBG_VERBOSE, "USB reset end\n");
1774 dev->gadget.speed = USB_SPEED_FULL;
1775 memset(&dev->stats, 0, sizeof dev->stats);
1776 /* driver and endpoints are still reset */
1780 u32 usir0 = USIR0 & ~UICR0;
1781 u32 usir1 = USIR1 & ~UICR1;
1784 if (unlikely (!usir0 && !usir1))
1787 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0);
1789 /* control traffic */
1790 if (usir0 & USIR0_IR0) {
1791 dev->ep[0].pio_irqs++;
1796 /* endpoint data transfers */
1797 for (i = 0; i < 8; i++) {
1800 if (i && (usir0 & tmp)) {
1801 handle_ep(&dev->ep[i]);
1806 handle_ep(&dev->ep[i+8]);
1813 /* we could also ask for 1 msec SOF (SIR) interrupts */
1819 /*-------------------------------------------------------------------------*/
1821 static void nop_release (struct device *dev)
1823 DMSG("%s %s\n", __func__, dev_name(dev));
1826 /* this uses load-time allocation and initialization (instead of
1827 * doing it at run-time) to save code, eliminate fault paths, and
1828 * be more obviously correct.
1830 static struct pxa25x_udc memory = {
1832 .ops = &pxa25x_udc_ops,
1833 .ep0 = &memory.ep[0].ep,
1834 .name = driver_name,
1836 .init_name = "gadget",
1837 .release = nop_release,
1841 /* control endpoint */
1845 .ops = &pxa25x_ep_ops,
1846 .maxpacket = EP0_FIFO_SIZE,
1849 .reg_udccs = &UDCCS0,
1853 /* first group of endpoints */
1856 .name = "ep1in-bulk",
1857 .ops = &pxa25x_ep_ops,
1858 .maxpacket = BULK_FIFO_SIZE,
1861 .fifo_size = BULK_FIFO_SIZE,
1862 .bEndpointAddress = USB_DIR_IN | 1,
1863 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1864 .reg_udccs = &UDCCS1,
1869 .name = "ep2out-bulk",
1870 .ops = &pxa25x_ep_ops,
1871 .maxpacket = BULK_FIFO_SIZE,
1874 .fifo_size = BULK_FIFO_SIZE,
1875 .bEndpointAddress = 2,
1876 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1877 .reg_udccs = &UDCCS2,
1881 #ifndef CONFIG_USB_PXA25X_SMALL
1884 .name = "ep3in-iso",
1885 .ops = &pxa25x_ep_ops,
1886 .maxpacket = ISO_FIFO_SIZE,
1889 .fifo_size = ISO_FIFO_SIZE,
1890 .bEndpointAddress = USB_DIR_IN | 3,
1891 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1892 .reg_udccs = &UDCCS3,
1897 .name = "ep4out-iso",
1898 .ops = &pxa25x_ep_ops,
1899 .maxpacket = ISO_FIFO_SIZE,
1902 .fifo_size = ISO_FIFO_SIZE,
1903 .bEndpointAddress = 4,
1904 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1905 .reg_udccs = &UDCCS4,
1911 .name = "ep5in-int",
1912 .ops = &pxa25x_ep_ops,
1913 .maxpacket = INT_FIFO_SIZE,
1916 .fifo_size = INT_FIFO_SIZE,
1917 .bEndpointAddress = USB_DIR_IN | 5,
1918 .bmAttributes = USB_ENDPOINT_XFER_INT,
1919 .reg_udccs = &UDCCS5,
1923 /* second group of endpoints */
1926 .name = "ep6in-bulk",
1927 .ops = &pxa25x_ep_ops,
1928 .maxpacket = BULK_FIFO_SIZE,
1931 .fifo_size = BULK_FIFO_SIZE,
1932 .bEndpointAddress = USB_DIR_IN | 6,
1933 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1934 .reg_udccs = &UDCCS6,
1939 .name = "ep7out-bulk",
1940 .ops = &pxa25x_ep_ops,
1941 .maxpacket = BULK_FIFO_SIZE,
1944 .fifo_size = BULK_FIFO_SIZE,
1945 .bEndpointAddress = 7,
1946 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1947 .reg_udccs = &UDCCS7,
1953 .name = "ep8in-iso",
1954 .ops = &pxa25x_ep_ops,
1955 .maxpacket = ISO_FIFO_SIZE,
1958 .fifo_size = ISO_FIFO_SIZE,
1959 .bEndpointAddress = USB_DIR_IN | 8,
1960 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1961 .reg_udccs = &UDCCS8,
1966 .name = "ep9out-iso",
1967 .ops = &pxa25x_ep_ops,
1968 .maxpacket = ISO_FIFO_SIZE,
1971 .fifo_size = ISO_FIFO_SIZE,
1972 .bEndpointAddress = 9,
1973 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1974 .reg_udccs = &UDCCS9,
1980 .name = "ep10in-int",
1981 .ops = &pxa25x_ep_ops,
1982 .maxpacket = INT_FIFO_SIZE,
1985 .fifo_size = INT_FIFO_SIZE,
1986 .bEndpointAddress = USB_DIR_IN | 10,
1987 .bmAttributes = USB_ENDPOINT_XFER_INT,
1988 .reg_udccs = &UDCCS10,
1989 .reg_uddr = &UDDR10,
1992 /* third group of endpoints */
1995 .name = "ep11in-bulk",
1996 .ops = &pxa25x_ep_ops,
1997 .maxpacket = BULK_FIFO_SIZE,
2000 .fifo_size = BULK_FIFO_SIZE,
2001 .bEndpointAddress = USB_DIR_IN | 11,
2002 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2003 .reg_udccs = &UDCCS11,
2004 .reg_uddr = &UDDR11,
2008 .name = "ep12out-bulk",
2009 .ops = &pxa25x_ep_ops,
2010 .maxpacket = BULK_FIFO_SIZE,
2013 .fifo_size = BULK_FIFO_SIZE,
2014 .bEndpointAddress = 12,
2015 .bmAttributes = USB_ENDPOINT_XFER_BULK,
2016 .reg_udccs = &UDCCS12,
2017 .reg_ubcr = &UBCR12,
2018 .reg_uddr = &UDDR12,
2022 .name = "ep13in-iso",
2023 .ops = &pxa25x_ep_ops,
2024 .maxpacket = ISO_FIFO_SIZE,
2027 .fifo_size = ISO_FIFO_SIZE,
2028 .bEndpointAddress = USB_DIR_IN | 13,
2029 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2030 .reg_udccs = &UDCCS13,
2031 .reg_uddr = &UDDR13,
2035 .name = "ep14out-iso",
2036 .ops = &pxa25x_ep_ops,
2037 .maxpacket = ISO_FIFO_SIZE,
2040 .fifo_size = ISO_FIFO_SIZE,
2041 .bEndpointAddress = 14,
2042 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
2043 .reg_udccs = &UDCCS14,
2044 .reg_ubcr = &UBCR14,
2045 .reg_uddr = &UDDR14,
2049 .name = "ep15in-int",
2050 .ops = &pxa25x_ep_ops,
2051 .maxpacket = INT_FIFO_SIZE,
2054 .fifo_size = INT_FIFO_SIZE,
2055 .bEndpointAddress = USB_DIR_IN | 15,
2056 .bmAttributes = USB_ENDPOINT_XFER_INT,
2057 .reg_udccs = &UDCCS15,
2058 .reg_uddr = &UDDR15,
2060 #endif /* !CONFIG_USB_PXA25X_SMALL */
2063 #define CP15R0_VENDOR_MASK 0xffffe000
2065 #if defined(CONFIG_ARCH_PXA)
2066 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */
2068 #elif defined(CONFIG_ARCH_IXP4XX)
2069 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */
2073 #define CP15R0_PROD_MASK 0x000003f0
2074 #define PXA25x 0x00000100 /* and PXA26x */
2075 #define PXA210 0x00000120
2077 #define CP15R0_REV_MASK 0x0000000f
2079 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK)
2081 #define PXA255_A0 0x00000106 /* or PXA260_B1 */
2082 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */
2083 #define PXA250_B2 0x00000104
2084 #define PXA250_B1 0x00000103 /* or PXA260_A0 */
2085 #define PXA250_B0 0x00000102
2086 #define PXA250_A1 0x00000101
2087 #define PXA250_A0 0x00000100
2089 #define PXA210_C0 0x00000125
2090 #define PXA210_B2 0x00000124
2091 #define PXA210_B1 0x00000123
2092 #define PXA210_B0 0x00000122
2093 #define IXP425_A0 0x000001c1
2094 #define IXP425_B0 0x000001f1
2095 #define IXP465_AD 0x00000200
2098 * probe - binds to the platform device
2100 static int __init pxa25x_udc_probe(struct platform_device *pdev)
2102 struct pxa25x_udc *dev = &memory;
2103 int retval, vbus_irq, irq;
2106 /* insist on Intel/ARM/XScale */
2107 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev));
2108 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) {
2109 pr_err("%s: not XScale!\n", driver_name);
2113 /* trigger chiprev-specific logic */
2114 switch (chiprev & CP15R0_PRODREV_MASK) {
2115 #if defined(CONFIG_ARCH_PXA)
2121 /* A0/A1 "not released"; ep 13, 15 unusable */
2123 case PXA250_B2: case PXA210_B2:
2124 case PXA250_B1: case PXA210_B1:
2125 case PXA250_B0: case PXA210_B0:
2126 /* OUT-DMA is broken ... */
2128 case PXA250_C0: case PXA210_C0:
2130 #elif defined(CONFIG_ARCH_IXP4XX)
2138 pr_err("%s: unrecognized processor: %08x\n",
2139 driver_name, chiprev);
2140 /* iop3xx, ixp4xx, ... */
2144 irq = platform_get_irq(pdev, 0);
2148 dev->clk = clk_get(&pdev->dev, NULL);
2149 if (IS_ERR(dev->clk)) {
2150 retval = PTR_ERR(dev->clk);
2154 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq,
2155 dev->has_cfr ? "" : " (!cfr)",
2159 /* other non-static parts of init */
2160 dev->dev = &pdev->dev;
2161 dev->mach = pdev->dev.platform_data;
2163 if (dev->mach->gpio_vbus) {
2164 if ((retval = gpio_request(dev->mach->gpio_vbus,
2165 "pxa25x_udc GPIO VBUS"))) {
2167 "can't get vbus gpio %d, err: %d\n",
2168 dev->mach->gpio_vbus, retval);
2171 gpio_direction_input(dev->mach->gpio_vbus);
2172 vbus_irq = gpio_to_irq(dev->mach->gpio_vbus);
2176 if (dev->mach->gpio_pullup) {
2177 if ((retval = gpio_request(dev->mach->gpio_pullup,
2178 "pca25x_udc GPIO PULLUP"))) {
2180 "can't get pullup gpio %d, err: %d\n",
2181 dev->mach->gpio_pullup, retval);
2182 goto err_gpio_pullup;
2184 gpio_direction_output(dev->mach->gpio_pullup, 0);
2187 init_timer(&dev->timer);
2188 dev->timer.function = udc_watchdog;
2189 dev->timer.data = (unsigned long) dev;
2191 device_initialize(&dev->gadget.dev);
2192 dev->gadget.dev.parent = &pdev->dev;
2193 dev->gadget.dev.dma_mask = pdev->dev.dma_mask;
2195 the_controller = dev;
2196 platform_set_drvdata(pdev, dev);
2201 dev->vbus = is_vbus_present();
2203 /* irq setup after old hardware state is cleaned up */
2204 retval = request_irq(irq, pxa25x_udc_irq,
2205 IRQF_DISABLED, driver_name, dev);
2207 pr_err("%s: can't get irq %d, err %d\n",
2208 driver_name, irq, retval);
2213 #ifdef CONFIG_ARCH_LUBBOCK
2214 if (machine_is_lubbock()) {
2215 retval = request_irq(LUBBOCK_USB_DISC_IRQ,
2217 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2220 pr_err("%s: can't get irq %i, err %d\n",
2221 driver_name, LUBBOCK_USB_DISC_IRQ, retval);
2225 retval = request_irq(LUBBOCK_USB_IRQ,
2227 IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
2230 pr_err("%s: can't get irq %i, err %d\n",
2231 driver_name, LUBBOCK_USB_IRQ, retval);
2232 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2238 retval = request_irq(vbus_irq, udc_vbus_irq,
2239 IRQF_DISABLED | IRQF_SAMPLE_RANDOM |
2240 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
2243 pr_err("%s: can't get irq %i, err %d\n",
2244 driver_name, vbus_irq, retval);
2248 create_debug_files(dev);
2253 #ifdef CONFIG_ARCH_LUBBOCK
2254 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2259 if (dev->mach->gpio_pullup)
2260 gpio_free(dev->mach->gpio_pullup);
2262 if (dev->mach->gpio_vbus)
2263 gpio_free(dev->mach->gpio_vbus);
2270 static void pxa25x_udc_shutdown(struct platform_device *_dev)
2275 static int __exit pxa25x_udc_remove(struct platform_device *pdev)
2277 struct pxa25x_udc *dev = platform_get_drvdata(pdev);
2285 remove_debug_files(dev);
2288 free_irq(platform_get_irq(pdev, 0), dev);
2291 #ifdef CONFIG_ARCH_LUBBOCK
2292 if (machine_is_lubbock()) {
2293 free_irq(LUBBOCK_USB_DISC_IRQ, dev);
2294 free_irq(LUBBOCK_USB_IRQ, dev);
2297 if (dev->mach->gpio_vbus) {
2298 free_irq(gpio_to_irq(dev->mach->gpio_vbus), dev);
2299 gpio_free(dev->mach->gpio_vbus);
2301 if (dev->mach->gpio_pullup)
2302 gpio_free(dev->mach->gpio_pullup);
2306 platform_set_drvdata(pdev, NULL);
2307 the_controller = NULL;
2311 /*-------------------------------------------------------------------------*/
2315 /* USB suspend (controlled by the host) and system suspend (controlled
2316 * by the PXA) don't necessarily work well together. If USB is active,
2317 * the 48 MHz clock is required; so the system can't enter 33 MHz idle
2318 * mode, or any deeper PM saving state.
2320 * For now, we punt and forcibly disconnect from the USB host when PXA
2321 * enters any suspend state. While we're disconnected, we always disable
2322 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states.
2323 * Boards without software pullup control shouldn't use those states.
2324 * VBUS IRQs should probably be ignored so that the PXA device just acts
2325 * "dead" to USB hosts until system resume.
2327 static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state)
2329 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2330 unsigned long flags;
2332 if (!udc->mach->gpio_pullup && !udc->mach->udc_command)
2333 WARNING("USB host won't detect disconnect!\n");
2336 local_irq_save(flags);
2338 local_irq_restore(flags);
2343 static int pxa25x_udc_resume(struct platform_device *dev)
2345 struct pxa25x_udc *udc = platform_get_drvdata(dev);
2346 unsigned long flags;
2349 local_irq_save(flags);
2351 local_irq_restore(flags);
2357 #define pxa25x_udc_suspend NULL
2358 #define pxa25x_udc_resume NULL
2361 /*-------------------------------------------------------------------------*/
2363 static struct platform_driver udc_driver = {
2364 .shutdown = pxa25x_udc_shutdown,
2365 .remove = __exit_p(pxa25x_udc_remove),
2366 .suspend = pxa25x_udc_suspend,
2367 .resume = pxa25x_udc_resume,
2369 .owner = THIS_MODULE,
2370 .name = "pxa25x-udc",
2374 static int __init udc_init(void)
2376 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION);
2377 return platform_driver_probe(&udc_driver, pxa25x_udc_probe);
2379 module_init(udc_init);
2381 static void __exit udc_exit(void)
2383 platform_driver_unregister(&udc_driver);
2385 module_exit(udc_exit);
2387 MODULE_DESCRIPTION(DRIVER_DESC);
2388 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
2389 MODULE_LICENSE("GPL");
2390 MODULE_ALIAS("platform:pxa25x-udc");