doc: Update rcu_assign_pointer() definition in whatisRCU.txt
[linux/fpc-iii.git] / drivers / usb / gadget / udc / at91_udc.c
blob8bc78418d40e4fd98636d55c6592ae83ab3f6cf7
1 /*
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.
14 #undef VERBOSE_DEBUG
15 #undef PACKET_TRACE
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>
31 #include <linux/of.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>
38 #include "at91_udc.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";
63 static const struct {
64 const char *name;
65 const struct usb_ep_caps caps;
66 } ep_info[] = {
67 #define EP_INFO(_name, _caps) \
68 { \
69 .name = _name, \
70 .caps = _caps, \
73 EP_INFO("ep0",
74 USB_EP_CAPS(USB_EP_CAPS_TYPE_CONTROL, USB_EP_CAPS_DIR_ALL)),
75 EP_INFO("ep1",
76 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
77 EP_INFO("ep2",
78 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
79 EP_INFO("ep3-int",
80 USB_EP_CAPS(USB_EP_CAPS_TYPE_INT, USB_EP_CAPS_DIR_ALL)),
81 EP_INFO("ep4",
82 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
83 EP_INFO("ep5",
84 USB_EP_CAPS(USB_EP_CAPS_TYPE_ALL, USB_EP_CAPS_DIR_ALL)),
86 #undef EP_INFO
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"};
115 u32 csr;
116 struct at91_request *req;
117 unsigned long flags;
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... */
126 seq_printf(s, "\n");
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" : "",
131 ep->is_pingpong
132 ? (ep->fifo_bank ? "pong" : "ping")
133 : "",
134 ep->stopped ? " stopped" : "");
135 seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
136 csr,
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 */
143 (!(csr & 0x700))
144 ? ((csr & (1 << 7)) ? " IN" : " OUT")
145 : "",
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",
161 &req->req, length,
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)
169 int i;
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++) {
180 if (mask & (1 << i))
181 seq_printf(s, " ep%d", i);
183 seq_printf(s, "\n");
186 static int proc_udc_show(struct seq_file *s, void *unused)
188 struct at91_udc *udc = s->private;
189 struct at91_ep *ep;
190 u32 tmp;
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",
196 udc->enabled
197 ? (udc->vbus ? "active" : "enabled")
198 : "disabled",
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 */
204 if (!udc->clocked) {
205 seq_printf(s, "(not clocked)\n");
206 return 0;
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) {
234 if (ep->ep.desc)
235 proc_ep_show(s, ep);
238 return 0;
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,
249 .read = seq_read,
250 .llseek = seq_lseek,
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)
261 if (udc->pde)
262 remove_proc_entry(debug_filename, NULL);
265 #else
267 static inline void create_debug_file(struct at91_udc *udc) {}
268 static inline void remove_debug_file(struct at91_udc *udc) {}
270 #endif
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;
283 else
284 status = req->req.status;
285 if (status && status != -ESHUTDOWN)
286 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
288 ep->stopped = 1;
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));
330 u32 csr;
331 u8 *buf;
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
341 rescan:
342 csr = __raw_readl(creg);
343 if ((csr & RX_DATA_READY) == 0)
344 return 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;
352 count = bufferspace;
354 __raw_readsb(dreg, buf, count);
356 /* release and swap pingpong mem bank */
357 csr |= CLR_FX;
358 if (ep->is_pingpong) {
359 if (ep->fifo_bank == 0) {
360 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
361 ep->fifo_bank = 1;
362 } else {
363 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
364 ep->fifo_bank = 0;
366 } else
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)
373 is_done = 1;
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
382 if (is_done)
383 done(ep, req, 0);
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
388 * RX_DATA_BK flags.
390 csr = __raw_readl(creg);
392 bufferspace -= count;
393 buf += count;
394 goto rescan;
397 return is_done;
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;
407 u8 *buf;
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) {
423 csr |= CLR_FX;
424 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
425 __raw_writel(csr, creg);
426 csr = __raw_readl(creg);
428 if (csr & AT91_UDP_TXPKTRDY)
429 return 0;
432 buf = req->req.buf + req->req.actual;
433 prefetch(buf);
434 total = req->req.length - req->req.actual;
435 if (ep->ep.maxpacket < total) {
436 count = ep->ep.maxpacket;
437 is_last = 0;
438 } else {
439 count = total;
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);
457 csr &= ~SET_FX;
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)" : "");
464 if (is_last)
465 done(ep, req, 0);
466 return is_last;
469 static void nuke(struct at91_ep *ep, int status)
471 struct at91_request *req;
473 /* terminate any request in the queue */
474 ep->stopped = 1;
475 if (list_empty(&ep->queue))
476 return;
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;
492 u16 maxpacket;
493 u32 tmp;
494 unsigned long flags;
496 if (!_ep || !ep
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");
502 return -EINVAL;
505 udc = ep->udc;
506 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
507 DBG("bogus device state\n");
508 return -ESHUTDOWN;
511 tmp = usb_endpoint_type(desc);
512 switch (tmp) {
513 case USB_ENDPOINT_XFER_CONTROL:
514 DBG("only one control endpoint\n");
515 return -EINVAL;
516 case USB_ENDPOINT_XFER_INT:
517 if (maxpacket > 64)
518 goto bogus_max;
519 break;
520 case USB_ENDPOINT_XFER_BULK:
521 switch (maxpacket) {
522 case 8:
523 case 16:
524 case 32:
525 case 64:
526 goto ok;
528 bogus_max:
529 DBG("bogus maxpacket %d\n", maxpacket);
530 return -EINVAL;
531 case USB_ENDPOINT_XFER_ISOC:
532 if (!ep->is_pingpong) {
533 DBG("iso requires double buffering\n");
534 return -EINVAL;
536 break;
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);
545 ep->stopped = 0;
546 if (ep->is_in)
547 tmp |= 0x04;
548 tmp <<= 8;
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);
562 return 0;
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;
569 unsigned long flags;
571 if (ep == &ep->udc->ep[0])
572 return -EINVAL;
574 spin_lock_irqsave(&udc->lock, flags);
576 nuke(ep, -ESHUTDOWN);
578 /* restore the endpoint's pristine config */
579 ep->ep.desc = NULL;
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);
590 return 0;
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);
604 if (!req)
605 return NULL;
607 INIT_LIST_HEAD(&req->queue);
608 return &req->req;
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));
617 kfree(req);
620 static int at91_ep_queue(struct usb_ep *_ep,
621 struct usb_request *_req, gfp_t gfp_flags)
623 struct at91_request *req;
624 struct at91_ep *ep;
625 struct at91_udc *udc;
626 int status;
627 unsigned long flags;
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");
635 return -EINVAL;
638 if (!_ep || (!ep->ep.desc && ep->ep.name != ep0name)) {
639 DBG("invalid ep\n");
640 return -EINVAL;
643 udc = ep->udc;
645 if (!udc || !udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
646 DBG("invalid device\n");
647 return -EINVAL;
650 _req->status = -EINPROGRESS;
651 _req->actual = 0;
653 spin_lock_irqsave(&udc->lock, flags);
655 /* try to kickstart any empty and idle queue */
656 if (list_empty(&ep->queue) && !ep->stopped) {
657 int is_ep0;
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);
668 if (is_ep0) {
669 u32 tmp;
671 if (!udc->req_pending) {
672 status = -EINVAL;
673 goto done;
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) {
687 ep0_in_status:
688 PACKET("ep0 in/status\n");
689 status = 0;
690 tmp = __raw_readl(ep->creg);
691 tmp &= ~SET_FX;
692 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
693 __raw_writel(tmp, ep->creg);
694 udc->req_pending = 0;
695 goto done;
699 if (ep->is_in)
700 status = write_fifo(ep, req);
701 else {
702 status = read_fifo(ep, req);
704 /* IN/STATUS stage is otherwise triggered by irq */
705 if (status && is_ep0)
706 goto ep0_in_status;
708 } else
709 status = 0;
711 if (req && !status) {
712 list_add_tail (&req->queue, &ep->queue);
713 at91_udp_write(udc, AT91_UDP_IER, ep->int_mask);
715 done:
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)
722 struct at91_ep *ep;
723 struct at91_request *req;
724 unsigned long flags;
725 struct at91_udc *udc;
727 ep = container_of(_ep, struct at91_ep, ep);
728 if (!_ep || ep->ep.name == ep0name)
729 return -EINVAL;
731 udc = ep->udc;
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)
738 break;
740 if (&req->req != _req) {
741 spin_unlock_irqrestore(&udc->lock, flags);
742 return -EINVAL;
745 done(ep, req, -ECONNRESET);
746 spin_unlock_irqrestore(&udc->lock, flags);
747 return 0;
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;
754 u32 __iomem *creg;
755 u32 csr;
756 unsigned long flags;
757 int status = 0;
759 if (!_ep || ep->is_iso || !ep->udc->clocked)
760 return -EINVAL;
762 creg = ep->creg;
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))
773 status = -EAGAIN;
774 else {
775 csr |= CLR_FX;
776 csr &= ~SET_FX;
777 if (value) {
778 csr |= AT91_UDP_FORCESTALL;
779 VDBG("halt %s\n", ep->ep.name);
780 } else {
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);
789 return status;
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)
810 return -EINVAL;
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);
817 u32 glbstate;
818 int status = -EINVAL;
819 unsigned long flags;
821 DBG("%s\n", __func__ );
822 spin_lock_irqsave(&udc->lock, flags);
824 if (!udc->clocked || !udc->suspended)
825 goto done;
827 /* NOTE: some "early versions" handle ESR differently ... */
829 glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
830 if (!(glbstate & AT91_UDP_ESR))
831 goto done;
832 glbstate |= AT91_UDP_ESR;
833 at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
835 done:
836 spin_unlock_irqrestore(&udc->lock, flags);
837 return status;
840 /* reinit == restore initial software state */
841 static void udc_reinit(struct at91_udc *udc)
843 u32 i;
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];
852 if (i != 0)
853 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
854 ep->ep.desc = NULL;
855 ep->stopped = 0;
856 ep->fifo_bank = 0;
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;
867 int i;
869 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
870 driver = NULL;
871 udc->gadget.speed = USB_SPEED_UNKNOWN;
872 udc->suspended = 0;
874 for (i = 0; i < NUM_ENDPOINTS; i++) {
875 struct at91_ep *ep = &udc->ep[i];
877 ep->stopped = 1;
878 nuke(ep, -ESHUTDOWN);
880 if (driver) {
881 spin_unlock(&udc->lock);
882 usb_gadget_udc_reset(&udc->gadget, driver);
883 spin_lock(&udc->lock);
886 udc_reinit(udc);
889 static void stop_activity(struct at91_udc *udc)
891 struct usb_gadget_driver *driver = udc->driver;
892 int i;
894 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
895 driver = NULL;
896 udc->gadget.speed = USB_SPEED_UNKNOWN;
897 udc->suspended = 0;
899 for (i = 0; i < NUM_ENDPOINTS; i++) {
900 struct at91_ep *ep = &udc->ep[i];
901 ep->stopped = 1;
902 nuke(ep, -ESHUTDOWN);
904 if (driver) {
905 spin_unlock(&udc->lock);
906 driver->disconnect(&udc->gadget);
907 spin_lock(&udc->lock);
910 udc_reinit(udc);
913 static void clk_on(struct at91_udc *udc)
915 if (udc->clocked)
916 return;
917 udc->clocked = 1;
919 clk_enable(udc->iclk);
920 clk_enable(udc->fclk);
923 static void clk_off(struct at91_udc *udc)
925 if (!udc->clocked)
926 return;
927 udc->clocked = 0;
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)
940 is_on = 0;
941 DBG("%sactive\n", is_on ? "" : "in");
943 if (is_on) {
944 clk_on(udc);
945 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
946 at91_udp_write(udc, AT91_UDP_TXVC, 0);
947 } else {
948 stop_activity(udc);
949 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
950 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
951 clk_off(udc);
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);
962 unsigned long flags;
964 /* VDBG("vbus %s\n", is_active ? "on" : "off"); */
965 spin_lock_irqsave(&udc->lock, flags);
966 udc->vbus = (is_active != 0);
967 if (udc->driver)
968 pullup(udc, is_active);
969 else
970 pullup(udc, 0);
971 spin_unlock_irqrestore(&udc->lock, flags);
972 return 0;
975 static int at91_pullup(struct usb_gadget *gadget, int is_on)
977 struct at91_udc *udc = to_udc(gadget);
978 unsigned long flags;
980 spin_lock_irqsave(&udc->lock, flags);
981 udc->enabled = is_on = !!is_on;
982 pullup(udc, is_on);
983 spin_unlock_irqrestore(&udc->lock, flags);
984 return 0;
987 static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
989 struct at91_udc *udc = to_udc(gadget);
990 unsigned long flags;
992 spin_lock_irqsave(&udc->lock, flags);
993 gadget->is_selfpowered = (is_on != 0);
994 spin_unlock_irqrestore(&udc->lock, flags);
995 return 0;
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);
1029 else
1030 req = NULL;
1032 if (ep->is_in) {
1033 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
1034 csr |= CLR_FX;
1035 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
1036 __raw_writel(csr, creg);
1038 if (req)
1039 return write_fifo(ep, req);
1041 } else {
1042 if (csr & AT91_UDP_STALLSENT) {
1043 /* STALLSENT bit == ISOERR */
1044 if (ep->is_iso && req)
1045 req->req.status = -EILSEQ;
1046 csr |= CLR_FX;
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);
1054 return 0;
1057 union setup {
1058 u8 raw[8];
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;
1067 u32 tmp;
1068 union setup pkt;
1069 int status = 0;
1071 /* read and ack SETUP; hard-fail for bogus packets */
1072 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1073 if (likely(rxcount == 8)) {
1074 while (rxcount--)
1075 pkt.raw[i++] = __raw_readb(dreg);
1076 if (pkt.r.bRequestType & USB_DIR_IN) {
1077 csr |= AT91_UDP_DIR;
1078 ep->is_in = 1;
1079 } else {
1080 csr &= ~AT91_UDP_DIR;
1081 ep->is_in = 0;
1083 } else {
1084 /* REVISIT this happens sometimes under load; why?? */
1085 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1086 status = -EINVAL;
1088 csr |= CLR_FX;
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;
1093 ep->stopped = 0;
1094 if (unlikely(status != 0))
1095 goto stall;
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);
1111 csr |= CLR_FX;
1112 csr &= ~SET_FX;
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 */
1122 return;
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;
1127 if (pkt.r.wValue)
1128 udc->wait_for_config_ack = (tmp == 0);
1129 else
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 */
1134 break;
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);
1148 goto write_in;
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)
1153 goto stall;
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);
1157 goto succeed;
1158 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1159 | USB_REQ_CLEAR_FEATURE:
1160 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1161 goto stall;
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);
1165 goto succeed;
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);
1176 goto write_in;
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:
1182 goto stall;
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;
1191 ep = &udc->ep[tmp];
1192 if (tmp >= NUM_ENDPOINTS || (tmp && !ep->ep.desc))
1193 goto stall;
1195 if (tmp) {
1196 if ((w_index & USB_DIR_IN)) {
1197 if (!ep->is_in)
1198 goto stall;
1199 } else if (ep->is_in)
1200 goto stall;
1202 PACKET("get %s status\n", ep->ep.name);
1203 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1204 tmp = (1 << USB_ENDPOINT_HALT);
1205 else
1206 tmp = 0;
1207 __raw_writeb(tmp, dreg);
1208 __raw_writeb(0, dreg);
1209 goto write_in;
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;
1214 ep = &udc->ep[tmp];
1215 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1216 goto stall;
1217 if (!ep->ep.desc || ep->is_iso)
1218 goto stall;
1219 if ((w_index & USB_DIR_IN)) {
1220 if (!ep->is_in)
1221 goto stall;
1222 } else if (ep->is_in)
1223 goto stall;
1225 tmp = __raw_readl(ep->creg);
1226 tmp &= ~SET_FX;
1227 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1228 __raw_writel(tmp, ep->creg);
1229 goto succeed;
1230 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1231 | USB_REQ_CLEAR_FEATURE:
1232 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1233 ep = &udc->ep[tmp];
1234 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1235 goto stall;
1236 if (tmp == 0)
1237 goto succeed;
1238 if (!ep->ep.desc || ep->is_iso)
1239 goto stall;
1240 if ((w_index & USB_DIR_IN)) {
1241 if (!ep->is_in)
1242 goto stall;
1243 } else if (ep->is_in)
1244 goto stall;
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);
1249 tmp |= CLR_FX;
1250 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1251 __raw_writel(tmp, ep->creg);
1252 if (!list_empty(&ep->queue))
1253 handle_ep(ep);
1254 goto succeed;
1257 #undef w_value
1258 #undef w_index
1259 #undef w_length
1261 /* pass request up to the gadget driver */
1262 if (udc->driver) {
1263 spin_unlock(&udc->lock);
1264 status = udc->driver->setup(&udc->gadget, &pkt.r);
1265 spin_lock(&udc->lock);
1267 else
1268 status = -ENODEV;
1269 if (status < 0) {
1270 stall:
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;
1277 return;
1279 succeed:
1280 /* immediate successful (IN) STATUS after zero length DATA */
1281 PACKET("ep0 in/status\n");
1282 write_in:
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)) {
1296 nuke(ep0, -EPROTO);
1297 udc->req_pending = 0;
1298 csr |= CLR_FX;
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) {
1305 nuke(ep0, 0);
1306 udc->req_pending = 0;
1307 handle_setup(udc, ep0, csr);
1308 return;
1311 if (list_empty(&ep0->queue))
1312 req = NULL;
1313 else
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) {
1318 csr |= CLR_FX;
1319 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1321 /* write more IN DATA? */
1322 if (req && ep0->is_in) {
1323 if (handle_ep(ep0))
1324 udc->req_pending = 0;
1327 * Ack after:
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.
1334 } else {
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) {
1343 u32 tmp;
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;
1349 if (udc->addr)
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) {
1361 csr |= CLR_FX;
1362 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1364 /* OUT DATA stage */
1365 if (!ep0->is_in) {
1366 if (req) {
1367 if (handle_ep(ep0)) {
1368 /* send IN/STATUS */
1369 PACKET("ep0 in/status\n");
1370 csr = __raw_readl(creg);
1371 csr &= ~SET_FX;
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. */
1400 } else {
1401 PACKET("ep0 out/status ACK\n");
1402 __raw_writel(csr, creg);
1404 /* "early" status stage */
1405 if (req)
1406 done(ep0, req, 0);
1411 static irqreturn_t at91_udc_irq (int irq, void *_udc)
1413 struct at91_udc *udc = _udc;
1414 u32 rescans = 5;
1415 int disable_clock = 0;
1416 unsigned long flags;
1418 spin_lock_irqsave(&udc->lock, flags);
1420 if (!udc->clocked) {
1421 clk_on(udc);
1422 disable_clock = 1;
1425 while (rescans--) {
1426 u32 status;
1428 status = at91_udp_read(udc, AT91_UDP_ISR)
1429 & at91_udp_read(udc, AT91_UDP_IMR);
1430 if (!status)
1431 break;
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");
1441 udc->addr = 0;
1442 reset_gadget(udc);
1444 /* enable ep0 */
1445 at91_udp_write(udc, AT91_UDP_CSR(0),
1446 AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1447 udc->gadget.speed = USB_SPEED_FULL;
1448 udc->suspended = 0;
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"); */
1464 if (udc->suspended)
1465 continue;
1466 udc->suspended = 1;
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)
1487 continue;
1488 udc->suspended = 0;
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 */
1502 } else {
1503 int i;
1504 unsigned mask = 1;
1505 struct at91_ep *ep = &udc->ep[1];
1507 if (status & mask)
1508 handle_ep0(udc);
1509 for (i = 1; i < NUM_ENDPOINTS; i++) {
1510 mask <<= 1;
1511 if (status & mask)
1512 handle_ep(ep);
1513 ep++;
1518 if (disable_clock)
1519 clk_off(udc);
1521 spin_unlock_irqrestore(&udc->lock, flags);
1523 return IRQ_HANDLED;
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 */
1540 udelay(10);
1541 at91_vbus_update(udc, gpio_get_value(udc->board.vbus_pin));
1543 return IRQ_HANDLED;
1546 static void at91_vbus_timer_work(struct work_struct *work)
1548 struct at91_udc *udc = container_of(work, struct at91_udc,
1549 vbus_timer_work);
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;
1577 udc->enabled = 1;
1578 udc->gadget.is_selfpowered = 1;
1580 return 0;
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);
1590 udc->enabled = 0;
1591 at91_udp_write(udc, AT91_UDP_IDR, ~0);
1592 spin_unlock_irqrestore(&udc->lock, flags);
1594 udc->driver = NULL;
1596 return 0;
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)
1614 struct at91_ep *ep;
1615 int ret;
1616 int i;
1618 for (i = 0; i < NUM_ENDPOINTS; i++) {
1619 ep = &udc->ep[i];
1621 switch (i) {
1622 case 0:
1623 case 3:
1624 ep->maxpacket = 8;
1625 break;
1626 case 1 ... 2:
1627 ep->maxpacket = 64;
1628 break;
1629 case 4 ... 5:
1630 ep->maxpacket = 256;
1631 break;
1635 if (!gpio_is_valid(udc->board.pullup_pin)) {
1636 DBG("no D+ pullup?\n");
1637 return -ENODEV;
1640 ret = devm_gpio_request(&udc->pdev->dev, udc->board.pullup_pin,
1641 "udc_pullup");
1642 if (ret) {
1643 DBG("D+ pullup is busy\n");
1644 return ret;
1647 gpio_direction_output(udc->board.pullup_pin,
1648 udc->board.pullup_active_low);
1650 return 0;
1653 static void at91rm9200_udc_pullup(struct at91_udc *udc, int is_on)
1655 int active = !udc->board.pullup_active_low;
1657 if (is_on)
1658 gpio_set_value(udc->board.pullup_pin, active);
1659 else
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)
1670 struct at91_ep *ep;
1671 int i;
1673 for (i = 0; i < NUM_ENDPOINTS; i++) {
1674 ep = &udc->ep[i];
1676 switch (i) {
1677 case 0 ... 3:
1678 ep->maxpacket = 64;
1679 break;
1680 case 4 ... 5:
1681 ep->maxpacket = 512;
1682 break;
1686 return 0;
1689 static void at91sam9260_udc_pullup(struct at91_udc *udc, int is_on)
1691 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
1693 if (is_on)
1694 txvc |= AT91_UDP_TXVC_PUON;
1695 else
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)
1708 struct at91_ep *ep;
1709 int i;
1711 for (i = 0; i < NUM_ENDPOINTS; i++) {
1712 ep = &udc->ep[i];
1714 switch (i) {
1715 case 0:
1716 ep->maxpacket = 8;
1717 break;
1718 case 1 ... 3:
1719 ep->maxpacket = 64;
1720 break;
1721 case 4 ... 5:
1722 ep->maxpacket = 256;
1723 break;
1727 udc->matrix = syscon_regmap_lookup_by_phandle(udc->pdev->dev.of_node,
1728 "atmel,matrix");
1729 return PTR_ERR_OR_ZERO(udc->matrix);
1732 static void at91sam9261_udc_pullup(struct at91_udc *udc, int is_on)
1734 u32 usbpucr = 0;
1736 if (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)
1750 struct at91_ep *ep;
1751 int i;
1753 for (i = 0; i < NUM_ENDPOINTS; i++) {
1754 ep = &udc->ep[i];
1756 switch (i) {
1757 case 0:
1758 case 1:
1759 case 2:
1760 case 3:
1761 ep->maxpacket = 64;
1762 break;
1763 case 4:
1764 case 5:
1765 ep->maxpacket = 256;
1766 break;
1770 return 0;
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,
1795 { /* sentinel */ }
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;
1804 u32 val;
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,
1810 &flags);
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,
1814 &flags);
1816 board->pullup_active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
1818 match = of_match_node(at91_udc_dt_ids, np);
1819 if (match)
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;
1827 int retval;
1828 struct resource *res;
1829 struct at91_ep *ep;
1830 int i;
1832 udc = devm_kzalloc(dev, sizeof(*udc), GFP_KERNEL);
1833 if (!udc)
1834 return -ENOMEM;
1836 /* init software state */
1837 udc->gadget.dev.parent = dev;
1838 at91udc_of_init(udc, pdev->dev.of_node);
1839 udc->pdev = pdev;
1840 udc->enabled = 0;
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++) {
1849 ep = &udc->ep[i];
1850 ep->ep.name = ep_info[i].name;
1851 ep->ep.caps = ep_info[i].caps;
1852 ep->ep.ops = &at91_ep_ops;
1853 ep->udc = udc;
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);
1866 if (retval)
1867 return retval;
1870 udc_reinit(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);
1884 if (retval)
1885 return retval;
1887 retval = clk_prepare_enable(udc->iclk);
1888 if (retval)
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,
1900 driver_name, udc);
1901 if (retval) {
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,
1908 "udc_vbus");
1909 if (retval) {
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);
1929 } else {
1930 retval = devm_request_irq(dev,
1931 gpio_to_irq(udc->board.vbus_pin),
1932 at91_vbus_irq, 0, driver_name, udc);
1933 if (retval) {
1934 DBG("request vbus irq %d failed\n",
1935 udc->board.vbus_pin);
1936 goto err_unprepare_iclk;
1939 } else {
1940 DBG("no VBUS detection, assuming always-on\n");
1941 udc->vbus = 1;
1943 retval = usb_add_gadget_udc(dev, &udc->gadget);
1944 if (retval)
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);
1951 return 0;
1953 err_unprepare_iclk:
1954 clk_unprepare(udc->iclk);
1955 err_unprepare_fclk:
1956 clk_unprepare(udc->fclk);
1958 DBG("%s probe failed, %d\n", driver_name, retval);
1960 return retval;
1963 static int at91udc_remove(struct platform_device *pdev)
1965 struct at91_udc *udc = platform_get_drvdata(pdev);
1966 unsigned long flags;
1968 DBG("remove\n");
1970 usb_del_gadget_udc(&udc->gadget);
1971 if (udc->driver)
1972 return -EBUSY;
1974 spin_lock_irqsave(&udc->lock, flags);
1975 pullup(udc, 0);
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);
1983 return 0;
1986 #ifdef CONFIG_PM
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)
1999 || !wake
2000 || at91_suspend_entering_slow_clock()) {
2001 spin_lock_irqsave(&udc->lock, flags);
2002 pullup(udc, 0);
2003 wake = 0;
2004 spin_unlock_irqrestore(&udc->lock, flags);
2005 } else
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);
2011 return 0;
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);
2026 else {
2027 spin_lock_irqsave(&udc->lock, flags);
2028 pullup(udc, 1);
2029 spin_unlock_irqrestore(&udc->lock, flags);
2031 return 0;
2033 #else
2034 #define at91udc_suspend NULL
2035 #define at91udc_resume NULL
2036 #endif
2038 static struct platform_driver at91_udc_driver = {
2039 .remove = at91udc_remove,
2040 .shutdown = at91udc_shutdown,
2041 .suspend = at91udc_suspend,
2042 .resume = at91udc_resume,
2043 .driver = {
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");