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.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/errno.h>
24 #include <linux/list.h>
25 #include <linux/interrupt.h>
26 #include <linux/proc_fs.h>
27 #include <linux/prefetch.h>
28 #include <linux/clk.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/gadget.h>
32 #include <linux/of_gpio.h>
33 #include <linux/platform_data/atmel.h>
34 #include <linux/regmap.h>
35 #include <linux/mfd/syscon.h>
36 #include <linux/mfd/syscon/atmel-matrix.h>
42 * This controller is simple and PIO-only. It's used in many AT91-series
43 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
44 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
46 * This driver expects the board has been wired with two GPIOs supporting
47 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
48 * testing hasn't covered such cases.)
50 * The pullup is most important (so it's integrated on sam926x parts). It
51 * provides software control over whether the host enumerates the device.
53 * The VBUS sensing helps during enumeration, and allows both USB clocks
54 * (and the transceiver) to stay gated off until they're necessary, saving
55 * power. During USB suspend, the 48 MHz clock is gated off in hardware;
56 * it may also be gated off by software during some Linux sleep states.
59 #define DRIVER_VERSION "3 May 2006"
61 static const char driver_name
[] = "at91_udc";
65 const struct usb_ep_caps caps
;
67 #define EP_INFO(_name, _caps) \
74 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL
, USB_EP_CAPS_DIR_ALL
)),
76 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
78 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
80 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT
, USB_EP_CAPS_DIR_ALL
)),
82 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
84 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL
, USB_EP_CAPS_DIR_ALL
)),
89 #define ep0name ep_info[0].name
91 #define VBUS_POLL_TIMEOUT msecs_to_jiffies(1000)
93 #define at91_udp_read(udc, reg) \
94 __raw_readl((udc)->udp_baseaddr + (reg))
95 #define at91_udp_write(udc, reg, val) \
96 __raw_writel((val), (udc)->udp_baseaddr + (reg))
98 /*-------------------------------------------------------------------------*/
100 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
102 #include <linux/seq_file.h>
104 static const char debug_filename
[] = "driver/udc";
106 #define FOURBITS "%s%s%s%s"
107 #define EIGHTBITS FOURBITS FOURBITS
109 static void proc_ep_show(struct seq_file
*s
, struct at91_ep
*ep
)
111 static char *types
[] = {
112 "control", "out-iso", "out-bulk", "out-int",
113 "BOGUS", "in-iso", "in-bulk", "in-int"};
116 struct at91_request
*req
;
118 struct at91_udc
*udc
= ep
->udc
;
120 spin_lock_irqsave(&udc
->lock
, flags
);
122 csr
= __raw_readl(ep
->creg
);
124 /* NOTE: not collecting per-endpoint irq statistics... */
127 seq_printf(s
, "%s, maxpacket %d %s%s %s%s\n",
128 ep
->ep
.name
, ep
->ep
.maxpacket
,
129 ep
->is_in
? "in" : "out",
130 ep
->is_iso
? " iso" : "",
132 ? (ep
->fifo_bank
? "pong" : "ping")
134 ep
->stopped
? " stopped" : "");
135 seq_printf(s
, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS
"\n",
137 (csr
& 0x07ff0000) >> 16,
138 (csr
& (1 << 15)) ? "enabled" : "disabled",
139 (csr
& (1 << 11)) ? "DATA1" : "DATA0",
140 types
[(csr
& 0x700) >> 8],
142 /* iff type is control then print current direction */
144 ? ((csr
& (1 << 7)) ? " IN" : " OUT")
146 (csr
& (1 << 6)) ? " rxdatabk1" : "",
147 (csr
& (1 << 5)) ? " forcestall" : "",
148 (csr
& (1 << 4)) ? " txpktrdy" : "",
150 (csr
& (1 << 3)) ? " stallsent" : "",
151 (csr
& (1 << 2)) ? " rxsetup" : "",
152 (csr
& (1 << 1)) ? " rxdatabk0" : "",
153 (csr
& (1 << 0)) ? " txcomp" : "");
154 if (list_empty (&ep
->queue
))
155 seq_printf(s
, "\t(queue empty)\n");
157 else list_for_each_entry (req
, &ep
->queue
, queue
) {
158 unsigned length
= req
->req
.actual
;
160 seq_printf(s
, "\treq %p len %d/%d buf %p\n",
162 req
->req
.length
, req
->req
.buf
);
164 spin_unlock_irqrestore(&udc
->lock
, flags
);
167 static void proc_irq_show(struct seq_file
*s
, const char *label
, u32 mask
)
171 seq_printf(s
, "%s %04x:%s%s" FOURBITS
, label
, mask
,
172 (mask
& (1 << 13)) ? " wakeup" : "",
173 (mask
& (1 << 12)) ? " endbusres" : "",
175 (mask
& (1 << 11)) ? " sofint" : "",
176 (mask
& (1 << 10)) ? " extrsm" : "",
177 (mask
& (1 << 9)) ? " rxrsm" : "",
178 (mask
& (1 << 8)) ? " rxsusp" : "");
179 for (i
= 0; i
< 8; i
++) {
181 seq_printf(s
, " ep%d", i
);
186 static int proc_udc_show(struct seq_file
*s
, void *unused
)
188 struct at91_udc
*udc
= s
->private;
192 seq_printf(s
, "%s: version %s\n", driver_name
, DRIVER_VERSION
);
194 seq_printf(s
, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
195 udc
->vbus
? "present" : "off",
197 ? (udc
->vbus
? "active" : "enabled")
199 udc
->gadget
.is_selfpowered
? "self" : "VBUS",
200 udc
->suspended
? ", suspended" : "",
201 udc
->driver
? udc
->driver
->driver
.name
: "(none)");
203 /* don't access registers when interface isn't clocked */
205 seq_printf(s
, "(not clocked)\n");
209 tmp
= at91_udp_read(udc
, AT91_UDP_FRM_NUM
);
210 seq_printf(s
, "frame %05x:%s%s frame=%d\n", tmp
,
211 (tmp
& AT91_UDP_FRM_OK
) ? " ok" : "",
212 (tmp
& AT91_UDP_FRM_ERR
) ? " err" : "",
213 (tmp
& AT91_UDP_NUM
));
215 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
216 seq_printf(s
, "glbstate %02x:%s" FOURBITS
"\n", tmp
,
217 (tmp
& AT91_UDP_RMWUPE
) ? " rmwupe" : "",
218 (tmp
& AT91_UDP_RSMINPR
) ? " rsminpr" : "",
219 (tmp
& AT91_UDP_ESR
) ? " esr" : "",
220 (tmp
& AT91_UDP_CONFG
) ? " confg" : "",
221 (tmp
& AT91_UDP_FADDEN
) ? " fadden" : "");
223 tmp
= at91_udp_read(udc
, AT91_UDP_FADDR
);
224 seq_printf(s
, "faddr %03x:%s fadd=%d\n", tmp
,
225 (tmp
& AT91_UDP_FEN
) ? " fen" : "",
226 (tmp
& AT91_UDP_FADD
));
228 proc_irq_show(s
, "imr ", at91_udp_read(udc
, AT91_UDP_IMR
));
229 proc_irq_show(s
, "isr ", at91_udp_read(udc
, AT91_UDP_ISR
));
231 if (udc
->enabled
&& udc
->vbus
) {
232 proc_ep_show(s
, &udc
->ep
[0]);
233 list_for_each_entry (ep
, &udc
->gadget
.ep_list
, ep
.ep_list
) {
241 static int proc_udc_open(struct inode
*inode
, struct file
*file
)
243 return single_open(file
, proc_udc_show
, PDE_DATA(inode
));
246 static const struct file_operations proc_ops
= {
247 .owner
= THIS_MODULE
,
248 .open
= proc_udc_open
,
251 .release
= single_release
,
254 static void create_debug_file(struct at91_udc
*udc
)
256 udc
->pde
= proc_create_data(debug_filename
, 0, NULL
, &proc_ops
, udc
);
259 static void remove_debug_file(struct at91_udc
*udc
)
262 remove_proc_entry(debug_filename
, NULL
);
267 static inline void create_debug_file(struct at91_udc
*udc
) {}
268 static inline void remove_debug_file(struct at91_udc
*udc
) {}
273 /*-------------------------------------------------------------------------*/
275 static void done(struct at91_ep
*ep
, struct at91_request
*req
, int status
)
277 unsigned stopped
= ep
->stopped
;
278 struct at91_udc
*udc
= ep
->udc
;
280 list_del_init(&req
->queue
);
281 if (req
->req
.status
== -EINPROGRESS
)
282 req
->req
.status
= status
;
284 status
= req
->req
.status
;
285 if (status
&& status
!= -ESHUTDOWN
)
286 VDBG("%s done %p, status %d\n", ep
->ep
.name
, req
, status
);
289 spin_unlock(&udc
->lock
);
290 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
291 spin_lock(&udc
->lock
);
292 ep
->stopped
= stopped
;
294 /* ep0 is always ready; other endpoints need a non-empty queue */
295 if (list_empty(&ep
->queue
) && ep
->int_mask
!= (1 << 0))
296 at91_udp_write(udc
, AT91_UDP_IDR
, ep
->int_mask
);
299 /*-------------------------------------------------------------------------*/
301 /* bits indicating OUT fifo has data ready */
302 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
305 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
306 * back most of the value you just read (because of side effects, including
307 * bits that may change after reading and before writing).
309 * Except when changing a specific bit, always write values which:
310 * - clear SET_FX bits (setting them could change something)
311 * - set CLR_FX bits (clearing them could change something)
313 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
314 * that shouldn't normally be changed.
316 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
317 * implying a need to wait for one write to complete (test relevant bits)
318 * before starting the next write. This shouldn't be an issue given how
319 * infrequently we write, except maybe for write-then-read idioms.
321 #define SET_FX (AT91_UDP_TXPKTRDY)
322 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
323 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
325 /* pull OUT packet data from the endpoint's fifo */
326 static int read_fifo (struct at91_ep
*ep
, struct at91_request
*req
)
328 u32 __iomem
*creg
= ep
->creg
;
329 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
332 unsigned int count
, bufferspace
, is_done
;
334 buf
= req
->req
.buf
+ req
->req
.actual
;
335 bufferspace
= req
->req
.length
- req
->req
.actual
;
338 * there might be nothing to read if ep_queue() calls us,
339 * or if we already emptied both pingpong buffers
342 csr
= __raw_readl(creg
);
343 if ((csr
& RX_DATA_READY
) == 0)
346 count
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
347 if (count
> ep
->ep
.maxpacket
)
348 count
= ep
->ep
.maxpacket
;
349 if (count
> bufferspace
) {
350 DBG("%s buffer overflow\n", ep
->ep
.name
);
351 req
->req
.status
= -EOVERFLOW
;
354 __raw_readsb(dreg
, buf
, count
);
356 /* release and swap pingpong mem bank */
358 if (ep
->is_pingpong
) {
359 if (ep
->fifo_bank
== 0) {
360 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
363 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK1
);
367 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
368 __raw_writel(csr
, creg
);
370 req
->req
.actual
+= count
;
371 is_done
= (count
< ep
->ep
.maxpacket
);
372 if (count
== bufferspace
)
375 PACKET("%s %p out/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
376 is_done
? " (done)" : "");
379 * avoid extra trips through IRQ logic for packets already in
380 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
384 else if (ep
->is_pingpong
) {
386 * One dummy read to delay the code because of a HW glitch:
387 * CSR returns bad RXCOUNT when read too soon after updating
390 csr
= __raw_readl(creg
);
392 bufferspace
-= count
;
400 /* load fifo for an IN packet */
401 static int write_fifo(struct at91_ep
*ep
, struct at91_request
*req
)
403 u32 __iomem
*creg
= ep
->creg
;
404 u32 csr
= __raw_readl(creg
);
405 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
406 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 buf
= req
->req
.buf
+ req
->req
.actual
;
434 total
= req
->req
.length
- req
->req
.actual
;
435 if (ep
->ep
.maxpacket
< total
) {
436 count
= ep
->ep
.maxpacket
;
440 is_last
= (count
< ep
->ep
.maxpacket
) || !req
->req
.zero
;
444 * Write the packet, maybe it's a ZLP.
446 * NOTE: incrementing req->actual before we receive the ACK means
447 * gadget driver IN bytecounts can be wrong in fault cases. That's
448 * fixable with PIO drivers like this one (save "count" here, and
449 * do the increment later on TX irq), but not for most DMA hardware.
451 * So all gadget drivers must accept that potential error. Some
452 * hardware supports precise fifo status reporting, letting them
453 * recover when the actual bytecount matters (e.g. for USB Test
454 * and Measurement Class devices).
456 __raw_writesb(dreg
, buf
, count
);
458 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
459 __raw_writel(csr
, creg
);
460 req
->req
.actual
+= count
;
462 PACKET("%s %p in/%d%s\n", ep
->ep
.name
, &req
->req
, count
,
463 is_last
? " (done)" : "");
469 static void nuke(struct at91_ep
*ep
, int status
)
471 struct at91_request
*req
;
473 /* terminate any request in the queue */
475 if (list_empty(&ep
->queue
))
478 VDBG("%s %s\n", __func__
, ep
->ep
.name
);
479 while (!list_empty(&ep
->queue
)) {
480 req
= list_entry(ep
->queue
.next
, struct at91_request
, queue
);
481 done(ep
, req
, status
);
485 /*-------------------------------------------------------------------------*/
487 static int at91_ep_enable(struct usb_ep
*_ep
,
488 const struct usb_endpoint_descriptor
*desc
)
490 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
491 struct at91_udc
*udc
;
497 || !desc
|| _ep
->name
== ep0name
498 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
499 || (maxpacket
= usb_endpoint_maxp(desc
)) == 0
500 || maxpacket
> ep
->maxpacket
) {
501 DBG("bad ep or descriptor\n");
506 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
507 DBG("bogus device state\n");
511 tmp
= usb_endpoint_type(desc
);
513 case USB_ENDPOINT_XFER_CONTROL
:
514 DBG("only one control endpoint\n");
516 case USB_ENDPOINT_XFER_INT
:
520 case USB_ENDPOINT_XFER_BULK
:
529 DBG("bogus maxpacket %d\n", maxpacket
);
531 case USB_ENDPOINT_XFER_ISOC
:
532 if (!ep
->is_pingpong
) {
533 DBG("iso requires double buffering\n");
540 spin_lock_irqsave(&udc
->lock
, flags
);
542 /* initialize endpoint to match this descriptor */
543 ep
->is_in
= usb_endpoint_dir_in(desc
);
544 ep
->is_iso
= (tmp
== USB_ENDPOINT_XFER_ISOC
);
549 tmp
|= AT91_UDP_EPEDS
;
550 __raw_writel(tmp
, ep
->creg
);
552 ep
->ep
.maxpacket
= maxpacket
;
555 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
556 * since endpoint resets don't reset hw pingpong state.
558 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
559 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
561 spin_unlock_irqrestore(&udc
->lock
, flags
);
565 static int at91_ep_disable (struct usb_ep
* _ep
)
567 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
568 struct at91_udc
*udc
= ep
->udc
;
571 if (ep
== &ep
->udc
->ep
[0])
574 spin_lock_irqsave(&udc
->lock
, flags
);
576 nuke(ep
, -ESHUTDOWN
);
578 /* restore the endpoint's pristine config */
580 ep
->ep
.maxpacket
= ep
->maxpacket
;
582 /* reset fifos and endpoint */
583 if (ep
->udc
->clocked
) {
584 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
585 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
586 __raw_writel(0, ep
->creg
);
589 spin_unlock_irqrestore(&udc
->lock
, flags
);
594 * this is a PIO-only driver, so there's nothing
595 * interesting for request or buffer allocation.
598 static struct usb_request
*
599 at91_ep_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
601 struct at91_request
*req
;
603 req
= kzalloc(sizeof (struct at91_request
), gfp_flags
);
607 INIT_LIST_HEAD(&req
->queue
);
611 static void at91_ep_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
613 struct at91_request
*req
;
615 req
= container_of(_req
, struct at91_request
, req
);
616 BUG_ON(!list_empty(&req
->queue
));
620 static int at91_ep_queue(struct usb_ep
*_ep
,
621 struct usb_request
*_req
, gfp_t gfp_flags
)
623 struct at91_request
*req
;
625 struct at91_udc
*udc
;
629 req
= container_of(_req
, struct at91_request
, req
);
630 ep
= container_of(_ep
, struct at91_ep
, ep
);
632 if (!_req
|| !_req
->complete
633 || !_req
->buf
|| !list_empty(&req
->queue
)) {
634 DBG("invalid request\n");
638 if (!_ep
|| (!ep
->ep
.desc
&& ep
->ep
.name
!= ep0name
)) {
645 if (!udc
|| !udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
646 DBG("invalid device\n");
650 _req
->status
= -EINPROGRESS
;
653 spin_lock_irqsave(&udc
->lock
, flags
);
655 /* try to kickstart any empty and idle queue */
656 if (list_empty(&ep
->queue
) && !ep
->stopped
) {
660 * If this control request has a non-empty DATA stage, this
661 * will start that stage. It works just like a non-control
662 * request (until the status stage starts, maybe early).
664 * If the data stage is empty, then this starts a successful
665 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
667 is_ep0
= (ep
->ep
.name
== ep0name
);
671 if (!udc
->req_pending
) {
677 * defer changing CONFG until after the gadget driver
678 * reconfigures the endpoints.
680 if (udc
->wait_for_config_ack
) {
681 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
682 tmp
^= AT91_UDP_CONFG
;
683 VDBG("toggle config\n");
684 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
686 if (req
->req
.length
== 0) {
688 PACKET("ep0 in/status\n");
690 tmp
= __raw_readl(ep
->creg
);
692 tmp
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
693 __raw_writel(tmp
, ep
->creg
);
694 udc
->req_pending
= 0;
700 status
= write_fifo(ep
, req
);
702 status
= read_fifo(ep
, req
);
704 /* IN/STATUS stage is otherwise triggered by irq */
705 if (status
&& is_ep0
)
711 if (req
&& !status
) {
712 list_add_tail (&req
->queue
, &ep
->queue
);
713 at91_udp_write(udc
, AT91_UDP_IER
, ep
->int_mask
);
716 spin_unlock_irqrestore(&udc
->lock
, flags
);
717 return (status
< 0) ? status
: 0;
720 static int at91_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
723 struct at91_request
*req
;
725 struct at91_udc
*udc
;
727 ep
= container_of(_ep
, struct at91_ep
, ep
);
728 if (!_ep
|| ep
->ep
.name
== ep0name
)
733 spin_lock_irqsave(&udc
->lock
, flags
);
735 /* make sure it's actually queued on this endpoint */
736 list_for_each_entry (req
, &ep
->queue
, queue
) {
737 if (&req
->req
== _req
)
740 if (&req
->req
!= _req
) {
741 spin_unlock_irqrestore(&udc
->lock
, flags
);
745 done(ep
, req
, -ECONNRESET
);
746 spin_unlock_irqrestore(&udc
->lock
, flags
);
750 static int at91_ep_set_halt(struct usb_ep
*_ep
, int value
)
752 struct at91_ep
*ep
= container_of(_ep
, struct at91_ep
, ep
);
753 struct at91_udc
*udc
= ep
->udc
;
759 if (!_ep
|| ep
->is_iso
|| !ep
->udc
->clocked
)
763 spin_lock_irqsave(&udc
->lock
, flags
);
765 csr
= __raw_readl(creg
);
768 * fail with still-busy IN endpoints, ensuring correct sequencing
769 * of data tx then stall. note that the fifo rx bytecount isn't
770 * completely accurate as a tx bytecount.
772 if (ep
->is_in
&& (!list_empty(&ep
->queue
) || (csr
>> 16) != 0))
778 csr
|= AT91_UDP_FORCESTALL
;
779 VDBG("halt %s\n", ep
->ep
.name
);
781 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
782 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
783 csr
&= ~AT91_UDP_FORCESTALL
;
785 __raw_writel(csr
, creg
);
788 spin_unlock_irqrestore(&udc
->lock
, flags
);
792 static const struct usb_ep_ops at91_ep_ops
= {
793 .enable
= at91_ep_enable
,
794 .disable
= at91_ep_disable
,
795 .alloc_request
= at91_ep_alloc_request
,
796 .free_request
= at91_ep_free_request
,
797 .queue
= at91_ep_queue
,
798 .dequeue
= at91_ep_dequeue
,
799 .set_halt
= at91_ep_set_halt
,
800 /* there's only imprecise fifo status reporting */
803 /*-------------------------------------------------------------------------*/
805 static int at91_get_frame(struct usb_gadget
*gadget
)
807 struct at91_udc
*udc
= to_udc(gadget
);
809 if (!to_udc(gadget
)->clocked
)
811 return at91_udp_read(udc
, AT91_UDP_FRM_NUM
) & AT91_UDP_NUM
;
814 static int at91_wakeup(struct usb_gadget
*gadget
)
816 struct at91_udc
*udc
= to_udc(gadget
);
818 int status
= -EINVAL
;
821 DBG("%s\n", __func__
);
822 spin_lock_irqsave(&udc
->lock
, flags
);
824 if (!udc
->clocked
|| !udc
->suspended
)
827 /* NOTE: some "early versions" handle ESR differently ... */
829 glbstate
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
830 if (!(glbstate
& AT91_UDP_ESR
))
832 glbstate
|= AT91_UDP_ESR
;
833 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, glbstate
);
836 spin_unlock_irqrestore(&udc
->lock
, flags
);
840 /* reinit == restore initial software state */
841 static void udc_reinit(struct at91_udc
*udc
)
845 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
846 INIT_LIST_HEAD(&udc
->gadget
.ep0
->ep_list
);
847 udc
->gadget
.quirk_stall_not_supp
= 1;
849 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
850 struct at91_ep
*ep
= &udc
->ep
[i
];
853 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
857 usb_ep_set_maxpacket_limit(&ep
->ep
, ep
->maxpacket
);
858 ep
->creg
= (void __iomem
*) udc
->udp_baseaddr
+ AT91_UDP_CSR(i
);
859 /* initialize one queue per endpoint */
860 INIT_LIST_HEAD(&ep
->queue
);
864 static void reset_gadget(struct at91_udc
*udc
)
866 struct usb_gadget_driver
*driver
= udc
->driver
;
869 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
871 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
874 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
875 struct at91_ep
*ep
= &udc
->ep
[i
];
878 nuke(ep
, -ESHUTDOWN
);
881 spin_unlock(&udc
->lock
);
882 usb_gadget_udc_reset(&udc
->gadget
, driver
);
883 spin_lock(&udc
->lock
);
889 static void stop_activity(struct at91_udc
*udc
)
891 struct usb_gadget_driver
*driver
= udc
->driver
;
894 if (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
896 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
899 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
900 struct at91_ep
*ep
= &udc
->ep
[i
];
902 nuke(ep
, -ESHUTDOWN
);
905 spin_unlock(&udc
->lock
);
906 driver
->disconnect(&udc
->gadget
);
907 spin_lock(&udc
->lock
);
913 static void clk_on(struct at91_udc
*udc
)
919 clk_enable(udc
->iclk
);
920 clk_enable(udc
->fclk
);
923 static void clk_off(struct at91_udc
*udc
)
928 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
929 clk_disable(udc
->fclk
);
930 clk_disable(udc
->iclk
);
934 * activate/deactivate link with host; minimize power usage for
935 * inactive links by cutting clocks and transceiver power.
937 static void pullup(struct at91_udc
*udc
, int is_on
)
939 if (!udc
->enabled
|| !udc
->vbus
)
941 DBG("%sactive\n", is_on
? "" : "in");
945 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXRSM
);
946 at91_udp_write(udc
, AT91_UDP_TXVC
, 0);
949 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXRSM
);
950 at91_udp_write(udc
, AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
954 if (udc
->caps
&& udc
->caps
->pullup
)
955 udc
->caps
->pullup(udc
, is_on
);
958 /* vbus is here! turn everything on that's ready */
959 static int at91_vbus_session(struct usb_gadget
*gadget
, int is_active
)
961 struct at91_udc
*udc
= to_udc(gadget
);
964 /* VDBG("vbus %s\n", is_active ? "on" : "off"); */
965 spin_lock_irqsave(&udc
->lock
, flags
);
966 udc
->vbus
= (is_active
!= 0);
968 pullup(udc
, is_active
);
971 spin_unlock_irqrestore(&udc
->lock
, flags
);
975 static int at91_pullup(struct usb_gadget
*gadget
, int is_on
)
977 struct at91_udc
*udc
= to_udc(gadget
);
980 spin_lock_irqsave(&udc
->lock
, flags
);
981 udc
->enabled
= is_on
= !!is_on
;
983 spin_unlock_irqrestore(&udc
->lock
, flags
);
987 static int at91_set_selfpowered(struct usb_gadget
*gadget
, int is_on
)
989 struct at91_udc
*udc
= to_udc(gadget
);
992 spin_lock_irqsave(&udc
->lock
, flags
);
993 gadget
->is_selfpowered
= (is_on
!= 0);
994 spin_unlock_irqrestore(&udc
->lock
, flags
);
998 static int at91_start(struct usb_gadget
*gadget
,
999 struct usb_gadget_driver
*driver
);
1000 static int at91_stop(struct usb_gadget
*gadget
);
1002 static const struct usb_gadget_ops at91_udc_ops
= {
1003 .get_frame
= at91_get_frame
,
1004 .wakeup
= at91_wakeup
,
1005 .set_selfpowered
= at91_set_selfpowered
,
1006 .vbus_session
= at91_vbus_session
,
1007 .pullup
= at91_pullup
,
1008 .udc_start
= at91_start
,
1009 .udc_stop
= at91_stop
,
1012 * VBUS-powered devices may also also want to support bigger
1013 * power budgets after an appropriate SET_CONFIGURATION.
1015 /* .vbus_power = at91_vbus_power, */
1018 /*-------------------------------------------------------------------------*/
1020 static int handle_ep(struct at91_ep
*ep
)
1022 struct at91_request
*req
;
1023 u32 __iomem
*creg
= ep
->creg
;
1024 u32 csr
= __raw_readl(creg
);
1026 if (!list_empty(&ep
->queue
))
1027 req
= list_entry(ep
->queue
.next
,
1028 struct at91_request
, queue
);
1033 if (csr
& (AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
)) {
1035 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_TXCOMP
);
1036 __raw_writel(csr
, creg
);
1039 return write_fifo(ep
, req
);
1042 if (csr
& AT91_UDP_STALLSENT
) {
1043 /* STALLSENT bit == ISOERR */
1044 if (ep
->is_iso
&& req
)
1045 req
->req
.status
= -EILSEQ
;
1047 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
);
1048 __raw_writel(csr
, creg
);
1049 csr
= __raw_readl(creg
);
1051 if (req
&& (csr
& RX_DATA_READY
))
1052 return read_fifo(ep
, req
);
1059 struct usb_ctrlrequest r
;
1062 static void handle_setup(struct at91_udc
*udc
, struct at91_ep
*ep
, u32 csr
)
1064 u32 __iomem
*creg
= ep
->creg
;
1065 u8 __iomem
*dreg
= ep
->creg
+ (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1066 unsigned rxcount
, i
= 0;
1071 /* read and ack SETUP; hard-fail for bogus packets */
1072 rxcount
= (csr
& AT91_UDP_RXBYTECNT
) >> 16;
1073 if (likely(rxcount
== 8)) {
1075 pkt
.raw
[i
++] = __raw_readb(dreg
);
1076 if (pkt
.r
.bRequestType
& USB_DIR_IN
) {
1077 csr
|= AT91_UDP_DIR
;
1080 csr
&= ~AT91_UDP_DIR
;
1084 /* REVISIT this happens sometimes under load; why?? */
1085 ERR("SETUP len %d, csr %08x\n", rxcount
, csr
);
1089 csr
&= ~(SET_FX
| AT91_UDP_RXSETUP
);
1090 __raw_writel(csr
, creg
);
1091 udc
->wait_for_addr_ack
= 0;
1092 udc
->wait_for_config_ack
= 0;
1094 if (unlikely(status
!= 0))
1097 #define w_index le16_to_cpu(pkt.r.wIndex)
1098 #define w_value le16_to_cpu(pkt.r.wValue)
1099 #define w_length le16_to_cpu(pkt.r.wLength)
1101 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1102 pkt
.r
.bRequestType
, pkt
.r
.bRequest
,
1103 w_value
, w_index
, w_length
);
1106 * A few standard requests get handled here, ones that touch
1107 * hardware ... notably for device and endpoint features.
1109 udc
->req_pending
= 1;
1110 csr
= __raw_readl(creg
);
1113 switch ((pkt
.r
.bRequestType
<< 8) | pkt
.r
.bRequest
) {
1115 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1116 | USB_REQ_SET_ADDRESS
:
1117 __raw_writel(csr
| AT91_UDP_TXPKTRDY
, creg
);
1118 udc
->addr
= w_value
;
1119 udc
->wait_for_addr_ack
= 1;
1120 udc
->req_pending
= 0;
1121 /* FADDR is set later, when we ack host STATUS */
1124 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1125 | USB_REQ_SET_CONFIGURATION
:
1126 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
) & AT91_UDP_CONFG
;
1128 udc
->wait_for_config_ack
= (tmp
== 0);
1130 udc
->wait_for_config_ack
= (tmp
!= 0);
1131 if (udc
->wait_for_config_ack
)
1132 VDBG("wait for config\n");
1133 /* CONFG is toggled later, if gadget driver succeeds */
1137 * Hosts may set or clear remote wakeup status, and
1138 * devices may report they're VBUS powered.
1140 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1141 | USB_REQ_GET_STATUS
:
1142 tmp
= (udc
->gadget
.is_selfpowered
<< USB_DEVICE_SELF_POWERED
);
1143 if (at91_udp_read(udc
, AT91_UDP_GLB_STAT
) & AT91_UDP_ESR
)
1144 tmp
|= (1 << USB_DEVICE_REMOTE_WAKEUP
);
1145 PACKET("get device status\n");
1146 __raw_writeb(tmp
, dreg
);
1147 __raw_writeb(0, dreg
);
1149 /* then STATUS starts later, automatically */
1150 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1151 | USB_REQ_SET_FEATURE
:
1152 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1154 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1155 tmp
|= AT91_UDP_ESR
;
1156 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1158 case ((USB_TYPE_STANDARD
|USB_RECIP_DEVICE
) << 8)
1159 | USB_REQ_CLEAR_FEATURE
:
1160 if (w_value
!= USB_DEVICE_REMOTE_WAKEUP
)
1162 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1163 tmp
&= ~AT91_UDP_ESR
;
1164 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1168 * Interfaces have no feature settings; this is pretty useless.
1169 * we won't even insist the interface exists...
1171 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1172 | USB_REQ_GET_STATUS
:
1173 PACKET("get interface status\n");
1174 __raw_writeb(0, dreg
);
1175 __raw_writeb(0, dreg
);
1177 /* then STATUS starts later, automatically */
1178 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1179 | USB_REQ_SET_FEATURE
:
1180 case ((USB_TYPE_STANDARD
|USB_RECIP_INTERFACE
) << 8)
1181 | USB_REQ_CLEAR_FEATURE
:
1185 * Hosts may clear bulk/intr endpoint halt after the gadget
1186 * driver sets it (not widely used); or set it (for testing)
1188 case ((USB_DIR_IN
|USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1189 | USB_REQ_GET_STATUS
:
1190 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1192 if (tmp
>= NUM_ENDPOINTS
|| (tmp
&& !ep
->ep
.desc
))
1196 if ((w_index
& USB_DIR_IN
)) {
1199 } else if (ep
->is_in
)
1202 PACKET("get %s status\n", ep
->ep
.name
);
1203 if (__raw_readl(ep
->creg
) & AT91_UDP_FORCESTALL
)
1204 tmp
= (1 << USB_ENDPOINT_HALT
);
1207 __raw_writeb(tmp
, dreg
);
1208 __raw_writeb(0, dreg
);
1210 /* then STATUS starts later, automatically */
1211 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1212 | USB_REQ_SET_FEATURE
:
1213 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1215 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
>= NUM_ENDPOINTS
)
1217 if (!ep
->ep
.desc
|| ep
->is_iso
)
1219 if ((w_index
& USB_DIR_IN
)) {
1222 } else if (ep
->is_in
)
1225 tmp
= __raw_readl(ep
->creg
);
1227 tmp
|= CLR_FX
| AT91_UDP_FORCESTALL
;
1228 __raw_writel(tmp
, ep
->creg
);
1230 case ((USB_TYPE_STANDARD
|USB_RECIP_ENDPOINT
) << 8)
1231 | USB_REQ_CLEAR_FEATURE
:
1232 tmp
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
1234 if (w_value
!= USB_ENDPOINT_HALT
|| tmp
>= NUM_ENDPOINTS
)
1238 if (!ep
->ep
.desc
|| ep
->is_iso
)
1240 if ((w_index
& USB_DIR_IN
)) {
1243 } else if (ep
->is_in
)
1246 at91_udp_write(udc
, AT91_UDP_RST_EP
, ep
->int_mask
);
1247 at91_udp_write(udc
, AT91_UDP_RST_EP
, 0);
1248 tmp
= __raw_readl(ep
->creg
);
1250 tmp
&= ~(SET_FX
| AT91_UDP_FORCESTALL
);
1251 __raw_writel(tmp
, ep
->creg
);
1252 if (!list_empty(&ep
->queue
))
1261 /* pass request up to the gadget driver */
1263 spin_unlock(&udc
->lock
);
1264 status
= udc
->driver
->setup(&udc
->gadget
, &pkt
.r
);
1265 spin_lock(&udc
->lock
);
1271 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1272 pkt
.r
.bRequestType
, pkt
.r
.bRequest
, status
);
1273 csr
|= AT91_UDP_FORCESTALL
;
1274 __raw_writel(csr
, creg
);
1275 udc
->req_pending
= 0;
1280 /* immediate successful (IN) STATUS after zero length DATA */
1281 PACKET("ep0 in/status\n");
1283 csr
|= AT91_UDP_TXPKTRDY
;
1284 __raw_writel(csr
, creg
);
1285 udc
->req_pending
= 0;
1288 static void handle_ep0(struct at91_udc
*udc
)
1290 struct at91_ep
*ep0
= &udc
->ep
[0];
1291 u32 __iomem
*creg
= ep0
->creg
;
1292 u32 csr
= __raw_readl(creg
);
1293 struct at91_request
*req
;
1295 if (unlikely(csr
& AT91_UDP_STALLSENT
)) {
1297 udc
->req_pending
= 0;
1299 csr
&= ~(SET_FX
| AT91_UDP_STALLSENT
| AT91_UDP_FORCESTALL
);
1300 __raw_writel(csr
, creg
);
1301 VDBG("ep0 stalled\n");
1302 csr
= __raw_readl(creg
);
1304 if (csr
& AT91_UDP_RXSETUP
) {
1306 udc
->req_pending
= 0;
1307 handle_setup(udc
, ep0
, csr
);
1311 if (list_empty(&ep0
->queue
))
1314 req
= list_entry(ep0
->queue
.next
, struct at91_request
, queue
);
1316 /* host ACKed an IN packet that we sent */
1317 if (csr
& AT91_UDP_TXCOMP
) {
1319 csr
&= ~(SET_FX
| AT91_UDP_TXCOMP
);
1321 /* write more IN DATA? */
1322 if (req
&& ep0
->is_in
) {
1324 udc
->req_pending
= 0;
1328 * - last IN DATA packet (including GET_STATUS)
1329 * - IN/STATUS for OUT DATA
1330 * - IN/STATUS for any zero-length DATA stage
1331 * except for the IN DATA case, the host should send
1332 * an OUT status later, which we'll ack.
1335 udc
->req_pending
= 0;
1336 __raw_writel(csr
, creg
);
1339 * SET_ADDRESS takes effect only after the STATUS
1340 * (to the original address) gets acked.
1342 if (udc
->wait_for_addr_ack
) {
1345 at91_udp_write(udc
, AT91_UDP_FADDR
,
1346 AT91_UDP_FEN
| udc
->addr
);
1347 tmp
= at91_udp_read(udc
, AT91_UDP_GLB_STAT
);
1348 tmp
&= ~AT91_UDP_FADDEN
;
1350 tmp
|= AT91_UDP_FADDEN
;
1351 at91_udp_write(udc
, AT91_UDP_GLB_STAT
, tmp
);
1353 udc
->wait_for_addr_ack
= 0;
1354 VDBG("address %d\n", udc
->addr
);
1359 /* OUT packet arrived ... */
1360 else if (csr
& AT91_UDP_RX_DATA_BK0
) {
1362 csr
&= ~(SET_FX
| AT91_UDP_RX_DATA_BK0
);
1364 /* OUT DATA stage */
1367 if (handle_ep(ep0
)) {
1368 /* send IN/STATUS */
1369 PACKET("ep0 in/status\n");
1370 csr
= __raw_readl(creg
);
1372 csr
|= CLR_FX
| AT91_UDP_TXPKTRDY
;
1373 __raw_writel(csr
, creg
);
1374 udc
->req_pending
= 0;
1376 } else if (udc
->req_pending
) {
1378 * AT91 hardware has a hard time with this
1379 * "deferred response" mode for control-OUT
1380 * transfers. (For control-IN it's fine.)
1382 * The normal solution leaves OUT data in the
1383 * fifo until the gadget driver is ready.
1384 * We couldn't do that here without disabling
1385 * the IRQ that tells about SETUP packets,
1386 * e.g. when the host gets impatient...
1388 * Working around it by copying into a buffer
1389 * would almost be a non-deferred response,
1390 * except that it wouldn't permit reliable
1391 * stalling of the request. Instead, demand
1392 * that gadget drivers not use this mode.
1394 DBG("no control-OUT deferred responses!\n");
1395 __raw_writel(csr
| AT91_UDP_FORCESTALL
, creg
);
1396 udc
->req_pending
= 0;
1399 /* STATUS stage for control-IN; ack. */
1401 PACKET("ep0 out/status ACK\n");
1402 __raw_writel(csr
, creg
);
1404 /* "early" status stage */
1411 static irqreturn_t
at91_udc_irq (int irq
, void *_udc
)
1413 struct at91_udc
*udc
= _udc
;
1415 int disable_clock
= 0;
1416 unsigned long flags
;
1418 spin_lock_irqsave(&udc
->lock
, flags
);
1420 if (!udc
->clocked
) {
1428 status
= at91_udp_read(udc
, AT91_UDP_ISR
)
1429 & at91_udp_read(udc
, AT91_UDP_IMR
);
1433 /* USB reset irq: not maskable */
1434 if (status
& AT91_UDP_ENDBUSRES
) {
1435 at91_udp_write(udc
, AT91_UDP_IDR
, ~MINIMUS_INTERRUPTUS
);
1436 at91_udp_write(udc
, AT91_UDP_IER
, MINIMUS_INTERRUPTUS
);
1437 /* Atmel code clears this irq twice */
1438 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1439 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_ENDBUSRES
);
1440 VDBG("end bus reset\n");
1445 at91_udp_write(udc
, AT91_UDP_CSR(0),
1446 AT91_UDP_EPEDS
| AT91_UDP_EPTYPE_CTRL
);
1447 udc
->gadget
.speed
= USB_SPEED_FULL
;
1449 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_EP(0));
1452 * NOTE: this driver keeps clocks off unless the
1453 * USB host is present. That saves power, but for
1454 * boards that don't support VBUS detection, both
1455 * clocks need to be active most of the time.
1458 /* host initiated suspend (3+ms bus idle) */
1459 } else if (status
& AT91_UDP_RXSUSP
) {
1460 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXSUSP
);
1461 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_RXRSM
);
1462 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXSUSP
);
1463 /* VDBG("bus suspend\n"); */
1469 * NOTE: when suspending a VBUS-powered device, the
1470 * gadget driver should switch into slow clock mode
1471 * and then into standby to avoid drawing more than
1472 * 500uA power (2500uA for some high-power configs).
1474 if (udc
->driver
&& udc
->driver
->suspend
) {
1475 spin_unlock(&udc
->lock
);
1476 udc
->driver
->suspend(&udc
->gadget
);
1477 spin_lock(&udc
->lock
);
1480 /* host initiated resume */
1481 } else if (status
& AT91_UDP_RXRSM
) {
1482 at91_udp_write(udc
, AT91_UDP_IDR
, AT91_UDP_RXRSM
);
1483 at91_udp_write(udc
, AT91_UDP_IER
, AT91_UDP_RXSUSP
);
1484 at91_udp_write(udc
, AT91_UDP_ICR
, AT91_UDP_RXRSM
);
1485 /* VDBG("bus resume\n"); */
1486 if (!udc
->suspended
)
1491 * NOTE: for a VBUS-powered device, the gadget driver
1492 * would normally want to switch out of slow clock
1493 * mode into normal mode.
1495 if (udc
->driver
&& udc
->driver
->resume
) {
1496 spin_unlock(&udc
->lock
);
1497 udc
->driver
->resume(&udc
->gadget
);
1498 spin_lock(&udc
->lock
);
1501 /* endpoint IRQs are cleared by handling them */
1505 struct at91_ep
*ep
= &udc
->ep
[1];
1509 for (i
= 1; i
< NUM_ENDPOINTS
; i
++) {
1521 spin_unlock_irqrestore(&udc
->lock
, flags
);
1526 /*-------------------------------------------------------------------------*/
1528 static void at91_vbus_update(struct at91_udc
*udc
, unsigned value
)
1530 value
^= udc
->board
.vbus_active_low
;
1531 if (value
!= udc
->vbus
)
1532 at91_vbus_session(&udc
->gadget
, value
);
1535 static irqreturn_t
at91_vbus_irq(int irq
, void *_udc
)
1537 struct at91_udc
*udc
= _udc
;
1539 /* vbus needs at least brief debouncing */
1541 at91_vbus_update(udc
, gpio_get_value(udc
->board
.vbus_pin
));
1546 static void at91_vbus_timer_work(struct work_struct
*work
)
1548 struct at91_udc
*udc
= container_of(work
, struct at91_udc
,
1551 at91_vbus_update(udc
, gpio_get_value_cansleep(udc
->board
.vbus_pin
));
1553 if (!timer_pending(&udc
->vbus_timer
))
1554 mod_timer(&udc
->vbus_timer
, jiffies
+ VBUS_POLL_TIMEOUT
);
1557 static void at91_vbus_timer(unsigned long data
)
1559 struct at91_udc
*udc
= (struct at91_udc
*)data
;
1562 * If we are polling vbus it is likely that the gpio is on an
1563 * bus such as i2c or spi which may sleep, so schedule some work
1564 * to read the vbus gpio
1566 schedule_work(&udc
->vbus_timer_work
);
1569 static int at91_start(struct usb_gadget
*gadget
,
1570 struct usb_gadget_driver
*driver
)
1572 struct at91_udc
*udc
;
1574 udc
= container_of(gadget
, struct at91_udc
, gadget
);
1575 udc
->driver
= driver
;
1576 udc
->gadget
.dev
.of_node
= udc
->pdev
->dev
.of_node
;
1578 udc
->gadget
.is_selfpowered
= 1;
1583 static int at91_stop(struct usb_gadget
*gadget
)
1585 struct at91_udc
*udc
;
1586 unsigned long flags
;
1588 udc
= container_of(gadget
, struct at91_udc
, gadget
);
1589 spin_lock_irqsave(&udc
->lock
, flags
);
1591 at91_udp_write(udc
, AT91_UDP_IDR
, ~0);
1592 spin_unlock_irqrestore(&udc
->lock
, flags
);
1599 /*-------------------------------------------------------------------------*/
1601 static void at91udc_shutdown(struct platform_device
*dev
)
1603 struct at91_udc
*udc
= platform_get_drvdata(dev
);
1604 unsigned long flags
;
1606 /* force disconnect on reboot */
1607 spin_lock_irqsave(&udc
->lock
, flags
);
1608 pullup(platform_get_drvdata(dev
), 0);
1609 spin_unlock_irqrestore(&udc
->lock
, flags
);
1612 static int at91rm9200_udc_init(struct at91_udc
*udc
)
1618 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1630 ep
->maxpacket
= 256;
1635 if (!gpio_is_valid(udc
->board
.pullup_pin
)) {
1636 DBG("no D+ pullup?\n");
1640 ret
= devm_gpio_request(&udc
->pdev
->dev
, udc
->board
.pullup_pin
,
1643 DBG("D+ pullup is busy\n");
1647 gpio_direction_output(udc
->board
.pullup_pin
,
1648 udc
->board
.pullup_active_low
);
1653 static void at91rm9200_udc_pullup(struct at91_udc
*udc
, int is_on
)
1655 int active
= !udc
->board
.pullup_active_low
;
1658 gpio_set_value(udc
->board
.pullup_pin
, active
);
1660 gpio_set_value(udc
->board
.pullup_pin
, !active
);
1663 static const struct at91_udc_caps at91rm9200_udc_caps
= {
1664 .init
= at91rm9200_udc_init
,
1665 .pullup
= at91rm9200_udc_pullup
,
1668 static int at91sam9260_udc_init(struct at91_udc
*udc
)
1673 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1681 ep
->maxpacket
= 512;
1689 static void at91sam9260_udc_pullup(struct at91_udc
*udc
, int is_on
)
1691 u32 txvc
= at91_udp_read(udc
, AT91_UDP_TXVC
);
1694 txvc
|= AT91_UDP_TXVC_PUON
;
1696 txvc
&= ~AT91_UDP_TXVC_PUON
;
1698 at91_udp_write(udc
, AT91_UDP_TXVC
, txvc
);
1701 static const struct at91_udc_caps at91sam9260_udc_caps
= {
1702 .init
= at91sam9260_udc_init
,
1703 .pullup
= at91sam9260_udc_pullup
,
1706 static int at91sam9261_udc_init(struct at91_udc
*udc
)
1711 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1722 ep
->maxpacket
= 256;
1727 udc
->matrix
= syscon_regmap_lookup_by_phandle(udc
->pdev
->dev
.of_node
,
1729 return PTR_ERR_OR_ZERO(udc
->matrix
);
1732 static void at91sam9261_udc_pullup(struct at91_udc
*udc
, int is_on
)
1737 usbpucr
= AT91_MATRIX_USBPUCR_PUON
;
1739 regmap_update_bits(udc
->matrix
, AT91SAM9261_MATRIX_USBPUCR
,
1740 AT91_MATRIX_USBPUCR_PUON
, usbpucr
);
1743 static const struct at91_udc_caps at91sam9261_udc_caps
= {
1744 .init
= at91sam9261_udc_init
,
1745 .pullup
= at91sam9261_udc_pullup
,
1748 static int at91sam9263_udc_init(struct at91_udc
*udc
)
1753 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1765 ep
->maxpacket
= 256;
1773 static const struct at91_udc_caps at91sam9263_udc_caps
= {
1774 .init
= at91sam9263_udc_init
,
1775 .pullup
= at91sam9260_udc_pullup
,
1778 static const struct of_device_id at91_udc_dt_ids
[] = {
1780 .compatible
= "atmel,at91rm9200-udc",
1781 .data
= &at91rm9200_udc_caps
,
1784 .compatible
= "atmel,at91sam9260-udc",
1785 .data
= &at91sam9260_udc_caps
,
1788 .compatible
= "atmel,at91sam9261-udc",
1789 .data
= &at91sam9261_udc_caps
,
1792 .compatible
= "atmel,at91sam9263-udc",
1793 .data
= &at91sam9263_udc_caps
,
1797 MODULE_DEVICE_TABLE(of
, at91_udc_dt_ids
);
1799 static void at91udc_of_init(struct at91_udc
*udc
, struct device_node
*np
)
1801 struct at91_udc_data
*board
= &udc
->board
;
1802 const struct of_device_id
*match
;
1803 enum of_gpio_flags flags
;
1806 if (of_property_read_u32(np
, "atmel,vbus-polled", &val
) == 0)
1807 board
->vbus_polled
= 1;
1809 board
->vbus_pin
= of_get_named_gpio_flags(np
, "atmel,vbus-gpio", 0,
1811 board
->vbus_active_low
= (flags
& OF_GPIO_ACTIVE_LOW
) ? 1 : 0;
1813 board
->pullup_pin
= of_get_named_gpio_flags(np
, "atmel,pullup-gpio", 0,
1816 board
->pullup_active_low
= (flags
& OF_GPIO_ACTIVE_LOW
) ? 1 : 0;
1818 match
= of_match_node(at91_udc_dt_ids
, np
);
1820 udc
->caps
= match
->data
;
1823 static int at91udc_probe(struct platform_device
*pdev
)
1825 struct device
*dev
= &pdev
->dev
;
1826 struct at91_udc
*udc
;
1828 struct resource
*res
;
1832 udc
= devm_kzalloc(dev
, sizeof(*udc
), GFP_KERNEL
);
1836 /* init software state */
1837 udc
->gadget
.dev
.parent
= dev
;
1838 at91udc_of_init(udc
, pdev
->dev
.of_node
);
1841 spin_lock_init(&udc
->lock
);
1843 udc
->gadget
.ops
= &at91_udc_ops
;
1844 udc
->gadget
.ep0
= &udc
->ep
[0].ep
;
1845 udc
->gadget
.name
= driver_name
;
1846 udc
->gadget
.dev
.init_name
= "gadget";
1848 for (i
= 0; i
< NUM_ENDPOINTS
; i
++) {
1850 ep
->ep
.name
= ep_info
[i
].name
;
1851 ep
->ep
.caps
= ep_info
[i
].caps
;
1852 ep
->ep
.ops
= &at91_ep_ops
;
1854 ep
->int_mask
= BIT(i
);
1855 if (i
!= 0 && i
!= 3)
1856 ep
->is_pingpong
= 1;
1859 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1860 udc
->udp_baseaddr
= devm_ioremap_resource(dev
, res
);
1861 if (IS_ERR(udc
->udp_baseaddr
))
1862 return PTR_ERR(udc
->udp_baseaddr
);
1864 if (udc
->caps
&& udc
->caps
->init
) {
1865 retval
= udc
->caps
->init(udc
);
1872 /* get interface and function clocks */
1873 udc
->iclk
= devm_clk_get(dev
, "pclk");
1874 if (IS_ERR(udc
->iclk
))
1875 return PTR_ERR(udc
->iclk
);
1877 udc
->fclk
= devm_clk_get(dev
, "hclk");
1878 if (IS_ERR(udc
->fclk
))
1879 return PTR_ERR(udc
->fclk
);
1881 /* don't do anything until we have both gadget driver and VBUS */
1882 clk_set_rate(udc
->fclk
, 48000000);
1883 retval
= clk_prepare(udc
->fclk
);
1887 retval
= clk_prepare_enable(udc
->iclk
);
1889 goto err_unprepare_fclk
;
1891 at91_udp_write(udc
, AT91_UDP_TXVC
, AT91_UDP_TXVC_TXVDIS
);
1892 at91_udp_write(udc
, AT91_UDP_IDR
, 0xffffffff);
1893 /* Clear all pending interrupts - UDP may be used by bootloader. */
1894 at91_udp_write(udc
, AT91_UDP_ICR
, 0xffffffff);
1895 clk_disable(udc
->iclk
);
1897 /* request UDC and maybe VBUS irqs */
1898 udc
->udp_irq
= platform_get_irq(pdev
, 0);
1899 retval
= devm_request_irq(dev
, udc
->udp_irq
, at91_udc_irq
, 0,
1902 DBG("request irq %d failed\n", udc
->udp_irq
);
1903 goto err_unprepare_iclk
;
1906 if (gpio_is_valid(udc
->board
.vbus_pin
)) {
1907 retval
= devm_gpio_request(dev
, udc
->board
.vbus_pin
,
1910 DBG("request vbus pin failed\n");
1911 goto err_unprepare_iclk
;
1914 gpio_direction_input(udc
->board
.vbus_pin
);
1917 * Get the initial state of VBUS - we cannot expect
1918 * a pending interrupt.
1920 udc
->vbus
= gpio_get_value_cansleep(udc
->board
.vbus_pin
) ^
1921 udc
->board
.vbus_active_low
;
1923 if (udc
->board
.vbus_polled
) {
1924 INIT_WORK(&udc
->vbus_timer_work
, at91_vbus_timer_work
);
1925 setup_timer(&udc
->vbus_timer
, at91_vbus_timer
,
1926 (unsigned long)udc
);
1927 mod_timer(&udc
->vbus_timer
,
1928 jiffies
+ VBUS_POLL_TIMEOUT
);
1930 retval
= devm_request_irq(dev
,
1931 gpio_to_irq(udc
->board
.vbus_pin
),
1932 at91_vbus_irq
, 0, driver_name
, udc
);
1934 DBG("request vbus irq %d failed\n",
1935 udc
->board
.vbus_pin
);
1936 goto err_unprepare_iclk
;
1940 DBG("no VBUS detection, assuming always-on\n");
1943 retval
= usb_add_gadget_udc(dev
, &udc
->gadget
);
1945 goto err_unprepare_iclk
;
1946 dev_set_drvdata(dev
, udc
);
1947 device_init_wakeup(dev
, 1);
1948 create_debug_file(udc
);
1950 INFO("%s version %s\n", driver_name
, DRIVER_VERSION
);
1954 clk_unprepare(udc
->iclk
);
1956 clk_unprepare(udc
->fclk
);
1958 DBG("%s probe failed, %d\n", driver_name
, retval
);
1963 static int at91udc_remove(struct platform_device
*pdev
)
1965 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1966 unsigned long flags
;
1970 usb_del_gadget_udc(&udc
->gadget
);
1974 spin_lock_irqsave(&udc
->lock
, flags
);
1976 spin_unlock_irqrestore(&udc
->lock
, flags
);
1978 device_init_wakeup(&pdev
->dev
, 0);
1979 remove_debug_file(udc
);
1980 clk_unprepare(udc
->fclk
);
1981 clk_unprepare(udc
->iclk
);
1987 static int at91udc_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1989 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
1990 int wake
= udc
->driver
&& device_may_wakeup(&pdev
->dev
);
1991 unsigned long flags
;
1993 /* Unless we can act normally to the host (letting it wake us up
1994 * whenever it has work for us) force disconnect. Wakeup requires
1995 * PLLB for USB events (signaling for reset, wakeup, or incoming
1996 * tokens) and VBUS irqs (on systems which support them).
1998 if ((!udc
->suspended
&& udc
->addr
)
2000 || at91_suspend_entering_slow_clock()) {
2001 spin_lock_irqsave(&udc
->lock
, flags
);
2004 spin_unlock_irqrestore(&udc
->lock
, flags
);
2006 enable_irq_wake(udc
->udp_irq
);
2008 udc
->active_suspend
= wake
;
2009 if (gpio_is_valid(udc
->board
.vbus_pin
) && !udc
->board
.vbus_polled
&& wake
)
2010 enable_irq_wake(udc
->board
.vbus_pin
);
2014 static int at91udc_resume(struct platform_device
*pdev
)
2016 struct at91_udc
*udc
= platform_get_drvdata(pdev
);
2017 unsigned long flags
;
2019 if (gpio_is_valid(udc
->board
.vbus_pin
) && !udc
->board
.vbus_polled
&&
2020 udc
->active_suspend
)
2021 disable_irq_wake(udc
->board
.vbus_pin
);
2023 /* maybe reconnect to host; if so, clocks on */
2024 if (udc
->active_suspend
)
2025 disable_irq_wake(udc
->udp_irq
);
2027 spin_lock_irqsave(&udc
->lock
, flags
);
2029 spin_unlock_irqrestore(&udc
->lock
, flags
);
2034 #define at91udc_suspend NULL
2035 #define at91udc_resume NULL
2038 static struct platform_driver at91_udc_driver
= {
2039 .remove
= at91udc_remove
,
2040 .shutdown
= at91udc_shutdown
,
2041 .suspend
= at91udc_suspend
,
2042 .resume
= at91udc_resume
,
2044 .name
= (char *) driver_name
,
2045 .of_match_table
= at91_udc_dt_ids
,
2049 module_platform_driver_probe(at91_udc_driver
, at91udc_probe
);
2051 MODULE_DESCRIPTION("AT91 udc driver");
2052 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
2053 MODULE_LICENSE("GPL");
2054 MODULE_ALIAS("platform:at91_udc");