2 * driver/usb/gadget/imx_udc.c
4 * Copyright (C) 2005 Mike Lee(eemike@gmail.com)
5 * Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/platform_device.h>
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/list.h>
24 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/device.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/clk.h>
30 #include <linux/delay.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/gadget.h>
36 #include <mach/hardware.h>
40 static const char driver_name[] = "imx_udc";
41 static const char ep0name[] = "ep0";
43 void ep0_chg_stat(const char *label, struct imx_udc_struct *imx_usb,
46 /*******************************************************************************
47 * IMX UDC hardware related functions
48 *******************************************************************************
51 void imx_udc_enable(struct imx_udc_struct *imx_usb)
53 int temp = __raw_readl(imx_usb->base + USB_CTRL);
54 __raw_writel(temp | CTRL_FE_ENA | CTRL_AFE_ENA, imx_usb->base + USB_CTRL);
55 imx_usb->gadget.speed = USB_SPEED_FULL;
58 void imx_udc_disable(struct imx_udc_struct *imx_usb)
60 int temp = __raw_readl(imx_usb->base + USB_CTRL);
62 __raw_writel(temp & ~(CTRL_FE_ENA | CTRL_AFE_ENA),
63 imx_usb->base + USB_CTRL);
65 ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
66 imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
69 void imx_udc_reset(struct imx_udc_struct *imx_usb)
71 int temp = __raw_readl(imx_usb->base + USB_ENAB);
74 __raw_writel(temp | ENAB_RST, imx_usb->base + USB_ENAB);
76 /* wait RST bit to clear */
77 do {} while (__raw_readl(imx_usb->base + USB_ENAB) & ENAB_RST);
79 /* wait CFG bit to assert */
80 do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));
82 /* udc module is now ready */
85 void imx_udc_config(struct imx_udc_struct *imx_usb)
89 struct imx_ep_struct *imx_ep;
91 /* wait CFG bit to assert */
92 do {} while (!(__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG));
94 /* Download the endpoint buffer for endpoint 0. */
95 for (j = 0; j < 5; j++) {
96 i = (j == 2 ? imx_usb->imx_ep[0].fifosize : 0x00);
97 __raw_writeb(i, imx_usb->base + USB_DDAT);
98 do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_BSY);
101 /* Download the endpoint buffers for endpoints 1-5.
102 * We specify two configurations, one interface
104 for (cfg = 1; cfg < 3; cfg++) {
105 for (i = 1; i < IMX_USB_NB_EP; i++) {
106 imx_ep = &imx_usb->imx_ep[i];
107 /* EP no | Config no */
108 ep_conf[0] = (i << 4) | (cfg << 2);
109 /* Type | Direction */
110 ep_conf[1] = (imx_ep->bmAttributes << 3) |
111 (EP_DIR(imx_ep) << 2);
112 /* Max packet size */
113 ep_conf[2] = imx_ep->fifosize;
120 "<%s> ep%d_conf[%d]:"
121 "[%02x-%02x-%02x-%02x-%02x]\n",
123 ep_conf[0], ep_conf[1], ep_conf[2],
124 ep_conf[3], ep_conf[4]);
126 for (j = 0; j < 5; j++) {
127 __raw_writeb(ep_conf[j],
128 imx_usb->base + USB_DDAT);
129 do {} while (__raw_readl(imx_usb->base + USB_DADR)
135 /* wait CFG bit to clear */
136 do {} while (__raw_readl(imx_usb->base + USB_DADR) & DADR_CFG);
139 void imx_udc_init_irq(struct imx_udc_struct *imx_usb)
143 /* Mask and clear all irqs */
144 __raw_writel(0xFFFFFFFF, imx_usb->base + USB_MASK);
145 __raw_writel(0xFFFFFFFF, imx_usb->base + USB_INTR);
146 for (i = 0; i < IMX_USB_NB_EP; i++) {
147 __raw_writel(0x1FF, imx_usb->base + USB_EP_MASK(i));
148 __raw_writel(0x1FF, imx_usb->base + USB_EP_INTR(i));
151 /* Enable USB irqs */
152 __raw_writel(INTR_MSOF | INTR_FRAME_MATCH, imx_usb->base + USB_MASK);
154 /* Enable EP0 irqs */
155 __raw_writel(0x1FF & ~(EPINTR_DEVREQ | EPINTR_MDEVREQ | EPINTR_EOT
156 | EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL),
157 imx_usb->base + USB_EP_MASK(0));
160 void imx_udc_init_ep(struct imx_udc_struct *imx_usb)
163 struct imx_ep_struct *imx_ep;
164 for (i = 0; i < IMX_USB_NB_EP; i++) {
165 imx_ep = &imx_usb->imx_ep[i];
166 switch (imx_ep->fifosize) {
183 temp = (EP_DIR(imx_ep) << 7) | (max << 5)
184 | (imx_ep->bmAttributes << 3);
185 __raw_writel(temp, imx_usb->base + USB_EP_STAT(i));
186 __raw_writel(temp | EPSTAT_FLUSH, imx_usb->base + USB_EP_STAT(i));
187 D_INI(imx_usb->dev, "<%s> ep%d_stat %08x\n", __func__, i,
188 __raw_readl(imx_usb->base + USB_EP_STAT(i)));
192 void imx_udc_init_fifo(struct imx_udc_struct *imx_usb)
195 struct imx_ep_struct *imx_ep;
196 for (i = 0; i < IMX_USB_NB_EP; i++) {
197 imx_ep = &imx_usb->imx_ep[i];
200 temp = EP_DIR(imx_ep) ? 0x0B000000 : 0x0F000000;
201 __raw_writel(temp, imx_usb->base + USB_EP_FCTRL(i));
202 D_INI(imx_usb->dev, "<%s> ep%d_fctrl %08x\n", __func__, i,
203 __raw_readl(imx_usb->base + USB_EP_FCTRL(i)));
206 temp = (i ? imx_ep->fifosize / 2 : 0);
207 __raw_writel(temp, imx_usb->base + USB_EP_FALRM(i));
208 D_INI(imx_usb->dev, "<%s> ep%d_falrm %08x\n", __func__, i,
209 __raw_readl(imx_usb->base + USB_EP_FALRM(i)));
213 static void imx_udc_init(struct imx_udc_struct *imx_usb)
216 imx_udc_reset(imx_usb);
218 /* Download config to enpoint buffer */
219 imx_udc_config(imx_usb);
221 /* Setup interrups */
222 imx_udc_init_irq(imx_usb);
224 /* Setup endpoints */
225 imx_udc_init_ep(imx_usb);
228 imx_udc_init_fifo(imx_usb);
231 void imx_ep_irq_enable(struct imx_ep_struct *imx_ep)
234 int i = EP_NO(imx_ep);
236 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
237 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
238 __raw_writel(0x1FF & ~(EPINTR_EOT | EPINTR_EOF),
239 imx_ep->imx_usb->base + USB_EP_MASK(i));
242 void imx_ep_irq_disable(struct imx_ep_struct *imx_ep)
245 int i = EP_NO(imx_ep);
247 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_MASK(i));
248 __raw_writel(0x1FF, imx_ep->imx_usb->base + USB_EP_INTR(i));
251 int imx_ep_empty(struct imx_ep_struct *imx_ep)
253 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
255 return __raw_readl(imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
259 unsigned imx_fifo_bcount(struct imx_ep_struct *imx_ep)
261 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
263 return (__raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)))
264 & EPSTAT_BCOUNT) >> 16;
267 void imx_flush(struct imx_ep_struct *imx_ep)
269 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
271 int temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
272 __raw_writel(temp | EPSTAT_FLUSH,
273 imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
276 void imx_ep_stall(struct imx_ep_struct *imx_ep)
278 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
281 D_ERR(imx_usb->dev, "<%s> Forced stall on %s\n", __func__, imx_ep->ep.name);
285 /* Special care for ep0 */
286 if (!EP_NO(imx_ep)) {
287 temp = __raw_readl(imx_usb->base + USB_CTRL);
288 __raw_writel(temp | CTRL_CMDOVER | CTRL_CMDERROR, imx_usb->base + USB_CTRL);
289 do { } while (__raw_readl(imx_usb->base + USB_CTRL) & CTRL_CMDOVER);
290 temp = __raw_readl(imx_usb->base + USB_CTRL);
291 __raw_writel(temp & ~CTRL_CMDERROR, imx_usb->base + USB_CTRL);
294 temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
295 __raw_writel(temp | EPSTAT_STALL,
296 imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
298 for (i = 0; i < 100; i ++) {
299 temp = __raw_readl(imx_usb->base + USB_EP_STAT(EP_NO(imx_ep)));
300 if (!(temp & EPSTAT_STALL))
305 D_ERR(imx_usb->dev, "<%s> Non finished stall on %s\n",
306 __func__, imx_ep->ep.name);
310 static int imx_udc_get_frame(struct usb_gadget *_gadget)
312 struct imx_udc_struct *imx_usb = container_of(_gadget,
313 struct imx_udc_struct, gadget);
315 return __raw_readl(imx_usb->base + USB_FRAME) & 0x7FF;
318 static int imx_udc_wakeup(struct usb_gadget *_gadget)
323 /*******************************************************************************
324 * USB request control functions
325 *******************************************************************************
328 static void ep_add_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
334 list_add_tail(&req->queue, &imx_ep->queue);
337 static void ep_del_request(struct imx_ep_struct *imx_ep, struct imx_request *req)
342 list_del_init(&req->queue);
346 static void done(struct imx_ep_struct *imx_ep, struct imx_request *req, int status)
348 ep_del_request(imx_ep, req);
350 if (likely(req->req.status == -EINPROGRESS))
351 req->req.status = status;
353 status = req->req.status;
355 if (status && status != -ESHUTDOWN)
356 D_ERR(imx_ep->imx_usb->dev,
357 "<%s> complete %s req %p stat %d len %u/%u\n", __func__,
358 imx_ep->ep.name, &req->req, status,
359 req->req.actual, req->req.length);
361 req->req.complete(&imx_ep->ep, &req->req);
364 static void nuke(struct imx_ep_struct *imx_ep, int status)
366 struct imx_request *req;
368 while (!list_empty(&imx_ep->queue)) {
369 req = list_entry(imx_ep->queue.next, struct imx_request, queue);
370 done(imx_ep, req, status);
374 /*******************************************************************************
375 * Data tansfer over USB functions
376 *******************************************************************************
378 static int read_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
381 int bytes_ep, bufferspace, count, i;
383 bytes_ep = imx_fifo_bcount(imx_ep);
384 bufferspace = req->req.length - req->req.actual;
386 buf = req->req.buf + req->req.actual;
389 if (unlikely(imx_ep_empty(imx_ep)))
392 count = min(bytes_ep, bufferspace);
394 for (i = count; i > 0; i--)
395 *buf++ = __raw_readb(imx_ep->imx_usb->base
396 + USB_EP_FDAT0(EP_NO(imx_ep)));
397 req->req.actual += count;
402 static int write_packet(struct imx_ep_struct *imx_ep, struct imx_request *req)
405 int length, count, temp;
407 buf = req->req.buf + req->req.actual;
410 length = min(req->req.length - req->req.actual, (u32)imx_ep->fifosize);
412 if (imx_fifo_bcount(imx_ep) + length > imx_ep->fifosize) {
413 D_TRX(imx_ep->imx_usb->dev, "<%s> packet overfill %s fifo\n",
414 __func__, imx_ep->ep.name);
418 req->req.actual += length;
421 if (!count && req->req.zero) { /* zlp */
422 temp = __raw_readl(imx_ep->imx_usb->base
423 + USB_EP_STAT(EP_NO(imx_ep)));
424 __raw_writel(temp | EPSTAT_ZLPS, imx_ep->imx_usb->base
425 + USB_EP_STAT(EP_NO(imx_ep)));
426 D_TRX(imx_ep->imx_usb->dev, "<%s> zero packet\n", __func__);
431 if (count == 0) { /* last byte */
432 temp = __raw_readl(imx_ep->imx_usb->base
433 + USB_EP_FCTRL(EP_NO(imx_ep)));
434 __raw_writel(temp | FCTRL_WFR, imx_ep->imx_usb->base
435 + USB_EP_FCTRL(EP_NO(imx_ep)));
438 imx_ep->imx_usb->base + USB_EP_FDAT0(EP_NO(imx_ep)));
444 static int read_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
450 while (__raw_readl(imx_ep->imx_usb->base + USB_EP_FSTAT(EP_NO(imx_ep)))
452 count = read_packet(imx_ep, req);
455 completed = (count != imx_ep->fifosize);
456 if (completed || req->req.actual == req->req.length) {
462 if (completed || !req->req.length) {
463 done(imx_ep, req, 0);
464 D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
465 __func__, imx_ep->ep.name, req,
466 completed ? "completed" : "not completed");
468 ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
471 D_TRX(imx_ep->imx_usb->dev, "<%s> bytes read: %d\n", __func__, bytes);
476 static int write_fifo(struct imx_ep_struct *imx_ep, struct imx_request *req)
483 count = write_packet(imx_ep, req);
488 /* last packet "must be" short (or a zlp) */
489 completed = (count != imx_ep->fifosize);
491 if (unlikely(completed)) {
492 done(imx_ep, req, 0);
493 D_REQ(imx_ep->imx_usb->dev, "<%s> %s req<%p> %s\n",
494 __func__, imx_ep->ep.name, req,
495 completed ? "completed" : "not completed");
497 ep0_chg_stat(__func__, imx_ep->imx_usb, EP0_IDLE);
501 D_TRX(imx_ep->imx_usb->dev, "<%s> bytes sent: %d\n", __func__, bytes);
506 /*******************************************************************************
508 *******************************************************************************
510 static int handle_ep(struct imx_ep_struct *imx_ep)
512 struct imx_request *req;
516 if (!list_empty(&imx_ep->queue))
517 req = list_entry(imx_ep->queue.next,
518 struct imx_request, queue);
520 D_REQ(imx_ep->imx_usb->dev, "<%s> no request on %s\n",
521 __func__, imx_ep->ep.name);
525 if (EP_DIR(imx_ep)) /* to host */
526 completed = write_fifo(imx_ep, req);
528 completed = read_fifo(imx_ep, req);
530 dump_ep_stat(__func__, imx_ep);
537 static int handle_ep0(struct imx_ep_struct *imx_ep)
539 struct imx_request *req = NULL;
542 if (!list_empty(&imx_ep->queue)) {
543 req = list_entry(imx_ep->queue.next, struct imx_request, queue);
545 switch (imx_ep->imx_usb->ep0state) {
547 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
548 write_fifo(imx_ep, req);
550 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
551 read_fifo(imx_ep, req);
554 D_EP0(imx_ep->imx_usb->dev,
555 "<%s> ep0 i/o, odd state %d\n",
556 __func__, imx_ep->imx_usb->ep0state);
557 ep_del_request(imx_ep, req);
564 D_ERR(imx_ep->imx_usb->dev, "<%s> no request on %s\n",
565 __func__, imx_ep->ep.name);
570 static void handle_ep0_devreq(struct imx_udc_struct *imx_usb)
572 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0];
574 struct usb_ctrlrequest r;
580 nuke(imx_ep, -EPROTO);
582 /* read SETUP packet */
583 for (i = 0; i < 2; i++) {
584 if (imx_ep_empty(imx_ep)) {
586 "<%s> no setup packet received\n", __func__);
589 u.word[i] = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
592 temp = imx_ep_empty(imx_ep);
593 while (!imx_ep_empty(imx_ep)) {
594 i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
596 "<%s> wrong to have extra bytes for setup : 0x%08x\n",
602 le16_to_cpus(&u.r.wValue);
603 le16_to_cpus(&u.r.wIndex);
604 le16_to_cpus(&u.r.wLength);
606 D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
607 __func__, u.r.bRequestType, u.r.bRequest,
608 u.r.wValue, u.r.wIndex, u.r.wLength);
610 if (imx_usb->set_config) {
611 /* NACK the host by using CMDOVER */
612 temp = __raw_readl(imx_usb->base + USB_CTRL);
613 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
616 "<%s> set config req is pending, NACK the host\n",
621 if (u.r.bRequestType & USB_DIR_IN)
622 ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE);
624 ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE);
626 i = imx_usb->driver->setup(&imx_usb->gadget, &u.r);
628 D_ERR(imx_usb->dev, "<%s> device setup error %d\n",
635 D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__);
636 imx_ep_stall(imx_ep);
637 ep0_chg_stat(__func__, imx_usb, EP0_STALL);
641 /*******************************************************************************
642 * USB gadget callback functions
643 *******************************************************************************
646 static int imx_ep_enable(struct usb_ep *usb_ep,
647 const struct usb_endpoint_descriptor *desc)
649 struct imx_ep_struct *imx_ep = container_of(usb_ep,
650 struct imx_ep_struct, ep);
651 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
657 || desc->bDescriptorType != USB_DT_ENDPOINT
658 || imx_ep->bEndpointAddress != desc->bEndpointAddress) {
660 "<%s> bad ep or descriptor\n", __func__);
664 if (imx_ep->bmAttributes != desc->bmAttributes) {
666 "<%s> %s type mismatch\n", __func__, usb_ep->name);
670 if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) {
672 "<%s> bad %s maxpacket\n", __func__, usb_ep->name);
676 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
677 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
681 local_irq_save(flags);
685 imx_ep_irq_enable(imx_ep);
687 local_irq_restore(flags);
689 D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name);
693 static int imx_ep_disable(struct usb_ep *usb_ep)
695 struct imx_ep_struct *imx_ep = container_of(usb_ep,
696 struct imx_ep_struct, ep);
699 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
700 D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n",
701 __func__, usb_ep ? imx_ep->ep.name : NULL);
705 local_irq_save(flags);
708 nuke(imx_ep, -ESHUTDOWN);
710 imx_ep_irq_disable(imx_ep);
712 local_irq_restore(flags);
714 D_EPX(imx_ep->imx_usb->dev,
715 "<%s> DISABLED %s\n", __func__, usb_ep->name);
719 static struct usb_request *imx_ep_alloc_request
720 (struct usb_ep *usb_ep, gfp_t gfp_flags)
722 struct imx_request *req;
724 req = kzalloc(sizeof *req, gfp_flags);
728 INIT_LIST_HEAD(&req->queue);
734 static void imx_ep_free_request
735 (struct usb_ep *usb_ep, struct usb_request *usb_req)
737 struct imx_request *req;
739 req = container_of(usb_req, struct imx_request, req);
740 WARN_ON(!list_empty(&req->queue));
744 static int imx_ep_queue
745 (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags)
747 struct imx_ep_struct *imx_ep;
748 struct imx_udc_struct *imx_usb;
749 struct imx_request *req;
753 imx_ep = container_of(usb_ep, struct imx_ep_struct, ep);
754 imx_usb = imx_ep->imx_usb;
755 req = container_of(usb_req, struct imx_request, req);
758 Special care on IMX udc.
759 Ignore enqueue when after set configuration from the
760 host. This assume all gadget drivers reply set
761 configuration with the next ep0 req enqueue.
763 if (imx_usb->set_config && !EP_NO(imx_ep)) {
764 imx_usb->set_config = 0;
766 "<%s> gadget reply set config\n", __func__);
770 if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) {
771 D_ERR(imx_usb->dev, "<%s> bad params\n", __func__);
775 if (unlikely(!usb_ep || !imx_ep)) {
776 D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__);
780 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
781 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
786 D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n",
787 __func__, EP_NO(imx_ep),
788 ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
789 || (EP_NO(imx_ep) && EP_DIR(imx_ep))) ? "IN" : "OUT", usb_req->length);
790 dump_req(__func__, imx_ep, usb_req);
792 if (imx_ep->stopped) {
793 usb_req->status = -ESHUTDOWN;
799 "<%s> refusing to queue req %p (already queued)\n",
804 local_irq_save(flags);
806 usb_req->status = -EINPROGRESS;
809 ep_add_request(imx_ep, req);
812 ret = handle_ep0(imx_ep);
814 ret = handle_ep(imx_ep);
816 local_irq_restore(flags);
820 static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
823 struct imx_ep_struct *imx_ep = container_of
824 (usb_ep, struct imx_ep_struct, ep);
825 struct imx_request *req;
828 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
829 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
833 local_irq_save(flags);
835 /* make sure it's actually queued on this endpoint */
836 list_for_each_entry(req, &imx_ep->queue, queue) {
837 if (&req->req == usb_req)
840 if (&req->req != usb_req) {
841 local_irq_restore(flags);
845 done(imx_ep, req, -ECONNRESET);
847 local_irq_restore(flags);
851 static int imx_ep_set_halt(struct usb_ep *usb_ep, int value)
853 struct imx_ep_struct *imx_ep = container_of
854 (usb_ep, struct imx_ep_struct, ep);
857 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
858 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
862 local_irq_save(flags);
864 if ((imx_ep->bEndpointAddress & USB_DIR_IN)
865 && !list_empty(&imx_ep->queue)) {
866 local_irq_restore(flags);
870 imx_ep_stall(imx_ep);
872 local_irq_restore(flags);
874 D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
878 static int imx_ep_fifo_status(struct usb_ep *usb_ep)
880 struct imx_ep_struct *imx_ep = container_of
881 (usb_ep, struct imx_ep_struct, ep);
884 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
888 if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
891 return imx_fifo_bcount(imx_ep);
894 static void imx_ep_fifo_flush(struct usb_ep *usb_ep)
896 struct imx_ep_struct *imx_ep = container_of
897 (usb_ep, struct imx_ep_struct, ep);
900 local_irq_save(flags);
902 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
903 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
904 local_irq_restore(flags);
908 /* toggle and halt bits stay unchanged */
911 local_irq_restore(flags);
914 static struct usb_ep_ops imx_ep_ops = {
915 .enable = imx_ep_enable,
916 .disable = imx_ep_disable,
918 .alloc_request = imx_ep_alloc_request,
919 .free_request = imx_ep_free_request,
921 .queue = imx_ep_queue,
922 .dequeue = imx_ep_dequeue,
924 .set_halt = imx_ep_set_halt,
925 .fifo_status = imx_ep_fifo_status,
926 .fifo_flush = imx_ep_fifo_flush,
929 /*******************************************************************************
930 * USB endpoint control functions
931 *******************************************************************************
934 void ep0_chg_stat(const char *label,
935 struct imx_udc_struct *imx_usb, enum ep0_state stat)
937 D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n",
938 label, state_name[imx_usb->ep0state], state_name[stat]);
940 if (imx_usb->ep0state == stat)
943 imx_usb->ep0state = stat;
946 static void usb_init_data(struct imx_udc_struct *imx_usb)
948 struct imx_ep_struct *imx_ep;
951 /* device/ep0 records init */
952 INIT_LIST_HEAD(&imx_usb->gadget.ep_list);
953 INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list);
954 ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
956 /* basic endpoint records init */
957 for (i = 0; i < IMX_USB_NB_EP; i++) {
958 imx_ep = &imx_usb->imx_ep[i];
961 list_add_tail(&imx_ep->ep.ep_list,
962 &imx_usb->gadget.ep_list);
967 INIT_LIST_HEAD(&imx_ep->queue);
971 static void udc_stop_activity(struct imx_udc_struct *imx_usb,
972 struct usb_gadget_driver *driver)
974 struct imx_ep_struct *imx_ep;
977 if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
980 /* prevent new request submissions, kill any outstanding requests */
981 for (i = 1; i < IMX_USB_NB_EP; i++) {
982 imx_ep = &imx_usb->imx_ep[i];
985 imx_ep_irq_disable(imx_ep);
986 nuke(imx_ep, -ESHUTDOWN);
994 driver->disconnect(&imx_usb->gadget);
997 /*******************************************************************************
999 *******************************************************************************
1002 static irqreturn_t imx_udc_irq(int irq, void *dev)
1004 struct imx_udc_struct *imx_usb = dev;
1005 struct usb_ctrlrequest u;
1006 int temp, cfg, intf, alt;
1007 int intr = __raw_readl(imx_usb->base + USB_INTR);
1009 if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START
1010 | INTR_RESET_STOP | INTR_CFG_CHG)) {
1011 dump_intr(__func__, intr, imx_usb->dev);
1012 dump_usb_stat(__func__, imx_usb);
1015 if (!imx_usb->driver)
1018 if (intr & INTR_WAKEUP) {
1019 if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN
1020 && imx_usb->driver && imx_usb->driver->resume)
1021 imx_usb->driver->resume(&imx_usb->gadget);
1022 imx_usb->set_config = 0;
1023 imx_usb->gadget.speed = USB_SPEED_FULL;
1026 if (intr & INTR_SUSPEND) {
1027 if (imx_usb->gadget.speed != USB_SPEED_UNKNOWN
1028 && imx_usb->driver && imx_usb->driver->suspend)
1029 imx_usb->driver->suspend(&imx_usb->gadget);
1030 imx_usb->set_config = 0;
1031 imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
1034 if (intr & INTR_RESET_START) {
1035 __raw_writel(intr, imx_usb->base + USB_INTR);
1036 udc_stop_activity(imx_usb, imx_usb->driver);
1037 imx_usb->set_config = 0;
1038 imx_usb->gadget.speed = USB_SPEED_UNKNOWN;
1041 if (intr & INTR_RESET_STOP)
1042 imx_usb->gadget.speed = USB_SPEED_FULL;
1044 if (intr & INTR_CFG_CHG) {
1045 __raw_writel(INTR_CFG_CHG, imx_usb->base + USB_INTR);
1046 temp = __raw_readl(imx_usb->base + USB_STAT);
1047 cfg = (temp & STAT_CFG) >> 5;
1048 intf = (temp & STAT_INTF) >> 3;
1049 alt = temp & STAT_ALTSET;
1052 "<%s> orig config C=%d, I=%d, A=%d / "
1053 "req config C=%d, I=%d, A=%d\n",
1054 __func__, imx_usb->cfg, imx_usb->intf, imx_usb->alt,
1057 if (cfg != 1 && cfg != 2)
1060 imx_usb->set_config = 0;
1063 if (imx_usb->cfg != cfg) {
1064 D_REQ(imx_usb->dev, "<%s> Change config start\n",__func__);
1065 u.bRequest = USB_REQ_SET_CONFIGURATION;
1066 u.bRequestType = USB_DIR_OUT |
1073 imx_usb->set_config = 1;
1074 imx_usb->driver->setup(&imx_usb->gadget, &u);
1075 imx_usb->set_config = 0;
1076 D_REQ(imx_usb->dev, "<%s> Change config done\n",__func__);
1079 if (imx_usb->intf != intf || imx_usb->alt != alt) {
1080 D_REQ(imx_usb->dev, "<%s> Change interface start\n",__func__);
1081 u.bRequest = USB_REQ_SET_INTERFACE;
1082 u.bRequestType = USB_DIR_OUT |
1084 USB_RECIP_INTERFACE;
1088 imx_usb->intf = intf;
1090 imx_usb->set_config = 1;
1091 imx_usb->driver->setup(&imx_usb->gadget, &u);
1092 imx_usb->set_config = 0;
1093 D_REQ(imx_usb->dev, "<%s> Change interface done\n",__func__);
1097 if (intr & INTR_SOF) {
1098 /* Copy from Freescale BSP.
1099 We must enable SOF intr and set CMDOVER.
1100 Datasheet don't specifiy this action, but it
1101 is done in Freescale BSP, so just copy it.
1103 if (imx_usb->ep0state == EP0_IDLE) {
1104 temp = __raw_readl(imx_usb->base + USB_CTRL);
1105 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
1110 __raw_writel(intr, imx_usb->base + USB_INTR);
1114 static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
1116 struct imx_udc_struct *imx_usb = dev;
1117 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0));
1119 dump_ep_intr(__func__, 0, intr, imx_usb->dev);
1121 if (!imx_usb->driver) {
1122 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1126 /* DEVREQ IRQ has highest priority */
1127 if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ))
1128 handle_ep0_devreq(imx_usb);
1129 /* Seem i.MX is missing EOF interrupt sometimes.
1130 * Therefore we monitor both EOF and FIFO_EMPTY interrups
1131 * when transmiting, and both EOF and FIFO_FULL when
1134 else if (intr & (EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL))
1135 handle_ep0(&imx_usb->imx_ep[0]);
1137 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1142 static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
1144 struct imx_udc_struct *imx_usb = dev;
1145 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0];
1146 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1148 dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev);
1150 if (!imx_usb->driver) {
1151 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1157 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1162 irq_handler_t intr_handler(int i)
1166 return imx_udc_ctrl_irq;
1172 return imx_udc_bulk_irq;
1178 /*******************************************************************************
1179 * Static defined IMX UDC structure
1180 *******************************************************************************
1183 static const struct usb_gadget_ops imx_udc_ops = {
1184 .get_frame = imx_udc_get_frame,
1185 .wakeup = imx_udc_wakeup,
1188 static struct imx_udc_struct controller = {
1190 .ops = &imx_udc_ops,
1191 .ep0 = &controller.imx_ep[0].ep,
1192 .name = driver_name,
1204 .imx_usb = &controller,
1206 .bEndpointAddress = 0,
1207 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1211 .name = "ep1in-bulk",
1215 .imx_usb = &controller,
1217 .bEndpointAddress = USB_DIR_IN | 1,
1218 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1222 .name = "ep2out-bulk",
1226 .imx_usb = &controller,
1228 .bEndpointAddress = USB_DIR_OUT | 2,
1229 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1233 .name = "ep3out-bulk",
1237 .imx_usb = &controller,
1239 .bEndpointAddress = USB_DIR_OUT | 3,
1240 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1244 .name = "ep4in-int",
1248 .imx_usb = &controller,
1250 .bEndpointAddress = USB_DIR_IN | 4,
1251 .bmAttributes = USB_ENDPOINT_XFER_INT,
1255 .name = "ep5out-int",
1259 .imx_usb = &controller,
1261 .bEndpointAddress = USB_DIR_OUT | 5,
1262 .bmAttributes = USB_ENDPOINT_XFER_INT,
1266 /*******************************************************************************
1267 * USB gadged driver functions
1268 *******************************************************************************
1270 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1272 struct imx_udc_struct *imx_usb = &controller;
1276 || driver->speed < USB_SPEED_FULL
1278 || !driver->disconnect
1283 if (imx_usb->driver)
1286 /* first hook up the driver ... */
1287 imx_usb->driver = driver;
1288 imx_usb->gadget.dev.driver = &driver->driver;
1290 retval = device_add(&imx_usb->gadget.dev);
1293 retval = driver->bind(&imx_usb->gadget);
1295 D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
1296 __func__, driver->driver.name, retval);
1297 device_del(&imx_usb->gadget.dev);
1302 D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
1303 __func__, driver->driver.name);
1305 imx_udc_enable(imx_usb);
1309 imx_usb->driver = NULL;
1310 imx_usb->gadget.dev.driver = NULL;
1313 EXPORT_SYMBOL(usb_gadget_register_driver);
1315 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1317 struct imx_udc_struct *imx_usb = &controller;
1321 if (!driver || driver != imx_usb->driver || !driver->unbind)
1324 udc_stop_activity(imx_usb, driver);
1325 imx_udc_disable(imx_usb);
1327 driver->unbind(&imx_usb->gadget);
1328 imx_usb->gadget.dev.driver = NULL;
1329 imx_usb->driver = NULL;
1331 device_del(&imx_usb->gadget.dev);
1333 D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
1334 __func__, driver->driver.name);
1338 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1340 /*******************************************************************************
1342 *******************************************************************************
1345 static int __init imx_udc_probe(struct platform_device *pdev)
1347 struct imx_udc_struct *imx_usb = &controller;
1348 struct resource *res;
1349 struct imxusb_platform_data *pdata;
1355 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1357 dev_err(&pdev->dev, "can't get device resources\n");
1361 pdata = pdev->dev.platform_data;
1363 dev_err(&pdev->dev, "driver needs platform data\n");
1367 res_size = res->end - res->start + 1;
1368 if (!request_mem_region(res->start, res_size, res->name)) {
1369 dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
1370 res_size, res->start);
1375 ret = pdata->init(&pdev->dev);
1380 base = ioremap(res->start, res_size);
1382 dev_err(&pdev->dev, "ioremap failed\n");
1387 clk = clk_get(NULL, "usbd_clk");
1390 dev_err(&pdev->dev, "can't get USB clock\n");
1395 if (clk_get_rate(clk) != 48000000) {
1397 "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
1398 (int)clk_get_rate(clk));
1399 if (clk_set_rate(clk, 48000000)) {
1401 "Unable to set correct USB clock (48MHz)\n");
1407 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1408 imx_usb->usbd_int[i] = platform_get_irq(pdev, i);
1409 if (imx_usb->usbd_int[i] < 0) {
1410 dev_err(&pdev->dev, "can't get irq number\n");
1416 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1417 ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
1418 IRQF_DISABLED, driver_name, imx_usb);
1420 dev_err(&pdev->dev, "can't get irq %i, err %d\n",
1421 imx_usb->usbd_int[i], ret);
1422 for (--i; i >= 0; i--)
1423 free_irq(imx_usb->usbd_int[i], imx_usb);
1429 imx_usb->base = base;
1431 imx_usb->dev = &pdev->dev;
1433 device_initialize(&imx_usb->gadget.dev);
1435 imx_usb->gadget.dev.parent = &pdev->dev;
1436 imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
1438 platform_set_drvdata(pdev, imx_usb);
1440 usb_init_data(imx_usb);
1441 imx_udc_init(imx_usb);
1452 pdata->exit(&pdev->dev);
1454 release_mem_region(res->start, res_size);
1458 static int __exit imx_udc_remove(struct platform_device *pdev)
1460 struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev);
1461 struct imxusb_platform_data *pdata = pdev->dev.platform_data;
1464 imx_udc_disable(imx_usb);
1466 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1467 free_irq(imx_usb->usbd_int[i], imx_usb);
1469 clk_put(imx_usb->clk);
1470 clk_disable(imx_usb->clk);
1471 iounmap(imx_usb->base);
1473 release_mem_region(imx_usb->res->start,
1474 imx_usb->res->end - imx_usb->res->start + 1);
1477 pdata->exit(&pdev->dev);
1479 platform_set_drvdata(pdev, NULL);
1484 /*----------------------------------------------------------------------------*/
1487 #define imx_udc_suspend NULL
1488 #define imx_udc_resume NULL
1490 #define imx_udc_suspend NULL
1491 #define imx_udc_resume NULL
1494 /*----------------------------------------------------------------------------*/
1496 static struct platform_driver udc_driver = {
1498 .name = driver_name,
1499 .owner = THIS_MODULE,
1501 .remove = __exit_p(imx_udc_remove),
1502 .suspend = imx_udc_suspend,
1503 .resume = imx_udc_resume,
1506 static int __init udc_init(void)
1508 return platform_driver_probe(&udc_driver, imx_udc_probe);
1510 module_init(udc_init);
1512 static void __exit udc_exit(void)
1514 platform_driver_unregister(&udc_driver);
1516 module_exit(udc_exit);
1518 MODULE_DESCRIPTION("IMX USB Device Controller driver");
1519 MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
1520 MODULE_LICENSE("GPL");
1521 MODULE_ALIAS("platform:imx_udc");