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 */
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);
546 switch (imx_ep->imx_usb->ep0state) {
548 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR */
549 write_fifo(imx_ep, req);
551 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR */
552 read_fifo(imx_ep, req);
555 D_EP0(imx_ep->imx_usb->dev,
556 "<%s> ep0 i/o, odd state %d\n",
557 __func__, imx_ep->imx_usb->ep0state);
558 ep_del_request(imx_ep, req);
567 static void handle_ep0_devreq(struct imx_udc_struct *imx_usb)
569 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[0];
571 struct usb_ctrlrequest r;
577 nuke(imx_ep, -EPROTO);
579 /* read SETUP packet */
580 for (i = 0; i < 2; i++) {
581 if (imx_ep_empty(imx_ep)) {
583 "<%s> no setup packet received\n", __func__);
586 u.word[i] = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
589 temp = imx_ep_empty(imx_ep);
590 while (!imx_ep_empty(imx_ep)) {
591 i = __raw_readl(imx_usb->base + USB_EP_FDAT(EP_NO(imx_ep)));
593 "<%s> wrong to have extra bytes for setup : 0x%08x\n",
599 le16_to_cpus(&u.r.wValue);
600 le16_to_cpus(&u.r.wIndex);
601 le16_to_cpus(&u.r.wLength);
603 D_REQ(imx_usb->dev, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
604 __func__, u.r.bRequestType, u.r.bRequest,
605 u.r.wValue, u.r.wIndex, u.r.wLength);
607 if (imx_usb->set_config) {
608 /* NACK the host by using CMDOVER */
609 temp = __raw_readl(imx_usb->base + USB_CTRL);
610 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
613 "<%s> set config req is pending, NACK the host\n",
618 if (u.r.bRequestType & USB_DIR_IN)
619 ep0_chg_stat(__func__, imx_usb, EP0_IN_DATA_PHASE);
621 ep0_chg_stat(__func__, imx_usb, EP0_OUT_DATA_PHASE);
623 i = imx_usb->driver->setup(&imx_usb->gadget, &u.r);
625 D_ERR(imx_usb->dev, "<%s> device setup error %d\n",
632 D_ERR(imx_usb->dev, "<%s> protocol STALL\n", __func__);
633 imx_ep_stall(imx_ep);
634 ep0_chg_stat(__func__, imx_usb, EP0_STALL);
638 /*******************************************************************************
639 * USB gadget callback functions
640 *******************************************************************************
643 static int imx_ep_enable(struct usb_ep *usb_ep,
644 const struct usb_endpoint_descriptor *desc)
646 struct imx_ep_struct *imx_ep = container_of(usb_ep,
647 struct imx_ep_struct, ep);
648 struct imx_udc_struct *imx_usb = imx_ep->imx_usb;
654 || desc->bDescriptorType != USB_DT_ENDPOINT
655 || imx_ep->bEndpointAddress != desc->bEndpointAddress) {
657 "<%s> bad ep or descriptor\n", __func__);
661 if (imx_ep->bmAttributes != desc->bmAttributes) {
663 "<%s> %s type mismatch\n", __func__, usb_ep->name);
667 if (imx_ep->fifosize < le16_to_cpu(desc->wMaxPacketSize)) {
669 "<%s> bad %s maxpacket\n", __func__, usb_ep->name);
673 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
674 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
678 local_irq_save(flags);
682 imx_ep_irq_enable(imx_ep);
684 local_irq_restore(flags);
686 D_EPX(imx_usb->dev, "<%s> ENABLED %s\n", __func__, usb_ep->name);
690 static int imx_ep_disable(struct usb_ep *usb_ep)
692 struct imx_ep_struct *imx_ep = container_of(usb_ep,
693 struct imx_ep_struct, ep);
696 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
697 D_ERR(imx_ep->imx_usb->dev, "<%s> %s can not be disabled\n",
698 __func__, usb_ep ? imx_ep->ep.name : NULL);
702 local_irq_save(flags);
705 nuke(imx_ep, -ESHUTDOWN);
707 imx_ep_irq_disable(imx_ep);
709 local_irq_restore(flags);
711 D_EPX(imx_ep->imx_usb->dev,
712 "<%s> DISABLED %s\n", __func__, usb_ep->name);
716 static struct usb_request *imx_ep_alloc_request
717 (struct usb_ep *usb_ep, gfp_t gfp_flags)
719 struct imx_request *req;
721 req = kzalloc(sizeof *req, gfp_flags);
725 INIT_LIST_HEAD(&req->queue);
731 static void imx_ep_free_request
732 (struct usb_ep *usb_ep, struct usb_request *usb_req)
734 struct imx_request *req;
736 req = container_of(usb_req, struct imx_request, req);
737 WARN_ON(!list_empty(&req->queue));
741 static int imx_ep_queue
742 (struct usb_ep *usb_ep, struct usb_request *usb_req, gfp_t gfp_flags)
744 struct imx_ep_struct *imx_ep;
745 struct imx_udc_struct *imx_usb;
746 struct imx_request *req;
750 imx_ep = container_of(usb_ep, struct imx_ep_struct, ep);
751 imx_usb = imx_ep->imx_usb;
752 req = container_of(usb_req, struct imx_request, req);
755 Special care on IMX udc.
756 Ignore enqueue when after set configuration from the
757 host. This assume all gadget drivers reply set
758 configuration with the next ep0 req enqueue.
760 if (imx_usb->set_config && !EP_NO(imx_ep)) {
761 imx_usb->set_config = 0;
763 "<%s> gadget reply set config\n", __func__);
767 if (unlikely(!usb_req || !req || !usb_req->complete || !usb_req->buf)) {
768 D_ERR(imx_usb->dev, "<%s> bad params\n", __func__);
772 if (unlikely(!usb_ep || !imx_ep)) {
773 D_ERR(imx_usb->dev, "<%s> bad ep\n", __func__);
777 if (!imx_usb->driver || imx_usb->gadget.speed == USB_SPEED_UNKNOWN) {
778 D_ERR(imx_usb->dev, "<%s> bogus device state\n", __func__);
782 local_irq_save(flags);
785 D_REQ(imx_usb->dev, "<%s> ep%d %s request for [%d] bytes\n",
786 __func__, EP_NO(imx_ep),
787 ((!EP_NO(imx_ep) && imx_ep->imx_usb->ep0state == EP0_IN_DATA_PHASE)
788 || (EP_NO(imx_ep) && EP_DIR(imx_ep))) ? "IN" : "OUT", usb_req->length);
789 dump_req(__func__, imx_ep, usb_req);
791 if (imx_ep->stopped) {
792 usb_req->status = -ESHUTDOWN;
799 "<%s> refusing to queue req %p (already queued)\n",
804 usb_req->status = -EINPROGRESS;
807 ep_add_request(imx_ep, req);
810 ret = handle_ep0(imx_ep);
812 ret = handle_ep(imx_ep);
814 local_irq_restore(flags);
818 static int imx_ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
821 struct imx_ep_struct *imx_ep = container_of
822 (usb_ep, struct imx_ep_struct, ep);
823 struct imx_request *req;
826 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
827 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
831 local_irq_save(flags);
833 /* make sure it's actually queued on this endpoint */
834 list_for_each_entry(req, &imx_ep->queue, queue) {
835 if (&req->req == usb_req)
838 if (&req->req != usb_req) {
839 local_irq_restore(flags);
843 done(imx_ep, req, -ECONNRESET);
845 local_irq_restore(flags);
849 static int imx_ep_set_halt(struct usb_ep *usb_ep, int value)
851 struct imx_ep_struct *imx_ep = container_of
852 (usb_ep, struct imx_ep_struct, ep);
855 if (unlikely(!usb_ep || !EP_NO(imx_ep))) {
856 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
860 local_irq_save(flags);
862 if ((imx_ep->bEndpointAddress & USB_DIR_IN)
863 && !list_empty(&imx_ep->queue)) {
864 local_irq_restore(flags);
868 imx_ep_stall(imx_ep);
870 local_irq_restore(flags);
872 D_EPX(imx_ep->imx_usb->dev, "<%s> %s halt\n", __func__, usb_ep->name);
876 static int imx_ep_fifo_status(struct usb_ep *usb_ep)
878 struct imx_ep_struct *imx_ep = container_of
879 (usb_ep, struct imx_ep_struct, ep);
882 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
886 if (imx_ep->imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
889 return imx_fifo_bcount(imx_ep);
892 static void imx_ep_fifo_flush(struct usb_ep *usb_ep)
894 struct imx_ep_struct *imx_ep = container_of
895 (usb_ep, struct imx_ep_struct, ep);
898 local_irq_save(flags);
900 if (!usb_ep || !EP_NO(imx_ep) || !list_empty(&imx_ep->queue)) {
901 D_ERR(imx_ep->imx_usb->dev, "<%s> bad ep\n", __func__);
902 local_irq_restore(flags);
906 /* toggle and halt bits stay unchanged */
909 local_irq_restore(flags);
912 static struct usb_ep_ops imx_ep_ops = {
913 .enable = imx_ep_enable,
914 .disable = imx_ep_disable,
916 .alloc_request = imx_ep_alloc_request,
917 .free_request = imx_ep_free_request,
919 .queue = imx_ep_queue,
920 .dequeue = imx_ep_dequeue,
922 .set_halt = imx_ep_set_halt,
923 .fifo_status = imx_ep_fifo_status,
924 .fifo_flush = imx_ep_fifo_flush,
927 /*******************************************************************************
928 * USB endpoint control functions
929 *******************************************************************************
932 void ep0_chg_stat(const char *label,
933 struct imx_udc_struct *imx_usb, enum ep0_state stat)
935 D_EP0(imx_usb->dev, "<%s> from %15s to %15s\n",
936 label, state_name[imx_usb->ep0state], state_name[stat]);
938 if (imx_usb->ep0state == stat)
941 imx_usb->ep0state = stat;
944 static void usb_init_data(struct imx_udc_struct *imx_usb)
946 struct imx_ep_struct *imx_ep;
949 /* device/ep0 records init */
950 INIT_LIST_HEAD(&imx_usb->gadget.ep_list);
951 INIT_LIST_HEAD(&imx_usb->gadget.ep0->ep_list);
952 ep0_chg_stat(__func__, imx_usb, EP0_IDLE);
954 /* basic endpoint records init */
955 for (i = 0; i < IMX_USB_NB_EP; i++) {
956 imx_ep = &imx_usb->imx_ep[i];
959 list_add_tail(&imx_ep->ep.ep_list,
960 &imx_usb->gadget.ep_list);
965 INIT_LIST_HEAD(&imx_ep->queue);
969 static void udc_stop_activity(struct imx_udc_struct *imx_usb,
970 struct usb_gadget_driver *driver)
972 struct imx_ep_struct *imx_ep;
975 if (imx_usb->gadget.speed == USB_SPEED_UNKNOWN)
978 /* prevent new request submissions, kill any outstanding requests */
979 for (i = 1; i < IMX_USB_NB_EP; i++) {
980 imx_ep = &imx_usb->imx_ep[i];
983 imx_ep_irq_disable(imx_ep);
984 nuke(imx_ep, -ESHUTDOWN);
992 driver->disconnect(&imx_usb->gadget);
995 /*******************************************************************************
997 *******************************************************************************
1000 static irqreturn_t imx_udc_irq(int irq, void *dev)
1002 struct imx_udc_struct *imx_usb = dev;
1003 struct usb_ctrlrequest u;
1004 int temp, cfg, intf, alt;
1005 int intr = __raw_readl(imx_usb->base + USB_INTR);
1007 if (intr & (INTR_WAKEUP | INTR_SUSPEND | INTR_RESUME | INTR_RESET_START
1008 | INTR_RESET_STOP | INTR_CFG_CHG)) {
1009 dump_intr(__func__, intr, imx_usb->dev);
1010 dump_usb_stat(__func__, imx_usb);
1013 if (!imx_usb->driver) {
1014 /*imx_udc_disable(imx_usb);*/
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 if (imx_usb->ep0state == EP0_IDLE) {
1099 temp = __raw_readl(imx_usb->base + USB_CTRL);
1100 __raw_writel(temp | CTRL_CMDOVER, imx_usb->base + USB_CTRL);
1105 __raw_writel(intr, imx_usb->base + USB_INTR);
1109 static irqreturn_t imx_udc_ctrl_irq(int irq, void *dev)
1111 struct imx_udc_struct *imx_usb = dev;
1112 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(0));
1114 dump_ep_intr(__func__, 0, intr, imx_usb->dev);
1116 if (!imx_usb->driver) {
1117 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1121 /* DEVREQ IRQ has highest priority */
1122 if (intr & (EPINTR_DEVREQ | EPINTR_MDEVREQ))
1123 handle_ep0_devreq(imx_usb);
1124 /* Seem i.MX is missing EOF interrupt sometimes.
1125 * Therefore we monitor both EOF and FIFO_EMPTY interrups
1126 * when transmiting, and both EOF and FIFO_FULL when
1129 else if (intr & (EPINTR_EOF | EPINTR_FIFO_EMPTY | EPINTR_FIFO_FULL))
1130 handle_ep0(&imx_usb->imx_ep[0]);
1132 __raw_writel(intr, imx_usb->base + USB_EP_INTR(0));
1137 static irqreturn_t imx_udc_bulk_irq(int irq, void *dev)
1139 struct imx_udc_struct *imx_usb = dev;
1140 struct imx_ep_struct *imx_ep = &imx_usb->imx_ep[irq - USBD_INT0];
1141 int intr = __raw_readl(imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1143 dump_ep_intr(__func__, irq - USBD_INT0, intr, imx_usb->dev);
1145 if (!imx_usb->driver) {
1146 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1152 __raw_writel(intr, imx_usb->base + USB_EP_INTR(EP_NO(imx_ep)));
1157 irq_handler_t intr_handler(int i)
1161 return imx_udc_ctrl_irq;
1167 return imx_udc_bulk_irq;
1173 /*******************************************************************************
1174 * Static defined IMX UDC structure
1175 *******************************************************************************
1178 static const struct usb_gadget_ops imx_udc_ops = {
1179 .get_frame = imx_udc_get_frame,
1180 .wakeup = imx_udc_wakeup,
1183 static struct imx_udc_struct controller = {
1185 .ops = &imx_udc_ops,
1186 .ep0 = &controller.imx_ep[0].ep,
1187 .name = driver_name,
1199 .imx_usb = &controller,
1201 .bEndpointAddress = 0,
1202 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1206 .name = "ep1in-bulk",
1210 .imx_usb = &controller,
1212 .bEndpointAddress = USB_DIR_IN | 1,
1213 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1217 .name = "ep2out-bulk",
1221 .imx_usb = &controller,
1223 .bEndpointAddress = USB_DIR_OUT | 2,
1224 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1228 .name = "ep3out-bulk",
1232 .imx_usb = &controller,
1234 .bEndpointAddress = USB_DIR_OUT | 3,
1235 .bmAttributes = USB_ENDPOINT_XFER_BULK,
1239 .name = "ep4in-int",
1243 .imx_usb = &controller,
1245 .bEndpointAddress = USB_DIR_IN | 4,
1246 .bmAttributes = USB_ENDPOINT_XFER_INT,
1250 .name = "ep5out-int",
1254 .imx_usb = &controller,
1256 .bEndpointAddress = USB_DIR_OUT | 5,
1257 .bmAttributes = USB_ENDPOINT_XFER_INT,
1261 /*******************************************************************************
1262 * USB gadged driver functions
1263 *******************************************************************************
1265 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
1267 struct imx_udc_struct *imx_usb = &controller;
1271 || driver->speed < USB_SPEED_FULL
1273 || !driver->disconnect
1278 if (imx_usb->driver)
1281 /* first hook up the driver ... */
1282 imx_usb->driver = driver;
1283 imx_usb->gadget.dev.driver = &driver->driver;
1285 retval = device_add(&imx_usb->gadget.dev);
1288 retval = driver->bind(&imx_usb->gadget);
1290 D_ERR(imx_usb->dev, "<%s> bind to driver %s --> error %d\n",
1291 __func__, driver->driver.name, retval);
1292 device_del(&imx_usb->gadget.dev);
1297 D_INI(imx_usb->dev, "<%s> registered gadget driver '%s'\n",
1298 __func__, driver->driver.name);
1300 imx_udc_enable(imx_usb);
1304 imx_usb->driver = NULL;
1305 imx_usb->gadget.dev.driver = NULL;
1308 EXPORT_SYMBOL(usb_gadget_register_driver);
1310 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
1312 struct imx_udc_struct *imx_usb = &controller;
1316 if (!driver || driver != imx_usb->driver || !driver->unbind)
1319 udc_stop_activity(imx_usb, driver);
1320 imx_udc_disable(imx_usb);
1322 driver->unbind(&imx_usb->gadget);
1323 imx_usb->gadget.dev.driver = NULL;
1324 imx_usb->driver = NULL;
1326 device_del(&imx_usb->gadget.dev);
1328 D_INI(imx_usb->dev, "<%s> unregistered gadget driver '%s'\n",
1329 __func__, driver->driver.name);
1333 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1335 /*******************************************************************************
1337 *******************************************************************************
1340 static int __init imx_udc_probe(struct platform_device *pdev)
1342 struct imx_udc_struct *imx_usb = &controller;
1343 struct resource *res;
1344 struct imxusb_platform_data *pdata;
1350 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1352 dev_err(&pdev->dev, "can't get device resources\n");
1356 pdata = pdev->dev.platform_data;
1358 dev_err(&pdev->dev, "driver needs platform data\n");
1362 res_size = res->end - res->start + 1;
1363 if (!request_mem_region(res->start, res_size, res->name)) {
1364 dev_err(&pdev->dev, "can't allocate %d bytes at %d address\n",
1365 res_size, res->start);
1370 ret = pdata->init(&pdev->dev);
1375 base = ioremap(res->start, res_size);
1377 dev_err(&pdev->dev, "ioremap failed\n");
1382 clk = clk_get(NULL, "usbd_clk");
1385 dev_err(&pdev->dev, "can't get USB clock\n");
1390 if (clk_get_rate(clk) != 48000000) {
1392 "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
1393 (int)clk_get_rate(clk));
1394 if (clk_set_rate(clk, 48000000)) {
1396 "Unable to set correct USB clock (48MHz)\n");
1402 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1403 imx_usb->usbd_int[i] = platform_get_irq(pdev, i);
1404 if (imx_usb->usbd_int[i] < 0) {
1405 dev_err(&pdev->dev, "can't get irq number\n");
1411 for (i = 0; i < IMX_USB_NB_EP + 1; i++) {
1412 ret = request_irq(imx_usb->usbd_int[i], intr_handler(i),
1413 IRQF_DISABLED, driver_name, imx_usb);
1415 dev_err(&pdev->dev, "can't get irq %i, err %d\n",
1416 imx_usb->usbd_int[i], ret);
1417 for (--i; i >= 0; i--)
1418 free_irq(imx_usb->usbd_int[i], imx_usb);
1424 imx_usb->base = base;
1426 imx_usb->dev = &pdev->dev;
1428 device_initialize(&imx_usb->gadget.dev);
1430 imx_usb->gadget.dev.parent = &pdev->dev;
1431 imx_usb->gadget.dev.dma_mask = pdev->dev.dma_mask;
1433 platform_set_drvdata(pdev, imx_usb);
1435 usb_init_data(imx_usb);
1436 imx_udc_init(imx_usb);
1447 pdata->exit(&pdev->dev);
1449 release_mem_region(res->start, res_size);
1453 static int __exit imx_udc_remove(struct platform_device *pdev)
1455 struct imx_udc_struct *imx_usb = platform_get_drvdata(pdev);
1456 struct imxusb_platform_data *pdata = pdev->dev.platform_data;
1459 imx_udc_disable(imx_usb);
1461 for (i = 0; i < IMX_USB_NB_EP + 1; i++)
1462 free_irq(imx_usb->usbd_int[i], imx_usb);
1464 clk_put(imx_usb->clk);
1465 clk_disable(imx_usb->clk);
1466 iounmap(imx_usb->base);
1468 release_mem_region(imx_usb->res->start,
1469 imx_usb->res->end - imx_usb->res->start + 1);
1472 pdata->exit(&pdev->dev);
1474 platform_set_drvdata(pdev, NULL);
1479 /*----------------------------------------------------------------------------*/
1482 #define imx_udc_suspend NULL
1483 #define imx_udc_resume NULL
1485 #define imx_udc_suspend NULL
1486 #define imx_udc_resume NULL
1489 /*----------------------------------------------------------------------------*/
1491 static struct platform_driver udc_driver = {
1493 .name = driver_name,
1494 .owner = THIS_MODULE,
1496 .remove = __exit_p(imx_udc_remove),
1497 .suspend = imx_udc_suspend,
1498 .resume = imx_udc_resume,
1501 static int __init udc_init(void)
1503 return platform_driver_probe(&udc_driver, imx_udc_probe);
1505 module_init(udc_init);
1507 static void __exit udc_exit(void)
1509 platform_driver_unregister(&udc_driver);
1511 module_exit(udc_exit);
1513 MODULE_DESCRIPTION("IMX USB Device Controller driver");
1514 MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
1515 MODULE_LICENSE("GPL");
1516 MODULE_ALIAS("platform:imx_udc");