2 * at91_udc -- driver for at91-series USB peripheral controller
4 * Copyright (C) 2004 by Thomas Rathbone
5 * Copyright (C) 2005 by HP Labs
6 * Copyright (C) 2005 by David Brownell
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/clk.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
42 #include <asm/byteorder.h>
43 #include <asm/hardware.h>
46 #include <asm/system.h>
47 #include <asm/mach-types.h>
50 #include <asm/arch/board.h>
51 #include <asm/arch/cpu.h>
52 #include <asm/arch/at91sam9261_matrix.h>
58 * This controller is simple and PIO-only. It's used in many AT91-series
59 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
60 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
62 * This driver expects the board has been wired with two GPIOs suppporting
63 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
64 * testing hasn't covered such cases.)
66 * The pullup is most important (so it's integrated on sam926x parts). It
67 * provides software control over whether the host enumerates the device.
69 * The VBUS sensing helps during enumeration, and allows both USB clocks
70 * (and the transceiver) to stay gated off until they're necessary, saving
71 * power. During USB suspend, the 48 MHz clock is gated off in hardware;
72 * it may also be gated off by software during some Linux sleep states.
75 #define DRIVER_VERSION "3 May 2006"
77 static const char driver_name [] = "at91_udc";
78 static const char ep0name[] = "ep0";
81 #define at91_udp_read(dev, reg) \
82 __raw_readl((dev)->udp_baseaddr + (reg))
83 #define at91_udp_write(dev, reg, val) \
84 __raw_writel((val), (dev)->udp_baseaddr + (reg))
86 /*-------------------------------------------------------------------------*/
88 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
90 #include <linux/seq_file.h>
92 static const char debug_filename[] = "driver/udc";
94 #define FOURBITS "%s%s%s%s"
95 #define EIGHTBITS FOURBITS FOURBITS
97 static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
99 static char *types[] = {
100 "control", "out-iso", "out-bulk", "out-int",
101 "BOGUS", "in-iso", "in-bulk", "in-int"};
104 struct at91_request *req;
107 local_irq_save(flags);
109 csr = __raw_readl(ep->creg);
111 /* NOTE: not collecting per-endpoint irq statistics... */
114 seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
115 ep->ep.name, ep->ep.maxpacket,
116 ep->is_in ? "in" : "out",
117 ep->is_iso ? " iso" : "",
119 ? (ep->fifo_bank ? "pong" : "ping")
121 ep->stopped ? " stopped" : "");
122 seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
124 (csr & 0x07ff0000) >> 16,
125 (csr & (1 << 15)) ? "enabled" : "disabled",
126 (csr & (1 << 11)) ? "DATA1" : "DATA0",
127 types[(csr & 0x700) >> 8],
129 /* iff type is control then print current direction */
131 ? ((csr & (1 << 7)) ? " IN" : " OUT")
133 (csr & (1 << 6)) ? " rxdatabk1" : "",
134 (csr & (1 << 5)) ? " forcestall" : "",
135 (csr & (1 << 4)) ? " txpktrdy" : "",
137 (csr & (1 << 3)) ? " stallsent" : "",
138 (csr & (1 << 2)) ? " rxsetup" : "",
139 (csr & (1 << 1)) ? " rxdatabk0" : "",
140 (csr & (1 << 0)) ? " txcomp" : "");
141 if (list_empty (&ep->queue))
142 seq_printf(s, "\t(queue empty)\n");
144 else list_for_each_entry (req, &ep->queue, queue) {
145 unsigned length = req->req.actual;
147 seq_printf(s, "\treq %p len %d/%d buf %p\n",
149 req->req.length, req->req.buf);
151 local_irq_restore(flags);
154 static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
158 seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
159 (mask & (1 << 13)) ? " wakeup" : "",
160 (mask & (1 << 12)) ? " endbusres" : "",
162 (mask & (1 << 11)) ? " sofint" : "",
163 (mask & (1 << 10)) ? " extrsm" : "",
164 (mask & (1 << 9)) ? " rxrsm" : "",
165 (mask & (1 << 8)) ? " rxsusp" : "");
166 for (i = 0; i < 8; i++) {
168 seq_printf(s, " ep%d", i);
173 static int proc_udc_show(struct seq_file *s, void *unused)
175 struct at91_udc *udc = s->private;
179 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
181 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
182 udc->vbus ? "present" : "off",
184 ? (udc->vbus ? "active" : "enabled")
186 udc->selfpowered ? "self" : "VBUS",
187 udc->suspended ? ", suspended" : "",
188 udc->driver ? udc->driver->driver.name : "(none)");
190 /* don't access registers when interface isn't clocked */
192 seq_printf(s, "(not clocked)\n");
196 tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
197 seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
198 (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
199 (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
200 (tmp & AT91_UDP_NUM));
202 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
203 seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
204 (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
205 (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
206 (tmp & AT91_UDP_ESR) ? " esr" : "",
207 (tmp & AT91_UDP_CONFG) ? " confg" : "",
208 (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
210 tmp = at91_udp_read(udc, AT91_UDP_FADDR);
211 seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
212 (tmp & AT91_UDP_FEN) ? " fen" : "",
213 (tmp & AT91_UDP_FADD));
215 proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
216 proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
218 if (udc->enabled && udc->vbus) {
219 proc_ep_show(s, &udc->ep[0]);
220 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
228 static int proc_udc_open(struct inode *inode, struct file *file)
230 return single_open(file, proc_udc_show, PDE(inode)->data);
233 static const struct file_operations proc_ops = {
234 .open = proc_udc_open,
237 .release = single_release,
240 static void create_debug_file(struct at91_udc *udc)
242 struct proc_dir_entry *pde;
244 pde = create_proc_entry (debug_filename, 0, NULL);
249 pde->proc_fops = &proc_ops;
253 static void remove_debug_file(struct at91_udc *udc)
256 remove_proc_entry(debug_filename, NULL);
261 static inline void create_debug_file(struct at91_udc *udc) {}
262 static inline void remove_debug_file(struct at91_udc *udc) {}
267 /*-------------------------------------------------------------------------*/
269 static void done(struct at91_ep *ep, struct at91_request *req, int status)
271 unsigned stopped = ep->stopped;
272 struct at91_udc *udc = ep->udc;
274 list_del_init(&req->queue);
275 if (req->req.status == -EINPROGRESS)
276 req->req.status = status;
278 status = req->req.status;
279 if (status && status != -ESHUTDOWN)
280 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
283 req->req.complete(&ep->ep, &req->req);
284 ep->stopped = stopped;
286 /* ep0 is always ready; other endpoints need a non-empty queue */
287 if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
288 at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
291 /*-------------------------------------------------------------------------*/
293 /* bits indicating OUT fifo has data ready */
294 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
297 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
298 * back most of the value you just read (because of side effects, including
299 * bits that may change after reading and before writing).
301 * Except when changing a specific bit, always write values which:
302 * - clear SET_FX bits (setting them could change something)
303 * - set CLR_FX bits (clearing them could change something)
305 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
306 * that shouldn't normally be changed.
308 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
309 * implying a need to wait for one write to complete (test relevant bits)
310 * before starting the next write. This shouldn't be an issue given how
311 * infrequently we write, except maybe for write-then-read idioms.
313 #define SET_FX (AT91_UDP_TXPKTRDY)
314 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
315 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
317 /* pull OUT packet data from the endpoint's fifo */
318 static int read_fifo (struct at91_ep *ep, struct at91_request *req)
320 u32 __iomem *creg = ep->creg;
321 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
324 unsigned int count, bufferspace, is_done;
326 buf = req->req.buf + req->req.actual;
327 bufferspace = req->req.length - req->req.actual;
330 * there might be nothing to read if ep_queue() calls us,
331 * or if we already emptied both pingpong buffers
334 csr = __raw_readl(creg);
335 if ((csr & RX_DATA_READY) == 0)
338 count = (csr & AT91_UDP_RXBYTECNT) >> 16;
339 if (count > ep->ep.maxpacket)
340 count = ep->ep.maxpacket;
341 if (count > bufferspace) {
342 DBG("%s buffer overflow\n", ep->ep.name);
343 req->req.status = -EOVERFLOW;
346 __raw_readsb(dreg, buf, count);
348 /* release and swap pingpong mem bank */
350 if (ep->is_pingpong) {
351 if (ep->fifo_bank == 0) {
352 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
355 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
359 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
360 __raw_writel(csr, creg);
362 req->req.actual += count;
363 is_done = (count < ep->ep.maxpacket);
364 if (count == bufferspace)
367 PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
368 is_done ? " (done)" : "");
371 * avoid extra trips through IRQ logic for packets already in
372 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
376 else if (ep->is_pingpong) {
377 bufferspace -= count;
385 /* load fifo for an IN packet */
386 static int write_fifo(struct at91_ep *ep, struct at91_request *req)
388 u32 __iomem *creg = ep->creg;
389 u32 csr = __raw_readl(creg);
390 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
391 unsigned total, count, is_last;
394 * TODO: allow for writing two packets to the fifo ... that'll
395 * reduce the amount of IN-NAKing, but probably won't affect
396 * throughput much. (Unlike preventing OUT-NAKing!)
400 * If ep_queue() calls us, the queue is empty and possibly in
401 * odd states like TXCOMP not yet cleared (we do it, saving at
402 * least one IRQ) or the fifo not yet being free. Those aren't
403 * issues normally (IRQ handler fast path).
405 if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
406 if (csr & AT91_UDP_TXCOMP) {
408 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
409 __raw_writel(csr, creg);
410 csr = __raw_readl(creg);
412 if (csr & AT91_UDP_TXPKTRDY)
416 total = req->req.length - req->req.actual;
417 if (ep->ep.maxpacket < total) {
418 count = ep->ep.maxpacket;
422 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
426 * Write the packet, maybe it's a ZLP.
428 * NOTE: incrementing req->actual before we receive the ACK means
429 * gadget driver IN bytecounts can be wrong in fault cases. That's
430 * fixable with PIO drivers like this one (save "count" here, and
431 * do the increment later on TX irq), but not for most DMA hardware.
433 * So all gadget drivers must accept that potential error. Some
434 * hardware supports precise fifo status reporting, letting them
435 * recover when the actual bytecount matters (e.g. for USB Test
436 * and Measurement Class devices).
438 __raw_writesb(dreg, req->req.buf + req->req.actual, count);
440 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
441 __raw_writel(csr, creg);
442 req->req.actual += count;
444 PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
445 is_last ? " (done)" : "");
451 static void nuke(struct at91_ep *ep, int status)
453 struct at91_request *req;
455 // terminer chaque requete dans la queue
457 if (list_empty(&ep->queue))
460 VDBG("%s %s\n", __FUNCTION__, ep->ep.name);
461 while (!list_empty(&ep->queue)) {
462 req = list_entry(ep->queue.next, struct at91_request, queue);
463 done(ep, req, status);
467 /*-------------------------------------------------------------------------*/
469 static int at91_ep_enable(struct usb_ep *_ep,
470 const struct usb_endpoint_descriptor *desc)
472 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
473 struct at91_udc *dev = ep->udc;
480 || _ep->name == ep0name
481 || desc->bDescriptorType != USB_DT_ENDPOINT
482 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
483 || maxpacket > ep->maxpacket) {
484 DBG("bad ep or descriptor\n");
488 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
489 DBG("bogus device state\n");
493 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
495 case USB_ENDPOINT_XFER_CONTROL:
496 DBG("only one control endpoint\n");
498 case USB_ENDPOINT_XFER_INT:
502 case USB_ENDPOINT_XFER_BULK:
511 DBG("bogus maxpacket %d\n", maxpacket);
513 case USB_ENDPOINT_XFER_ISOC:
514 if (!ep->is_pingpong) {
515 DBG("iso requires double buffering\n");
522 local_irq_save(flags);
524 /* initialize endpoint to match this descriptor */
525 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
526 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
531 tmp |= AT91_UDP_EPEDS;
532 __raw_writel(tmp, ep->creg);
535 ep->ep.maxpacket = maxpacket;
538 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
539 * since endpoint resets don't reset hw pingpong state.
541 at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
542 at91_udp_write(dev, AT91_UDP_RST_EP, 0);
544 local_irq_restore(flags);
548 static int at91_ep_disable (struct usb_ep * _ep)
550 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
551 struct at91_udc *udc = ep->udc;
554 if (ep == &ep->udc->ep[0])
557 local_irq_save(flags);
559 nuke(ep, -ESHUTDOWN);
561 /* restore the endpoint's pristine config */
563 ep->ep.maxpacket = ep->maxpacket;
565 /* reset fifos and endpoint */
566 if (ep->udc->clocked) {
567 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
568 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
569 __raw_writel(0, ep->creg);
572 local_irq_restore(flags);
577 * this is a PIO-only driver, so there's nothing
578 * interesting for request or buffer allocation.
581 static struct usb_request *
582 at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
584 struct at91_request *req;
586 req = kzalloc(sizeof (struct at91_request), gfp_flags);
590 INIT_LIST_HEAD(&req->queue);
594 static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
596 struct at91_request *req;
598 req = container_of(_req, struct at91_request, req);
599 BUG_ON(!list_empty(&req->queue));
603 static int at91_ep_queue(struct usb_ep *_ep,
604 struct usb_request *_req, gfp_t gfp_flags)
606 struct at91_request *req;
608 struct at91_udc *dev;
612 req = container_of(_req, struct at91_request, req);
613 ep = container_of(_ep, struct at91_ep, ep);
615 if (!_req || !_req->complete
616 || !_req->buf || !list_empty(&req->queue)) {
617 DBG("invalid request\n");
621 if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
628 if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
629 DBG("invalid device\n");
633 _req->status = -EINPROGRESS;
636 local_irq_save(flags);
638 /* try to kickstart any empty and idle queue */
639 if (list_empty(&ep->queue) && !ep->stopped) {
643 * If this control request has a non-empty DATA stage, this
644 * will start that stage. It works just like a non-control
645 * request (until the status stage starts, maybe early).
647 * If the data stage is empty, then this starts a successful
648 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
650 is_ep0 = (ep->ep.name == ep0name);
654 if (!dev->req_pending) {
660 * defer changing CONFG until after the gadget driver
661 * reconfigures the endpoints.
663 if (dev->wait_for_config_ack) {
664 tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
665 tmp ^= AT91_UDP_CONFG;
666 VDBG("toggle config\n");
667 at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
669 if (req->req.length == 0) {
671 PACKET("ep0 in/status\n");
673 tmp = __raw_readl(ep->creg);
675 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
676 __raw_writel(tmp, ep->creg);
677 dev->req_pending = 0;
683 status = write_fifo(ep, req);
685 status = read_fifo(ep, req);
687 /* IN/STATUS stage is otherwise triggered by irq */
688 if (status && is_ep0)
694 if (req && !status) {
695 list_add_tail (&req->queue, &ep->queue);
696 at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
699 local_irq_restore(flags);
700 return (status < 0) ? status : 0;
703 static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
706 struct at91_request *req;
708 ep = container_of(_ep, struct at91_ep, ep);
709 if (!_ep || ep->ep.name == ep0name)
712 /* make sure it's actually queued on this endpoint */
713 list_for_each_entry (req, &ep->queue, queue) {
714 if (&req->req == _req)
717 if (&req->req != _req)
720 done(ep, req, -ECONNRESET);
724 static int at91_ep_set_halt(struct usb_ep *_ep, int value)
726 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
727 struct at91_udc *udc = ep->udc;
733 if (!_ep || ep->is_iso || !ep->udc->clocked)
737 local_irq_save(flags);
739 csr = __raw_readl(creg);
742 * fail with still-busy IN endpoints, ensuring correct sequencing
743 * of data tx then stall. note that the fifo rx bytecount isn't
744 * completely accurate as a tx bytecount.
746 if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
752 csr |= AT91_UDP_FORCESTALL;
753 VDBG("halt %s\n", ep->ep.name);
755 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
756 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
757 csr &= ~AT91_UDP_FORCESTALL;
759 __raw_writel(csr, creg);
762 local_irq_restore(flags);
766 static const struct usb_ep_ops at91_ep_ops = {
767 .enable = at91_ep_enable,
768 .disable = at91_ep_disable,
769 .alloc_request = at91_ep_alloc_request,
770 .free_request = at91_ep_free_request,
771 .queue = at91_ep_queue,
772 .dequeue = at91_ep_dequeue,
773 .set_halt = at91_ep_set_halt,
774 // there's only imprecise fifo status reporting
777 /*-------------------------------------------------------------------------*/
779 static int at91_get_frame(struct usb_gadget *gadget)
781 struct at91_udc *udc = to_udc(gadget);
783 if (!to_udc(gadget)->clocked)
785 return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
788 static int at91_wakeup(struct usb_gadget *gadget)
790 struct at91_udc *udc = to_udc(gadget);
792 int status = -EINVAL;
795 DBG("%s\n", __FUNCTION__ );
796 local_irq_save(flags);
798 if (!udc->clocked || !udc->suspended)
801 /* NOTE: some "early versions" handle ESR differently ... */
803 glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
804 if (!(glbstate & AT91_UDP_ESR))
806 glbstate |= AT91_UDP_ESR;
807 at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
810 local_irq_restore(flags);
814 /* reinit == restore inital software state */
815 static void udc_reinit(struct at91_udc *udc)
819 INIT_LIST_HEAD(&udc->gadget.ep_list);
820 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
822 for (i = 0; i < NUM_ENDPOINTS; i++) {
823 struct at91_ep *ep = &udc->ep[i];
826 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
830 ep->ep.maxpacket = ep->maxpacket;
831 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
832 // initialiser une queue par endpoint
833 INIT_LIST_HEAD(&ep->queue);
837 static void stop_activity(struct at91_udc *udc)
839 struct usb_gadget_driver *driver = udc->driver;
842 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
844 udc->gadget.speed = USB_SPEED_UNKNOWN;
847 for (i = 0; i < NUM_ENDPOINTS; i++) {
848 struct at91_ep *ep = &udc->ep[i];
850 nuke(ep, -ESHUTDOWN);
853 driver->disconnect(&udc->gadget);
858 static void clk_on(struct at91_udc *udc)
863 clk_enable(udc->iclk);
864 clk_enable(udc->fclk);
867 static void clk_off(struct at91_udc *udc)
872 udc->gadget.speed = USB_SPEED_UNKNOWN;
873 clk_disable(udc->fclk);
874 clk_disable(udc->iclk);
878 * activate/deactivate link with host; minimize power usage for
879 * inactive links by cutting clocks and transceiver power.
881 static void pullup(struct at91_udc *udc, int is_on)
883 int active = !udc->board.pullup_active_low;
885 if (!udc->enabled || !udc->vbus)
887 DBG("%sactive\n", is_on ? "" : "in");
891 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
892 at91_udp_write(udc, AT91_UDP_TXVC, 0);
893 if (cpu_is_at91rm9200())
894 gpio_set_value(udc->board.pullup_pin, active);
895 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
896 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
898 txvc |= AT91_UDP_TXVC_PUON;
899 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
900 } else if (cpu_is_at91sam9261()) {
903 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
904 usbpucr |= AT91_MATRIX_USBPUCR_PUON;
905 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
909 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
910 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
911 if (cpu_is_at91rm9200())
912 gpio_set_value(udc->board.pullup_pin, !active);
913 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
914 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
916 txvc &= ~AT91_UDP_TXVC_PUON;
917 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
918 } else if (cpu_is_at91sam9261()) {
921 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
922 usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
923 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
929 /* vbus is here! turn everything on that's ready */
930 static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
932 struct at91_udc *udc = to_udc(gadget);
935 // VDBG("vbus %s\n", is_active ? "on" : "off");
936 local_irq_save(flags);
937 udc->vbus = (is_active != 0);
939 pullup(udc, is_active);
942 local_irq_restore(flags);
946 static int at91_pullup(struct usb_gadget *gadget, int is_on)
948 struct at91_udc *udc = to_udc(gadget);
951 local_irq_save(flags);
952 udc->enabled = is_on = !!is_on;
954 local_irq_restore(flags);
958 static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
960 struct at91_udc *udc = to_udc(gadget);
963 local_irq_save(flags);
964 udc->selfpowered = (is_on != 0);
965 local_irq_restore(flags);
969 static const struct usb_gadget_ops at91_udc_ops = {
970 .get_frame = at91_get_frame,
971 .wakeup = at91_wakeup,
972 .set_selfpowered = at91_set_selfpowered,
973 .vbus_session = at91_vbus_session,
974 .pullup = at91_pullup,
977 * VBUS-powered devices may also also want to support bigger
978 * power budgets after an appropriate SET_CONFIGURATION.
980 // .vbus_power = at91_vbus_power,
983 /*-------------------------------------------------------------------------*/
985 static int handle_ep(struct at91_ep *ep)
987 struct at91_request *req;
988 u32 __iomem *creg = ep->creg;
989 u32 csr = __raw_readl(creg);
991 if (!list_empty(&ep->queue))
992 req = list_entry(ep->queue.next,
993 struct at91_request, queue);
998 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
1000 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
1001 __raw_writel(csr, creg);
1004 return write_fifo(ep, req);
1007 if (csr & AT91_UDP_STALLSENT) {
1008 /* STALLSENT bit == ISOERR */
1009 if (ep->is_iso && req)
1010 req->req.status = -EILSEQ;
1012 csr &= ~(SET_FX | AT91_UDP_STALLSENT);
1013 __raw_writel(csr, creg);
1014 csr = __raw_readl(creg);
1016 if (req && (csr & RX_DATA_READY))
1017 return read_fifo(ep, req);
1024 struct usb_ctrlrequest r;
1027 static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1029 u32 __iomem *creg = ep->creg;
1030 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1031 unsigned rxcount, i = 0;
1036 /* read and ack SETUP; hard-fail for bogus packets */
1037 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1038 if (likely(rxcount == 8)) {
1040 pkt.raw[i++] = __raw_readb(dreg);
1041 if (pkt.r.bRequestType & USB_DIR_IN) {
1042 csr |= AT91_UDP_DIR;
1045 csr &= ~AT91_UDP_DIR;
1049 // REVISIT this happens sometimes under load; why??
1050 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1054 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1055 __raw_writel(csr, creg);
1056 udc->wait_for_addr_ack = 0;
1057 udc->wait_for_config_ack = 0;
1059 if (unlikely(status != 0))
1062 #define w_index le16_to_cpu(pkt.r.wIndex)
1063 #define w_value le16_to_cpu(pkt.r.wValue)
1064 #define w_length le16_to_cpu(pkt.r.wLength)
1066 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1067 pkt.r.bRequestType, pkt.r.bRequest,
1068 w_value, w_index, w_length);
1071 * A few standard requests get handled here, ones that touch
1072 * hardware ... notably for device and endpoint features.
1074 udc->req_pending = 1;
1075 csr = __raw_readl(creg);
1078 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1080 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1081 | USB_REQ_SET_ADDRESS:
1082 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
1083 udc->addr = w_value;
1084 udc->wait_for_addr_ack = 1;
1085 udc->req_pending = 0;
1086 /* FADDR is set later, when we ack host STATUS */
1089 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1090 | USB_REQ_SET_CONFIGURATION:
1091 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
1093 udc->wait_for_config_ack = (tmp == 0);
1095 udc->wait_for_config_ack = (tmp != 0);
1096 if (udc->wait_for_config_ack)
1097 VDBG("wait for config\n");
1098 /* CONFG is toggled later, if gadget driver succeeds */
1102 * Hosts may set or clear remote wakeup status, and
1103 * devices may report they're VBUS powered.
1105 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1106 | USB_REQ_GET_STATUS:
1107 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1108 if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1109 tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1110 PACKET("get device status\n");
1111 __raw_writeb(tmp, dreg);
1112 __raw_writeb(0, dreg);
1114 /* then STATUS starts later, automatically */
1115 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1116 | USB_REQ_SET_FEATURE:
1117 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1119 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1120 tmp |= AT91_UDP_ESR;
1121 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1123 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1124 | USB_REQ_CLEAR_FEATURE:
1125 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1127 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1128 tmp &= ~AT91_UDP_ESR;
1129 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1133 * Interfaces have no feature settings; this is pretty useless.
1134 * we won't even insist the interface exists...
1136 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1137 | USB_REQ_GET_STATUS:
1138 PACKET("get interface status\n");
1139 __raw_writeb(0, dreg);
1140 __raw_writeb(0, dreg);
1142 /* then STATUS starts later, automatically */
1143 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1144 | USB_REQ_SET_FEATURE:
1145 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1146 | USB_REQ_CLEAR_FEATURE:
1150 * Hosts may clear bulk/intr endpoint halt after the gadget
1151 * driver sets it (not widely used); or set it (for testing)
1153 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1154 | USB_REQ_GET_STATUS:
1155 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1157 if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
1161 if ((w_index & USB_DIR_IN)) {
1164 } else if (ep->is_in)
1167 PACKET("get %s status\n", ep->ep.name);
1168 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1169 tmp = (1 << USB_ENDPOINT_HALT);
1172 __raw_writeb(tmp, dreg);
1173 __raw_writeb(0, dreg);
1175 /* then STATUS starts later, automatically */
1176 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1177 | USB_REQ_SET_FEATURE:
1178 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1180 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1182 if (!ep->desc || ep->is_iso)
1184 if ((w_index & USB_DIR_IN)) {
1187 } else if (ep->is_in)
1190 tmp = __raw_readl(ep->creg);
1192 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1193 __raw_writel(tmp, ep->creg);
1195 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1196 | USB_REQ_CLEAR_FEATURE:
1197 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1199 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1203 if (!ep->desc || ep->is_iso)
1205 if ((w_index & USB_DIR_IN)) {
1208 } else if (ep->is_in)
1211 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
1212 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
1213 tmp = __raw_readl(ep->creg);
1215 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1216 __raw_writel(tmp, ep->creg);
1217 if (!list_empty(&ep->queue))
1226 /* pass request up to the gadget driver */
1228 status = udc->driver->setup(&udc->gadget, &pkt.r);
1233 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1234 pkt.r.bRequestType, pkt.r.bRequest, status);
1235 csr |= AT91_UDP_FORCESTALL;
1236 __raw_writel(csr, creg);
1237 udc->req_pending = 0;
1242 /* immediate successful (IN) STATUS after zero length DATA */
1243 PACKET("ep0 in/status\n");
1245 csr |= AT91_UDP_TXPKTRDY;
1246 __raw_writel(csr, creg);
1247 udc->req_pending = 0;
1251 static void handle_ep0(struct at91_udc *udc)
1253 struct at91_ep *ep0 = &udc->ep[0];
1254 u32 __iomem *creg = ep0->creg;
1255 u32 csr = __raw_readl(creg);
1256 struct at91_request *req;
1258 if (unlikely(csr & AT91_UDP_STALLSENT)) {
1260 udc->req_pending = 0;
1262 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1263 __raw_writel(csr, creg);
1264 VDBG("ep0 stalled\n");
1265 csr = __raw_readl(creg);
1267 if (csr & AT91_UDP_RXSETUP) {
1269 udc->req_pending = 0;
1270 handle_setup(udc, ep0, csr);
1274 if (list_empty(&ep0->queue))
1277 req = list_entry(ep0->queue.next, struct at91_request, queue);
1279 /* host ACKed an IN packet that we sent */
1280 if (csr & AT91_UDP_TXCOMP) {
1282 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1284 /* write more IN DATA? */
1285 if (req && ep0->is_in) {
1287 udc->req_pending = 0;
1291 * - last IN DATA packet (including GET_STATUS)
1292 * - IN/STATUS for OUT DATA
1293 * - IN/STATUS for any zero-length DATA stage
1294 * except for the IN DATA case, the host should send
1295 * an OUT status later, which we'll ack.
1298 udc->req_pending = 0;
1299 __raw_writel(csr, creg);
1302 * SET_ADDRESS takes effect only after the STATUS
1303 * (to the original address) gets acked.
1305 if (udc->wait_for_addr_ack) {
1308 at91_udp_write(udc, AT91_UDP_FADDR,
1309 AT91_UDP_FEN | udc->addr);
1310 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1311 tmp &= ~AT91_UDP_FADDEN;
1313 tmp |= AT91_UDP_FADDEN;
1314 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1316 udc->wait_for_addr_ack = 0;
1317 VDBG("address %d\n", udc->addr);
1322 /* OUT packet arrived ... */
1323 else if (csr & AT91_UDP_RX_DATA_BK0) {
1325 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1327 /* OUT DATA stage */
1330 if (handle_ep(ep0)) {
1331 /* send IN/STATUS */
1332 PACKET("ep0 in/status\n");
1333 csr = __raw_readl(creg);
1335 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1336 __raw_writel(csr, creg);
1337 udc->req_pending = 0;
1339 } else if (udc->req_pending) {
1341 * AT91 hardware has a hard time with this
1342 * "deferred response" mode for control-OUT
1343 * transfers. (For control-IN it's fine.)
1345 * The normal solution leaves OUT data in the
1346 * fifo until the gadget driver is ready.
1347 * We couldn't do that here without disabling
1348 * the IRQ that tells about SETUP packets,
1349 * e.g. when the host gets impatient...
1351 * Working around it by copying into a buffer
1352 * would almost be a non-deferred response,
1353 * except that it wouldn't permit reliable
1354 * stalling of the request. Instead, demand
1355 * that gadget drivers not use this mode.
1357 DBG("no control-OUT deferred responses!\n");
1358 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1359 udc->req_pending = 0;
1362 /* STATUS stage for control-IN; ack. */
1364 PACKET("ep0 out/status ACK\n");
1365 __raw_writel(csr, creg);
1367 /* "early" status stage */
1374 static irqreturn_t at91_udc_irq (int irq, void *_udc)
1376 struct at91_udc *udc = _udc;
1382 status = at91_udp_read(udc, AT91_UDP_ISR)
1383 & at91_udp_read(udc, AT91_UDP_IMR);
1387 /* USB reset irq: not maskable */
1388 if (status & AT91_UDP_ENDBUSRES) {
1389 at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1390 at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1391 /* Atmel code clears this irq twice */
1392 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1393 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1394 VDBG("end bus reset\n");
1399 at91_udp_write(udc, AT91_UDP_CSR(0),
1400 AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1401 udc->gadget.speed = USB_SPEED_FULL;
1403 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
1406 * NOTE: this driver keeps clocks off unless the
1407 * USB host is present. That saves power, but for
1408 * boards that don't support VBUS detection, both
1409 * clocks need to be active most of the time.
1412 /* host initiated suspend (3+ms bus idle) */
1413 } else if (status & AT91_UDP_RXSUSP) {
1414 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1415 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
1416 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1417 // VDBG("bus suspend\n");
1423 * NOTE: when suspending a VBUS-powered device, the
1424 * gadget driver should switch into slow clock mode
1425 * and then into standby to avoid drawing more than
1426 * 500uA power (2500uA for some high-power configs).
1428 if (udc->driver && udc->driver->suspend)
1429 udc->driver->suspend(&udc->gadget);
1431 /* host initiated resume */
1432 } else if (status & AT91_UDP_RXRSM) {
1433 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1434 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
1435 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1436 // VDBG("bus resume\n");
1437 if (!udc->suspended)
1442 * NOTE: for a VBUS-powered device, the gadget driver
1443 * would normally want to switch out of slow clock
1444 * mode into normal mode.
1446 if (udc->driver && udc->driver->resume)
1447 udc->driver->resume(&udc->gadget);
1449 /* endpoint IRQs are cleared by handling them */
1453 struct at91_ep *ep = &udc->ep[1];
1457 for (i = 1; i < NUM_ENDPOINTS; i++) {
1469 /*-------------------------------------------------------------------------*/
1471 static void nop_release(struct device *dev)
1473 /* nothing to free */
1476 static struct at91_udc controller = {
1478 .ops = &at91_udc_ops,
1479 .ep0 = &controller.ep[0].ep,
1480 .name = driver_name,
1483 .release = nop_release,
1489 .ops = &at91_ep_ops,
1498 .ops = &at91_ep_ops,
1508 .ops = &at91_ep_ops,
1517 /* could actually do bulk too */
1519 .ops = &at91_ep_ops,
1528 .ops = &at91_ep_ops,
1538 .ops = &at91_ep_ops,
1545 /* ep6 and ep7 are also reserved (custom silicon might use them) */
1548 static irqreturn_t at91_vbus_irq(int irq, void *_udc)
1550 struct at91_udc *udc = _udc;
1553 /* vbus needs at least brief debouncing */
1555 value = gpio_get_value(udc->board.vbus_pin);
1556 if (value != udc->vbus)
1557 at91_vbus_session(&udc->gadget, value);
1562 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1564 struct at91_udc *udc = &controller;
1568 || driver->speed < USB_SPEED_FULL
1570 || !driver->setup) {
1571 DBG("bad parameter.\n");
1576 DBG("UDC already has a gadget driver\n");
1580 udc->driver = driver;
1581 udc->gadget.dev.driver = &driver->driver;
1582 udc->gadget.dev.driver_data = &driver->driver;
1584 udc->selfpowered = 1;
1586 retval = driver->bind(&udc->gadget);
1588 DBG("driver->bind() returned %d\n", retval);
1590 udc->gadget.dev.driver = NULL;
1591 udc->gadget.dev.driver_data = NULL;
1593 udc->selfpowered = 0;
1597 local_irq_disable();
1601 DBG("bound to %s\n", driver->driver.name);
1604 EXPORT_SYMBOL (usb_gadget_register_driver);
1606 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1608 struct at91_udc *udc = &controller;
1610 if (!driver || driver != udc->driver || !driver->unbind)
1613 local_irq_disable();
1615 at91_udp_write(udc, AT91_UDP_IDR, ~0);
1619 driver->unbind(&udc->gadget);
1620 udc->gadget.dev.driver = NULL;
1621 udc->gadget.dev.driver_data = NULL;
1624 DBG("unbound from %s\n", driver->driver.name);
1627 EXPORT_SYMBOL (usb_gadget_unregister_driver);
1629 /*-------------------------------------------------------------------------*/
1631 static void at91udc_shutdown(struct platform_device *dev)
1633 /* force disconnect on reboot */
1634 pullup(platform_get_drvdata(dev), 0);
1637 static int __init at91udc_probe(struct platform_device *pdev)
1639 struct device *dev = &pdev->dev;
1640 struct at91_udc *udc;
1642 struct resource *res;
1644 if (!dev->platform_data) {
1645 /* small (so we copy it) but critical! */
1646 DBG("missing platform_data\n");
1650 if (pdev->num_resources != 2) {
1651 DBG("invalid num_resources\n");
1654 if ((pdev->resource[0].flags != IORESOURCE_MEM)
1655 || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
1656 DBG("invalid resource type\n");
1660 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1664 if (!request_mem_region(res->start,
1665 res->end - res->start + 1,
1667 DBG("someone's using UDC memory\n");
1671 /* init software state */
1673 udc->gadget.dev.parent = dev;
1674 udc->board = *(struct at91_udc_data *) dev->platform_data;
1678 /* rm9200 needs manual D+ pullup; off by default */
1679 if (cpu_is_at91rm9200()) {
1680 if (udc->board.pullup_pin <= 0) {
1681 DBG("no D+ pullup?\n");
1685 retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
1687 DBG("D+ pullup is busy\n");
1690 gpio_direction_output(udc->board.pullup_pin,
1691 udc->board.pullup_active_low);
1694 udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
1695 if (!udc->udp_baseaddr) {
1702 /* get interface and function clocks */
1703 udc->iclk = clk_get(dev, "udc_clk");
1704 udc->fclk = clk_get(dev, "udpck");
1705 if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
1706 DBG("clocks missing\n");
1708 /* NOTE: we "know" here that refcounts on these are NOPs */
1712 retval = device_register(&udc->gadget.dev);
1716 /* don't do anything until we have both gadget driver and VBUS */
1717 clk_enable(udc->iclk);
1718 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
1719 at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
1720 /* Clear all pending interrupts - UDP may be used by bootloader. */
1721 at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
1722 clk_disable(udc->iclk);
1724 /* request UDC and maybe VBUS irqs */
1725 udc->udp_irq = platform_get_irq(pdev, 0);
1726 retval = request_irq(udc->udp_irq, at91_udc_irq,
1727 IRQF_DISABLED, driver_name, udc);
1729 DBG("request irq %d failed\n", udc->udp_irq);
1732 if (udc->board.vbus_pin > 0) {
1733 retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
1735 DBG("request vbus pin failed\n");
1738 gpio_direction_input(udc->board.vbus_pin);
1741 * Get the initial state of VBUS - we cannot expect
1742 * a pending interrupt.
1744 udc->vbus = gpio_get_value(udc->board.vbus_pin);
1745 if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
1746 IRQF_DISABLED, driver_name, udc)) {
1747 DBG("request vbus irq %d failed\n",
1748 udc->board.vbus_pin);
1749 free_irq(udc->udp_irq, udc);
1754 DBG("no VBUS detection, assuming always-on\n");
1757 dev_set_drvdata(dev, udc);
1758 device_init_wakeup(dev, 1);
1759 create_debug_file(udc);
1761 INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1765 if (udc->board.vbus_pin > 0)
1766 gpio_free(udc->board.vbus_pin);
1768 free_irq(udc->udp_irq, udc);
1770 device_unregister(&udc->gadget.dev);
1772 iounmap(udc->udp_baseaddr);
1774 if (cpu_is_at91rm9200())
1775 gpio_free(udc->board.pullup_pin);
1777 release_mem_region(res->start, res->end - res->start + 1);
1778 DBG("%s probe failed, %d\n", driver_name, retval);
1782 static int __exit at91udc_remove(struct platform_device *pdev)
1784 struct at91_udc *udc = platform_get_drvdata(pdev);
1785 struct resource *res;
1794 device_init_wakeup(&pdev->dev, 0);
1795 remove_debug_file(udc);
1796 if (udc->board.vbus_pin > 0) {
1797 free_irq(udc->board.vbus_pin, udc);
1798 gpio_free(udc->board.vbus_pin);
1800 free_irq(udc->udp_irq, udc);
1801 device_unregister(&udc->gadget.dev);
1803 iounmap(udc->udp_baseaddr);
1805 if (cpu_is_at91rm9200())
1806 gpio_free(udc->board.pullup_pin);
1808 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1809 release_mem_region(res->start, res->end - res->start + 1);
1818 static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
1820 struct at91_udc *udc = platform_get_drvdata(pdev);
1821 int wake = udc->driver && device_may_wakeup(&pdev->dev);
1823 /* Unless we can act normally to the host (letting it wake us up
1824 * whenever it has work for us) force disconnect. Wakeup requires
1825 * PLLB for USB events (signaling for reset, wakeup, or incoming
1826 * tokens) and VBUS irqs (on systems which support them).
1828 if ((!udc->suspended && udc->addr)
1830 || at91_suspend_entering_slow_clock()) {
1834 enable_irq_wake(udc->udp_irq);
1836 udc->active_suspend = wake;
1837 if (udc->board.vbus_pin > 0 && wake)
1838 enable_irq_wake(udc->board.vbus_pin);
1842 static int at91udc_resume(struct platform_device *pdev)
1844 struct at91_udc *udc = platform_get_drvdata(pdev);
1846 if (udc->board.vbus_pin > 0 && udc->active_suspend)
1847 disable_irq_wake(udc->board.vbus_pin);
1849 /* maybe reconnect to host; if so, clocks on */
1850 if (udc->active_suspend)
1851 disable_irq_wake(udc->udp_irq);
1857 #define at91udc_suspend NULL
1858 #define at91udc_resume NULL
1861 static struct platform_driver at91_udc_driver = {
1862 .remove = __exit_p(at91udc_remove),
1863 .shutdown = at91udc_shutdown,
1864 .suspend = at91udc_suspend,
1865 .resume = at91udc_resume,
1867 .name = (char *) driver_name,
1868 .owner = THIS_MODULE,
1872 static int __init udc_init_module(void)
1874 return platform_driver_probe(&at91_udc_driver, at91udc_probe);
1876 module_init(udc_init_module);
1878 static void __exit udc_exit_module(void)
1880 platform_driver_unregister(&at91_udc_driver);
1882 module_exit(udc_exit_module);
1884 MODULE_DESCRIPTION("AT91 udc driver");
1885 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1886 MODULE_LICENSE("GPL");