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>
31 #include <linux/timer.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
37 #include <mach/hardware.h>
41 static const char driver_name
[] = "imx_udc";
42 static const char ep0name
[] = "ep0";
44 void ep0_chg_stat(const char *label
, struct imx_udc_struct
*imx_usb
,
47 /*******************************************************************************
48 * IMX UDC hardware related functions
49 *******************************************************************************
52 void imx_udc_enable(struct imx_udc_struct
*imx_usb
)
54 int temp
= __raw_readl(imx_usb
->base
+ USB_CTRL
);
55 __raw_writel(temp
| CTRL_FE_ENA
| CTRL_AFE_ENA
,
56 imx_usb
->base
+ USB_CTRL
);
57 imx_usb
->gadget
.speed
= USB_SPEED_FULL
;
60 void imx_udc_disable(struct imx_udc_struct
*imx_usb
)
62 int temp
= __raw_readl(imx_usb
->base
+ USB_CTRL
);
64 __raw_writel(temp
& ~(CTRL_FE_ENA
| CTRL_AFE_ENA
),
65 imx_usb
->base
+ USB_CTRL
);
67 ep0_chg_stat(__func__
, imx_usb
, EP0_IDLE
);
68 imx_usb
->gadget
.speed
= USB_SPEED_UNKNOWN
;
71 void imx_udc_reset(struct imx_udc_struct
*imx_usb
)
73 int temp
= __raw_readl(imx_usb
->base
+ USB_ENAB
);
76 __raw_writel(temp
| ENAB_RST
, imx_usb
->base
+ USB_ENAB
);
78 /* wait RST bit to clear */
79 do {} while (__raw_readl(imx_usb
->base
+ USB_ENAB
) & ENAB_RST
);
81 /* wait CFG bit to assert */
82 do {} while (!(__raw_readl(imx_usb
->base
+ USB_DADR
) & DADR_CFG
));
84 /* udc module is now ready */
87 void imx_udc_config(struct imx_udc_struct
*imx_usb
)
91 struct imx_ep_struct
*imx_ep
;
93 /* wait CFG bit to assert */
94 do {} while (!(__raw_readl(imx_usb
->base
+ USB_DADR
) & DADR_CFG
));
96 /* Download the endpoint buffer for endpoint 0. */
97 for (j
= 0; j
< 5; j
++) {
98 i
= (j
== 2 ? imx_usb
->imx_ep
[0].fifosize
: 0x00);
99 __raw_writeb(i
, imx_usb
->base
+ USB_DDAT
);
100 do {} while (__raw_readl(imx_usb
->base
+ USB_DADR
) & DADR_BSY
);
103 /* Download the endpoint buffers for endpoints 1-5.
104 * We specify two configurations, one interface
106 for (cfg
= 1; cfg
< 3; cfg
++) {
107 for (i
= 1; i
< IMX_USB_NB_EP
; i
++) {
108 imx_ep
= &imx_usb
->imx_ep
[i
];
109 /* EP no | Config no */
110 ep_conf
[0] = (i
<< 4) | (cfg
<< 2);
111 /* Type | Direction */
112 ep_conf
[1] = (imx_ep
->bmAttributes
<< 3) |
113 (EP_DIR(imx_ep
) << 2);
114 /* Max packet size */
115 ep_conf
[2] = imx_ep
->fifosize
;
122 "<%s> ep%d_conf[%d]:"
123 "[%02x-%02x-%02x-%02x-%02x]\n",
125 ep_conf
[0], ep_conf
[1], ep_conf
[2],
126 ep_conf
[3], ep_conf
[4]);
128 for (j
= 0; j
< 5; j
++) {
129 __raw_writeb(ep_conf
[j
],
130 imx_usb
->base
+ USB_DDAT
);
131 do {} while (__raw_readl(imx_usb
->base
138 /* wait CFG bit to clear */
139 do {} while (__raw_readl(imx_usb
->base
+ USB_DADR
) & DADR_CFG
);
142 void imx_udc_init_irq(struct imx_udc_struct
*imx_usb
)
146 /* Mask and clear all irqs */
147 __raw_writel(0xFFFFFFFF, imx_usb
->base
+ USB_MASK
);
148 __raw_writel(0xFFFFFFFF, imx_usb
->base
+ USB_INTR
);
149 for (i
= 0; i
< IMX_USB_NB_EP
; i
++) {
150 __raw_writel(0x1FF, imx_usb
->base
+ USB_EP_MASK(i
));
151 __raw_writel(0x1FF, imx_usb
->base
+ USB_EP_INTR(i
));
154 /* Enable USB irqs */
155 __raw_writel(INTR_MSOF
| INTR_FRAME_MATCH
, imx_usb
->base
+ USB_MASK
);
157 /* Enable EP0 irqs */
158 __raw_writel(0x1FF & ~(EPINTR_DEVREQ
| EPINTR_MDEVREQ
| EPINTR_EOT
159 | EPINTR_EOF
| EPINTR_FIFO_EMPTY
| EPINTR_FIFO_FULL
),
160 imx_usb
->base
+ USB_EP_MASK(0));
163 void imx_udc_init_ep(struct imx_udc_struct
*imx_usb
)
166 struct imx_ep_struct
*imx_ep
;
167 for (i
= 0; i
< IMX_USB_NB_EP
; i
++) {
168 imx_ep
= &imx_usb
->imx_ep
[i
];
169 switch (imx_ep
->fifosize
) {
186 temp
= (EP_DIR(imx_ep
) << 7) | (max
<< 5)
187 | (imx_ep
->bmAttributes
<< 3);
188 __raw_writel(temp
, imx_usb
->base
+ USB_EP_STAT(i
));
189 __raw_writel(temp
| EPSTAT_FLUSH
,
190 imx_usb
->base
+ USB_EP_STAT(i
));
191 D_INI(imx_usb
->dev
, "<%s> ep%d_stat %08x\n", __func__
, i
,
192 __raw_readl(imx_usb
->base
+ USB_EP_STAT(i
)));
196 void imx_udc_init_fifo(struct imx_udc_struct
*imx_usb
)
199 struct imx_ep_struct
*imx_ep
;
200 for (i
= 0; i
< IMX_USB_NB_EP
; i
++) {
201 imx_ep
= &imx_usb
->imx_ep
[i
];
204 temp
= EP_DIR(imx_ep
) ? 0x0B000000 : 0x0F000000;
205 __raw_writel(temp
, imx_usb
->base
+ USB_EP_FCTRL(i
));
206 D_INI(imx_usb
->dev
, "<%s> ep%d_fctrl %08x\n", __func__
, i
,
207 __raw_readl(imx_usb
->base
+ USB_EP_FCTRL(i
)));
210 temp
= (i
? imx_ep
->fifosize
/ 2 : 0);
211 __raw_writel(temp
, imx_usb
->base
+ USB_EP_FALRM(i
));
212 D_INI(imx_usb
->dev
, "<%s> ep%d_falrm %08x\n", __func__
, i
,
213 __raw_readl(imx_usb
->base
+ USB_EP_FALRM(i
)));
217 static void imx_udc_init(struct imx_udc_struct
*imx_usb
)
220 imx_udc_reset(imx_usb
);
222 /* Download config to enpoint buffer */
223 imx_udc_config(imx_usb
);
225 /* Setup interrups */
226 imx_udc_init_irq(imx_usb
);
228 /* Setup endpoints */
229 imx_udc_init_ep(imx_usb
);
232 imx_udc_init_fifo(imx_usb
);
235 void imx_ep_irq_enable(struct imx_ep_struct
*imx_ep
)
238 int i
= EP_NO(imx_ep
);
240 __raw_writel(0x1FF, imx_ep
->imx_usb
->base
+ USB_EP_MASK(i
));
241 __raw_writel(0x1FF, imx_ep
->imx_usb
->base
+ USB_EP_INTR(i
));
242 __raw_writel(0x1FF & ~(EPINTR_EOT
| EPINTR_EOF
),
243 imx_ep
->imx_usb
->base
+ USB_EP_MASK(i
));
246 void imx_ep_irq_disable(struct imx_ep_struct
*imx_ep
)
249 int i
= EP_NO(imx_ep
);
251 __raw_writel(0x1FF, imx_ep
->imx_usb
->base
+ USB_EP_MASK(i
));
252 __raw_writel(0x1FF, imx_ep
->imx_usb
->base
+ USB_EP_INTR(i
));
255 int imx_ep_empty(struct imx_ep_struct
*imx_ep
)
257 struct imx_udc_struct
*imx_usb
= imx_ep
->imx_usb
;
259 return __raw_readl(imx_usb
->base
+ USB_EP_FSTAT(EP_NO(imx_ep
)))
263 unsigned imx_fifo_bcount(struct imx_ep_struct
*imx_ep
)
265 struct imx_udc_struct
*imx_usb
= imx_ep
->imx_usb
;
267 return (__raw_readl(imx_usb
->base
+ USB_EP_STAT(EP_NO(imx_ep
)))
268 & EPSTAT_BCOUNT
) >> 16;
271 void imx_flush(struct imx_ep_struct
*imx_ep
)
273 struct imx_udc_struct
*imx_usb
= imx_ep
->imx_usb
;
275 int temp
= __raw_readl(imx_usb
->base
+ USB_EP_STAT(EP_NO(imx_ep
)));
276 __raw_writel(temp
| EPSTAT_FLUSH
,
277 imx_usb
->base
+ USB_EP_STAT(EP_NO(imx_ep
)));
280 void imx_ep_stall(struct imx_ep_struct
*imx_ep
)
282 struct imx_udc_struct
*imx_usb
= imx_ep
->imx_usb
;
286 "<%s> Forced stall on %s\n", __func__
, imx_ep
->ep
.name
);
290 /* Special care for ep0 */
291 if (!EP_NO(imx_ep
)) {
292 temp
= __raw_readl(imx_usb
->base
+ USB_CTRL
);
293 __raw_writel(temp
| CTRL_CMDOVER
| CTRL_CMDERROR
,
294 imx_usb
->base
+ USB_CTRL
);
295 do { } while (__raw_readl(imx_usb
->base
+ USB_CTRL
)
297 temp
= __raw_readl(imx_usb
->base
+ USB_CTRL
);
298 __raw_writel(temp
& ~CTRL_CMDERROR
, imx_usb
->base
+ USB_CTRL
);
301 temp
= __raw_readl(imx_usb
->base
+ USB_EP_STAT(EP_NO(imx_ep
)));
302 __raw_writel(temp
| EPSTAT_STALL
,
303 imx_usb
->base
+ USB_EP_STAT(EP_NO(imx_ep
)));
305 for (i
= 0; i
< 100; i
++) {
306 temp
= __raw_readl(imx_usb
->base
307 + USB_EP_STAT(EP_NO(imx_ep
)));
308 if (!(temp
& EPSTAT_STALL
))
313 D_ERR(imx_usb
->dev
, "<%s> Non finished stall on %s\n",
314 __func__
, imx_ep
->ep
.name
);
318 static int imx_udc_get_frame(struct usb_gadget
*_gadget
)
320 struct imx_udc_struct
*imx_usb
= container_of(_gadget
,
321 struct imx_udc_struct
, gadget
);
323 return __raw_readl(imx_usb
->base
+ USB_FRAME
) & 0x7FF;
326 static int imx_udc_wakeup(struct usb_gadget
*_gadget
)
331 /*******************************************************************************
332 * USB request control functions
333 *******************************************************************************
336 static void ep_add_request(struct imx_ep_struct
*imx_ep
,
337 struct imx_request
*req
)
343 list_add_tail(&req
->queue
, &imx_ep
->queue
);
346 static void ep_del_request(struct imx_ep_struct
*imx_ep
,
347 struct imx_request
*req
)
352 list_del_init(&req
->queue
);
356 static void done(struct imx_ep_struct
*imx_ep
,
357 struct imx_request
*req
, int status
)
359 ep_del_request(imx_ep
, req
);
361 if (likely(req
->req
.status
== -EINPROGRESS
))
362 req
->req
.status
= status
;
364 status
= req
->req
.status
;
366 if (status
&& status
!= -ESHUTDOWN
)
367 D_ERR(imx_ep
->imx_usb
->dev
,
368 "<%s> complete %s req %p stat %d len %u/%u\n", __func__
,
369 imx_ep
->ep
.name
, &req
->req
, status
,
370 req
->req
.actual
, req
->req
.length
);
372 req
->req
.complete(&imx_ep
->ep
, &req
->req
);
375 static void nuke(struct imx_ep_struct
*imx_ep
, int status
)
377 struct imx_request
*req
;
379 while (!list_empty(&imx_ep
->queue
)) {
380 req
= list_entry(imx_ep
->queue
.next
, struct imx_request
, queue
);
381 done(imx_ep
, req
, status
);
385 /*******************************************************************************
386 * Data tansfer over USB functions
387 *******************************************************************************
389 static int read_packet(struct imx_ep_struct
*imx_ep
, struct imx_request
*req
)
392 int bytes_ep
, bufferspace
, count
, i
;
394 bytes_ep
= imx_fifo_bcount(imx_ep
);
395 bufferspace
= req
->req
.length
- req
->req
.actual
;
397 buf
= req
->req
.buf
+ req
->req
.actual
;
400 if (unlikely(imx_ep_empty(imx_ep
)))
403 count
= min(bytes_ep
, bufferspace
);
405 for (i
= count
; i
> 0; i
--)
406 *buf
++ = __raw_readb(imx_ep
->imx_usb
->base
407 + USB_EP_FDAT0(EP_NO(imx_ep
)));
408 req
->req
.actual
+= count
;
413 static int write_packet(struct imx_ep_struct
*imx_ep
, struct imx_request
*req
)
416 int length
, count
, temp
;
418 if (unlikely(__raw_readl(imx_ep
->imx_usb
->base
+
419 USB_EP_STAT(EP_NO(imx_ep
))) & EPSTAT_ZLPS
)) {
420 D_TRX(imx_ep
->imx_usb
->dev
, "<%s> zlp still queued in EP %s\n",
421 __func__
, imx_ep
->ep
.name
);
425 buf
= req
->req
.buf
+ req
->req
.actual
;
428 length
= min(req
->req
.length
- req
->req
.actual
, (u32
)imx_ep
->fifosize
);
430 if (imx_fifo_bcount(imx_ep
) + length
> imx_ep
->fifosize
) {
431 D_TRX(imx_ep
->imx_usb
->dev
, "<%s> packet overfill %s fifo\n",
432 __func__
, imx_ep
->ep
.name
);
436 req
->req
.actual
+= length
;
439 if (!count
&& req
->req
.zero
) { /* zlp */
440 temp
= __raw_readl(imx_ep
->imx_usb
->base
441 + USB_EP_STAT(EP_NO(imx_ep
)));
442 __raw_writel(temp
| EPSTAT_ZLPS
, imx_ep
->imx_usb
->base
443 + USB_EP_STAT(EP_NO(imx_ep
)));
444 D_TRX(imx_ep
->imx_usb
->dev
, "<%s> zero packet\n", __func__
);
449 if (count
== 0) { /* last byte */
450 temp
= __raw_readl(imx_ep
->imx_usb
->base
451 + USB_EP_FCTRL(EP_NO(imx_ep
)));
452 __raw_writel(temp
| FCTRL_WFR
, imx_ep
->imx_usb
->base
453 + USB_EP_FCTRL(EP_NO(imx_ep
)));
456 imx_ep
->imx_usb
->base
+ USB_EP_FDAT0(EP_NO(imx_ep
)));
462 static int read_fifo(struct imx_ep_struct
*imx_ep
, struct imx_request
*req
)
468 while (__raw_readl(imx_ep
->imx_usb
->base
+ USB_EP_FSTAT(EP_NO(imx_ep
)))
470 count
= read_packet(imx_ep
, req
);
473 completed
= (count
!= imx_ep
->fifosize
);
474 if (completed
|| req
->req
.actual
== req
->req
.length
) {
480 if (completed
|| !req
->req
.length
) {
481 done(imx_ep
, req
, 0);
482 D_REQ(imx_ep
->imx_usb
->dev
, "<%s> %s req<%p> %s\n",
483 __func__
, imx_ep
->ep
.name
, req
,
484 completed
? "completed" : "not completed");
486 ep0_chg_stat(__func__
, imx_ep
->imx_usb
, EP0_IDLE
);
489 D_TRX(imx_ep
->imx_usb
->dev
, "<%s> bytes read: %d\n", __func__
, bytes
);
494 static int write_fifo(struct imx_ep_struct
*imx_ep
, struct imx_request
*req
)
501 count
= write_packet(imx_ep
, req
);
506 /* last packet "must be" short (or a zlp) */
507 completed
= (count
!= imx_ep
->fifosize
);
509 if (unlikely(completed
)) {
510 done(imx_ep
, req
, 0);
511 D_REQ(imx_ep
->imx_usb
->dev
, "<%s> %s req<%p> %s\n",
512 __func__
, imx_ep
->ep
.name
, req
,
513 completed
? "completed" : "not completed");
515 ep0_chg_stat(__func__
,
516 imx_ep
->imx_usb
, EP0_IDLE
);
520 D_TRX(imx_ep
->imx_usb
->dev
, "<%s> bytes sent: %d\n", __func__
, bytes
);
525 /*******************************************************************************
527 *******************************************************************************
529 static int handle_ep(struct imx_ep_struct
*imx_ep
)
531 struct imx_request
*req
;
535 if (!list_empty(&imx_ep
->queue
))
536 req
= list_entry(imx_ep
->queue
.next
,
537 struct imx_request
, queue
);
539 D_REQ(imx_ep
->imx_usb
->dev
, "<%s> no request on %s\n",
540 __func__
, imx_ep
->ep
.name
);
544 if (EP_DIR(imx_ep
)) /* to host */
545 completed
= write_fifo(imx_ep
, req
);
547 completed
= read_fifo(imx_ep
, req
);
549 dump_ep_stat(__func__
, imx_ep
);
556 static int handle_ep0(struct imx_ep_struct
*imx_ep
)
558 struct imx_request
*req
= NULL
;
561 if (!list_empty(&imx_ep
->queue
)) {
562 req
= list_entry(imx_ep
->queue
.next
, struct imx_request
, queue
);
564 switch (imx_ep
->imx_usb
->ep0state
) {
566 case EP0_IN_DATA_PHASE
: /* GET_DESCRIPTOR */
567 write_fifo(imx_ep
, req
);
569 case EP0_OUT_DATA_PHASE
: /* SET_DESCRIPTOR */
570 read_fifo(imx_ep
, req
);
573 D_EP0(imx_ep
->imx_usb
->dev
,
574 "<%s> ep0 i/o, odd state %d\n",
575 __func__
, imx_ep
->imx_usb
->ep0state
);
576 ep_del_request(imx_ep
, req
);
583 D_ERR(imx_ep
->imx_usb
->dev
, "<%s> no request on %s\n",
584 __func__
, imx_ep
->ep
.name
);
589 static void handle_ep0_devreq(struct imx_udc_struct
*imx_usb
)
591 struct imx_ep_struct
*imx_ep
= &imx_usb
->imx_ep
[0];
593 struct usb_ctrlrequest r
;
599 nuke(imx_ep
, -EPROTO
);
601 /* read SETUP packet */
602 for (i
= 0; i
< 2; i
++) {
603 if (imx_ep_empty(imx_ep
)) {
605 "<%s> no setup packet received\n", __func__
);
608 u
.word
[i
] = __raw_readl(imx_usb
->base
609 + USB_EP_FDAT(EP_NO(imx_ep
)));
612 temp
= imx_ep_empty(imx_ep
);
613 while (!imx_ep_empty(imx_ep
)) {
614 i
= __raw_readl(imx_usb
->base
+ USB_EP_FDAT(EP_NO(imx_ep
)));
616 "<%s> wrong to have extra bytes for setup : 0x%08x\n",
622 le16_to_cpus(&u
.r
.wValue
);
623 le16_to_cpus(&u
.r
.wIndex
);
624 le16_to_cpus(&u
.r
.wLength
);
626 D_REQ(imx_usb
->dev
, "<%s> SETUP %02x.%02x v%04x i%04x l%04x\n",
627 __func__
, u
.r
.bRequestType
, u
.r
.bRequest
,
628 u
.r
.wValue
, u
.r
.wIndex
, u
.r
.wLength
);
630 if (imx_usb
->set_config
) {
631 /* NACK the host by using CMDOVER */
632 temp
= __raw_readl(imx_usb
->base
+ USB_CTRL
);
633 __raw_writel(temp
| CTRL_CMDOVER
, imx_usb
->base
+ USB_CTRL
);
636 "<%s> set config req is pending, NACK the host\n",
641 if (u
.r
.bRequestType
& USB_DIR_IN
)
642 ep0_chg_stat(__func__
, imx_usb
, EP0_IN_DATA_PHASE
);
644 ep0_chg_stat(__func__
, imx_usb
, EP0_OUT_DATA_PHASE
);
646 i
= imx_usb
->driver
->setup(&imx_usb
->gadget
, &u
.r
);
648 D_ERR(imx_usb
->dev
, "<%s> device setup error %d\n",
655 D_ERR(imx_usb
->dev
, "<%s> protocol STALL\n", __func__
);
656 imx_ep_stall(imx_ep
);
657 ep0_chg_stat(__func__
, imx_usb
, EP0_STALL
);
661 /*******************************************************************************
662 * USB gadget callback functions
663 *******************************************************************************
666 static int imx_ep_enable(struct usb_ep
*usb_ep
,
667 const struct usb_endpoint_descriptor
*desc
)
669 struct imx_ep_struct
*imx_ep
= container_of(usb_ep
,
670 struct imx_ep_struct
, ep
);
671 struct imx_udc_struct
*imx_usb
= imx_ep
->imx_usb
;
677 || desc
->bDescriptorType
!= USB_DT_ENDPOINT
678 || imx_ep
->bEndpointAddress
!= desc
->bEndpointAddress
) {
680 "<%s> bad ep or descriptor\n", __func__
);
684 if (imx_ep
->bmAttributes
!= desc
->bmAttributes
) {
686 "<%s> %s type mismatch\n", __func__
, usb_ep
->name
);
690 if (imx_ep
->fifosize
< le16_to_cpu(desc
->wMaxPacketSize
)) {
692 "<%s> bad %s maxpacket\n", __func__
, usb_ep
->name
);
696 if (!imx_usb
->driver
|| imx_usb
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
697 D_ERR(imx_usb
->dev
, "<%s> bogus device state\n", __func__
);
701 local_irq_save(flags
);
705 imx_ep_irq_enable(imx_ep
);
707 local_irq_restore(flags
);
709 D_EPX(imx_usb
->dev
, "<%s> ENABLED %s\n", __func__
, usb_ep
->name
);
713 static int imx_ep_disable(struct usb_ep
*usb_ep
)
715 struct imx_ep_struct
*imx_ep
= container_of(usb_ep
,
716 struct imx_ep_struct
, ep
);
719 if (!usb_ep
|| !EP_NO(imx_ep
) || !list_empty(&imx_ep
->queue
)) {
720 D_ERR(imx_ep
->imx_usb
->dev
, "<%s> %s can not be disabled\n",
721 __func__
, usb_ep
? imx_ep
->ep
.name
: NULL
);
725 local_irq_save(flags
);
728 nuke(imx_ep
, -ESHUTDOWN
);
730 imx_ep_irq_disable(imx_ep
);
732 local_irq_restore(flags
);
734 D_EPX(imx_ep
->imx_usb
->dev
,
735 "<%s> DISABLED %s\n", __func__
, usb_ep
->name
);
739 static struct usb_request
*imx_ep_alloc_request
740 (struct usb_ep
*usb_ep
, gfp_t gfp_flags
)
742 struct imx_request
*req
;
747 req
= kzalloc(sizeof *req
, gfp_flags
);
751 INIT_LIST_HEAD(&req
->queue
);
757 static void imx_ep_free_request
758 (struct usb_ep
*usb_ep
, struct usb_request
*usb_req
)
760 struct imx_request
*req
;
762 req
= container_of(usb_req
, struct imx_request
, req
);
763 WARN_ON(!list_empty(&req
->queue
));
767 static int imx_ep_queue
768 (struct usb_ep
*usb_ep
, struct usb_request
*usb_req
, gfp_t gfp_flags
)
770 struct imx_ep_struct
*imx_ep
;
771 struct imx_udc_struct
*imx_usb
;
772 struct imx_request
*req
;
776 imx_ep
= container_of(usb_ep
, struct imx_ep_struct
, ep
);
777 imx_usb
= imx_ep
->imx_usb
;
778 req
= container_of(usb_req
, struct imx_request
, req
);
781 Special care on IMX udc.
782 Ignore enqueue when after set configuration from the
783 host. This assume all gadget drivers reply set
784 configuration with the next ep0 req enqueue.
786 if (imx_usb
->set_config
&& !EP_NO(imx_ep
)) {
787 imx_usb
->set_config
= 0;
789 "<%s> gadget reply set config\n", __func__
);
793 if (unlikely(!usb_req
|| !req
|| !usb_req
->complete
|| !usb_req
->buf
)) {
794 D_ERR(imx_usb
->dev
, "<%s> bad params\n", __func__
);
798 if (unlikely(!usb_ep
|| !imx_ep
)) {
799 D_ERR(imx_usb
->dev
, "<%s> bad ep\n", __func__
);
803 if (!imx_usb
->driver
|| imx_usb
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
804 D_ERR(imx_usb
->dev
, "<%s> bogus device state\n", __func__
);
809 D_REQ(imx_usb
->dev
, "<%s> ep%d %s request for [%d] bytes\n",
810 __func__
, EP_NO(imx_ep
),
811 ((!EP_NO(imx_ep
) && imx_ep
->imx_usb
->ep0state
812 == EP0_IN_DATA_PHASE
)
813 || (EP_NO(imx_ep
) && EP_DIR(imx_ep
)))
814 ? "IN" : "OUT", usb_req
->length
);
815 dump_req(__func__
, imx_ep
, usb_req
);
817 if (imx_ep
->stopped
) {
818 usb_req
->status
= -ESHUTDOWN
;
824 "<%s> refusing to queue req %p (already queued)\n",
829 local_irq_save(flags
);
831 usb_req
->status
= -EINPROGRESS
;
834 ep_add_request(imx_ep
, req
);
837 ret
= handle_ep0(imx_ep
);
839 ret
= handle_ep(imx_ep
);
841 local_irq_restore(flags
);
845 static int imx_ep_dequeue(struct usb_ep
*usb_ep
, struct usb_request
*usb_req
)
848 struct imx_ep_struct
*imx_ep
= container_of
849 (usb_ep
, struct imx_ep_struct
, ep
);
850 struct imx_request
*req
;
853 if (unlikely(!usb_ep
|| !EP_NO(imx_ep
))) {
854 D_ERR(imx_ep
->imx_usb
->dev
, "<%s> bad ep\n", __func__
);
858 local_irq_save(flags
);
860 /* make sure it's actually queued on this endpoint */
861 list_for_each_entry(req
, &imx_ep
->queue
, queue
) {
862 if (&req
->req
== usb_req
)
865 if (&req
->req
!= usb_req
) {
866 local_irq_restore(flags
);
870 done(imx_ep
, req
, -ECONNRESET
);
872 local_irq_restore(flags
);
876 static int imx_ep_set_halt(struct usb_ep
*usb_ep
, int value
)
878 struct imx_ep_struct
*imx_ep
= container_of
879 (usb_ep
, struct imx_ep_struct
, ep
);
882 if (unlikely(!usb_ep
|| !EP_NO(imx_ep
))) {
883 D_ERR(imx_ep
->imx_usb
->dev
, "<%s> bad ep\n", __func__
);
887 local_irq_save(flags
);
889 if ((imx_ep
->bEndpointAddress
& USB_DIR_IN
)
890 && !list_empty(&imx_ep
->queue
)) {
891 local_irq_restore(flags
);
895 imx_ep_stall(imx_ep
);
897 local_irq_restore(flags
);
899 D_EPX(imx_ep
->imx_usb
->dev
, "<%s> %s halt\n", __func__
, usb_ep
->name
);
903 static int imx_ep_fifo_status(struct usb_ep
*usb_ep
)
905 struct imx_ep_struct
*imx_ep
= container_of
906 (usb_ep
, struct imx_ep_struct
, ep
);
909 D_ERR(imx_ep
->imx_usb
->dev
, "<%s> bad ep\n", __func__
);
913 if (imx_ep
->imx_usb
->gadget
.speed
== USB_SPEED_UNKNOWN
)
916 return imx_fifo_bcount(imx_ep
);
919 static void imx_ep_fifo_flush(struct usb_ep
*usb_ep
)
921 struct imx_ep_struct
*imx_ep
= container_of
922 (usb_ep
, struct imx_ep_struct
, ep
);
925 local_irq_save(flags
);
927 if (!usb_ep
|| !EP_NO(imx_ep
) || !list_empty(&imx_ep
->queue
)) {
928 D_ERR(imx_ep
->imx_usb
->dev
, "<%s> bad ep\n", __func__
);
929 local_irq_restore(flags
);
933 /* toggle and halt bits stay unchanged */
936 local_irq_restore(flags
);
939 static struct usb_ep_ops imx_ep_ops
= {
940 .enable
= imx_ep_enable
,
941 .disable
= imx_ep_disable
,
943 .alloc_request
= imx_ep_alloc_request
,
944 .free_request
= imx_ep_free_request
,
946 .queue
= imx_ep_queue
,
947 .dequeue
= imx_ep_dequeue
,
949 .set_halt
= imx_ep_set_halt
,
950 .fifo_status
= imx_ep_fifo_status
,
951 .fifo_flush
= imx_ep_fifo_flush
,
954 /*******************************************************************************
955 * USB endpoint control functions
956 *******************************************************************************
959 void ep0_chg_stat(const char *label
,
960 struct imx_udc_struct
*imx_usb
, enum ep0_state stat
)
962 D_EP0(imx_usb
->dev
, "<%s> from %15s to %15s\n",
963 label
, state_name
[imx_usb
->ep0state
], state_name
[stat
]);
965 if (imx_usb
->ep0state
== stat
)
968 imx_usb
->ep0state
= stat
;
971 static void usb_init_data(struct imx_udc_struct
*imx_usb
)
973 struct imx_ep_struct
*imx_ep
;
976 /* device/ep0 records init */
977 INIT_LIST_HEAD(&imx_usb
->gadget
.ep_list
);
978 INIT_LIST_HEAD(&imx_usb
->gadget
.ep0
->ep_list
);
979 ep0_chg_stat(__func__
, imx_usb
, EP0_IDLE
);
981 /* basic endpoint records init */
982 for (i
= 0; i
< IMX_USB_NB_EP
; i
++) {
983 imx_ep
= &imx_usb
->imx_ep
[i
];
986 list_add_tail(&imx_ep
->ep
.ep_list
,
987 &imx_usb
->gadget
.ep_list
);
992 INIT_LIST_HEAD(&imx_ep
->queue
);
996 static void udc_stop_activity(struct imx_udc_struct
*imx_usb
,
997 struct usb_gadget_driver
*driver
)
999 struct imx_ep_struct
*imx_ep
;
1002 if (imx_usb
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1005 /* prevent new request submissions, kill any outstanding requests */
1006 for (i
= 1; i
< IMX_USB_NB_EP
; i
++) {
1007 imx_ep
= &imx_usb
->imx_ep
[i
];
1009 imx_ep
->stopped
= 1;
1010 imx_ep_irq_disable(imx_ep
);
1011 nuke(imx_ep
, -ESHUTDOWN
);
1019 driver
->disconnect(&imx_usb
->gadget
);
1022 /*******************************************************************************
1023 * Interrupt handlers
1024 *******************************************************************************
1028 * Called when timer expires.
1029 * Timer is started when CFG_CHG is received.
1031 static void handle_config(unsigned long data
)
1033 struct imx_udc_struct
*imx_usb
= (void *)data
;
1034 struct usb_ctrlrequest u
;
1035 int temp
, cfg
, intf
, alt
;
1037 local_irq_disable();
1039 temp
= __raw_readl(imx_usb
->base
+ USB_STAT
);
1040 cfg
= (temp
& STAT_CFG
) >> 5;
1041 intf
= (temp
& STAT_INTF
) >> 3;
1042 alt
= temp
& STAT_ALTSET
;
1045 "<%s> orig config C=%d, I=%d, A=%d / "
1046 "req config C=%d, I=%d, A=%d\n",
1047 __func__
, imx_usb
->cfg
, imx_usb
->intf
, imx_usb
->alt
,
1050 if (cfg
== 1 || cfg
== 2) {
1052 if (imx_usb
->cfg
!= cfg
) {
1053 u
.bRequest
= USB_REQ_SET_CONFIGURATION
;
1054 u
.bRequestType
= USB_DIR_OUT
|
1061 imx_usb
->driver
->setup(&imx_usb
->gadget
, &u
);
1064 if (imx_usb
->intf
!= intf
|| imx_usb
->alt
!= alt
) {
1065 u
.bRequest
= USB_REQ_SET_INTERFACE
;
1066 u
.bRequestType
= USB_DIR_OUT
|
1068 USB_RECIP_INTERFACE
;
1072 imx_usb
->intf
= intf
;
1074 imx_usb
->driver
->setup(&imx_usb
->gadget
, &u
);
1078 imx_usb
->set_config
= 0;
1083 static irqreturn_t
imx_udc_irq(int irq
, void *dev
)
1085 struct imx_udc_struct
*imx_usb
= dev
;
1086 int intr
= __raw_readl(imx_usb
->base
+ USB_INTR
);
1089 if (intr
& (INTR_WAKEUP
| INTR_SUSPEND
| INTR_RESUME
| INTR_RESET_START
1090 | INTR_RESET_STOP
| INTR_CFG_CHG
)) {
1091 dump_intr(__func__
, intr
, imx_usb
->dev
);
1092 dump_usb_stat(__func__
, imx_usb
);
1095 if (!imx_usb
->driver
)
1098 if (intr
& INTR_SOF
) {
1099 /* Copy from Freescale BSP.
1100 We must enable SOF intr and set CMDOVER.
1101 Datasheet don't specifiy this action, but it
1102 is done in Freescale BSP, so just copy it.
1104 if (imx_usb
->ep0state
== EP0_IDLE
) {
1105 temp
= __raw_readl(imx_usb
->base
+ USB_CTRL
);
1106 __raw_writel(temp
| CTRL_CMDOVER
,
1107 imx_usb
->base
+ USB_CTRL
);
1111 if (intr
& INTR_CFG_CHG
) {
1112 /* A workaround of serious IMX UDC bug.
1113 Handling of CFG_CHG should be delayed for some time, because
1114 IMX does not NACK the host when CFG_CHG interrupt is pending.
1115 There is no time to handle current CFG_CHG
1116 if next CFG_CHG or SETUP packed is send immediately.
1117 We have to clear CFG_CHG, start the timer and
1118 NACK the host by setting CTRL_CMDOVER
1119 if it sends any SETUP packet.
1120 When timer expires, handler is called to handle configuration
1121 changes. While CFG_CHG is not handled (set_config=1),
1122 we must NACK the host to every SETUP packed.
1123 This delay prevents from going out of sync with host.
1125 __raw_writel(INTR_CFG_CHG
, imx_usb
->base
+ USB_INTR
);
1126 imx_usb
->set_config
= 1;
1127 mod_timer(&imx_usb
->timer
, jiffies
+ 5);
1131 if (intr
& INTR_WAKEUP
) {
1132 if (imx_usb
->gadget
.speed
== USB_SPEED_UNKNOWN
1133 && imx_usb
->driver
&& imx_usb
->driver
->resume
)
1134 imx_usb
->driver
->resume(&imx_usb
->gadget
);
1135 imx_usb
->set_config
= 0;
1136 del_timer(&imx_usb
->timer
);
1137 imx_usb
->gadget
.speed
= USB_SPEED_FULL
;
1140 if (intr
& INTR_SUSPEND
) {
1141 if (imx_usb
->gadget
.speed
!= USB_SPEED_UNKNOWN
1142 && imx_usb
->driver
&& imx_usb
->driver
->suspend
)
1143 imx_usb
->driver
->suspend(&imx_usb
->gadget
);
1144 imx_usb
->set_config
= 0;
1145 del_timer(&imx_usb
->timer
);
1146 imx_usb
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1149 if (intr
& INTR_RESET_START
) {
1150 __raw_writel(intr
, imx_usb
->base
+ USB_INTR
);
1151 udc_stop_activity(imx_usb
, imx_usb
->driver
);
1152 imx_usb
->set_config
= 0;
1153 del_timer(&imx_usb
->timer
);
1154 imx_usb
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1157 if (intr
& INTR_RESET_STOP
)
1158 imx_usb
->gadget
.speed
= USB_SPEED_FULL
;
1161 __raw_writel(intr
, imx_usb
->base
+ USB_INTR
);
1165 static irqreturn_t
imx_udc_ctrl_irq(int irq
, void *dev
)
1167 struct imx_udc_struct
*imx_usb
= dev
;
1168 struct imx_ep_struct
*imx_ep
= &imx_usb
->imx_ep
[0];
1169 int intr
= __raw_readl(imx_usb
->base
+ USB_EP_INTR(0));
1171 dump_ep_intr(__func__
, 0, intr
, imx_usb
->dev
);
1173 if (!imx_usb
->driver
) {
1174 __raw_writel(intr
, imx_usb
->base
+ USB_EP_INTR(0));
1178 /* DEVREQ has highest priority */
1179 if (intr
& (EPINTR_DEVREQ
| EPINTR_MDEVREQ
))
1180 handle_ep0_devreq(imx_usb
);
1181 /* Seem i.MX is missing EOF interrupt sometimes.
1182 * Therefore we don't monitor EOF.
1183 * We call handle_ep0() only if a request is queued for ep0.
1185 else if (!list_empty(&imx_ep
->queue
))
1188 __raw_writel(intr
, imx_usb
->base
+ USB_EP_INTR(0));
1193 static irqreturn_t
imx_udc_bulk_irq(int irq
, void *dev
)
1195 struct imx_udc_struct
*imx_usb
= dev
;
1196 struct imx_ep_struct
*imx_ep
= &imx_usb
->imx_ep
[irq
- USBD_INT0
];
1197 int intr
= __raw_readl(imx_usb
->base
+ USB_EP_INTR(EP_NO(imx_ep
)));
1199 dump_ep_intr(__func__
, irq
- USBD_INT0
, intr
, imx_usb
->dev
);
1201 if (!imx_usb
->driver
) {
1202 __raw_writel(intr
, imx_usb
->base
+ USB_EP_INTR(EP_NO(imx_ep
)));
1208 __raw_writel(intr
, imx_usb
->base
+ USB_EP_INTR(EP_NO(imx_ep
)));
1213 irq_handler_t
intr_handler(int i
)
1217 return imx_udc_ctrl_irq
;
1223 return imx_udc_bulk_irq
;
1229 /*******************************************************************************
1230 * Static defined IMX UDC structure
1231 *******************************************************************************
1234 static const struct usb_gadget_ops imx_udc_ops
= {
1235 .get_frame
= imx_udc_get_frame
,
1236 .wakeup
= imx_udc_wakeup
,
1239 static struct imx_udc_struct controller
= {
1241 .ops
= &imx_udc_ops
,
1242 .ep0
= &controller
.imx_ep
[0].ep
,
1243 .name
= driver_name
,
1245 .init_name
= "gadget",
1255 .imx_usb
= &controller
,
1257 .bEndpointAddress
= 0,
1258 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
1262 .name
= "ep1in-bulk",
1266 .imx_usb
= &controller
,
1268 .bEndpointAddress
= USB_DIR_IN
| 1,
1269 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1273 .name
= "ep2out-bulk",
1277 .imx_usb
= &controller
,
1279 .bEndpointAddress
= USB_DIR_OUT
| 2,
1280 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1284 .name
= "ep3out-bulk",
1288 .imx_usb
= &controller
,
1290 .bEndpointAddress
= USB_DIR_OUT
| 3,
1291 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
1295 .name
= "ep4in-int",
1299 .imx_usb
= &controller
,
1301 .bEndpointAddress
= USB_DIR_IN
| 4,
1302 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
1306 .name
= "ep5out-int",
1310 .imx_usb
= &controller
,
1312 .bEndpointAddress
= USB_DIR_OUT
| 5,
1313 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
1317 /*******************************************************************************
1318 * USB gadged driver functions
1319 *******************************************************************************
1321 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
1323 struct imx_udc_struct
*imx_usb
= &controller
;
1327 || driver
->speed
< USB_SPEED_FULL
1329 || !driver
->disconnect
1334 if (imx_usb
->driver
)
1337 /* first hook up the driver ... */
1338 imx_usb
->driver
= driver
;
1339 imx_usb
->gadget
.dev
.driver
= &driver
->driver
;
1341 retval
= device_add(&imx_usb
->gadget
.dev
);
1344 retval
= driver
->bind(&imx_usb
->gadget
);
1346 D_ERR(imx_usb
->dev
, "<%s> bind to driver %s --> error %d\n",
1347 __func__
, driver
->driver
.name
, retval
);
1348 device_del(&imx_usb
->gadget
.dev
);
1353 D_INI(imx_usb
->dev
, "<%s> registered gadget driver '%s'\n",
1354 __func__
, driver
->driver
.name
);
1356 imx_udc_enable(imx_usb
);
1360 imx_usb
->driver
= NULL
;
1361 imx_usb
->gadget
.dev
.driver
= NULL
;
1364 EXPORT_SYMBOL(usb_gadget_register_driver
);
1366 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1368 struct imx_udc_struct
*imx_usb
= &controller
;
1372 if (!driver
|| driver
!= imx_usb
->driver
|| !driver
->unbind
)
1375 udc_stop_activity(imx_usb
, driver
);
1376 imx_udc_disable(imx_usb
);
1377 del_timer(&imx_usb
->timer
);
1379 driver
->unbind(&imx_usb
->gadget
);
1380 imx_usb
->gadget
.dev
.driver
= NULL
;
1381 imx_usb
->driver
= NULL
;
1383 device_del(&imx_usb
->gadget
.dev
);
1385 D_INI(imx_usb
->dev
, "<%s> unregistered gadget driver '%s'\n",
1386 __func__
, driver
->driver
.name
);
1390 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1392 /*******************************************************************************
1394 *******************************************************************************
1397 static int __init
imx_udc_probe(struct platform_device
*pdev
)
1399 struct imx_udc_struct
*imx_usb
= &controller
;
1400 struct resource
*res
;
1401 struct imxusb_platform_data
*pdata
;
1406 resource_size_t res_size
;
1408 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1410 dev_err(&pdev
->dev
, "can't get device resources\n");
1414 pdata
= pdev
->dev
.platform_data
;
1416 dev_err(&pdev
->dev
, "driver needs platform data\n");
1420 res_size
= resource_size(res
);
1421 if (!request_mem_region(res
->start
, res_size
, res
->name
)) {
1422 dev_err(&pdev
->dev
, "can't allocate %d bytes at %d address\n",
1423 res_size
, res
->start
);
1428 ret
= pdata
->init(&pdev
->dev
);
1433 base
= ioremap(res
->start
, res_size
);
1435 dev_err(&pdev
->dev
, "ioremap failed\n");
1440 clk
= clk_get(NULL
, "usbd_clk");
1443 dev_err(&pdev
->dev
, "can't get USB clock\n");
1448 if (clk_get_rate(clk
) != 48000000) {
1450 "Bad USB clock (%d Hz), changing to 48000000 Hz\n",
1451 (int)clk_get_rate(clk
));
1452 if (clk_set_rate(clk
, 48000000)) {
1454 "Unable to set correct USB clock (48MHz)\n");
1460 for (i
= 0; i
< IMX_USB_NB_EP
+ 1; i
++) {
1461 imx_usb
->usbd_int
[i
] = platform_get_irq(pdev
, i
);
1462 if (imx_usb
->usbd_int
[i
] < 0) {
1463 dev_err(&pdev
->dev
, "can't get irq number\n");
1469 for (i
= 0; i
< IMX_USB_NB_EP
+ 1; i
++) {
1470 ret
= request_irq(imx_usb
->usbd_int
[i
], intr_handler(i
),
1471 IRQF_DISABLED
, driver_name
, imx_usb
);
1473 dev_err(&pdev
->dev
, "can't get irq %i, err %d\n",
1474 imx_usb
->usbd_int
[i
], ret
);
1475 for (--i
; i
>= 0; i
--)
1476 free_irq(imx_usb
->usbd_int
[i
], imx_usb
);
1482 imx_usb
->base
= base
;
1484 imx_usb
->dev
= &pdev
->dev
;
1486 device_initialize(&imx_usb
->gadget
.dev
);
1488 imx_usb
->gadget
.dev
.parent
= &pdev
->dev
;
1489 imx_usb
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1491 platform_set_drvdata(pdev
, imx_usb
);
1493 usb_init_data(imx_usb
);
1494 imx_udc_init(imx_usb
);
1496 init_timer(&imx_usb
->timer
);
1497 imx_usb
->timer
.function
= handle_config
;
1498 imx_usb
->timer
.data
= (unsigned long)imx_usb
;
1509 pdata
->exit(&pdev
->dev
);
1511 release_mem_region(res
->start
, res_size
);
1515 static int __exit
imx_udc_remove(struct platform_device
*pdev
)
1517 struct imx_udc_struct
*imx_usb
= platform_get_drvdata(pdev
);
1518 struct imxusb_platform_data
*pdata
= pdev
->dev
.platform_data
;
1521 imx_udc_disable(imx_usb
);
1522 del_timer(&imx_usb
->timer
);
1524 for (i
= 0; i
< IMX_USB_NB_EP
+ 1; i
++)
1525 free_irq(imx_usb
->usbd_int
[i
], imx_usb
);
1527 clk_put(imx_usb
->clk
);
1528 clk_disable(imx_usb
->clk
);
1529 iounmap(imx_usb
->base
);
1531 release_mem_region(imx_usb
->res
->start
, resource_size(imx_usb
->res
));
1534 pdata
->exit(&pdev
->dev
);
1536 platform_set_drvdata(pdev
, NULL
);
1541 /*----------------------------------------------------------------------------*/
1544 #define imx_udc_suspend NULL
1545 #define imx_udc_resume NULL
1547 #define imx_udc_suspend NULL
1548 #define imx_udc_resume NULL
1551 /*----------------------------------------------------------------------------*/
1553 static struct platform_driver udc_driver
= {
1555 .name
= driver_name
,
1556 .owner
= THIS_MODULE
,
1558 .remove
= __exit_p(imx_udc_remove
),
1559 .suspend
= imx_udc_suspend
,
1560 .resume
= imx_udc_resume
,
1563 static int __init
udc_init(void)
1565 return platform_driver_probe(&udc_driver
, imx_udc_probe
);
1567 module_init(udc_init
);
1569 static void __exit
udc_exit(void)
1571 platform_driver_unregister(&udc_driver
);
1573 module_exit(udc_exit
);
1575 MODULE_DESCRIPTION("IMX USB Device Controller driver");
1576 MODULE_AUTHOR("Darius Augulis <augulis.darius@gmail.com>");
1577 MODULE_LICENSE("GPL");
1578 MODULE_ALIAS("platform:imx_udc");