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.
26 //#define PACKET_TRACE 1
29 #include <asm/errno.h>
30 #include <linux/list.h>
32 #include <asm/arch/at91_pmc.h>
33 #include <asm/arch-at91/cpu.h>
34 #include <asm/arch/at91sam9261_matrix.h>
36 #include <linux/usb/ch9.h>
37 #include <linux/usb/gadget.h>
39 #include <asm/byteorder.h>
40 #include <asm/hardware.h>
42 #include <asm-arm/proc-armv/system.h>
43 #include <asm/mach-types.h>
45 #include <asm/arch/gpio.h>
49 #include <usb/at91_udc.h>
53 * This controller is simple and PIO-only. It's used in many AT91-series
54 * full speed USB controllers, including the at91rm9200 (arm920T, with MMU),
55 * at91sam926x (arm926ejs, with MMU), and several no-mmu versions.
57 * This driver expects the board has been wired with two GPIOs suppporting
58 * a VBUS sensing IRQ, and a D+ pullup. (They may be omitted, but the
59 * testing hasn't covered such cases.)
61 * The pullup is most important (so it's integrated on sam926x parts). It
62 * provides software control over whether the host enumerates the device.
64 * The VBUS sensing helps during enumeration, and allows both USB clocks
65 * (and the transceiver) to stay gated off until they're necessary, saving
66 * power. During USB suspend, the 48 MHz clock is gated off in hardware;
67 * it may also be gated off by software during some Linux sleep states.
70 #define DRIVER_VERSION "3 May 2006"
72 static const char driver_name[] = "at91_udc";
73 static const char ep0name[] = "ep0";
76 #define at91_udp_read(dev, reg) \
77 __raw_readl((dev)->udp_baseaddr + (reg))
78 #define at91_udp_write(dev, reg, val) \
79 __raw_writel((val), (dev)->udp_baseaddr + (reg))
81 /*============================================================================*/
83 #define MAX_REQ (REQ_COUNT-1)
84 static struct at91_request req_pool[REQ_COUNT];
86 static void init_requests(void)
90 for (i = 0; i < MAX_REQ; i++) {
95 static void free_request(struct at91_request* ptr)
97 if (ptr < &req_pool[0] && ptr > &req_pool[REQ_COUNT]){
98 printf("%s: ptr 0x%p does not specify valid req\n", \
101 printf("%s: ptr not marked as \"in_use\"\n", __func__);
108 static struct at91_request* alloc_request(void)
111 struct at91_request* ptr=NULL;
113 for (i = 0; i < MAX_REQ; i++) {
114 if (!req_pool[i].in_use) {
116 req_pool[i].in_use=1;
117 DBG("alloc_req: request[%d]\n",i);
122 DBG("panic: no more free req buffers\n");
126 /*-------------------------------------------------------------------------*/
128 static void done(struct at91_ep *ep, struct at91_request *req, int status)
130 unsigned stopped = ep->stopped;
131 struct at91_udc *udc = ep->udc;
133 list_del_init(&req->queue);
134 if (req->req.status == -EINPROGRESS)
135 req->req.status = status;
137 status = req->req.status;
138 if (status && status != -ESHUTDOWN)
139 VDBG("%s done %p, status %d\n", ep->ep.name, req, status);
142 req->req.complete(&ep->ep, &req->req);
143 ep->stopped = stopped;
145 /* ep0 is always ready; other endpoints need a non-empty queue */
146 if (list_empty(&ep->queue) && ep->int_mask != (1 << 0))
147 at91_udp_write(udc, AT91_UDP_IDR, ep->int_mask);
150 /*-------------------------------------------------------------------------*/
152 /* bits indicating OUT fifo has data ready */
153 #define RX_DATA_READY (AT91_UDP_RX_DATA_BK0 | AT91_UDP_RX_DATA_BK1)
156 * Endpoint FIFO CSR bits have a mix of bits, making it unsafe to just write
157 * back most of the value you just read (because of side effects, including
158 * bits that may change after reading and before writing).
160 * Except when changing a specific bit, always write values which:
161 * - clear SET_FX bits (setting them could change something)
162 * - set CLR_FX bits (clearing them could change something)
164 * There are also state bits like FORCESTALL, EPEDS, DIR, and EPTYPE
165 * that shouldn't normally be changed.
167 * NOTE at91sam9260 docs mention synch between UDPCK and MCK clock domains,
168 * implying a need to wait for one write to complete (test relevant bits)
169 * before starting the next write. This shouldn't be an issue given how
170 * infrequently we write, except maybe for write-then-read idioms.
172 #define SET_FX (AT91_UDP_TXPKTRDY)
173 #define CLR_FX (RX_DATA_READY | AT91_UDP_RXSETUP \
174 | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)
176 /* pull OUT packet data from the endpoint's fifo */
177 static int read_fifo (struct at91_ep *ep, struct at91_request *req)
179 u32 __iomem *creg = ep->creg;
180 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
183 unsigned int count, bufferspace, is_done;
185 buf = req->req.buf + req->req.actual;
186 bufferspace = req->req.length - req->req.actual;
189 * there might be nothing to read if ep_queue() calls us,
190 * or if we already emptied both pingpong buffers
193 csr = __raw_readl(creg);
194 if ((csr & RX_DATA_READY) == 0)
197 count = (csr & AT91_UDP_RXBYTECNT) >> 16;
198 if (count > ep->ep.maxpacket)
199 count = ep->ep.maxpacket;
200 if (count > bufferspace) {
201 DBG("%s buffer overflow\n", ep->ep.name);
202 req->req.status = -EOVERFLOW;
205 __raw_readsb((unsigned long)dreg, buf, count);
207 /* release and swap pingpong mem bank */
209 if (ep->is_pingpong) {
210 if (ep->fifo_bank == 0) {
211 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
214 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK1);
218 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
219 __raw_writel(csr, creg);
221 req->req.actual += count;
222 is_done = (count < ep->ep.maxpacket);
223 if (count == bufferspace)
226 PACKET("%s %p out/%d%s\n", ep->ep.name, &req->req, count,
227 is_done ? " (done)" : "");
230 * avoid extra trips through IRQ logic for packets already in
231 * the fifo ... maybe preventing an extra (expensive) OUT-NAK
235 else if (ep->is_pingpong) {
236 bufferspace -= count;
244 /* load fifo for an IN packet */
245 static int write_fifo(struct at91_ep *ep, struct at91_request *req)
247 u32 __iomem *creg = ep->creg;
248 u32 csr = __raw_readl(creg);
249 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
250 unsigned total, count, is_last;
253 * TODO: allow for writing two packets to the fifo ... that'll
254 * reduce the amount of IN-NAKing, but probably won't affect
255 * throughput much. (Unlike preventing OUT-NAKing!)
259 * If ep_queue() calls us, the queue is empty and possibly in
260 * odd states like TXCOMP not yet cleared (we do it, saving at
261 * least one IRQ) or the fifo not yet being free. Those aren't
262 * issues normally (IRQ handler fast path).
264 if (csr & (AT91_UDP_TXCOMP | AT91_UDP_TXPKTRDY)) {
265 if (csr & AT91_UDP_TXCOMP) {
267 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
268 __raw_writel(csr, creg);
269 csr = __raw_readl(creg);
271 if (csr & AT91_UDP_TXPKTRDY)
275 total = req->req.length - req->req.actual;
276 if (ep->ep.maxpacket < total) {
277 count = ep->ep.maxpacket;
281 is_last = (count < ep->ep.maxpacket) || !req->req.zero;
285 * Write the packet, maybe it's a ZLP.
287 * NOTE: incrementing req->actual before we receive the ACK means
288 * gadget driver IN bytecounts can be wrong in fault cases. That's
289 * fixable with PIO drivers like this one (save "count" here, and
290 * do the increment later on TX irq), but not for most DMA hardware.
292 * So all gadget drivers must accept that potential error. Some
293 * hardware supports precise fifo status reporting, letting them
294 * recover when the actual bytecount matters (e.g. for USB Test
295 * and Measurement Class devices).
298 __raw_writesb((unsigned long)dreg,
299 req->req.buf + req->req.actual, count);
301 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
302 __raw_writel(csr, creg);
303 req->req.actual += count;
305 PACKET("%s %p in/%d%s\n", ep->ep.name, &req->req, count,
306 is_last ? " (done)" : "");
312 static void nuke(struct at91_ep *ep, int status)
314 struct at91_request *req;
316 // terminer chaque requete dans la queue
318 if (list_empty(&ep->queue))
321 VDBG("%s %s\n", __func__, ep->ep.name);
322 while (!list_empty(&ep->queue)) {
323 req = list_entry(ep->queue.next, struct at91_request, queue);
324 done(ep, req, status);
328 /*-------------------------------------------------------------------------*/
330 static int at91_ep_enable(struct usb_ep *_ep,
331 const struct usb_endpoint_descriptor *desc)
333 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
334 struct at91_udc *dev = ep->udc;
341 || _ep->name == ep0name
342 || desc->bDescriptorType != USB_DT_ENDPOINT
343 || (maxpacket = le16_to_cpu(desc->wMaxPacketSize)) == 0
344 || maxpacket > ep->maxpacket) {
345 DBG("bad ep or descriptor\n");
349 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
350 DBG("bogus device state\n");
354 tmp = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
356 case USB_ENDPOINT_XFER_CONTROL:
357 DBG("only one control endpoint\n");
359 case USB_ENDPOINT_XFER_INT:
363 case USB_ENDPOINT_XFER_BULK:
372 DBG("bogus maxpacket %d\n", maxpacket);
374 case USB_ENDPOINT_XFER_ISOC:
375 if (!ep->is_pingpong) {
376 DBG("iso requires double buffering\n");
383 local_irq_save(flags);
385 /* initialize endpoint to match this descriptor */
386 ep->is_in = (desc->bEndpointAddress & USB_DIR_IN) != 0;
387 ep->is_iso = (tmp == USB_ENDPOINT_XFER_ISOC);
392 tmp |= AT91_UDP_EPEDS;
393 __raw_writel(tmp, ep->creg);
396 ep->ep.maxpacket = maxpacket;
399 * reset/init endpoint fifo. NOTE: leaves fifo_bank alone,
400 * since endpoint resets don't reset hw pingpong state.
402 at91_udp_write(dev, AT91_UDP_RST_EP, ep->int_mask);
403 at91_udp_write(dev, AT91_UDP_RST_EP, 0);
405 local_irq_restore(flags);
409 static int at91_ep_disable (struct usb_ep * _ep)
411 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
412 struct at91_udc *udc = ep->udc;
415 if (ep == &ep->udc->ep[0])
418 local_irq_save(flags);
420 nuke(ep, -ESHUTDOWN);
422 /* restore the endpoint's pristine config */
424 ep->ep.maxpacket = ep->maxpacket;
426 /* reset fifos and endpoint */
427 if (ep->udc->clocked) {
428 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
429 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
430 __raw_writel(0, ep->creg);
433 local_irq_restore(flags);
438 * this is a PIO-only driver, so there's nothing
439 * interesting for request or buffer allocation.
442 static struct usb_request *
443 at91_ep_alloc_request(struct usb_ep *_ep, unsigned int gfp_flags)
445 struct at91_request *req;
447 req = alloc_request();
452 INIT_LIST_HEAD(&req->queue);
456 static void at91_ep_free_request(struct usb_ep *_ep, struct usb_request *_req)
458 struct at91_request *req;
460 req = container_of(_req, struct at91_request, req);
462 * BUG_ON(!list_empty(&req->queue));
467 static int at91_ep_queue(struct usb_ep *_ep,
468 struct usb_request *_req, gfp_t gfp_flags)
470 struct at91_request *req;
472 struct at91_udc *dev;
476 req = container_of(_req, struct at91_request, req);
477 ep = container_of(_ep, struct at91_ep, ep);
479 if (!_req || !_req->complete
480 || !_req->buf || !list_empty(&req->queue)) {
481 DBG("invalid request\n");
485 if (!_ep || (!ep->desc && ep->ep.name != ep0name)) {
492 if (!dev || !dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) {
493 DBG("invalid device\n");
497 _req->status = -EINPROGRESS;
500 local_irq_save(flags);
502 /* try to kickstart any empty and idle queue */
503 if (list_empty(&ep->queue) && !ep->stopped) {
507 * If this control request has a non-empty DATA stage, this
508 * will start that stage. It works just like a non-control
509 * request (until the status stage starts, maybe early).
511 * If the data stage is empty, then this starts a successful
512 * IN/STATUS stage. (Unsuccessful ones use set_halt.)
514 is_ep0 = (ep->ep.name == ep0name);
518 if (!dev->req_pending) {
524 * defer changing CONFG until after the gadget driver
525 * reconfigures the endpoints.
527 if (dev->wait_for_config_ack) {
528 tmp = at91_udp_read(dev, AT91_UDP_GLB_STAT);
529 tmp ^= AT91_UDP_CONFG;
530 VDBG("toggle config\n");
531 at91_udp_write(dev, AT91_UDP_GLB_STAT, tmp);
533 if (req->req.length == 0) {
535 PACKET("ep0 in/status\n");
537 tmp = __raw_readl(ep->creg);
539 tmp |= CLR_FX | AT91_UDP_TXPKTRDY;
540 __raw_writel(tmp, ep->creg);
541 dev->req_pending = 0;
547 status = write_fifo(ep, req);
549 status = read_fifo(ep, req);
551 /* IN/STATUS stage is otherwise triggered by irq */
552 if (status && is_ep0)
558 if (req && !status) {
559 list_add_tail (&req->queue, &ep->queue);
562 local_irq_restore(flags);
563 return (status < 0) ? status : 0;
566 static int at91_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
569 struct at91_request *req;
571 ep = container_of(_ep, struct at91_ep, ep);
572 if (!_ep || ep->ep.name == ep0name)
575 /* make sure it's actually queued on this endpoint */
576 list_for_each_entry (req, &ep->queue, queue) {
577 if (&req->req == _req)
580 if (&req->req != _req)
583 done(ep, req, -ECONNRESET);
587 static int at91_ep_set_halt(struct usb_ep *_ep, int value)
589 struct at91_ep *ep = container_of(_ep, struct at91_ep, ep);
590 struct at91_udc *udc = ep->udc;
596 if (!_ep || ep->is_iso || !ep->udc->clocked)
600 local_irq_save(flags);
602 csr = __raw_readl(creg);
605 * fail with still-busy IN endpoints, ensuring correct sequencing
606 * of data tx then stall. note that the fifo rx bytecount isn't
607 * completely accurate as a tx bytecount.
609 if (ep->is_in && (!list_empty(&ep->queue) || (csr >> 16) != 0))
615 csr |= AT91_UDP_FORCESTALL;
616 VDBG("halt %s\n", ep->ep.name);
618 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
619 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
620 csr &= ~AT91_UDP_FORCESTALL;
622 __raw_writel(csr, creg);
625 local_irq_restore(flags);
629 static const struct usb_ep_ops at91_ep_ops = {
630 .enable = at91_ep_enable,
631 .disable = at91_ep_disable,
632 .alloc_request = at91_ep_alloc_request,
633 .free_request = at91_ep_free_request,
634 .queue = at91_ep_queue,
635 .dequeue = at91_ep_dequeue,
636 .set_halt = at91_ep_set_halt,
637 // there's only imprecise fifo status reporting
640 /*-------------------------------------------------------------------------*/
642 static int at91_get_frame(struct usb_gadget *gadget)
644 struct at91_udc *udc = to_udc(gadget);
646 if (!to_udc(gadget)->clocked)
648 return at91_udp_read(udc, AT91_UDP_FRM_NUM) & AT91_UDP_NUM;
651 static int at91_wakeup(struct usb_gadget *gadget)
653 struct at91_udc *udc = to_udc(gadget);
655 int status = -EINVAL;
658 DBG("%s\n", __func__ );
659 local_irq_save(flags);
661 if (!udc->clocked || !udc->suspended)
664 /* NOTE: some "early versions" handle ESR differently ... */
666 glbstate = at91_udp_read(udc, AT91_UDP_GLB_STAT);
667 if (!(glbstate & AT91_UDP_ESR))
669 glbstate |= AT91_UDP_ESR;
670 at91_udp_write(udc, AT91_UDP_GLB_STAT, glbstate);
673 local_irq_restore(flags);
677 /* reinit == restore inital software state */
678 static void udc_reinit(struct at91_udc *udc)
682 INIT_LIST_HEAD(&udc->gadget.ep_list);
683 INIT_LIST_HEAD(&udc->gadget.ep0->ep_list);
685 for (i = 0; i < NUM_ENDPOINTS; i++) {
686 struct at91_ep *ep = &udc->ep[i];
689 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
693 ep->ep.maxpacket = ep->maxpacket;
694 ep->creg = (void __iomem *) udc->udp_baseaddr + AT91_UDP_CSR(i);
695 // initialiser une queue par endpoint
696 INIT_LIST_HEAD(&ep->queue);
700 static void stop_activity(struct at91_udc *udc)
702 struct usb_gadget_driver *driver = udc->driver;
705 if (udc->gadget.speed == USB_SPEED_UNKNOWN)
707 udc->gadget.speed = USB_SPEED_UNKNOWN;
710 for (i = 0; i < NUM_ENDPOINTS; i++) {
711 struct at91_ep *ep = &udc->ep[i];
713 nuke(ep, -ESHUTDOWN);
716 driver->disconnect(&udc->gadget);
721 static inline void clk_enable(unsigned id)
723 at91_sys_write(AT91_PMC_PCER, 1 << id);
726 static inline void clk_disable(unsigned id)
728 at91_sys_write(AT91_PMC_PCER, 1 << id);
731 static void clk_on(struct at91_udc *udc)
736 clk_enable(udc->iclk);
737 /*clk_enable(udc->fclk);*/
740 static void clk_off(struct at91_udc *udc)
745 udc->gadget.speed = USB_SPEED_UNKNOWN;
747 /*clk_disable(udc->fclk);*/
748 clk_disable(udc->iclk);
751 #define VBUS_DOWNTIME 5 /* seconds */
753 * activate/deactivate link with host; minimize power usage for
754 * inactive links by cutting clocks and transceiver power.
756 static void pullup(struct at91_udc *udc, int is_on)
758 static int last_pulldown, pulledup_once;
759 if (!udc->enabled || !udc->vbus)
764 * We need to slow down the pullup/pulldown sequence.
765 * between pull-down and up at least 5 seconds time is
766 * required, Windows cannot keep up properly.
769 ulong last = 0, time = get_timer(last_pulldown);
770 printf("Holding VBUS down: ");
771 while (time < (VBUS_DOWNTIME * CONFIG_SYS_HZ)) {
772 if (((time % CONFIG_SYS_HZ) == 0) &&
776 (time / CONFIG_SYS_HZ));
779 time = get_timer(last_pulldown);
785 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
786 at91_udp_write(udc, AT91_UDP_TXVC, 0);
787 if (cpu_is_at91rm9200())
788 at91_set_gpio_value(udc->board.pullup_pin, 1);
789 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
790 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
792 txvc |= AT91_UDP_TXVC_PUON;
793 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
794 } else if (cpu_is_at91sam9261()) {
797 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
798 usbpucr |= AT91_MATRIX_USBPUCR_PUON;
799 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
804 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
805 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
806 if (cpu_is_at91rm9200())
807 at91_set_gpio_value(udc->board.pullup_pin, 0);
808 else if (cpu_is_at91sam9260() || cpu_is_at91sam9263()) {
809 u32 txvc = at91_udp_read(udc, AT91_UDP_TXVC);
811 txvc &= ~AT91_UDP_TXVC_PUON;
812 at91_udp_write(udc, AT91_UDP_TXVC, txvc);
813 } else if (cpu_is_at91sam9261()) {
816 usbpucr = at91_sys_read(AT91_MATRIX_USBPUCR);
817 usbpucr &= ~AT91_MATRIX_USBPUCR_PUON;
818 at91_sys_write(AT91_MATRIX_USBPUCR, usbpucr);
822 /* Only interesting if it has been pulled up once. */
824 last_pulldown = get_timer(0);
828 /* vbus is here! turn everything on that's ready */
829 static int at91_vbus_session(struct usb_gadget *gadget, int is_active)
831 struct at91_udc *udc = to_udc(gadget);
834 /* VDBG("vbus %s\n", is_active ? "on" : "off"); */
835 local_irq_save(flags);
837 udc->vbus = (is_active != 0);
838 pullup(udc, is_active);
841 local_irq_restore(flags);
845 static int at91_pullup(struct usb_gadget *gadget, int is_on)
847 struct at91_udc *udc = to_udc(gadget);
850 local_irq_save(flags);
851 udc->enabled = is_on = !!is_on;
853 local_irq_restore(flags);
857 static int at91_set_selfpowered(struct usb_gadget *gadget, int is_on)
859 struct at91_udc *udc = to_udc(gadget);
862 local_irq_save(flags);
863 udc->selfpowered = (is_on != 0);
864 local_irq_restore(flags);
868 static const struct usb_gadget_ops at91_udc_ops = {
869 .get_frame = at91_get_frame,
870 .wakeup = at91_wakeup,
871 .set_selfpowered = at91_set_selfpowered,
872 .vbus_session = at91_vbus_session,
873 .pullup = at91_pullup,
876 * VBUS-powered devices may also also want to support bigger
877 * power budgets after an appropriate SET_CONFIGURATION.
879 // .vbus_power = at91_vbus_power,
882 /*-------------------------------------------------------------------------*/
884 static int handle_ep(struct at91_ep *ep)
886 struct at91_request *req;
887 u32 __iomem *creg = ep->creg;
888 u32 csr = __raw_readl(creg);
890 if (!list_empty(&ep->queue))
891 req = list_entry(ep->queue.next,
892 struct at91_request, queue);
897 if (csr & (AT91_UDP_STALLSENT | AT91_UDP_TXCOMP)) {
899 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_TXCOMP);
900 __raw_writel(csr, creg);
903 return write_fifo(ep, req);
906 if (csr & AT91_UDP_STALLSENT) {
907 /* STALLSENT bit == ISOERR */
908 if (ep->is_iso && req)
909 req->req.status = -EILSEQ;
911 csr &= ~(SET_FX | AT91_UDP_STALLSENT);
912 __raw_writel(csr, creg);
913 csr = __raw_readl(creg);
915 if (req && (csr & RX_DATA_READY))
916 return read_fifo(ep, req);
923 struct usb_ctrlrequest r;
926 static void handle_setup(struct at91_udc *udc, struct at91_ep *ep, u32 csr)
928 u32 __iomem *creg = ep->creg;
929 u8 __iomem *dreg = ep->creg + (AT91_UDP_FDR(0) - AT91_UDP_CSR(0));
930 unsigned rxcount, i = 0;
935 /* read and ack SETUP; hard-fail for bogus packets */
936 rxcount = (csr & AT91_UDP_RXBYTECNT) >> 16;
939 pkt.raw[i++] = __raw_readb(dreg);
940 if (pkt.r.bRequestType & USB_DIR_IN) {
944 csr &= ~AT91_UDP_DIR;
948 // REVISIT this happens sometimes under load; why??
949 printf("error: SETUP len %d, csr %08x\n", rxcount, csr);
953 csr &= ~(SET_FX | AT91_UDP_RXSETUP);
954 __raw_writel(csr, creg);
955 udc->wait_for_addr_ack = 0;
956 udc->wait_for_config_ack = 0;
961 #define w_index le16_to_cpu(pkt.r.wIndex)
962 #define w_value le16_to_cpu(pkt.r.wValue)
963 #define w_length le16_to_cpu(pkt.r.wLength)
965 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
966 pkt.r.bRequestType, pkt.r.bRequest,
967 w_value, w_index, w_length);
970 * A few standard requests get handled here, ones that touch
971 * hardware ... notably for device and endpoint features.
973 udc->req_pending = 1;
974 csr = __raw_readl(creg);
977 switch ((pkt.r.bRequestType << 8) | pkt.r.bRequest) {
979 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
980 | USB_REQ_SET_ADDRESS:
981 __raw_writel(csr | AT91_UDP_TXPKTRDY, creg);
983 udc->wait_for_addr_ack = 1;
984 udc->req_pending = 0;
985 /* FADDR is set later, when we ack host STATUS */
988 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
989 | USB_REQ_SET_CONFIGURATION:
990 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_CONFG;
992 udc->wait_for_config_ack = (tmp == 0);
994 udc->wait_for_config_ack = (tmp != 0);
995 if (udc->wait_for_config_ack)
996 VDBG("wait for config\n");
997 /* CONFG is toggled later, if gadget driver succeeds */
1001 * Hosts may set or clear remote wakeup status, and
1002 * devices may report they're VBUS powered.
1004 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1005 | USB_REQ_GET_STATUS:
1006 tmp = (udc->selfpowered << USB_DEVICE_SELF_POWERED);
1007 if (at91_udp_read(udc, AT91_UDP_GLB_STAT) & AT91_UDP_ESR)
1008 tmp |= (1 << USB_DEVICE_REMOTE_WAKEUP);
1009 PACKET("get device status\n");
1010 __raw_writeb(tmp, dreg);
1011 __raw_writeb(0, dreg);
1013 /* then STATUS starts later, automatically */
1014 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1015 | USB_REQ_SET_FEATURE:
1016 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1018 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1019 tmp |= AT91_UDP_ESR;
1020 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1022 case ((USB_TYPE_STANDARD|USB_RECIP_DEVICE) << 8)
1023 | USB_REQ_CLEAR_FEATURE:
1024 if (w_value != USB_DEVICE_REMOTE_WAKEUP)
1026 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1027 tmp &= ~AT91_UDP_ESR;
1028 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1032 * Interfaces have no feature settings; this is pretty useless.
1033 * we won't even insist the interface exists...
1035 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1036 | USB_REQ_GET_STATUS:
1037 PACKET("get interface status\n");
1038 __raw_writeb(0, dreg);
1039 __raw_writeb(0, dreg);
1041 /* then STATUS starts later, automatically */
1042 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1043 | USB_REQ_SET_FEATURE:
1044 case ((USB_TYPE_STANDARD|USB_RECIP_INTERFACE) << 8)
1045 | USB_REQ_CLEAR_FEATURE:
1049 * Hosts may clear bulk/intr endpoint halt after the gadget
1050 * driver sets it (not widely used); or set it (for testing)
1052 case ((USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1053 | USB_REQ_GET_STATUS:
1054 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1056 if (tmp >= NUM_ENDPOINTS || (tmp && !ep->desc))
1060 if ((w_index & USB_DIR_IN)) {
1063 } else if (ep->is_in)
1066 PACKET("get %s status\n", ep->ep.name);
1067 if (__raw_readl(ep->creg) & AT91_UDP_FORCESTALL)
1068 tmp = (1 << USB_ENDPOINT_HALT);
1071 __raw_writeb(tmp, dreg);
1072 __raw_writeb(0, dreg);
1074 /* then STATUS starts later, automatically */
1075 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1076 | USB_REQ_SET_FEATURE:
1077 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1079 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1081 if (!ep->desc || ep->is_iso)
1083 if ((w_index & USB_DIR_IN)) {
1086 } else if (ep->is_in)
1089 tmp = __raw_readl(ep->creg);
1091 tmp |= CLR_FX | AT91_UDP_FORCESTALL;
1092 __raw_writel(tmp, ep->creg);
1094 case ((USB_TYPE_STANDARD|USB_RECIP_ENDPOINT) << 8)
1095 | USB_REQ_CLEAR_FEATURE:
1096 tmp = w_index & USB_ENDPOINT_NUMBER_MASK;
1098 if (w_value != USB_ENDPOINT_HALT || tmp >= NUM_ENDPOINTS)
1102 if (!ep->desc || ep->is_iso)
1104 if ((w_index & USB_DIR_IN)) {
1107 } else if (ep->is_in)
1110 at91_udp_write(udc, AT91_UDP_RST_EP, ep->int_mask);
1111 at91_udp_write(udc, AT91_UDP_RST_EP, 0);
1112 tmp = __raw_readl(ep->creg);
1114 tmp &= ~(SET_FX | AT91_UDP_FORCESTALL);
1115 __raw_writel(tmp, ep->creg);
1116 if (!list_empty(&ep->queue))
1125 /* pass request up to the gadget driver */
1127 status = udc->driver->setup(&udc->gadget, &pkt.r);
1132 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1133 pkt.r.bRequestType, pkt.r.bRequest, status);
1134 csr |= AT91_UDP_FORCESTALL;
1135 __raw_writel(csr, creg);
1136 udc->req_pending = 0;
1141 /* immediate successful (IN) STATUS after zero length DATA */
1142 PACKET("ep0 in/status\n");
1144 csr |= AT91_UDP_TXPKTRDY;
1145 __raw_writel(csr, creg);
1146 udc->req_pending = 0;
1150 static void handle_ep0(struct at91_udc *udc)
1152 struct at91_ep *ep0 = &udc->ep[0];
1153 u32 __iomem *creg = ep0->creg;
1154 u32 csr = __raw_readl(creg);
1155 struct at91_request *req;
1157 // VDBG("%s\n",__func__);
1159 if (csr & AT91_UDP_STALLSENT) {
1161 udc->req_pending = 0;
1163 csr &= ~(SET_FX | AT91_UDP_STALLSENT | AT91_UDP_FORCESTALL);
1164 __raw_writel(csr, creg);
1165 VDBG("ep0 stalled\n");
1166 csr = __raw_readl(creg);
1168 if (csr & AT91_UDP_RXSETUP) {
1170 udc->req_pending = 0;
1171 handle_setup(udc, ep0, csr);
1175 if (list_empty(&ep0->queue))
1178 req = list_entry(ep0->queue.next, struct at91_request, queue);
1180 /* host ACKed an IN packet that we sent */
1181 if (csr & AT91_UDP_TXCOMP) {
1183 csr &= ~(SET_FX | AT91_UDP_TXCOMP);
1185 /* write more IN DATA? */
1186 if (req && ep0->is_in) {
1188 udc->req_pending = 0;
1192 * - last IN DATA packet (including GET_STATUS)
1193 * - IN/STATUS for OUT DATA
1194 * - IN/STATUS for any zero-length DATA stage
1195 * except for the IN DATA case, the host should send
1196 * an OUT status later, which we'll ack.
1199 udc->req_pending = 0;
1200 __raw_writel(csr, creg);
1203 * SET_ADDRESS takes effect only after the STATUS
1204 * (to the original address) gets acked.
1206 if (udc->wait_for_addr_ack) {
1209 at91_udp_write(udc, AT91_UDP_FADDR,
1210 AT91_UDP_FEN | udc->addr);
1211 tmp = at91_udp_read(udc, AT91_UDP_GLB_STAT);
1212 tmp &= ~AT91_UDP_FADDEN;
1214 tmp |= AT91_UDP_FADDEN;
1215 at91_udp_write(udc, AT91_UDP_GLB_STAT, tmp);
1217 udc->wait_for_addr_ack = 0;
1218 VDBG("address %d\n", udc->addr);
1223 /* OUT packet arrived ... */
1224 else if (csr & AT91_UDP_RX_DATA_BK0) {
1226 csr &= ~(SET_FX | AT91_UDP_RX_DATA_BK0);
1228 /* OUT DATA stage */
1231 if (handle_ep(ep0)) {
1232 /* send IN/STATUS */
1233 PACKET("ep0 in/status\n");
1234 csr = __raw_readl(creg);
1236 csr |= CLR_FX | AT91_UDP_TXPKTRDY;
1237 __raw_writel(csr, creg);
1238 udc->req_pending = 0;
1240 } else if (udc->req_pending) {
1242 * AT91 hardware has a hard time with this
1243 * "deferred response" mode for control-OUT
1244 * transfers. (For control-IN it's fine.)
1246 * The normal solution leaves OUT data in the
1247 * fifo until the gadget driver is ready.
1248 * We couldn't do that here without disabling
1249 * the IRQ that tells about SETUP packets,
1250 * e.g. when the host gets impatient...
1252 * Working around it by copying into a buffer
1253 * would almost be a non-deferred response,
1254 * except that it wouldn't permit reliable
1255 * stalling of the request. Instead, demand
1256 * that gadget drivers not use this mode.
1258 DBG("no control-OUT deferred responses!\n");
1259 __raw_writel(csr | AT91_UDP_FORCESTALL, creg);
1260 udc->req_pending = 0;
1263 /* STATUS stage for control-IN; ack. */
1265 PACKET("ep0 out/status ACK\n");
1266 __raw_writel(csr, creg);
1268 /* "early" status stage */
1275 static void at91_udc_irq (int irq, void *_udc)
1277 struct at91_udc *udc = _udc;
1283 status = at91_udp_read(udc, AT91_UDP_ISR);
1287 /* USB reset irq: not maskable */
1288 if (status & AT91_UDP_ENDBUSRES) {
1289 at91_udp_write(udc, AT91_UDP_IDR, ~MINIMUS_INTERRUPTUS);
1290 /* Atmel code clears this irq twice */
1291 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1292 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_ENDBUSRES);
1293 VDBG("end bus reset\n");
1298 at91_udp_write(udc, AT91_UDP_CSR(0),
1299 AT91_UDP_EPEDS | AT91_UDP_EPTYPE_CTRL);
1300 udc->gadget.speed = USB_SPEED_FULL;
1304 * NOTE: this driver keeps clocks off unless the
1305 * USB host is present. That saves power, but for
1306 * boards that don't support VBUS detection, both
1307 * clocks need to be active most of the time.
1310 /* host initiated suspend (3+ms bus idle) */
1311 } else if (status & AT91_UDP_RXSUSP) {
1312 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXSUSP);
1313 /* at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXRSM); */
1314 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXSUSP);
1315 /* VDBG("bus suspend\n"); */
1321 * NOTE: when suspending a VBUS-powered device, the
1322 * gadget driver should switch into slow clock mode
1323 * and then into standby to avoid drawing more than
1324 * 500uA power (2500uA for some high-power configs).
1326 if (udc->driver && udc->driver->suspend)
1327 udc->driver->suspend(&udc->gadget);
1329 /* host initiated resume */
1330 } else if (status & AT91_UDP_RXRSM) {
1331 at91_udp_write(udc, AT91_UDP_IDR, AT91_UDP_RXRSM);
1332 /*at91_udp_write(udc, AT91_UDP_IER, AT91_UDP_RXSUSP);*/
1333 at91_udp_write(udc, AT91_UDP_ICR, AT91_UDP_RXRSM);
1334 /* VDBG("bus resume\n"); */
1335 if (!udc->suspended)
1340 * NOTE: for a VBUS-powered device, the gadget driver
1341 * would normally want to switch out of slow clock
1342 * mode into normal mode.
1344 if (udc->driver && udc->driver->resume)
1345 udc->driver->resume(&udc->gadget);
1347 /* endpoint IRQs are cleared by handling them */
1351 struct at91_ep *ep = &udc->ep[1];
1355 for (i = 1; i < NUM_ENDPOINTS; i++) {
1365 /*-------------------------------------------------------------------------*/
1367 static struct at91_udc controller = {
1368 .udp_baseaddr = (unsigned *) CFG_USBD_REGS_BASE,
1370 .ops = &at91_udc_ops,
1371 .ep0 = &controller.ep[0].ep,
1372 .name = driver_name,
1377 .ops = &at91_ep_ops,
1386 .ops = &at91_ep_ops,
1396 .ops = &at91_ep_ops,
1405 /* could actually do bulk too */
1407 .ops = &at91_ep_ops,
1416 .ops = &at91_ep_ops,
1426 .ops = &at91_ep_ops,
1433 /* ep6 and ep7 are also reserved (custom silicon might use them) */
1436 int usb_gadget_handle_interrupts(void)
1438 struct at91_udc *udc = &controller;
1441 value = at91_udp_read(udc, AT91_UDP_ISR) & (~(AT91_UDP_SOFINT));
1443 at91_udc_irq(0, udc);
1448 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1450 struct at91_udc *udc = &controller;
1455 || driver->speed < USB_SPEED_FULL
1457 || !driver->setup) {
1458 DBG("bad parameter.\n");
1463 DBG("UDC already has a gadget driver\n");
1467 udc->driver = driver;
1469 udc->selfpowered = 1;
1472 retval = driver->bind(&udc->gadget);
1474 DBG("driver->bind() returned %d\n", retval);
1477 udc->selfpowered = 0;
1481 /* todo: anyway allow registering the driver, maybe a later
1482 * bringup may succeed. postpone pullup the eth_init?
1486 value = at91_get_gpio_value(udc->board.vbus_pin);
1490 printf("no USB host connected...\n");
1496 DBG("bound to %s\n", driver->driver_name);
1500 int at91udc_probe(struct platform_data *pdata)
1502 struct at91_udc *udc;
1506 DBG("missing platform_data\n");
1510 /* init software state */
1512 //udc->gadget.dev.parent = dev;
1513 udc->board = pdata->board;
1520 /* get interface and function clocks */
1521 udc->iclk = pdata->udc_clk;
1523 DBG("clocks missing\n");
1528 /* don't do anything until we have both gadget driver and VBUS */
1529 clk_enable(udc->iclk);
1530 at91_udp_write(udc, AT91_UDP_TXVC, AT91_UDP_TXVC_TXVDIS);
1531 at91_udp_write(udc, AT91_UDP_IDR, 0xffffffff);
1532 /* Clear all pending interrupts - UDP may be used by bootloader. */
1533 at91_udp_write(udc, AT91_UDP_ICR, 0xffffffff);
1534 clk_disable(udc->iclk);
1536 if (udc->board.vbus_pin > 0) {
1538 * Get the initial state of VBUS - we cannot expect
1539 * a pending interrupt.
1541 udc->vbus = at91_get_gpio_value(udc->board.vbus_pin);
1542 DBG("VBUS detection: host:%s \n",
1543 udc->vbus ? "present":"absent");
1545 DBG("no VBUS detection, assuming always-on\n");
1551 printf("probe failed, %d\n", retval);