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/prefetch.h>
39 #include <linux/clk.h>
40 #include <linux/usb/ch9.h>
41 #include <linux/usb/gadget.h>
42 #include <linux/prefetch.h>
44 #include <asm/byteorder.h>
45 #include <mach/hardware.h>
48 #include <asm/system.h>
51 #include <mach/board.h>
53 #include <mach/at91sam9261_matrix.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 #define VBUS_POLL_TIMEOUT msecs_to_jiffies(1000)
83 #define at91_udp_read(udc, reg) \
84 __raw_readl((udc)->udp_baseaddr + (reg))
85 #define at91_udp_write(udc, reg, val) \
86 __raw_writel((val), (udc)->udp_baseaddr + (reg))
88 /*-------------------------------------------------------------------------*/
90 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
92 #include <linux/seq_file.h>
94 static const char debug_filename
[] = "driver/udc";
96 #define FOURBITS "%s%s%s%s"
97 #define EIGHTBITS FOURBITS FOURBITS
99 static void proc_ep_show(struct seq_file
*s
, struct at91_ep
*ep
)
101 static char *types
[] = {
102 "control", "out-iso", "out-bulk", "out-int",
103 "BOGUS", "in-iso", "in-bulk", "in-int"};
106 struct at91_request
*req
;
108 struct at91_udc
*udc
= ep
->udc
;
110 spin_lock_irqsave(&udc
->lock
, flags
);
112 csr
= __raw_readl(ep
->creg
);
114 /* NOTE: not collecting per-endpoint irq statistics... */
117 seq_printf(s
, "%s, maxpacket %d %s%s %s%s\n",
118 ep
->ep
.name
, ep
->ep
.maxpacket
,
119 ep
->is_in
? "in" : "out",
120 ep
->is_iso
? " iso" : "",
122 ? (ep
->fifo_bank
? "pong" : "ping")
124 ep
->stopped
? " stopped" : "");
125 seq_printf(s
, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS
"\n",
127 (csr
& 0x07ff0000) >> 16,
128 (csr
& (1 << 15)) ? "enabled" : "disabled",
129 (csr
& (1 << 11)) ? "DATA1" : "DATA0",
130 types
[(csr
& 0x700) >> 8],
132 /* iff type is control then print current direction */
134 ? ((csr
& (1 << 7)) ? " IN" : " OUT")
136 (csr
& (1 << 6)) ? " rxdatabk1" : "",
137 (csr
& (1 << 5)) ? " forcestall" : "",
138 (csr
& (1 << 4)) ? " txpktrdy" : "",
140 (csr
& (1 << 3)) ? " stallsent" : "",
141 (csr
& (1 << 2)) ? " rxsetup" : "",
142 (csr
& (1 << 1)) ? " rxdatabk0" : "",
143 (csr
& (1 << 0)) ? " txcomp" : "");
144 if (list_empty (&ep
->queue
))
145 seq_printf(s
, "\t(queue empty)\n");
147 else list_for_each_entry (req
, &ep
->queue
, queue
) {
148 unsigned length
= req
->req
.actual
;
150 seq_printf(s
, "\treq %p len %d/%d buf %p\n",
152 req
->req
.length
, req
->req
.buf
);
154 spin_unlock_irqrestore(&udc
->lock
, flags
);
157 static void proc_irq_show(struct seq_file
*s
, const char *label
, u32 mask
)
161 seq_printf(s
, "%s %04x:%s%s" FOURBITS
, label
, mask
,
162 (mask
& (1 << 13)) ? " wakeup" : "",
163 (mask
& (1 << 12)) ? " endbusres" : "",
165 (mask
& (1 << 11)) ? " sofint" : "",
166 (mask
& (1 << 10)) ? " extrsm" : "",
167 (mask
& (1 << 9)) ? " rxrsm" : "",
168 (mask
& (1 << 8)) ? " rxsusp" : "");
169 for (i
= 0; i
< 8; i
++) {
171 seq_printf(s
, " ep%d", i
);
176 static int proc_udc_show(struct seq_file
*s
, void *unused
)
178 struct at91_udc
*udc
= s
->private;
182 seq_printf(s
, "%s: version %s\n", driver_name
, DRIVER_VERSION
);
184 seq_printf(s
, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
185 udc
->vbus
? "present" : "off",
187 ? (udc
->vbus
? "active" : "enabled")
189 udc
->selfpowered
? "self" : "VBUS",
190 udc
->suspended
? ", suspended" : "",
191 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
193 /* don't access registers when interface isn't clocked */
195 seq_printf(s
, "(not clocked)\n");
199 tmp
= at91_udp_read(udc
, AT91_UDP_FRM_NUM
);
200 seq_printf(s
, "frame %05x:%s%s frame=%d\n", tmp
,
201 (tmp
& AT91_UDP_FRM_OK
) ? " ok" : "",
202 (tmp
& AT91_UDP_FRM_ERR
) ? " err" : "",
203 (tmp
& AT91_UDP_NUM
));
205 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
206 seq_printf(s
, "glbstate %02x:%s" FOURBITS
"\n", tmp
,
207 (tmp
& AT91_UDP_RMWUPE
) ? " rmwupe" : "",
208 (tmp
& AT91_UDP_RSMINPR
) ? " rsminpr" : "",
209 (tmp
& AT91_UDP_ESR
) ? " esr" : "",
210 (tmp
& AT91_UDP_CONFG
) ? " confg" : "",
211 (tmp
& AT91_UDP_FADDEN
) ? " fadden" : "");
213 tmp
= at91_udp_read(udc
, AT91_UDP_FADDR
);
214 seq_printf(s
, "faddr %03x:%s fadd=%d\n", tmp
,
215 (tmp
& AT91_UDP_FEN
) ? " fen" : "",
216 (tmp
& AT91_UDP_FADD
));
218 proc_irq_show(s
, "imr ", at91_udp_read(udc
, AT91_UDP_IMR
));
219 proc_irq_show(s
, "isr ", at91_udp_read(udc
, AT91_UDP_ISR
));
221 if (udc
->enabled
&& udc
->vbus
) {
222 proc_ep_show(s
, &udc
->ep
[0]);
223 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
231 static int proc_udc_open(struct inode
*inode
, struct file
*file
)
233 return single_open(file
, proc_udc_show
, PDE(inode
)->data
);
236 static const struct file_operations proc_ops
= {
237 .owner
= THIS_MODULE
,
238 .open
= proc_udc_open
,
241 .release
= single_release
,
244 static void create_debug_file(struct at91_udc
*udc
)
246 udc
->pde
= proc_create_data(debug_filename
, 0, NULL
, &proc_ops
, udc
);
249 static void remove_debug_file(struct at91_udc
*udc
)
252 remove_proc_entry(debug_filename
, NULL
);
257 static inline void create_debug_file(struct at91_udc
*udc
) {}
258 static inline void remove_debug_file(struct at91_udc
*udc
) {}
263 /*-------------------------------------------------------------------------*/
265 static void done(struct at91_ep
*ep
, struct at91_request
*req
, int status
)
267 unsigned stopped
= ep
->stopped
;
268 struct at91_udc
*udc
= ep
->udc
;
270 list_del_init(&req
->queue
);
271 if (req
->req
.status
== -EINPROGRESS
)
272 req
->req
.status
= status
;
274 status
= req
->req
.status
;
275 if (status
&& status
!= -ESHUTDOWN
)
276 VDBG("%s done %p, status %d\n", ep
->ep
.name
, req
, status
);
279 spin_unlock(&udc
->lock
);
280 req
->req
.complete(&ep
->ep
, &req
->req
);
281 spin_lock(&udc
->lock
);
282 ep
->stopped
= stopped
;
284 /* ep0 is always ready; other endpoints need a non-empty queue */
285 if (list_empty(&ep
->queue
) && ep
->int_mask
!= (1 << 0))
286 at91_udp_write(udc
, AT91_UDP_IDR
, ep
->int_mask
);
289 /*-------------------------------------------------------------------------*/
291 /* bits indicating OUT fifo has data ready */
292 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
295 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
296 * back most of the value you just read (because of side effects, including
297 * bits that may change after reading and before writing).
299 * Except when changing a specific bit, always write values which:
300 * - clear SET_FX bits (setting them could change something)
301 * - set CLR_FX bits (clearing them could change something)
303 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
304 * that shouldn't normally be changed.
306 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
307 * implying a need to wait for one write to complete (test relevant bits)
308 * before starting the next write. This shouldn't be an issue given how
309 * infrequently we write, except maybe for write-then-read idioms.
311 #define SET_FX (AT91_UDP_TXPKTRDY)
312 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
313 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
315 /* pull OUT packet data from the endpoint's fifo */
316 static int read_fifo (struct at91_ep
*ep
, struct at91_request
*req
)
318 u32 __iomem
*creg
= ep
->creg
;
319 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
322 unsigned int count
, bufferspace
, is_done
;
324 buf
= req
->req
.buf
+ req
->req
.actual
;
325 bufferspace
= req
->req
.length
- req
->req
.actual
;
328 * there might be nothing to read if ep_queue() calls us,
329 * or if we already emptied both pingpong buffers
332 csr
= __raw_readl(creg
);
333 if ((csr
& RX_DATA_READY
) == 0)
336 count
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
337 if (count
> ep
->ep
.maxpacket
)
338 count
= ep
->ep
.maxpacket
;
339 if (count
> bufferspace
) {
340 DBG("%s buffer overflow\n", ep
->ep
.name
);
341 req
->req
.status
= -EOVERFLOW
;
344 __raw_readsb(dreg
, buf
, count
);
346 /* release and swap pingpong mem bank */
348 if (ep
->is_pingpong
) {
349 if (ep
->fifo_bank
== 0) {
350 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
353 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK1
);
357 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
358 __raw_writel(csr
, creg
);
360 req
->req
.actual
+= count
;
361 is_done
= (count
< ep
->ep
.maxpacket
);
362 if (count
== bufferspace
)
365 PACKET("%s %p out/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
366 is_done
? " (done)" : "");
369 * avoid extra trips through IRQ logic for packets already in
370 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
374 else if (ep
->is_pingpong
) {
376 * One dummy read to delay the code because of a HW glitch:
377 * CSR returns bad RXCOUNT when read too soon after updating
380 csr
= __raw_readl(creg
);
382 bufferspace
-= count
;
390 /* load fifo for an IN packet */
391 static int write_fifo(struct at91_ep
*ep
, struct at91_request
*req
)
393 u32 __iomem
*creg
= ep
->creg
;
394 u32 csr
= __raw_readl(creg
);
395 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
396 unsigned total
, count
, is_last
;
400 * TODO: allow for writing two packets to the fifo ... that'll
401 * reduce the amount of IN-NAKing, but probably won't affect
402 * throughput much. (Unlike preventing OUT-NAKing!)
406 * If ep_queue() calls us, the queue is empty and possibly in
407 * odd states like TXCOMP not yet cleared (we do it, saving at
408 * least one IRQ) or the fifo not yet being free. Those aren't
409 * issues normally (IRQ handler fast path).
411 if (unlikely(csr
& (AT91_UDP_TXCOMP
| AT91_UDP_TXPKTRDY
))) {
412 if (csr
& AT91_UDP_TXCOMP
) {
414 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
415 __raw_writel(csr
, creg
);
416 csr
= __raw_readl(creg
);
418 if (csr
& AT91_UDP_TXPKTRDY
)
422 buf
= req
->req
.buf
+ req
->req
.actual
;
424 total
= req
->req
.length
- req
->req
.actual
;
425 if (ep
->ep
.maxpacket
< total
) {
426 count
= ep
->ep
.maxpacket
;
430 is_last
= (count
< ep
->ep
.maxpacket
) || !req
->req
.zero
;
434 * Write the packet, maybe it's a ZLP.
436 * NOTE: incrementing req->actual before we receive the ACK means
437 * gadget driver IN bytecounts can be wrong in fault cases. That's
438 * fixable with PIO drivers like this one (save "count" here, and
439 * do the increment later on TX irq), but not for most DMA hardware.
441 * So all gadget drivers must accept that potential error. Some
442 * hardware supports precise fifo status reporting, letting them
443 * recover when the actual bytecount matters (e.g. for USB Test
444 * and Measurement Class devices).
446 __raw_writesb(dreg
, buf
, count
);
448 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
449 __raw_writel(csr
, creg
);
450 req
->req
.actual
+= count
;
452 PACKET("%s %p in/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
453 is_last
? " (done)" : "");
459 static void nuke(struct at91_ep
*ep
, int status
)
461 struct at91_request
*req
;
463 // terminer chaque requete dans la queue
465 if (list_empty(&ep
->queue
))
468 VDBG("%s %s\n", __func__
, ep
->ep
.name
);
469 while (!list_empty(&ep
->queue
)) {
470 req
= list_entry(ep
->queue
.next
, struct at91_request
, queue
);
471 done(ep
, req
, status
);
475 /*-------------------------------------------------------------------------*/
477 static int at91_ep_enable(struct usb_ep
*_ep
,
478 const struct usb_endpoint_descriptor
*desc
)
480 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
481 struct at91_udc
*udc
= ep
->udc
;
488 || _ep
->name
== ep0name
489 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
490 || (maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
)) == 0
491 || maxpacket
> ep
->maxpacket
) {
492 DBG("bad ep or descriptor\n");
496 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
497 DBG("bogus device state\n");
501 tmp
= usb_endpoint_type(desc
);
503 case USB_ENDPOINT_XFER_CONTROL
:
504 DBG("only one control endpoint\n");
506 case USB_ENDPOINT_XFER_INT
:
510 case USB_ENDPOINT_XFER_BULK
:
519 DBG("bogus maxpacket %d\n", maxpacket
);
521 case USB_ENDPOINT_XFER_ISOC
:
522 if (!ep
->is_pingpong
) {
523 DBG("iso requires double buffering\n");
530 spin_lock_irqsave(&udc
->lock
, flags
);
532 /* initialize endpoint to match this descriptor */
533 ep
->is_in
= usb_endpoint_dir_in(desc
);
534 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
);
539 tmp
|= AT91_UDP_EPEDS
;
540 __raw_writel(tmp
, ep
->creg
);
543 ep
->ep
.maxpacket
= maxpacket
;
546 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
547 * since endpoint resets don't reset hw pingpong state.
549 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
550 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
552 spin_unlock_irqrestore(&udc
->lock
, flags
);
556 static int at91_ep_disable (struct usb_ep
* _ep
)
558 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
559 struct at91_udc
*udc
= ep
->udc
;
562 if (ep
== &ep
->udc
->ep
[0])
565 spin_lock_irqsave(&udc
->lock
, flags
);
567 nuke(ep
, -ESHUTDOWN
);
569 /* restore the endpoint's pristine config */
571 ep
->ep
.maxpacket
= ep
->maxpacket
;
573 /* reset fifos and endpoint */
574 if (ep
->udc
->clocked
) {
575 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
576 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
577 __raw_writel(0, ep
->creg
);
580 spin_unlock_irqrestore(&udc
->lock
, flags
);
585 * this is a PIO-only driver, so there's nothing
586 * interesting for request or buffer allocation.
589 static struct usb_request
*
590 at91_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
592 struct at91_request
*req
;
594 req
= kzalloc(sizeof (struct at91_request
), gfp_flags
);
598 INIT_LIST_HEAD(&req
->queue
);
602 static void at91_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
604 struct at91_request
*req
;
606 req
= container_of(_req
, struct at91_request
, req
);
607 BUG_ON(!list_empty(&req
->queue
));
611 static int at91_ep_queue(struct usb_ep
*_ep
,
612 struct usb_request
*_req
, gfp_t gfp_flags
)
614 struct at91_request
*req
;
616 struct at91_udc
*udc
;
620 req
= container_of(_req
, struct at91_request
, req
);
621 ep
= container_of(_ep
, struct at91_ep
, ep
);
623 if (!_req
|| !_req
->complete
624 || !_req
->buf
|| !list_empty(&req
->queue
)) {
625 DBG("invalid request\n");
629 if (!_ep
|| (!ep
->desc
&& ep
->ep
.name
!= ep0name
)) {
636 if (!udc
|| !udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
637 DBG("invalid device\n");
641 _req
->status
= -EINPROGRESS
;
644 spin_lock_irqsave(&udc
->lock
, flags
);
646 /* try to kickstart any empty and idle queue */
647 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
651 * If this control request has a non-empty DATA stage, this
652 * will start that stage. It works just like a non-control
653 * request (until the status stage starts, maybe early).
655 * If the data stage is empty, then this starts a successful
656 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
658 is_ep0
= (ep
->ep
.name
== ep0name
);
662 if (!udc
->req_pending
) {
668 * defer changing CONFG until after the gadget driver
669 * reconfigures the endpoints.
671 if (udc
->wait_for_config_ack
) {
672 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
673 tmp
^= AT91_UDP_CONFG
;
674 VDBG("toggle config\n");
675 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
677 if (req
->req
.length
== 0) {
679 PACKET("ep0 in/status\n");
681 tmp
= __raw_readl(ep
->creg
);
683 tmp
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
684 __raw_writel(tmp
, ep
->creg
);
685 udc
->req_pending
= 0;
691 status
= write_fifo(ep
, req
);
693 status
= read_fifo(ep
, req
);
695 /* IN/STATUS stage is otherwise triggered by irq */
696 if (status
&& is_ep0
)
702 if (req
&& !status
) {
703 list_add_tail (&req
->queue
, &ep
->queue
);
704 at91_udp_write(udc
, AT91_UDP_IER
, ep
->int_mask
);
707 spin_unlock_irqrestore(&udc
->lock
, flags
);
708 return (status
< 0) ? status
: 0;
711 static int at91_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
714 struct at91_request
*req
;
716 struct at91_udc
*udc
;
718 ep
= container_of(_ep
, struct at91_ep
, ep
);
719 if (!_ep
|| ep
->ep
.name
== ep0name
)
724 spin_lock_irqsave(&udc
->lock
, flags
);
726 /* make sure it's actually queued on this endpoint */
727 list_for_each_entry (req
, &ep
->queue
, queue
) {
728 if (&req
->req
== _req
)
731 if (&req
->req
!= _req
) {
732 spin_unlock_irqrestore(&udc
->lock
, flags
);
736 done(ep
, req
, -ECONNRESET
);
737 spin_unlock_irqrestore(&udc
->lock
, flags
);
741 static int at91_ep_set_halt(struct usb_ep
*_ep
, int value
)
743 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
744 struct at91_udc
*udc
= ep
->udc
;
750 if (!_ep
|| ep
->is_iso
|| !ep
->udc
->clocked
)
754 spin_lock_irqsave(&udc
->lock
, flags
);
756 csr
= __raw_readl(creg
);
759 * fail with still-busy IN endpoints, ensuring correct sequencing
760 * of data tx then stall. note that the fifo rx bytecount isn't
761 * completely accurate as a tx bytecount.
763 if (ep
->is_in
&& (!list_empty(&ep
->queue
) || (csr
>> 16) != 0))
769 csr
|= AT91_UDP_FORCESTALL
;
770 VDBG("halt %s\n", ep
->ep
.name
);
772 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
773 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
774 csr
&= ~AT91_UDP_FORCESTALL
;
776 __raw_writel(csr
, creg
);
779 spin_unlock_irqrestore(&udc
->lock
, flags
);
783 static const struct usb_ep_ops at91_ep_ops
= {
784 .enable
= at91_ep_enable
,
785 .disable
= at91_ep_disable
,
786 .alloc_request
= at91_ep_alloc_request
,
787 .free_request
= at91_ep_free_request
,
788 .queue
= at91_ep_queue
,
789 .dequeue
= at91_ep_dequeue
,
790 .set_halt
= at91_ep_set_halt
,
791 // there's only imprecise fifo status reporting
794 /*-------------------------------------------------------------------------*/
796 static int at91_get_frame(struct usb_gadget
*gadget
)
798 struct at91_udc
*udc
= to_udc(gadget
);
800 if (!to_udc(gadget
)->clocked
)
802 return at91_udp_read(udc
, AT91_UDP_FRM_NUM
) & AT91_UDP_NUM
;
805 static int at91_wakeup(struct usb_gadget
*gadget
)
807 struct at91_udc
*udc
= to_udc(gadget
);
809 int status
= -EINVAL
;
812 DBG("%s\n", __func__
);
813 spin_lock_irqsave(&udc
->lock
, flags
);
815 if (!udc
->clocked
|| !udc
->suspended
)
818 /* NOTE: some "early versions" handle ESR differently ... */
820 glbstate
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
821 if (!(glbstate
& AT91_UDP_ESR
))
823 glbstate
|= AT91_UDP_ESR
;
824 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, glbstate
);
827 spin_unlock_irqrestore(&udc
->lock
, flags
);
831 /* reinit == restore initial software state */
832 static void udc_reinit(struct at91_udc
*udc
)
836 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
837 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
839 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
840 struct at91_ep
*ep
= &udc
->ep
[i
];
843 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
847 ep
->ep
.maxpacket
= ep
->maxpacket
;
848 ep
->creg
= (void __iomem
*) udc
->udp_baseaddr
+ AT91_UDP_CSR(i
);
849 // initialiser une queue par endpoint
850 INIT_LIST_HEAD(&ep
->queue
);
854 static void stop_activity(struct at91_udc
*udc
)
856 struct usb_gadget_driver
*driver
= udc
->driver
;
859 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
861 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
864 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
865 struct at91_ep
*ep
= &udc
->ep
[i
];
867 nuke(ep
, -ESHUTDOWN
);
870 spin_unlock(&udc
->lock
);
871 driver
->disconnect(&udc
->gadget
);
872 spin_lock(&udc
->lock
);
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
->fclk
);
894 clk_disable(udc
->iclk
);
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 int active
= !udc
->board
.pullup_active_low
;
905 if (!udc
->enabled
|| !udc
->vbus
)
907 DBG("%sactive\n", is_on
? "" : "in");
911 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXRSM
);
912 at91_udp_write(udc
, AT91_UDP_TXVC
, 0);
913 if (cpu_is_at91rm9200())
914 gpio_set_value(udc
->board
.pullup_pin
, active
);
915 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
916 u32 txvc
= at91_udp_read(udc
, AT91_UDP_TXVC
);
918 txvc
|= AT91_UDP_TXVC_PUON
;
919 at91_udp_write(udc
, AT91_UDP_TXVC
, txvc
);
920 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
923 usbpucr
= at91_sys_read(AT91_MATRIX_USBPUCR
);
924 usbpucr
|= AT91_MATRIX_USBPUCR_PUON
;
925 at91_sys_write(AT91_MATRIX_USBPUCR
, usbpucr
);
929 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXRSM
);
930 at91_udp_write(udc
, AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
931 if (cpu_is_at91rm9200())
932 gpio_set_value(udc
->board
.pullup_pin
, !active
);
933 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263() || cpu_is_at91sam9g20()) {
934 u32 txvc
= at91_udp_read(udc
, AT91_UDP_TXVC
);
936 txvc
&= ~AT91_UDP_TXVC_PUON
;
937 at91_udp_write(udc
, AT91_UDP_TXVC
, txvc
);
938 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
941 usbpucr
= at91_sys_read(AT91_MATRIX_USBPUCR
);
942 usbpucr
&= ~AT91_MATRIX_USBPUCR_PUON
;
943 at91_sys_write(AT91_MATRIX_USBPUCR
, usbpucr
);
949 /* vbus is here! turn everything on that's ready */
950 static int at91_vbus_session(struct usb_gadget
*gadget
, int is_active
)
952 struct at91_udc
*udc
= to_udc(gadget
);
955 // VDBG("vbus %s\n", is_active ? "on" : "off");
956 spin_lock_irqsave(&udc
->lock
, flags
);
957 udc
->vbus
= (is_active
!= 0);
959 pullup(udc
, is_active
);
962 spin_unlock_irqrestore(&udc
->lock
, flags
);
966 static int at91_pullup(struct usb_gadget
*gadget
, int is_on
)
968 struct at91_udc
*udc
= to_udc(gadget
);
971 spin_lock_irqsave(&udc
->lock
, flags
);
972 udc
->enabled
= is_on
= !!is_on
;
974 spin_unlock_irqrestore(&udc
->lock
, flags
);
978 static int at91_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
980 struct at91_udc
*udc
= to_udc(gadget
);
983 spin_lock_irqsave(&udc
->lock
, flags
);
984 udc
->selfpowered
= (is_on
!= 0);
985 spin_unlock_irqrestore(&udc
->lock
, flags
);
989 static int at91_start(struct usb_gadget_driver
*driver
,
990 int (*bind
)(struct usb_gadget
*));
991 static int at91_stop(struct usb_gadget_driver
*driver
);
993 static const struct usb_gadget_ops at91_udc_ops
= {
994 .get_frame
= at91_get_frame
,
995 .wakeup
= at91_wakeup
,
996 .set_selfpowered
= at91_set_selfpowered
,
997 .vbus_session
= at91_vbus_session
,
998 .pullup
= at91_pullup
,
1003 * VBUS-powered devices may also also want to support bigger
1004 * power budgets after an appropriate SET_CONFIGURATION.
1006 // .vbus_power = at91_vbus_power,
1009 /*-------------------------------------------------------------------------*/
1011 static int handle_ep(struct at91_ep
*ep
)
1013 struct at91_request
*req
;
1014 u32 __iomem
*creg
= ep
->creg
;
1015 u32 csr
= __raw_readl(creg
);
1017 if (!list_empty(&ep
->queue
))
1018 req
= list_entry(ep
->queue
.next
,
1019 struct at91_request
, queue
);
1024 if (csr
& (AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
)) {
1026 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
);
1027 __raw_writel(csr
, creg
);
1030 return write_fifo(ep
, req
);
1033 if (csr
& AT91_UDP_STALLSENT
) {
1034 /* STALLSENT bit == ISOERR */
1035 if (ep
->is_iso
&& req
)
1036 req
->req
.status
= -EILSEQ
;
1038 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
);
1039 __raw_writel(csr
, creg
);
1040 csr
= __raw_readl(creg
);
1042 if (req
&& (csr
& RX_DATA_READY
))
1043 return read_fifo(ep
, req
);
1050 struct usb_ctrlrequest r
;
1053 static void handle_setup(struct at91_udc
*udc
, struct at91_ep
*ep
, u32 csr
)
1055 u32 __iomem
*creg
= ep
->creg
;
1056 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1057 unsigned rxcount
, i
= 0;
1062 /* read and ack SETUP; hard-fail for bogus packets */
1063 rxcount
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
1064 if (likely(rxcount
== 8)) {
1066 pkt
.raw
[i
++] = __raw_readb(dreg
);
1067 if (pkt
.r
.bRequestType
& USB_DIR_IN
) {
1068 csr
|= AT91_UDP_DIR
;
1071 csr
&= ~AT91_UDP_DIR
;
1075 // REVISIT this happens sometimes under load; why??
1076 ERR("SETUP len %d, csr %08x\n", rxcount
, csr
);
1080 csr
&= ~(SET_FX
| AT91_UDP_RXSETUP
);
1081 __raw_writel(csr
, creg
);
1082 udc
->wait_for_addr_ack
= 0;
1083 udc
->wait_for_config_ack
= 0;
1085 if (unlikely(status
!= 0))
1088 #define w_index le16_to_cpu(pkt.r.wIndex)
1089 #define w_value le16_to_cpu(pkt.r.wValue)
1090 #define w_length le16_to_cpu(pkt.r.wLength)
1092 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1093 pkt
.r
.bRequestType
, pkt
.r
.bRequest
,
1094 w_value
, w_index
, w_length
);
1097 * A few standard requests get handled here, ones that touch
1098 * hardware ... notably for device and endpoint features.
1100 udc
->req_pending
= 1;
1101 csr
= __raw_readl(creg
);
1104 switch ((pkt
.r
.bRequestType
<< 8) | pkt
.r
.bRequest
) {
1106 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1107 | USB_REQ_SET_ADDRESS
:
1108 __raw_writel(csr
| AT91_UDP_TXPKTRDY
, creg
);
1109 udc
->addr
= w_value
;
1110 udc
->wait_for_addr_ack
= 1;
1111 udc
->req_pending
= 0;
1112 /* FADDR is set later, when we ack host STATUS */
1115 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1116 | USB_REQ_SET_CONFIGURATION
:
1117 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
) & AT91_UDP_CONFG
;
1119 udc
->wait_for_config_ack
= (tmp
== 0);
1121 udc
->wait_for_config_ack
= (tmp
!= 0);
1122 if (udc
->wait_for_config_ack
)
1123 VDBG("wait for config\n");
1124 /* CONFG is toggled later, if gadget driver succeeds */
1128 * Hosts may set or clear remote wakeup status, and
1129 * devices may report they're VBUS powered.
1131 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1132 | USB_REQ_GET_STATUS
:
1133 tmp
= (udc
->selfpowered
<< USB_DEVICE_SELF_POWERED
);
1134 if (at91_udp_read(udc
, AT91_UDP_GLB_STAT
) & AT91_UDP_ESR
)
1135 tmp
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
1136 PACKET("get device status\n");
1137 __raw_writeb(tmp
, dreg
);
1138 __raw_writeb(0, dreg
);
1140 /* then STATUS starts later, automatically */
1141 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1142 | USB_REQ_SET_FEATURE
:
1143 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1145 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1146 tmp
|= AT91_UDP_ESR
;
1147 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1149 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1150 | USB_REQ_CLEAR_FEATURE
:
1151 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1153 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1154 tmp
&= ~AT91_UDP_ESR
;
1155 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1159 * Interfaces have no feature settings; this is pretty useless.
1160 * we won't even insist the interface exists...
1162 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1163 | USB_REQ_GET_STATUS
:
1164 PACKET("get interface status\n");
1165 __raw_writeb(0, dreg
);
1166 __raw_writeb(0, dreg
);
1168 /* then STATUS starts later, automatically */
1169 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1170 | USB_REQ_SET_FEATURE
:
1171 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1172 | USB_REQ_CLEAR_FEATURE
:
1176 * Hosts may clear bulk/intr endpoint halt after the gadget
1177 * driver sets it (not widely used); or set it (for testing)
1179 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1180 | USB_REQ_GET_STATUS
:
1181 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1183 if (tmp
>= NUM_ENDPOINTS
|| (tmp
&& !ep
->desc
))
1187 if ((w_index
& USB_DIR_IN
)) {
1190 } else if (ep
->is_in
)
1193 PACKET("get %s status\n", ep
->ep
.name
);
1194 if (__raw_readl(ep
->creg
) & AT91_UDP_FORCESTALL
)
1195 tmp
= (1 << USB_ENDPOINT_HALT
);
1198 __raw_writeb(tmp
, dreg
);
1199 __raw_writeb(0, dreg
);
1201 /* then STATUS starts later, automatically */
1202 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1203 | USB_REQ_SET_FEATURE
:
1204 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1206 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
>= NUM_ENDPOINTS
)
1208 if (!ep
->desc
|| ep
->is_iso
)
1210 if ((w_index
& USB_DIR_IN
)) {
1213 } else if (ep
->is_in
)
1216 tmp
= __raw_readl(ep
->creg
);
1218 tmp
|= CLR_FX
| AT91_UDP_FORCESTALL
;
1219 __raw_writel(tmp
, ep
->creg
);
1221 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1222 | USB_REQ_CLEAR_FEATURE
:
1223 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1225 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
>= NUM_ENDPOINTS
)
1229 if (!ep
->desc
|| ep
->is_iso
)
1231 if ((w_index
& USB_DIR_IN
)) {
1234 } else if (ep
->is_in
)
1237 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
1238 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
1239 tmp
= __raw_readl(ep
->creg
);
1241 tmp
&= ~(SET_FX
| AT91_UDP_FORCESTALL
);
1242 __raw_writel(tmp
, ep
->creg
);
1243 if (!list_empty(&ep
->queue
))
1252 /* pass request up to the gadget driver */
1254 spin_unlock(&udc
->lock
);
1255 status
= udc
->driver
->setup(&udc
->gadget
, &pkt
.r
);
1256 spin_lock(&udc
->lock
);
1262 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1263 pkt
.r
.bRequestType
, pkt
.r
.bRequest
, status
);
1264 csr
|= AT91_UDP_FORCESTALL
;
1265 __raw_writel(csr
, creg
);
1266 udc
->req_pending
= 0;
1271 /* immediate successful (IN) STATUS after zero length DATA */
1272 PACKET("ep0 in/status\n");
1274 csr
|= AT91_UDP_TXPKTRDY
;
1275 __raw_writel(csr
, creg
);
1276 udc
->req_pending
= 0;
1279 static void handle_ep0(struct at91_udc
*udc
)
1281 struct at91_ep
*ep0
= &udc
->ep
[0];
1282 u32 __iomem
*creg
= ep0
->creg
;
1283 u32 csr
= __raw_readl(creg
);
1284 struct at91_request
*req
;
1286 if (unlikely(csr
& AT91_UDP_STALLSENT
)) {
1288 udc
->req_pending
= 0;
1290 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_FORCESTALL
);
1291 __raw_writel(csr
, creg
);
1292 VDBG("ep0 stalled\n");
1293 csr
= __raw_readl(creg
);
1295 if (csr
& AT91_UDP_RXSETUP
) {
1297 udc
->req_pending
= 0;
1298 handle_setup(udc
, ep0
, csr
);
1302 if (list_empty(&ep0
->queue
))
1305 req
= list_entry(ep0
->queue
.next
, struct at91_request
, queue
);
1307 /* host ACKed an IN packet that we sent */
1308 if (csr
& AT91_UDP_TXCOMP
) {
1310 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
1312 /* write more IN DATA? */
1313 if (req
&& ep0
->is_in
) {
1315 udc
->req_pending
= 0;
1319 * - last IN DATA packet (including GET_STATUS)
1320 * - IN/STATUS for OUT DATA
1321 * - IN/STATUS for any zero-length DATA stage
1322 * except for the IN DATA case, the host should send
1323 * an OUT status later, which we'll ack.
1326 udc
->req_pending
= 0;
1327 __raw_writel(csr
, creg
);
1330 * SET_ADDRESS takes effect only after the STATUS
1331 * (to the original address) gets acked.
1333 if (udc
->wait_for_addr_ack
) {
1336 at91_udp_write(udc
, AT91_UDP_FADDR
,
1337 AT91_UDP_FEN
| udc
->addr
);
1338 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1339 tmp
&= ~AT91_UDP_FADDEN
;
1341 tmp
|= AT91_UDP_FADDEN
;
1342 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1344 udc
->wait_for_addr_ack
= 0;
1345 VDBG("address %d\n", udc
->addr
);
1350 /* OUT packet arrived ... */
1351 else if (csr
& AT91_UDP_RX_DATA_BK0
) {
1353 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
1355 /* OUT DATA stage */
1358 if (handle_ep(ep0
)) {
1359 /* send IN/STATUS */
1360 PACKET("ep0 in/status\n");
1361 csr
= __raw_readl(creg
);
1363 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
1364 __raw_writel(csr
, creg
);
1365 udc
->req_pending
= 0;
1367 } else if (udc
->req_pending
) {
1369 * AT91 hardware has a hard time with this
1370 * "deferred response" mode for control-OUT
1371 * transfers. (For control-IN it's fine.)
1373 * The normal solution leaves OUT data in the
1374 * fifo until the gadget driver is ready.
1375 * We couldn't do that here without disabling
1376 * the IRQ that tells about SETUP packets,
1377 * e.g. when the host gets impatient...
1379 * Working around it by copying into a buffer
1380 * would almost be a non-deferred response,
1381 * except that it wouldn't permit reliable
1382 * stalling of the request. Instead, demand
1383 * that gadget drivers not use this mode.
1385 DBG("no control-OUT deferred responses!\n");
1386 __raw_writel(csr
| AT91_UDP_FORCESTALL
, creg
);
1387 udc
->req_pending
= 0;
1390 /* STATUS stage for control-IN; ack. */
1392 PACKET("ep0 out/status ACK\n");
1393 __raw_writel(csr
, creg
);
1395 /* "early" status stage */
1402 static irqreturn_t
at91_udc_irq (int irq
, void *_udc
)
1404 struct at91_udc
*udc
= _udc
;
1406 int disable_clock
= 0;
1407 unsigned long flags
;
1409 spin_lock_irqsave(&udc
->lock
, flags
);
1411 if (!udc
->clocked
) {
1419 status
= at91_udp_read(udc
, AT91_UDP_ISR
)
1420 & at91_udp_read(udc
, AT91_UDP_IMR
);
1424 /* USB reset irq: not maskable */
1425 if (status
& AT91_UDP_ENDBUSRES
) {
1426 at91_udp_write(udc
, AT91_UDP_IDR
, ~MINIMUS_INTERRUPTUS
);
1427 at91_udp_write(udc
, AT91_UDP_IER
, MINIMUS_INTERRUPTUS
);
1428 /* Atmel code clears this irq twice */
1429 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1430 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1431 VDBG("end bus reset\n");
1436 at91_udp_write(udc
, AT91_UDP_CSR(0),
1437 AT91_UDP_EPEDS
| AT91_UDP_EPTYPE_CTRL
);
1438 udc
->gadget
.speed
= USB_SPEED_FULL
;
1440 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_EP(0));
1443 * NOTE: this driver keeps clocks off unless the
1444 * USB host is present. That saves power, but for
1445 * boards that don't support VBUS detection, both
1446 * clocks need to be active most of the time.
1449 /* host initiated suspend (3+ms bus idle) */
1450 } else if (status
& AT91_UDP_RXSUSP
) {
1451 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXSUSP
);
1452 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_RXRSM
);
1453 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXSUSP
);
1454 // VDBG("bus suspend\n");
1460 * NOTE: when suspending a VBUS-powered device, the
1461 * gadget driver should switch into slow clock mode
1462 * and then into standby to avoid drawing more than
1463 * 500uA power (2500uA for some high-power configs).
1465 if (udc
->driver
&& udc
->driver
->suspend
) {
1466 spin_unlock(&udc
->lock
);
1467 udc
->driver
->suspend(&udc
->gadget
);
1468 spin_lock(&udc
->lock
);
1471 /* host initiated resume */
1472 } else if (status
& AT91_UDP_RXRSM
) {
1473 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXRSM
);
1474 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_RXSUSP
);
1475 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXRSM
);
1476 // VDBG("bus resume\n");
1477 if (!udc
->suspended
)
1482 * NOTE: for a VBUS-powered device, the gadget driver
1483 * would normally want to switch out of slow clock
1484 * mode into normal mode.
1486 if (udc
->driver
&& udc
->driver
->resume
) {
1487 spin_unlock(&udc
->lock
);
1488 udc
->driver
->resume(&udc
->gadget
);
1489 spin_lock(&udc
->lock
);
1492 /* endpoint IRQs are cleared by handling them */
1496 struct at91_ep
*ep
= &udc
->ep
[1];
1500 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1512 spin_unlock_irqrestore(&udc
->lock
, flags
);
1517 /*-------------------------------------------------------------------------*/
1519 static void nop_release(struct device
*dev
)
1521 /* nothing to free */
1524 static struct at91_udc controller
= {
1526 .ops
= &at91_udc_ops
,
1527 .ep0
= &controller
.ep
[0].ep
,
1528 .name
= driver_name
,
1530 .init_name
= "gadget",
1531 .release
= nop_release
,
1537 .ops
= &at91_ep_ops
,
1546 .ops
= &at91_ep_ops
,
1556 .ops
= &at91_ep_ops
,
1565 /* could actually do bulk too */
1567 .ops
= &at91_ep_ops
,
1576 .ops
= &at91_ep_ops
,
1586 .ops
= &at91_ep_ops
,
1593 /* ep6 and ep7 are also reserved (custom silicon might use them) */
1596 static void at91_vbus_update(struct at91_udc
*udc
, unsigned value
)
1598 value
^= udc
->board
.vbus_active_low
;
1599 if (value
!= udc
->vbus
)
1600 at91_vbus_session(&udc
->gadget
, value
);
1603 static irqreturn_t
at91_vbus_irq(int irq
, void *_udc
)
1605 struct at91_udc
*udc
= _udc
;
1607 /* vbus needs at least brief debouncing */
1609 at91_vbus_update(udc
, gpio_get_value(udc
->board
.vbus_pin
));
1614 static void at91_vbus_timer_work(struct work_struct
*work
)
1616 struct at91_udc
*udc
= container_of(work
, struct at91_udc
,
1619 at91_vbus_update(udc
, gpio_get_value_cansleep(udc
->board
.vbus_pin
));
1621 if (!timer_pending(&udc
->vbus_timer
))
1622 mod_timer(&udc
->vbus_timer
, jiffies
+ VBUS_POLL_TIMEOUT
);
1625 static void at91_vbus_timer(unsigned long data
)
1627 struct at91_udc
*udc
= (struct at91_udc
*)data
;
1630 * If we are polling vbus it is likely that the gpio is on an
1631 * bus such as i2c or spi which may sleep, so schedule some work
1632 * to read the vbus gpio
1634 if (!work_pending(&udc
->vbus_timer_work
))
1635 schedule_work(&udc
->vbus_timer_work
);
1638 static int at91_start(struct usb_gadget_driver
*driver
,
1639 int (*bind
)(struct usb_gadget
*))
1641 struct at91_udc
*udc
= &controller
;
1643 unsigned long flags
;
1646 || driver
->speed
< USB_SPEED_FULL
1648 || !driver
->setup
) {
1649 DBG("bad parameter.\n");
1654 DBG("UDC already has a gadget driver\n");
1658 udc
->driver
= driver
;
1659 udc
->gadget
.dev
.driver
= &driver
->driver
;
1660 dev_set_drvdata(&udc
->gadget
.dev
, &driver
->driver
);
1662 udc
->selfpowered
= 1;
1664 retval
= bind(&udc
->gadget
);
1666 DBG("bind() returned %d\n", retval
);
1668 udc
->gadget
.dev
.driver
= NULL
;
1669 dev_set_drvdata(&udc
->gadget
.dev
, NULL
);
1671 udc
->selfpowered
= 0;
1675 spin_lock_irqsave(&udc
->lock
, flags
);
1677 spin_unlock_irqrestore(&udc
->lock
, flags
);
1679 DBG("bound to %s\n", driver
->driver
.name
);
1683 static int at91_stop(struct usb_gadget_driver
*driver
)
1685 struct at91_udc
*udc
= &controller
;
1686 unsigned long flags
;
1688 if (!driver
|| driver
!= udc
->driver
|| !driver
->unbind
)
1691 spin_lock_irqsave(&udc
->lock
, flags
);
1693 at91_udp_write(udc
, AT91_UDP_IDR
, ~0);
1695 spin_unlock_irqrestore(&udc
->lock
, flags
);
1697 driver
->unbind(&udc
->gadget
);
1698 udc
->gadget
.dev
.driver
= NULL
;
1699 dev_set_drvdata(&udc
->gadget
.dev
, NULL
);
1702 DBG("unbound from %s\n", driver
->driver
.name
);
1706 /*-------------------------------------------------------------------------*/
1708 static void at91udc_shutdown(struct platform_device
*dev
)
1710 struct at91_udc
*udc
= platform_get_drvdata(dev
);
1711 unsigned long flags
;
1713 /* force disconnect on reboot */
1714 spin_lock_irqsave(&udc
->lock
, flags
);
1715 pullup(platform_get_drvdata(dev
), 0);
1716 spin_unlock_irqrestore(&udc
->lock
, flags
);
1719 static int __init
at91udc_probe(struct platform_device
*pdev
)
1721 struct device
*dev
= &pdev
->dev
;
1722 struct at91_udc
*udc
;
1724 struct resource
*res
;
1726 if (!dev
->platform_data
) {
1727 /* small (so we copy it) but critical! */
1728 DBG("missing platform_data\n");
1732 if (pdev
->num_resources
!= 2) {
1733 DBG("invalid num_resources\n");
1736 if ((pdev
->resource
[0].flags
!= IORESOURCE_MEM
)
1737 || (pdev
->resource
[1].flags
!= IORESOURCE_IRQ
)) {
1738 DBG("invalid resource type\n");
1742 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1746 if (!request_mem_region(res
->start
, resource_size(res
), driver_name
)) {
1747 DBG("someone's using UDC memory\n");
1751 /* init software state */
1753 udc
->gadget
.dev
.parent
= dev
;
1754 udc
->board
= *(struct at91_udc_data
*) dev
->platform_data
;
1757 spin_lock_init(&udc
->lock
);
1759 /* rm9200 needs manual D+ pullup; off by default */
1760 if (cpu_is_at91rm9200()) {
1761 if (udc
->board
.pullup_pin
<= 0) {
1762 DBG("no D+ pullup?\n");
1766 retval
= gpio_request(udc
->board
.pullup_pin
, "udc_pullup");
1768 DBG("D+ pullup is busy\n");
1771 gpio_direction_output(udc
->board
.pullup_pin
,
1772 udc
->board
.pullup_active_low
);
1775 /* newer chips have more FIFO memory than rm9200 */
1776 if (cpu_is_at91sam9260() || cpu_is_at91sam9g20()) {
1777 udc
->ep
[0].maxpacket
= 64;
1778 udc
->ep
[3].maxpacket
= 64;
1779 udc
->ep
[4].maxpacket
= 512;
1780 udc
->ep
[5].maxpacket
= 512;
1781 } else if (cpu_is_at91sam9261() || cpu_is_at91sam9g10()) {
1782 udc
->ep
[3].maxpacket
= 64;
1783 } else if (cpu_is_at91sam9263()) {
1784 udc
->ep
[0].maxpacket
= 64;
1785 udc
->ep
[3].maxpacket
= 64;
1788 udc
->udp_baseaddr
= ioremap(res
->start
, resource_size(res
));
1789 if (!udc
->udp_baseaddr
) {
1796 /* get interface and function clocks */
1797 udc
->iclk
= clk_get(dev
, "udc_clk");
1798 udc
->fclk
= clk_get(dev
, "udpck");
1799 if (IS_ERR(udc
->iclk
) || IS_ERR(udc
->fclk
)) {
1800 DBG("clocks missing\n");
1802 /* NOTE: we "know" here that refcounts on these are NOPs */
1806 retval
= device_register(&udc
->gadget
.dev
);
1808 put_device(&udc
->gadget
.dev
);
1812 /* don't do anything until we have both gadget driver and VBUS */
1813 clk_enable(udc
->iclk
);
1814 at91_udp_write(udc
, AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
1815 at91_udp_write(udc
, AT91_UDP_IDR
, 0xffffffff);
1816 /* Clear all pending interrupts - UDP may be used by bootloader. */
1817 at91_udp_write(udc
, AT91_UDP_ICR
, 0xffffffff);
1818 clk_disable(udc
->iclk
);
1820 /* request UDC and maybe VBUS irqs */
1821 udc
->udp_irq
= platform_get_irq(pdev
, 0);
1822 retval
= request_irq(udc
->udp_irq
, at91_udc_irq
,
1823 IRQF_DISABLED
, driver_name
, udc
);
1825 DBG("request irq %d failed\n", udc
->udp_irq
);
1828 if (udc
->board
.vbus_pin
> 0) {
1829 retval
= gpio_request(udc
->board
.vbus_pin
, "udc_vbus");
1831 DBG("request vbus pin failed\n");
1834 gpio_direction_input(udc
->board
.vbus_pin
);
1837 * Get the initial state of VBUS - we cannot expect
1838 * a pending interrupt.
1840 udc
->vbus
= gpio_get_value_cansleep(udc
->board
.vbus_pin
) ^
1841 udc
->board
.vbus_active_low
;
1843 if (udc
->board
.vbus_polled
) {
1844 INIT_WORK(&udc
->vbus_timer_work
, at91_vbus_timer_work
);
1845 setup_timer(&udc
->vbus_timer
, at91_vbus_timer
,
1846 (unsigned long)udc
);
1847 mod_timer(&udc
->vbus_timer
,
1848 jiffies
+ VBUS_POLL_TIMEOUT
);
1850 if (request_irq(udc
->board
.vbus_pin
, at91_vbus_irq
,
1851 IRQF_DISABLED
, driver_name
, udc
)) {
1852 DBG("request vbus irq %d failed\n",
1853 udc
->board
.vbus_pin
);
1859 DBG("no VBUS detection, assuming always-on\n");
1862 retval
= usb_add_gadget_udc(dev
, &udc
->gadget
);
1865 dev_set_drvdata(dev
, udc
);
1866 device_init_wakeup(dev
, 1);
1867 create_debug_file(udc
);
1869 INFO("%s version %s\n", driver_name
, DRIVER_VERSION
);
1872 if (udc
->board
.vbus_pin
> 0 && !udc
->board
.vbus_polled
)
1873 free_irq(udc
->board
.vbus_pin
, udc
);
1875 if (udc
->board
.vbus_pin
> 0)
1876 gpio_free(udc
->board
.vbus_pin
);
1878 free_irq(udc
->udp_irq
, udc
);
1880 device_unregister(&udc
->gadget
.dev
);
1882 iounmap(udc
->udp_baseaddr
);
1884 if (cpu_is_at91rm9200())
1885 gpio_free(udc
->board
.pullup_pin
);
1887 release_mem_region(res
->start
, resource_size(res
));
1888 DBG("%s probe failed, %d\n", driver_name
, retval
);
1892 static int __exit
at91udc_remove(struct platform_device
*pdev
)
1894 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1895 struct resource
*res
;
1896 unsigned long flags
;
1900 usb_del_gadget_udc(&udc
->gadget
);
1904 spin_lock_irqsave(&udc
->lock
, flags
);
1906 spin_unlock_irqrestore(&udc
->lock
, flags
);
1908 device_init_wakeup(&pdev
->dev
, 0);
1909 remove_debug_file(udc
);
1910 if (udc
->board
.vbus_pin
> 0) {
1911 free_irq(udc
->board
.vbus_pin
, udc
);
1912 gpio_free(udc
->board
.vbus_pin
);
1914 free_irq(udc
->udp_irq
, udc
);
1915 device_unregister(&udc
->gadget
.dev
);
1917 iounmap(udc
->udp_baseaddr
);
1919 if (cpu_is_at91rm9200())
1920 gpio_free(udc
->board
.pullup_pin
);
1922 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1923 release_mem_region(res
->start
, resource_size(res
));
1932 static int at91udc_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1934 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1935 int wake
= udc
->driver
&& device_may_wakeup(&pdev
->dev
);
1936 unsigned long flags
;
1938 /* Unless we can act normally to the host (letting it wake us up
1939 * whenever it has work for us) force disconnect. Wakeup requires
1940 * PLLB for USB events (signaling for reset, wakeup, or incoming
1941 * tokens) and VBUS irqs (on systems which support them).
1943 if ((!udc
->suspended
&& udc
->addr
)
1945 || at91_suspend_entering_slow_clock()) {
1946 spin_lock_irqsave(&udc
->lock
, flags
);
1949 spin_unlock_irqrestore(&udc
->lock
, flags
);
1951 enable_irq_wake(udc
->udp_irq
);
1953 udc
->active_suspend
= wake
;
1954 if (udc
->board
.vbus_pin
> 0 && !udc
->board
.vbus_polled
&& wake
)
1955 enable_irq_wake(udc
->board
.vbus_pin
);
1959 static int at91udc_resume(struct platform_device
*pdev
)
1961 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1962 unsigned long flags
;
1964 if (udc
->board
.vbus_pin
> 0 && !udc
->board
.vbus_polled
&&
1965 udc
->active_suspend
)
1966 disable_irq_wake(udc
->board
.vbus_pin
);
1968 /* maybe reconnect to host; if so, clocks on */
1969 if (udc
->active_suspend
)
1970 disable_irq_wake(udc
->udp_irq
);
1972 spin_lock_irqsave(&udc
->lock
, flags
);
1974 spin_unlock_irqrestore(&udc
->lock
, flags
);
1979 #define at91udc_suspend NULL
1980 #define at91udc_resume NULL
1983 static struct platform_driver at91_udc_driver
= {
1984 .remove
= __exit_p(at91udc_remove
),
1985 .shutdown
= at91udc_shutdown
,
1986 .suspend
= at91udc_suspend
,
1987 .resume
= at91udc_resume
,
1989 .name
= (char *) driver_name
,
1990 .owner
= THIS_MODULE
,
1994 static int __init
udc_init_module(void)
1996 return platform_driver_probe(&at91_udc_driver
, at91udc_probe
);
1998 module_init(udc_init_module
);
2000 static void __exit
udc_exit_module(void)
2002 platform_driver_unregister(&at91_udc_driver
);
2004 module_exit(udc_exit_module
);
2006 MODULE_DESCRIPTION("AT91 udc driver");
2007 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
2008 MODULE_LICENSE("GPL");
2009 MODULE_ALIAS("platform:at91_udc");