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 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell");
28 #define CONFIG_USB_PXA25X_SMALL
29 #define DRIVER_NAME "pxa25x_udc_linux"
30 #define ARCH_HAS_PREFETCH
34 #include <asm/byteorder.h>
35 #include <asm/system.h>
36 #include <asm/mach-types.h>
37 #include <asm/unaligned.h>
38 #include <linux/compat.h>
41 #include <asm/arch/pxa.h>
43 #include <linux/usb/ch9.h>
44 #include <linux/usb/gadget.h>
45 #include <usb/lin_gadget_compat.h>
46 #include <asm/arch/pxa-regs.h>
48 #include "pxa25x_udc.h"
51 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x
52 * series processors. The UDC for the IXP 4xx series is very similar.
53 * There are fifteen endpoints, in addition to ep0.
55 * Such controller drivers work with a gadget driver. The gadget driver
56 * returns descriptors, implements configuration and data protocols used
57 * by the host to interact with this device, and allocates endpoints to
58 * the different protocol interfaces. The controller driver virtualizes
59 * usb hardware so that the gadget drivers will be more portable.
61 * This UDC hardware wants to implement a bit too much USB protocol, so
62 * it constrains the sorts of USB configuration change events that work.
63 * The errata for these chips are misleading; some "fixed" bugs from
64 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there.
66 * Note that the UDC hardware supports DMA (except on IXP) but that's
67 * not used here. IN-DMA (to host) is simple enough, when the data is
68 * suitably aligned (16 bytes) ... the network stack doesn't do that,
69 * other software can. OUT-DMA is buggy in most chip versions, as well
70 * as poorly designed (data toggle not automatic). So this driver won't
71 * bother using DMA. (Mostly-working IN-DMA support was available in
72 * kernels before 2.6.23, but was never enabled or well tested.)
75 #define DRIVER_VERSION "18-August-2012"
76 #define DRIVER_DESC "PXA 25x USB Device Controller driver"
78 static const char driver_name[] = "pxa25x_udc";
79 static const char ep0name[] = "ep0";
82 static inline void start_watchdog(struct pxa25x_udc *udc)
84 debug("Started watchdog\n");
85 udc->watchdog.base = get_timer(0);
86 udc->watchdog.running = 1;
89 static inline void stop_watchdog(struct pxa25x_udc *udc)
91 udc->watchdog.running = 0;
92 debug("Stopped watchdog\n");
95 static inline void test_watchdog(struct pxa25x_udc *udc)
97 if (!udc->watchdog.running)
100 debug("watchdog %ld %ld\n", get_timer(udc->watchdog.base),
101 udc->watchdog.period);
103 if (get_timer(udc->watchdog.base) >= udc->watchdog.period) {
105 udc->watchdog.function(udc);
109 static void udc_watchdog(struct pxa25x_udc *dev)
111 uint32_t udccs0 = readl(&dev->regs->udccs[0]);
113 debug("Fired up udc_watchdog\n");
116 if (dev->ep0state == EP0_STALL
117 && (udccs0 & UDCCS0_FST) == 0
118 && (udccs0 & UDCCS0_SST) == 0) {
119 writel(UDCCS0_FST|UDCCS0_FTF, &dev->regs->udccs[0]);
120 debug("ep0 re-stall\n");
128 static const char * const state_name[] = {
130 "EP0_IN_DATA_PHASE", "EP0_OUT_DATA_PHASE",
131 "EP0_END_XFER", "EP0_STALL"
135 dump_udccr(const char *label)
137 u32 udccr = readl(&UDC_REGS->udccr);
138 debug("%s %02X =%s%s%s%s%s%s%s%s\n",
140 (udccr & UDCCR_REM) ? " rem" : "",
141 (udccr & UDCCR_RSTIR) ? " rstir" : "",
142 (udccr & UDCCR_SRM) ? " srm" : "",
143 (udccr & UDCCR_SUSIR) ? " susir" : "",
144 (udccr & UDCCR_RESIR) ? " resir" : "",
145 (udccr & UDCCR_RSM) ? " rsm" : "",
146 (udccr & UDCCR_UDA) ? " uda" : "",
147 (udccr & UDCCR_UDE) ? " ude" : "");
151 dump_udccs0(const char *label)
153 u32 udccs0 = readl(&UDC_REGS->udccs[0]);
155 debug("%s %s %02X =%s%s%s%s%s%s%s%s\n",
156 label, state_name[the_controller->ep0state], udccs0,
157 (udccs0 & UDCCS0_SA) ? " sa" : "",
158 (udccs0 & UDCCS0_RNE) ? " rne" : "",
159 (udccs0 & UDCCS0_FST) ? " fst" : "",
160 (udccs0 & UDCCS0_SST) ? " sst" : "",
161 (udccs0 & UDCCS0_DRWF) ? " dwrf" : "",
162 (udccs0 & UDCCS0_FTF) ? " ftf" : "",
163 (udccs0 & UDCCS0_IPR) ? " ipr" : "",
164 (udccs0 & UDCCS0_OPR) ? " opr" : "");
168 dump_state(struct pxa25x_udc *dev)
173 debug("%s, uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n",
174 state_name[dev->ep0state],
175 readl(&UDC_REGS->uicr1), readl(&UDC_REGS->uicr0),
176 readl(&UDC_REGS->usir1), readl(&UDC_REGS->usir0),
177 readl(&UDC_REGS->ufnrh), readl(&UDC_REGS->ufnrl));
180 tmp = readl(&UDC_REGS->udccfr);
181 debug("udccfr %02X =%s%s\n", tmp,
182 (tmp & UDCCFR_AREN) ? " aren" : "",
183 (tmp & UDCCFR_ACM) ? " acm" : "");
187 debug("no gadget driver bound\n");
190 debug("ep0 driver '%s'\n", "ether");
192 dump_udccs0("udccs0");
193 debug("ep0 IN %lu/%lu, OUT %lu/%lu\n",
194 dev->stats.write.bytes, dev->stats.write.ops,
195 dev->stats.read.bytes, dev->stats.read.ops);
197 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) {
198 if (dev->ep[i].desc == NULL)
200 debug("udccs%d = %02x\n", i, *dev->ep->reg_udccs);
206 static inline void dump_udccr(const char *label) { }
207 static inline void dump_udccs0(const char *label) { }
208 static inline void dump_state(struct pxa25x_udc *dev) { }
213 * ---------------------------------------------------------------------------
214 * endpoint related parts of the api to the usb controller hardware,
215 * used by gadget driver; and the inner talker-to-hardware core.
216 * ---------------------------------------------------------------------------
219 static void pxa25x_ep_fifo_flush(struct usb_ep *ep);
220 static void nuke(struct pxa25x_ep *, int status);
222 /* one GPIO should control a D+ pullup, so host sees this device (or not) */
223 static void pullup_off(void)
225 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
227 if (mach->udc_command)
228 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT);
231 static void pullup_on(void)
233 struct pxa2xx_udc_mach_info *mach = the_controller->mach;
235 if (mach->udc_command)
236 mach->udc_command(PXA2XX_UDC_CMD_CONNECT);
239 static void pio_irq_enable(int bEndpointAddress)
241 bEndpointAddress &= 0xf;
242 if (bEndpointAddress < 8) {
243 clrbits_le32(&the_controller->regs->uicr0,
244 1 << bEndpointAddress);
246 bEndpointAddress -= 8;
247 clrbits_le32(&the_controller->regs->uicr1,
248 1 << bEndpointAddress);
252 static void pio_irq_disable(int bEndpointAddress)
254 bEndpointAddress &= 0xf;
255 if (bEndpointAddress < 8) {
256 setbits_le32(&the_controller->regs->uicr0,
257 1 << bEndpointAddress);
259 bEndpointAddress -= 8;
260 setbits_le32(&the_controller->regs->uicr1,
261 1 << bEndpointAddress);
265 static inline void udc_set_mask_UDCCR(int mask)
268 * The UDCCR reg contains mask and interrupt status bits,
269 * so using '|=' isn't safe as it may ack an interrupt.
271 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
274 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
277 static inline void udc_clear_mask_UDCCR(int mask)
279 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
281 mask = ~mask & mask_bits;
282 clrbits_le32(&the_controller->regs->udccr, ~mask);
285 static inline void udc_ack_int_UDCCR(int mask)
287 const uint32_t mask_bits = UDCCR_REM | UDCCR_SRM | UDCCR_UDE;
290 clrsetbits_le32(&the_controller->regs->udccr, ~mask_bits, mask);
294 * endpoint enable/disable
296 * we need to verify the descriptors used to enable endpoints. since pxa25x
297 * endpoint configurations are fixed, and are pretty much always enabled,
298 * there's not a lot to manage here.
300 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints,
301 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except
302 * for a single interface (with only the default altsetting) and for gadget
303 * drivers that don't halt endpoints (not reset by set_interface). that also
304 * means that if you use ISO, you must violate the USB spec rule that all
305 * iso endpoints must be in non-default altsettings.
307 static int pxa25x_ep_enable(struct usb_ep *_ep,
308 const struct usb_endpoint_descriptor *desc)
310 struct pxa25x_ep *ep;
311 struct pxa25x_udc *dev;
313 ep = container_of(_ep, struct pxa25x_ep, ep);
314 if (!_ep || !desc || ep->desc || _ep->name == ep0name
315 || desc->bDescriptorType != USB_DT_ENDPOINT
316 || ep->bEndpointAddress != desc->bEndpointAddress
318 le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))) {
319 printf("%s, bad ep or descriptor\n", __func__);
323 /* xfer types must match, except that interrupt ~= bulk */
324 if (ep->bmAttributes != desc->bmAttributes
325 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
326 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
327 printf("%s, %s type mismatch\n", __func__, _ep->name);
331 /* hardware _could_ do smaller, but driver doesn't */
332 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
333 && le16_to_cpu(get_unaligned(&desc->wMaxPacketSize))
335 || !get_unaligned(&desc->wMaxPacketSize)) {
336 printf("%s, bad %s maxpacket\n", __func__, _ep->name);
341 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
342 printf("%s, bogus device state\n", __func__);
349 ep->ep.maxpacket = le16_to_cpu(get_unaligned(&desc->wMaxPacketSize));
351 /* flush fifo (mostly for OUT buffers) */
352 pxa25x_ep_fifo_flush(_ep);
354 /* ... reset halt state too, if we could ... */
356 debug("enabled %s\n", _ep->name);
360 static int pxa25x_ep_disable(struct usb_ep *_ep)
362 struct pxa25x_ep *ep;
365 ep = container_of(_ep, struct pxa25x_ep, ep);
366 if (!_ep || !ep->desc) {
367 printf("%s, %s not enabled\n", __func__,
368 _ep ? ep->ep.name : NULL);
371 local_irq_save(flags);
373 nuke(ep, -ESHUTDOWN);
375 /* flush fifo (mostly for IN buffers) */
376 pxa25x_ep_fifo_flush(_ep);
381 local_irq_restore(flags);
382 debug("%s disabled\n", _ep->name);
386 /*-------------------------------------------------------------------------*/
389 * for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers
390 * must still pass correctly initialized endpoints, since other controller
391 * drivers may care about how it's currently set up (dma issues etc).
395 * pxa25x_ep_alloc_request - allocate a request data structure
397 static struct usb_request *
398 pxa25x_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
400 struct pxa25x_request *req;
402 req = kzalloc(sizeof(*req), gfp_flags);
406 INIT_LIST_HEAD(&req->queue);
412 * pxa25x_ep_free_request - deallocate a request data structure
415 pxa25x_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
417 struct pxa25x_request *req;
419 req = container_of(_req, struct pxa25x_request, req);
420 WARN_ON(!list_empty(&req->queue));
424 /*-------------------------------------------------------------------------*/
427 * done - retire a request; caller blocked irqs
429 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status)
431 unsigned stopped = ep->stopped;
433 list_del_init(&req->queue);
435 if (likely(req->req.status == -EINPROGRESS))
436 req->req.status = status;
438 status = req->req.status;
440 if (status && status != -ESHUTDOWN)
441 debug("complete %s req %p stat %d len %u/%u\n",
442 ep->ep.name, &req->req, status,
443 req->req.actual, req->req.length);
445 /* don't modify queue heads during completion callback */
447 req->req.complete(&ep->ep, &req->req);
448 ep->stopped = stopped;
452 static inline void ep0_idle(struct pxa25x_udc *dev)
454 dev->ep0state = EP0_IDLE;
458 write_packet(u32 *uddr, struct pxa25x_request *req, unsigned max)
461 unsigned length, count;
463 debug("%s(): uddr %p\n", __func__, uddr);
465 buf = req->req.buf + req->req.actual;
468 /* how big will this packet be? */
469 length = min(req->req.length - req->req.actual, max);
470 req->req.actual += length;
473 while (likely(count--))
474 writeb(*buf++, uddr);
480 * write to an IN endpoint fifo, as many packets as possible.
481 * irqs will use this to write the rest later.
482 * caller guarantees at least one packet buffer is ready (or a zlp).
485 write_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
489 max = le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize));
492 int is_last, is_short;
494 count = write_packet(ep->reg_uddr, req, max);
496 /* last packet is usually short (or a zlp) */
497 if (unlikely(count != max))
498 is_last = is_short = 1;
500 if (likely(req->req.length != req->req.actual)
505 /* interrupt/iso maxpacket may not fill the fifo */
506 is_short = unlikely(max < ep->fifo_size);
509 debug_cond(NOISY, "wrote %s %d bytes%s%s %d left %p\n",
511 is_last ? "/L" : "", is_short ? "/S" : "",
512 req->req.length - req->req.actual, req);
515 * let loose that packet. maybe try writing another one,
516 * double buffering might work. TSP, TPC, and TFS
517 * bit values are the same for all normal IN endpoints.
519 writel(UDCCS_BI_TPC, ep->reg_udccs);
521 writel(UDCCS_BI_TSP, ep->reg_udccs);
523 /* requests complete when all IN data is in the FIFO */
526 if (list_empty(&ep->queue))
527 pio_irq_disable(ep->bEndpointAddress);
532 * TODO experiment: how robust can fifo mode tweaking be?
533 * double buffering is off in the default fifo mode, which
534 * prevents TFS from being set here.
537 } while (readl(ep->reg_udccs) & UDCCS_BI_TFS);
542 * caller asserts req->pending (ep0 irq status nyet cleared); starts
543 * ep0 data stage. these chips want very simple state transitions.
546 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag)
548 writel(flags|UDCCS0_SA|UDCCS0_OPR, &dev->regs->udccs[0]);
549 writel(USIR0_IR0, &dev->regs->usir0);
550 dev->req_pending = 0;
551 debug_cond(NOISY, "%s() %s, udccs0: %02x/%02x usir: %X.%X\n",
552 __func__, tag, readl(&dev->regs->udccs[0]), flags,
553 readl(&dev->regs->usir1), readl(&dev->regs->usir0));
557 write_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
562 count = write_packet(&ep->dev->regs->uddr0, req, EP0_FIFO_SIZE);
563 ep->dev->stats.write.bytes += count;
565 /* last packet "must be" short (or a zlp) */
566 is_short = (count != EP0_FIFO_SIZE);
568 debug_cond(NOISY, "ep0in %d bytes %d left %p\n", count,
569 req->req.length - req->req.actual, req);
571 if (unlikely(is_short)) {
572 if (ep->dev->req_pending)
573 ep0start(ep->dev, UDCCS0_IPR, "short IN");
575 writel(UDCCS0_IPR, &ep->dev->regs->udccs[0]);
577 count = req->req.length;
582 * This seems to get rid of lost status irqs in some cases:
583 * host responds quickly, or next request involves config
584 * change automagic, or should have been hidden, or ...
586 * FIXME get rid of all udelays possible...
588 if (count >= EP0_FIFO_SIZE) {
591 if ((readl(&ep->dev->regs->udccs[0]) &
593 /* clear OPR, generate ack */
595 &ep->dev->regs->udccs[0]);
602 } else if (ep->dev->req_pending)
603 ep0start(ep->dev, 0, "IN");
610 * read_fifo - unload packet(s) from the fifo we use for usb OUT
611 * transfers and put them into the request. caller should have made
612 * sure there's at least one packet ready.
614 * returns true if the request completed because of short packet or the
615 * request buffer having filled (and maybe overran till end-of-packet).
618 read_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
622 unsigned bufferspace, count, is_short;
626 * make sure there's a packet in the FIFO.
627 * UDCCS_{BO,IO}_RPC are all the same bit value.
628 * UDCCS_{BO,IO}_RNE are all the same bit value.
630 udccs = readl(ep->reg_udccs);
631 if (unlikely((udccs & UDCCS_BO_RPC) == 0))
633 buf = req->req.buf + req->req.actual;
635 bufferspace = req->req.length - req->req.actual;
637 /* read all bytes from this packet */
638 if (likely(udccs & UDCCS_BO_RNE)) {
639 count = 1 + (0x0ff & readl(ep->reg_ubcr));
640 req->req.actual += min(count, bufferspace);
643 is_short = (count < ep->ep.maxpacket);
644 debug_cond(NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n",
645 ep->ep.name, udccs, count,
646 is_short ? "/S" : "",
647 req, req->req.actual, req->req.length);
648 while (likely(count-- != 0)) {
649 u8 byte = readb(ep->reg_uddr);
651 if (unlikely(bufferspace == 0)) {
653 * this happens when the driver's buffer
654 * is smaller than what the host sent.
655 * discard the extra data.
657 if (req->req.status != -EOVERFLOW)
658 printf("%s overflow %d\n",
660 req->req.status = -EOVERFLOW;
666 writel(UDCCS_BO_RPC, ep->reg_udccs);
667 /* RPC/RSP/RNE could now reflect the other packet buffer */
669 /* iso is one request per packet */
670 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
671 if (udccs & UDCCS_IO_ROF)
672 req->req.status = -EHOSTUNREACH;
673 /* more like "is_done" */
678 if (is_short || req->req.actual == req->req.length) {
680 if (list_empty(&ep->queue))
681 pio_irq_disable(ep->bEndpointAddress);
685 /* finished that packet. the next one may be waiting... */
691 * special ep0 version of the above. no UBCR0 or double buffering; status
692 * handshaking is magic. most device protocols don't need control-OUT.
693 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other
694 * protocols do use them.
697 read_ep0_fifo(struct pxa25x_ep *ep, struct pxa25x_request *req)
700 unsigned bufferspace;
702 buf = req->req.buf + req->req.actual;
703 bufferspace = req->req.length - req->req.actual;
705 while (readl(&ep->dev->regs->udccs[0]) & UDCCS0_RNE) {
706 byte = (u8)readb(&ep->dev->regs->uddr0);
708 if (unlikely(bufferspace == 0)) {
710 * this happens when the driver's buffer
711 * is smaller than what the host sent.
712 * discard the extra data.
714 if (req->req.status != -EOVERFLOW)
715 printf("%s overflow\n", ep->ep.name);
716 req->req.status = -EOVERFLOW;
724 writel(UDCCS0_OPR | UDCCS0_IPR, &ep->dev->regs->udccs[0]);
727 if (req->req.actual >= req->req.length)
730 /* finished that packet. the next one may be waiting... */
734 /*-------------------------------------------------------------------------*/
737 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
739 struct pxa25x_request *req;
740 struct pxa25x_ep *ep;
741 struct pxa25x_udc *dev;
744 req = container_of(_req, struct pxa25x_request, req);
745 if (unlikely(!_req || !_req->complete || !_req->buf
746 || !list_empty(&req->queue))) {
747 printf("%s, bad params\n", __func__);
751 ep = container_of(_ep, struct pxa25x_ep, ep);
752 if (unlikely(!_ep || (!ep->desc && ep->ep.name != ep0name))) {
753 printf("%s, bad ep\n", __func__);
758 if (unlikely(!dev->driver
759 || dev->gadget.speed == USB_SPEED_UNKNOWN)) {
760 printf("%s, bogus device state\n", __func__);
765 * iso is always one packet per request, that's the only way
766 * we can report per-packet status. that also helps with dma.
768 if (unlikely(ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
770 le16_to_cpu(get_unaligned(&ep->desc->wMaxPacketSize))))
773 debug_cond(NOISY, "%s queue req %p, len %d buf %p\n",
774 _ep->name, _req, _req->length, _req->buf);
776 local_irq_save(flags);
778 _req->status = -EINPROGRESS;
781 /* kickstart this i/o queue? */
782 if (list_empty(&ep->queue) && !ep->stopped) {
783 if (ep->desc == NULL/* ep0 */) {
784 unsigned length = _req->length;
786 switch (dev->ep0state) {
787 case EP0_IN_DATA_PHASE:
788 dev->stats.write.ops++;
789 if (write_ep0_fifo(ep, req))
793 case EP0_OUT_DATA_PHASE:
794 dev->stats.read.ops++;
796 if (dev->req_config) {
797 debug("ep0 config ack%s\n",
798 dev->has_cfr ? "" : " raced");
800 writel(UDCCFR_AREN|UDCCFR_ACM
802 &ep->dev->regs->udccfr);
804 dev->ep0state = EP0_END_XFER;
805 local_irq_restore(flags);
808 if (dev->req_pending)
809 ep0start(dev, UDCCS0_IPR, "OUT");
812 &ep->dev->regs->udccs[0])
814 && read_ep0_fifo(ep, req))) {
822 printf("ep0 i/o, odd state %d\n",
824 local_irq_restore(flags);
827 /* can the FIFO can satisfy the request immediately? */
828 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) {
829 if ((readl(ep->reg_udccs) & UDCCS_BI_TFS) != 0
830 && write_fifo(ep, req))
832 } else if ((readl(ep->reg_udccs) & UDCCS_BO_RFS) != 0
833 && read_fifo(ep, req)) {
837 if (likely(req && ep->desc))
838 pio_irq_enable(ep->bEndpointAddress);
841 /* pio or dma irq handler advances the queue. */
842 if (likely(req != NULL))
843 list_add_tail(&req->queue, &ep->queue);
844 local_irq_restore(flags);
851 * nuke - dequeue ALL requests
853 static void nuke(struct pxa25x_ep *ep, int status)
855 struct pxa25x_request *req;
857 /* called with irqs blocked */
858 while (!list_empty(&ep->queue)) {
859 req = list_entry(ep->queue.next,
860 struct pxa25x_request,
862 done(ep, req, status);
865 pio_irq_disable(ep->bEndpointAddress);
869 /* dequeue JUST ONE request */
870 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
872 struct pxa25x_ep *ep;
873 struct pxa25x_request *req;
876 ep = container_of(_ep, struct pxa25x_ep, ep);
877 if (!_ep || ep->ep.name == ep0name)
880 local_irq_save(flags);
882 /* make sure it's actually queued on this endpoint */
883 list_for_each_entry(req, &ep->queue, queue) {
884 if (&req->req == _req)
887 if (&req->req != _req) {
888 local_irq_restore(flags);
892 done(ep, req, -ECONNRESET);
894 local_irq_restore(flags);
898 /*-------------------------------------------------------------------------*/
900 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value)
902 struct pxa25x_ep *ep;
905 ep = container_of(_ep, struct pxa25x_ep, ep);
907 || (!ep->desc && ep->ep.name != ep0name))
908 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
909 printf("%s, bad ep\n", __func__);
914 * this path (reset toggle+halt) is needed to implement
915 * SET_INTERFACE on normal hardware. but it can't be
916 * done from software on the PXA UDC, and the hardware
917 * forgets to do it as part of SET_INTERFACE automagic.
919 printf("only host can clear %s halt\n", _ep->name);
923 local_irq_save(flags);
925 if ((ep->bEndpointAddress & USB_DIR_IN) != 0
926 && ((readl(ep->reg_udccs) & UDCCS_BI_TFS) == 0
927 || !list_empty(&ep->queue))) {
928 local_irq_restore(flags);
932 /* FST bit is the same for control, bulk in, bulk out, interrupt in */
933 writel(UDCCS_BI_FST|UDCCS_BI_FTF, ep->reg_udccs);
935 /* ep0 needs special care */
937 start_watchdog(ep->dev);
938 ep->dev->req_pending = 0;
939 ep->dev->ep0state = EP0_STALL;
941 /* and bulk/intr endpoints like dropping stalls too */
944 for (i = 0; i < 1000; i += 20) {
945 if (readl(ep->reg_udccs) & UDCCS_BI_SST)
950 local_irq_restore(flags);
952 debug("%s halt\n", _ep->name);
956 static int pxa25x_ep_fifo_status(struct usb_ep *_ep)
958 struct pxa25x_ep *ep;
960 ep = container_of(_ep, struct pxa25x_ep, ep);
962 printf("%s, bad ep\n", __func__);
965 /* pxa can't report unclaimed bytes from IN fifos */
966 if ((ep->bEndpointAddress & USB_DIR_IN) != 0)
968 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN
969 || (readl(ep->reg_udccs) & UDCCS_BO_RFS) == 0)
972 return (readl(ep->reg_ubcr) & 0xfff) + 1;
975 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep)
977 struct pxa25x_ep *ep;
979 ep = container_of(_ep, struct pxa25x_ep, ep);
980 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) {
981 printf("%s, bad ep\n", __func__);
985 /* toggle and halt bits stay unchanged */
987 /* for OUT, just read and discard the FIFO contents. */
988 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) {
989 while (((readl(ep->reg_udccs)) & UDCCS_BO_RNE) != 0)
990 (void)readb(ep->reg_uddr);
994 /* most IN status is the same, but ISO can't stall */
995 writel(UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR
996 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
997 ? 0 : UDCCS_BI_SST), ep->reg_udccs);
1001 static struct usb_ep_ops pxa25x_ep_ops = {
1002 .enable = pxa25x_ep_enable,
1003 .disable = pxa25x_ep_disable,
1005 .alloc_request = pxa25x_ep_alloc_request,
1006 .free_request = pxa25x_ep_free_request,
1008 .queue = pxa25x_ep_queue,
1009 .dequeue = pxa25x_ep_dequeue,
1011 .set_halt = pxa25x_ep_set_halt,
1012 .fifo_status = pxa25x_ep_fifo_status,
1013 .fifo_flush = pxa25x_ep_fifo_flush,
1017 /* ---------------------------------------------------------------------------
1018 * device-scoped parts of the api to the usb controller hardware
1019 * ---------------------------------------------------------------------------
1022 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget)
1024 return ((readl(&the_controller->regs->ufnrh) & 0x07) << 8) |
1025 (readl(&the_controller->regs->ufnrl) & 0xff);
1028 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget)
1030 /* host may not have enabled remote wakeup */
1031 if ((readl(&the_controller->regs->udccs[0]) & UDCCS0_DRWF) == 0)
1032 return -EHOSTUNREACH;
1033 udc_set_mask_UDCCR(UDCCR_RSM);
1037 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *);
1038 static void udc_enable(struct pxa25x_udc *);
1039 static void udc_disable(struct pxa25x_udc *);
1042 * We disable the UDC -- and its 48 MHz clock -- whenever it's not
1045 static int pullup(struct pxa25x_udc *udc)
1053 int is_active = udc->pullup;
1061 if (udc->gadget.speed != USB_SPEED_UNKNOWN)
1062 stop_activity(udc, udc->driver);
1071 /* VBUS reporting logically comes from a transceiver */
1072 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active)
1074 struct pxa25x_udc *udc;
1076 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1077 printf("vbus %s\n", is_active ? "supplied" : "inactive");
1082 /* drivers may have software control over D+ pullup */
1083 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active)
1085 struct pxa25x_udc *udc;
1087 udc = container_of(_gadget, struct pxa25x_udc, gadget);
1089 /* not all boards support pullup control */
1090 if (!udc->mach->udc_command)
1093 udc->pullup = (is_active != 0);
1099 * boards may consume current from VBUS, up to 100-500mA based on config.
1100 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs
1101 * violate USB specs.
1103 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA)
1108 static const struct usb_gadget_ops pxa25x_udc_ops = {
1109 .get_frame = pxa25x_udc_get_frame,
1110 .wakeup = pxa25x_udc_wakeup,
1111 .vbus_session = pxa25x_udc_vbus_session,
1112 .pullup = pxa25x_udc_pullup,
1113 .vbus_draw = pxa25x_udc_vbus_draw,
1116 /*-------------------------------------------------------------------------*/
1119 * udc_disable - disable USB device controller
1121 static void udc_disable(struct pxa25x_udc *dev)
1123 /* block all irqs */
1124 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM);
1125 writel(0xff, &dev->regs->uicr0);
1126 writel(0xff, &dev->regs->uicr1);
1127 writel(UFNRH_SIM, &dev->regs->ufnrh);
1129 /* if hardware supports it, disconnect from usb */
1132 udc_clear_mask_UDCCR(UDCCR_UDE);
1135 dev->gadget.speed = USB_SPEED_UNKNOWN;
1139 * udc_reinit - initialize software state
1141 static void udc_reinit(struct pxa25x_udc *dev)
1145 /* device/ep0 records init */
1146 INIT_LIST_HEAD(&dev->gadget.ep_list);
1147 INIT_LIST_HEAD(&dev->gadget.ep0->ep_list);
1148 dev->ep0state = EP0_IDLE;
1150 /* basic endpoint records init */
1151 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
1152 struct pxa25x_ep *ep = &dev->ep[i];
1155 list_add_tail(&ep->ep.ep_list, &dev->gadget.ep_list);
1159 INIT_LIST_HEAD(&ep->queue);
1163 /* the rest was statically initialized, and is read-only */
1167 * until it's enabled, this UDC should be completely invisible
1170 static void udc_enable(struct pxa25x_udc *dev)
1172 debug("udc: enabling udc\n");
1174 udc_clear_mask_UDCCR(UDCCR_UDE);
1177 * Try to clear these bits before we enable the udc.
1178 * Do not touch reset ack bit, we would take care of it in
1179 * interrupt handle routine
1181 udc_ack_int_UDCCR(UDCCR_SUSIR|UDCCR_RESIR);
1184 dev->gadget.speed = USB_SPEED_UNKNOWN;
1185 dev->stats.irqs = 0;
1188 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual:
1190 * - if RESET is already in progress, ack interrupt
1191 * - unmask reset interrupt
1193 udc_set_mask_UDCCR(UDCCR_UDE);
1194 if (!(readl(&dev->regs->udccr) & UDCCR_UDA))
1195 udc_ack_int_UDCCR(UDCCR_RSTIR);
1197 if (dev->has_cfr /* UDC_RES2 is defined */) {
1199 * pxa255 (a0+) can avoid a set_config race that could
1200 * prevent gadget drivers from configuring correctly
1202 writel(UDCCFR_ACM | UDCCFR_MB1, &dev->regs->udccfr);
1205 /* enable suspend/resume and reset irqs */
1206 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM);
1208 /* enable ep0 irqs */
1209 clrbits_le32(&dev->regs->uicr0, UICR0_IM0);
1211 /* if hardware supports it, pullup D+ and wait for reset */
1215 static inline void clear_ep_state(struct pxa25x_udc *dev)
1220 * hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint
1221 * fifos, and pending transactions mustn't be continued in any case.
1223 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++)
1224 nuke(&dev->ep[i], -ECONNABORTED);
1227 static void handle_ep0(struct pxa25x_udc *dev)
1229 u32 udccs0 = readl(&dev->regs->udccs[0]);
1230 struct pxa25x_ep *ep = &dev->ep[0];
1231 struct pxa25x_request *req;
1233 struct usb_ctrlrequest r;
1238 if (list_empty(&ep->queue))
1241 req = list_entry(ep->queue.next, struct pxa25x_request, queue);
1243 /* clear stall status */
1244 if (udccs0 & UDCCS0_SST) {
1246 writel(UDCCS0_SST, &dev->regs->udccs[0]);
1251 /* previous request unfinished? non-error iff back-to-back ... */
1252 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) {
1258 switch (dev->ep0state) {
1260 /* late-breaking status? */
1261 udccs0 = readl(&dev->regs->udccs[0]);
1263 /* start control request? */
1264 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))
1265 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) {
1270 /* read SETUP packet */
1271 for (i = 0; i < 8; i++) {
1272 if (unlikely(!(readl(&dev->regs->udccs[0]) &
1275 debug("SETUP %d!\n", i);
1278 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1280 if (unlikely((readl(&dev->regs->udccs[0]) &
1285 debug("SETUP %02x.%02x v%04x i%04x l%04x\n",
1286 u.r.bRequestType, u.r.bRequest,
1287 le16_to_cpu(u.r.wValue),
1288 le16_to_cpu(u.r.wIndex),
1289 le16_to_cpu(u.r.wLength));
1291 /* cope with automagic for some standard requests. */
1292 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK)
1293 == USB_TYPE_STANDARD;
1294 dev->req_config = 0;
1295 dev->req_pending = 1;
1296 switch (u.r.bRequest) {
1297 /* hardware restricts gadget drivers here! */
1298 case USB_REQ_SET_CONFIGURATION:
1299 debug("GOT SET_CONFIGURATION\n");
1300 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1302 * reflect hardware's automagic
1303 * up to the gadget driver.
1306 dev->req_config = 1;
1307 clear_ep_state(dev);
1309 * if !has_cfr, there's no synch
1310 * else use AREN (later) not SA|OPR
1311 * USIR0_IR0 acts edge sensitive
1315 /* ... and here, even more ... */
1316 case USB_REQ_SET_INTERFACE:
1317 if (u.r.bRequestType == USB_RECIP_INTERFACE) {
1319 * udc hardware is broken by design:
1320 * - altsetting may only be zero;
1321 * - hw resets all interfaces' eps;
1322 * - ep reset doesn't include halt(?).
1324 printf("broken set_interface (%d/%d)\n",
1325 le16_to_cpu(u.r.wIndex),
1326 le16_to_cpu(u.r.wValue));
1330 /* hardware was supposed to hide this */
1331 case USB_REQ_SET_ADDRESS:
1332 debug("GOT SET ADDRESS\n");
1333 if (u.r.bRequestType == USB_RECIP_DEVICE) {
1334 ep0start(dev, 0, "address");
1340 if (u.r.bRequestType & USB_DIR_IN)
1341 dev->ep0state = EP0_IN_DATA_PHASE;
1343 dev->ep0state = EP0_OUT_DATA_PHASE;
1345 i = dev->driver->setup(&dev->gadget, &u.r);
1347 /* hardware automagic preventing STALL... */
1348 if (dev->req_config) {
1350 * hardware sometimes neglects to tell
1351 * tell us about config change events,
1352 * so later ones may fail...
1354 printf("config change %02x fail %d?\n",
1358 * TODO experiment: if has_cfr,
1359 * hardware didn't ACK; maybe we
1360 * could actually STALL!
1365 /* uninitialized when goto stall */
1368 debug("protocol STALL, "
1370 readl(&dev->regs->udccs[0]), i);
1373 * the watchdog timer helps deal with cases
1374 * where udc seems to clear FST wrongly, and
1375 * then NAKs instead of STALLing.
1377 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall");
1378 start_watchdog(dev);
1379 dev->ep0state = EP0_STALL;
1381 /* deferred i/o == no response yet */
1382 } else if (dev->req_pending) {
1383 if (likely(dev->ep0state == EP0_IN_DATA_PHASE
1384 || dev->req_std || u.r.wLength))
1385 ep0start(dev, 0, "defer");
1387 ep0start(dev, UDCCS0_IPR, "defer/IPR");
1390 /* expect at least one data or status stage irq */
1393 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA))
1394 == (UDCCS0_OPR|UDCCS0_SA))) {
1398 * pxa210/250 erratum 131 for B0/B1 says RNE lies.
1399 * still observed on a pxa255 a0.
1404 /* read SETUP data, but don't trust it too much */
1405 for (i = 0; i < 8; i++)
1406 u.raw[i] = (u8)readb(&dev->regs->uddr0);
1407 if ((u.r.bRequestType & USB_RECIP_MASK)
1410 if (u.word[0] == 0 && u.word[1] == 0)
1415 * some random early IRQ:
1418 * - OPR got set, without SA (likely status stage)
1420 debug("random IRQ %X %X\n", udccs0,
1421 readl(&dev->regs->udccs[0]));
1422 writel(udccs0 & (UDCCS0_SA|UDCCS0_OPR),
1423 &dev->regs->udccs[0]);
1426 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */
1427 if (udccs0 & UDCCS0_OPR) {
1428 debug("ep0in premature status\n");
1432 } else /* irq was IPR clearing */ {
1434 debug("next ep0 in packet\n");
1435 /* this IN packet might finish the request */
1436 (void) write_ep0_fifo(ep, req);
1437 } /* else IN token before response was written */
1440 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */
1441 if (udccs0 & UDCCS0_OPR) {
1443 /* this OUT packet might finish the request */
1444 if (read_ep0_fifo(ep, req))
1446 /* else more OUT packets expected */
1447 } /* else OUT token before read was issued */
1448 } else /* irq was IPR clearing */ {
1449 debug("ep0out premature status\n");
1459 * ack control-IN status (maybe in-zlp was skipped)
1460 * also appears after some config change events.
1462 if (udccs0 & UDCCS0_OPR)
1463 writel(UDCCS0_OPR, &dev->regs->udccs[0]);
1467 writel(UDCCS0_FST, &dev->regs->udccs[0]);
1471 writel(USIR0_IR0, &dev->regs->usir0);
1474 static void handle_ep(struct pxa25x_ep *ep)
1476 struct pxa25x_request *req;
1477 int is_in = ep->bEndpointAddress & USB_DIR_IN;
1483 if (likely(!list_empty(&ep->queue)))
1484 req = list_entry(ep->queue.next,
1485 struct pxa25x_request, queue);
1489 /* TODO check FST handling */
1491 udccs = readl(ep->reg_udccs);
1492 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */
1494 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1495 tmp |= UDCCS_BI_SST;
1498 writel(tmp, ep->reg_udccs);
1499 if (req && likely((udccs & UDCCS_BI_TFS) != 0))
1500 completed = write_fifo(ep, req);
1502 } else { /* irq from RPC (or for ISO, ROF) */
1503 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK))
1504 tmp = UDCCS_BO_SST | UDCCS_BO_DME;
1506 tmp = UDCCS_IO_ROF | UDCCS_IO_DME;
1509 writel(tmp, ep->reg_udccs);
1511 /* fifos can hold packets, ready for reading... */
1513 completed = read_fifo(ep, req);
1515 pio_irq_disable(ep->bEndpointAddress);
1518 } while (completed);
1522 * pxa25x_udc_irq - interrupt handler
1524 * avoid delays in ep0 processing. the control handshaking isn't always
1525 * under software control (pxa250c0 and the pxa255 are better), and delays
1526 * could cause usb protocol errors.
1528 static struct pxa25x_udc memory;
1530 pxa25x_udc_irq(void)
1532 struct pxa25x_udc *dev = &memory;
1539 u32 udccr = readl(&dev->regs->udccr);
1543 /* SUSpend Interrupt Request */
1544 if (unlikely(udccr & UDCCR_SUSIR)) {
1545 udc_ack_int_UDCCR(UDCCR_SUSIR);
1547 debug("USB suspend\n");
1549 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1551 && dev->driver->suspend)
1552 dev->driver->suspend(&dev->gadget);
1556 /* RESume Interrupt Request */
1557 if (unlikely(udccr & UDCCR_RESIR)) {
1558 udc_ack_int_UDCCR(UDCCR_RESIR);
1560 debug("USB resume\n");
1562 if (dev->gadget.speed != USB_SPEED_UNKNOWN
1564 && dev->driver->resume)
1565 dev->driver->resume(&dev->gadget);
1568 /* ReSeT Interrupt Request - USB reset */
1569 if (unlikely(udccr & UDCCR_RSTIR)) {
1570 udc_ack_int_UDCCR(UDCCR_RSTIR);
1573 if ((readl(&dev->regs->udccr) & UDCCR_UDA) == 0) {
1574 debug("USB reset start\n");
1577 * reset driver and endpoints,
1578 * in case that's not yet done
1580 stop_activity(dev, dev->driver);
1583 debug("USB reset end\n");
1584 dev->gadget.speed = USB_SPEED_FULL;
1585 memset(&dev->stats, 0, sizeof dev->stats);
1586 /* driver and endpoints are still reset */
1590 u32 uicr0 = readl(&dev->regs->uicr0);
1591 u32 uicr1 = readl(&dev->regs->uicr1);
1592 u32 usir0 = readl(&dev->regs->usir0);
1593 u32 usir1 = readl(&dev->regs->usir1);
1595 usir0 = usir0 & ~uicr0;
1596 usir1 = usir1 & ~uicr1;
1599 if (unlikely(!usir0 && !usir1))
1602 debug_cond(NOISY, "irq %02x.%02x\n", usir1, usir0);
1604 /* control traffic */
1605 if (usir0 & USIR0_IR0) {
1606 dev->ep[0].pio_irqs++;
1611 /* endpoint data transfers */
1612 for (i = 0; i < 8; i++) {
1615 if (i && (usir0 & tmp)) {
1616 handle_ep(&dev->ep[i]);
1617 setbits_le32(&dev->regs->usir0, tmp);
1620 #ifndef CONFIG_USB_PXA25X_SMALL
1622 handle_ep(&dev->ep[i+8]);
1623 setbits_le32(&dev->regs->usir1, tmp);
1630 /* we could also ask for 1 msec SOF (SIR) interrupts */
1636 /*-------------------------------------------------------------------------*/
1639 * this uses load-time allocation and initialization (instead of
1640 * doing it at run-time) to save code, eliminate fault paths, and
1641 * be more obviously correct.
1643 static struct pxa25x_udc memory = {
1647 .ops = &pxa25x_udc_ops,
1648 .ep0 = &memory.ep[0].ep,
1649 .name = driver_name,
1652 /* control endpoint */
1656 .ops = &pxa25x_ep_ops,
1657 .maxpacket = EP0_FIFO_SIZE,
1660 .reg_udccs = &UDC_REGS->udccs[0],
1661 .reg_uddr = &UDC_REGS->uddr0,
1664 /* first group of endpoints */
1667 .name = "ep1in-bulk",
1668 .ops = &pxa25x_ep_ops,
1669 .maxpacket = BULK_FIFO_SIZE,
1672 .fifo_size = BULK_FIFO_SIZE,
1673 .bEndpointAddress = USB_DIR_IN | 1,
1674 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1675 .reg_udccs = &UDC_REGS->udccs[1],
1676 .reg_uddr = &UDC_REGS->uddr1,
1680 .name = "ep2out-bulk",
1681 .ops = &pxa25x_ep_ops,
1682 .maxpacket = BULK_FIFO_SIZE,
1685 .fifo_size = BULK_FIFO_SIZE,
1686 .bEndpointAddress = 2,
1687 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1688 .reg_udccs = &UDC_REGS->udccs[2],
1689 .reg_ubcr = &UDC_REGS->ubcr2,
1690 .reg_uddr = &UDC_REGS->uddr2,
1692 #ifndef CONFIG_USB_PXA25X_SMALL
1695 .name = "ep3in-iso",
1696 .ops = &pxa25x_ep_ops,
1697 .maxpacket = ISO_FIFO_SIZE,
1700 .fifo_size = ISO_FIFO_SIZE,
1701 .bEndpointAddress = USB_DIR_IN | 3,
1702 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1703 .reg_udccs = &UDC_REGS->udccs[3],
1704 .reg_uddr = &UDC_REGS->uddr3,
1708 .name = "ep4out-iso",
1709 .ops = &pxa25x_ep_ops,
1710 .maxpacket = ISO_FIFO_SIZE,
1713 .fifo_size = ISO_FIFO_SIZE,
1714 .bEndpointAddress = 4,
1715 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1716 .reg_udccs = &UDC_REGS->udccs[4],
1717 .reg_ubcr = &UDC_REGS->ubcr4,
1718 .reg_uddr = &UDC_REGS->uddr4,
1722 .name = "ep5in-int",
1723 .ops = &pxa25x_ep_ops,
1724 .maxpacket = INT_FIFO_SIZE,
1727 .fifo_size = INT_FIFO_SIZE,
1728 .bEndpointAddress = USB_DIR_IN | 5,
1729 .bmAttributes = USB_ENDPOINT_XFER_INT,
1730 .reg_udccs = &UDC_REGS->udccs[5],
1731 .reg_uddr = &UDC_REGS->uddr5,
1734 /* second group of endpoints */
1737 .name = "ep6in-bulk",
1738 .ops = &pxa25x_ep_ops,
1739 .maxpacket = BULK_FIFO_SIZE,
1742 .fifo_size = BULK_FIFO_SIZE,
1743 .bEndpointAddress = USB_DIR_IN | 6,
1744 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1745 .reg_udccs = &UDC_REGS->udccs[6],
1746 .reg_uddr = &UDC_REGS->uddr6,
1750 .name = "ep7out-bulk",
1751 .ops = &pxa25x_ep_ops,
1752 .maxpacket = BULK_FIFO_SIZE,
1755 .fifo_size = BULK_FIFO_SIZE,
1756 .bEndpointAddress = 7,
1757 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1758 .reg_udccs = &UDC_REGS->udccs[7],
1759 .reg_ubcr = &UDC_REGS->ubcr7,
1760 .reg_uddr = &UDC_REGS->uddr7,
1764 .name = "ep8in-iso",
1765 .ops = &pxa25x_ep_ops,
1766 .maxpacket = ISO_FIFO_SIZE,
1769 .fifo_size = ISO_FIFO_SIZE,
1770 .bEndpointAddress = USB_DIR_IN | 8,
1771 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1772 .reg_udccs = &UDC_REGS->udccs[8],
1773 .reg_uddr = &UDC_REGS->uddr8,
1777 .name = "ep9out-iso",
1778 .ops = &pxa25x_ep_ops,
1779 .maxpacket = ISO_FIFO_SIZE,
1782 .fifo_size = ISO_FIFO_SIZE,
1783 .bEndpointAddress = 9,
1784 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1785 .reg_udccs = &UDC_REGS->udccs[9],
1786 .reg_ubcr = &UDC_REGS->ubcr9,
1787 .reg_uddr = &UDC_REGS->uddr9,
1791 .name = "ep10in-int",
1792 .ops = &pxa25x_ep_ops,
1793 .maxpacket = INT_FIFO_SIZE,
1796 .fifo_size = INT_FIFO_SIZE,
1797 .bEndpointAddress = USB_DIR_IN | 10,
1798 .bmAttributes = USB_ENDPOINT_XFER_INT,
1799 .reg_udccs = &UDC_REGS->udccs[10],
1800 .reg_uddr = &UDC_REGS->uddr10,
1803 /* third group of endpoints */
1806 .name = "ep11in-bulk",
1807 .ops = &pxa25x_ep_ops,
1808 .maxpacket = BULK_FIFO_SIZE,
1811 .fifo_size = BULK_FIFO_SIZE,
1812 .bEndpointAddress = USB_DIR_IN | 11,
1813 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1814 .reg_udccs = &UDC_REGS->udccs[11],
1815 .reg_uddr = &UDC_REGS->uddr11,
1819 .name = "ep12out-bulk",
1820 .ops = &pxa25x_ep_ops,
1821 .maxpacket = BULK_FIFO_SIZE,
1824 .fifo_size = BULK_FIFO_SIZE,
1825 .bEndpointAddress = 12,
1826 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1827 .reg_udccs = &UDC_REGS->udccs[12],
1828 .reg_ubcr = &UDC_REGS->ubcr12,
1829 .reg_uddr = &UDC_REGS->uddr12,
1833 .name = "ep13in-iso",
1834 .ops = &pxa25x_ep_ops,
1835 .maxpacket = ISO_FIFO_SIZE,
1838 .fifo_size = ISO_FIFO_SIZE,
1839 .bEndpointAddress = USB_DIR_IN | 13,
1840 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1841 .reg_udccs = &UDC_REGS->udccs[13],
1842 .reg_uddr = &UDC_REGS->uddr13,
1846 .name = "ep14out-iso",
1847 .ops = &pxa25x_ep_ops,
1848 .maxpacket = ISO_FIFO_SIZE,
1851 .fifo_size = ISO_FIFO_SIZE,
1852 .bEndpointAddress = 14,
1853 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
1854 .reg_udccs = &UDC_REGS->udccs[14],
1855 .reg_ubcr = &UDC_REGS->ubcr14,
1856 .reg_uddr = &UDC_REGS->uddr14,
1860 .name = "ep15in-int",
1861 .ops = &pxa25x_ep_ops,
1862 .maxpacket = INT_FIFO_SIZE,
1865 .fifo_size = INT_FIFO_SIZE,
1866 .bEndpointAddress = USB_DIR_IN | 15,
1867 .bmAttributes = USB_ENDPOINT_XFER_INT,
1868 .reg_udccs = &UDC_REGS->udccs[15],
1869 .reg_uddr = &UDC_REGS->uddr15,
1871 #endif /* !CONFIG_USB_PXA25X_SMALL */
1874 static void udc_command(int cmd)
1877 case PXA2XX_UDC_CMD_CONNECT:
1878 setbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1879 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1882 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1883 GPCR(CONFIG_USB_DEV_PULLUP_GPIO));
1885 debug("Connected to USB\n");
1888 case PXA2XX_UDC_CMD_DISCONNECT:
1889 /* disable pullup resistor */
1890 writel(GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO),
1891 GPSR(CONFIG_USB_DEV_PULLUP_GPIO));
1893 /* setup pin as input, line will float */
1894 clrbits_le32(GPDR(CONFIG_USB_DEV_PULLUP_GPIO),
1895 GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO));
1897 debug("Disconnected from USB\n");
1902 static struct pxa2xx_udc_mach_info mach_info = {
1903 .udc_command = udc_command,
1907 * when a driver is successfully registered, it will receive
1908 * control requests including set_configuration(), which enables
1909 * non-control requests. then usb traffic follows until a
1910 * disconnect is reported. then a host may connect again, or
1911 * the driver might get unbound.
1913 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1915 struct pxa25x_udc *dev = &memory;
1920 || driver->speed < USB_SPEED_FULL
1921 || !driver->disconnect
1929 /* Enable clock for usb controller */
1930 setbits_le32(CKEN, CKEN11_USB);
1932 /* first hook up the driver ... */
1933 dev->driver = driver;
1936 /* trigger chiprev-specific logic */
1937 switch ((chiprev = pxa_get_cpu_revision())) {
1943 /* A0/A1 "not released"; ep 13, 15 unusable */
1945 case PXA250_B2: case PXA210_B2:
1946 case PXA250_B1: case PXA210_B1:
1947 case PXA250_B0: case PXA210_B0:
1948 /* OUT-DMA is broken ... */
1950 case PXA250_C0: case PXA210_C0:
1953 printf("%s: unrecognized processor: %08x\n",
1954 DRIVER_NAME, chiprev);
1958 the_controller = dev;
1960 /* prepare watchdog timer */
1961 dev->watchdog.running = 0;
1962 dev->watchdog.period = 5000 * CONFIG_SYS_HZ / 1000000; /* 5 ms */
1963 dev->watchdog.function = udc_watchdog;
1968 dev->mach = &mach_info;
1970 dev->gadget.name = "pxa2xx_udc";
1971 retval = driver->bind(&dev->gadget);
1973 printf("bind to driver %s --> error %d\n",
1974 DRIVER_NAME, retval);
1980 * ... then enable host detection and ep0; and we're ready
1981 * for set_configuration as well as eventual disconnect.
1983 printf("registered gadget driver '%s'\n", DRIVER_NAME);
1991 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver)
1995 /* don't disconnect drivers more than once */
1996 if (dev->gadget.speed == USB_SPEED_UNKNOWN)
1998 dev->gadget.speed = USB_SPEED_UNKNOWN;
2000 /* prevent new request submissions, kill any outstanding requests */
2001 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) {
2002 struct pxa25x_ep *ep = &dev->ep[i];
2005 nuke(ep, -ESHUTDOWN);
2009 /* report disconnect; the driver is already quiesced */
2011 driver->disconnect(&dev->gadget);
2013 /* re-init driver-visible data structures */
2017 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2019 struct pxa25x_udc *dev = the_controller;
2023 if (!driver || driver != dev->driver || !driver->unbind)
2026 local_irq_disable();
2029 stop_activity(dev, driver);
2032 driver->unbind(&dev->gadget);
2035 printf("unregistered gadget driver '%s'\n", DRIVER_NAME);
2038 the_controller = NULL;
2040 clrbits_le32(CKEN, CKEN11_USB);
2045 extern void udc_disconnect(void)
2047 setbits_le32(CKEN, CKEN11_USB);
2048 udc_clear_mask_UDCCR(UDCCR_UDE);
2049 udc_command(PXA2XX_UDC_CMD_DISCONNECT);
2050 clrbits_le32(CKEN, CKEN11_USB);
2053 /*-------------------------------------------------------------------------*/
2056 usb_gadget_handle_interrupts(void)
2058 return pxa25x_udc_irq();