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.
28 #include <linux/config.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/platform_device.h>
32 #include <linux/delay.h>
33 #include <linux/ioport.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/smp_lock.h>
37 #include <linux/errno.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/proc_fs.h>
42 #include <linux/clk.h>
43 #include <linux/usb_ch9.h>
44 #include <linux/usb_gadget.h>
46 #include <asm/byteorder.h>
49 #include <asm/system.h>
50 #include <asm/mach-types.h>
52 #include <asm/arch/hardware.h>
53 #include <asm/arch/gpio.h>
54 #include <asm/arch/board.h>
60 * This controller is simple and PIO-only. It's used in many AT91-series
61 * ARMv4T controllers, including the at91rm9200 (arm920T, with MMU),
62 * at91sam9261 (arm926ejs, with MMU), and several no-mmu versions.
64 * This driver expects the board has been wired with two GPIOs suppporting
65 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
66 * testing hasn't covered such cases.) The pullup is most important; it
67 * provides software control over whether the host enumerates the device.
68 * The VBUS sensing helps during enumeration, and allows both USB clocks
69 * (and the transceiver) to stay gated off until they're necessary, saving
70 * power. During USB suspend, the 48 MHz clock is gated off.
73 #define DRIVER_VERSION "8 March 2005"
75 static const char driver_name
[] = "at91_udc";
76 static const char ep0name
[] = "ep0";
78 /*-------------------------------------------------------------------------*/
81 * Read from a UDP register.
83 static inline unsigned long at91_udp_read(unsigned int reg
)
85 void __iomem
*udp_base
= (void __iomem
*)AT91_VA_BASE_UDP
;
87 return __raw_readl(udp_base
+ reg
);
91 * Write to a UDP register.
93 static inline void at91_udp_write(unsigned int reg
, unsigned long value
)
95 void __iomem
*udp_base
= (void __iomem
*)AT91_VA_BASE_UDP
;
97 __raw_writel(value
, udp_base
+ reg
);
100 /*-------------------------------------------------------------------------*/
102 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
104 #include <linux/seq_file.h>
106 static const char debug_filename
[] = "driver/udc";
108 #define FOURBITS "%s%s%s%s"
109 #define EIGHTBITS FOURBITS FOURBITS
111 static void proc_ep_show(struct seq_file
*s
, struct at91_ep
*ep
)
113 static char *types
[] = {
114 "control", "out-iso", "out-bulk", "out-int",
115 "BOGUS", "in-iso", "in-bulk", "in-int"};
118 struct at91_request
*req
;
121 local_irq_save(flags
);
123 csr
= __raw_readl(ep
->creg
);
125 /* NOTE: not collecting per-endpoint irq statistics... */
128 seq_printf(s
, "%s, maxpacket %d %s%s %s%s\n",
129 ep
->ep
.name
, ep
->ep
.maxpacket
,
130 ep
->is_in
? "in" : "out",
131 ep
->is_iso
? " iso" : "",
133 ? (ep
->fifo_bank
? "pong" : "ping")
135 ep
->stopped
? " stopped" : "");
136 seq_printf(s
, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS
"\n",
138 (csr
& 0x07ff0000) >> 16,
139 (csr
& (1 << 15)) ? "enabled" : "disabled",
140 (csr
& (1 << 11)) ? "DATA1" : "DATA0",
141 types
[(csr
& 0x700) >> 8],
143 /* iff type is control then print current direction */
145 ? ((csr
& (1 << 7)) ? " IN" : " OUT")
147 (csr
& (1 << 6)) ? " rxdatabk1" : "",
148 (csr
& (1 << 5)) ? " forcestall" : "",
149 (csr
& (1 << 4)) ? " txpktrdy" : "",
151 (csr
& (1 << 3)) ? " stallsent" : "",
152 (csr
& (1 << 2)) ? " rxsetup" : "",
153 (csr
& (1 << 1)) ? " rxdatabk0" : "",
154 (csr
& (1 << 0)) ? " txcomp" : "");
155 if (list_empty (&ep
->queue
))
156 seq_printf(s
, "\t(queue empty)\n");
158 else list_for_each_entry (req
, &ep
->queue
, queue
) {
159 unsigned length
= req
->req
.actual
;
161 seq_printf(s
, "\treq %p len %d/%d buf %p\n",
163 req
->req
.length
, req
->req
.buf
);
165 local_irq_restore(flags
);
168 static void proc_irq_show(struct seq_file
*s
, const char *label
, u32 mask
)
172 seq_printf(s
, "%s %04x:%s%s" FOURBITS
, label
, mask
,
173 (mask
& (1 << 13)) ? " wakeup" : "",
174 (mask
& (1 << 12)) ? " endbusres" : "",
176 (mask
& (1 << 11)) ? " sofint" : "",
177 (mask
& (1 << 10)) ? " extrsm" : "",
178 (mask
& (1 << 9)) ? " rxrsm" : "",
179 (mask
& (1 << 8)) ? " rxsusp" : "");
180 for (i
= 0; i
< 8; i
++) {
182 seq_printf(s
, " ep%d", i
);
187 static int proc_udc_show(struct seq_file
*s
, void *unused
)
189 struct at91_udc
*udc
= s
->private;
193 seq_printf(s
, "%s: version %s\n", driver_name
, DRIVER_VERSION
);
195 seq_printf(s
, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
196 udc
->vbus
? "present" : "off",
198 ? (udc
->vbus
? "active" : "enabled")
200 udc
->selfpowered
? "self" : "VBUS",
201 udc
->suspended
? ", suspended" : "",
202 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
204 /* don't access registers when interface isn't clocked */
206 seq_printf(s
, "(not clocked)\n");
210 tmp
= at91_udp_read(AT91_UDP_FRM_NUM
);
211 seq_printf(s
, "frame %05x:%s%s frame=%d\n", tmp
,
212 (tmp
& AT91_UDP_FRM_OK
) ? " ok" : "",
213 (tmp
& AT91_UDP_FRM_ERR
) ? " err" : "",
214 (tmp
& AT91_UDP_NUM
));
216 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
217 seq_printf(s
, "glbstate %02x:%s" FOURBITS
"\n", tmp
,
218 (tmp
& AT91_UDP_RMWUPE
) ? " rmwupe" : "",
219 (tmp
& AT91_UDP_RSMINPR
) ? " rsminpr" : "",
220 (tmp
& AT91_UDP_ESR
) ? " esr" : "",
221 (tmp
& AT91_UDP_CONFG
) ? " confg" : "",
222 (tmp
& AT91_UDP_FADDEN
) ? " fadden" : "");
224 tmp
= at91_udp_read(AT91_UDP_FADDR
);
225 seq_printf(s
, "faddr %03x:%s fadd=%d\n", tmp
,
226 (tmp
& AT91_UDP_FEN
) ? " fen" : "",
227 (tmp
& AT91_UDP_FADD
));
229 proc_irq_show(s
, "imr ", at91_udp_read(AT91_UDP_IMR
));
230 proc_irq_show(s
, "isr ", at91_udp_read(AT91_UDP_ISR
));
232 if (udc
->enabled
&& udc
->vbus
) {
233 proc_ep_show(s
, &udc
->ep
[0]);
234 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
242 static int proc_udc_open(struct inode
*inode
, struct file
*file
)
244 return single_open(file
, proc_udc_show
, PDE(inode
)->data
);
247 static struct file_operations proc_ops
= {
248 .open
= proc_udc_open
,
251 .release
= single_release
,
254 static void create_debug_file(struct at91_udc
*udc
)
256 struct proc_dir_entry
*pde
;
258 pde
= create_proc_entry (debug_filename
, 0, NULL
);
263 pde
->proc_fops
= &proc_ops
;
267 static void remove_debug_file(struct at91_udc
*udc
)
270 remove_proc_entry(debug_filename
, NULL
);
275 static inline void create_debug_file(struct at91_udc
*udc
) {}
276 static inline void remove_debug_file(struct at91_udc
*udc
) {}
281 /*-------------------------------------------------------------------------*/
283 static void done(struct at91_ep
*ep
, struct at91_request
*req
, int status
)
285 unsigned stopped
= ep
->stopped
;
287 list_del_init(&req
->queue
);
288 if (req
->req
.status
== -EINPROGRESS
)
289 req
->req
.status
= status
;
291 status
= req
->req
.status
;
292 if (status
&& status
!= -ESHUTDOWN
)
293 VDBG("%s done %p, status %d\n", ep
->ep
.name
, req
, status
);
296 req
->req
.complete(&ep
->ep
, &req
->req
);
297 ep
->stopped
= stopped
;
299 /* ep0 is always ready; other endpoints need a non-empty queue */
300 if (list_empty(&ep
->queue
) && ep
->int_mask
!= (1 << 0))
301 at91_udp_write(AT91_UDP_IDR
, ep
->int_mask
);
304 /*-------------------------------------------------------------------------*/
306 /* bits indicating OUT fifo has data ready */
307 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
310 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
311 * back most of the value you just read (because of side effects, including
312 * bits that may change after reading and before writing).
314 * Except when changing a specific bit, always write values which:
315 * - clear SET_FX bits (setting them could change something)
316 * - set CLR_FX bits (clearing them could change something)
318 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
319 * that shouldn't normally be changed.
321 #define SET_FX (AT91_UDP_TXPKTRDY)
322 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
324 /* pull OUT packet data from the endpoint's fifo */
325 static int read_fifo (struct at91_ep
*ep
, struct at91_request
*req
)
327 u32 __iomem
*creg
= ep
->creg
;
328 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
331 unsigned int count
, bufferspace
, is_done
;
333 buf
= req
->req
.buf
+ req
->req
.actual
;
334 bufferspace
= req
->req
.length
- req
->req
.actual
;
337 * there might be nothing to read if ep_queue() calls us,
338 * or if we already emptied both pingpong buffers
341 csr
= __raw_readl(creg
);
342 if ((csr
& RX_DATA_READY
) == 0)
345 count
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
346 if (count
> ep
->ep
.maxpacket
)
347 count
= ep
->ep
.maxpacket
;
348 if (count
> bufferspace
) {
349 DBG("%s buffer overflow\n", ep
->ep
.name
);
350 req
->req
.status
= -EOVERFLOW
;
353 __raw_readsb(dreg
, buf
, count
);
355 /* release and swap pingpong mem bank */
357 if (ep
->is_pingpong
) {
358 if (ep
->fifo_bank
== 0) {
359 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
362 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK1
);
366 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
367 __raw_writel(csr
, creg
);
369 req
->req
.actual
+= count
;
370 is_done
= (count
< ep
->ep
.maxpacket
);
371 if (count
== bufferspace
)
374 PACKET("%s %p out/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
375 is_done
? " (done)" : "");
378 * avoid extra trips through IRQ logic for packets already in
379 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
383 else if (ep
->is_pingpong
) {
384 bufferspace
-= count
;
392 /* load fifo for an IN packet */
393 static int write_fifo(struct at91_ep
*ep
, struct at91_request
*req
)
395 u32 __iomem
*creg
= ep
->creg
;
396 u32 csr
= __raw_readl(creg
);
397 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
398 unsigned total
, count
, is_last
;
401 * TODO: allow for writing two packets to the fifo ... that'll
402 * reduce the amount of IN-NAKing, but probably won't affect
403 * throughput much. (Unlike preventing OUT-NAKing!)
407 * If ep_queue() calls us, the queue is empty and possibly in
408 * odd states like TXCOMP not yet cleared (we do it, saving at
409 * least one IRQ) or the fifo not yet being free. Those aren't
410 * issues normally (IRQ handler fast path).
412 if (unlikely(csr
& (AT91_UDP_TXCOMP
| AT91_UDP_TXPKTRDY
))) {
413 if (csr
& AT91_UDP_TXCOMP
) {
415 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
416 __raw_writel(csr
, creg
);
417 csr
= __raw_readl(creg
);
419 if (csr
& AT91_UDP_TXPKTRDY
)
423 total
= req
->req
.length
- req
->req
.actual
;
424 if (ep
->ep
.maxpacket
< total
) {
425 count
= ep
->ep
.maxpacket
;
429 is_last
= (count
< ep
->ep
.maxpacket
) || !req
->req
.zero
;
433 * Write the packet, maybe it's a ZLP.
435 * NOTE: incrementing req->actual before we receive the ACK means
436 * gadget driver IN bytecounts can be wrong in fault cases. That's
437 * fixable with PIO drivers like this one (save "count" here, and
438 * do the increment later on TX irq), but not for most DMA hardware.
440 * So all gadget drivers must accept that potential error. Some
441 * hardware supports precise fifo status reporting, letting them
442 * recover when the actual bytecount matters (e.g. for USB Test
443 * and Measurement Class devices).
445 __raw_writesb(dreg
, req
->req
.buf
+ req
->req
.actual
, count
);
447 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
448 __raw_writel(csr
, creg
);
449 req
->req
.actual
+= count
;
451 PACKET("%s %p in/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
452 is_last
? " (done)" : "");
458 static void nuke(struct at91_ep
*ep
, int status
)
460 struct at91_request
*req
;
462 // terminer chaque requete dans la queue
464 if (list_empty(&ep
->queue
))
467 VDBG("%s %s\n", __FUNCTION__
, ep
->ep
.name
);
468 while (!list_empty(&ep
->queue
)) {
469 req
= list_entry(ep
->queue
.next
, struct at91_request
, queue
);
470 done(ep
, req
, status
);
474 /*-------------------------------------------------------------------------*/
476 static int at91_ep_enable(struct usb_ep
*_ep
, const struct usb_endpoint_descriptor
*desc
)
478 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
479 struct at91_udc
*dev
= ep
->udc
;
486 || _ep
->name
== ep0name
487 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
488 || (maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
)) == 0
489 || maxpacket
> ep
->maxpacket
) {
490 DBG("bad ep or descriptor\n");
494 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
495 DBG("bogus device state\n");
499 tmp
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
501 case USB_ENDPOINT_XFER_CONTROL
:
502 DBG("only one control endpoint\n");
504 case USB_ENDPOINT_XFER_INT
:
508 case USB_ENDPOINT_XFER_BULK
:
517 DBG("bogus maxpacket %d\n", maxpacket
);
519 case USB_ENDPOINT_XFER_ISOC
:
520 if (!ep
->is_pingpong
) {
521 DBG("iso requires double buffering\n");
528 local_irq_save(flags
);
530 /* initialize endpoint to match this descriptor */
531 ep
->is_in
= (desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
532 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
);
537 tmp
|= AT91_UDP_EPEDS
;
538 __raw_writel(tmp
, ep
->creg
);
541 ep
->ep
.maxpacket
= maxpacket
;
544 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
545 * since endpoint resets don't reset hw pingpong state.
547 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
548 at91_udp_write(AT91_UDP_RST_EP
, 0);
550 local_irq_restore(flags
);
554 static int at91_ep_disable (struct usb_ep
* _ep
)
556 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
559 if (ep
== &ep
->udc
->ep
[0])
562 local_irq_save(flags
);
564 nuke(ep
, -ESHUTDOWN
);
566 /* restore the endpoint's pristine config */
568 ep
->ep
.maxpacket
= ep
->maxpacket
;
570 /* reset fifos and endpoint */
571 if (ep
->udc
->clocked
) {
572 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
573 at91_udp_write(AT91_UDP_RST_EP
, 0);
574 __raw_writel(0, ep
->creg
);
577 local_irq_restore(flags
);
582 * this is a PIO-only driver, so there's nothing
583 * interesting for request or buffer allocation.
586 static struct usb_request
*at91_ep_alloc_request (struct usb_ep
*_ep
, unsigned int gfp_flags
)
588 struct at91_request
*req
;
590 req
= kcalloc(1, sizeof (struct at91_request
), SLAB_KERNEL
);
594 INIT_LIST_HEAD(&req
->queue
);
598 static void at91_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
600 struct at91_request
*req
;
602 req
= container_of(_req
, struct at91_request
, req
);
603 BUG_ON(!list_empty(&req
->queue
));
607 static void *at91_ep_alloc_buffer(
614 return kmalloc(bytes
, gfp_flags
);
617 static void at91_ep_free_buffer(
626 static int at91_ep_queue(struct usb_ep
*_ep
,
627 struct usb_request
*_req
, gfp_t gfp_flags
)
629 struct at91_request
*req
;
631 struct at91_udc
*dev
;
635 req
= container_of(_req
, struct at91_request
, req
);
636 ep
= container_of(_ep
, struct at91_ep
, ep
);
638 if (!_req
|| !_req
->complete
639 || !_req
->buf
|| !list_empty(&req
->queue
)) {
640 DBG("invalid request\n");
644 if (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
)) {
651 if (!dev
|| !dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
652 DBG("invalid device\n");
656 _req
->status
= -EINPROGRESS
;
659 local_irq_save(flags
);
661 /* try to kickstart any empty and idle queue */
662 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
666 * If this control request has a non-empty DATA stage, this
667 * will start that stage. It works just like a non-control
668 * request (until the status stage starts, maybe early).
670 * If the data stage is empty, then this starts a successful
671 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
673 is_ep0
= (ep
->ep
.name
== ep0name
);
677 if (!dev
->req_pending
) {
683 * defer changing CONFG until after the gadget driver
684 * reconfigures the endpoints.
686 if (dev
->wait_for_config_ack
) {
687 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
688 tmp
^= AT91_UDP_CONFG
;
689 VDBG("toggle config\n");
690 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
692 if (req
->req
.length
== 0) {
694 PACKET("ep0 in/status\n");
696 tmp
= __raw_readl(ep
->creg
);
698 tmp
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
699 __raw_writel(tmp
, ep
->creg
);
700 dev
->req_pending
= 0;
706 status
= write_fifo(ep
, req
);
708 status
= read_fifo(ep
, req
);
710 /* IN/STATUS stage is otherwise triggered by irq */
711 if (status
&& is_ep0
)
717 if (req
&& !status
) {
718 list_add_tail (&req
->queue
, &ep
->queue
);
719 at91_udp_write(AT91_UDP_IER
, ep
->int_mask
);
722 local_irq_restore(flags
);
723 return (status
< 0) ? status
: 0;
726 static int at91_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
729 struct at91_request
*req
;
731 ep
= container_of(_ep
, struct at91_ep
, ep
);
732 if (!_ep
|| ep
->ep
.name
== ep0name
)
735 /* make sure it's actually queued on this endpoint */
736 list_for_each_entry (req
, &ep
->queue
, queue
) {
737 if (&req
->req
== _req
)
740 if (&req
->req
!= _req
)
743 done(ep
, req
, -ECONNRESET
);
747 static int at91_ep_set_halt(struct usb_ep
*_ep
, int value
)
749 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
755 if (!_ep
|| ep
->is_iso
|| !ep
->udc
->clocked
)
759 local_irq_save(flags
);
761 csr
= __raw_readl(creg
);
764 * fail with still-busy IN endpoints, ensuring correct sequencing
765 * of data tx then stall. note that the fifo rx bytecount isn't
766 * completely accurate as a tx bytecount.
768 if (ep
->is_in
&& (!list_empty(&ep
->queue
) || (csr
>> 16) != 0))
774 csr
|= AT91_UDP_FORCESTALL
;
775 VDBG("halt %s\n", ep
->ep
.name
);
777 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
778 at91_udp_write(AT91_UDP_RST_EP
, 0);
779 csr
&= ~AT91_UDP_FORCESTALL
;
781 __raw_writel(csr
, creg
);
784 local_irq_restore(flags
);
788 static struct usb_ep_ops at91_ep_ops
= {
789 .enable
= at91_ep_enable
,
790 .disable
= at91_ep_disable
,
791 .alloc_request
= at91_ep_alloc_request
,
792 .free_request
= at91_ep_free_request
,
793 .alloc_buffer
= at91_ep_alloc_buffer
,
794 .free_buffer
= at91_ep_free_buffer
,
795 .queue
= at91_ep_queue
,
796 .dequeue
= at91_ep_dequeue
,
797 .set_halt
= at91_ep_set_halt
,
798 // there's only imprecise fifo status reporting
801 /*-------------------------------------------------------------------------*/
803 static int at91_get_frame(struct usb_gadget
*gadget
)
805 if (!to_udc(gadget
)->clocked
)
807 return at91_udp_read(AT91_UDP_FRM_NUM
) & AT91_UDP_NUM
;
810 static int at91_wakeup(struct usb_gadget
*gadget
)
812 struct at91_udc
*udc
= to_udc(gadget
);
814 int status
= -EINVAL
;
817 DBG("%s\n", __FUNCTION__
);
818 local_irq_save(flags
);
820 if (!udc
->clocked
|| !udc
->suspended
)
823 /* NOTE: some "early versions" handle ESR differently ... */
825 glbstate
= at91_udp_read(AT91_UDP_GLB_STAT
);
826 if (!(glbstate
& AT91_UDP_ESR
))
828 glbstate
|= AT91_UDP_ESR
;
829 at91_udp_write(AT91_UDP_GLB_STAT
, glbstate
);
832 local_irq_restore(flags
);
836 /* reinit == restore inital software state */
837 static void udc_reinit(struct at91_udc
*udc
)
841 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
842 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
844 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
845 struct at91_ep
*ep
= &udc
->ep
[i
];
848 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
852 ep
->ep
.maxpacket
= ep
->maxpacket
;
853 // initialiser une queue par endpoint
854 INIT_LIST_HEAD(&ep
->queue
);
858 static void stop_activity(struct at91_udc
*udc
)
860 struct usb_gadget_driver
*driver
= udc
->driver
;
863 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
865 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
867 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
868 struct at91_ep
*ep
= &udc
->ep
[i
];
870 nuke(ep
, -ESHUTDOWN
);
873 driver
->disconnect(&udc
->gadget
);
878 static void clk_on(struct at91_udc
*udc
)
883 clk_enable(udc
->iclk
);
884 clk_enable(udc
->fclk
);
887 static void clk_off(struct at91_udc
*udc
)
892 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
893 clk_disable(udc
->iclk
);
894 clk_disable(udc
->fclk
);
898 * activate/deactivate link with host; minimize power usage for
899 * inactive links by cutting clocks and transceiver power.
901 static void pullup(struct at91_udc
*udc
, int is_on
)
903 if (!udc
->enabled
|| !udc
->vbus
)
905 DBG("%sactive\n", is_on
? "" : "in");
908 at91_udp_write(AT91_UDP_TXVC
, 0);
909 at91_set_gpio_value(udc
->board
.pullup_pin
, 1);
912 at91_udp_write(AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
913 at91_set_gpio_value(udc
->board
.pullup_pin
, 0);
916 // REVISIT: with transceiver disabled, will D- float
917 // so that a host would falsely detect a device?
921 /* vbus is here! turn everything on that's ready */
922 static int at91_vbus_session(struct usb_gadget
*gadget
, int is_active
)
924 struct at91_udc
*udc
= to_udc(gadget
);
927 // VDBG("vbus %s\n", is_active ? "on" : "off");
928 local_irq_save(flags
);
929 udc
->vbus
= (is_active
!= 0);
930 pullup(udc
, is_active
);
931 local_irq_restore(flags
);
935 static int at91_pullup(struct usb_gadget
*gadget
, int is_on
)
937 struct at91_udc
*udc
= to_udc(gadget
);
940 local_irq_save(flags
);
941 udc
->enabled
= is_on
= !!is_on
;
943 local_irq_restore(flags
);
947 static int at91_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
949 struct at91_udc
*udc
= to_udc(gadget
);
952 local_irq_save(flags
);
953 udc
->selfpowered
= (is_on
!= 0);
954 local_irq_restore(flags
);
958 static const struct usb_gadget_ops at91_udc_ops
= {
959 .get_frame
= at91_get_frame
,
960 .wakeup
= at91_wakeup
,
961 .set_selfpowered
= at91_set_selfpowered
,
962 .vbus_session
= at91_vbus_session
,
963 .pullup
= at91_pullup
,
966 * VBUS-powered devices may also also want to support bigger
967 * power budgets after an appropriate SET_CONFIGURATION.
969 // .vbus_power = at91_vbus_power,
972 /*-------------------------------------------------------------------------*/
974 static int handle_ep(struct at91_ep
*ep
)
976 struct at91_request
*req
;
977 u32 __iomem
*creg
= ep
->creg
;
978 u32 csr
= __raw_readl(creg
);
980 if (!list_empty(&ep
->queue
))
981 req
= list_entry(ep
->queue
.next
,
982 struct at91_request
, queue
);
987 if (csr
& (AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
)) {
989 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
);
990 __raw_writel(csr
, creg
);
993 return write_fifo(ep
, req
);
996 if (csr
& AT91_UDP_STALLSENT
) {
997 /* STALLSENT bit == ISOERR */
998 if (ep
->is_iso
&& req
)
999 req
->req
.status
= -EILSEQ
;
1001 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
);
1002 __raw_writel(csr
, creg
);
1003 csr
= __raw_readl(creg
);
1005 if (req
&& (csr
& RX_DATA_READY
))
1006 return read_fifo(ep
, req
);
1013 struct usb_ctrlrequest r
;
1016 static void handle_setup(struct at91_udc
*udc
, struct at91_ep
*ep
, u32 csr
)
1018 u32 __iomem
*creg
= ep
->creg
;
1019 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1020 unsigned rxcount
, i
= 0;
1025 /* read and ack SETUP; hard-fail for bogus packets */
1026 rxcount
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
1027 if (likely(rxcount
== 8)) {
1029 pkt
.raw
[i
++] = __raw_readb(dreg
);
1030 if (pkt
.r
.bRequestType
& USB_DIR_IN
) {
1031 csr
|= AT91_UDP_DIR
;
1034 csr
&= ~AT91_UDP_DIR
;
1038 // REVISIT this happens sometimes under load; why??
1039 ERR("SETUP len %d, csr %08x\n", rxcount
, csr
);
1043 csr
&= ~(SET_FX
| AT91_UDP_RXSETUP
);
1044 __raw_writel(csr
, creg
);
1045 udc
->wait_for_addr_ack
= 0;
1046 udc
->wait_for_config_ack
= 0;
1048 if (unlikely(status
!= 0))
1051 #define w_index le16_to_cpu(pkt.r.wIndex)
1052 #define w_value le16_to_cpu(pkt.r.wValue)
1053 #define w_length le16_to_cpu(pkt.r.wLength)
1055 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1056 pkt
.r
.bRequestType
, pkt
.r
.bRequest
,
1057 w_value
, w_index
, w_length
);
1060 * A few standard requests get handled here, ones that touch
1061 * hardware ... notably for device and endpoint features.
1063 udc
->req_pending
= 1;
1064 csr
= __raw_readl(creg
);
1067 switch ((pkt
.r
.bRequestType
<< 8) | pkt
.r
.bRequest
) {
1069 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1070 | USB_REQ_SET_ADDRESS
:
1071 __raw_writel(csr
| AT91_UDP_TXPKTRDY
, creg
);
1072 udc
->addr
= w_value
;
1073 udc
->wait_for_addr_ack
= 1;
1074 udc
->req_pending
= 0;
1075 /* FADDR is set later, when we ack host STATUS */
1078 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1079 | USB_REQ_SET_CONFIGURATION
:
1080 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
) & AT91_UDP_CONFG
;
1082 udc
->wait_for_config_ack
= (tmp
== 0);
1084 udc
->wait_for_config_ack
= (tmp
!= 0);
1085 if (udc
->wait_for_config_ack
)
1086 VDBG("wait for config\n");
1087 /* CONFG is toggled later, if gadget driver succeeds */
1091 * Hosts may set or clear remote wakeup status, and
1092 * devices may report they're VBUS powered.
1094 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1095 | USB_REQ_GET_STATUS
:
1096 tmp
= (udc
->selfpowered
<< USB_DEVICE_SELF_POWERED
);
1097 if (at91_udp_read(AT91_UDP_GLB_STAT
) & AT91_UDP_ESR
)
1098 tmp
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
1099 PACKET("get device status\n");
1100 __raw_writeb(tmp
, dreg
);
1101 __raw_writeb(0, dreg
);
1103 /* then STATUS starts later, automatically */
1104 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1105 | USB_REQ_SET_FEATURE
:
1106 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1108 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
1109 tmp
|= AT91_UDP_ESR
;
1110 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
1112 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1113 | USB_REQ_CLEAR_FEATURE
:
1114 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1116 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
1117 tmp
&= ~AT91_UDP_ESR
;
1118 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
1122 * Interfaces have no feature settings; this is pretty useless.
1123 * we won't even insist the interface exists...
1125 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1126 | USB_REQ_GET_STATUS
:
1127 PACKET("get interface status\n");
1128 __raw_writeb(0, dreg
);
1129 __raw_writeb(0, dreg
);
1131 /* then STATUS starts later, automatically */
1132 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1133 | USB_REQ_SET_FEATURE
:
1134 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1135 | USB_REQ_CLEAR_FEATURE
:
1139 * Hosts may clear bulk/intr endpoint halt after the gadget
1140 * driver sets it (not widely used); or set it (for testing)
1142 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1143 | USB_REQ_GET_STATUS
:
1144 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1146 if (tmp
> NUM_ENDPOINTS
|| (tmp
&& !ep
->desc
))
1150 if ((w_index
& USB_DIR_IN
)) {
1153 } else if (ep
->is_in
)
1156 PACKET("get %s status\n", ep
->ep
.name
);
1157 if (__raw_readl(ep
->creg
) & AT91_UDP_FORCESTALL
)
1158 tmp
= (1 << USB_ENDPOINT_HALT
);
1161 __raw_writeb(tmp
, dreg
);
1162 __raw_writeb(0, dreg
);
1164 /* then STATUS starts later, automatically */
1165 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1166 | USB_REQ_SET_FEATURE
:
1167 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1169 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
> NUM_ENDPOINTS
)
1171 if (!ep
->desc
|| ep
->is_iso
)
1173 if ((w_index
& USB_DIR_IN
)) {
1176 } else if (ep
->is_in
)
1179 tmp
= __raw_readl(ep
->creg
);
1181 tmp
|= CLR_FX
| AT91_UDP_FORCESTALL
;
1182 __raw_writel(tmp
, ep
->creg
);
1184 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1185 | USB_REQ_CLEAR_FEATURE
:
1186 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1188 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
> NUM_ENDPOINTS
)
1192 if (!ep
->desc
|| ep
->is_iso
)
1194 if ((w_index
& USB_DIR_IN
)) {
1197 } else if (ep
->is_in
)
1200 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
1201 at91_udp_write(AT91_UDP_RST_EP
, 0);
1202 tmp
= __raw_readl(ep
->creg
);
1204 tmp
&= ~(SET_FX
| AT91_UDP_FORCESTALL
);
1205 __raw_writel(tmp
, ep
->creg
);
1206 if (!list_empty(&ep
->queue
))
1215 /* pass request up to the gadget driver */
1216 status
= udc
->driver
->setup(&udc
->gadget
, &pkt
.r
);
1219 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1220 pkt
.r
.bRequestType
, pkt
.r
.bRequest
, status
);
1221 csr
|= AT91_UDP_FORCESTALL
;
1222 __raw_writel(csr
, creg
);
1223 udc
->req_pending
= 0;
1228 /* immediate successful (IN) STATUS after zero length DATA */
1229 PACKET("ep0 in/status\n");
1231 csr
|= AT91_UDP_TXPKTRDY
;
1232 __raw_writel(csr
, creg
);
1233 udc
->req_pending
= 0;
1237 static void handle_ep0(struct at91_udc
*udc
)
1239 struct at91_ep
*ep0
= &udc
->ep
[0];
1240 u32 __iomem
*creg
= ep0
->creg
;
1241 u32 csr
= __raw_readl(creg
);
1242 struct at91_request
*req
;
1244 if (unlikely(csr
& AT91_UDP_STALLSENT
)) {
1246 udc
->req_pending
= 0;
1248 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_FORCESTALL
);
1249 __raw_writel(csr
, creg
);
1250 VDBG("ep0 stalled\n");
1251 csr
= __raw_readl(creg
);
1253 if (csr
& AT91_UDP_RXSETUP
) {
1255 udc
->req_pending
= 0;
1256 handle_setup(udc
, ep0
, csr
);
1260 if (list_empty(&ep0
->queue
))
1263 req
= list_entry(ep0
->queue
.next
, struct at91_request
, queue
);
1265 /* host ACKed an IN packet that we sent */
1266 if (csr
& AT91_UDP_TXCOMP
) {
1268 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
1270 /* write more IN DATA? */
1271 if (req
&& ep0
->is_in
) {
1273 udc
->req_pending
= 0;
1277 * - last IN DATA packet (including GET_STATUS)
1278 * - IN/STATUS for OUT DATA
1279 * - IN/STATUS for any zero-length DATA stage
1280 * except for the IN DATA case, the host should send
1281 * an OUT status later, which we'll ack.
1284 udc
->req_pending
= 0;
1285 __raw_writel(csr
, creg
);
1288 * SET_ADDRESS takes effect only after the STATUS
1289 * (to the original address) gets acked.
1291 if (udc
->wait_for_addr_ack
) {
1294 at91_udp_write(AT91_UDP_FADDR
, AT91_UDP_FEN
| udc
->addr
);
1295 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
1296 tmp
&= ~AT91_UDP_FADDEN
;
1298 tmp
|= AT91_UDP_FADDEN
;
1299 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
1301 udc
->wait_for_addr_ack
= 0;
1302 VDBG("address %d\n", udc
->addr
);
1307 /* OUT packet arrived ... */
1308 else if (csr
& AT91_UDP_RX_DATA_BK0
) {
1310 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
1312 /* OUT DATA stage */
1315 if (handle_ep(ep0
)) {
1316 /* send IN/STATUS */
1317 PACKET("ep0 in/status\n");
1318 csr
= __raw_readl(creg
);
1320 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
1321 __raw_writel(csr
, creg
);
1322 udc
->req_pending
= 0;
1324 } else if (udc
->req_pending
) {
1326 * AT91 hardware has a hard time with this
1327 * "deferred response" mode for control-OUT
1328 * transfers. (For control-IN it's fine.)
1330 * The normal solution leaves OUT data in the
1331 * fifo until the gadget driver is ready.
1332 * We couldn't do that here without disabling
1333 * the IRQ that tells about SETUP packets,
1334 * e.g. when the host gets impatient...
1336 * Working around it by copying into a buffer
1337 * would almost be a non-deferred response,
1338 * except that it wouldn't permit reliable
1339 * stalling of the request. Instead, demand
1340 * that gadget drivers not use this mode.
1342 DBG("no control-OUT deferred responses!\n");
1343 __raw_writel(csr
| AT91_UDP_FORCESTALL
, creg
);
1344 udc
->req_pending
= 0;
1347 /* STATUS stage for control-IN; ack. */
1349 PACKET("ep0 out/status ACK\n");
1350 __raw_writel(csr
, creg
);
1352 /* "early" status stage */
1359 static irqreturn_t
at91_udc_irq (int irq
, void *_udc
, struct pt_regs
*r
)
1361 struct at91_udc
*udc
= _udc
;
1365 u32 status
= at91_udp_read(AT91_UDP_ISR
);
1367 status
&= at91_udp_read(AT91_UDP_IMR
);
1371 /* USB reset irq: not maskable */
1372 if (status
& AT91_UDP_ENDBUSRES
) {
1373 at91_udp_write(AT91_UDP_IDR
, ~MINIMUS_INTERRUPTUS
);
1374 at91_udp_write(AT91_UDP_IER
, MINIMUS_INTERRUPTUS
);
1375 /* Atmel code clears this irq twice */
1376 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1377 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1378 VDBG("end bus reset\n");
1383 at91_udp_write(AT91_UDP_CSR(0), AT91_UDP_EPEDS
| AT91_UDP_EPTYPE_CTRL
);
1384 udc
->gadget
.speed
= USB_SPEED_FULL
;
1386 at91_udp_write(AT91_UDP_IER
, AT91_UDP_EP(0));
1389 * NOTE: this driver keeps clocks off unless the
1390 * USB host is present. That saves power, and also
1391 * eliminates IRQs (reset, resume, suspend) that can
1392 * otherwise flood from the controller. If your
1393 * board doesn't support VBUS detection, suspend and
1394 * resume irq logic may need more attention...
1397 /* host initiated suspend (3+ms bus idle) */
1398 } else if (status
& AT91_UDP_RXSUSP
) {
1399 at91_udp_write(AT91_UDP_IDR
, AT91_UDP_RXSUSP
);
1400 at91_udp_write(AT91_UDP_IER
, AT91_UDP_RXRSM
);
1401 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_RXSUSP
);
1402 // VDBG("bus suspend\n");
1408 * NOTE: when suspending a VBUS-powered device, the
1409 * gadget driver should switch into slow clock mode
1410 * and then into standby to avoid drawing more than
1411 * 500uA power (2500uA for some high-power configs).
1413 if (udc
->driver
&& udc
->driver
->suspend
)
1414 udc
->driver
->suspend(&udc
->gadget
);
1416 /* host initiated resume */
1417 } else if (status
& AT91_UDP_RXRSM
) {
1418 at91_udp_write(AT91_UDP_IDR
, AT91_UDP_RXRSM
);
1419 at91_udp_write(AT91_UDP_IER
, AT91_UDP_RXSUSP
);
1420 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_RXRSM
);
1421 // VDBG("bus resume\n");
1422 if (!udc
->suspended
)
1427 * NOTE: for a VBUS-powered device, the gadget driver
1428 * would normally want to switch out of slow clock
1429 * mode into normal mode.
1431 if (udc
->driver
&& udc
->driver
->resume
)
1432 udc
->driver
->resume(&udc
->gadget
);
1434 /* endpoint IRQs are cleared by handling them */
1438 struct at91_ep
*ep
= &udc
->ep
[1];
1442 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1454 /*-------------------------------------------------------------------------*/
1456 static struct at91_udc controller
= {
1458 .ops
= &at91_udc_ops
,
1459 .ep0
= &controller
.ep
[0].ep
,
1460 .name
= driver_name
,
1468 .ops
= &at91_ep_ops
,
1472 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
+ AT91_UDP_CSR(0)),
1478 .ops
= &at91_ep_ops
,
1483 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
+ AT91_UDP_CSR(1)),
1489 .ops
= &at91_ep_ops
,
1494 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
+ AT91_UDP_CSR(2)),
1499 /* could actually do bulk too */
1501 .ops
= &at91_ep_ops
,
1505 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
+ AT91_UDP_CSR(3)),
1511 .ops
= &at91_ep_ops
,
1516 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
+ AT91_UDP_CSR(4)),
1522 .ops
= &at91_ep_ops
,
1527 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
+ AT91_UDP_CSR(5)),
1530 /* ep6 and ep7 are also reserved */
1533 static irqreturn_t
at91_vbus_irq(int irq
, void *_udc
, struct pt_regs
*r
)
1535 struct at91_udc
*udc
= _udc
;
1538 /* vbus needs at least brief debouncing */
1540 value
= at91_get_gpio_value(udc
->board
.vbus_pin
);
1541 if (value
!= udc
->vbus
)
1542 at91_vbus_session(&udc
->gadget
, value
);
1547 int usb_gadget_register_driver (struct usb_gadget_driver
*driver
)
1549 struct at91_udc
*udc
= &controller
;
1553 || driver
->speed
!= USB_SPEED_FULL
1556 || !driver
->setup
) {
1557 DBG("bad parameter.\n");
1562 DBG("UDC already has a gadget driver\n");
1566 udc
->driver
= driver
;
1567 udc
->gadget
.dev
.driver
= &driver
->driver
;
1568 udc
->gadget
.dev
.driver_data
= &driver
->driver
;
1570 udc
->selfpowered
= 1;
1572 retval
= driver
->bind(&udc
->gadget
);
1574 DBG("driver->bind() returned %d\n", retval
);
1579 local_irq_disable();
1583 DBG("bound to %s\n", driver
->driver
.name
);
1586 EXPORT_SYMBOL (usb_gadget_register_driver
);
1588 int usb_gadget_unregister_driver (struct usb_gadget_driver
*driver
)
1590 struct at91_udc
*udc
= &controller
;
1592 if (!driver
|| driver
!= udc
->driver
)
1595 local_irq_disable();
1600 driver
->unbind(&udc
->gadget
);
1603 DBG("unbound from %s\n", driver
->driver
.name
);
1606 EXPORT_SYMBOL (usb_gadget_unregister_driver
);
1608 /*-------------------------------------------------------------------------*/
1610 static void at91udc_shutdown(struct platform_device
*dev
)
1612 /* force disconnect on reboot */
1613 pullup(platform_get_drvdata(dev
), 0);
1616 static int __devinit
at91udc_probe(struct platform_device
*pdev
)
1618 struct device
*dev
= &pdev
->dev
;
1619 struct at91_udc
*udc
;
1622 if (!dev
->platform_data
) {
1623 /* small (so we copy it) but critical! */
1624 DBG("missing platform_data\n");
1628 if (!request_mem_region(AT91_BASE_UDP
, SZ_16K
, driver_name
)) {
1629 DBG("someone's using UDC memory\n");
1633 /* init software state */
1635 udc
->gadget
.dev
.parent
= dev
;
1636 udc
->board
= *(struct at91_udc_data
*) dev
->platform_data
;
1641 /* get interface and function clocks */
1642 udc
->iclk
= clk_get(dev
, "udc_clk");
1643 udc
->fclk
= clk_get(dev
, "udpck");
1644 if (IS_ERR(udc
->iclk
) || IS_ERR(udc
->fclk
)) {
1645 DBG("clocks missing\n");
1649 retval
= device_register(&udc
->gadget
.dev
);
1653 /* disable everything until there's a gadget driver and vbus */
1656 /* request UDC and maybe VBUS irqs */
1657 if (request_irq(AT91_ID_UDP
, at91_udc_irq
, SA_INTERRUPT
, driver_name
, udc
)) {
1658 DBG("request irq %d failed\n", AT91_ID_UDP
);
1662 if (udc
->board
.vbus_pin
> 0) {
1663 if (request_irq(udc
->board
.vbus_pin
, at91_vbus_irq
, SA_INTERRUPT
, driver_name
, udc
)) {
1664 DBG("request vbus irq %d failed\n", udc
->board
.vbus_pin
);
1665 free_irq(AT91_ID_UDP
, udc
);
1670 DBG("no VBUS detection, assuming always-on\n");
1673 dev_set_drvdata(dev
, udc
);
1674 create_debug_file(udc
);
1676 INFO("%s version %s\n", driver_name
, DRIVER_VERSION
);
1680 device_unregister(&udc
->gadget
.dev
);
1682 release_mem_region(AT91_VA_BASE_UDP
, SZ_16K
);
1683 DBG("%s probe failed, %d\n", driver_name
, retval
);
1687 static int __devexit
at91udc_remove(struct platform_device
*dev
)
1689 struct at91_udc
*udc
= platform_get_drvdata(dev
);
1695 if (udc
->driver
!= 0)
1696 usb_gadget_unregister_driver(udc
->driver
);
1698 remove_debug_file(udc
);
1699 if (udc
->board
.vbus_pin
> 0)
1700 free_irq(udc
->board
.vbus_pin
, udc
);
1701 free_irq(AT91_ID_UDP
, udc
);
1702 device_unregister(&udc
->gadget
.dev
);
1703 release_mem_region(AT91_BASE_UDP
, SZ_16K
);
1712 static int at91udc_suspend(struct platform_device
*dev
, pm_message_t mesg
)
1714 struct at91_udc
*udc
= platform_get_drvdata(dev
);
1717 * The "safe" suspend transitions are opportunistic ... e.g. when
1718 * the USB link is suspended (48MHz clock autogated off), or when
1719 * it's disconnected (programmatically gated off, elsewhere).
1720 * Then we can suspend, and the chip can enter slow clock mode.
1722 * The problem case is some component (user mode?) suspending this
1723 * device while it's active, with the 48 MHz clock in use. There
1724 * are two basic approaches: (a) veto suspend levels involving slow
1725 * clock mode, (b) disconnect, so 48 MHz will no longer be in use
1726 * and we can enter slow clock mode. This uses (b) for now, since
1727 * it's simplest until AT91 PM exists and supports the other option.
1729 if (udc
->vbus
&& !udc
->suspended
)
1734 static int at91udc_resume(struct platform_device
*dev
)
1736 struct at91_udc
*udc
= platform_get_drvdata(dev
);
1738 /* maybe reconnect to host; if so, clocks on */
1743 #define at91udc_suspend NULL
1744 #define at91udc_resume NULL
1747 static struct platform_driver at91_udc
= {
1748 .probe
= at91udc_probe
,
1749 .remove
= __devexit_p(at91udc_remove
),
1750 .shutdown
= at91udc_shutdown
,
1751 .suspend
= at91udc_suspend
,
1752 .resume
= at91udc_resume
,
1754 .name
= (char *) driver_name
,
1755 .owner
= THIS_MODULE
,
1759 static int __devinit
udc_init_module(void)
1761 return platform_driver_register(&at91_udc
);
1763 module_init(udc_init_module
);
1765 static void __devexit
udc_exit_module(void)
1767 platform_driver_unregister(&at91_udc
);
1769 module_exit(udc_exit_module
);
1771 MODULE_DESCRIPTION("AT91RM9200 udc driver");
1772 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1773 MODULE_LICENSE("GPL");