1 // SPDX-License-Identifier: GPL-2.0+
3 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers
5 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker)
6 * Copyright (C) 2003 Robert Schwebel, Pengutronix
7 * Copyright (C) 2003 Benedikt Spranger, Pengutronix
8 * Copyright (C) 2003 David Brownell
9 * Copyright (C) 2003 Joshua Wise
10 * Copyright (C) 2012 Lukasz Dalek <luk0104@gmail.com>
12 * MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
15 #define CONFIG_USB_PXA25X_SMALL
16 #define DRIVER_NAME "pxa25x_udc_linux"
17 #define ARCH_HAS_PREFETCH
21 #include <asm/byteorder.h>
22 #include <asm/system.h>
23 #include <asm/mach-types.h>
24 #include <asm/unaligned.h>
25 #include <linux/compat.h>
28 #include <asm/arch/pxa.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <asm/arch/pxa-regs.h>
34 #include "pxa25x_udc.h"
37 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
38 * series processors. The UDC for the IXP 4xx series is very similar.
39 * There are fifteen endpoints, in addition to ep0.
41 * Such controller drivers work with a gadget driver. The gadget driver
42 * returns descriptors, implements configuration and data protocols used
43 * by the host to interact with this device, and allocates endpoints to
44 * the different protocol interfaces. The controller driver virtualizes
45 * usb hardware so that the gadget drivers will be more portable.
47 * This UDC hardware wants to implement a bit too much USB protocol, so
48 * it constrains the sorts of USB configuration change events that work.
49 * The errata for these chips are misleading; some "fixed" bugs from
50 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
52 * Note that the UDC hardware supports DMA (except on IXP) but that's
53 * not used here. IN-DMA (to host) is simple enough, when the data is
54 * suitably aligned (16 bytes) ... the network stack doesn't do that,
55 * other software can. OUT-DMA is buggy in most chip versions, as well
56 * as poorly designed (data toggle not automatic). So this driver won't
57 * bother using DMA. (Mostly-working IN-DMA support was available in
58 * kernels before 2.6.23, but was never enabled or well tested.)
61 #define DRIVER_VERSION "18-August-2012"
62 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
64 static const char driver_name[] = "pxa25x_udc";
65 static const char ep0name[] = "ep0";
68 static inline void start_watchdog(struct pxa25x_udc *udc)
70 debug("Started watchdog\n");
71 udc->watchdog.base = get_timer(0);
72 udc->watchdog.running = 1;
75 static inline void stop_watchdog(struct pxa25x_udc *udc)
77 udc->watchdog.running = 0;
78 debug("Stopped watchdog\n");
81 static inline void test_watchdog(struct pxa25x_udc *udc)
83 if (!udc->watchdog.running)
86 debug("watchdog %ld %ld\n", get_timer(udc->watchdog.base),
87 udc->watchdog.period);
89 if (get_timer(udc->watchdog.base) >= udc->watchdog.period) {
91 udc->watchdog.function(udc);
95 static void udc_watchdog(struct pxa25x_udc *dev)
97 uint32_t udccs0 = readl(&dev->regs->udccs[0]);
99 debug("Fired up udc_watchdog\n");
102 if (dev->ep0state == EP0_STALL
103 && (udccs0 & UDCCS0_FST) == 0
104 && (udccs0 & UDCCS0_SST) == 0) {
105 writel(UDCCS0_FST|UDCCS0_FTF, &dev->regs->udccs[0]);
106 debug("ep0 re-stall\n");
114 static const char * const state_name[] = {
116 "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
117 "EP0_END_XFER", "EP0_STALL"
121 dump_udccr(const char *label)
123 u32 udccr = readl(&UDC_REGS->udccr);
124 debug("%s %02X =%s%s%s%s%s%s%s%s\n",
126 (udccr & UDCCR_REM) ? " rem" : "",
127 (udccr & UDCCR_RSTIR) ? " rstir" : "",
128 (udccr & UDCCR_SRM) ? " srm" : "",
129 (udccr & UDCCR_SUSIR) ? " susir" : "",
130 (udccr & UDCCR_RESIR) ? " resir" : "",
131 (udccr & UDCCR_RSM) ? " rsm" : "",
132 (udccr & UDCCR_UDA) ? " uda" : "",
133 (udccr & UDCCR_UDE) ? " ude" : "");
137 dump_udccs0(const char *label)
139 u32 udccs0 = readl(&UDC_REGS->udccs[0]);
141 debug("%s %s %02X =%s%s%s%s%s%s%s%s\n",
142 label, state_name[the_controller->ep0state], udccs0,
143 (udccs0 & UDCCS0_SA) ? " sa" : "",
144 (udccs0 & UDCCS0_RNE) ? " rne" : "",
145 (udccs0 & UDCCS0_FST) ? " fst" : "",
146 (udccs0 & UDCCS0_SST) ? " sst" : "",
147 (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
148 (udccs0 & UDCCS0_FTF) ? " ftf" : "",
149 (udccs0 & UDCCS0_IPR) ? " ipr" : "",
150 (udccs0 & UDCCS0_OPR) ? " opr" : "");
154 dump_state(struct pxa25x_udc *dev)
159 debug("%s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
160 state_name[dev->ep0state],
161 readl(&UDC_REGS->uicr1), readl(&UDC_REGS->uicr0),
162 readl(&UDC_REGS->usir1), readl(&UDC_REGS->usir0),
163 readl(&UDC_REGS->ufnrh), readl(&UDC_REGS->ufnrl));
166 tmp = readl(&UDC_REGS->udccfr);
167 debug("udccfr %02X =%s%s\n", tmp,
168 (tmp & UDCCFR_AREN) ? " aren" : "",
169 (tmp & UDCCFR_ACM) ? " acm" : "");
173 debug("no gadget driver bound\n");
176 debug("ep0 driver '%s'\n", "ether");
178 dump_udccs0("udccs0");
179 debug("ep0 IN %lu/%lu, OUT %lu/%lu\n",
180 dev->stats.write.bytes, dev->stats.write.ops,
181 dev->stats.read.bytes, dev->stats.read.ops);
183 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
184 if (dev->ep[i].desc == NULL)
186 debug("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
192 static inline void dump_udccr(const char *label) { }
193 static inline void dump_udccs0(const char *label) { }
194 static inline void dump_state(struct pxa25x_udc *dev) { }
199 * ---------------------------------------------------------------------------
200 * endpoint related parts of the api to the usb controller hardware,
201 * used by gadget driver; and the inner talker-to-hardware core.
202 * ---------------------------------------------------------------------------
205 static void pxa25x_ep_fifo_flush(struct usb_ep *ep);
206 static void nuke(struct pxa25x_ep *, int status);
208 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
209 static void pullup_off(void)
211 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
213 if (mach->udc_command)
214 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
217 static void pullup_on(void)
219 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
221 if (mach->udc_command)
222 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
225 static void pio_irq_enable(int bEndpointAddress)
227 bEndpointAddress &= 0xf;
228 if (bEndpointAddress < 8) {
229 clrbits_le32(&the_controller->regs->uicr0,
230 1 << bEndpointAddress);
232 bEndpointAddress -= 8;
233 clrbits_le32(&the_controller->regs->uicr1,
234 1 << bEndpointAddress);
238 static void pio_irq_disable(int bEndpointAddress)
240 bEndpointAddress &= 0xf;
241 if (bEndpointAddress < 8) {
242 setbits_le32(&the_controller->regs->uicr0,
243 1 << bEndpointAddress);
245 bEndpointAddress -= 8;
246 setbits_le32(&the_controller->regs->uicr1,
247 1 << bEndpointAddress);
251 static inline void udc_set_mask_UDCCR(int mask)
254 * The UDCCR reg contains mask and interrupt status bits,
255 * so using '|=' isn't safe as it may ack an interrupt.
257 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
260 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
263 static inline void udc_clear_mask_UDCCR(int mask)
265 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
267 mask = ~mask & mask_bits;
268 clrbits_le32(&the_controller->regs->udccr, ~mask);
271 static inline void udc_ack_int_UDCCR(int mask)
273 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
276 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
280 * endpoint enable/disable
282 * we need to verify the descriptors used to enable endpoints. since pxa25x
283 * endpoint configurations are fixed, and are pretty much always enabled,
284 * there's not a lot to manage here.
286 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
287 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
288 * for a single interface (with only the default altsetting) and for gadget
289 * drivers that don't halt endpoints (not reset by set_interface). that also
290 * means that if you use ISO, you must violate the USB spec rule that all
291 * iso endpoints must be in non-default altsettings.
293 static int pxa25x_ep_enable(struct usb_ep *_ep,
294 const struct usb_endpoint_descriptor *desc)
296 struct pxa25x_ep *ep;
297 struct pxa25x_udc *dev;
299 ep = container_of(_ep, struct pxa25x_ep, ep);
300 if (!_ep || !desc || ep->desc || _ep->name == ep0name
301 || desc->bDescriptorType != USB_DT_ENDPOINT
302 || ep->bEndpointAddress != desc->bEndpointAddress
304 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
305 printf("%s, bad ep or descriptor\n", __func__);
309 /* xfer types must match, except that interrupt ~= bulk */
310 if (ep->bmAttributes != desc->bmAttributes
311 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
312 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
313 printf("%s, %s type mismatch\n", __func__, _ep->name);
317 /* hardware _could_ do smaller, but driver doesn't */
318 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
319 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
321 || !get_unaligned(&desc->wMaxPacketSize)) {
322 printf("%s, bad %s maxpacket\n", __func__, _ep->name);
327 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
328 printf("%s, bogus device state\n", __func__);
335 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
337 /* flush fifo (mostly for OUT buffers) */
338 pxa25x_ep_fifo_flush(_ep);
340 /* ... reset halt state too, if we could ... */
342 debug("enabled %s\n", _ep->name);
346 static int pxa25x_ep_disable(struct usb_ep *_ep)
348 struct pxa25x_ep *ep;
351 ep = container_of(_ep, struct pxa25x_ep, ep);
352 if (!_ep || !ep->desc) {
353 printf("%s, %s not enabled\n", __func__,
354 _ep ? ep->ep.name : NULL);
357 local_irq_save(flags);
359 nuke(ep, -ESHUTDOWN);
361 /* flush fifo (mostly for IN buffers) */
362 pxa25x_ep_fifo_flush(_ep);
367 local_irq_restore(flags);
368 debug("%s disabled\n", _ep->name);
372 /*-------------------------------------------------------------------------*/
375 * for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
376 * must still pass correctly initialized endpoints, since other controller
377 * drivers may care about how it's currently set up (dma issues etc).
381 * pxa25x_ep_alloc_request - allocate a request data structure
383 static struct usb_request *
384 pxa25x_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
386 struct pxa25x_request *req;
388 req = kzalloc(sizeof(*req), gfp_flags);
392 INIT_LIST_HEAD(&req->queue);
398 * pxa25x_ep_free_request - deallocate a request data structure
401 pxa25x_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
403 struct pxa25x_request *req;
405 req = container_of(_req, struct pxa25x_request, req);
406 WARN_ON(!list_empty(&req->queue));
410 /*-------------------------------------------------------------------------*/
413 * done - retire a request; caller blocked irqs
415 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
417 unsigned stopped = ep->stopped;
419 list_del_init(&req->queue);
421 if (likely(req->req.status == -EINPROGRESS))
422 req->req.status = status;
424 status = req->req.status;
426 if (status && status != -ESHUTDOWN)
427 debug("complete %s req %p stat %d len %u/%u\n",
428 ep->ep.name, &req->req, status,
429 req->req.actual, req->req.length);
431 /* don't modify queue heads during completion callback */
433 req->req.complete(&ep->ep, &req->req);
434 ep->stopped = stopped;
438 static inline void ep0_idle(struct pxa25x_udc *dev)
440 dev->ep0state = EP0_IDLE;
444 write_packet(u32 *uddr, struct pxa25x_request *req, unsigned max)
447 unsigned length, count;
449 debug("%s(): uddr %p\n", __func__, uddr);
451 buf = req->req.buf + req->req.actual;
454 /* how big will this packet be? */
455 length = min(req->req.length - req->req.actual, max);
456 req->req.actual += length;
459 while (likely(count--))
460 writeb(*buf++, uddr);
466 * write to an IN endpoint fifo, as many packets as possible.
467 * irqs will use this to write the rest later.
468 * caller guarantees at least one packet buffer is ready (or a zlp).
471 write_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
475 max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
478 int is_last, is_short;
480 count = write_packet(ep->reg_uddr, req, max);
482 /* last packet is usually short (or a zlp) */
483 if (unlikely(count != max))
484 is_last = is_short = 1;
486 if (likely(req->req.length != req->req.actual)
491 /* interrupt/iso maxpacket may not fill the fifo */
492 is_short = unlikely(max < ep->fifo_size);
495 debug_cond(NOISY, "wrote %s %d bytes%s%s %d left %p\n",
497 is_last ? "/L" : "", is_short ? "/S" : "",
498 req->req.length - req->req.actual, req);
501 * let loose that packet. maybe try writing another one,
502 * double buffering might work. TSP, TPC, and TFS
503 * bit values are the same for all normal IN endpoints.
505 writel(UDCCS_BI_TPC, ep->reg_udccs);
507 writel(UDCCS_BI_TSP, ep->reg_udccs);
509 /* requests complete when all IN data is in the FIFO */
512 if (list_empty(&ep->queue))
513 pio_irq_disable(ep->bEndpointAddress);
518 * TODO experiment: how robust can fifo mode tweaking be?
519 * double buffering is off in the default fifo mode, which
520 * prevents TFS from being set here.
523 } while (readl(ep->reg_udccs) & UDCCS_BI_TFS);
528 * caller asserts req->pending (ep0 irq status nyet cleared); starts
529 * ep0 data stage. these chips want very simple state transitions.
532 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
534 writel(flags|UDCCS0_SA|UDCCS0_OPR, &dev->regs->udccs[0]);
535 writel(USIR0_IR0, &dev->regs->usir0);
536 dev->req_pending = 0;
537 debug_cond(NOISY, "%s() %s, udccs0: %02x/%02x usir: %X.%X\n",
538 __func__, tag, readl(&dev->regs->udccs[0]), flags,
539 readl(&dev->regs->usir1), readl(&dev->regs->usir0));
543 write_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
548 count = write_packet(&ep->dev->regs->uddr0, req, EP0_FIFO_SIZE);
549 ep->dev->stats.write.bytes += count;
551 /* last packet "must be" short (or a zlp) */
552 is_short = (count != EP0_FIFO_SIZE);
554 debug_cond(NOISY, "ep0in %d bytes %d left %p\n", count,
555 req->req.length - req->req.actual, req);
557 if (unlikely(is_short)) {
558 if (ep->dev->req_pending)
559 ep0start(ep->dev, UDCCS0_IPR, "short IN");
561 writel(UDCCS0_IPR, &ep->dev->regs->udccs[0]);
563 count = req->req.length;
568 * This seems to get rid of lost status irqs in some cases:
569 * host responds quickly, or next request involves config
570 * change automagic, or should have been hidden, or ...
572 * FIXME get rid of all udelays possible...
574 if (count >= EP0_FIFO_SIZE) {
577 if ((readl(&ep->dev->regs->udccs[0]) &
579 /* clear OPR, generate ack */
581 &ep->dev->regs->udccs[0]);
588 } else if (ep->dev->req_pending)
589 ep0start(ep->dev, 0, "IN");
596 * read_fifo - unload packet(s) from the fifo we use for usb OUT
597 * transfers and put them into the request. caller should have made
598 * sure there's at least one packet ready.
600 * returns true if the request completed because of short packet or the
601 * request buffer having filled (and maybe overran till end-of-packet).
604 read_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
608 unsigned bufferspace, count, is_short;
612 * make sure there's a packet in the FIFO.
613 * UDCCS_{BO,IO}_RPC are all the same bit value.
614 * UDCCS_{BO,IO}_RNE are all the same bit value.
616 udccs = readl(ep->reg_udccs);
617 if (unlikely((udccs & UDCCS_BO_RPC) == 0))
619 buf = req->req.buf + req->req.actual;
621 bufferspace = req->req.length - req->req.actual;
623 /* read all bytes from this packet */
624 if (likely(udccs & UDCCS_BO_RNE)) {
625 count = 1 + (0x0ff & readl(ep->reg_ubcr));
626 req->req.actual += min(count, bufferspace);
629 is_short = (count < ep->ep.maxpacket);
630 debug_cond(NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
631 ep->ep.name, udccs, count,
632 is_short ? "/S" : "",
633 req, req->req.actual, req->req.length);
634 while (likely(count-- != 0)) {
635 u8 byte = readb(ep->reg_uddr);
637 if (unlikely(bufferspace == 0)) {
639 * this happens when the driver's buffer
640 * is smaller than what the host sent.
641 * discard the extra data.
643 if (req->req.status != -EOVERFLOW)
644 printf("%s overflow %d\n",
646 req->req.status = -EOVERFLOW;
652 writel(UDCCS_BO_RPC, ep->reg_udccs);
653 /* RPC/RSP/RNE could now reflect the other packet buffer */
655 /* iso is one request per packet */
656 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
657 if (udccs & UDCCS_IO_ROF)
658 req->req.status = -EHOSTUNREACH;
659 /* more like "is_done" */
664 if (is_short || req->req.actual == req->req.length) {
666 if (list_empty(&ep->queue))
667 pio_irq_disable(ep->bEndpointAddress);
671 /* finished that packet. the next one may be waiting... */
677 * special ep0 version of the above. no UBCR0 or double buffering; status
678 * handshaking is magic. most device protocols don't need control-OUT.
679 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
680 * protocols do use them.
683 read_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
686 unsigned bufferspace;
688 buf = req->req.buf + req->req.actual;
689 bufferspace = req->req.length - req->req.actual;
691 while (readl(&ep->dev->regs->udccs[0]) & UDCCS0_RNE) {
692 byte = (u8)readb(&ep->dev->regs->uddr0);
694 if (unlikely(bufferspace == 0)) {
696 * this happens when the driver's buffer
697 * is smaller than what the host sent.
698 * discard the extra data.
700 if (req->req.status != -EOVERFLOW)
701 printf("%s overflow\n", ep->ep.name);
702 req->req.status = -EOVERFLOW;
710 writel(UDCCS0_OPR | UDCCS0_IPR, &ep->dev->regs->udccs[0]);
713 if (req->req.actual >= req->req.length)
716 /* finished that packet. the next one may be waiting... */
720 /*-------------------------------------------------------------------------*/
723 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
725 struct pxa25x_request *req;
726 struct pxa25x_ep *ep;
727 struct pxa25x_udc *dev;
730 req = container_of(_req, struct pxa25x_request, req);
731 if (unlikely(!_req || !_req->complete || !_req->buf
732 || !list_empty(&req->queue))) {
733 printf("%s, bad params\n", __func__);
737 ep = container_of(_ep, struct pxa25x_ep, ep);
738 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
739 printf("%s, bad ep\n", __func__);
744 if (unlikely(!dev->driver
745 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
746 printf("%s, bogus device state\n", __func__);
751 * iso is always one packet per request, that's the only way
752 * we can report per-packet status. that also helps with dma.
754 if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
756 le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
759 debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
760 _ep->name, _req, _req->length, _req->buf);
762 local_irq_save(flags);
764 _req->status = -EINPROGRESS;
767 /* kickstart this i/o queue? */
768 if (list_empty(&ep->queue) && !ep->stopped) {
769 if (ep->desc == NULL/* ep0 */) {
770 unsigned length = _req->length;
772 switch (dev->ep0state) {
773 case EP0_IN_DATA_PHASE:
774 dev->stats.write.ops++;
775 if (write_ep0_fifo(ep, req))
779 case EP0_OUT_DATA_PHASE:
780 dev->stats.read.ops++;
782 if (dev->req_config) {
783 debug("ep0 config ack%s\n",
784 dev->has_cfr ? "" : " raced");
786 writel(UDCCFR_AREN|UDCCFR_ACM
788 &ep->dev->regs->udccfr);
790 dev->ep0state = EP0_END_XFER;
791 local_irq_restore(flags);
794 if (dev->req_pending)
795 ep0start(dev, UDCCS0_IPR, "OUT");
798 &ep->dev->regs->udccs[0])
800 && read_ep0_fifo(ep, req))) {
808 printf("ep0 i/o, odd state %d\n",
810 local_irq_restore(flags);
813 /* can the FIFO can satisfy the request immediately? */
814 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
815 if ((readl(ep->reg_udccs) & UDCCS_BI_TFS) != 0
816 && write_fifo(ep, req))
818 } else if ((readl(ep->reg_udccs) & UDCCS_BO_RFS) != 0
819 && read_fifo(ep, req)) {
823 if (likely(req && ep->desc))
824 pio_irq_enable(ep->bEndpointAddress);
827 /* pio or dma irq handler advances the queue. */
828 if (likely(req != NULL))
829 list_add_tail(&req->queue, &ep->queue);
830 local_irq_restore(flags);
837 * nuke - dequeue ALL requests
839 static void nuke(struct pxa25x_ep *ep, int status)
841 struct pxa25x_request *req;
843 /* called with irqs blocked */
844 while (!list_empty(&ep->queue)) {
845 req = list_entry(ep->queue.next,
846 struct pxa25x_request,
848 done(ep, req, status);
851 pio_irq_disable(ep->bEndpointAddress);
855 /* dequeue JUST ONE request */
856 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
858 struct pxa25x_ep *ep;
859 struct pxa25x_request *req;
862 ep = container_of(_ep, struct pxa25x_ep, ep);
863 if (!_ep || ep->ep.name == ep0name)
866 local_irq_save(flags);
868 /* make sure it's actually queued on this endpoint */
869 list_for_each_entry(req, &ep->queue, queue) {
870 if (&req->req == _req)
873 if (&req->req != _req) {
874 local_irq_restore(flags);
878 done(ep, req, -ECONNRESET);
880 local_irq_restore(flags);
884 /*-------------------------------------------------------------------------*/
886 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
888 struct pxa25x_ep *ep;
891 ep = container_of(_ep, struct pxa25x_ep, ep);
893 || (!ep->desc && ep->ep.name != ep0name))
894 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
895 printf("%s, bad ep\n", __func__);
900 * this path (reset toggle+halt) is needed to implement
901 * SET_INTERFACE on normal hardware. but it can't be
902 * done from software on the PXA UDC, and the hardware
903 * forgets to do it as part of SET_INTERFACE automagic.
905 printf("only host can clear %s halt\n", _ep->name);
909 local_irq_save(flags);
911 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
912 && ((readl(ep->reg_udccs) & UDCCS_BI_TFS) == 0
913 || !list_empty(&ep->queue))) {
914 local_irq_restore(flags);
918 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
919 writel(UDCCS_BI_FST|UDCCS_BI_FTF, ep->reg_udccs);
921 /* ep0 needs special care */
923 start_watchdog(ep->dev);
924 ep->dev->req_pending = 0;
925 ep->dev->ep0state = EP0_STALL;
927 /* and bulk/intr endpoints like dropping stalls too */
930 for (i = 0; i < 1000; i += 20) {
931 if (readl(ep->reg_udccs) & UDCCS_BI_SST)
936 local_irq_restore(flags);
938 debug("%s halt\n", _ep->name);
942 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
944 struct pxa25x_ep *ep;
946 ep = container_of(_ep, struct pxa25x_ep, ep);
948 printf("%s, bad ep\n", __func__);
951 /* pxa can't report unclaimed bytes from IN fifos */
952 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
954 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
955 || (readl(ep->reg_udccs) & UDCCS_BO_RFS) == 0)
958 return (readl(ep->reg_ubcr) & 0xfff) + 1;
961 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
963 struct pxa25x_ep *ep;
965 ep = container_of(_ep, struct pxa25x_ep, ep);
966 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
967 printf("%s, bad ep\n", __func__);
971 /* toggle and halt bits stay unchanged */
973 /* for OUT, just read and discard the FIFO contents. */
974 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
975 while (((readl(ep->reg_udccs)) & UDCCS_BO_RNE) != 0)
976 (void)readb(ep->reg_uddr);
980 /* most IN status is the same, but ISO can't stall */
981 writel(UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
982 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
983 ? 0 : UDCCS_BI_SST), ep->reg_udccs);
987 static struct usb_ep_ops pxa25x_ep_ops = {
988 .enable = pxa25x_ep_enable,
989 .disable = pxa25x_ep_disable,
991 .alloc_request = pxa25x_ep_alloc_request,
992 .free_request = pxa25x_ep_free_request,
994 .queue = pxa25x_ep_queue,
995 .dequeue = pxa25x_ep_dequeue,
997 .set_halt = pxa25x_ep_set_halt,
998 .fifo_status = pxa25x_ep_fifo_status,
999 .fifo_flush = pxa25x_ep_fifo_flush,
1003 /* ---------------------------------------------------------------------------
1004 * device-scoped parts of the api to the usb controller hardware
1005 * ---------------------------------------------------------------------------
1008 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
1010 return ((readl(&the_controller->regs->ufnrh) & 0x07) << 8) |
1011 (readl(&the_controller->regs->ufnrl) & 0xff);
1014 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
1016 /* host may not have enabled remote wakeup */
1017 if ((readl(&the_controller->regs->udccs[0]) & UDCCS0_DRWF) == 0)
1018 return -EHOSTUNREACH;
1019 udc_set_mask_UDCCR(UDCCR_RSM);
1023 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
1024 static void udc_enable(struct pxa25x_udc *);
1025 static void udc_disable(struct pxa25x_udc *);
1028 * We disable the UDC -- and its 48 MHz clock -- whenever it's not
1031 static int pullup(struct pxa25x_udc *udc)
1039 int is_active = udc->pullup;
1047 if (udc->gadget.speed != USB_SPEED_UNKNOWN)
1048 stop_activity(udc, udc->driver);
1057 /* VBUS reporting logically comes from a transceiver */
1058 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1060 struct pxa25x_udc *udc;
1062 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1063 printf("vbus %s\n", is_active ? "supplied" : "inactive");
1068 /* drivers may have software control over D+ pullup */
1069 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
1071 struct pxa25x_udc *udc;
1073 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1075 /* not all boards support pullup control */
1076 if (!udc->mach->udc_command)
1079 udc->pullup = (is_active != 0);
1085 * boards may consume current from VBUS, up to 100-500mA based on config.
1086 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
1087 * violate USB specs.
1089 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1094 static const struct usb_gadget_ops pxa25x_udc_ops = {
1095 .get_frame = pxa25x_udc_get_frame,
1096 .wakeup = pxa25x_udc_wakeup,
1097 .vbus_session = pxa25x_udc_vbus_session,
1098 .pullup = pxa25x_udc_pullup,
1099 .vbus_draw = pxa25x_udc_vbus_draw,
1102 /*-------------------------------------------------------------------------*/
1105 * udc_disable - disable USB device controller
1107 static void udc_disable(struct pxa25x_udc *dev)
1109 /* block all irqs */
1110 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1111 writel(0xff, &dev->regs->uicr0);
1112 writel(0xff, &dev->regs->uicr1);
1113 writel(UFNRH_SIM, &dev->regs->ufnrh);
1115 /* if hardware supports it, disconnect from usb */
1118 udc_clear_mask_UDCCR(UDCCR_UDE);
1121 dev->gadget.speed = USB_SPEED_UNKNOWN;
1125 * udc_reinit - initialize software state
1127 static void udc_reinit(struct pxa25x_udc *dev)
1131 /* device/ep0 records init */
1132 INIT_LIST_HEAD(&dev->gadget.ep_list);
1133 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1134 dev->ep0state = EP0_IDLE;
1136 /* basic endpoint records init */
1137 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1138 struct pxa25x_ep *ep = &dev->ep[i];
1141 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1145 INIT_LIST_HEAD(&ep->queue);
1149 /* the rest was statically initialized, and is read-only */
1153 * until it's enabled, this UDC should be completely invisible
1156 static void udc_enable(struct pxa25x_udc *dev)
1158 debug("udc: enabling udc\n");
1160 udc_clear_mask_UDCCR(UDCCR_UDE);
1163 * Try to clear these bits before we enable the udc.
1164 * Do not touch reset ack bit, we would take care of it in
1165 * interrupt handle routine
1167 udc_ack_int_UDCCR(UDCCR_SUSIR|UDCCR_RESIR);
1170 dev->gadget.speed = USB_SPEED_UNKNOWN;
1171 dev->stats.irqs = 0;
1174 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1176 * - if RESET is already in progress, ack interrupt
1177 * - unmask reset interrupt
1179 udc_set_mask_UDCCR(UDCCR_UDE);
1180 if (!(readl(&dev->regs->udccr) & UDCCR_UDA))
1181 udc_ack_int_UDCCR(UDCCR_RSTIR);
1183 if (dev->has_cfr /* UDC_RES2 is defined */) {
1185 * pxa255 (a0+) can avoid a set_config race that could
1186 * prevent gadget drivers from configuring correctly
1188 writel(UDCCFR_ACM | UDCCFR_MB1, &dev->regs->udccfr);
1191 /* enable suspend/resume and reset irqs */
1192 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1194 /* enable ep0 irqs */
1195 clrbits_le32(&dev->regs->uicr0, UICR0_IM0);
1197 /* if hardware supports it, pullup D+ and wait for reset */
1201 static inline void clear_ep_state(struct pxa25x_udc *dev)
1206 * hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1207 * fifos, and pending transactions mustn't be continued in any case.
1209 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1210 nuke(&dev->ep[i], -ECONNABORTED);
1213 static void handle_ep0(struct pxa25x_udc *dev)
1215 u32 udccs0 = readl(&dev->regs->udccs[0]);
1216 struct pxa25x_ep *ep = &dev->ep[0];
1217 struct pxa25x_request *req;
1219 struct usb_ctrlrequest r;
1224 if (list_empty(&ep->queue))
1227 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1229 /* clear stall status */
1230 if (udccs0 & UDCCS0_SST) {
1232 writel(UDCCS0_SST, &dev->regs->udccs[0]);
1237 /* previous request unfinished? non-error iff back-to-back ... */
1238 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1244 switch (dev->ep0state) {
1246 /* late-breaking status? */
1247 udccs0 = readl(&dev->regs->udccs[0]);
1249 /* start control request? */
1250 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1251 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1256 /* read SETUP packet */
1257 for (i = 0; i < 8; i++) {
1258 if (unlikely(!(readl(&dev->regs->udccs[0]) &
1261 debug("SETUP %d!\n", i);
1264 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1266 if (unlikely((readl(&dev->regs->udccs[0]) &
1271 debug("SETUP %02x.%02x v%04x i%04x l%04x\n",
1272 u.r.bRequestType, u.r.bRequest,
1273 le16_to_cpu(u.r.wValue),
1274 le16_to_cpu(u.r.wIndex),
1275 le16_to_cpu(u.r.wLength));
1277 /* cope with automagic for some standard requests. */
1278 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1279 == USB_TYPE_STANDARD;
1280 dev->req_config = 0;
1281 dev->req_pending = 1;
1282 switch (u.r.bRequest) {
1283 /* hardware restricts gadget drivers here! */
1284 case USB_REQ_SET_CONFIGURATION:
1285 debug("GOT SET_CONFIGURATION\n");
1286 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1288 * reflect hardware's automagic
1289 * up to the gadget driver.
1292 dev->req_config = 1;
1293 clear_ep_state(dev);
1295 * if !has_cfr, there's no synch
1296 * else use AREN (later) not SA|OPR
1297 * USIR0_IR0 acts edge sensitive
1301 /* ... and here, even more ... */
1302 case USB_REQ_SET_INTERFACE:
1303 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1305 * udc hardware is broken by design:
1306 * - altsetting may only be zero;
1307 * - hw resets all interfaces' eps;
1308 * - ep reset doesn't include halt(?).
1310 printf("broken set_interface (%d/%d)\n",
1311 le16_to_cpu(u.r.wIndex),
1312 le16_to_cpu(u.r.wValue));
1316 /* hardware was supposed to hide this */
1317 case USB_REQ_SET_ADDRESS:
1318 debug("GOT SET ADDRESS\n");
1319 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1320 ep0start(dev, 0, "address");
1326 if (u.r.bRequestType & USB_DIR_IN)
1327 dev->ep0state = EP0_IN_DATA_PHASE;
1329 dev->ep0state = EP0_OUT_DATA_PHASE;
1331 i = dev->driver->setup(&dev->gadget, &u.r);
1333 /* hardware automagic preventing STALL... */
1334 if (dev->req_config) {
1336 * hardware sometimes neglects to tell
1337 * tell us about config change events,
1338 * so later ones may fail...
1340 printf("config change %02x fail %d?\n",
1344 * TODO experiment: if has_cfr,
1345 * hardware didn't ACK; maybe we
1346 * could actually STALL!
1351 /* uninitialized when goto stall */
1354 debug("protocol STALL, "
1356 readl(&dev->regs->udccs[0]), i);
1359 * the watchdog timer helps deal with cases
1360 * where udc seems to clear FST wrongly, and
1361 * then NAKs instead of STALLing.
1363 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1364 start_watchdog(dev);
1365 dev->ep0state = EP0_STALL;
1367 /* deferred i/o == no response yet */
1368 } else if (dev->req_pending) {
1369 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1370 || dev->req_std || u.r.wLength))
1371 ep0start(dev, 0, "defer");
1373 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1376 /* expect at least one data or status stage irq */
1379 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1380 == (UDCCS0_OPR|UDCCS0_SA))) {
1384 * pxa210/250 erratum 131 for B0/B1 says RNE lies.
1385 * still observed on a pxa255 a0.
1390 /* read SETUP data, but don't trust it too much */
1391 for (i = 0; i < 8; i++)
1392 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1393 if ((u.r.bRequestType & USB_RECIP_MASK)
1396 if (u.word[0] == 0 && u.word[1] == 0)
1401 * some random early IRQ:
1404 * - OPR got set, without SA (likely status stage)
1406 debug("random IRQ %X %X\n", udccs0,
1407 readl(&dev->regs->udccs[0]));
1408 writel(udccs0 & (UDCCS0_SA|UDCCS0_OPR),
1409 &dev->regs->udccs[0]);
1412 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1413 if (udccs0 & UDCCS0_OPR) {
1414 debug("ep0in premature status\n");
1418 } else /* irq was IPR clearing */ {
1420 debug("next ep0 in packet\n");
1421 /* this IN packet might finish the request */
1422 (void) write_ep0_fifo(ep, req);
1423 } /* else IN token before response was written */
1426 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1427 if (udccs0 & UDCCS0_OPR) {
1429 /* this OUT packet might finish the request */
1430 if (read_ep0_fifo(ep, req))
1432 /* else more OUT packets expected */
1433 } /* else OUT token before read was issued */
1434 } else /* irq was IPR clearing */ {
1435 debug("ep0out premature status\n");
1445 * ack control-IN status (maybe in-zlp was skipped)
1446 * also appears after some config change events.
1448 if (udccs0 & UDCCS0_OPR)
1449 writel(UDCCS0_OPR, &dev->regs->udccs[0]);
1453 writel(UDCCS0_FST, &dev->regs->udccs[0]);
1457 writel(USIR0_IR0, &dev->regs->usir0);
1460 static void handle_ep(struct pxa25x_ep *ep)
1462 struct pxa25x_request *req;
1463 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1469 if (likely(!list_empty(&ep->queue)))
1470 req = list_entry(ep->queue.next,
1471 struct pxa25x_request, queue);
1475 /* TODO check FST handling */
1477 udccs = readl(ep->reg_udccs);
1478 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1480 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1481 tmp |= UDCCS_BI_SST;
1484 writel(tmp, ep->reg_udccs);
1485 if (req && likely((udccs & UDCCS_BI_TFS) != 0))
1486 completed = write_fifo(ep, req);
1488 } else { /* irq from RPC (or for ISO, ROF) */
1489 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1490 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1492 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1495 writel(tmp, ep->reg_udccs);
1497 /* fifos can hold packets, ready for reading... */
1499 completed = read_fifo(ep, req);
1501 pio_irq_disable(ep->bEndpointAddress);
1504 } while (completed);
1508 * pxa25x_udc_irq - interrupt handler
1510 * avoid delays in ep0 processing. the control handshaking isn't always
1511 * under software control (pxa250c0 and the pxa255 are better), and delays
1512 * could cause usb protocol errors.
1514 static struct pxa25x_udc memory;
1516 pxa25x_udc_irq(void)
1518 struct pxa25x_udc *dev = &memory;
1525 u32 udccr = readl(&dev->regs->udccr);
1529 /* SUSpend Interrupt Request */
1530 if (unlikely(udccr & UDCCR_SUSIR)) {
1531 udc_ack_int_UDCCR(UDCCR_SUSIR);
1533 debug("USB suspend\n");
1535 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1537 && dev->driver->suspend)
1538 dev->driver->suspend(&dev->gadget);
1542 /* RESume Interrupt Request */
1543 if (unlikely(udccr & UDCCR_RESIR)) {
1544 udc_ack_int_UDCCR(UDCCR_RESIR);
1546 debug("USB resume\n");
1548 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1550 && dev->driver->resume)
1551 dev->driver->resume(&dev->gadget);
1554 /* ReSeT Interrupt Request - USB reset */
1555 if (unlikely(udccr & UDCCR_RSTIR)) {
1556 udc_ack_int_UDCCR(UDCCR_RSTIR);
1559 if ((readl(&dev->regs->udccr) & UDCCR_UDA) == 0) {
1560 debug("USB reset start\n");
1563 * reset driver and endpoints,
1564 * in case that's not yet done
1566 stop_activity(dev, dev->driver);
1569 debug("USB reset end\n");
1570 dev->gadget.speed = USB_SPEED_FULL;
1571 memset(&dev->stats, 0, sizeof dev->stats);
1572 /* driver and endpoints are still reset */
1576 u32 uicr0 = readl(&dev->regs->uicr0);
1577 u32 uicr1 = readl(&dev->regs->uicr1);
1578 u32 usir0 = readl(&dev->regs->usir0);
1579 u32 usir1 = readl(&dev->regs->usir1);
1581 usir0 = usir0 & ~uicr0;
1582 usir1 = usir1 & ~uicr1;
1585 if (unlikely(!usir0 && !usir1))
1588 debug_cond(NOISY, "irq %02x.%02x\n", usir1, usir0);
1590 /* control traffic */
1591 if (usir0 & USIR0_IR0) {
1592 dev->ep[0].pio_irqs++;
1597 /* endpoint data transfers */
1598 for (i = 0; i < 8; i++) {
1601 if (i && (usir0 & tmp)) {
1602 handle_ep(&dev->ep[i]);
1603 setbits_le32(&dev->regs->usir0, tmp);
1606 #ifndef CONFIG_USB_PXA25X_SMALL
1608 handle_ep(&dev->ep[i+8]);
1609 setbits_le32(&dev->regs->usir1, tmp);
1616 /* we could also ask for 1 msec SOF (SIR) interrupts */
1622 /*-------------------------------------------------------------------------*/
1625 * this uses load-time allocation and initialization (instead of
1626 * doing it at run-time) to save code, eliminate fault paths, and
1627 * be more obviously correct.
1629 static struct pxa25x_udc memory = {
1633 .ops = &pxa25x_udc_ops,
1634 .ep0 = &memory.ep[0].ep,
1635 .name = driver_name,
1638 /* control endpoint */
1642 .ops = &pxa25x_ep_ops,
1643 .maxpacket = EP0_FIFO_SIZE,
1646 .reg_udccs = &UDC_REGS->udccs[0],
1647 .reg_uddr = &UDC_REGS->uddr0,
1650 /* first group of endpoints */
1653 .name = "ep1in-bulk",
1654 .ops = &pxa25x_ep_ops,
1655 .maxpacket = BULK_FIFO_SIZE,
1658 .fifo_size = BULK_FIFO_SIZE,
1659 .bEndpointAddress = USB_DIR_IN | 1,
1660 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1661 .reg_udccs = &UDC_REGS->udccs[1],
1662 .reg_uddr = &UDC_REGS->uddr1,
1666 .name = "ep2out-bulk",
1667 .ops = &pxa25x_ep_ops,
1668 .maxpacket = BULK_FIFO_SIZE,
1671 .fifo_size = BULK_FIFO_SIZE,
1672 .bEndpointAddress = 2,
1673 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1674 .reg_udccs = &UDC_REGS->udccs[2],
1675 .reg_ubcr = &UDC_REGS->ubcr2,
1676 .reg_uddr = &UDC_REGS->uddr2,
1678 #ifndef CONFIG_USB_PXA25X_SMALL
1681 .name = "ep3in-iso",
1682 .ops = &pxa25x_ep_ops,
1683 .maxpacket = ISO_FIFO_SIZE,
1686 .fifo_size = ISO_FIFO_SIZE,
1687 .bEndpointAddress = USB_DIR_IN | 3,
1688 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1689 .reg_udccs = &UDC_REGS->udccs[3],
1690 .reg_uddr = &UDC_REGS->uddr3,
1694 .name = "ep4out-iso",
1695 .ops = &pxa25x_ep_ops,
1696 .maxpacket = ISO_FIFO_SIZE,
1699 .fifo_size = ISO_FIFO_SIZE,
1700 .bEndpointAddress = 4,
1701 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1702 .reg_udccs = &UDC_REGS->udccs[4],
1703 .reg_ubcr = &UDC_REGS->ubcr4,
1704 .reg_uddr = &UDC_REGS->uddr4,
1708 .name = "ep5in-int",
1709 .ops = &pxa25x_ep_ops,
1710 .maxpacket = INT_FIFO_SIZE,
1713 .fifo_size = INT_FIFO_SIZE,
1714 .bEndpointAddress = USB_DIR_IN | 5,
1715 .bmAttributes = USB_ENDPOINT_XFER_INT,
1716 .reg_udccs = &UDC_REGS->udccs[5],
1717 .reg_uddr = &UDC_REGS->uddr5,
1720 /* second group of endpoints */
1723 .name = "ep6in-bulk",
1724 .ops = &pxa25x_ep_ops,
1725 .maxpacket = BULK_FIFO_SIZE,
1728 .fifo_size = BULK_FIFO_SIZE,
1729 .bEndpointAddress = USB_DIR_IN | 6,
1730 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1731 .reg_udccs = &UDC_REGS->udccs[6],
1732 .reg_uddr = &UDC_REGS->uddr6,
1736 .name = "ep7out-bulk",
1737 .ops = &pxa25x_ep_ops,
1738 .maxpacket = BULK_FIFO_SIZE,
1741 .fifo_size = BULK_FIFO_SIZE,
1742 .bEndpointAddress = 7,
1743 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1744 .reg_udccs = &UDC_REGS->udccs[7],
1745 .reg_ubcr = &UDC_REGS->ubcr7,
1746 .reg_uddr = &UDC_REGS->uddr7,
1750 .name = "ep8in-iso",
1751 .ops = &pxa25x_ep_ops,
1752 .maxpacket = ISO_FIFO_SIZE,
1755 .fifo_size = ISO_FIFO_SIZE,
1756 .bEndpointAddress = USB_DIR_IN | 8,
1757 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1758 .reg_udccs = &UDC_REGS->udccs[8],
1759 .reg_uddr = &UDC_REGS->uddr8,
1763 .name = "ep9out-iso",
1764 .ops = &pxa25x_ep_ops,
1765 .maxpacket = ISO_FIFO_SIZE,
1768 .fifo_size = ISO_FIFO_SIZE,
1769 .bEndpointAddress = 9,
1770 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1771 .reg_udccs = &UDC_REGS->udccs[9],
1772 .reg_ubcr = &UDC_REGS->ubcr9,
1773 .reg_uddr = &UDC_REGS->uddr9,
1777 .name = "ep10in-int",
1778 .ops = &pxa25x_ep_ops,
1779 .maxpacket = INT_FIFO_SIZE,
1782 .fifo_size = INT_FIFO_SIZE,
1783 .bEndpointAddress = USB_DIR_IN | 10,
1784 .bmAttributes = USB_ENDPOINT_XFER_INT,
1785 .reg_udccs = &UDC_REGS->udccs[10],
1786 .reg_uddr = &UDC_REGS->uddr10,
1789 /* third group of endpoints */
1792 .name = "ep11in-bulk",
1793 .ops = &pxa25x_ep_ops,
1794 .maxpacket = BULK_FIFO_SIZE,
1797 .fifo_size = BULK_FIFO_SIZE,
1798 .bEndpointAddress = USB_DIR_IN | 11,
1799 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1800 .reg_udccs = &UDC_REGS->udccs[11],
1801 .reg_uddr = &UDC_REGS->uddr11,
1805 .name = "ep12out-bulk",
1806 .ops = &pxa25x_ep_ops,
1807 .maxpacket = BULK_FIFO_SIZE,
1810 .fifo_size = BULK_FIFO_SIZE,
1811 .bEndpointAddress = 12,
1812 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1813 .reg_udccs = &UDC_REGS->udccs[12],
1814 .reg_ubcr = &UDC_REGS->ubcr12,
1815 .reg_uddr = &UDC_REGS->uddr12,
1819 .name = "ep13in-iso",
1820 .ops = &pxa25x_ep_ops,
1821 .maxpacket = ISO_FIFO_SIZE,
1824 .fifo_size = ISO_FIFO_SIZE,
1825 .bEndpointAddress = USB_DIR_IN | 13,
1826 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1827 .reg_udccs = &UDC_REGS->udccs[13],
1828 .reg_uddr = &UDC_REGS->uddr13,
1832 .name = "ep14out-iso",
1833 .ops = &pxa25x_ep_ops,
1834 .maxpacket = ISO_FIFO_SIZE,
1837 .fifo_size = ISO_FIFO_SIZE,
1838 .bEndpointAddress = 14,
1839 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1840 .reg_udccs = &UDC_REGS->udccs[14],
1841 .reg_ubcr = &UDC_REGS->ubcr14,
1842 .reg_uddr = &UDC_REGS->uddr14,
1846 .name = "ep15in-int",
1847 .ops = &pxa25x_ep_ops,
1848 .maxpacket = INT_FIFO_SIZE,
1851 .fifo_size = INT_FIFO_SIZE,
1852 .bEndpointAddress = USB_DIR_IN | 15,
1853 .bmAttributes = USB_ENDPOINT_XFER_INT,
1854 .reg_udccs = &UDC_REGS->udccs[15],
1855 .reg_uddr = &UDC_REGS->uddr15,
1857 #endif /* !CONFIG_USB_PXA25X_SMALL */
1860 static void udc_command(int cmd)
1863 case PXA2XX_UDC_CMD_CONNECT:
1864 setbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1865 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1868 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1869 GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
1871 debug("Connected to USB\n");
1874 case PXA2XX_UDC_CMD_DISCONNECT:
1875 /* disable pullup resistor */
1876 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1877 GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
1879 /* setup pin as input, line will float */
1880 clrbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1881 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1883 debug("Disconnected from USB\n");
1888 static struct pxa2xx_udc_mach_info mach_info = {
1889 .udc_command = udc_command,
1893 * when a driver is successfully registered, it will receive
1894 * control requests including set_configuration(), which enables
1895 * non-control requests. then usb traffic follows until a
1896 * disconnect is reported. then a host may connect again, or
1897 * the driver might get unbound.
1899 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1901 struct pxa25x_udc *dev = &memory;
1906 || driver->speed < USB_SPEED_FULL
1907 || !driver->disconnect
1915 /* Enable clock for usb controller */
1916 setbits_le32(CKEN, CKEN11_USB);
1918 /* first hook up the driver ... */
1919 dev->driver = driver;
1922 /* trigger chiprev-specific logic */
1923 switch ((chiprev = pxa_get_cpu_revision())) {
1929 /* A0/A1 "not released"; ep 13, 15 unusable */
1931 case PXA250_B2: case PXA210_B2:
1932 case PXA250_B1: case PXA210_B1:
1933 case PXA250_B0: case PXA210_B0:
1934 /* OUT-DMA is broken ... */
1936 case PXA250_C0: case PXA210_C0:
1939 printf("%s: unrecognized processor: %08x\n",
1940 DRIVER_NAME, chiprev);
1944 the_controller = dev;
1946 /* prepare watchdog timer */
1947 dev->watchdog.running = 0;
1948 dev->watchdog.period = 5000 * CONFIG_SYS_HZ / 1000000; /* 5 ms */
1949 dev->watchdog.function = udc_watchdog;
1951 dev->mach = &mach_info;
1956 dev->gadget.name = "pxa2xx_udc";
1957 retval = driver->bind(&dev->gadget);
1959 printf("bind to driver %s --> error %d\n",
1960 DRIVER_NAME, retval);
1966 * ... then enable host detection and ep0; and we're ready
1967 * for set_configuration as well as eventual disconnect.
1969 printf("registered gadget driver '%s'\n", DRIVER_NAME);
1977 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1981 /* don't disconnect drivers more than once */
1982 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1984 dev->gadget.speed = USB_SPEED_UNKNOWN;
1986 /* prevent new request submissions, kill any outstanding requests */
1987 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1988 struct pxa25x_ep *ep = &dev->ep[i];
1991 nuke(ep, -ESHUTDOWN);
1995 /* report disconnect; the driver is already quiesced */
1997 driver->disconnect(&dev->gadget);
1999 /* re-init driver-visible data structures */
2003 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2005 struct pxa25x_udc *dev = the_controller;
2009 if (!driver || driver != dev->driver || !driver->unbind)
2012 local_irq_disable();
2015 stop_activity(dev, driver);
2018 driver->unbind(&dev->gadget);
2021 printf("unregistered gadget driver '%s'\n", DRIVER_NAME);
2024 the_controller = NULL;
2026 clrbits_le32(CKEN, CKEN11_USB);
2031 extern void udc_disconnect(void)
2033 setbits_le32(CKEN, CKEN11_USB);
2034 udc_clear_mask_UDCCR(UDCCR_UDE);
2035 udc_command(PXA2XX_UDC_CMD_DISCONNECT);
2036 clrbits_le32(CKEN, CKEN11_USB);
2039 /*-------------------------------------------------------------------------*/
2042 usb_gadget_handle_interrupts(int index)
2044 return pxa25x_udc_irq();