mac-hid: fix build after lockdep annotation
[linux-2.6/linux-loongson.git] / drivers / usb / gadget / at91_udc.c
blob9b913afb2e6dab7beb727a7400516247b4863a59
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.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #undef VERBOSE_DEBUG
25 #undef PACKET_TRACE
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/delay.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/init.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
38 #include <linux/clk.h>
39 #include <linux/usb/ch9.h>
40 #include <linux/usb/gadget.h>
42 #include <asm/byteorder.h>
43 #include <asm/hardware.h>
44 #include <asm/io.h>
45 #include <asm/irq.h>
46 #include <asm/system.h>
47 #include <asm/mach-types.h>
48 #include <asm/gpio.h>
50 #include <asm/arch/board.h>
51 #include <asm/arch/cpu.h>
52 #include <asm/arch/at91sam9261_matrix.h>
54 #include "at91_udc.h"
58 * This controller is simple and PIO-only. It's used in many AT91-series
59 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
60 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
62 * This driver expects the board has been wired with two GPIOs suppporting
63 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
64 * testing hasn't covered such cases.)
66 * The pullup is most important (so it's integrated on sam926x parts). It
67 * provides software control over whether the host enumerates the device.
69 * The VBUS sensing helps during enumeration, and allows both USB clocks
70 * (and the transceiver) to stay gated off until they're necessary, saving
71 * power. During USB suspend, the 48 MHz clock is gated off in hardware;
72 * it may also be gated off by software during some Linux sleep states.
75 #define DRIVER_VERSION "3 May 2006"
77 static const char driver_name [] = "at91_udc";
78 static const char ep0name[] = "ep0";
81 #define at91_udp_read(dev, reg) \
82 __raw_readl((dev)->udp_baseaddr + (reg))
83 #define at91_udp_write(dev, reg, val) \
84 __raw_writel((val), (dev)->udp_baseaddr + (reg))
86 /*-------------------------------------------------------------------------*/
88 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
90 #include <linux/seq_file.h>
92 static const char debug_filename[] = "driver/udc";
94 #define FOURBITS "%s%s%s%s"
95 #define EIGHTBITS FOURBITS FOURBITS
97 static void proc_ep_show(struct seq_file *s, struct at91_ep *ep)
99 static char *types[] = {
100 "control", "out-iso", "out-bulk", "out-int",
101 "BOGUS", "in-iso", "in-bulk", "in-int"};
103 u32 csr;
104 struct at91_request *req;
105 unsigned long flags;
107 local_irq_save(flags);
109 csr = __raw_readl(ep->creg);
111 /* NOTE: not collecting per-endpoint irq statistics... */
113 seq_printf(s, "\n");
114 seq_printf(s, "%s, maxpacket %d %s%s %s%s\n",
115 ep->ep.name, ep->ep.maxpacket,
116 ep->is_in ? "in" : "out",
117 ep->is_iso ? " iso" : "",
118 ep->is_pingpong
119 ? (ep->fifo_bank ? "pong" : "ping")
120 : "",
121 ep->stopped ? " stopped" : "");
122 seq_printf(s, "csr %08x rxbytes=%d %s %s %s" EIGHTBITS "\n",
123 csr,
124 (csr & 0x07ff0000) >> 16,
125 (csr & (1 << 15)) ? "enabled" : "disabled",
126 (csr & (1 << 11)) ? "DATA1" : "DATA0",
127 types[(csr & 0x700) >> 8],
129 /* iff type is control then print current direction */
130 (!(csr & 0x700))
131 ? ((csr & (1 << 7)) ? " IN" : " OUT")
132 : "",
133 (csr & (1 << 6)) ? " rxdatabk1" : "",
134 (csr & (1 << 5)) ? " forcestall" : "",
135 (csr & (1 << 4)) ? " txpktrdy" : "",
137 (csr & (1 << 3)) ? " stallsent" : "",
138 (csr & (1 << 2)) ? " rxsetup" : "",
139 (csr & (1 << 1)) ? " rxdatabk0" : "",
140 (csr & (1 << 0)) ? " txcomp" : "");
141 if (list_empty (&ep->queue))
142 seq_printf(s, "\t(queue empty)\n");
144 else list_for_each_entry (req, &ep->queue, queue) {
145 unsigned length = req->req.actual;
147 seq_printf(s, "\treq %p len %d/%d buf %p\n",
148 &req->req, length,
149 req->req.length, req->req.buf);
151 local_irq_restore(flags);
154 static void proc_irq_show(struct seq_file *s, const char *label, u32 mask)
156 int i;
158 seq_printf(s, "%s %04x:%s%s" FOURBITS, label, mask,
159 (mask & (1 << 13)) ? " wakeup" : "",
160 (mask & (1 << 12)) ? " endbusres" : "",
162 (mask & (1 << 11)) ? " sofint" : "",
163 (mask & (1 << 10)) ? " extrsm" : "",
164 (mask & (1 << 9)) ? " rxrsm" : "",
165 (mask & (1 << 8)) ? " rxsusp" : "");
166 for (i = 0; i < 8; i++) {
167 if (mask & (1 << i))
168 seq_printf(s, " ep%d", i);
170 seq_printf(s, "\n");
173 static int proc_udc_show(struct seq_file *s, void *unused)
175 struct at91_udc *udc = s->private;
176 struct at91_ep *ep;
177 u32 tmp;
179 seq_printf(s, "%s: version %s\n", driver_name, DRIVER_VERSION);
181 seq_printf(s, "vbus %s, pullup %s, %s powered%s, gadget %s\n\n",
182 udc->vbus ? "present" : "off",
183 udc->enabled
184 ? (udc->vbus ? "active" : "enabled")
185 : "disabled",
186 udc->selfpowered ? "self" : "VBUS",
187 udc->suspended ? ", suspended" : "",
188 udc->driver ? udc->driver->driver.name : "(none)");
190 /* don't access registers when interface isn't clocked */
191 if (!udc->clocked) {
192 seq_printf(s, "(not clocked)\n");
193 return 0;
196 tmp = at91_udp_read(udc, AT91_UDP_FRM_NUM);
197 seq_printf(s, "frame %05x:%s%s frame=%d\n", tmp,
198 (tmp & AT91_UDP_FRM_OK) ? " ok" : "",
199 (tmp & AT91_UDP_FRM_ERR) ? " err" : "",
200 (tmp & AT91_UDP_NUM));
202 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
203 seq_printf(s, "glbstate %02x:%s" FOURBITS "\n", tmp,
204 (tmp & AT91_UDP_RMWUPE) ? " rmwupe" : "",
205 (tmp & AT91_UDP_RSMINPR) ? " rsminpr" : "",
206 (tmp & AT91_UDP_ESR) ? " esr" : "",
207 (tmp & AT91_UDP_CONFG) ? " confg" : "",
208 (tmp & AT91_UDP_FADDEN) ? " fadden" : "");
210 tmp = at91_udp_read(udc, AT91_UDP_FADDR);
211 seq_printf(s, "faddr %03x:%s fadd=%d\n", tmp,
212 (tmp & AT91_UDP_FEN) ? " fen" : "",
213 (tmp & AT91_UDP_FADD));
215 proc_irq_show(s, "imr ", at91_udp_read(udc, AT91_UDP_IMR));
216 proc_irq_show(s, "isr ", at91_udp_read(udc, AT91_UDP_ISR));
218 if (udc->enabled && udc->vbus) {
219 proc_ep_show(s, &udc->ep[0]);
220 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
221 if (ep->desc)
222 proc_ep_show(s, ep);
225 return 0;
228 static int proc_udc_open(struct inode *inode, struct file *file)
230 return single_open(file, proc_udc_show, PDE(inode)->data);
233 static const struct file_operations proc_ops = {
234 .open = proc_udc_open,
235 .read = seq_read,
236 .llseek = seq_lseek,
237 .release = single_release,
240 static void create_debug_file(struct at91_udc *udc)
242 struct proc_dir_entry *pde;
244 pde = create_proc_entry (debug_filename, 0, NULL);
245 udc->pde = pde;
246 if (pde == NULL)
247 return;
249 pde->proc_fops = &proc_ops;
250 pde->data = udc;
253 static void remove_debug_file(struct at91_udc *udc)
255 if (udc->pde)
256 remove_proc_entry(debug_filename, NULL);
259 #else
261 static inline void create_debug_file(struct at91_udc *udc) {}
262 static inline void remove_debug_file(struct at91_udc *udc) {}
264 #endif
267 /*-------------------------------------------------------------------------*/
269 static void done(struct at91_ep *ep, struct at91_request *req, int status)
271 unsigned stopped = ep->stopped;
272 struct at91_udc *udc = ep->udc;
274 list_del_init(&req->queue);
275 if (req->req.status == -EINPROGRESS)
276 req->req.status = status;
277 else
278 status = req->req.status;
279 if (status && status != -ESHUTDOWN)
280 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
282 ep->stopped = 1;
283 req->req.complete(&ep->ep, &req->req);
284 ep->stopped = stopped;
286 /* ep0 is always ready; other endpoints need a non-empty queue */
287 if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
288 at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
291 /*-------------------------------------------------------------------------*/
293 /* bits indicating OUT fifo has data ready */
294 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
297 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
298 * back most of the value you just read (because of side effects, including
299 * bits that may change after reading and before writing).
301 * Except when changing a specific bit, always write values which:
302 * - clear SET_FX bits (setting them could change something)
303 * - set CLR_FX bits (clearing them could change something)
305 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
306 * that shouldn't normally be changed.
308 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
309 * implying a need to wait for one write to complete (test relevant bits)
310 * before starting the next write. This shouldn't be an issue given how
311 * infrequently we write, except maybe for write-then-read idioms.
313 #define SET_FX (AT91_UDP_TXPKTRDY)
314 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
315 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
317 /* pull OUT packet data from the endpoint's fifo */
318 static int read_fifo (struct at91_ep *ep, struct at91_request *req)
320 u32 __iomem *creg = ep->creg;
321 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
322 u32 csr;
323 u8 *buf;
324 unsigned int count, bufferspace, is_done;
326 buf = req->req.buf + req->req.actual;
327 bufferspace = req->req.length - req->req.actual;
330 * there might be nothing to read if ep_queue() calls us,
331 * or if we already emptied both pingpong buffers
333 rescan:
334 csr = __raw_readl(creg);
335 if ((csr & RX_DATA_READY) == 0)
336 return 0;
338 count = (csr & AT91_UDP_RXBYTECNT) >> 16;
339 if (count > ep->ep.maxpacket)
340 count = ep->ep.maxpacket;
341 if (count > bufferspace) {
342 DBG("%s buffer overflow\n", ep->ep.name);
343 req->req.status = -EOVERFLOW;
344 count = bufferspace;
346 __raw_readsb(dreg, buf, count);
348 /* release and swap pingpong mem bank */
349 csr |= CLR_FX;
350 if (ep->is_pingpong) {
351 if (ep->fifo_bank == 0) {
352 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
353 ep->fifo_bank = 1;
354 } else {
355 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
356 ep->fifo_bank = 0;
358 } else
359 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
360 __raw_writel(csr, creg);
362 req->req.actual += count;
363 is_done = (count < ep->ep.maxpacket);
364 if (count == bufferspace)
365 is_done = 1;
367 PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
368 is_done ? " (done)" : "");
371 * avoid extra trips through IRQ logic for packets already in
372 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
374 if (is_done)
375 done(ep, req, 0);
376 else if (ep->is_pingpong) {
377 bufferspace -= count;
378 buf += count;
379 goto rescan;
382 return is_done;
385 /* load fifo for an IN packet */
386 static int write_fifo(struct at91_ep *ep, struct at91_request *req)
388 u32 __iomem *creg = ep->creg;
389 u32 csr = __raw_readl(creg);
390 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
391 unsigned total, count, is_last;
392 u8 *buf;
395 * TODO: allow for writing two packets to the fifo ... that'll
396 * reduce the amount of IN-NAKing, but probably won't affect
397 * throughput much. (Unlike preventing OUT-NAKing!)
401 * If ep_queue() calls us, the queue is empty and possibly in
402 * odd states like TXCOMP not yet cleared (we do it, saving at
403 * least one IRQ) or the fifo not yet being free. Those aren't
404 * issues normally (IRQ handler fast path).
406 if (unlikely(csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY))) {
407 if (csr & AT91_UDP_TXCOMP) {
408 csr |= CLR_FX;
409 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
410 __raw_writel(csr, creg);
411 csr = __raw_readl(creg);
413 if (csr & AT91_UDP_TXPKTRDY)
414 return 0;
417 buf = req->req.buf + req->req.actual;
418 prefetch(buf);
419 total = req->req.length - req->req.actual;
420 if (ep->ep.maxpacket < total) {
421 count = ep->ep.maxpacket;
422 is_last = 0;
423 } else {
424 count = total;
425 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
429 * Write the packet, maybe it's a ZLP.
431 * NOTE: incrementing req->actual before we receive the ACK means
432 * gadget driver IN bytecounts can be wrong in fault cases. That's
433 * fixable with PIO drivers like this one (save "count" here, and
434 * do the increment later on TX irq), but not for most DMA hardware.
436 * So all gadget drivers must accept that potential error. Some
437 * hardware supports precise fifo status reporting, letting them
438 * recover when the actual bytecount matters (e.g. for USB Test
439 * and Measurement Class devices).
441 __raw_writesb(dreg, buf, count);
442 csr &= ~SET_FX;
443 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
444 __raw_writel(csr, creg);
445 req->req.actual += count;
447 PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
448 is_last ? " (done)" : "");
449 if (is_last)
450 done(ep, req, 0);
451 return is_last;
454 static void nuke(struct at91_ep *ep, int status)
456 struct at91_request *req;
458 // terminer chaque requete dans la queue
459 ep->stopped = 1;
460 if (list_empty(&ep->queue))
461 return;
463 VDBG("%s %s\n", __func__, ep->ep.name);
464 while (!list_empty(&ep->queue)) {
465 req = list_entry(ep->queue.next, struct at91_request, queue);
466 done(ep, req, status);
470 /*-------------------------------------------------------------------------*/
472 static int at91_ep_enable(struct usb_ep *_ep,
473 const struct usb_endpoint_descriptor *desc)
475 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
476 struct at91_udc *dev = ep->udc;
477 u16 maxpacket;
478 u32 tmp;
479 unsigned long flags;
481 if (!_ep || !ep
482 || !desc || ep->desc
483 || _ep->name == ep0name
484 || desc->bDescriptorType != USB_DT_ENDPOINT
485 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
486 || maxpacket > ep->maxpacket) {
487 DBG("bad ep or descriptor\n");
488 return -EINVAL;
491 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
492 DBG("bogus device state\n");
493 return -ESHUTDOWN;
496 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
497 switch (tmp) {
498 case USB_ENDPOINT_XFER_CONTROL:
499 DBG("only one control endpoint\n");
500 return -EINVAL;
501 case USB_ENDPOINT_XFER_INT:
502 if (maxpacket > 64)
503 goto bogus_max;
504 break;
505 case USB_ENDPOINT_XFER_BULK:
506 switch (maxpacket) {
507 case 8:
508 case 16:
509 case 32:
510 case 64:
511 goto ok;
513 bogus_max:
514 DBG("bogus maxpacket %d\n", maxpacket);
515 return -EINVAL;
516 case USB_ENDPOINT_XFER_ISOC:
517 if (!ep->is_pingpong) {
518 DBG("iso requires double buffering\n");
519 return -EINVAL;
521 break;
525 local_irq_save(flags);
527 /* initialize endpoint to match this descriptor */
528 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
529 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
530 ep->stopped = 0;
531 if (ep->is_in)
532 tmp |= 0x04;
533 tmp <<= 8;
534 tmp |= AT91_UDP_EPEDS;
535 __raw_writel(tmp, ep->creg);
537 ep->desc = desc;
538 ep->ep.maxpacket = maxpacket;
541 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
542 * since endpoint resets don't reset hw pingpong state.
544 at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
545 at91_udp_write(dev, AT91_UDP_RST_EP, 0);
547 local_irq_restore(flags);
548 return 0;
551 static int at91_ep_disable (struct usb_ep * _ep)
553 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
554 struct at91_udc *udc = ep->udc;
555 unsigned long flags;
557 if (ep == &ep->udc->ep[0])
558 return -EINVAL;
560 local_irq_save(flags);
562 nuke(ep, -ESHUTDOWN);
564 /* restore the endpoint's pristine config */
565 ep->desc = NULL;
566 ep->ep.maxpacket = ep->maxpacket;
568 /* reset fifos and endpoint */
569 if (ep->udc->clocked) {
570 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
571 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
572 __raw_writel(0, ep->creg);
575 local_irq_restore(flags);
576 return 0;
580 * this is a PIO-only driver, so there's nothing
581 * interesting for request or buffer allocation.
584 static struct usb_request *
585 at91_ep_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
587 struct at91_request *req;
589 req = kzalloc(sizeof (struct at91_request), gfp_flags);
590 if (!req)
591 return NULL;
593 INIT_LIST_HEAD(&req->queue);
594 return &req->req;
597 static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
599 struct at91_request *req;
601 req = container_of(_req, struct at91_request, req);
602 BUG_ON(!list_empty(&req->queue));
603 kfree(req);
606 static int at91_ep_queue(struct usb_ep *_ep,
607 struct usb_request *_req, gfp_t gfp_flags)
609 struct at91_request *req;
610 struct at91_ep *ep;
611 struct at91_udc *dev;
612 int status;
613 unsigned long flags;
615 req = container_of(_req, struct at91_request, req);
616 ep = container_of(_ep, struct at91_ep, ep);
618 if (!_req || !_req->complete
619 || !_req->buf || !list_empty(&req->queue)) {
620 DBG("invalid request\n");
621 return -EINVAL;
624 if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
625 DBG("invalid ep\n");
626 return -EINVAL;
629 dev = ep->udc;
631 if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
632 DBG("invalid device\n");
633 return -EINVAL;
636 _req->status = -EINPROGRESS;
637 _req->actual = 0;
639 local_irq_save(flags);
641 /* try to kickstart any empty and idle queue */
642 if (list_empty(&ep->queue) && !ep->stopped) {
643 int is_ep0;
646 * If this control request has a non-empty DATA stage, this
647 * will start that stage. It works just like a non-control
648 * request (until the status stage starts, maybe early).
650 * If the data stage is empty, then this starts a successful
651 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
653 is_ep0 = (ep->ep.name == ep0name);
654 if (is_ep0) {
655 u32 tmp;
657 if (!dev->req_pending) {
658 status = -EINVAL;
659 goto done;
663 * defer changing CONFG until after the gadget driver
664 * reconfigures the endpoints.
666 if (dev->wait_for_config_ack) {
667 tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
668 tmp ^= AT91_UDP_CONFG;
669 VDBG("toggle config\n");
670 at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
672 if (req->req.length == 0) {
673 ep0_in_status:
674 PACKET("ep0 in/status\n");
675 status = 0;
676 tmp = __raw_readl(ep->creg);
677 tmp &= ~SET_FX;
678 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
679 __raw_writel(tmp, ep->creg);
680 dev->req_pending = 0;
681 goto done;
685 if (ep->is_in)
686 status = write_fifo(ep, req);
687 else {
688 status = read_fifo(ep, req);
690 /* IN/STATUS stage is otherwise triggered by irq */
691 if (status && is_ep0)
692 goto ep0_in_status;
694 } else
695 status = 0;
697 if (req && !status) {
698 list_add_tail (&req->queue, &ep->queue);
699 at91_udp_write(dev, AT91_UDP_IER, ep->int_mask);
701 done:
702 local_irq_restore(flags);
703 return (status < 0) ? status : 0;
706 static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
708 struct at91_ep *ep;
709 struct at91_request *req;
711 ep = container_of(_ep, struct at91_ep, ep);
712 if (!_ep || ep->ep.name == ep0name)
713 return -EINVAL;
715 /* make sure it's actually queued on this endpoint */
716 list_for_each_entry (req, &ep->queue, queue) {
717 if (&req->req == _req)
718 break;
720 if (&req->req != _req)
721 return -EINVAL;
723 done(ep, req, -ECONNRESET);
724 return 0;
727 static int at91_ep_set_halt(struct usb_ep *_ep, int value)
729 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
730 struct at91_udc *udc = ep->udc;
731 u32 __iomem *creg;
732 u32 csr;
733 unsigned long flags;
734 int status = 0;
736 if (!_ep || ep->is_iso || !ep->udc->clocked)
737 return -EINVAL;
739 creg = ep->creg;
740 local_irq_save(flags);
742 csr = __raw_readl(creg);
745 * fail with still-busy IN endpoints, ensuring correct sequencing
746 * of data tx then stall. note that the fifo rx bytecount isn't
747 * completely accurate as a tx bytecount.
749 if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
750 status = -EAGAIN;
751 else {
752 csr |= CLR_FX;
753 csr &= ~SET_FX;
754 if (value) {
755 csr |= AT91_UDP_FORCESTALL;
756 VDBG("halt %s\n", ep->ep.name);
757 } else {
758 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
759 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
760 csr &= ~AT91_UDP_FORCESTALL;
762 __raw_writel(csr, creg);
765 local_irq_restore(flags);
766 return status;
769 static const struct usb_ep_ops at91_ep_ops = {
770 .enable = at91_ep_enable,
771 .disable = at91_ep_disable,
772 .alloc_request = at91_ep_alloc_request,
773 .free_request = at91_ep_free_request,
774 .queue = at91_ep_queue,
775 .dequeue = at91_ep_dequeue,
776 .set_halt = at91_ep_set_halt,
777 // there's only imprecise fifo status reporting
780 /*-------------------------------------------------------------------------*/
782 static int at91_get_frame(struct usb_gadget *gadget)
784 struct at91_udc *udc = to_udc(gadget);
786 if (!to_udc(gadget)->clocked)
787 return -EINVAL;
788 return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
791 static int at91_wakeup(struct usb_gadget *gadget)
793 struct at91_udc *udc = to_udc(gadget);
794 u32 glbstate;
795 int status = -EINVAL;
796 unsigned long flags;
798 DBG("%s\n", __func__ );
799 local_irq_save(flags);
801 if (!udc->clocked || !udc->suspended)
802 goto done;
804 /* NOTE: some "early versions" handle ESR differently ... */
806 glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
807 if (!(glbstate & AT91_UDP_ESR))
808 goto done;
809 glbstate |= AT91_UDP_ESR;
810 at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
812 done:
813 local_irq_restore(flags);
814 return status;
817 /* reinit == restore inital software state */
818 static void udc_reinit(struct at91_udc *udc)
820 u32 i;
822 INIT_LIST_HEAD(&udc->gadget.ep_list);
823 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
825 for (i = 0; i < NUM_ENDPOINTS; i++) {
826 struct at91_ep *ep = &udc->ep[i];
828 if (i != 0)
829 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
830 ep->desc = NULL;
831 ep->stopped = 0;
832 ep->fifo_bank = 0;
833 ep->ep.maxpacket = ep->maxpacket;
834 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
835 // initialiser une queue par endpoint
836 INIT_LIST_HEAD(&ep->queue);
840 static void stop_activity(struct at91_udc *udc)
842 struct usb_gadget_driver *driver = udc->driver;
843 int i;
845 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
846 driver = NULL;
847 udc->gadget.speed = USB_SPEED_UNKNOWN;
848 udc->suspended = 0;
850 for (i = 0; i < NUM_ENDPOINTS; i++) {
851 struct at91_ep *ep = &udc->ep[i];
852 ep->stopped = 1;
853 nuke(ep, -ESHUTDOWN);
855 if (driver)
856 driver->disconnect(&udc->gadget);
858 udc_reinit(udc);
861 static void clk_on(struct at91_udc *udc)
863 if (udc->clocked)
864 return;
865 udc->clocked = 1;
866 clk_enable(udc->iclk);
867 clk_enable(udc->fclk);
870 static void clk_off(struct at91_udc *udc)
872 if (!udc->clocked)
873 return;
874 udc->clocked = 0;
875 udc->gadget.speed = USB_SPEED_UNKNOWN;
876 clk_disable(udc->fclk);
877 clk_disable(udc->iclk);
881 * activate/deactivate link with host; minimize power usage for
882 * inactive links by cutting clocks and transceiver power.
884 static void pullup(struct at91_udc *udc, int is_on)
886 int active = !udc->board.pullup_active_low;
888 if (!udc->enabled || !udc->vbus)
889 is_on = 0;
890 DBG("%sactive\n", is_on ? "" : "in");
892 if (is_on) {
893 clk_on(udc);
894 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
895 at91_udp_write(udc, AT91_UDP_TXVC, 0);
896 if (cpu_is_at91rm9200())
897 gpio_set_value(udc->board.pullup_pin, active);
898 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
899 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
901 txvc |= AT91_UDP_TXVC_PUON;
902 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
903 } else if (cpu_is_at91sam9261()) {
904 u32 usbpucr;
906 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
907 usbpucr |= AT91_MATRIX_USBPUCR_PUON;
908 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
910 } else {
911 stop_activity(udc);
912 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
913 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
914 if (cpu_is_at91rm9200())
915 gpio_set_value(udc->board.pullup_pin, !active);
916 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
917 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
919 txvc &= ~AT91_UDP_TXVC_PUON;
920 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
921 } else if (cpu_is_at91sam9261()) {
922 u32 usbpucr;
924 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
925 usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
926 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
928 clk_off(udc);
932 /* vbus is here! turn everything on that's ready */
933 static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
935 struct at91_udc *udc = to_udc(gadget);
936 unsigned long flags;
938 // VDBG("vbus %s\n", is_active ? "on" : "off");
939 local_irq_save(flags);
940 udc->vbus = (is_active != 0);
941 if (udc->driver)
942 pullup(udc, is_active);
943 else
944 pullup(udc, 0);
945 local_irq_restore(flags);
946 return 0;
949 static int at91_pullup(struct usb_gadget *gadget, int is_on)
951 struct at91_udc *udc = to_udc(gadget);
952 unsigned long flags;
954 local_irq_save(flags);
955 udc->enabled = is_on = !!is_on;
956 pullup(udc, is_on);
957 local_irq_restore(flags);
958 return 0;
961 static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
963 struct at91_udc *udc = to_udc(gadget);
964 unsigned long flags;
966 local_irq_save(flags);
967 udc->selfpowered = (is_on != 0);
968 local_irq_restore(flags);
969 return 0;
972 static const struct usb_gadget_ops at91_udc_ops = {
973 .get_frame = at91_get_frame,
974 .wakeup = at91_wakeup,
975 .set_selfpowered = at91_set_selfpowered,
976 .vbus_session = at91_vbus_session,
977 .pullup = at91_pullup,
980 * VBUS-powered devices may also also want to support bigger
981 * power budgets after an appropriate SET_CONFIGURATION.
983 // .vbus_power = at91_vbus_power,
986 /*-------------------------------------------------------------------------*/
988 static int handle_ep(struct at91_ep *ep)
990 struct at91_request *req;
991 u32 __iomem *creg = ep->creg;
992 u32 csr = __raw_readl(creg);
994 if (!list_empty(&ep->queue))
995 req = list_entry(ep->queue.next,
996 struct at91_request, queue);
997 else
998 req = NULL;
1000 if (ep->is_in) {
1001 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
1002 csr |= CLR_FX;
1003 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
1004 __raw_writel(csr, creg);
1006 if (req)
1007 return write_fifo(ep, req);
1009 } else {
1010 if (csr & AT91_UDP_STALLSENT) {
1011 /* STALLSENT bit == ISOERR */
1012 if (ep->is_iso && req)
1013 req->req.status = -EILSEQ;
1014 csr |= CLR_FX;
1015 csr &= ~(SET_FX | AT91_UDP_STALLSENT);
1016 __raw_writel(csr, creg);
1017 csr = __raw_readl(creg);
1019 if (req && (csr & RX_DATA_READY))
1020 return read_fifo(ep, req);
1022 return 0;
1025 union setup {
1026 u8 raw[8];
1027 struct usb_ctrlrequest r;
1030 static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
1032 u32 __iomem *creg = ep->creg;
1033 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
1034 unsigned rxcount, i = 0;
1035 u32 tmp;
1036 union setup pkt;
1037 int status = 0;
1039 /* read and ack SETUP; hard-fail for bogus packets */
1040 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
1041 if (likely(rxcount == 8)) {
1042 while (rxcount--)
1043 pkt.raw[i++] = __raw_readb(dreg);
1044 if (pkt.r.bRequestType & USB_DIR_IN) {
1045 csr |= AT91_UDP_DIR;
1046 ep->is_in = 1;
1047 } else {
1048 csr &= ~AT91_UDP_DIR;
1049 ep->is_in = 0;
1051 } else {
1052 // REVISIT this happens sometimes under load; why??
1053 ERR("SETUP len %d, csr %08x\n", rxcount, csr);
1054 status = -EINVAL;
1056 csr |= CLR_FX;
1057 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
1058 __raw_writel(csr, creg);
1059 udc->wait_for_addr_ack = 0;
1060 udc->wait_for_config_ack = 0;
1061 ep->stopped = 0;
1062 if (unlikely(status != 0))
1063 goto stall;
1065 #define w_index le16_to_cpu(pkt.r.wIndex)
1066 #define w_value le16_to_cpu(pkt.r.wValue)
1067 #define w_length le16_to_cpu(pkt.r.wLength)
1069 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1070 pkt.r.bRequestType, pkt.r.bRequest,
1071 w_value, w_index, w_length);
1074 * A few standard requests get handled here, ones that touch
1075 * hardware ... notably for device and endpoint features.
1077 udc->req_pending = 1;
1078 csr = __raw_readl(creg);
1079 csr |= CLR_FX;
1080 csr &= ~SET_FX;
1081 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
1083 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1084 | USB_REQ_SET_ADDRESS:
1085 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
1086 udc->addr = w_value;
1087 udc->wait_for_addr_ack = 1;
1088 udc->req_pending = 0;
1089 /* FADDR is set later, when we ack host STATUS */
1090 return;
1092 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1093 | USB_REQ_SET_CONFIGURATION:
1094 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
1095 if (pkt.r.wValue)
1096 udc->wait_for_config_ack = (tmp == 0);
1097 else
1098 udc->wait_for_config_ack = (tmp != 0);
1099 if (udc->wait_for_config_ack)
1100 VDBG("wait for config\n");
1101 /* CONFG is toggled later, if gadget driver succeeds */
1102 break;
1105 * Hosts may set or clear remote wakeup status, and
1106 * devices may report they're VBUS powered.
1108 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1109 | USB_REQ_GET_STATUS:
1110 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1111 if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1112 tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1113 PACKET("get device status\n");
1114 __raw_writeb(tmp, dreg);
1115 __raw_writeb(0, dreg);
1116 goto write_in;
1117 /* then STATUS starts later, automatically */
1118 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1119 | USB_REQ_SET_FEATURE:
1120 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1121 goto stall;
1122 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1123 tmp |= AT91_UDP_ESR;
1124 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1125 goto succeed;
1126 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1127 | USB_REQ_CLEAR_FEATURE:
1128 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1129 goto stall;
1130 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1131 tmp &= ~AT91_UDP_ESR;
1132 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1133 goto succeed;
1136 * Interfaces have no feature settings; this is pretty useless.
1137 * we won't even insist the interface exists...
1139 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1140 | USB_REQ_GET_STATUS:
1141 PACKET("get interface status\n");
1142 __raw_writeb(0, dreg);
1143 __raw_writeb(0, dreg);
1144 goto write_in;
1145 /* then STATUS starts later, automatically */
1146 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1147 | USB_REQ_SET_FEATURE:
1148 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1149 | USB_REQ_CLEAR_FEATURE:
1150 goto stall;
1153 * Hosts may clear bulk/intr endpoint halt after the gadget
1154 * driver sets it (not widely used); or set it (for testing)
1156 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1157 | USB_REQ_GET_STATUS:
1158 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1159 ep = &udc->ep[tmp];
1160 if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
1161 goto stall;
1163 if (tmp) {
1164 if ((w_index & USB_DIR_IN)) {
1165 if (!ep->is_in)
1166 goto stall;
1167 } else if (ep->is_in)
1168 goto stall;
1170 PACKET("get %s status\n", ep->ep.name);
1171 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1172 tmp = (1 << USB_ENDPOINT_HALT);
1173 else
1174 tmp = 0;
1175 __raw_writeb(tmp, dreg);
1176 __raw_writeb(0, dreg);
1177 goto write_in;
1178 /* then STATUS starts later, automatically */
1179 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1180 | USB_REQ_SET_FEATURE:
1181 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1182 ep = &udc->ep[tmp];
1183 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1184 goto stall;
1185 if (!ep->desc || ep->is_iso)
1186 goto stall;
1187 if ((w_index & USB_DIR_IN)) {
1188 if (!ep->is_in)
1189 goto stall;
1190 } else if (ep->is_in)
1191 goto stall;
1193 tmp = __raw_readl(ep->creg);
1194 tmp &= ~SET_FX;
1195 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1196 __raw_writel(tmp, ep->creg);
1197 goto succeed;
1198 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1199 | USB_REQ_CLEAR_FEATURE:
1200 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1201 ep = &udc->ep[tmp];
1202 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1203 goto stall;
1204 if (tmp == 0)
1205 goto succeed;
1206 if (!ep->desc || ep->is_iso)
1207 goto stall;
1208 if ((w_index & USB_DIR_IN)) {
1209 if (!ep->is_in)
1210 goto stall;
1211 } else if (ep->is_in)
1212 goto stall;
1214 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
1215 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
1216 tmp = __raw_readl(ep->creg);
1217 tmp |= CLR_FX;
1218 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1219 __raw_writel(tmp, ep->creg);
1220 if (!list_empty(&ep->queue))
1221 handle_ep(ep);
1222 goto succeed;
1225 #undef w_value
1226 #undef w_index
1227 #undef w_length
1229 /* pass request up to the gadget driver */
1230 if (udc->driver)
1231 status = udc->driver->setup(&udc->gadget, &pkt.r);
1232 else
1233 status = -ENODEV;
1234 if (status < 0) {
1235 stall:
1236 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1237 pkt.r.bRequestType, pkt.r.bRequest, status);
1238 csr |= AT91_UDP_FORCESTALL;
1239 __raw_writel(csr, creg);
1240 udc->req_pending = 0;
1242 return;
1244 succeed:
1245 /* immediate successful (IN) STATUS after zero length DATA */
1246 PACKET("ep0 in/status\n");
1247 write_in:
1248 csr |= AT91_UDP_TXPKTRDY;
1249 __raw_writel(csr, creg);
1250 udc->req_pending = 0;
1251 return;
1254 static void handle_ep0(struct at91_udc *udc)
1256 struct at91_ep *ep0 = &udc->ep[0];
1257 u32 __iomem *creg = ep0->creg;
1258 u32 csr = __raw_readl(creg);
1259 struct at91_request *req;
1261 if (unlikely(csr & AT91_UDP_STALLSENT)) {
1262 nuke(ep0, -EPROTO);
1263 udc->req_pending = 0;
1264 csr |= CLR_FX;
1265 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1266 __raw_writel(csr, creg);
1267 VDBG("ep0 stalled\n");
1268 csr = __raw_readl(creg);
1270 if (csr & AT91_UDP_RXSETUP) {
1271 nuke(ep0, 0);
1272 udc->req_pending = 0;
1273 handle_setup(udc, ep0, csr);
1274 return;
1277 if (list_empty(&ep0->queue))
1278 req = NULL;
1279 else
1280 req = list_entry(ep0->queue.next, struct at91_request, queue);
1282 /* host ACKed an IN packet that we sent */
1283 if (csr & AT91_UDP_TXCOMP) {
1284 csr |= CLR_FX;
1285 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1287 /* write more IN DATA? */
1288 if (req && ep0->is_in) {
1289 if (handle_ep(ep0))
1290 udc->req_pending = 0;
1293 * Ack after:
1294 * - last IN DATA packet (including GET_STATUS)
1295 * - IN/STATUS for OUT DATA
1296 * - IN/STATUS for any zero-length DATA stage
1297 * except for the IN DATA case, the host should send
1298 * an OUT status later, which we'll ack.
1300 } else {
1301 udc->req_pending = 0;
1302 __raw_writel(csr, creg);
1305 * SET_ADDRESS takes effect only after the STATUS
1306 * (to the original address) gets acked.
1308 if (udc->wait_for_addr_ack) {
1309 u32 tmp;
1311 at91_udp_write(udc, AT91_UDP_FADDR,
1312 AT91_UDP_FEN | udc->addr);
1313 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1314 tmp &= ~AT91_UDP_FADDEN;
1315 if (udc->addr)
1316 tmp |= AT91_UDP_FADDEN;
1317 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1319 udc->wait_for_addr_ack = 0;
1320 VDBG("address %d\n", udc->addr);
1325 /* OUT packet arrived ... */
1326 else if (csr & AT91_UDP_RX_DATA_BK0) {
1327 csr |= CLR_FX;
1328 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1330 /* OUT DATA stage */
1331 if (!ep0->is_in) {
1332 if (req) {
1333 if (handle_ep(ep0)) {
1334 /* send IN/STATUS */
1335 PACKET("ep0 in/status\n");
1336 csr = __raw_readl(creg);
1337 csr &= ~SET_FX;
1338 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1339 __raw_writel(csr, creg);
1340 udc->req_pending = 0;
1342 } else if (udc->req_pending) {
1344 * AT91 hardware has a hard time with this
1345 * "deferred response" mode for control-OUT
1346 * transfers. (For control-IN it's fine.)
1348 * The normal solution leaves OUT data in the
1349 * fifo until the gadget driver is ready.
1350 * We couldn't do that here without disabling
1351 * the IRQ that tells about SETUP packets,
1352 * e.g. when the host gets impatient...
1354 * Working around it by copying into a buffer
1355 * would almost be a non-deferred response,
1356 * except that it wouldn't permit reliable
1357 * stalling of the request. Instead, demand
1358 * that gadget drivers not use this mode.
1360 DBG("no control-OUT deferred responses!\n");
1361 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1362 udc->req_pending = 0;
1365 /* STATUS stage for control-IN; ack. */
1366 } else {
1367 PACKET("ep0 out/status ACK\n");
1368 __raw_writel(csr, creg);
1370 /* "early" status stage */
1371 if (req)
1372 done(ep0, req, 0);
1377 static irqreturn_t at91_udc_irq (int irq, void *_udc)
1379 struct at91_udc *udc = _udc;
1380 u32 rescans = 5;
1382 while (rescans--) {
1383 u32 status;
1385 status = at91_udp_read(udc, AT91_UDP_ISR)
1386 & at91_udp_read(udc, AT91_UDP_IMR);
1387 if (!status)
1388 break;
1390 /* USB reset irq: not maskable */
1391 if (status & AT91_UDP_ENDBUSRES) {
1392 at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1393 at91_udp_write(udc, AT91_UDP_IER, MINIMUS_INTERRUPTUS);
1394 /* Atmel code clears this irq twice */
1395 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1396 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1397 VDBG("end bus reset\n");
1398 udc->addr = 0;
1399 stop_activity(udc);
1401 /* enable ep0 */
1402 at91_udp_write(udc, AT91_UDP_CSR(0),
1403 AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1404 udc->gadget.speed = USB_SPEED_FULL;
1405 udc->suspended = 0;
1406 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_EP(0));
1409 * NOTE: this driver keeps clocks off unless the
1410 * USB host is present. That saves power, but for
1411 * boards that don't support VBUS detection, both
1412 * clocks need to be active most of the time.
1415 /* host initiated suspend (3+ms bus idle) */
1416 } else if (status & AT91_UDP_RXSUSP) {
1417 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1418 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM);
1419 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1420 // VDBG("bus suspend\n");
1421 if (udc->suspended)
1422 continue;
1423 udc->suspended = 1;
1426 * NOTE: when suspending a VBUS-powered device, the
1427 * gadget driver should switch into slow clock mode
1428 * and then into standby to avoid drawing more than
1429 * 500uA power (2500uA for some high-power configs).
1431 if (udc->driver && udc->driver->suspend)
1432 udc->driver->suspend(&udc->gadget);
1434 /* host initiated resume */
1435 } else if (status & AT91_UDP_RXRSM) {
1436 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1437 at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);
1438 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1439 // VDBG("bus resume\n");
1440 if (!udc->suspended)
1441 continue;
1442 udc->suspended = 0;
1445 * NOTE: for a VBUS-powered device, the gadget driver
1446 * would normally want to switch out of slow clock
1447 * mode into normal mode.
1449 if (udc->driver && udc->driver->resume)
1450 udc->driver->resume(&udc->gadget);
1452 /* endpoint IRQs are cleared by handling them */
1453 } else {
1454 int i;
1455 unsigned mask = 1;
1456 struct at91_ep *ep = &udc->ep[1];
1458 if (status & mask)
1459 handle_ep0(udc);
1460 for (i = 1; i < NUM_ENDPOINTS; i++) {
1461 mask <<= 1;
1462 if (status & mask)
1463 handle_ep(ep);
1464 ep++;
1469 return IRQ_HANDLED;
1472 /*-------------------------------------------------------------------------*/
1474 static void nop_release(struct device *dev)
1476 /* nothing to free */
1479 static struct at91_udc controller = {
1480 .gadget = {
1481 .ops = &at91_udc_ops,
1482 .ep0 = &controller.ep[0].ep,
1483 .name = driver_name,
1484 .dev = {
1485 .bus_id = "gadget",
1486 .release = nop_release,
1489 .ep[0] = {
1490 .ep = {
1491 .name = ep0name,
1492 .ops = &at91_ep_ops,
1494 .udc = &controller,
1495 .maxpacket = 8,
1496 .int_mask = 1 << 0,
1498 .ep[1] = {
1499 .ep = {
1500 .name = "ep1",
1501 .ops = &at91_ep_ops,
1503 .udc = &controller,
1504 .is_pingpong = 1,
1505 .maxpacket = 64,
1506 .int_mask = 1 << 1,
1508 .ep[2] = {
1509 .ep = {
1510 .name = "ep2",
1511 .ops = &at91_ep_ops,
1513 .udc = &controller,
1514 .is_pingpong = 1,
1515 .maxpacket = 64,
1516 .int_mask = 1 << 2,
1518 .ep[3] = {
1519 .ep = {
1520 /* could actually do bulk too */
1521 .name = "ep3-int",
1522 .ops = &at91_ep_ops,
1524 .udc = &controller,
1525 .maxpacket = 8,
1526 .int_mask = 1 << 3,
1528 .ep[4] = {
1529 .ep = {
1530 .name = "ep4",
1531 .ops = &at91_ep_ops,
1533 .udc = &controller,
1534 .is_pingpong = 1,
1535 .maxpacket = 256,
1536 .int_mask = 1 << 4,
1538 .ep[5] = {
1539 .ep = {
1540 .name = "ep5",
1541 .ops = &at91_ep_ops,
1543 .udc = &controller,
1544 .is_pingpong = 1,
1545 .maxpacket = 256,
1546 .int_mask = 1 << 5,
1548 /* ep6 and ep7 are also reserved (custom silicon might use them) */
1551 static irqreturn_t at91_vbus_irq(int irq, void *_udc)
1553 struct at91_udc *udc = _udc;
1554 unsigned value;
1556 /* vbus needs at least brief debouncing */
1557 udelay(10);
1558 value = gpio_get_value(udc->board.vbus_pin);
1559 if (value != udc->vbus)
1560 at91_vbus_session(&udc->gadget, value);
1562 return IRQ_HANDLED;
1565 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1567 struct at91_udc *udc = &controller;
1568 int retval;
1570 if (!driver
1571 || driver->speed < USB_SPEED_FULL
1572 || !driver->bind
1573 || !driver->setup) {
1574 DBG("bad parameter.\n");
1575 return -EINVAL;
1578 if (udc->driver) {
1579 DBG("UDC already has a gadget driver\n");
1580 return -EBUSY;
1583 udc->driver = driver;
1584 udc->gadget.dev.driver = &driver->driver;
1585 udc->gadget.dev.driver_data = &driver->driver;
1586 udc->enabled = 1;
1587 udc->selfpowered = 1;
1589 retval = driver->bind(&udc->gadget);
1590 if (retval) {
1591 DBG("driver->bind() returned %d\n", retval);
1592 udc->driver = NULL;
1593 udc->gadget.dev.driver = NULL;
1594 udc->gadget.dev.driver_data = NULL;
1595 udc->enabled = 0;
1596 udc->selfpowered = 0;
1597 return retval;
1600 local_irq_disable();
1601 pullup(udc, 1);
1602 local_irq_enable();
1604 DBG("bound to %s\n", driver->driver.name);
1605 return 0;
1607 EXPORT_SYMBOL (usb_gadget_register_driver);
1609 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
1611 struct at91_udc *udc = &controller;
1613 if (!driver || driver != udc->driver || !driver->unbind)
1614 return -EINVAL;
1616 local_irq_disable();
1617 udc->enabled = 0;
1618 at91_udp_write(udc, AT91_UDP_IDR, ~0);
1619 pullup(udc, 0);
1620 local_irq_enable();
1622 driver->unbind(&udc->gadget);
1623 udc->gadget.dev.driver = NULL;
1624 udc->gadget.dev.driver_data = NULL;
1625 udc->driver = NULL;
1627 DBG("unbound from %s\n", driver->driver.name);
1628 return 0;
1630 EXPORT_SYMBOL (usb_gadget_unregister_driver);
1632 /*-------------------------------------------------------------------------*/
1634 static void at91udc_shutdown(struct platform_device *dev)
1636 /* force disconnect on reboot */
1637 pullup(platform_get_drvdata(dev), 0);
1640 static int __init at91udc_probe(struct platform_device *pdev)
1642 struct device *dev = &pdev->dev;
1643 struct at91_udc *udc;
1644 int retval;
1645 struct resource *res;
1647 if (!dev->platform_data) {
1648 /* small (so we copy it) but critical! */
1649 DBG("missing platform_data\n");
1650 return -ENODEV;
1653 if (pdev->num_resources != 2) {
1654 DBG("invalid num_resources\n");
1655 return -ENODEV;
1657 if ((pdev->resource[0].flags != IORESOURCE_MEM)
1658 || (pdev->resource[1].flags != IORESOURCE_IRQ)) {
1659 DBG("invalid resource type\n");
1660 return -ENODEV;
1663 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1664 if (!res)
1665 return -ENXIO;
1667 if (!request_mem_region(res->start,
1668 res->end - res->start + 1,
1669 driver_name)) {
1670 DBG("someone's using UDC memory\n");
1671 return -EBUSY;
1674 /* init software state */
1675 udc = &controller;
1676 udc->gadget.dev.parent = dev;
1677 udc->board = *(struct at91_udc_data *) dev->platform_data;
1678 udc->pdev = pdev;
1679 udc->enabled = 0;
1681 /* rm9200 needs manual D+ pullup; off by default */
1682 if (cpu_is_at91rm9200()) {
1683 if (udc->board.pullup_pin <= 0) {
1684 DBG("no D+ pullup?\n");
1685 retval = -ENODEV;
1686 goto fail0;
1688 retval = gpio_request(udc->board.pullup_pin, "udc_pullup");
1689 if (retval) {
1690 DBG("D+ pullup is busy\n");
1691 goto fail0;
1693 gpio_direction_output(udc->board.pullup_pin,
1694 udc->board.pullup_active_low);
1697 udc->udp_baseaddr = ioremap(res->start, res->end - res->start + 1);
1698 if (!udc->udp_baseaddr) {
1699 retval = -ENOMEM;
1700 goto fail0a;
1703 udc_reinit(udc);
1705 /* get interface and function clocks */
1706 udc->iclk = clk_get(dev, "udc_clk");
1707 udc->fclk = clk_get(dev, "udpck");
1708 if (IS_ERR(udc->iclk) || IS_ERR(udc->fclk)) {
1709 DBG("clocks missing\n");
1710 retval = -ENODEV;
1711 /* NOTE: we "know" here that refcounts on these are NOPs */
1712 goto fail0b;
1715 retval = device_register(&udc->gadget.dev);
1716 if (retval < 0)
1717 goto fail0b;
1719 /* don't do anything until we have both gadget driver and VBUS */
1720 clk_enable(udc->iclk);
1721 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
1722 at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
1723 /* Clear all pending interrupts - UDP may be used by bootloader. */
1724 at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
1725 clk_disable(udc->iclk);
1727 /* request UDC and maybe VBUS irqs */
1728 udc->udp_irq = platform_get_irq(pdev, 0);
1729 retval = request_irq(udc->udp_irq, at91_udc_irq,
1730 IRQF_DISABLED, driver_name, udc);
1731 if (retval < 0) {
1732 DBG("request irq %d failed\n", udc->udp_irq);
1733 goto fail1;
1735 if (udc->board.vbus_pin > 0) {
1736 retval = gpio_request(udc->board.vbus_pin, "udc_vbus");
1737 if (retval < 0) {
1738 DBG("request vbus pin failed\n");
1739 goto fail2;
1741 gpio_direction_input(udc->board.vbus_pin);
1744 * Get the initial state of VBUS - we cannot expect
1745 * a pending interrupt.
1747 udc->vbus = gpio_get_value(udc->board.vbus_pin);
1748 if (request_irq(udc->board.vbus_pin, at91_vbus_irq,
1749 IRQF_DISABLED, driver_name, udc)) {
1750 DBG("request vbus irq %d failed\n",
1751 udc->board.vbus_pin);
1752 free_irq(udc->udp_irq, udc);
1753 retval = -EBUSY;
1754 goto fail3;
1756 } else {
1757 DBG("no VBUS detection, assuming always-on\n");
1758 udc->vbus = 1;
1760 dev_set_drvdata(dev, udc);
1761 device_init_wakeup(dev, 1);
1762 create_debug_file(udc);
1764 INFO("%s version %s\n", driver_name, DRIVER_VERSION);
1765 return 0;
1767 fail3:
1768 if (udc->board.vbus_pin > 0)
1769 gpio_free(udc->board.vbus_pin);
1770 fail2:
1771 free_irq(udc->udp_irq, udc);
1772 fail1:
1773 device_unregister(&udc->gadget.dev);
1774 fail0b:
1775 iounmap(udc->udp_baseaddr);
1776 fail0a:
1777 if (cpu_is_at91rm9200())
1778 gpio_free(udc->board.pullup_pin);
1779 fail0:
1780 release_mem_region(res->start, res->end - res->start + 1);
1781 DBG("%s probe failed, %d\n", driver_name, retval);
1782 return retval;
1785 static int __exit at91udc_remove(struct platform_device *pdev)
1787 struct at91_udc *udc = platform_get_drvdata(pdev);
1788 struct resource *res;
1790 DBG("remove\n");
1792 if (udc->driver)
1793 return -EBUSY;
1795 pullup(udc, 0);
1797 device_init_wakeup(&pdev->dev, 0);
1798 remove_debug_file(udc);
1799 if (udc->board.vbus_pin > 0) {
1800 free_irq(udc->board.vbus_pin, udc);
1801 gpio_free(udc->board.vbus_pin);
1803 free_irq(udc->udp_irq, udc);
1804 device_unregister(&udc->gadget.dev);
1806 iounmap(udc->udp_baseaddr);
1808 if (cpu_is_at91rm9200())
1809 gpio_free(udc->board.pullup_pin);
1811 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1812 release_mem_region(res->start, res->end - res->start + 1);
1814 clk_put(udc->iclk);
1815 clk_put(udc->fclk);
1817 return 0;
1820 #ifdef CONFIG_PM
1821 static int at91udc_suspend(struct platform_device *pdev, pm_message_t mesg)
1823 struct at91_udc *udc = platform_get_drvdata(pdev);
1824 int wake = udc->driver && device_may_wakeup(&pdev->dev);
1826 /* Unless we can act normally to the host (letting it wake us up
1827 * whenever it has work for us) force disconnect. Wakeup requires
1828 * PLLB for USB events (signaling for reset, wakeup, or incoming
1829 * tokens) and VBUS irqs (on systems which support them).
1831 if ((!udc->suspended && udc->addr)
1832 || !wake
1833 || at91_suspend_entering_slow_clock()) {
1834 pullup(udc, 0);
1835 wake = 0;
1836 } else
1837 enable_irq_wake(udc->udp_irq);
1839 udc->active_suspend = wake;
1840 if (udc->board.vbus_pin > 0 && wake)
1841 enable_irq_wake(udc->board.vbus_pin);
1842 return 0;
1845 static int at91udc_resume(struct platform_device *pdev)
1847 struct at91_udc *udc = platform_get_drvdata(pdev);
1849 if (udc->board.vbus_pin > 0 && udc->active_suspend)
1850 disable_irq_wake(udc->board.vbus_pin);
1852 /* maybe reconnect to host; if so, clocks on */
1853 if (udc->active_suspend)
1854 disable_irq_wake(udc->udp_irq);
1855 else
1856 pullup(udc, 1);
1857 return 0;
1859 #else
1860 #define at91udc_suspend NULL
1861 #define at91udc_resume NULL
1862 #endif
1864 static struct platform_driver at91_udc_driver = {
1865 .remove = __exit_p(at91udc_remove),
1866 .shutdown = at91udc_shutdown,
1867 .suspend = at91udc_suspend,
1868 .resume = at91udc_resume,
1869 .driver = {
1870 .name = (char *) driver_name,
1871 .owner = THIS_MODULE,
1875 static int __init udc_init_module(void)
1877 return platform_driver_probe(&at91_udc_driver, at91udc_probe);
1879 module_init(udc_init_module);
1881 static void __exit udc_exit_module(void)
1883 platform_driver_unregister(&at91_udc_driver);
1885 module_exit(udc_exit_module);
1887 MODULE_DESCRIPTION("AT91 udc driver");
1888 MODULE_AUTHOR("Thomas Rathbone, David Brownell");
1889 MODULE_LICENSE("GPL");
1890 MODULE_ALIAS("platform:at91_udc");