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/kernel.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/smp_lock.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/list.h>
39 #include <linux/interrupt.h>
40 #include <linux/proc_fs.h>
41 #include <linux/clk.h>
42 #include <linux/usb_ch9.h>
43 #include <linux/usb_gadget.h>
45 #include <asm/byteorder.h>
48 #include <asm/system.h>
49 #include <asm/mach-types.h>
51 #include <asm/arch/hardware.h>
52 #include <asm/arch/gpio.h>
53 #include <asm/arch/board.h>
59 * This controller is simple and PIO-only. It's used in many AT91-series
60 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
61 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
63 * This driver expects the board has been wired with two GPIOs suppporting
64 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
65 * testing hasn't covered such cases.)
67 * The pullup is most important (so it's integrated on sam926x parts). It
68 * provides software control over whether the host enumerates the device.
70 * The VBUS sensing helps during enumeration, and allows both USB clocks
71 * (and the transceiver) to stay gated off until they're necessary, saving
72 * power. During USB suspend, the 48 MHz clock is gated off in hardware;
73 * it may also be gated off by software during some Linux sleep states.
76 #define DRIVER_VERSION "3 May 2006"
78 static const char driver_name
[] = "at91_udc";
79 static const char ep0name
[] = "ep0";
81 /*-------------------------------------------------------------------------*/
84 * Read from a UDP register.
86 static inline unsigned long at91_udp_read(unsigned int reg
)
88 void __iomem
*udp_base
= (void __iomem
*)AT91_VA_BASE_UDP
;
90 return __raw_readl(udp_base
+ reg
);
94 * Write to a UDP register.
96 static inline void at91_udp_write(unsigned int reg
, unsigned long value
)
98 void __iomem
*udp_base
= (void __iomem
*)AT91_VA_BASE_UDP
;
100 __raw_writel(value
, udp_base
+ reg
);
103 /*-------------------------------------------------------------------------*/
105 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
107 #include <linux/seq_file.h>
109 static const char debug_filename
[] = "driver/udc";
111 #define FOURBITS "%s%s%s%s"
112 #define EIGHTBITS FOURBITS FOURBITS
114 static void proc_ep_show(struct seq_file
*s
, struct at91_ep
*ep
)
116 static char *types
[] = {
117 "control", "out-iso", "out-bulk", "out-int",
118 "BOGUS", "in-iso", "in-bulk", "in-int"};
121 struct at91_request
*req
;
124 local_irq_save(flags
);
126 csr
= __raw_readl(ep
->creg
);
128 /* NOTE: not collecting per-endpoint irq statistics... */
131 seq_printf(s
, "%s, maxpacket %d %s%s %s%s\n",
132 ep
->ep
.name
, ep
->ep
.maxpacket
,
133 ep
->is_in
? "in" : "out",
134 ep
->is_iso
? " iso" : "",
136 ? (ep
->fifo_bank
? "pong" : "ping")
138 ep
->stopped
? " stopped" : "");
139 seq_printf(s
, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS
"\n",
141 (csr
& 0x07ff0000) >> 16,
142 (csr
& (1 << 15)) ? "enabled" : "disabled",
143 (csr
& (1 << 11)) ? "DATA1" : "DATA0",
144 types
[(csr
& 0x700) >> 8],
146 /* iff type is control then print current direction */
148 ? ((csr
& (1 << 7)) ? " IN" : " OUT")
150 (csr
& (1 << 6)) ? " rxdatabk1" : "",
151 (csr
& (1 << 5)) ? " forcestall" : "",
152 (csr
& (1 << 4)) ? " txpktrdy" : "",
154 (csr
& (1 << 3)) ? " stallsent" : "",
155 (csr
& (1 << 2)) ? " rxsetup" : "",
156 (csr
& (1 << 1)) ? " rxdatabk0" : "",
157 (csr
& (1 << 0)) ? " txcomp" : "");
158 if (list_empty (&ep
->queue
))
159 seq_printf(s
, "\t(queue empty)\n");
161 else list_for_each_entry (req
, &ep
->queue
, queue
) {
162 unsigned length
= req
->req
.actual
;
164 seq_printf(s
, "\treq %p len %d/%d buf %p\n",
166 req
->req
.length
, req
->req
.buf
);
168 local_irq_restore(flags
);
171 static void proc_irq_show(struct seq_file
*s
, const char *label
, u32 mask
)
175 seq_printf(s
, "%s %04x:%s%s" FOURBITS
, label
, mask
,
176 (mask
& (1 << 13)) ? " wakeup" : "",
177 (mask
& (1 << 12)) ? " endbusres" : "",
179 (mask
& (1 << 11)) ? " sofint" : "",
180 (mask
& (1 << 10)) ? " extrsm" : "",
181 (mask
& (1 << 9)) ? " rxrsm" : "",
182 (mask
& (1 << 8)) ? " rxsusp" : "");
183 for (i
= 0; i
< 8; i
++) {
185 seq_printf(s
, " ep%d", i
);
190 static int proc_udc_show(struct seq_file
*s
, void *unused
)
192 struct at91_udc
*udc
= s
->private;
196 seq_printf(s
, "%s: version %s\n", driver_name
, DRIVER_VERSION
);
198 seq_printf(s
, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
199 udc
->vbus
? "present" : "off",
201 ? (udc
->vbus
? "active" : "enabled")
203 udc
->selfpowered
? "self" : "VBUS",
204 udc
->suspended
? ", suspended" : "",
205 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
207 /* don't access registers when interface isn't clocked */
209 seq_printf(s
, "(not clocked)\n");
213 tmp
= at91_udp_read(AT91_UDP_FRM_NUM
);
214 seq_printf(s
, "frame %05x:%s%s frame=%d\n", tmp
,
215 (tmp
& AT91_UDP_FRM_OK
) ? " ok" : "",
216 (tmp
& AT91_UDP_FRM_ERR
) ? " err" : "",
217 (tmp
& AT91_UDP_NUM
));
219 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
220 seq_printf(s
, "glbstate %02x:%s" FOURBITS
"\n", tmp
,
221 (tmp
& AT91_UDP_RMWUPE
) ? " rmwupe" : "",
222 (tmp
& AT91_UDP_RSMINPR
) ? " rsminpr" : "",
223 (tmp
& AT91_UDP_ESR
) ? " esr" : "",
224 (tmp
& AT91_UDP_CONFG
) ? " confg" : "",
225 (tmp
& AT91_UDP_FADDEN
) ? " fadden" : "");
227 tmp
= at91_udp_read(AT91_UDP_FADDR
);
228 seq_printf(s
, "faddr %03x:%s fadd=%d\n", tmp
,
229 (tmp
& AT91_UDP_FEN
) ? " fen" : "",
230 (tmp
& AT91_UDP_FADD
));
232 proc_irq_show(s
, "imr ", at91_udp_read(AT91_UDP_IMR
));
233 proc_irq_show(s
, "isr ", at91_udp_read(AT91_UDP_ISR
));
235 if (udc
->enabled
&& udc
->vbus
) {
236 proc_ep_show(s
, &udc
->ep
[0]);
237 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
245 static int proc_udc_open(struct inode
*inode
, struct file
*file
)
247 return single_open(file
, proc_udc_show
, PDE(inode
)->data
);
250 static const struct file_operations proc_ops
= {
251 .open
= proc_udc_open
,
254 .release
= single_release
,
257 static void create_debug_file(struct at91_udc
*udc
)
259 struct proc_dir_entry
*pde
;
261 pde
= create_proc_entry (debug_filename
, 0, NULL
);
266 pde
->proc_fops
= &proc_ops
;
270 static void remove_debug_file(struct at91_udc
*udc
)
273 remove_proc_entry(debug_filename
, NULL
);
278 static inline void create_debug_file(struct at91_udc
*udc
) {}
279 static inline void remove_debug_file(struct at91_udc
*udc
) {}
284 /*-------------------------------------------------------------------------*/
286 static void done(struct at91_ep
*ep
, struct at91_request
*req
, int status
)
288 unsigned stopped
= ep
->stopped
;
290 list_del_init(&req
->queue
);
291 if (req
->req
.status
== -EINPROGRESS
)
292 req
->req
.status
= status
;
294 status
= req
->req
.status
;
295 if (status
&& status
!= -ESHUTDOWN
)
296 VDBG("%s done %p, status %d\n", ep
->ep
.name
, req
, status
);
299 req
->req
.complete(&ep
->ep
, &req
->req
);
300 ep
->stopped
= stopped
;
302 /* ep0 is always ready; other endpoints need a non-empty queue */
303 if (list_empty(&ep
->queue
) && ep
->int_mask
!= (1 << 0))
304 at91_udp_write(AT91_UDP_IDR
, ep
->int_mask
);
307 /*-------------------------------------------------------------------------*/
309 /* bits indicating OUT fifo has data ready */
310 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
313 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
314 * back most of the value you just read (because of side effects, including
315 * bits that may change after reading and before writing).
317 * Except when changing a specific bit, always write values which:
318 * - clear SET_FX bits (setting them could change something)
319 * - set CLR_FX bits (clearing them could change something)
321 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
322 * that shouldn't normally be changed.
324 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
325 * implying a need to wait for one write to complete (test relevant bits)
326 * before starting the next write. This shouldn't be an issue given how
327 * infrequently we write, except maybe for write-then-read idioms.
329 #define SET_FX (AT91_UDP_TXPKTRDY)
330 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
331 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
333 /* pull OUT packet data from the endpoint's fifo */
334 static int read_fifo (struct at91_ep
*ep
, struct at91_request
*req
)
336 u32 __iomem
*creg
= ep
->creg
;
337 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
340 unsigned int count
, bufferspace
, is_done
;
342 buf
= req
->req
.buf
+ req
->req
.actual
;
343 bufferspace
= req
->req
.length
- req
->req
.actual
;
346 * there might be nothing to read if ep_queue() calls us,
347 * or if we already emptied both pingpong buffers
350 csr
= __raw_readl(creg
);
351 if ((csr
& RX_DATA_READY
) == 0)
354 count
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
355 if (count
> ep
->ep
.maxpacket
)
356 count
= ep
->ep
.maxpacket
;
357 if (count
> bufferspace
) {
358 DBG("%s buffer overflow\n", ep
->ep
.name
);
359 req
->req
.status
= -EOVERFLOW
;
362 __raw_readsb(dreg
, buf
, count
);
364 /* release and swap pingpong mem bank */
366 if (ep
->is_pingpong
) {
367 if (ep
->fifo_bank
== 0) {
368 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
371 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK1
);
375 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
376 __raw_writel(csr
, creg
);
378 req
->req
.actual
+= count
;
379 is_done
= (count
< ep
->ep
.maxpacket
);
380 if (count
== bufferspace
)
383 PACKET("%s %p out/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
384 is_done
? " (done)" : "");
387 * avoid extra trips through IRQ logic for packets already in
388 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
392 else if (ep
->is_pingpong
) {
393 bufferspace
-= count
;
401 /* load fifo for an IN packet */
402 static int write_fifo(struct at91_ep
*ep
, struct at91_request
*req
)
404 u32 __iomem
*creg
= ep
->creg
;
405 u32 csr
= __raw_readl(creg
);
406 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
407 unsigned total
, count
, is_last
;
410 * TODO: allow for writing two packets to the fifo ... that'll
411 * reduce the amount of IN-NAKing, but probably won't affect
412 * throughput much. (Unlike preventing OUT-NAKing!)
416 * If ep_queue() calls us, the queue is empty and possibly in
417 * odd states like TXCOMP not yet cleared (we do it, saving at
418 * least one IRQ) or the fifo not yet being free. Those aren't
419 * issues normally (IRQ handler fast path).
421 if (unlikely(csr
& (AT91_UDP_TXCOMP
| AT91_UDP_TXPKTRDY
))) {
422 if (csr
& AT91_UDP_TXCOMP
) {
424 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
425 __raw_writel(csr
, creg
);
426 csr
= __raw_readl(creg
);
428 if (csr
& AT91_UDP_TXPKTRDY
)
432 total
= req
->req
.length
- req
->req
.actual
;
433 if (ep
->ep
.maxpacket
< total
) {
434 count
= ep
->ep
.maxpacket
;
438 is_last
= (count
< ep
->ep
.maxpacket
) || !req
->req
.zero
;
442 * Write the packet, maybe it's a ZLP.
444 * NOTE: incrementing req->actual before we receive the ACK means
445 * gadget driver IN bytecounts can be wrong in fault cases. That's
446 * fixable with PIO drivers like this one (save "count" here, and
447 * do the increment later on TX irq), but not for most DMA hardware.
449 * So all gadget drivers must accept that potential error. Some
450 * hardware supports precise fifo status reporting, letting them
451 * recover when the actual bytecount matters (e.g. for USB Test
452 * and Measurement Class devices).
454 __raw_writesb(dreg
, req
->req
.buf
+ req
->req
.actual
, count
);
456 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
457 __raw_writel(csr
, creg
);
458 req
->req
.actual
+= count
;
460 PACKET("%s %p in/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
461 is_last
? " (done)" : "");
467 static void nuke(struct at91_ep
*ep
, int status
)
469 struct at91_request
*req
;
471 // terminer chaque requete dans la queue
473 if (list_empty(&ep
->queue
))
476 VDBG("%s %s\n", __FUNCTION__
, ep
->ep
.name
);
477 while (!list_empty(&ep
->queue
)) {
478 req
= list_entry(ep
->queue
.next
, struct at91_request
, queue
);
479 done(ep
, req
, status
);
483 /*-------------------------------------------------------------------------*/
485 static int at91_ep_enable(struct usb_ep
*_ep
,
486 const struct usb_endpoint_descriptor
*desc
)
488 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
489 struct at91_udc
*dev
= ep
->udc
;
496 || _ep
->name
== ep0name
497 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
498 || (maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
)) == 0
499 || maxpacket
> ep
->maxpacket
) {
500 DBG("bad ep or descriptor\n");
504 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
505 DBG("bogus device state\n");
509 tmp
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
511 case USB_ENDPOINT_XFER_CONTROL
:
512 DBG("only one control endpoint\n");
514 case USB_ENDPOINT_XFER_INT
:
518 case USB_ENDPOINT_XFER_BULK
:
527 DBG("bogus maxpacket %d\n", maxpacket
);
529 case USB_ENDPOINT_XFER_ISOC
:
530 if (!ep
->is_pingpong
) {
531 DBG("iso requires double buffering\n");
538 local_irq_save(flags
);
540 /* initialize endpoint to match this descriptor */
541 ep
->is_in
= (desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
542 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
);
547 tmp
|= AT91_UDP_EPEDS
;
548 __raw_writel(tmp
, ep
->creg
);
551 ep
->ep
.maxpacket
= maxpacket
;
554 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
555 * since endpoint resets don't reset hw pingpong state.
557 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
558 at91_udp_write(AT91_UDP_RST_EP
, 0);
560 local_irq_restore(flags
);
564 static int at91_ep_disable (struct usb_ep
* _ep
)
566 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
569 if (ep
== &ep
->udc
->ep
[0])
572 local_irq_save(flags
);
574 nuke(ep
, -ESHUTDOWN
);
576 /* restore the endpoint's pristine config */
578 ep
->ep
.maxpacket
= ep
->maxpacket
;
580 /* reset fifos and endpoint */
581 if (ep
->udc
->clocked
) {
582 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
583 at91_udp_write(AT91_UDP_RST_EP
, 0);
584 __raw_writel(0, ep
->creg
);
587 local_irq_restore(flags
);
592 * this is a PIO-only driver, so there's nothing
593 * interesting for request or buffer allocation.
596 static struct usb_request
*
597 at91_ep_alloc_request(struct usb_ep
*_ep
, unsigned int gfp_flags
)
599 struct at91_request
*req
;
601 req
= kcalloc(1, sizeof (struct at91_request
), gfp_flags
);
605 INIT_LIST_HEAD(&req
->queue
);
609 static void at91_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
611 struct at91_request
*req
;
613 req
= container_of(_req
, struct at91_request
, req
);
614 BUG_ON(!list_empty(&req
->queue
));
618 static void *at91_ep_alloc_buffer(
625 return kmalloc(bytes
, gfp_flags
);
628 static void at91_ep_free_buffer(
637 static int at91_ep_queue(struct usb_ep
*_ep
,
638 struct usb_request
*_req
, gfp_t gfp_flags
)
640 struct at91_request
*req
;
642 struct at91_udc
*dev
;
646 req
= container_of(_req
, struct at91_request
, req
);
647 ep
= container_of(_ep
, struct at91_ep
, ep
);
649 if (!_req
|| !_req
->complete
650 || !_req
->buf
|| !list_empty(&req
->queue
)) {
651 DBG("invalid request\n");
655 if (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
)) {
662 if (!dev
|| !dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
663 DBG("invalid device\n");
667 _req
->status
= -EINPROGRESS
;
670 local_irq_save(flags
);
672 /* try to kickstart any empty and idle queue */
673 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
677 * If this control request has a non-empty DATA stage, this
678 * will start that stage. It works just like a non-control
679 * request (until the status stage starts, maybe early).
681 * If the data stage is empty, then this starts a successful
682 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
684 is_ep0
= (ep
->ep
.name
== ep0name
);
688 if (!dev
->req_pending
) {
694 * defer changing CONFG until after the gadget driver
695 * reconfigures the endpoints.
697 if (dev
->wait_for_config_ack
) {
698 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
699 tmp
^= AT91_UDP_CONFG
;
700 VDBG("toggle config\n");
701 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
703 if (req
->req
.length
== 0) {
705 PACKET("ep0 in/status\n");
707 tmp
= __raw_readl(ep
->creg
);
709 tmp
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
710 __raw_writel(tmp
, ep
->creg
);
711 dev
->req_pending
= 0;
717 status
= write_fifo(ep
, req
);
719 status
= read_fifo(ep
, req
);
721 /* IN/STATUS stage is otherwise triggered by irq */
722 if (status
&& is_ep0
)
728 if (req
&& !status
) {
729 list_add_tail (&req
->queue
, &ep
->queue
);
730 at91_udp_write(AT91_UDP_IER
, ep
->int_mask
);
733 local_irq_restore(flags
);
734 return (status
< 0) ? status
: 0;
737 static int at91_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
740 struct at91_request
*req
;
742 ep
= container_of(_ep
, struct at91_ep
, ep
);
743 if (!_ep
|| ep
->ep
.name
== ep0name
)
746 /* make sure it's actually queued on this endpoint */
747 list_for_each_entry (req
, &ep
->queue
, queue
) {
748 if (&req
->req
== _req
)
751 if (&req
->req
!= _req
)
754 done(ep
, req
, -ECONNRESET
);
758 static int at91_ep_set_halt(struct usb_ep
*_ep
, int value
)
760 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
766 if (!_ep
|| ep
->is_iso
|| !ep
->udc
->clocked
)
770 local_irq_save(flags
);
772 csr
= __raw_readl(creg
);
775 * fail with still-busy IN endpoints, ensuring correct sequencing
776 * of data tx then stall. note that the fifo rx bytecount isn't
777 * completely accurate as a tx bytecount.
779 if (ep
->is_in
&& (!list_empty(&ep
->queue
) || (csr
>> 16) != 0))
785 csr
|= AT91_UDP_FORCESTALL
;
786 VDBG("halt %s\n", ep
->ep
.name
);
788 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
789 at91_udp_write(AT91_UDP_RST_EP
, 0);
790 csr
&= ~AT91_UDP_FORCESTALL
;
792 __raw_writel(csr
, creg
);
795 local_irq_restore(flags
);
799 static struct usb_ep_ops at91_ep_ops
= {
800 .enable
= at91_ep_enable
,
801 .disable
= at91_ep_disable
,
802 .alloc_request
= at91_ep_alloc_request
,
803 .free_request
= at91_ep_free_request
,
804 .alloc_buffer
= at91_ep_alloc_buffer
,
805 .free_buffer
= at91_ep_free_buffer
,
806 .queue
= at91_ep_queue
,
807 .dequeue
= at91_ep_dequeue
,
808 .set_halt
= at91_ep_set_halt
,
809 // there's only imprecise fifo status reporting
812 /*-------------------------------------------------------------------------*/
814 static int at91_get_frame(struct usb_gadget
*gadget
)
816 if (!to_udc(gadget
)->clocked
)
818 return at91_udp_read(AT91_UDP_FRM_NUM
) & AT91_UDP_NUM
;
821 static int at91_wakeup(struct usb_gadget
*gadget
)
823 struct at91_udc
*udc
= to_udc(gadget
);
825 int status
= -EINVAL
;
828 DBG("%s\n", __FUNCTION__
);
829 local_irq_save(flags
);
831 if (!udc
->clocked
|| !udc
->suspended
)
834 /* NOTE: some "early versions" handle ESR differently ... */
836 glbstate
= at91_udp_read(AT91_UDP_GLB_STAT
);
837 if (!(glbstate
& AT91_UDP_ESR
))
839 glbstate
|= AT91_UDP_ESR
;
840 at91_udp_write(AT91_UDP_GLB_STAT
, glbstate
);
843 local_irq_restore(flags
);
847 /* reinit == restore inital software state */
848 static void udc_reinit(struct at91_udc
*udc
)
852 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
853 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
855 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
856 struct at91_ep
*ep
= &udc
->ep
[i
];
859 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
863 ep
->ep
.maxpacket
= ep
->maxpacket
;
864 // initialiser une queue par endpoint
865 INIT_LIST_HEAD(&ep
->queue
);
869 static void stop_activity(struct at91_udc
*udc
)
871 struct usb_gadget_driver
*driver
= udc
->driver
;
874 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
876 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
879 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
880 struct at91_ep
*ep
= &udc
->ep
[i
];
882 nuke(ep
, -ESHUTDOWN
);
885 driver
->disconnect(&udc
->gadget
);
890 static void clk_on(struct at91_udc
*udc
)
895 clk_enable(udc
->iclk
);
896 clk_enable(udc
->fclk
);
899 static void clk_off(struct at91_udc
*udc
)
904 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
905 clk_disable(udc
->fclk
);
906 clk_disable(udc
->iclk
);
910 * activate/deactivate link with host; minimize power usage for
911 * inactive links by cutting clocks and transceiver power.
913 static void pullup(struct at91_udc
*udc
, int is_on
)
915 if (!udc
->enabled
|| !udc
->vbus
)
917 DBG("%sactive\n", is_on
? "" : "in");
920 at91_udp_write(AT91_UDP_TXVC
, 0);
921 at91_set_gpio_value(udc
->board
.pullup_pin
, 1);
924 at91_udp_write(AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
925 at91_set_gpio_value(udc
->board
.pullup_pin
, 0);
930 /* vbus is here! turn everything on that's ready */
931 static int at91_vbus_session(struct usb_gadget
*gadget
, int is_active
)
933 struct at91_udc
*udc
= to_udc(gadget
);
936 // VDBG("vbus %s\n", is_active ? "on" : "off");
937 local_irq_save(flags
);
938 udc
->vbus
= (is_active
!= 0);
939 pullup(udc
, is_active
);
940 local_irq_restore(flags
);
944 static int at91_pullup(struct usb_gadget
*gadget
, int is_on
)
946 struct at91_udc
*udc
= to_udc(gadget
);
949 local_irq_save(flags
);
950 udc
->enabled
= is_on
= !!is_on
;
952 local_irq_restore(flags
);
956 static int at91_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
958 struct at91_udc
*udc
= to_udc(gadget
);
961 local_irq_save(flags
);
962 udc
->selfpowered
= (is_on
!= 0);
963 local_irq_restore(flags
);
967 static const struct usb_gadget_ops at91_udc_ops
= {
968 .get_frame
= at91_get_frame
,
969 .wakeup
= at91_wakeup
,
970 .set_selfpowered
= at91_set_selfpowered
,
971 .vbus_session
= at91_vbus_session
,
972 .pullup
= at91_pullup
,
975 * VBUS-powered devices may also also want to support bigger
976 * power budgets after an appropriate SET_CONFIGURATION.
978 // .vbus_power = at91_vbus_power,
981 /*-------------------------------------------------------------------------*/
983 static int handle_ep(struct at91_ep
*ep
)
985 struct at91_request
*req
;
986 u32 __iomem
*creg
= ep
->creg
;
987 u32 csr
= __raw_readl(creg
);
989 if (!list_empty(&ep
->queue
))
990 req
= list_entry(ep
->queue
.next
,
991 struct at91_request
, queue
);
996 if (csr
& (AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
)) {
998 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
);
999 __raw_writel(csr
, creg
);
1002 return write_fifo(ep
, req
);
1005 if (csr
& AT91_UDP_STALLSENT
) {
1006 /* STALLSENT bit == ISOERR */
1007 if (ep
->is_iso
&& req
)
1008 req
->req
.status
= -EILSEQ
;
1010 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
);
1011 __raw_writel(csr
, creg
);
1012 csr
= __raw_readl(creg
);
1014 if (req
&& (csr
& RX_DATA_READY
))
1015 return read_fifo(ep
, req
);
1022 struct usb_ctrlrequest r
;
1025 static void handle_setup(struct at91_udc
*udc
, struct at91_ep
*ep
, u32 csr
)
1027 u32 __iomem
*creg
= ep
->creg
;
1028 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1029 unsigned rxcount
, i
= 0;
1034 /* read and ack SETUP; hard-fail for bogus packets */
1035 rxcount
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
1036 if (likely(rxcount
== 8)) {
1038 pkt
.raw
[i
++] = __raw_readb(dreg
);
1039 if (pkt
.r
.bRequestType
& USB_DIR_IN
) {
1040 csr
|= AT91_UDP_DIR
;
1043 csr
&= ~AT91_UDP_DIR
;
1047 // REVISIT this happens sometimes under load; why??
1048 ERR("SETUP len %d, csr %08x\n", rxcount
, csr
);
1052 csr
&= ~(SET_FX
| AT91_UDP_RXSETUP
);
1053 __raw_writel(csr
, creg
);
1054 udc
->wait_for_addr_ack
= 0;
1055 udc
->wait_for_config_ack
= 0;
1057 if (unlikely(status
!= 0))
1060 #define w_index le16_to_cpu(pkt.r.wIndex)
1061 #define w_value le16_to_cpu(pkt.r.wValue)
1062 #define w_length le16_to_cpu(pkt.r.wLength)
1064 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1065 pkt
.r
.bRequestType
, pkt
.r
.bRequest
,
1066 w_value
, w_index
, w_length
);
1069 * A few standard requests get handled here, ones that touch
1070 * hardware ... notably for device and endpoint features.
1072 udc
->req_pending
= 1;
1073 csr
= __raw_readl(creg
);
1076 switch ((pkt
.r
.bRequestType
<< 8) | pkt
.r
.bRequest
) {
1078 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1079 | USB_REQ_SET_ADDRESS
:
1080 __raw_writel(csr
| AT91_UDP_TXPKTRDY
, creg
);
1081 udc
->addr
= w_value
;
1082 udc
->wait_for_addr_ack
= 1;
1083 udc
->req_pending
= 0;
1084 /* FADDR is set later, when we ack host STATUS */
1087 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1088 | USB_REQ_SET_CONFIGURATION
:
1089 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
) & AT91_UDP_CONFG
;
1091 udc
->wait_for_config_ack
= (tmp
== 0);
1093 udc
->wait_for_config_ack
= (tmp
!= 0);
1094 if (udc
->wait_for_config_ack
)
1095 VDBG("wait for config\n");
1096 /* CONFG is toggled later, if gadget driver succeeds */
1100 * Hosts may set or clear remote wakeup status, and
1101 * devices may report they're VBUS powered.
1103 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1104 | USB_REQ_GET_STATUS
:
1105 tmp
= (udc
->selfpowered
<< USB_DEVICE_SELF_POWERED
);
1106 if (at91_udp_read(AT91_UDP_GLB_STAT
) & AT91_UDP_ESR
)
1107 tmp
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
1108 PACKET("get device status\n");
1109 __raw_writeb(tmp
, dreg
);
1110 __raw_writeb(0, dreg
);
1112 /* then STATUS starts later, automatically */
1113 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1114 | USB_REQ_SET_FEATURE
:
1115 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1117 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
1118 tmp
|= AT91_UDP_ESR
;
1119 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
1121 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1122 | USB_REQ_CLEAR_FEATURE
:
1123 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1125 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
1126 tmp
&= ~AT91_UDP_ESR
;
1127 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
1131 * Interfaces have no feature settings; this is pretty useless.
1132 * we won't even insist the interface exists...
1134 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1135 | USB_REQ_GET_STATUS
:
1136 PACKET("get interface status\n");
1137 __raw_writeb(0, dreg
);
1138 __raw_writeb(0, dreg
);
1140 /* then STATUS starts later, automatically */
1141 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1142 | USB_REQ_SET_FEATURE
:
1143 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1144 | USB_REQ_CLEAR_FEATURE
:
1148 * Hosts may clear bulk/intr endpoint halt after the gadget
1149 * driver sets it (not widely used); or set it (for testing)
1151 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1152 | USB_REQ_GET_STATUS
:
1153 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1155 if (tmp
> NUM_ENDPOINTS
|| (tmp
&& !ep
->desc
))
1159 if ((w_index
& USB_DIR_IN
)) {
1162 } else if (ep
->is_in
)
1165 PACKET("get %s status\n", ep
->ep
.name
);
1166 if (__raw_readl(ep
->creg
) & AT91_UDP_FORCESTALL
)
1167 tmp
= (1 << USB_ENDPOINT_HALT
);
1170 __raw_writeb(tmp
, dreg
);
1171 __raw_writeb(0, dreg
);
1173 /* then STATUS starts later, automatically */
1174 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1175 | USB_REQ_SET_FEATURE
:
1176 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1178 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
> NUM_ENDPOINTS
)
1180 if (!ep
->desc
|| ep
->is_iso
)
1182 if ((w_index
& USB_DIR_IN
)) {
1185 } else if (ep
->is_in
)
1188 tmp
= __raw_readl(ep
->creg
);
1190 tmp
|= CLR_FX
| AT91_UDP_FORCESTALL
;
1191 __raw_writel(tmp
, ep
->creg
);
1193 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1194 | USB_REQ_CLEAR_FEATURE
:
1195 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1197 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
> NUM_ENDPOINTS
)
1201 if (!ep
->desc
|| ep
->is_iso
)
1203 if ((w_index
& USB_DIR_IN
)) {
1206 } else if (ep
->is_in
)
1209 at91_udp_write(AT91_UDP_RST_EP
, ep
->int_mask
);
1210 at91_udp_write(AT91_UDP_RST_EP
, 0);
1211 tmp
= __raw_readl(ep
->creg
);
1213 tmp
&= ~(SET_FX
| AT91_UDP_FORCESTALL
);
1214 __raw_writel(tmp
, ep
->creg
);
1215 if (!list_empty(&ep
->queue
))
1224 /* pass request up to the gadget driver */
1225 status
= udc
->driver
->setup(&udc
->gadget
, &pkt
.r
);
1228 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1229 pkt
.r
.bRequestType
, pkt
.r
.bRequest
, status
);
1230 csr
|= AT91_UDP_FORCESTALL
;
1231 __raw_writel(csr
, creg
);
1232 udc
->req_pending
= 0;
1237 /* immediate successful (IN) STATUS after zero length DATA */
1238 PACKET("ep0 in/status\n");
1240 csr
|= AT91_UDP_TXPKTRDY
;
1241 __raw_writel(csr
, creg
);
1242 udc
->req_pending
= 0;
1246 static void handle_ep0(struct at91_udc
*udc
)
1248 struct at91_ep
*ep0
= &udc
->ep
[0];
1249 u32 __iomem
*creg
= ep0
->creg
;
1250 u32 csr
= __raw_readl(creg
);
1251 struct at91_request
*req
;
1253 if (unlikely(csr
& AT91_UDP_STALLSENT
)) {
1255 udc
->req_pending
= 0;
1257 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_FORCESTALL
);
1258 __raw_writel(csr
, creg
);
1259 VDBG("ep0 stalled\n");
1260 csr
= __raw_readl(creg
);
1262 if (csr
& AT91_UDP_RXSETUP
) {
1264 udc
->req_pending
= 0;
1265 handle_setup(udc
, ep0
, csr
);
1269 if (list_empty(&ep0
->queue
))
1272 req
= list_entry(ep0
->queue
.next
, struct at91_request
, queue
);
1274 /* host ACKed an IN packet that we sent */
1275 if (csr
& AT91_UDP_TXCOMP
) {
1277 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
1279 /* write more IN DATA? */
1280 if (req
&& ep0
->is_in
) {
1282 udc
->req_pending
= 0;
1286 * - last IN DATA packet (including GET_STATUS)
1287 * - IN/STATUS for OUT DATA
1288 * - IN/STATUS for any zero-length DATA stage
1289 * except for the IN DATA case, the host should send
1290 * an OUT status later, which we'll ack.
1293 udc
->req_pending
= 0;
1294 __raw_writel(csr
, creg
);
1297 * SET_ADDRESS takes effect only after the STATUS
1298 * (to the original address) gets acked.
1300 if (udc
->wait_for_addr_ack
) {
1303 at91_udp_write(AT91_UDP_FADDR
,
1304 AT91_UDP_FEN
| udc
->addr
);
1305 tmp
= at91_udp_read(AT91_UDP_GLB_STAT
);
1306 tmp
&= ~AT91_UDP_FADDEN
;
1308 tmp
|= AT91_UDP_FADDEN
;
1309 at91_udp_write(AT91_UDP_GLB_STAT
, tmp
);
1311 udc
->wait_for_addr_ack
= 0;
1312 VDBG("address %d\n", udc
->addr
);
1317 /* OUT packet arrived ... */
1318 else if (csr
& AT91_UDP_RX_DATA_BK0
) {
1320 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
1322 /* OUT DATA stage */
1325 if (handle_ep(ep0
)) {
1326 /* send IN/STATUS */
1327 PACKET("ep0 in/status\n");
1328 csr
= __raw_readl(creg
);
1330 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
1331 __raw_writel(csr
, creg
);
1332 udc
->req_pending
= 0;
1334 } else if (udc
->req_pending
) {
1336 * AT91 hardware has a hard time with this
1337 * "deferred response" mode for control-OUT
1338 * transfers. (For control-IN it's fine.)
1340 * The normal solution leaves OUT data in the
1341 * fifo until the gadget driver is ready.
1342 * We couldn't do that here without disabling
1343 * the IRQ that tells about SETUP packets,
1344 * e.g. when the host gets impatient...
1346 * Working around it by copying into a buffer
1347 * would almost be a non-deferred response,
1348 * except that it wouldn't permit reliable
1349 * stalling of the request. Instead, demand
1350 * that gadget drivers not use this mode.
1352 DBG("no control-OUT deferred responses!\n");
1353 __raw_writel(csr
| AT91_UDP_FORCESTALL
, creg
);
1354 udc
->req_pending
= 0;
1357 /* STATUS stage for control-IN; ack. */
1359 PACKET("ep0 out/status ACK\n");
1360 __raw_writel(csr
, creg
);
1362 /* "early" status stage */
1369 static irqreturn_t
at91_udc_irq (int irq
, void *_udc
)
1371 struct at91_udc
*udc
= _udc
;
1377 status
= at91_udp_read(AT91_UDP_ISR
)
1378 & at91_udp_read(AT91_UDP_IMR
);
1382 /* USB reset irq: not maskable */
1383 if (status
& AT91_UDP_ENDBUSRES
) {
1384 at91_udp_write(AT91_UDP_IDR
, ~MINIMUS_INTERRUPTUS
);
1385 at91_udp_write(AT91_UDP_IER
, MINIMUS_INTERRUPTUS
);
1386 /* Atmel code clears this irq twice */
1387 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1388 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1389 VDBG("end bus reset\n");
1394 at91_udp_write(AT91_UDP_CSR(0),
1395 AT91_UDP_EPEDS
| AT91_UDP_EPTYPE_CTRL
);
1396 udc
->gadget
.speed
= USB_SPEED_FULL
;
1398 at91_udp_write(AT91_UDP_IER
, AT91_UDP_EP(0));
1401 * NOTE: this driver keeps clocks off unless the
1402 * USB host is present. That saves power, but for
1403 * boards that don't support VBUS detection, both
1404 * clocks need to be active most of the time.
1407 /* host initiated suspend (3+ms bus idle) */
1408 } else if (status
& AT91_UDP_RXSUSP
) {
1409 at91_udp_write(AT91_UDP_IDR
, AT91_UDP_RXSUSP
);
1410 at91_udp_write(AT91_UDP_IER
, AT91_UDP_RXRSM
);
1411 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_RXSUSP
);
1412 // VDBG("bus suspend\n");
1418 * NOTE: when suspending a VBUS-powered device, the
1419 * gadget driver should switch into slow clock mode
1420 * and then into standby to avoid drawing more than
1421 * 500uA power (2500uA for some high-power configs).
1423 if (udc
->driver
&& udc
->driver
->suspend
)
1424 udc
->driver
->suspend(&udc
->gadget
);
1426 /* host initiated resume */
1427 } else if (status
& AT91_UDP_RXRSM
) {
1428 at91_udp_write(AT91_UDP_IDR
, AT91_UDP_RXRSM
);
1429 at91_udp_write(AT91_UDP_IER
, AT91_UDP_RXSUSP
);
1430 at91_udp_write(AT91_UDP_ICR
, AT91_UDP_RXRSM
);
1431 // VDBG("bus resume\n");
1432 if (!udc
->suspended
)
1437 * NOTE: for a VBUS-powered device, the gadget driver
1438 * would normally want to switch out of slow clock
1439 * mode into normal mode.
1441 if (udc
->driver
&& udc
->driver
->resume
)
1442 udc
->driver
->resume(&udc
->gadget
);
1444 /* endpoint IRQs are cleared by handling them */
1448 struct at91_ep
*ep
= &udc
->ep
[1];
1452 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1464 /*-------------------------------------------------------------------------*/
1466 static void nop_release(struct device
*dev
)
1468 /* nothing to free */
1471 static struct at91_udc controller
= {
1473 .ops
= &at91_udc_ops
,
1474 .ep0
= &controller
.ep
[0].ep
,
1475 .name
= driver_name
,
1478 .release
= nop_release
,
1484 .ops
= &at91_ep_ops
,
1488 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
1495 .ops
= &at91_ep_ops
,
1500 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
1507 .ops
= &at91_ep_ops
,
1512 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
1518 /* could actually do bulk too */
1520 .ops
= &at91_ep_ops
,
1524 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
1531 .ops
= &at91_ep_ops
,
1536 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
1543 .ops
= &at91_ep_ops
,
1548 .creg
= (void __iomem
*)(AT91_VA_BASE_UDP
1552 /* ep6 and ep7 are also reserved (custom silicon might use them) */
1555 static irqreturn_t
at91_vbus_irq(int irq
, void *_udc
)
1557 struct at91_udc
*udc
= _udc
;
1560 /* vbus needs at least brief debouncing */
1562 value
= at91_get_gpio_value(udc
->board
.vbus_pin
);
1563 if (value
!= udc
->vbus
)
1564 at91_vbus_session(&udc
->gadget
, value
);
1569 int usb_gadget_register_driver (struct usb_gadget_driver
*driver
)
1571 struct at91_udc
*udc
= &controller
;
1575 || driver
->speed
!= USB_SPEED_FULL
1578 || !driver
->setup
) {
1579 DBG("bad parameter.\n");
1584 DBG("UDC already has a gadget driver\n");
1588 udc
->driver
= driver
;
1589 udc
->gadget
.dev
.driver
= &driver
->driver
;
1590 udc
->gadget
.dev
.driver_data
= &driver
->driver
;
1592 udc
->selfpowered
= 1;
1594 retval
= driver
->bind(&udc
->gadget
);
1596 DBG("driver->bind() returned %d\n", retval
);
1601 local_irq_disable();
1605 DBG("bound to %s\n", driver
->driver
.name
);
1608 EXPORT_SYMBOL (usb_gadget_register_driver
);
1610 int usb_gadget_unregister_driver (struct usb_gadget_driver
*driver
)
1612 struct at91_udc
*udc
= &controller
;
1614 if (!driver
|| driver
!= udc
->driver
)
1617 local_irq_disable();
1619 at91_udp_write(AT91_UDP_IDR
, ~0);
1623 driver
->unbind(&udc
->gadget
);
1626 DBG("unbound from %s\n", driver
->driver
.name
);
1629 EXPORT_SYMBOL (usb_gadget_unregister_driver
);
1631 /*-------------------------------------------------------------------------*/
1633 static void at91udc_shutdown(struct platform_device
*dev
)
1635 /* force disconnect on reboot */
1636 pullup(platform_get_drvdata(dev
), 0);
1639 static int __devinit
at91udc_probe(struct platform_device
*pdev
)
1641 struct device
*dev
= &pdev
->dev
;
1642 struct at91_udc
*udc
;
1645 if (!dev
->platform_data
) {
1646 /* small (so we copy it) but critical! */
1647 DBG("missing platform_data\n");
1651 if (pdev
->num_resources
!= 2) {
1652 DBG("invalid num_resources");
1655 if ((pdev
->resource
[0].flags
!= IORESOURCE_MEM
)
1656 || (pdev
->resource
[1].flags
!= IORESOURCE_IRQ
)) {
1657 DBG("invalid resource type");
1661 if (!request_mem_region(AT91RM9200_BASE_UDP
, SZ_16K
, driver_name
)) {
1662 DBG("someone's using UDC memory\n");
1666 /* init software state */
1668 udc
->gadget
.dev
.parent
= dev
;
1669 udc
->board
= *(struct at91_udc_data
*) dev
->platform_data
;
1674 /* get interface and function clocks */
1675 udc
->iclk
= clk_get(dev
, "udc_clk");
1676 udc
->fclk
= clk_get(dev
, "udpck");
1677 if (IS_ERR(udc
->iclk
) || IS_ERR(udc
->fclk
)) {
1678 DBG("clocks missing\n");
1682 retval
= device_register(&udc
->gadget
.dev
);
1686 /* don't do anything until we have both gadget driver and VBUS */
1687 clk_enable(udc
->iclk
);
1688 at91_udp_write(AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
1689 at91_udp_write(AT91_UDP_IDR
, 0xffffffff);
1690 clk_disable(udc
->iclk
);
1692 /* request UDC and maybe VBUS irqs */
1693 udc
->udp_irq
= platform_get_irq(pdev
, 0);
1694 if (request_irq(udc
->udp_irq
, at91_udc_irq
,
1695 IRQF_DISABLED
, driver_name
, udc
)) {
1696 DBG("request irq %d failed\n", udc
->udp_irq
);
1700 if (udc
->board
.vbus_pin
> 0) {
1701 if (request_irq(udc
->board
.vbus_pin
, at91_vbus_irq
,
1702 IRQF_DISABLED
, driver_name
, udc
)) {
1703 DBG("request vbus irq %d failed\n",
1704 udc
->board
.vbus_pin
);
1705 free_irq(udc
->udp_irq
, udc
);
1710 DBG("no VBUS detection, assuming always-on\n");
1713 dev_set_drvdata(dev
, udc
);
1714 device_init_wakeup(dev
, 1);
1715 create_debug_file(udc
);
1717 INFO("%s version %s\n", driver_name
, DRIVER_VERSION
);
1721 device_unregister(&udc
->gadget
.dev
);
1723 release_mem_region(AT91RM9200_BASE_UDP
, SZ_16K
);
1724 DBG("%s probe failed, %d\n", driver_name
, retval
);
1728 static int __devexit
at91udc_remove(struct platform_device
*pdev
)
1730 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1736 if (udc
->driver
!= 0)
1737 usb_gadget_unregister_driver(udc
->driver
);
1739 device_init_wakeup(&pdev
->dev
, 0);
1740 remove_debug_file(udc
);
1741 if (udc
->board
.vbus_pin
> 0)
1742 free_irq(udc
->board
.vbus_pin
, udc
);
1743 free_irq(udc
->udp_irq
, udc
);
1744 device_unregister(&udc
->gadget
.dev
);
1745 release_mem_region(AT91RM9200_BASE_UDP
, SZ_16K
);
1754 static int at91udc_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1756 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1757 int wake
= udc
->driver
&& device_may_wakeup(&pdev
->dev
);
1759 /* Unless we can act normally to the host (letting it wake us up
1760 * whenever it has work for us) force disconnect. Wakeup requires
1761 * PLLB for USB events (signaling for reset, wakeup, or incoming
1762 * tokens) and VBUS irqs (on systems which support them).
1764 if ((!udc
->suspended
&& udc
->addr
)
1766 || at91_suspend_entering_slow_clock()) {
1768 disable_irq_wake(udc
->udp_irq
);
1770 enable_irq_wake(udc
->udp_irq
);
1772 if (udc
->board
.vbus_pin
> 0) {
1774 enable_irq_wake(udc
->board
.vbus_pin
);
1776 disable_irq_wake(udc
->board
.vbus_pin
);
1781 static int at91udc_resume(struct platform_device
*pdev
)
1783 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1785 /* maybe reconnect to host; if so, clocks on */
1790 #define at91udc_suspend NULL
1791 #define at91udc_resume NULL
1794 static struct platform_driver at91_udc
= {
1795 .probe
= at91udc_probe
,
1796 .remove
= __devexit_p(at91udc_remove
),
1797 .shutdown
= at91udc_shutdown
,
1798 .suspend
= at91udc_suspend
,
1799 .resume
= at91udc_resume
,
1801 .name
= (char *) driver_name
,
1802 .owner
= THIS_MODULE
,
1806 static int __devinit
udc_init_module(void)
1808 return platform_driver_register(&at91_udc
);
1810 module_init(udc_init_module
);
1812 static void __devexit
udc_exit_module(void)
1814 platform_driver_unregister(&at91_udc
);
1816 module_exit(udc_exit_module
);
1818 MODULE_DESCRIPTION("AT91 udc driver");
1819 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1820 MODULE_LICENSE("GPL");