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
;
395 * TODO: allow for writing two packets to the fifo ... that'll
396 * reduce the amount of IN-NAKing, but probably won't affect
397 * throughput much. (Unlike preventing OUT-NAKing!)
401 * If ep_queue() calls us, the queue is empty and possibly in
402 * odd states like TXCOMP not yet cleared (we do it, saving at
403 * least one IRQ) or the fifo not yet being free. Those aren't
404 * issues normally (IRQ handler fast path).
406 if (unlikely(csr
& (AT91_UDP_TXCOMP
| AT91_UDP_TXPKTRDY
))) {
407 if (csr
& AT91_UDP_TXCOMP
) {
409 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
410 __raw_writel(csr
, creg
);
411 csr
= __raw_readl(creg
);
413 if (csr
& AT91_UDP_TXPKTRDY
)
417 buf
= req
->req
.buf
+ req
->req
.actual
;
419 total
= req
->req
.length
- req
->req
.actual
;
420 if (ep
->ep
.maxpacket
< total
) {
421 count
= ep
->ep
.maxpacket
;
425 is_last
= (count
< ep
->ep
.maxpacket
) || !req
->req
.zero
;
429 * Write the packet, maybe it's a ZLP.
431 * NOTE: incrementing req->actual before we receive the ACK means
432 * gadget driver IN bytecounts can be wrong in fault cases. That's
433 * fixable with PIO drivers like this one (save "count" here, and
434 * do the increment later on TX irq), but not for most DMA hardware.
436 * So all gadget drivers must accept that potential error. Some
437 * hardware supports precise fifo status reporting, letting them
438 * recover when the actual bytecount matters (e.g. for USB Test
439 * and Measurement Class devices).
441 __raw_writesb(dreg
, buf
, count
);
443 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
444 __raw_writel(csr
, creg
);
445 req
->req
.actual
+= count
;
447 PACKET("%s %p in/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
448 is_last
? " (done)" : "");
454 static void nuke(struct at91_ep
*ep
, int status
)
456 struct at91_request
*req
;
458 // terminer chaque requete dans la queue
460 if (list_empty(&ep
->queue
))
463 VDBG("%s %s\n", __func__
, ep
->ep
.name
);
464 while (!list_empty(&ep
->queue
)) {
465 req
= list_entry(ep
->queue
.next
, struct at91_request
, queue
);
466 done(ep
, req
, status
);
470 /*-------------------------------------------------------------------------*/
472 static int at91_ep_enable(struct usb_ep
*_ep
,
473 const struct usb_endpoint_descriptor
*desc
)
475 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
476 struct at91_udc
*dev
= ep
->udc
;
483 || _ep
->name
== ep0name
484 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
485 || (maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
)) == 0
486 || maxpacket
> ep
->maxpacket
) {
487 DBG("bad ep or descriptor\n");
491 if (!dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
492 DBG("bogus device state\n");
496 tmp
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
498 case USB_ENDPOINT_XFER_CONTROL
:
499 DBG("only one control endpoint\n");
501 case USB_ENDPOINT_XFER_INT
:
505 case USB_ENDPOINT_XFER_BULK
:
514 DBG("bogus maxpacket %d\n", maxpacket
);
516 case USB_ENDPOINT_XFER_ISOC
:
517 if (!ep
->is_pingpong
) {
518 DBG("iso requires double buffering\n");
525 local_irq_save(flags
);
527 /* initialize endpoint to match this descriptor */
528 ep
->is_in
= (desc
->bEndpointAddress
& USB_DIR_IN
) != 0;
529 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
);
534 tmp
|= AT91_UDP_EPEDS
;
535 __raw_writel(tmp
, ep
->creg
);
538 ep
->ep
.maxpacket
= maxpacket
;
541 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
542 * since endpoint resets don't reset hw pingpong state.
544 at91_udp_write(dev
, AT91_UDP_RST_EP
, ep
->int_mask
);
545 at91_udp_write(dev
, AT91_UDP_RST_EP
, 0);
547 local_irq_restore(flags
);
551 static int at91_ep_disable (struct usb_ep
* _ep
)
553 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
554 struct at91_udc
*udc
= ep
->udc
;
557 if (ep
== &ep
->udc
->ep
[0])
560 local_irq_save(flags
);
562 nuke(ep
, -ESHUTDOWN
);
564 /* restore the endpoint's pristine config */
566 ep
->ep
.maxpacket
= ep
->maxpacket
;
568 /* reset fifos and endpoint */
569 if (ep
->udc
->clocked
) {
570 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
571 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
572 __raw_writel(0, ep
->creg
);
575 local_irq_restore(flags
);
580 * this is a PIO-only driver, so there's nothing
581 * interesting for request or buffer allocation.
584 static struct usb_request
*
585 at91_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
587 struct at91_request
*req
;
589 req
= kzalloc(sizeof (struct at91_request
), gfp_flags
);
593 INIT_LIST_HEAD(&req
->queue
);
597 static void at91_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
599 struct at91_request
*req
;
601 req
= container_of(_req
, struct at91_request
, req
);
602 BUG_ON(!list_empty(&req
->queue
));
606 static int at91_ep_queue(struct usb_ep
*_ep
,
607 struct usb_request
*_req
, gfp_t gfp_flags
)
609 struct at91_request
*req
;
611 struct at91_udc
*dev
;
615 req
= container_of(_req
, struct at91_request
, req
);
616 ep
= container_of(_ep
, struct at91_ep
, ep
);
618 if (!_req
|| !_req
->complete
619 || !_req
->buf
|| !list_empty(&req
->queue
)) {
620 DBG("invalid request\n");
624 if (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
)) {
631 if (!dev
|| !dev
->driver
|| dev
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
632 DBG("invalid device\n");
636 _req
->status
= -EINPROGRESS
;
639 local_irq_save(flags
);
641 /* try to kickstart any empty and idle queue */
642 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
646 * If this control request has a non-empty DATA stage, this
647 * will start that stage. It works just like a non-control
648 * request (until the status stage starts, maybe early).
650 * If the data stage is empty, then this starts a successful
651 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
653 is_ep0
= (ep
->ep
.name
== ep0name
);
657 if (!dev
->req_pending
) {
663 * defer changing CONFG until after the gadget driver
664 * reconfigures the endpoints.
666 if (dev
->wait_for_config_ack
) {
667 tmp
= at91_udp_read(dev
, AT91_UDP_GLB_STAT
);
668 tmp
^= AT91_UDP_CONFG
;
669 VDBG("toggle config\n");
670 at91_udp_write(dev
, AT91_UDP_GLB_STAT
, tmp
);
672 if (req
->req
.length
== 0) {
674 PACKET("ep0 in/status\n");
676 tmp
= __raw_readl(ep
->creg
);
678 tmp
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
679 __raw_writel(tmp
, ep
->creg
);
680 dev
->req_pending
= 0;
686 status
= write_fifo(ep
, req
);
688 status
= read_fifo(ep
, req
);
690 /* IN/STATUS stage is otherwise triggered by irq */
691 if (status
&& is_ep0
)
697 if (req
&& !status
) {
698 list_add_tail (&req
->queue
, &ep
->queue
);
699 at91_udp_write(dev
, AT91_UDP_IER
, ep
->int_mask
);
702 local_irq_restore(flags
);
703 return (status
< 0) ? status
: 0;
706 static int at91_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
709 struct at91_request
*req
;
711 ep
= container_of(_ep
, struct at91_ep
, ep
);
712 if (!_ep
|| ep
->ep
.name
== ep0name
)
715 /* make sure it's actually queued on this endpoint */
716 list_for_each_entry (req
, &ep
->queue
, queue
) {
717 if (&req
->req
== _req
)
720 if (&req
->req
!= _req
)
723 done(ep
, req
, -ECONNRESET
);
727 static int at91_ep_set_halt(struct usb_ep
*_ep
, int value
)
729 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
730 struct at91_udc
*udc
= ep
->udc
;
736 if (!_ep
|| ep
->is_iso
|| !ep
->udc
->clocked
)
740 local_irq_save(flags
);
742 csr
= __raw_readl(creg
);
745 * fail with still-busy IN endpoints, ensuring correct sequencing
746 * of data tx then stall. note that the fifo rx bytecount isn't
747 * completely accurate as a tx bytecount.
749 if (ep
->is_in
&& (!list_empty(&ep
->queue
) || (csr
>> 16) != 0))
755 csr
|= AT91_UDP_FORCESTALL
;
756 VDBG("halt %s\n", ep
->ep
.name
);
758 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
759 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
760 csr
&= ~AT91_UDP_FORCESTALL
;
762 __raw_writel(csr
, creg
);
765 local_irq_restore(flags
);
769 static const struct usb_ep_ops at91_ep_ops
= {
770 .enable
= at91_ep_enable
,
771 .disable
= at91_ep_disable
,
772 .alloc_request
= at91_ep_alloc_request
,
773 .free_request
= at91_ep_free_request
,
774 .queue
= at91_ep_queue
,
775 .dequeue
= at91_ep_dequeue
,
776 .set_halt
= at91_ep_set_halt
,
777 // there's only imprecise fifo status reporting
780 /*-------------------------------------------------------------------------*/
782 static int at91_get_frame(struct usb_gadget
*gadget
)
784 struct at91_udc
*udc
= to_udc(gadget
);
786 if (!to_udc(gadget
)->clocked
)
788 return at91_udp_read(udc
, AT91_UDP_FRM_NUM
) & AT91_UDP_NUM
;
791 static int at91_wakeup(struct usb_gadget
*gadget
)
793 struct at91_udc
*udc
= to_udc(gadget
);
795 int status
= -EINVAL
;
798 DBG("%s\n", __func__
);
799 local_irq_save(flags
);
801 if (!udc
->clocked
|| !udc
->suspended
)
804 /* NOTE: some "early versions" handle ESR differently ... */
806 glbstate
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
807 if (!(glbstate
& AT91_UDP_ESR
))
809 glbstate
|= AT91_UDP_ESR
;
810 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, glbstate
);
813 local_irq_restore(flags
);
817 /* reinit == restore inital software state */
818 static void udc_reinit(struct at91_udc
*udc
)
822 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
823 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
825 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
826 struct at91_ep
*ep
= &udc
->ep
[i
];
829 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
833 ep
->ep
.maxpacket
= ep
->maxpacket
;
834 ep
->creg
= (void __iomem
*) udc
->udp_baseaddr
+ AT91_UDP_CSR(i
);
835 // initialiser une queue par endpoint
836 INIT_LIST_HEAD(&ep
->queue
);
840 static void stop_activity(struct at91_udc
*udc
)
842 struct usb_gadget_driver
*driver
= udc
->driver
;
845 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
847 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
850 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
851 struct at91_ep
*ep
= &udc
->ep
[i
];
853 nuke(ep
, -ESHUTDOWN
);
856 driver
->disconnect(&udc
->gadget
);
861 static void clk_on(struct at91_udc
*udc
)
866 clk_enable(udc
->iclk
);
867 clk_enable(udc
->fclk
);
870 static void clk_off(struct at91_udc
*udc
)
875 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
876 clk_disable(udc
->fclk
);
877 clk_disable(udc
->iclk
);
881 * activate/deactivate link with host; minimize power usage for
882 * inactive links by cutting clocks and transceiver power.
884 static void pullup(struct at91_udc
*udc
, int is_on
)
886 int active
= !udc
->board
.pullup_active_low
;
888 if (!udc
->enabled
|| !udc
->vbus
)
890 DBG("%sactive\n", is_on
? "" : "in");
894 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXRSM
);
895 at91_udp_write(udc
, AT91_UDP_TXVC
, 0);
896 if (cpu_is_at91rm9200())
897 gpio_set_value(udc
->board
.pullup_pin
, active
);
898 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
899 u32 txvc
= at91_udp_read(udc
, AT91_UDP_TXVC
);
901 txvc
|= AT91_UDP_TXVC_PUON
;
902 at91_udp_write(udc
, AT91_UDP_TXVC
, txvc
);
903 } else if (cpu_is_at91sam9261()) {
906 usbpucr
= at91_sys_read(AT91_MATRIX_USBPUCR
);
907 usbpucr
|= AT91_MATRIX_USBPUCR_PUON
;
908 at91_sys_write(AT91_MATRIX_USBPUCR
, usbpucr
);
912 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXRSM
);
913 at91_udp_write(udc
, AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
914 if (cpu_is_at91rm9200())
915 gpio_set_value(udc
->board
.pullup_pin
, !active
);
916 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
917 u32 txvc
= at91_udp_read(udc
, AT91_UDP_TXVC
);
919 txvc
&= ~AT91_UDP_TXVC_PUON
;
920 at91_udp_write(udc
, AT91_UDP_TXVC
, txvc
);
921 } else if (cpu_is_at91sam9261()) {
924 usbpucr
= at91_sys_read(AT91_MATRIX_USBPUCR
);
925 usbpucr
&= ~AT91_MATRIX_USBPUCR_PUON
;
926 at91_sys_write(AT91_MATRIX_USBPUCR
, usbpucr
);
932 /* vbus is here! turn everything on that's ready */
933 static int at91_vbus_session(struct usb_gadget
*gadget
, int is_active
)
935 struct at91_udc
*udc
= to_udc(gadget
);
938 // VDBG("vbus %s\n", is_active ? "on" : "off");
939 local_irq_save(flags
);
940 udc
->vbus
= (is_active
!= 0);
942 pullup(udc
, is_active
);
945 local_irq_restore(flags
);
949 static int at91_pullup(struct usb_gadget
*gadget
, int is_on
)
951 struct at91_udc
*udc
= to_udc(gadget
);
954 local_irq_save(flags
);
955 udc
->enabled
= is_on
= !!is_on
;
957 local_irq_restore(flags
);
961 static int at91_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
963 struct at91_udc
*udc
= to_udc(gadget
);
966 local_irq_save(flags
);
967 udc
->selfpowered
= (is_on
!= 0);
968 local_irq_restore(flags
);
972 static const struct usb_gadget_ops at91_udc_ops
= {
973 .get_frame
= at91_get_frame
,
974 .wakeup
= at91_wakeup
,
975 .set_selfpowered
= at91_set_selfpowered
,
976 .vbus_session
= at91_vbus_session
,
977 .pullup
= at91_pullup
,
980 * VBUS-powered devices may also also want to support bigger
981 * power budgets after an appropriate SET_CONFIGURATION.
983 // .vbus_power = at91_vbus_power,
986 /*-------------------------------------------------------------------------*/
988 static int handle_ep(struct at91_ep
*ep
)
990 struct at91_request
*req
;
991 u32 __iomem
*creg
= ep
->creg
;
992 u32 csr
= __raw_readl(creg
);
994 if (!list_empty(&ep
->queue
))
995 req
= list_entry(ep
->queue
.next
,
996 struct at91_request
, queue
);
1001 if (csr
& (AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
)) {
1003 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
);
1004 __raw_writel(csr
, creg
);
1007 return write_fifo(ep
, req
);
1010 if (csr
& AT91_UDP_STALLSENT
) {
1011 /* STALLSENT bit == ISOERR */
1012 if (ep
->is_iso
&& req
)
1013 req
->req
.status
= -EILSEQ
;
1015 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
);
1016 __raw_writel(csr
, creg
);
1017 csr
= __raw_readl(creg
);
1019 if (req
&& (csr
& RX_DATA_READY
))
1020 return read_fifo(ep
, req
);
1027 struct usb_ctrlrequest r
;
1030 static void handle_setup(struct at91_udc
*udc
, struct at91_ep
*ep
, u32 csr
)
1032 u32 __iomem
*creg
= ep
->creg
;
1033 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1034 unsigned rxcount
, i
= 0;
1039 /* read and ack SETUP; hard-fail for bogus packets */
1040 rxcount
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
1041 if (likely(rxcount
== 8)) {
1043 pkt
.raw
[i
++] = __raw_readb(dreg
);
1044 if (pkt
.r
.bRequestType
& USB_DIR_IN
) {
1045 csr
|= AT91_UDP_DIR
;
1048 csr
&= ~AT91_UDP_DIR
;
1052 // REVISIT this happens sometimes under load; why??
1053 ERR("SETUP len %d, csr %08x\n", rxcount
, csr
);
1057 csr
&= ~(SET_FX
| AT91_UDP_RXSETUP
);
1058 __raw_writel(csr
, creg
);
1059 udc
->wait_for_addr_ack
= 0;
1060 udc
->wait_for_config_ack
= 0;
1062 if (unlikely(status
!= 0))
1065 #define w_index le16_to_cpu(pkt.r.wIndex)
1066 #define w_value le16_to_cpu(pkt.r.wValue)
1067 #define w_length le16_to_cpu(pkt.r.wLength)
1069 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1070 pkt
.r
.bRequestType
, pkt
.r
.bRequest
,
1071 w_value
, w_index
, w_length
);
1074 * A few standard requests get handled here, ones that touch
1075 * hardware ... notably for device and endpoint features.
1077 udc
->req_pending
= 1;
1078 csr
= __raw_readl(creg
);
1081 switch ((pkt
.r
.bRequestType
<< 8) | pkt
.r
.bRequest
) {
1083 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1084 | USB_REQ_SET_ADDRESS
:
1085 __raw_writel(csr
| AT91_UDP_TXPKTRDY
, creg
);
1086 udc
->addr
= w_value
;
1087 udc
->wait_for_addr_ack
= 1;
1088 udc
->req_pending
= 0;
1089 /* FADDR is set later, when we ack host STATUS */
1092 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1093 | USB_REQ_SET_CONFIGURATION
:
1094 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
) & AT91_UDP_CONFG
;
1096 udc
->wait_for_config_ack
= (tmp
== 0);
1098 udc
->wait_for_config_ack
= (tmp
!= 0);
1099 if (udc
->wait_for_config_ack
)
1100 VDBG("wait for config\n");
1101 /* CONFG is toggled later, if gadget driver succeeds */
1105 * Hosts may set or clear remote wakeup status, and
1106 * devices may report they're VBUS powered.
1108 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1109 | USB_REQ_GET_STATUS
:
1110 tmp
= (udc
->selfpowered
<< USB_DEVICE_SELF_POWERED
);
1111 if (at91_udp_read(udc
, AT91_UDP_GLB_STAT
) & AT91_UDP_ESR
)
1112 tmp
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
1113 PACKET("get device status\n");
1114 __raw_writeb(tmp
, dreg
);
1115 __raw_writeb(0, dreg
);
1117 /* then STATUS starts later, automatically */
1118 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1119 | USB_REQ_SET_FEATURE
:
1120 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1122 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1123 tmp
|= AT91_UDP_ESR
;
1124 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1126 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1127 | USB_REQ_CLEAR_FEATURE
:
1128 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1130 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1131 tmp
&= ~AT91_UDP_ESR
;
1132 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1136 * Interfaces have no feature settings; this is pretty useless.
1137 * we won't even insist the interface exists...
1139 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1140 | USB_REQ_GET_STATUS
:
1141 PACKET("get interface status\n");
1142 __raw_writeb(0, dreg
);
1143 __raw_writeb(0, dreg
);
1145 /* then STATUS starts later, automatically */
1146 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1147 | USB_REQ_SET_FEATURE
:
1148 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1149 | USB_REQ_CLEAR_FEATURE
:
1153 * Hosts may clear bulk/intr endpoint halt after the gadget
1154 * driver sets it (not widely used); or set it (for testing)
1156 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1157 | USB_REQ_GET_STATUS
:
1158 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1160 if (tmp
>= NUM_ENDPOINTS
|| (tmp
&& !ep
->desc
))
1164 if ((w_index
& USB_DIR_IN
)) {
1167 } else if (ep
->is_in
)
1170 PACKET("get %s status\n", ep
->ep
.name
);
1171 if (__raw_readl(ep
->creg
) & AT91_UDP_FORCESTALL
)
1172 tmp
= (1 << USB_ENDPOINT_HALT
);
1175 __raw_writeb(tmp
, dreg
);
1176 __raw_writeb(0, dreg
);
1178 /* then STATUS starts later, automatically */
1179 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1180 | USB_REQ_SET_FEATURE
:
1181 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1183 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
>= NUM_ENDPOINTS
)
1185 if (!ep
->desc
|| ep
->is_iso
)
1187 if ((w_index
& USB_DIR_IN
)) {
1190 } else if (ep
->is_in
)
1193 tmp
= __raw_readl(ep
->creg
);
1195 tmp
|= CLR_FX
| AT91_UDP_FORCESTALL
;
1196 __raw_writel(tmp
, ep
->creg
);
1198 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1199 | USB_REQ_CLEAR_FEATURE
:
1200 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1202 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
>= NUM_ENDPOINTS
)
1206 if (!ep
->desc
|| ep
->is_iso
)
1208 if ((w_index
& USB_DIR_IN
)) {
1211 } else if (ep
->is_in
)
1214 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
1215 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
1216 tmp
= __raw_readl(ep
->creg
);
1218 tmp
&= ~(SET_FX
| AT91_UDP_FORCESTALL
);
1219 __raw_writel(tmp
, ep
->creg
);
1220 if (!list_empty(&ep
->queue
))
1229 /* pass request up to the gadget driver */
1231 status
= udc
->driver
->setup(&udc
->gadget
, &pkt
.r
);
1236 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1237 pkt
.r
.bRequestType
, pkt
.r
.bRequest
, status
);
1238 csr
|= AT91_UDP_FORCESTALL
;
1239 __raw_writel(csr
, creg
);
1240 udc
->req_pending
= 0;
1245 /* immediate successful (IN) STATUS after zero length DATA */
1246 PACKET("ep0 in/status\n");
1248 csr
|= AT91_UDP_TXPKTRDY
;
1249 __raw_writel(csr
, creg
);
1250 udc
->req_pending
= 0;
1254 static void handle_ep0(struct at91_udc
*udc
)
1256 struct at91_ep
*ep0
= &udc
->ep
[0];
1257 u32 __iomem
*creg
= ep0
->creg
;
1258 u32 csr
= __raw_readl(creg
);
1259 struct at91_request
*req
;
1261 if (unlikely(csr
& AT91_UDP_STALLSENT
)) {
1263 udc
->req_pending
= 0;
1265 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_FORCESTALL
);
1266 __raw_writel(csr
, creg
);
1267 VDBG("ep0 stalled\n");
1268 csr
= __raw_readl(creg
);
1270 if (csr
& AT91_UDP_RXSETUP
) {
1272 udc
->req_pending
= 0;
1273 handle_setup(udc
, ep0
, csr
);
1277 if (list_empty(&ep0
->queue
))
1280 req
= list_entry(ep0
->queue
.next
, struct at91_request
, queue
);
1282 /* host ACKed an IN packet that we sent */
1283 if (csr
& AT91_UDP_TXCOMP
) {
1285 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
1287 /* write more IN DATA? */
1288 if (req
&& ep0
->is_in
) {
1290 udc
->req_pending
= 0;
1294 * - last IN DATA packet (including GET_STATUS)
1295 * - IN/STATUS for OUT DATA
1296 * - IN/STATUS for any zero-length DATA stage
1297 * except for the IN DATA case, the host should send
1298 * an OUT status later, which we'll ack.
1301 udc
->req_pending
= 0;
1302 __raw_writel(csr
, creg
);
1305 * SET_ADDRESS takes effect only after the STATUS
1306 * (to the original address) gets acked.
1308 if (udc
->wait_for_addr_ack
) {
1311 at91_udp_write(udc
, AT91_UDP_FADDR
,
1312 AT91_UDP_FEN
| udc
->addr
);
1313 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1314 tmp
&= ~AT91_UDP_FADDEN
;
1316 tmp
|= AT91_UDP_FADDEN
;
1317 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1319 udc
->wait_for_addr_ack
= 0;
1320 VDBG("address %d\n", udc
->addr
);
1325 /* OUT packet arrived ... */
1326 else if (csr
& AT91_UDP_RX_DATA_BK0
) {
1328 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
1330 /* OUT DATA stage */
1333 if (handle_ep(ep0
)) {
1334 /* send IN/STATUS */
1335 PACKET("ep0 in/status\n");
1336 csr
= __raw_readl(creg
);
1338 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
1339 __raw_writel(csr
, creg
);
1340 udc
->req_pending
= 0;
1342 } else if (udc
->req_pending
) {
1344 * AT91 hardware has a hard time with this
1345 * "deferred response" mode for control-OUT
1346 * transfers. (For control-IN it's fine.)
1348 * The normal solution leaves OUT data in the
1349 * fifo until the gadget driver is ready.
1350 * We couldn't do that here without disabling
1351 * the IRQ that tells about SETUP packets,
1352 * e.g. when the host gets impatient...
1354 * Working around it by copying into a buffer
1355 * would almost be a non-deferred response,
1356 * except that it wouldn't permit reliable
1357 * stalling of the request. Instead, demand
1358 * that gadget drivers not use this mode.
1360 DBG("no control-OUT deferred responses!\n");
1361 __raw_writel(csr
| AT91_UDP_FORCESTALL
, creg
);
1362 udc
->req_pending
= 0;
1365 /* STATUS stage for control-IN; ack. */
1367 PACKET("ep0 out/status ACK\n");
1368 __raw_writel(csr
, creg
);
1370 /* "early" status stage */
1377 static irqreturn_t
at91_udc_irq (int irq
, void *_udc
)
1379 struct at91_udc
*udc
= _udc
;
1385 status
= at91_udp_read(udc
, AT91_UDP_ISR
)
1386 & at91_udp_read(udc
, AT91_UDP_IMR
);
1390 /* USB reset irq: not maskable */
1391 if (status
& AT91_UDP_ENDBUSRES
) {
1392 at91_udp_write(udc
, AT91_UDP_IDR
, ~MINIMUS_INTERRUPTUS
);
1393 at91_udp_write(udc
, AT91_UDP_IER
, MINIMUS_INTERRUPTUS
);
1394 /* Atmel code clears this irq twice */
1395 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1396 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1397 VDBG("end bus reset\n");
1402 at91_udp_write(udc
, AT91_UDP_CSR(0),
1403 AT91_UDP_EPEDS
| AT91_UDP_EPTYPE_CTRL
);
1404 udc
->gadget
.speed
= USB_SPEED_FULL
;
1406 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_EP(0));
1409 * NOTE: this driver keeps clocks off unless the
1410 * USB host is present. That saves power, but for
1411 * boards that don't support VBUS detection, both
1412 * clocks need to be active most of the time.
1415 /* host initiated suspend (3+ms bus idle) */
1416 } else if (status
& AT91_UDP_RXSUSP
) {
1417 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXSUSP
);
1418 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_RXRSM
);
1419 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXSUSP
);
1420 // VDBG("bus suspend\n");
1426 * NOTE: when suspending a VBUS-powered device, the
1427 * gadget driver should switch into slow clock mode
1428 * and then into standby to avoid drawing more than
1429 * 500uA power (2500uA for some high-power configs).
1431 if (udc
->driver
&& udc
->driver
->suspend
)
1432 udc
->driver
->suspend(&udc
->gadget
);
1434 /* host initiated resume */
1435 } else if (status
& AT91_UDP_RXRSM
) {
1436 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXRSM
);
1437 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_RXSUSP
);
1438 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXRSM
);
1439 // VDBG("bus resume\n");
1440 if (!udc
->suspended
)
1445 * NOTE: for a VBUS-powered device, the gadget driver
1446 * would normally want to switch out of slow clock
1447 * mode into normal mode.
1449 if (udc
->driver
&& udc
->driver
->resume
)
1450 udc
->driver
->resume(&udc
->gadget
);
1452 /* endpoint IRQs are cleared by handling them */
1456 struct at91_ep
*ep
= &udc
->ep
[1];
1460 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1472 /*-------------------------------------------------------------------------*/
1474 static void nop_release(struct device
*dev
)
1476 /* nothing to free */
1479 static struct at91_udc controller
= {
1481 .ops
= &at91_udc_ops
,
1482 .ep0
= &controller
.ep
[0].ep
,
1483 .name
= driver_name
,
1486 .release
= nop_release
,
1492 .ops
= &at91_ep_ops
,
1501 .ops
= &at91_ep_ops
,
1511 .ops
= &at91_ep_ops
,
1520 /* could actually do bulk too */
1522 .ops
= &at91_ep_ops
,
1531 .ops
= &at91_ep_ops
,
1541 .ops
= &at91_ep_ops
,
1548 /* ep6 and ep7 are also reserved (custom silicon might use them) */
1551 static irqreturn_t
at91_vbus_irq(int irq
, void *_udc
)
1553 struct at91_udc
*udc
= _udc
;
1556 /* vbus needs at least brief debouncing */
1558 value
= gpio_get_value(udc
->board
.vbus_pin
);
1559 if (value
!= udc
->vbus
)
1560 at91_vbus_session(&udc
->gadget
, value
);
1565 int usb_gadget_register_driver (struct usb_gadget_driver
*driver
)
1567 struct at91_udc
*udc
= &controller
;
1571 || driver
->speed
< USB_SPEED_FULL
1573 || !driver
->setup
) {
1574 DBG("bad parameter.\n");
1579 DBG("UDC already has a gadget driver\n");
1583 udc
->driver
= driver
;
1584 udc
->gadget
.dev
.driver
= &driver
->driver
;
1585 udc
->gadget
.dev
.driver_data
= &driver
->driver
;
1587 udc
->selfpowered
= 1;
1589 retval
= driver
->bind(&udc
->gadget
);
1591 DBG("driver->bind() returned %d\n", retval
);
1593 udc
->gadget
.dev
.driver
= NULL
;
1594 udc
->gadget
.dev
.driver_data
= NULL
;
1596 udc
->selfpowered
= 0;
1600 local_irq_disable();
1604 DBG("bound to %s\n", driver
->driver
.name
);
1607 EXPORT_SYMBOL (usb_gadget_register_driver
);
1609 int usb_gadget_unregister_driver (struct usb_gadget_driver
*driver
)
1611 struct at91_udc
*udc
= &controller
;
1613 if (!driver
|| driver
!= udc
->driver
|| !driver
->unbind
)
1616 local_irq_disable();
1618 at91_udp_write(udc
, AT91_UDP_IDR
, ~0);
1622 driver
->unbind(&udc
->gadget
);
1623 udc
->gadget
.dev
.driver
= NULL
;
1624 udc
->gadget
.dev
.driver_data
= NULL
;
1627 DBG("unbound from %s\n", driver
->driver
.name
);
1630 EXPORT_SYMBOL (usb_gadget_unregister_driver
);
1632 /*-------------------------------------------------------------------------*/
1634 static void at91udc_shutdown(struct platform_device
*dev
)
1636 /* force disconnect on reboot */
1637 pullup(platform_get_drvdata(dev
), 0);
1640 static int __init
at91udc_probe(struct platform_device
*pdev
)
1642 struct device
*dev
= &pdev
->dev
;
1643 struct at91_udc
*udc
;
1645 struct resource
*res
;
1647 if (!dev
->platform_data
) {
1648 /* small (so we copy it) but critical! */
1649 DBG("missing platform_data\n");
1653 if (pdev
->num_resources
!= 2) {
1654 DBG("invalid num_resources\n");
1657 if ((pdev
->resource
[0].flags
!= IORESOURCE_MEM
)
1658 || (pdev
->resource
[1].flags
!= IORESOURCE_IRQ
)) {
1659 DBG("invalid resource type\n");
1663 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1667 if (!request_mem_region(res
->start
,
1668 res
->end
- res
->start
+ 1,
1670 DBG("someone's using UDC memory\n");
1674 /* init software state */
1676 udc
->gadget
.dev
.parent
= dev
;
1677 udc
->board
= *(struct at91_udc_data
*) dev
->platform_data
;
1681 /* rm9200 needs manual D+ pullup; off by default */
1682 if (cpu_is_at91rm9200()) {
1683 if (udc
->board
.pullup_pin
<= 0) {
1684 DBG("no D+ pullup?\n");
1688 retval
= gpio_request(udc
->board
.pullup_pin
, "udc_pullup");
1690 DBG("D+ pullup is busy\n");
1693 gpio_direction_output(udc
->board
.pullup_pin
,
1694 udc
->board
.pullup_active_low
);
1697 udc
->udp_baseaddr
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
1698 if (!udc
->udp_baseaddr
) {
1705 /* get interface and function clocks */
1706 udc
->iclk
= clk_get(dev
, "udc_clk");
1707 udc
->fclk
= clk_get(dev
, "udpck");
1708 if (IS_ERR(udc
->iclk
) || IS_ERR(udc
->fclk
)) {
1709 DBG("clocks missing\n");
1711 /* NOTE: we "know" here that refcounts on these are NOPs */
1715 retval
= device_register(&udc
->gadget
.dev
);
1719 /* don't do anything until we have both gadget driver and VBUS */
1720 clk_enable(udc
->iclk
);
1721 at91_udp_write(udc
, AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
1722 at91_udp_write(udc
, AT91_UDP_IDR
, 0xffffffff);
1723 /* Clear all pending interrupts - UDP may be used by bootloader. */
1724 at91_udp_write(udc
, AT91_UDP_ICR
, 0xffffffff);
1725 clk_disable(udc
->iclk
);
1727 /* request UDC and maybe VBUS irqs */
1728 udc
->udp_irq
= platform_get_irq(pdev
, 0);
1729 retval
= request_irq(udc
->udp_irq
, at91_udc_irq
,
1730 IRQF_DISABLED
, driver_name
, udc
);
1732 DBG("request irq %d failed\n", udc
->udp_irq
);
1735 if (udc
->board
.vbus_pin
> 0) {
1736 retval
= gpio_request(udc
->board
.vbus_pin
, "udc_vbus");
1738 DBG("request vbus pin failed\n");
1741 gpio_direction_input(udc
->board
.vbus_pin
);
1744 * Get the initial state of VBUS - we cannot expect
1745 * a pending interrupt.
1747 udc
->vbus
= gpio_get_value(udc
->board
.vbus_pin
);
1748 if (request_irq(udc
->board
.vbus_pin
, at91_vbus_irq
,
1749 IRQF_DISABLED
, driver_name
, udc
)) {
1750 DBG("request vbus irq %d failed\n",
1751 udc
->board
.vbus_pin
);
1752 free_irq(udc
->udp_irq
, udc
);
1757 DBG("no VBUS detection, assuming always-on\n");
1760 dev_set_drvdata(dev
, udc
);
1761 device_init_wakeup(dev
, 1);
1762 create_debug_file(udc
);
1764 INFO("%s version %s\n", driver_name
, DRIVER_VERSION
);
1768 if (udc
->board
.vbus_pin
> 0)
1769 gpio_free(udc
->board
.vbus_pin
);
1771 free_irq(udc
->udp_irq
, udc
);
1773 device_unregister(&udc
->gadget
.dev
);
1775 iounmap(udc
->udp_baseaddr
);
1777 if (cpu_is_at91rm9200())
1778 gpio_free(udc
->board
.pullup_pin
);
1780 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1781 DBG("%s probe failed, %d\n", driver_name
, retval
);
1785 static int __exit
at91udc_remove(struct platform_device
*pdev
)
1787 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1788 struct resource
*res
;
1797 device_init_wakeup(&pdev
->dev
, 0);
1798 remove_debug_file(udc
);
1799 if (udc
->board
.vbus_pin
> 0) {
1800 free_irq(udc
->board
.vbus_pin
, udc
);
1801 gpio_free(udc
->board
.vbus_pin
);
1803 free_irq(udc
->udp_irq
, udc
);
1804 device_unregister(&udc
->gadget
.dev
);
1806 iounmap(udc
->udp_baseaddr
);
1808 if (cpu_is_at91rm9200())
1809 gpio_free(udc
->board
.pullup_pin
);
1811 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1812 release_mem_region(res
->start
, res
->end
- res
->start
+ 1);
1821 static int at91udc_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1823 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1824 int wake
= udc
->driver
&& device_may_wakeup(&pdev
->dev
);
1826 /* Unless we can act normally to the host (letting it wake us up
1827 * whenever it has work for us) force disconnect. Wakeup requires
1828 * PLLB for USB events (signaling for reset, wakeup, or incoming
1829 * tokens) and VBUS irqs (on systems which support them).
1831 if ((!udc
->suspended
&& udc
->addr
)
1833 || at91_suspend_entering_slow_clock()) {
1837 enable_irq_wake(udc
->udp_irq
);
1839 udc
->active_suspend
= wake
;
1840 if (udc
->board
.vbus_pin
> 0 && wake
)
1841 enable_irq_wake(udc
->board
.vbus_pin
);
1845 static int at91udc_resume(struct platform_device
*pdev
)
1847 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1849 if (udc
->board
.vbus_pin
> 0 && udc
->active_suspend
)
1850 disable_irq_wake(udc
->board
.vbus_pin
);
1852 /* maybe reconnect to host; if so, clocks on */
1853 if (udc
->active_suspend
)
1854 disable_irq_wake(udc
->udp_irq
);
1860 #define at91udc_suspend NULL
1861 #define at91udc_resume NULL
1864 static struct platform_driver at91_udc_driver
= {
1865 .remove
= __exit_p(at91udc_remove
),
1866 .shutdown
= at91udc_shutdown
,
1867 .suspend
= at91udc_suspend
,
1868 .resume
= at91udc_resume
,
1870 .name
= (char *) driver_name
,
1871 .owner
= THIS_MODULE
,
1875 static int __init
udc_init_module(void)
1877 return platform_driver_probe(&at91_udc_driver
, at91udc_probe
);
1879 module_init(udc_init_module
);
1881 static void __exit
udc_exit_module(void)
1883 platform_driver_unregister(&at91_udc_driver
);
1885 module_exit(udc_exit_module
);
1887 MODULE_DESCRIPTION("AT91 udc driver");
1888 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1889 MODULE_LICENSE("GPL");
1890 MODULE_ALIAS("platform:at91_udc");