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
22 #include <asm/byteorder.h>
23 #include <asm/system.h>
24 #include <asm/mach-types.h>
25 #include <asm/unaligned.h>
26 #include <dm/devres.h>
27 #include <linux/bug.h>
28 #include <linux/compat.h>
31 #include <asm/arch/pxa.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
35 #include <asm/arch/pxa-regs.h>
37 #include "pxa25x_udc.h"
40 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
41 * series processors. The UDC for the IXP 4xx series is very similar.
42 * There are fifteen endpoints, in addition to ep0.
44 * Such controller drivers work with a gadget driver. The gadget driver
45 * returns descriptors, implements configuration and data protocols used
46 * by the host to interact with this device, and allocates endpoints to
47 * the different protocol interfaces. The controller driver virtualizes
48 * usb hardware so that the gadget drivers will be more portable.
50 * This UDC hardware wants to implement a bit too much USB protocol, so
51 * it constrains the sorts of USB configuration change events that work.
52 * The errata for these chips are misleading; some "fixed" bugs from
53 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
55 * Note that the UDC hardware supports DMA (except on IXP) but that's
56 * not used here. IN-DMA (to host) is simple enough, when the data is
57 * suitably aligned (16 bytes) ... the network stack doesn't do that,
58 * other software can. OUT-DMA is buggy in most chip versions, as well
59 * as poorly designed (data toggle not automatic). So this driver won't
60 * bother using DMA. (Mostly-working IN-DMA support was available in
61 * kernels before 2.6.23, but was never enabled or well tested.)
64 #define DRIVER_VERSION "18-August-2012"
65 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
67 static const char driver_name[] = "pxa25x_udc";
68 static const char ep0name[] = "ep0";
71 static inline void start_watchdog(struct pxa25x_udc *udc)
73 debug("Started watchdog\n");
74 udc->watchdog.base = get_timer(0);
75 udc->watchdog.running = 1;
78 static inline void stop_watchdog(struct pxa25x_udc *udc)
80 udc->watchdog.running = 0;
81 debug("Stopped watchdog\n");
84 static inline void test_watchdog(struct pxa25x_udc *udc)
86 if (!udc->watchdog.running)
89 debug("watchdog %ld %ld\n", get_timer(udc->watchdog.base),
90 udc->watchdog.period);
92 if (get_timer(udc->watchdog.base) >= udc->watchdog.period) {
94 udc->watchdog.function(udc);
98 static void udc_watchdog(struct pxa25x_udc *dev)
100 uint32_t udccs0 = readl(&dev->regs->udccs[0]);
102 debug("Fired up udc_watchdog\n");
105 if (dev->ep0state == EP0_STALL
106 && (udccs0 & UDCCS0_FST) == 0
107 && (udccs0 & UDCCS0_SST) == 0) {
108 writel(UDCCS0_FST|UDCCS0_FTF, &dev->regs->udccs[0]);
109 debug("ep0 re-stall\n");
117 static const char * const state_name[] = {
119 "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
120 "EP0_END_XFER", "EP0_STALL"
124 dump_udccr(const char *label)
126 u32 udccr = readl(&UDC_REGS->udccr);
127 debug("%s %02X =%s%s%s%s%s%s%s%s\n",
129 (udccr & UDCCR_REM) ? " rem" : "",
130 (udccr & UDCCR_RSTIR) ? " rstir" : "",
131 (udccr & UDCCR_SRM) ? " srm" : "",
132 (udccr & UDCCR_SUSIR) ? " susir" : "",
133 (udccr & UDCCR_RESIR) ? " resir" : "",
134 (udccr & UDCCR_RSM) ? " rsm" : "",
135 (udccr & UDCCR_UDA) ? " uda" : "",
136 (udccr & UDCCR_UDE) ? " ude" : "");
140 dump_udccs0(const char *label)
142 u32 udccs0 = readl(&UDC_REGS->udccs[0]);
144 debug("%s %s %02X =%s%s%s%s%s%s%s%s\n",
145 label, state_name[the_controller->ep0state], udccs0,
146 (udccs0 & UDCCS0_SA) ? " sa" : "",
147 (udccs0 & UDCCS0_RNE) ? " rne" : "",
148 (udccs0 & UDCCS0_FST) ? " fst" : "",
149 (udccs0 & UDCCS0_SST) ? " sst" : "",
150 (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
151 (udccs0 & UDCCS0_FTF) ? " ftf" : "",
152 (udccs0 & UDCCS0_IPR) ? " ipr" : "",
153 (udccs0 & UDCCS0_OPR) ? " opr" : "");
157 dump_state(struct pxa25x_udc *dev)
162 debug("%s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
163 state_name[dev->ep0state],
164 readl(&UDC_REGS->uicr1), readl(&UDC_REGS->uicr0),
165 readl(&UDC_REGS->usir1), readl(&UDC_REGS->usir0),
166 readl(&UDC_REGS->ufnrh), readl(&UDC_REGS->ufnrl));
169 tmp = readl(&UDC_REGS->udccfr);
170 debug("udccfr %02X =%s%s\n", tmp,
171 (tmp & UDCCFR_AREN) ? " aren" : "",
172 (tmp & UDCCFR_ACM) ? " acm" : "");
176 debug("no gadget driver bound\n");
179 debug("ep0 driver '%s'\n", "ether");
181 dump_udccs0("udccs0");
182 debug("ep0 IN %lu/%lu, OUT %lu/%lu\n",
183 dev->stats.write.bytes, dev->stats.write.ops,
184 dev->stats.read.bytes, dev->stats.read.ops);
186 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
187 if (dev->ep[i].desc == NULL)
189 debug("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
195 static inline void dump_udccr(const char *label) { }
196 static inline void dump_udccs0(const char *label) { }
197 static inline void dump_state(struct pxa25x_udc *dev) { }
202 * ---------------------------------------------------------------------------
203 * endpoint related parts of the api to the usb controller hardware,
204 * used by gadget driver; and the inner talker-to-hardware core.
205 * ---------------------------------------------------------------------------
208 static void pxa25x_ep_fifo_flush(struct usb_ep *ep);
209 static void nuke(struct pxa25x_ep *, int status);
211 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
212 static void pullup_off(void)
214 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
216 if (mach->udc_command)
217 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
220 static void pullup_on(void)
222 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
224 if (mach->udc_command)
225 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
228 static void pio_irq_enable(int bEndpointAddress)
230 bEndpointAddress &= 0xf;
231 if (bEndpointAddress < 8) {
232 clrbits_le32(&the_controller->regs->uicr0,
233 1 << bEndpointAddress);
235 bEndpointAddress -= 8;
236 clrbits_le32(&the_controller->regs->uicr1,
237 1 << bEndpointAddress);
241 static void pio_irq_disable(int bEndpointAddress)
243 bEndpointAddress &= 0xf;
244 if (bEndpointAddress < 8) {
245 setbits_le32(&the_controller->regs->uicr0,
246 1 << bEndpointAddress);
248 bEndpointAddress -= 8;
249 setbits_le32(&the_controller->regs->uicr1,
250 1 << bEndpointAddress);
254 static inline void udc_set_mask_UDCCR(int mask)
257 * The UDCCR reg contains mask and interrupt status bits,
258 * so using '|=' isn't safe as it may ack an interrupt.
260 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
263 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
266 static inline void udc_clear_mask_UDCCR(int mask)
268 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
270 mask = ~mask & mask_bits;
271 clrbits_le32(&the_controller->regs->udccr, ~mask);
274 static inline void udc_ack_int_UDCCR(int mask)
276 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
279 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
283 * endpoint enable/disable
285 * we need to verify the descriptors used to enable endpoints. since pxa25x
286 * endpoint configurations are fixed, and are pretty much always enabled,
287 * there's not a lot to manage here.
289 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
290 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
291 * for a single interface (with only the default altsetting) and for gadget
292 * drivers that don't halt endpoints (not reset by set_interface). that also
293 * means that if you use ISO, you must violate the USB spec rule that all
294 * iso endpoints must be in non-default altsettings.
296 static int pxa25x_ep_enable(struct usb_ep *_ep,
297 const struct usb_endpoint_descriptor *desc)
299 struct pxa25x_ep *ep;
300 struct pxa25x_udc *dev;
302 ep = container_of(_ep, struct pxa25x_ep, ep);
303 if (!_ep || !desc || ep->desc || _ep->name == ep0name
304 || desc->bDescriptorType != USB_DT_ENDPOINT
305 || ep->bEndpointAddress != desc->bEndpointAddress
307 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
308 printf("%s, bad ep or descriptor\n", __func__);
312 /* xfer types must match, except that interrupt ~= bulk */
313 if (ep->bmAttributes != desc->bmAttributes
314 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
315 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
316 printf("%s, %s type mismatch\n", __func__, _ep->name);
320 /* hardware _could_ do smaller, but driver doesn't */
321 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
322 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
324 || !get_unaligned(&desc->wMaxPacketSize)) {
325 printf("%s, bad %s maxpacket\n", __func__, _ep->name);
330 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
331 printf("%s, bogus device state\n", __func__);
338 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
340 /* flush fifo (mostly for OUT buffers) */
341 pxa25x_ep_fifo_flush(_ep);
343 /* ... reset halt state too, if we could ... */
345 debug("enabled %s\n", _ep->name);
349 static int pxa25x_ep_disable(struct usb_ep *_ep)
351 struct pxa25x_ep *ep;
354 ep = container_of(_ep, struct pxa25x_ep, ep);
355 if (!_ep || !ep->desc) {
356 printf("%s, %s not enabled\n", __func__,
357 _ep ? ep->ep.name : NULL);
360 local_irq_save(flags);
362 nuke(ep, -ESHUTDOWN);
364 /* flush fifo (mostly for IN buffers) */
365 pxa25x_ep_fifo_flush(_ep);
370 local_irq_restore(flags);
371 debug("%s disabled\n", _ep->name);
375 /*-------------------------------------------------------------------------*/
378 * for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
379 * must still pass correctly initialized endpoints, since other controller
380 * drivers may care about how it's currently set up (dma issues etc).
384 * pxa25x_ep_alloc_request - allocate a request data structure
386 static struct usb_request *
387 pxa25x_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
389 struct pxa25x_request *req;
391 req = kzalloc(sizeof(*req), gfp_flags);
395 INIT_LIST_HEAD(&req->queue);
401 * pxa25x_ep_free_request - deallocate a request data structure
404 pxa25x_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
406 struct pxa25x_request *req;
408 req = container_of(_req, struct pxa25x_request, req);
409 WARN_ON(!list_empty(&req->queue));
413 /*-------------------------------------------------------------------------*/
416 * done - retire a request; caller blocked irqs
418 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
420 unsigned stopped = ep->stopped;
422 list_del_init(&req->queue);
424 if (likely(req->req.status == -EINPROGRESS))
425 req->req.status = status;
427 status = req->req.status;
429 if (status && status != -ESHUTDOWN)
430 debug("complete %s req %p stat %d len %u/%u\n",
431 ep->ep.name, &req->req, status,
432 req->req.actual, req->req.length);
434 /* don't modify queue heads during completion callback */
436 req->req.complete(&ep->ep, &req->req);
437 ep->stopped = stopped;
441 static inline void ep0_idle(struct pxa25x_udc *dev)
443 dev->ep0state = EP0_IDLE;
447 write_packet(u32 *uddr, struct pxa25x_request *req, unsigned max)
450 unsigned length, count;
452 debug("%s(): uddr %p\n", __func__, uddr);
454 buf = req->req.buf + req->req.actual;
457 /* how big will this packet be? */
458 length = min(req->req.length - req->req.actual, max);
459 req->req.actual += length;
462 while (likely(count--))
463 writeb(*buf++, uddr);
469 * write to an IN endpoint fifo, as many packets as possible.
470 * irqs will use this to write the rest later.
471 * caller guarantees at least one packet buffer is ready (or a zlp).
474 write_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
478 max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
481 int is_last, is_short;
483 count = write_packet(ep->reg_uddr, req, max);
485 /* last packet is usually short (or a zlp) */
486 if (unlikely(count != max))
487 is_last = is_short = 1;
489 if (likely(req->req.length != req->req.actual)
494 /* interrupt/iso maxpacket may not fill the fifo */
495 is_short = unlikely(max < ep->fifo_size);
498 debug_cond(NOISY, "wrote %s %d bytes%s%s %d left %p\n",
500 is_last ? "/L" : "", is_short ? "/S" : "",
501 req->req.length - req->req.actual, req);
504 * let loose that packet. maybe try writing another one,
505 * double buffering might work. TSP, TPC, and TFS
506 * bit values are the same for all normal IN endpoints.
508 writel(UDCCS_BI_TPC, ep->reg_udccs);
510 writel(UDCCS_BI_TSP, ep->reg_udccs);
512 /* requests complete when all IN data is in the FIFO */
515 if (list_empty(&ep->queue))
516 pio_irq_disable(ep->bEndpointAddress);
521 * TODO experiment: how robust can fifo mode tweaking be?
522 * double buffering is off in the default fifo mode, which
523 * prevents TFS from being set here.
526 } while (readl(ep->reg_udccs) & UDCCS_BI_TFS);
531 * caller asserts req->pending (ep0 irq status nyet cleared); starts
532 * ep0 data stage. these chips want very simple state transitions.
535 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
537 writel(flags|UDCCS0_SA|UDCCS0_OPR, &dev->regs->udccs[0]);
538 writel(USIR0_IR0, &dev->regs->usir0);
539 dev->req_pending = 0;
540 debug_cond(NOISY, "%s() %s, udccs0: %02x/%02x usir: %X.%X\n",
541 __func__, tag, readl(&dev->regs->udccs[0]), flags,
542 readl(&dev->regs->usir1), readl(&dev->regs->usir0));
546 write_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
551 count = write_packet(&ep->dev->regs->uddr0, req, EP0_FIFO_SIZE);
552 ep->dev->stats.write.bytes += count;
554 /* last packet "must be" short (or a zlp) */
555 is_short = (count != EP0_FIFO_SIZE);
557 debug_cond(NOISY, "ep0in %d bytes %d left %p\n", count,
558 req->req.length - req->req.actual, req);
560 if (unlikely(is_short)) {
561 if (ep->dev->req_pending)
562 ep0start(ep->dev, UDCCS0_IPR, "short IN");
564 writel(UDCCS0_IPR, &ep->dev->regs->udccs[0]);
566 count = req->req.length;
571 * This seems to get rid of lost status irqs in some cases:
572 * host responds quickly, or next request involves config
573 * change automagic, or should have been hidden, or ...
575 * FIXME get rid of all udelays possible...
577 if (count >= EP0_FIFO_SIZE) {
580 if ((readl(&ep->dev->regs->udccs[0]) &
582 /* clear OPR, generate ack */
584 &ep->dev->regs->udccs[0]);
591 } else if (ep->dev->req_pending)
592 ep0start(ep->dev, 0, "IN");
599 * read_fifo - unload packet(s) from the fifo we use for usb OUT
600 * transfers and put them into the request. caller should have made
601 * sure there's at least one packet ready.
603 * returns true if the request completed because of short packet or the
604 * request buffer having filled (and maybe overran till end-of-packet).
607 read_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
611 unsigned bufferspace, count, is_short;
615 * make sure there's a packet in the FIFO.
616 * UDCCS_{BO,IO}_RPC are all the same bit value.
617 * UDCCS_{BO,IO}_RNE are all the same bit value.
619 udccs = readl(ep->reg_udccs);
620 if (unlikely((udccs & UDCCS_BO_RPC) == 0))
622 buf = req->req.buf + req->req.actual;
624 bufferspace = req->req.length - req->req.actual;
626 /* read all bytes from this packet */
627 if (likely(udccs & UDCCS_BO_RNE)) {
628 count = 1 + (0x0ff & readl(ep->reg_ubcr));
629 req->req.actual += min(count, bufferspace);
632 is_short = (count < ep->ep.maxpacket);
633 debug_cond(NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
634 ep->ep.name, udccs, count,
635 is_short ? "/S" : "",
636 req, req->req.actual, req->req.length);
637 while (likely(count-- != 0)) {
638 u8 byte = readb(ep->reg_uddr);
640 if (unlikely(bufferspace == 0)) {
642 * this happens when the driver's buffer
643 * is smaller than what the host sent.
644 * discard the extra data.
646 if (req->req.status != -EOVERFLOW)
647 printf("%s overflow %d\n",
649 req->req.status = -EOVERFLOW;
655 writel(UDCCS_BO_RPC, ep->reg_udccs);
656 /* RPC/RSP/RNE could now reflect the other packet buffer */
658 /* iso is one request per packet */
659 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
660 if (udccs & UDCCS_IO_ROF)
661 req->req.status = -EHOSTUNREACH;
662 /* more like "is_done" */
667 if (is_short || req->req.actual == req->req.length) {
669 if (list_empty(&ep->queue))
670 pio_irq_disable(ep->bEndpointAddress);
674 /* finished that packet. the next one may be waiting... */
680 * special ep0 version of the above. no UBCR0 or double buffering; status
681 * handshaking is magic. most device protocols don't need control-OUT.
682 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
683 * protocols do use them.
686 read_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
689 unsigned bufferspace;
691 buf = req->req.buf + req->req.actual;
692 bufferspace = req->req.length - req->req.actual;
694 while (readl(&ep->dev->regs->udccs[0]) & UDCCS0_RNE) {
695 byte = (u8)readb(&ep->dev->regs->uddr0);
697 if (unlikely(bufferspace == 0)) {
699 * this happens when the driver's buffer
700 * is smaller than what the host sent.
701 * discard the extra data.
703 if (req->req.status != -EOVERFLOW)
704 printf("%s overflow\n", ep->ep.name);
705 req->req.status = -EOVERFLOW;
713 writel(UDCCS0_OPR | UDCCS0_IPR, &ep->dev->regs->udccs[0]);
716 if (req->req.actual >= req->req.length)
719 /* finished that packet. the next one may be waiting... */
723 /*-------------------------------------------------------------------------*/
726 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
728 struct pxa25x_request *req;
729 struct pxa25x_ep *ep;
730 struct pxa25x_udc *dev;
733 req = container_of(_req, struct pxa25x_request, req);
734 if (unlikely(!_req || !_req->complete || !_req->buf
735 || !list_empty(&req->queue))) {
736 printf("%s, bad params\n", __func__);
740 ep = container_of(_ep, struct pxa25x_ep, ep);
741 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
742 printf("%s, bad ep\n", __func__);
747 if (unlikely(!dev->driver
748 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
749 printf("%s, bogus device state\n", __func__);
754 * iso is always one packet per request, that's the only way
755 * we can report per-packet status. that also helps with dma.
757 if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
759 le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
762 debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
763 _ep->name, _req, _req->length, _req->buf);
765 local_irq_save(flags);
767 _req->status = -EINPROGRESS;
770 /* kickstart this i/o queue? */
771 if (list_empty(&ep->queue) && !ep->stopped) {
772 if (ep->desc == NULL/* ep0 */) {
773 unsigned length = _req->length;
775 switch (dev->ep0state) {
776 case EP0_IN_DATA_PHASE:
777 dev->stats.write.ops++;
778 if (write_ep0_fifo(ep, req))
782 case EP0_OUT_DATA_PHASE:
783 dev->stats.read.ops++;
785 if (dev->req_config) {
786 debug("ep0 config ack%s\n",
787 dev->has_cfr ? "" : " raced");
789 writel(UDCCFR_AREN|UDCCFR_ACM
791 &ep->dev->regs->udccfr);
793 dev->ep0state = EP0_END_XFER;
794 local_irq_restore(flags);
797 if (dev->req_pending)
798 ep0start(dev, UDCCS0_IPR, "OUT");
801 &ep->dev->regs->udccs[0])
803 && read_ep0_fifo(ep, req))) {
811 printf("ep0 i/o, odd state %d\n",
813 local_irq_restore(flags);
816 /* can the FIFO can satisfy the request immediately? */
817 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
818 if ((readl(ep->reg_udccs) & UDCCS_BI_TFS) != 0
819 && write_fifo(ep, req))
821 } else if ((readl(ep->reg_udccs) & UDCCS_BO_RFS) != 0
822 && read_fifo(ep, req)) {
826 if (likely(req && ep->desc))
827 pio_irq_enable(ep->bEndpointAddress);
830 /* pio or dma irq handler advances the queue. */
831 if (likely(req != NULL))
832 list_add_tail(&req->queue, &ep->queue);
833 local_irq_restore(flags);
840 * nuke - dequeue ALL requests
842 static void nuke(struct pxa25x_ep *ep, int status)
844 struct pxa25x_request *req;
846 /* called with irqs blocked */
847 while (!list_empty(&ep->queue)) {
848 req = list_entry(ep->queue.next,
849 struct pxa25x_request,
851 done(ep, req, status);
854 pio_irq_disable(ep->bEndpointAddress);
858 /* dequeue JUST ONE request */
859 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
861 struct pxa25x_ep *ep;
862 struct pxa25x_request *req;
865 ep = container_of(_ep, struct pxa25x_ep, ep);
866 if (!_ep || ep->ep.name == ep0name)
869 local_irq_save(flags);
871 /* make sure it's actually queued on this endpoint */
872 list_for_each_entry(req, &ep->queue, queue) {
873 if (&req->req == _req)
876 if (&req->req != _req) {
877 local_irq_restore(flags);
881 done(ep, req, -ECONNRESET);
883 local_irq_restore(flags);
887 /*-------------------------------------------------------------------------*/
889 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
891 struct pxa25x_ep *ep;
894 ep = container_of(_ep, struct pxa25x_ep, ep);
896 || (!ep->desc && ep->ep.name != ep0name))
897 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
898 printf("%s, bad ep\n", __func__);
903 * this path (reset toggle+halt) is needed to implement
904 * SET_INTERFACE on normal hardware. but it can't be
905 * done from software on the PXA UDC, and the hardware
906 * forgets to do it as part of SET_INTERFACE automagic.
908 printf("only host can clear %s halt\n", _ep->name);
912 local_irq_save(flags);
914 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
915 && ((readl(ep->reg_udccs) & UDCCS_BI_TFS) == 0
916 || !list_empty(&ep->queue))) {
917 local_irq_restore(flags);
921 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
922 writel(UDCCS_BI_FST|UDCCS_BI_FTF, ep->reg_udccs);
924 /* ep0 needs special care */
926 start_watchdog(ep->dev);
927 ep->dev->req_pending = 0;
928 ep->dev->ep0state = EP0_STALL;
930 /* and bulk/intr endpoints like dropping stalls too */
933 for (i = 0; i < 1000; i += 20) {
934 if (readl(ep->reg_udccs) & UDCCS_BI_SST)
939 local_irq_restore(flags);
941 debug("%s halt\n", _ep->name);
945 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
947 struct pxa25x_ep *ep;
949 ep = container_of(_ep, struct pxa25x_ep, ep);
951 printf("%s, bad ep\n", __func__);
954 /* pxa can't report unclaimed bytes from IN fifos */
955 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
957 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
958 || (readl(ep->reg_udccs) & UDCCS_BO_RFS) == 0)
961 return (readl(ep->reg_ubcr) & 0xfff) + 1;
964 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
966 struct pxa25x_ep *ep;
968 ep = container_of(_ep, struct pxa25x_ep, ep);
969 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
970 printf("%s, bad ep\n", __func__);
974 /* toggle and halt bits stay unchanged */
976 /* for OUT, just read and discard the FIFO contents. */
977 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
978 while (((readl(ep->reg_udccs)) & UDCCS_BO_RNE) != 0)
979 (void)readb(ep->reg_uddr);
983 /* most IN status is the same, but ISO can't stall */
984 writel(UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
985 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
986 ? 0 : UDCCS_BI_SST), ep->reg_udccs);
990 static struct usb_ep_ops pxa25x_ep_ops = {
991 .enable = pxa25x_ep_enable,
992 .disable = pxa25x_ep_disable,
994 .alloc_request = pxa25x_ep_alloc_request,
995 .free_request = pxa25x_ep_free_request,
997 .queue = pxa25x_ep_queue,
998 .dequeue = pxa25x_ep_dequeue,
1000 .set_halt = pxa25x_ep_set_halt,
1001 .fifo_status = pxa25x_ep_fifo_status,
1002 .fifo_flush = pxa25x_ep_fifo_flush,
1006 /* ---------------------------------------------------------------------------
1007 * device-scoped parts of the api to the usb controller hardware
1008 * ---------------------------------------------------------------------------
1011 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
1013 return ((readl(&the_controller->regs->ufnrh) & 0x07) << 8) |
1014 (readl(&the_controller->regs->ufnrl) & 0xff);
1017 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
1019 /* host may not have enabled remote wakeup */
1020 if ((readl(&the_controller->regs->udccs[0]) & UDCCS0_DRWF) == 0)
1021 return -EHOSTUNREACH;
1022 udc_set_mask_UDCCR(UDCCR_RSM);
1026 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
1027 static void udc_enable(struct pxa25x_udc *);
1028 static void udc_disable(struct pxa25x_udc *);
1031 * We disable the UDC -- and its 48 MHz clock -- whenever it's not
1034 static int pullup(struct pxa25x_udc *udc)
1042 int is_active = udc->pullup;
1050 if (udc->gadget.speed != USB_SPEED_UNKNOWN)
1051 stop_activity(udc, udc->driver);
1060 /* VBUS reporting logically comes from a transceiver */
1061 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1063 struct pxa25x_udc *udc;
1065 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1066 printf("vbus %s\n", is_active ? "supplied" : "inactive");
1071 /* drivers may have software control over D+ pullup */
1072 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
1074 struct pxa25x_udc *udc;
1076 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1078 /* not all boards support pullup control */
1079 if (!udc->mach->udc_command)
1082 udc->pullup = (is_active != 0);
1088 * boards may consume current from VBUS, up to 100-500mA based on config.
1089 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
1090 * violate USB specs.
1092 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1097 static const struct usb_gadget_ops pxa25x_udc_ops = {
1098 .get_frame = pxa25x_udc_get_frame,
1099 .wakeup = pxa25x_udc_wakeup,
1100 .vbus_session = pxa25x_udc_vbus_session,
1101 .pullup = pxa25x_udc_pullup,
1102 .vbus_draw = pxa25x_udc_vbus_draw,
1105 /*-------------------------------------------------------------------------*/
1108 * udc_disable - disable USB device controller
1110 static void udc_disable(struct pxa25x_udc *dev)
1112 /* block all irqs */
1113 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1114 writel(0xff, &dev->regs->uicr0);
1115 writel(0xff, &dev->regs->uicr1);
1116 writel(UFNRH_SIM, &dev->regs->ufnrh);
1118 /* if hardware supports it, disconnect from usb */
1121 udc_clear_mask_UDCCR(UDCCR_UDE);
1124 dev->gadget.speed = USB_SPEED_UNKNOWN;
1128 * udc_reinit - initialize software state
1130 static void udc_reinit(struct pxa25x_udc *dev)
1134 /* device/ep0 records init */
1135 INIT_LIST_HEAD(&dev->gadget.ep_list);
1136 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1137 dev->ep0state = EP0_IDLE;
1139 /* basic endpoint records init */
1140 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1141 struct pxa25x_ep *ep = &dev->ep[i];
1144 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1148 INIT_LIST_HEAD(&ep->queue);
1152 /* the rest was statically initialized, and is read-only */
1156 * until it's enabled, this UDC should be completely invisible
1159 static void udc_enable(struct pxa25x_udc *dev)
1161 debug("udc: enabling udc\n");
1163 udc_clear_mask_UDCCR(UDCCR_UDE);
1166 * Try to clear these bits before we enable the udc.
1167 * Do not touch reset ack bit, we would take care of it in
1168 * interrupt handle routine
1170 udc_ack_int_UDCCR(UDCCR_SUSIR|UDCCR_RESIR);
1173 dev->gadget.speed = USB_SPEED_UNKNOWN;
1174 dev->stats.irqs = 0;
1177 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1179 * - if RESET is already in progress, ack interrupt
1180 * - unmask reset interrupt
1182 udc_set_mask_UDCCR(UDCCR_UDE);
1183 if (!(readl(&dev->regs->udccr) & UDCCR_UDA))
1184 udc_ack_int_UDCCR(UDCCR_RSTIR);
1186 if (dev->has_cfr /* UDC_RES2 is defined */) {
1188 * pxa255 (a0+) can avoid a set_config race that could
1189 * prevent gadget drivers from configuring correctly
1191 writel(UDCCFR_ACM | UDCCFR_MB1, &dev->regs->udccfr);
1194 /* enable suspend/resume and reset irqs */
1195 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1197 /* enable ep0 irqs */
1198 clrbits_le32(&dev->regs->uicr0, UICR0_IM0);
1200 /* if hardware supports it, pullup D+ and wait for reset */
1204 static inline void clear_ep_state(struct pxa25x_udc *dev)
1209 * hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1210 * fifos, and pending transactions mustn't be continued in any case.
1212 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1213 nuke(&dev->ep[i], -ECONNABORTED);
1216 static void handle_ep0(struct pxa25x_udc *dev)
1218 u32 udccs0 = readl(&dev->regs->udccs[0]);
1219 struct pxa25x_ep *ep = &dev->ep[0];
1220 struct pxa25x_request *req;
1222 struct usb_ctrlrequest r;
1227 if (list_empty(&ep->queue))
1230 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1232 /* clear stall status */
1233 if (udccs0 & UDCCS0_SST) {
1235 writel(UDCCS0_SST, &dev->regs->udccs[0]);
1240 /* previous request unfinished? non-error iff back-to-back ... */
1241 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1247 switch (dev->ep0state) {
1249 /* late-breaking status? */
1250 udccs0 = readl(&dev->regs->udccs[0]);
1252 /* start control request? */
1253 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1254 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1259 /* read SETUP packet */
1260 for (i = 0; i < 8; i++) {
1261 if (unlikely(!(readl(&dev->regs->udccs[0]) &
1264 debug("SETUP %d!\n", i);
1267 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1269 if (unlikely((readl(&dev->regs->udccs[0]) &
1274 debug("SETUP %02x.%02x v%04x i%04x l%04x\n",
1275 u.r.bRequestType, u.r.bRequest,
1276 le16_to_cpu(u.r.wValue),
1277 le16_to_cpu(u.r.wIndex),
1278 le16_to_cpu(u.r.wLength));
1280 /* cope with automagic for some standard requests. */
1281 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1282 == USB_TYPE_STANDARD;
1283 dev->req_config = 0;
1284 dev->req_pending = 1;
1285 switch (u.r.bRequest) {
1286 /* hardware restricts gadget drivers here! */
1287 case USB_REQ_SET_CONFIGURATION:
1288 debug("GOT SET_CONFIGURATION\n");
1289 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1291 * reflect hardware's automagic
1292 * up to the gadget driver.
1295 dev->req_config = 1;
1296 clear_ep_state(dev);
1298 * if !has_cfr, there's no synch
1299 * else use AREN (later) not SA|OPR
1300 * USIR0_IR0 acts edge sensitive
1304 /* ... and here, even more ... */
1305 case USB_REQ_SET_INTERFACE:
1306 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1308 * udc hardware is broken by design:
1309 * - altsetting may only be zero;
1310 * - hw resets all interfaces' eps;
1311 * - ep reset doesn't include halt(?).
1313 printf("broken set_interface (%d/%d)\n",
1314 le16_to_cpu(u.r.wIndex),
1315 le16_to_cpu(u.r.wValue));
1319 /* hardware was supposed to hide this */
1320 case USB_REQ_SET_ADDRESS:
1321 debug("GOT SET ADDRESS\n");
1322 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1323 ep0start(dev, 0, "address");
1329 if (u.r.bRequestType & USB_DIR_IN)
1330 dev->ep0state = EP0_IN_DATA_PHASE;
1332 dev->ep0state = EP0_OUT_DATA_PHASE;
1334 i = dev->driver->setup(&dev->gadget, &u.r);
1336 /* hardware automagic preventing STALL... */
1337 if (dev->req_config) {
1339 * hardware sometimes neglects to tell
1340 * tell us about config change events,
1341 * so later ones may fail...
1343 printf("config change %02x fail %d?\n",
1347 * TODO experiment: if has_cfr,
1348 * hardware didn't ACK; maybe we
1349 * could actually STALL!
1354 /* uninitialized when goto stall */
1357 debug("protocol STALL, "
1359 readl(&dev->regs->udccs[0]), i);
1362 * the watchdog timer helps deal with cases
1363 * where udc seems to clear FST wrongly, and
1364 * then NAKs instead of STALLing.
1366 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1367 start_watchdog(dev);
1368 dev->ep0state = EP0_STALL;
1370 /* deferred i/o == no response yet */
1371 } else if (dev->req_pending) {
1372 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1373 || dev->req_std || u.r.wLength))
1374 ep0start(dev, 0, "defer");
1376 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1379 /* expect at least one data or status stage irq */
1382 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1383 == (UDCCS0_OPR|UDCCS0_SA))) {
1387 * pxa210/250 erratum 131 for B0/B1 says RNE lies.
1388 * still observed on a pxa255 a0.
1393 /* read SETUP data, but don't trust it too much */
1394 for (i = 0; i < 8; i++)
1395 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1396 if ((u.r.bRequestType & USB_RECIP_MASK)
1399 if (u.word[0] == 0 && u.word[1] == 0)
1404 * some random early IRQ:
1407 * - OPR got set, without SA (likely status stage)
1409 debug("random IRQ %X %X\n", udccs0,
1410 readl(&dev->regs->udccs[0]));
1411 writel(udccs0 & (UDCCS0_SA|UDCCS0_OPR),
1412 &dev->regs->udccs[0]);
1415 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1416 if (udccs0 & UDCCS0_OPR) {
1417 debug("ep0in premature status\n");
1421 } else /* irq was IPR clearing */ {
1423 debug("next ep0 in packet\n");
1424 /* this IN packet might finish the request */
1425 (void) write_ep0_fifo(ep, req);
1426 } /* else IN token before response was written */
1429 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1430 if (udccs0 & UDCCS0_OPR) {
1432 /* this OUT packet might finish the request */
1433 if (read_ep0_fifo(ep, req))
1435 /* else more OUT packets expected */
1436 } /* else OUT token before read was issued */
1437 } else /* irq was IPR clearing */ {
1438 debug("ep0out premature status\n");
1448 * ack control-IN status (maybe in-zlp was skipped)
1449 * also appears after some config change events.
1451 if (udccs0 & UDCCS0_OPR)
1452 writel(UDCCS0_OPR, &dev->regs->udccs[0]);
1456 writel(UDCCS0_FST, &dev->regs->udccs[0]);
1460 writel(USIR0_IR0, &dev->regs->usir0);
1463 static void handle_ep(struct pxa25x_ep *ep)
1465 struct pxa25x_request *req;
1466 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1472 if (likely(!list_empty(&ep->queue)))
1473 req = list_entry(ep->queue.next,
1474 struct pxa25x_request, queue);
1478 /* TODO check FST handling */
1480 udccs = readl(ep->reg_udccs);
1481 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1483 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1484 tmp |= UDCCS_BI_SST;
1487 writel(tmp, ep->reg_udccs);
1488 if (req && likely((udccs & UDCCS_BI_TFS) != 0))
1489 completed = write_fifo(ep, req);
1491 } else { /* irq from RPC (or for ISO, ROF) */
1492 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1493 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1495 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1498 writel(tmp, ep->reg_udccs);
1500 /* fifos can hold packets, ready for reading... */
1502 completed = read_fifo(ep, req);
1504 pio_irq_disable(ep->bEndpointAddress);
1507 } while (completed);
1511 * pxa25x_udc_irq - interrupt handler
1513 * avoid delays in ep0 processing. the control handshaking isn't always
1514 * under software control (pxa250c0 and the pxa255 are better), and delays
1515 * could cause usb protocol errors.
1517 static struct pxa25x_udc memory;
1519 pxa25x_udc_irq(void)
1521 struct pxa25x_udc *dev = &memory;
1528 u32 udccr = readl(&dev->regs->udccr);
1532 /* SUSpend Interrupt Request */
1533 if (unlikely(udccr & UDCCR_SUSIR)) {
1534 udc_ack_int_UDCCR(UDCCR_SUSIR);
1536 debug("USB suspend\n");
1538 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1540 && dev->driver->suspend)
1541 dev->driver->suspend(&dev->gadget);
1545 /* RESume Interrupt Request */
1546 if (unlikely(udccr & UDCCR_RESIR)) {
1547 udc_ack_int_UDCCR(UDCCR_RESIR);
1549 debug("USB resume\n");
1551 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1553 && dev->driver->resume)
1554 dev->driver->resume(&dev->gadget);
1557 /* ReSeT Interrupt Request - USB reset */
1558 if (unlikely(udccr & UDCCR_RSTIR)) {
1559 udc_ack_int_UDCCR(UDCCR_RSTIR);
1562 if ((readl(&dev->regs->udccr) & UDCCR_UDA) == 0) {
1563 debug("USB reset start\n");
1566 * reset driver and endpoints,
1567 * in case that's not yet done
1569 stop_activity(dev, dev->driver);
1572 debug("USB reset end\n");
1573 dev->gadget.speed = USB_SPEED_FULL;
1574 memset(&dev->stats, 0, sizeof dev->stats);
1575 /* driver and endpoints are still reset */
1579 u32 uicr0 = readl(&dev->regs->uicr0);
1580 u32 uicr1 = readl(&dev->regs->uicr1);
1581 u32 usir0 = readl(&dev->regs->usir0);
1582 u32 usir1 = readl(&dev->regs->usir1);
1584 usir0 = usir0 & ~uicr0;
1585 usir1 = usir1 & ~uicr1;
1588 if (unlikely(!usir0 && !usir1))
1591 debug_cond(NOISY, "irq %02x.%02x\n", usir1, usir0);
1593 /* control traffic */
1594 if (usir0 & USIR0_IR0) {
1595 dev->ep[0].pio_irqs++;
1600 /* endpoint data transfers */
1601 for (i = 0; i < 8; i++) {
1604 if (i && (usir0 & tmp)) {
1605 handle_ep(&dev->ep[i]);
1606 setbits_le32(&dev->regs->usir0, tmp);
1609 #ifndef CONFIG_USB_PXA25X_SMALL
1611 handle_ep(&dev->ep[i+8]);
1612 setbits_le32(&dev->regs->usir1, tmp);
1619 /* we could also ask for 1 msec SOF (SIR) interrupts */
1625 /*-------------------------------------------------------------------------*/
1628 * this uses load-time allocation and initialization (instead of
1629 * doing it at run-time) to save code, eliminate fault paths, and
1630 * be more obviously correct.
1632 static struct pxa25x_udc memory = {
1636 .ops = &pxa25x_udc_ops,
1637 .ep0 = &memory.ep[0].ep,
1638 .name = driver_name,
1641 /* control endpoint */
1645 .ops = &pxa25x_ep_ops,
1646 .maxpacket = EP0_FIFO_SIZE,
1649 .reg_udccs = &UDC_REGS->udccs[0],
1650 .reg_uddr = &UDC_REGS->uddr0,
1653 /* first group of endpoints */
1656 .name = "ep1in-bulk",
1657 .ops = &pxa25x_ep_ops,
1658 .maxpacket = BULK_FIFO_SIZE,
1661 .fifo_size = BULK_FIFO_SIZE,
1662 .bEndpointAddress = USB_DIR_IN | 1,
1663 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1664 .reg_udccs = &UDC_REGS->udccs[1],
1665 .reg_uddr = &UDC_REGS->uddr1,
1669 .name = "ep2out-bulk",
1670 .ops = &pxa25x_ep_ops,
1671 .maxpacket = BULK_FIFO_SIZE,
1674 .fifo_size = BULK_FIFO_SIZE,
1675 .bEndpointAddress = 2,
1676 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1677 .reg_udccs = &UDC_REGS->udccs[2],
1678 .reg_ubcr = &UDC_REGS->ubcr2,
1679 .reg_uddr = &UDC_REGS->uddr2,
1681 #ifndef CONFIG_USB_PXA25X_SMALL
1684 .name = "ep3in-iso",
1685 .ops = &pxa25x_ep_ops,
1686 .maxpacket = ISO_FIFO_SIZE,
1689 .fifo_size = ISO_FIFO_SIZE,
1690 .bEndpointAddress = USB_DIR_IN | 3,
1691 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1692 .reg_udccs = &UDC_REGS->udccs[3],
1693 .reg_uddr = &UDC_REGS->uddr3,
1697 .name = "ep4out-iso",
1698 .ops = &pxa25x_ep_ops,
1699 .maxpacket = ISO_FIFO_SIZE,
1702 .fifo_size = ISO_FIFO_SIZE,
1703 .bEndpointAddress = 4,
1704 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1705 .reg_udccs = &UDC_REGS->udccs[4],
1706 .reg_ubcr = &UDC_REGS->ubcr4,
1707 .reg_uddr = &UDC_REGS->uddr4,
1711 .name = "ep5in-int",
1712 .ops = &pxa25x_ep_ops,
1713 .maxpacket = INT_FIFO_SIZE,
1716 .fifo_size = INT_FIFO_SIZE,
1717 .bEndpointAddress = USB_DIR_IN | 5,
1718 .bmAttributes = USB_ENDPOINT_XFER_INT,
1719 .reg_udccs = &UDC_REGS->udccs[5],
1720 .reg_uddr = &UDC_REGS->uddr5,
1723 /* second group of endpoints */
1726 .name = "ep6in-bulk",
1727 .ops = &pxa25x_ep_ops,
1728 .maxpacket = BULK_FIFO_SIZE,
1731 .fifo_size = BULK_FIFO_SIZE,
1732 .bEndpointAddress = USB_DIR_IN | 6,
1733 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1734 .reg_udccs = &UDC_REGS->udccs[6],
1735 .reg_uddr = &UDC_REGS->uddr6,
1739 .name = "ep7out-bulk",
1740 .ops = &pxa25x_ep_ops,
1741 .maxpacket = BULK_FIFO_SIZE,
1744 .fifo_size = BULK_FIFO_SIZE,
1745 .bEndpointAddress = 7,
1746 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1747 .reg_udccs = &UDC_REGS->udccs[7],
1748 .reg_ubcr = &UDC_REGS->ubcr7,
1749 .reg_uddr = &UDC_REGS->uddr7,
1753 .name = "ep8in-iso",
1754 .ops = &pxa25x_ep_ops,
1755 .maxpacket = ISO_FIFO_SIZE,
1758 .fifo_size = ISO_FIFO_SIZE,
1759 .bEndpointAddress = USB_DIR_IN | 8,
1760 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1761 .reg_udccs = &UDC_REGS->udccs[8],
1762 .reg_uddr = &UDC_REGS->uddr8,
1766 .name = "ep9out-iso",
1767 .ops = &pxa25x_ep_ops,
1768 .maxpacket = ISO_FIFO_SIZE,
1771 .fifo_size = ISO_FIFO_SIZE,
1772 .bEndpointAddress = 9,
1773 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1774 .reg_udccs = &UDC_REGS->udccs[9],
1775 .reg_ubcr = &UDC_REGS->ubcr9,
1776 .reg_uddr = &UDC_REGS->uddr9,
1780 .name = "ep10in-int",
1781 .ops = &pxa25x_ep_ops,
1782 .maxpacket = INT_FIFO_SIZE,
1785 .fifo_size = INT_FIFO_SIZE,
1786 .bEndpointAddress = USB_DIR_IN | 10,
1787 .bmAttributes = USB_ENDPOINT_XFER_INT,
1788 .reg_udccs = &UDC_REGS->udccs[10],
1789 .reg_uddr = &UDC_REGS->uddr10,
1792 /* third group of endpoints */
1795 .name = "ep11in-bulk",
1796 .ops = &pxa25x_ep_ops,
1797 .maxpacket = BULK_FIFO_SIZE,
1800 .fifo_size = BULK_FIFO_SIZE,
1801 .bEndpointAddress = USB_DIR_IN | 11,
1802 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1803 .reg_udccs = &UDC_REGS->udccs[11],
1804 .reg_uddr = &UDC_REGS->uddr11,
1808 .name = "ep12out-bulk",
1809 .ops = &pxa25x_ep_ops,
1810 .maxpacket = BULK_FIFO_SIZE,
1813 .fifo_size = BULK_FIFO_SIZE,
1814 .bEndpointAddress = 12,
1815 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1816 .reg_udccs = &UDC_REGS->udccs[12],
1817 .reg_ubcr = &UDC_REGS->ubcr12,
1818 .reg_uddr = &UDC_REGS->uddr12,
1822 .name = "ep13in-iso",
1823 .ops = &pxa25x_ep_ops,
1824 .maxpacket = ISO_FIFO_SIZE,
1827 .fifo_size = ISO_FIFO_SIZE,
1828 .bEndpointAddress = USB_DIR_IN | 13,
1829 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1830 .reg_udccs = &UDC_REGS->udccs[13],
1831 .reg_uddr = &UDC_REGS->uddr13,
1835 .name = "ep14out-iso",
1836 .ops = &pxa25x_ep_ops,
1837 .maxpacket = ISO_FIFO_SIZE,
1840 .fifo_size = ISO_FIFO_SIZE,
1841 .bEndpointAddress = 14,
1842 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1843 .reg_udccs = &UDC_REGS->udccs[14],
1844 .reg_ubcr = &UDC_REGS->ubcr14,
1845 .reg_uddr = &UDC_REGS->uddr14,
1849 .name = "ep15in-int",
1850 .ops = &pxa25x_ep_ops,
1851 .maxpacket = INT_FIFO_SIZE,
1854 .fifo_size = INT_FIFO_SIZE,
1855 .bEndpointAddress = USB_DIR_IN | 15,
1856 .bmAttributes = USB_ENDPOINT_XFER_INT,
1857 .reg_udccs = &UDC_REGS->udccs[15],
1858 .reg_uddr = &UDC_REGS->uddr15,
1860 #endif /* !CONFIG_USB_PXA25X_SMALL */
1863 static void udc_command(int cmd)
1866 case PXA2XX_UDC_CMD_CONNECT:
1867 setbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1868 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1871 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1872 GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
1874 debug("Connected to USB\n");
1877 case PXA2XX_UDC_CMD_DISCONNECT:
1878 /* disable pullup resistor */
1879 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1880 GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
1882 /* setup pin as input, line will float */
1883 clrbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1884 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1886 debug("Disconnected from USB\n");
1891 static struct pxa2xx_udc_mach_info mach_info = {
1892 .udc_command = udc_command,
1896 * when a driver is successfully registered, it will receive
1897 * control requests including set_configuration(), which enables
1898 * non-control requests. then usb traffic follows until a
1899 * disconnect is reported. then a host may connect again, or
1900 * the driver might get unbound.
1902 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1904 struct pxa25x_udc *dev = &memory;
1909 || driver->speed < USB_SPEED_FULL
1910 || !driver->disconnect
1918 /* Enable clock for usb controller */
1919 setbits_le32(CKEN, CKEN11_USB);
1921 /* first hook up the driver ... */
1922 dev->driver = driver;
1925 /* trigger chiprev-specific logic */
1926 switch ((chiprev = pxa_get_cpu_revision())) {
1932 /* A0/A1 "not released"; ep 13, 15 unusable */
1934 case PXA250_B2: case PXA210_B2:
1935 case PXA250_B1: case PXA210_B1:
1936 case PXA250_B0: case PXA210_B0:
1937 /* OUT-DMA is broken ... */
1939 case PXA250_C0: case PXA210_C0:
1942 printf("%s: unrecognized processor: %08x\n",
1943 DRIVER_NAME, chiprev);
1947 the_controller = dev;
1949 /* prepare watchdog timer */
1950 dev->watchdog.running = 0;
1951 dev->watchdog.period = 5000 * CONFIG_SYS_HZ / 1000000; /* 5 ms */
1952 dev->watchdog.function = udc_watchdog;
1954 dev->mach = &mach_info;
1959 dev->gadget.name = "pxa2xx_udc";
1960 retval = driver->bind(&dev->gadget);
1962 printf("bind to driver %s --> error %d\n",
1963 DRIVER_NAME, retval);
1969 * ... then enable host detection and ep0; and we're ready
1970 * for set_configuration as well as eventual disconnect.
1972 printf("registered gadget driver '%s'\n", DRIVER_NAME);
1980 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1984 /* don't disconnect drivers more than once */
1985 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1987 dev->gadget.speed = USB_SPEED_UNKNOWN;
1989 /* prevent new request submissions, kill any outstanding requests */
1990 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1991 struct pxa25x_ep *ep = &dev->ep[i];
1994 nuke(ep, -ESHUTDOWN);
1998 /* report disconnect; the driver is already quiesced */
2000 driver->disconnect(&dev->gadget);
2002 /* re-init driver-visible data structures */
2006 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2008 struct pxa25x_udc *dev = the_controller;
2012 if (!driver || driver != dev->driver || !driver->unbind)
2015 local_irq_disable();
2018 stop_activity(dev, driver);
2021 driver->unbind(&dev->gadget);
2024 printf("unregistered gadget driver '%s'\n", DRIVER_NAME);
2027 the_controller = NULL;
2029 clrbits_le32(CKEN, CKEN11_USB);
2034 extern void udc_disconnect(void)
2036 setbits_le32(CKEN, CKEN11_USB);
2037 udc_clear_mask_UDCCR(UDCCR_UDE);
2038 udc_command(PXA2XX_UDC_CMD_DISCONNECT);
2039 clrbits_le32(CKEN, CKEN11_USB);
2042 /*-------------------------------------------------------------------------*/
2045 usb_gadget_handle_interrupts(int index)
2047 return pxa25x_udc_irq();