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
9 * Copyright (C) 2012 Lukasz Dalek <luk0104@gmail.com>
11 * SPDX-License-Identifier: GPL-2.0+
13 * MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
16 #define CONFIG_USB_PXA25X_SMALL
17 #define DRIVER_NAME "pxa25x_udc_linux"
18 #define ARCH_HAS_PREFETCH
22 #include <asm/byteorder.h>
23 #include <asm/system.h>
24 #include <asm/mach-types.h>
25 #include <asm/unaligned.h>
26 #include <linux/compat.h>
29 #include <asm/arch/pxa.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/gadget.h>
33 #include <usb/lin_gadget_compat.h>
34 #include <asm/arch/pxa-regs.h>
36 #include "pxa25x_udc.h"
39 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
40 * series processors. The UDC for the IXP 4xx series is very similar.
41 * There are fifteen endpoints, in addition to ep0.
43 * Such controller drivers work with a gadget driver. The gadget driver
44 * returns descriptors, implements configuration and data protocols used
45 * by the host to interact with this device, and allocates endpoints to
46 * the different protocol interfaces. The controller driver virtualizes
47 * usb hardware so that the gadget drivers will be more portable.
49 * This UDC hardware wants to implement a bit too much USB protocol, so
50 * it constrains the sorts of USB configuration change events that work.
51 * The errata for these chips are misleading; some "fixed" bugs from
52 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
54 * Note that the UDC hardware supports DMA (except on IXP) but that's
55 * not used here. IN-DMA (to host) is simple enough, when the data is
56 * suitably aligned (16 bytes) ... the network stack doesn't do that,
57 * other software can. OUT-DMA is buggy in most chip versions, as well
58 * as poorly designed (data toggle not automatic). So this driver won't
59 * bother using DMA. (Mostly-working IN-DMA support was available in
60 * kernels before 2.6.23, but was never enabled or well tested.)
63 #define DRIVER_VERSION "18-August-2012"
64 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
66 static const char driver_name[] = "pxa25x_udc";
67 static const char ep0name[] = "ep0";
70 static inline void start_watchdog(struct pxa25x_udc *udc)
72 debug("Started watchdog\n");
73 udc->watchdog.base = get_timer(0);
74 udc->watchdog.running = 1;
77 static inline void stop_watchdog(struct pxa25x_udc *udc)
79 udc->watchdog.running = 0;
80 debug("Stopped watchdog\n");
83 static inline void test_watchdog(struct pxa25x_udc *udc)
85 if (!udc->watchdog.running)
88 debug("watchdog %ld %ld\n", get_timer(udc->watchdog.base),
89 udc->watchdog.period);
91 if (get_timer(udc->watchdog.base) >= udc->watchdog.period) {
93 udc->watchdog.function(udc);
97 static void udc_watchdog(struct pxa25x_udc *dev)
99 uint32_t udccs0 = readl(&dev->regs->udccs[0]);
101 debug("Fired up udc_watchdog\n");
104 if (dev->ep0state == EP0_STALL
105 && (udccs0 & UDCCS0_FST) == 0
106 && (udccs0 & UDCCS0_SST) == 0) {
107 writel(UDCCS0_FST|UDCCS0_FTF, &dev->regs->udccs[0]);
108 debug("ep0 re-stall\n");
116 static const char * const state_name[] = {
118 "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
119 "EP0_END_XFER", "EP0_STALL"
123 dump_udccr(const char *label)
125 u32 udccr = readl(&UDC_REGS->udccr);
126 debug("%s %02X =%s%s%s%s%s%s%s%s\n",
128 (udccr & UDCCR_REM) ? " rem" : "",
129 (udccr & UDCCR_RSTIR) ? " rstir" : "",
130 (udccr & UDCCR_SRM) ? " srm" : "",
131 (udccr & UDCCR_SUSIR) ? " susir" : "",
132 (udccr & UDCCR_RESIR) ? " resir" : "",
133 (udccr & UDCCR_RSM) ? " rsm" : "",
134 (udccr & UDCCR_UDA) ? " uda" : "",
135 (udccr & UDCCR_UDE) ? " ude" : "");
139 dump_udccs0(const char *label)
141 u32 udccs0 = readl(&UDC_REGS->udccs[0]);
143 debug("%s %s %02X =%s%s%s%s%s%s%s%s\n",
144 label, state_name[the_controller->ep0state], udccs0,
145 (udccs0 & UDCCS0_SA) ? " sa" : "",
146 (udccs0 & UDCCS0_RNE) ? " rne" : "",
147 (udccs0 & UDCCS0_FST) ? " fst" : "",
148 (udccs0 & UDCCS0_SST) ? " sst" : "",
149 (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
150 (udccs0 & UDCCS0_FTF) ? " ftf" : "",
151 (udccs0 & UDCCS0_IPR) ? " ipr" : "",
152 (udccs0 & UDCCS0_OPR) ? " opr" : "");
156 dump_state(struct pxa25x_udc *dev)
161 debug("%s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
162 state_name[dev->ep0state],
163 readl(&UDC_REGS->uicr1), readl(&UDC_REGS->uicr0),
164 readl(&UDC_REGS->usir1), readl(&UDC_REGS->usir0),
165 readl(&UDC_REGS->ufnrh), readl(&UDC_REGS->ufnrl));
168 tmp = readl(&UDC_REGS->udccfr);
169 debug("udccfr %02X =%s%s\n", tmp,
170 (tmp & UDCCFR_AREN) ? " aren" : "",
171 (tmp & UDCCFR_ACM) ? " acm" : "");
175 debug("no gadget driver bound\n");
178 debug("ep0 driver '%s'\n", "ether");
180 dump_udccs0("udccs0");
181 debug("ep0 IN %lu/%lu, OUT %lu/%lu\n",
182 dev->stats.write.bytes, dev->stats.write.ops,
183 dev->stats.read.bytes, dev->stats.read.ops);
185 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
186 if (dev->ep[i].desc == NULL)
188 debug("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
194 static inline void dump_udccr(const char *label) { }
195 static inline void dump_udccs0(const char *label) { }
196 static inline void dump_state(struct pxa25x_udc *dev) { }
201 * ---------------------------------------------------------------------------
202 * endpoint related parts of the api to the usb controller hardware,
203 * used by gadget driver; and the inner talker-to-hardware core.
204 * ---------------------------------------------------------------------------
207 static void pxa25x_ep_fifo_flush(struct usb_ep *ep);
208 static void nuke(struct pxa25x_ep *, int status);
210 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
211 static void pullup_off(void)
213 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
215 if (mach->udc_command)
216 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
219 static void pullup_on(void)
221 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
223 if (mach->udc_command)
224 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
227 static void pio_irq_enable(int bEndpointAddress)
229 bEndpointAddress &= 0xf;
230 if (bEndpointAddress < 8) {
231 clrbits_le32(&the_controller->regs->uicr0,
232 1 << bEndpointAddress);
234 bEndpointAddress -= 8;
235 clrbits_le32(&the_controller->regs->uicr1,
236 1 << bEndpointAddress);
240 static void pio_irq_disable(int bEndpointAddress)
242 bEndpointAddress &= 0xf;
243 if (bEndpointAddress < 8) {
244 setbits_le32(&the_controller->regs->uicr0,
245 1 << bEndpointAddress);
247 bEndpointAddress -= 8;
248 setbits_le32(&the_controller->regs->uicr1,
249 1 << bEndpointAddress);
253 static inline void udc_set_mask_UDCCR(int mask)
256 * The UDCCR reg contains mask and interrupt status bits,
257 * so using '|=' isn't safe as it may ack an interrupt.
259 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
262 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
265 static inline void udc_clear_mask_UDCCR(int mask)
267 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
269 mask = ~mask & mask_bits;
270 clrbits_le32(&the_controller->regs->udccr, ~mask);
273 static inline void udc_ack_int_UDCCR(int mask)
275 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
278 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
282 * endpoint enable/disable
284 * we need to verify the descriptors used to enable endpoints. since pxa25x
285 * endpoint configurations are fixed, and are pretty much always enabled,
286 * there's not a lot to manage here.
288 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
289 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
290 * for a single interface (with only the default altsetting) and for gadget
291 * drivers that don't halt endpoints (not reset by set_interface). that also
292 * means that if you use ISO, you must violate the USB spec rule that all
293 * iso endpoints must be in non-default altsettings.
295 static int pxa25x_ep_enable(struct usb_ep *_ep,
296 const struct usb_endpoint_descriptor *desc)
298 struct pxa25x_ep *ep;
299 struct pxa25x_udc *dev;
301 ep = container_of(_ep, struct pxa25x_ep, ep);
302 if (!_ep || !desc || ep->desc || _ep->name == ep0name
303 || desc->bDescriptorType != USB_DT_ENDPOINT
304 || ep->bEndpointAddress != desc->bEndpointAddress
306 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
307 printf("%s, bad ep or descriptor\n", __func__);
311 /* xfer types must match, except that interrupt ~= bulk */
312 if (ep->bmAttributes != desc->bmAttributes
313 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
314 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
315 printf("%s, %s type mismatch\n", __func__, _ep->name);
319 /* hardware _could_ do smaller, but driver doesn't */
320 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
321 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
323 || !get_unaligned(&desc->wMaxPacketSize)) {
324 printf("%s, bad %s maxpacket\n", __func__, _ep->name);
329 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
330 printf("%s, bogus device state\n", __func__);
337 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
339 /* flush fifo (mostly for OUT buffers) */
340 pxa25x_ep_fifo_flush(_ep);
342 /* ... reset halt state too, if we could ... */
344 debug("enabled %s\n", _ep->name);
348 static int pxa25x_ep_disable(struct usb_ep *_ep)
350 struct pxa25x_ep *ep;
353 ep = container_of(_ep, struct pxa25x_ep, ep);
354 if (!_ep || !ep->desc) {
355 printf("%s, %s not enabled\n", __func__,
356 _ep ? ep->ep.name : NULL);
359 local_irq_save(flags);
361 nuke(ep, -ESHUTDOWN);
363 /* flush fifo (mostly for IN buffers) */
364 pxa25x_ep_fifo_flush(_ep);
369 local_irq_restore(flags);
370 debug("%s disabled\n", _ep->name);
374 /*-------------------------------------------------------------------------*/
377 * for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
378 * must still pass correctly initialized endpoints, since other controller
379 * drivers may care about how it's currently set up (dma issues etc).
383 * pxa25x_ep_alloc_request - allocate a request data structure
385 static struct usb_request *
386 pxa25x_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
388 struct pxa25x_request *req;
390 req = kzalloc(sizeof(*req), gfp_flags);
394 INIT_LIST_HEAD(&req->queue);
400 * pxa25x_ep_free_request - deallocate a request data structure
403 pxa25x_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
405 struct pxa25x_request *req;
407 req = container_of(_req, struct pxa25x_request, req);
408 WARN_ON(!list_empty(&req->queue));
412 /*-------------------------------------------------------------------------*/
415 * done - retire a request; caller blocked irqs
417 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
419 unsigned stopped = ep->stopped;
421 list_del_init(&req->queue);
423 if (likely(req->req.status == -EINPROGRESS))
424 req->req.status = status;
426 status = req->req.status;
428 if (status && status != -ESHUTDOWN)
429 debug("complete %s req %p stat %d len %u/%u\n",
430 ep->ep.name, &req->req, status,
431 req->req.actual, req->req.length);
433 /* don't modify queue heads during completion callback */
435 req->req.complete(&ep->ep, &req->req);
436 ep->stopped = stopped;
440 static inline void ep0_idle(struct pxa25x_udc *dev)
442 dev->ep0state = EP0_IDLE;
446 write_packet(u32 *uddr, struct pxa25x_request *req, unsigned max)
449 unsigned length, count;
451 debug("%s(): uddr %p\n", __func__, uddr);
453 buf = req->req.buf + req->req.actual;
456 /* how big will this packet be? */
457 length = min(req->req.length - req->req.actual, max);
458 req->req.actual += length;
461 while (likely(count--))
462 writeb(*buf++, uddr);
468 * write to an IN endpoint fifo, as many packets as possible.
469 * irqs will use this to write the rest later.
470 * caller guarantees at least one packet buffer is ready (or a zlp).
473 write_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
477 max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
480 int is_last, is_short;
482 count = write_packet(ep->reg_uddr, req, max);
484 /* last packet is usually short (or a zlp) */
485 if (unlikely(count != max))
486 is_last = is_short = 1;
488 if (likely(req->req.length != req->req.actual)
493 /* interrupt/iso maxpacket may not fill the fifo */
494 is_short = unlikely(max < ep->fifo_size);
497 debug_cond(NOISY, "wrote %s %d bytes%s%s %d left %p\n",
499 is_last ? "/L" : "", is_short ? "/S" : "",
500 req->req.length - req->req.actual, req);
503 * let loose that packet. maybe try writing another one,
504 * double buffering might work. TSP, TPC, and TFS
505 * bit values are the same for all normal IN endpoints.
507 writel(UDCCS_BI_TPC, ep->reg_udccs);
509 writel(UDCCS_BI_TSP, ep->reg_udccs);
511 /* requests complete when all IN data is in the FIFO */
514 if (list_empty(&ep->queue))
515 pio_irq_disable(ep->bEndpointAddress);
520 * TODO experiment: how robust can fifo mode tweaking be?
521 * double buffering is off in the default fifo mode, which
522 * prevents TFS from being set here.
525 } while (readl(ep->reg_udccs) & UDCCS_BI_TFS);
530 * caller asserts req->pending (ep0 irq status nyet cleared); starts
531 * ep0 data stage. these chips want very simple state transitions.
534 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
536 writel(flags|UDCCS0_SA|UDCCS0_OPR, &dev->regs->udccs[0]);
537 writel(USIR0_IR0, &dev->regs->usir0);
538 dev->req_pending = 0;
539 debug_cond(NOISY, "%s() %s, udccs0: %02x/%02x usir: %X.%X\n",
540 __func__, tag, readl(&dev->regs->udccs[0]), flags,
541 readl(&dev->regs->usir1), readl(&dev->regs->usir0));
545 write_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
550 count = write_packet(&ep->dev->regs->uddr0, req, EP0_FIFO_SIZE);
551 ep->dev->stats.write.bytes += count;
553 /* last packet "must be" short (or a zlp) */
554 is_short = (count != EP0_FIFO_SIZE);
556 debug_cond(NOISY, "ep0in %d bytes %d left %p\n", count,
557 req->req.length - req->req.actual, req);
559 if (unlikely(is_short)) {
560 if (ep->dev->req_pending)
561 ep0start(ep->dev, UDCCS0_IPR, "short IN");
563 writel(UDCCS0_IPR, &ep->dev->regs->udccs[0]);
565 count = req->req.length;
570 * This seems to get rid of lost status irqs in some cases:
571 * host responds quickly, or next request involves config
572 * change automagic, or should have been hidden, or ...
574 * FIXME get rid of all udelays possible...
576 if (count >= EP0_FIFO_SIZE) {
579 if ((readl(&ep->dev->regs->udccs[0]) &
581 /* clear OPR, generate ack */
583 &ep->dev->regs->udccs[0]);
590 } else if (ep->dev->req_pending)
591 ep0start(ep->dev, 0, "IN");
598 * read_fifo - unload packet(s) from the fifo we use for usb OUT
599 * transfers and put them into the request. caller should have made
600 * sure there's at least one packet ready.
602 * returns true if the request completed because of short packet or the
603 * request buffer having filled (and maybe overran till end-of-packet).
606 read_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
610 unsigned bufferspace, count, is_short;
614 * make sure there's a packet in the FIFO.
615 * UDCCS_{BO,IO}_RPC are all the same bit value.
616 * UDCCS_{BO,IO}_RNE are all the same bit value.
618 udccs = readl(ep->reg_udccs);
619 if (unlikely((udccs & UDCCS_BO_RPC) == 0))
621 buf = req->req.buf + req->req.actual;
623 bufferspace = req->req.length - req->req.actual;
625 /* read all bytes from this packet */
626 if (likely(udccs & UDCCS_BO_RNE)) {
627 count = 1 + (0x0ff & readl(ep->reg_ubcr));
628 req->req.actual += min(count, bufferspace);
631 is_short = (count < ep->ep.maxpacket);
632 debug_cond(NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
633 ep->ep.name, udccs, count,
634 is_short ? "/S" : "",
635 req, req->req.actual, req->req.length);
636 while (likely(count-- != 0)) {
637 u8 byte = readb(ep->reg_uddr);
639 if (unlikely(bufferspace == 0)) {
641 * this happens when the driver's buffer
642 * is smaller than what the host sent.
643 * discard the extra data.
645 if (req->req.status != -EOVERFLOW)
646 printf("%s overflow %d\n",
648 req->req.status = -EOVERFLOW;
654 writel(UDCCS_BO_RPC, ep->reg_udccs);
655 /* RPC/RSP/RNE could now reflect the other packet buffer */
657 /* iso is one request per packet */
658 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
659 if (udccs & UDCCS_IO_ROF)
660 req->req.status = -EHOSTUNREACH;
661 /* more like "is_done" */
666 if (is_short || req->req.actual == req->req.length) {
668 if (list_empty(&ep->queue))
669 pio_irq_disable(ep->bEndpointAddress);
673 /* finished that packet. the next one may be waiting... */
679 * special ep0 version of the above. no UBCR0 or double buffering; status
680 * handshaking is magic. most device protocols don't need control-OUT.
681 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
682 * protocols do use them.
685 read_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
688 unsigned bufferspace;
690 buf = req->req.buf + req->req.actual;
691 bufferspace = req->req.length - req->req.actual;
693 while (readl(&ep->dev->regs->udccs[0]) & UDCCS0_RNE) {
694 byte = (u8)readb(&ep->dev->regs->uddr0);
696 if (unlikely(bufferspace == 0)) {
698 * this happens when the driver's buffer
699 * is smaller than what the host sent.
700 * discard the extra data.
702 if (req->req.status != -EOVERFLOW)
703 printf("%s overflow\n", ep->ep.name);
704 req->req.status = -EOVERFLOW;
712 writel(UDCCS0_OPR | UDCCS0_IPR, &ep->dev->regs->udccs[0]);
715 if (req->req.actual >= req->req.length)
718 /* finished that packet. the next one may be waiting... */
722 /*-------------------------------------------------------------------------*/
725 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
727 struct pxa25x_request *req;
728 struct pxa25x_ep *ep;
729 struct pxa25x_udc *dev;
732 req = container_of(_req, struct pxa25x_request, req);
733 if (unlikely(!_req || !_req->complete || !_req->buf
734 || !list_empty(&req->queue))) {
735 printf("%s, bad params\n", __func__);
739 ep = container_of(_ep, struct pxa25x_ep, ep);
740 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
741 printf("%s, bad ep\n", __func__);
746 if (unlikely(!dev->driver
747 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
748 printf("%s, bogus device state\n", __func__);
753 * iso is always one packet per request, that's the only way
754 * we can report per-packet status. that also helps with dma.
756 if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
758 le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
761 debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
762 _ep->name, _req, _req->length, _req->buf);
764 local_irq_save(flags);
766 _req->status = -EINPROGRESS;
769 /* kickstart this i/o queue? */
770 if (list_empty(&ep->queue) && !ep->stopped) {
771 if (ep->desc == NULL/* ep0 */) {
772 unsigned length = _req->length;
774 switch (dev->ep0state) {
775 case EP0_IN_DATA_PHASE:
776 dev->stats.write.ops++;
777 if (write_ep0_fifo(ep, req))
781 case EP0_OUT_DATA_PHASE:
782 dev->stats.read.ops++;
784 if (dev->req_config) {
785 debug("ep0 config ack%s\n",
786 dev->has_cfr ? "" : " raced");
788 writel(UDCCFR_AREN|UDCCFR_ACM
790 &ep->dev->regs->udccfr);
792 dev->ep0state = EP0_END_XFER;
793 local_irq_restore(flags);
796 if (dev->req_pending)
797 ep0start(dev, UDCCS0_IPR, "OUT");
800 &ep->dev->regs->udccs[0])
802 && read_ep0_fifo(ep, req))) {
810 printf("ep0 i/o, odd state %d\n",
812 local_irq_restore(flags);
815 /* can the FIFO can satisfy the request immediately? */
816 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
817 if ((readl(ep->reg_udccs) & UDCCS_BI_TFS) != 0
818 && write_fifo(ep, req))
820 } else if ((readl(ep->reg_udccs) & UDCCS_BO_RFS) != 0
821 && read_fifo(ep, req)) {
825 if (likely(req && ep->desc))
826 pio_irq_enable(ep->bEndpointAddress);
829 /* pio or dma irq handler advances the queue. */
830 if (likely(req != NULL))
831 list_add_tail(&req->queue, &ep->queue);
832 local_irq_restore(flags);
839 * nuke - dequeue ALL requests
841 static void nuke(struct pxa25x_ep *ep, int status)
843 struct pxa25x_request *req;
845 /* called with irqs blocked */
846 while (!list_empty(&ep->queue)) {
847 req = list_entry(ep->queue.next,
848 struct pxa25x_request,
850 done(ep, req, status);
853 pio_irq_disable(ep->bEndpointAddress);
857 /* dequeue JUST ONE request */
858 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
860 struct pxa25x_ep *ep;
861 struct pxa25x_request *req;
864 ep = container_of(_ep, struct pxa25x_ep, ep);
865 if (!_ep || ep->ep.name == ep0name)
868 local_irq_save(flags);
870 /* make sure it's actually queued on this endpoint */
871 list_for_each_entry(req, &ep->queue, queue) {
872 if (&req->req == _req)
875 if (&req->req != _req) {
876 local_irq_restore(flags);
880 done(ep, req, -ECONNRESET);
882 local_irq_restore(flags);
886 /*-------------------------------------------------------------------------*/
888 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
890 struct pxa25x_ep *ep;
893 ep = container_of(_ep, struct pxa25x_ep, ep);
895 || (!ep->desc && ep->ep.name != ep0name))
896 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
897 printf("%s, bad ep\n", __func__);
902 * this path (reset toggle+halt) is needed to implement
903 * SET_INTERFACE on normal hardware. but it can't be
904 * done from software on the PXA UDC, and the hardware
905 * forgets to do it as part of SET_INTERFACE automagic.
907 printf("only host can clear %s halt\n", _ep->name);
911 local_irq_save(flags);
913 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
914 && ((readl(ep->reg_udccs) & UDCCS_BI_TFS) == 0
915 || !list_empty(&ep->queue))) {
916 local_irq_restore(flags);
920 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
921 writel(UDCCS_BI_FST|UDCCS_BI_FTF, ep->reg_udccs);
923 /* ep0 needs special care */
925 start_watchdog(ep->dev);
926 ep->dev->req_pending = 0;
927 ep->dev->ep0state = EP0_STALL;
929 /* and bulk/intr endpoints like dropping stalls too */
932 for (i = 0; i < 1000; i += 20) {
933 if (readl(ep->reg_udccs) & UDCCS_BI_SST)
938 local_irq_restore(flags);
940 debug("%s halt\n", _ep->name);
944 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
946 struct pxa25x_ep *ep;
948 ep = container_of(_ep, struct pxa25x_ep, ep);
950 printf("%s, bad ep\n", __func__);
953 /* pxa can't report unclaimed bytes from IN fifos */
954 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
956 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
957 || (readl(ep->reg_udccs) & UDCCS_BO_RFS) == 0)
960 return (readl(ep->reg_ubcr) & 0xfff) + 1;
963 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
965 struct pxa25x_ep *ep;
967 ep = container_of(_ep, struct pxa25x_ep, ep);
968 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
969 printf("%s, bad ep\n", __func__);
973 /* toggle and halt bits stay unchanged */
975 /* for OUT, just read and discard the FIFO contents. */
976 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
977 while (((readl(ep->reg_udccs)) & UDCCS_BO_RNE) != 0)
978 (void)readb(ep->reg_uddr);
982 /* most IN status is the same, but ISO can't stall */
983 writel(UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
984 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
985 ? 0 : UDCCS_BI_SST), ep->reg_udccs);
989 static struct usb_ep_ops pxa25x_ep_ops = {
990 .enable = pxa25x_ep_enable,
991 .disable = pxa25x_ep_disable,
993 .alloc_request = pxa25x_ep_alloc_request,
994 .free_request = pxa25x_ep_free_request,
996 .queue = pxa25x_ep_queue,
997 .dequeue = pxa25x_ep_dequeue,
999 .set_halt = pxa25x_ep_set_halt,
1000 .fifo_status = pxa25x_ep_fifo_status,
1001 .fifo_flush = pxa25x_ep_fifo_flush,
1005 /* ---------------------------------------------------------------------------
1006 * device-scoped parts of the api to the usb controller hardware
1007 * ---------------------------------------------------------------------------
1010 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
1012 return ((readl(&the_controller->regs->ufnrh) & 0x07) << 8) |
1013 (readl(&the_controller->regs->ufnrl) & 0xff);
1016 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
1018 /* host may not have enabled remote wakeup */
1019 if ((readl(&the_controller->regs->udccs[0]) & UDCCS0_DRWF) == 0)
1020 return -EHOSTUNREACH;
1021 udc_set_mask_UDCCR(UDCCR_RSM);
1025 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
1026 static void udc_enable(struct pxa25x_udc *);
1027 static void udc_disable(struct pxa25x_udc *);
1030 * We disable the UDC -- and its 48 MHz clock -- whenever it's not
1033 static int pullup(struct pxa25x_udc *udc)
1041 int is_active = udc->pullup;
1049 if (udc->gadget.speed != USB_SPEED_UNKNOWN)
1050 stop_activity(udc, udc->driver);
1059 /* VBUS reporting logically comes from a transceiver */
1060 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1062 struct pxa25x_udc *udc;
1064 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1065 printf("vbus %s\n", is_active ? "supplied" : "inactive");
1070 /* drivers may have software control over D+ pullup */
1071 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
1073 struct pxa25x_udc *udc;
1075 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1077 /* not all boards support pullup control */
1078 if (!udc->mach->udc_command)
1081 udc->pullup = (is_active != 0);
1087 * boards may consume current from VBUS, up to 100-500mA based on config.
1088 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
1089 * violate USB specs.
1091 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1096 static const struct usb_gadget_ops pxa25x_udc_ops = {
1097 .get_frame = pxa25x_udc_get_frame,
1098 .wakeup = pxa25x_udc_wakeup,
1099 .vbus_session = pxa25x_udc_vbus_session,
1100 .pullup = pxa25x_udc_pullup,
1101 .vbus_draw = pxa25x_udc_vbus_draw,
1104 /*-------------------------------------------------------------------------*/
1107 * udc_disable - disable USB device controller
1109 static void udc_disable(struct pxa25x_udc *dev)
1111 /* block all irqs */
1112 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1113 writel(0xff, &dev->regs->uicr0);
1114 writel(0xff, &dev->regs->uicr1);
1115 writel(UFNRH_SIM, &dev->regs->ufnrh);
1117 /* if hardware supports it, disconnect from usb */
1120 udc_clear_mask_UDCCR(UDCCR_UDE);
1123 dev->gadget.speed = USB_SPEED_UNKNOWN;
1127 * udc_reinit - initialize software state
1129 static void udc_reinit(struct pxa25x_udc *dev)
1133 /* device/ep0 records init */
1134 INIT_LIST_HEAD(&dev->gadget.ep_list);
1135 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1136 dev->ep0state = EP0_IDLE;
1138 /* basic endpoint records init */
1139 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1140 struct pxa25x_ep *ep = &dev->ep[i];
1143 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1147 INIT_LIST_HEAD(&ep->queue);
1151 /* the rest was statically initialized, and is read-only */
1155 * until it's enabled, this UDC should be completely invisible
1158 static void udc_enable(struct pxa25x_udc *dev)
1160 debug("udc: enabling udc\n");
1162 udc_clear_mask_UDCCR(UDCCR_UDE);
1165 * Try to clear these bits before we enable the udc.
1166 * Do not touch reset ack bit, we would take care of it in
1167 * interrupt handle routine
1169 udc_ack_int_UDCCR(UDCCR_SUSIR|UDCCR_RESIR);
1172 dev->gadget.speed = USB_SPEED_UNKNOWN;
1173 dev->stats.irqs = 0;
1176 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1178 * - if RESET is already in progress, ack interrupt
1179 * - unmask reset interrupt
1181 udc_set_mask_UDCCR(UDCCR_UDE);
1182 if (!(readl(&dev->regs->udccr) & UDCCR_UDA))
1183 udc_ack_int_UDCCR(UDCCR_RSTIR);
1185 if (dev->has_cfr /* UDC_RES2 is defined */) {
1187 * pxa255 (a0+) can avoid a set_config race that could
1188 * prevent gadget drivers from configuring correctly
1190 writel(UDCCFR_ACM | UDCCFR_MB1, &dev->regs->udccfr);
1193 /* enable suspend/resume and reset irqs */
1194 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1196 /* enable ep0 irqs */
1197 clrbits_le32(&dev->regs->uicr0, UICR0_IM0);
1199 /* if hardware supports it, pullup D+ and wait for reset */
1203 static inline void clear_ep_state(struct pxa25x_udc *dev)
1208 * hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1209 * fifos, and pending transactions mustn't be continued in any case.
1211 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1212 nuke(&dev->ep[i], -ECONNABORTED);
1215 static void handle_ep0(struct pxa25x_udc *dev)
1217 u32 udccs0 = readl(&dev->regs->udccs[0]);
1218 struct pxa25x_ep *ep = &dev->ep[0];
1219 struct pxa25x_request *req;
1221 struct usb_ctrlrequest r;
1226 if (list_empty(&ep->queue))
1229 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1231 /* clear stall status */
1232 if (udccs0 & UDCCS0_SST) {
1234 writel(UDCCS0_SST, &dev->regs->udccs[0]);
1239 /* previous request unfinished? non-error iff back-to-back ... */
1240 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1246 switch (dev->ep0state) {
1248 /* late-breaking status? */
1249 udccs0 = readl(&dev->regs->udccs[0]);
1251 /* start control request? */
1252 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1253 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1258 /* read SETUP packet */
1259 for (i = 0; i < 8; i++) {
1260 if (unlikely(!(readl(&dev->regs->udccs[0]) &
1263 debug("SETUP %d!\n", i);
1266 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1268 if (unlikely((readl(&dev->regs->udccs[0]) &
1273 debug("SETUP %02x.%02x v%04x i%04x l%04x\n",
1274 u.r.bRequestType, u.r.bRequest,
1275 le16_to_cpu(u.r.wValue),
1276 le16_to_cpu(u.r.wIndex),
1277 le16_to_cpu(u.r.wLength));
1279 /* cope with automagic for some standard requests. */
1280 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1281 == USB_TYPE_STANDARD;
1282 dev->req_config = 0;
1283 dev->req_pending = 1;
1284 switch (u.r.bRequest) {
1285 /* hardware restricts gadget drivers here! */
1286 case USB_REQ_SET_CONFIGURATION:
1287 debug("GOT SET_CONFIGURATION\n");
1288 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1290 * reflect hardware's automagic
1291 * up to the gadget driver.
1294 dev->req_config = 1;
1295 clear_ep_state(dev);
1297 * if !has_cfr, there's no synch
1298 * else use AREN (later) not SA|OPR
1299 * USIR0_IR0 acts edge sensitive
1303 /* ... and here, even more ... */
1304 case USB_REQ_SET_INTERFACE:
1305 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1307 * udc hardware is broken by design:
1308 * - altsetting may only be zero;
1309 * - hw resets all interfaces' eps;
1310 * - ep reset doesn't include halt(?).
1312 printf("broken set_interface (%d/%d)\n",
1313 le16_to_cpu(u.r.wIndex),
1314 le16_to_cpu(u.r.wValue));
1318 /* hardware was supposed to hide this */
1319 case USB_REQ_SET_ADDRESS:
1320 debug("GOT SET ADDRESS\n");
1321 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1322 ep0start(dev, 0, "address");
1328 if (u.r.bRequestType & USB_DIR_IN)
1329 dev->ep0state = EP0_IN_DATA_PHASE;
1331 dev->ep0state = EP0_OUT_DATA_PHASE;
1333 i = dev->driver->setup(&dev->gadget, &u.r);
1335 /* hardware automagic preventing STALL... */
1336 if (dev->req_config) {
1338 * hardware sometimes neglects to tell
1339 * tell us about config change events,
1340 * so later ones may fail...
1342 printf("config change %02x fail %d?\n",
1346 * TODO experiment: if has_cfr,
1347 * hardware didn't ACK; maybe we
1348 * could actually STALL!
1353 /* uninitialized when goto stall */
1356 debug("protocol STALL, "
1358 readl(&dev->regs->udccs[0]), i);
1361 * the watchdog timer helps deal with cases
1362 * where udc seems to clear FST wrongly, and
1363 * then NAKs instead of STALLing.
1365 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1366 start_watchdog(dev);
1367 dev->ep0state = EP0_STALL;
1369 /* deferred i/o == no response yet */
1370 } else if (dev->req_pending) {
1371 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1372 || dev->req_std || u.r.wLength))
1373 ep0start(dev, 0, "defer");
1375 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1378 /* expect at least one data or status stage irq */
1381 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1382 == (UDCCS0_OPR|UDCCS0_SA))) {
1386 * pxa210/250 erratum 131 for B0/B1 says RNE lies.
1387 * still observed on a pxa255 a0.
1392 /* read SETUP data, but don't trust it too much */
1393 for (i = 0; i < 8; i++)
1394 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1395 if ((u.r.bRequestType & USB_RECIP_MASK)
1398 if (u.word[0] == 0 && u.word[1] == 0)
1403 * some random early IRQ:
1406 * - OPR got set, without SA (likely status stage)
1408 debug("random IRQ %X %X\n", udccs0,
1409 readl(&dev->regs->udccs[0]));
1410 writel(udccs0 & (UDCCS0_SA|UDCCS0_OPR),
1411 &dev->regs->udccs[0]);
1414 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1415 if (udccs0 & UDCCS0_OPR) {
1416 debug("ep0in premature status\n");
1420 } else /* irq was IPR clearing */ {
1422 debug("next ep0 in packet\n");
1423 /* this IN packet might finish the request */
1424 (void) write_ep0_fifo(ep, req);
1425 } /* else IN token before response was written */
1428 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1429 if (udccs0 & UDCCS0_OPR) {
1431 /* this OUT packet might finish the request */
1432 if (read_ep0_fifo(ep, req))
1434 /* else more OUT packets expected */
1435 } /* else OUT token before read was issued */
1436 } else /* irq was IPR clearing */ {
1437 debug("ep0out premature status\n");
1447 * ack control-IN status (maybe in-zlp was skipped)
1448 * also appears after some config change events.
1450 if (udccs0 & UDCCS0_OPR)
1451 writel(UDCCS0_OPR, &dev->regs->udccs[0]);
1455 writel(UDCCS0_FST, &dev->regs->udccs[0]);
1459 writel(USIR0_IR0, &dev->regs->usir0);
1462 static void handle_ep(struct pxa25x_ep *ep)
1464 struct pxa25x_request *req;
1465 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1471 if (likely(!list_empty(&ep->queue)))
1472 req = list_entry(ep->queue.next,
1473 struct pxa25x_request, queue);
1477 /* TODO check FST handling */
1479 udccs = readl(ep->reg_udccs);
1480 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1482 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1483 tmp |= UDCCS_BI_SST;
1486 writel(tmp, ep->reg_udccs);
1487 if (req && likely((udccs & UDCCS_BI_TFS) != 0))
1488 completed = write_fifo(ep, req);
1490 } else { /* irq from RPC (or for ISO, ROF) */
1491 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1492 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1494 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1497 writel(tmp, ep->reg_udccs);
1499 /* fifos can hold packets, ready for reading... */
1501 completed = read_fifo(ep, req);
1503 pio_irq_disable(ep->bEndpointAddress);
1506 } while (completed);
1510 * pxa25x_udc_irq - interrupt handler
1512 * avoid delays in ep0 processing. the control handshaking isn't always
1513 * under software control (pxa250c0 and the pxa255 are better), and delays
1514 * could cause usb protocol errors.
1516 static struct pxa25x_udc memory;
1518 pxa25x_udc_irq(void)
1520 struct pxa25x_udc *dev = &memory;
1527 u32 udccr = readl(&dev->regs->udccr);
1531 /* SUSpend Interrupt Request */
1532 if (unlikely(udccr & UDCCR_SUSIR)) {
1533 udc_ack_int_UDCCR(UDCCR_SUSIR);
1535 debug("USB suspend\n");
1537 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1539 && dev->driver->suspend)
1540 dev->driver->suspend(&dev->gadget);
1544 /* RESume Interrupt Request */
1545 if (unlikely(udccr & UDCCR_RESIR)) {
1546 udc_ack_int_UDCCR(UDCCR_RESIR);
1548 debug("USB resume\n");
1550 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1552 && dev->driver->resume)
1553 dev->driver->resume(&dev->gadget);
1556 /* ReSeT Interrupt Request - USB reset */
1557 if (unlikely(udccr & UDCCR_RSTIR)) {
1558 udc_ack_int_UDCCR(UDCCR_RSTIR);
1561 if ((readl(&dev->regs->udccr) & UDCCR_UDA) == 0) {
1562 debug("USB reset start\n");
1565 * reset driver and endpoints,
1566 * in case that's not yet done
1568 stop_activity(dev, dev->driver);
1571 debug("USB reset end\n");
1572 dev->gadget.speed = USB_SPEED_FULL;
1573 memset(&dev->stats, 0, sizeof dev->stats);
1574 /* driver and endpoints are still reset */
1578 u32 uicr0 = readl(&dev->regs->uicr0);
1579 u32 uicr1 = readl(&dev->regs->uicr1);
1580 u32 usir0 = readl(&dev->regs->usir0);
1581 u32 usir1 = readl(&dev->regs->usir1);
1583 usir0 = usir0 & ~uicr0;
1584 usir1 = usir1 & ~uicr1;
1587 if (unlikely(!usir0 && !usir1))
1590 debug_cond(NOISY, "irq %02x.%02x\n", usir1, usir0);
1592 /* control traffic */
1593 if (usir0 & USIR0_IR0) {
1594 dev->ep[0].pio_irqs++;
1599 /* endpoint data transfers */
1600 for (i = 0; i < 8; i++) {
1603 if (i && (usir0 & tmp)) {
1604 handle_ep(&dev->ep[i]);
1605 setbits_le32(&dev->regs->usir0, tmp);
1608 #ifndef CONFIG_USB_PXA25X_SMALL
1610 handle_ep(&dev->ep[i+8]);
1611 setbits_le32(&dev->regs->usir1, tmp);
1618 /* we could also ask for 1 msec SOF (SIR) interrupts */
1624 /*-------------------------------------------------------------------------*/
1627 * this uses load-time allocation and initialization (instead of
1628 * doing it at run-time) to save code, eliminate fault paths, and
1629 * be more obviously correct.
1631 static struct pxa25x_udc memory = {
1635 .ops = &pxa25x_udc_ops,
1636 .ep0 = &memory.ep[0].ep,
1637 .name = driver_name,
1640 /* control endpoint */
1644 .ops = &pxa25x_ep_ops,
1645 .maxpacket = EP0_FIFO_SIZE,
1648 .reg_udccs = &UDC_REGS->udccs[0],
1649 .reg_uddr = &UDC_REGS->uddr0,
1652 /* first group of endpoints */
1655 .name = "ep1in-bulk",
1656 .ops = &pxa25x_ep_ops,
1657 .maxpacket = BULK_FIFO_SIZE,
1660 .fifo_size = BULK_FIFO_SIZE,
1661 .bEndpointAddress = USB_DIR_IN | 1,
1662 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1663 .reg_udccs = &UDC_REGS->udccs[1],
1664 .reg_uddr = &UDC_REGS->uddr1,
1668 .name = "ep2out-bulk",
1669 .ops = &pxa25x_ep_ops,
1670 .maxpacket = BULK_FIFO_SIZE,
1673 .fifo_size = BULK_FIFO_SIZE,
1674 .bEndpointAddress = 2,
1675 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1676 .reg_udccs = &UDC_REGS->udccs[2],
1677 .reg_ubcr = &UDC_REGS->ubcr2,
1678 .reg_uddr = &UDC_REGS->uddr2,
1680 #ifndef CONFIG_USB_PXA25X_SMALL
1683 .name = "ep3in-iso",
1684 .ops = &pxa25x_ep_ops,
1685 .maxpacket = ISO_FIFO_SIZE,
1688 .fifo_size = ISO_FIFO_SIZE,
1689 .bEndpointAddress = USB_DIR_IN | 3,
1690 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1691 .reg_udccs = &UDC_REGS->udccs[3],
1692 .reg_uddr = &UDC_REGS->uddr3,
1696 .name = "ep4out-iso",
1697 .ops = &pxa25x_ep_ops,
1698 .maxpacket = ISO_FIFO_SIZE,
1701 .fifo_size = ISO_FIFO_SIZE,
1702 .bEndpointAddress = 4,
1703 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1704 .reg_udccs = &UDC_REGS->udccs[4],
1705 .reg_ubcr = &UDC_REGS->ubcr4,
1706 .reg_uddr = &UDC_REGS->uddr4,
1710 .name = "ep5in-int",
1711 .ops = &pxa25x_ep_ops,
1712 .maxpacket = INT_FIFO_SIZE,
1715 .fifo_size = INT_FIFO_SIZE,
1716 .bEndpointAddress = USB_DIR_IN | 5,
1717 .bmAttributes = USB_ENDPOINT_XFER_INT,
1718 .reg_udccs = &UDC_REGS->udccs[5],
1719 .reg_uddr = &UDC_REGS->uddr5,
1722 /* second group of endpoints */
1725 .name = "ep6in-bulk",
1726 .ops = &pxa25x_ep_ops,
1727 .maxpacket = BULK_FIFO_SIZE,
1730 .fifo_size = BULK_FIFO_SIZE,
1731 .bEndpointAddress = USB_DIR_IN | 6,
1732 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1733 .reg_udccs = &UDC_REGS->udccs[6],
1734 .reg_uddr = &UDC_REGS->uddr6,
1738 .name = "ep7out-bulk",
1739 .ops = &pxa25x_ep_ops,
1740 .maxpacket = BULK_FIFO_SIZE,
1743 .fifo_size = BULK_FIFO_SIZE,
1744 .bEndpointAddress = 7,
1745 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1746 .reg_udccs = &UDC_REGS->udccs[7],
1747 .reg_ubcr = &UDC_REGS->ubcr7,
1748 .reg_uddr = &UDC_REGS->uddr7,
1752 .name = "ep8in-iso",
1753 .ops = &pxa25x_ep_ops,
1754 .maxpacket = ISO_FIFO_SIZE,
1757 .fifo_size = ISO_FIFO_SIZE,
1758 .bEndpointAddress = USB_DIR_IN | 8,
1759 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1760 .reg_udccs = &UDC_REGS->udccs[8],
1761 .reg_uddr = &UDC_REGS->uddr8,
1765 .name = "ep9out-iso",
1766 .ops = &pxa25x_ep_ops,
1767 .maxpacket = ISO_FIFO_SIZE,
1770 .fifo_size = ISO_FIFO_SIZE,
1771 .bEndpointAddress = 9,
1772 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1773 .reg_udccs = &UDC_REGS->udccs[9],
1774 .reg_ubcr = &UDC_REGS->ubcr9,
1775 .reg_uddr = &UDC_REGS->uddr9,
1779 .name = "ep10in-int",
1780 .ops = &pxa25x_ep_ops,
1781 .maxpacket = INT_FIFO_SIZE,
1784 .fifo_size = INT_FIFO_SIZE,
1785 .bEndpointAddress = USB_DIR_IN | 10,
1786 .bmAttributes = USB_ENDPOINT_XFER_INT,
1787 .reg_udccs = &UDC_REGS->udccs[10],
1788 .reg_uddr = &UDC_REGS->uddr10,
1791 /* third group of endpoints */
1794 .name = "ep11in-bulk",
1795 .ops = &pxa25x_ep_ops,
1796 .maxpacket = BULK_FIFO_SIZE,
1799 .fifo_size = BULK_FIFO_SIZE,
1800 .bEndpointAddress = USB_DIR_IN | 11,
1801 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1802 .reg_udccs = &UDC_REGS->udccs[11],
1803 .reg_uddr = &UDC_REGS->uddr11,
1807 .name = "ep12out-bulk",
1808 .ops = &pxa25x_ep_ops,
1809 .maxpacket = BULK_FIFO_SIZE,
1812 .fifo_size = BULK_FIFO_SIZE,
1813 .bEndpointAddress = 12,
1814 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1815 .reg_udccs = &UDC_REGS->udccs[12],
1816 .reg_ubcr = &UDC_REGS->ubcr12,
1817 .reg_uddr = &UDC_REGS->uddr12,
1821 .name = "ep13in-iso",
1822 .ops = &pxa25x_ep_ops,
1823 .maxpacket = ISO_FIFO_SIZE,
1826 .fifo_size = ISO_FIFO_SIZE,
1827 .bEndpointAddress = USB_DIR_IN | 13,
1828 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1829 .reg_udccs = &UDC_REGS->udccs[13],
1830 .reg_uddr = &UDC_REGS->uddr13,
1834 .name = "ep14out-iso",
1835 .ops = &pxa25x_ep_ops,
1836 .maxpacket = ISO_FIFO_SIZE,
1839 .fifo_size = ISO_FIFO_SIZE,
1840 .bEndpointAddress = 14,
1841 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1842 .reg_udccs = &UDC_REGS->udccs[14],
1843 .reg_ubcr = &UDC_REGS->ubcr14,
1844 .reg_uddr = &UDC_REGS->uddr14,
1848 .name = "ep15in-int",
1849 .ops = &pxa25x_ep_ops,
1850 .maxpacket = INT_FIFO_SIZE,
1853 .fifo_size = INT_FIFO_SIZE,
1854 .bEndpointAddress = USB_DIR_IN | 15,
1855 .bmAttributes = USB_ENDPOINT_XFER_INT,
1856 .reg_udccs = &UDC_REGS->udccs[15],
1857 .reg_uddr = &UDC_REGS->uddr15,
1859 #endif /* !CONFIG_USB_PXA25X_SMALL */
1862 static void udc_command(int cmd)
1865 case PXA2XX_UDC_CMD_CONNECT:
1866 setbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1867 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1870 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1871 GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
1873 debug("Connected to USB\n");
1876 case PXA2XX_UDC_CMD_DISCONNECT:
1877 /* disable pullup resistor */
1878 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1879 GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
1881 /* setup pin as input, line will float */
1882 clrbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1883 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1885 debug("Disconnected from USB\n");
1890 static struct pxa2xx_udc_mach_info mach_info = {
1891 .udc_command = udc_command,
1895 * when a driver is successfully registered, it will receive
1896 * control requests including set_configuration(), which enables
1897 * non-control requests. then usb traffic follows until a
1898 * disconnect is reported. then a host may connect again, or
1899 * the driver might get unbound.
1901 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1903 struct pxa25x_udc *dev = &memory;
1908 || driver->speed < USB_SPEED_FULL
1909 || !driver->disconnect
1917 /* Enable clock for usb controller */
1918 setbits_le32(CKEN, CKEN11_USB);
1920 /* first hook up the driver ... */
1921 dev->driver = driver;
1924 /* trigger chiprev-specific logic */
1925 switch ((chiprev = pxa_get_cpu_revision())) {
1931 /* A0/A1 "not released"; ep 13, 15 unusable */
1933 case PXA250_B2: case PXA210_B2:
1934 case PXA250_B1: case PXA210_B1:
1935 case PXA250_B0: case PXA210_B0:
1936 /* OUT-DMA is broken ... */
1938 case PXA250_C0: case PXA210_C0:
1941 printf("%s: unrecognized processor: %08x\n",
1942 DRIVER_NAME, chiprev);
1946 the_controller = dev;
1948 /* prepare watchdog timer */
1949 dev->watchdog.running = 0;
1950 dev->watchdog.period = 5000 * CONFIG_SYS_HZ / 1000000; /* 5 ms */
1951 dev->watchdog.function = udc_watchdog;
1956 dev->mach = &mach_info;
1958 dev->gadget.name = "pxa2xx_udc";
1959 retval = driver->bind(&dev->gadget);
1961 printf("bind to driver %s --> error %d\n",
1962 DRIVER_NAME, retval);
1968 * ... then enable host detection and ep0; and we're ready
1969 * for set_configuration as well as eventual disconnect.
1971 printf("registered gadget driver '%s'\n", DRIVER_NAME);
1979 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1983 /* don't disconnect drivers more than once */
1984 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1986 dev->gadget.speed = USB_SPEED_UNKNOWN;
1988 /* prevent new request submissions, kill any outstanding requests */
1989 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1990 struct pxa25x_ep *ep = &dev->ep[i];
1993 nuke(ep, -ESHUTDOWN);
1997 /* report disconnect; the driver is already quiesced */
1999 driver->disconnect(&dev->gadget);
2001 /* re-init driver-visible data structures */
2005 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2007 struct pxa25x_udc *dev = the_controller;
2011 if (!driver || driver != dev->driver || !driver->unbind)
2014 local_irq_disable();
2017 stop_activity(dev, driver);
2020 driver->unbind(&dev->gadget);
2023 printf("unregistered gadget driver '%s'\n", DRIVER_NAME);
2026 the_controller = NULL;
2028 clrbits_le32(CKEN, CKEN11_USB);
2033 extern void udc_disconnect(void)
2035 setbits_le32(CKEN, CKEN11_USB);
2036 udc_clear_mask_UDCCR(UDCCR_UDE);
2037 udc_command(PXA2XX_UDC_CMD_DISCONNECT);
2038 clrbits_le32(CKEN, CKEN11_USB);
2041 /*-------------------------------------------------------------------------*/
2044 usb_gadget_handle_interrupts(void)
2046 return pxa25x_udc_irq();