| Linux kernel & device driver programming |
| [ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] |
1 /* 1
2 * driver/usb/gadget/imx_udc.c
3 *
4 * Copyright (C) 2005 Mike Lee <eemike@gm
5 * Copyright (C) 2008 Darius Augulis <aug
6 *
7 * This program is free software; you can
8 * it under the terms of the GNU General
9 * the Free Software Foundation; either v
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hop
13 * but WITHOUT ANY WARRANTY; without even
14 * MERCHANTABILITY or FITNESS FOR A PARTI
15 * GNU General Public License for more de
16 */
17
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>
25 #include <linux/io.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>
32
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/gadget.h>
35
36 #include <mach/usb.h>
37 #include <mach/hardware.h>
38
39 #include "imx_udc.h"
40
41 static const char driver_name[] = "imx_udc";
42 static const char ep0name[] = "ep0";
43
44 void ep0_chg_stat(const char *label, struct im
45
46
47 /*********************************************
48 * IMX UDC hardware related functions
49 *********************************************
50 */
51
52 void imx_udc_enable(struct imx_udc_struct *imx
53 {
54 int temp = __raw_readl(imx_usb->base +
55 __raw_writel(temp | CTRL_FE_ENA | CTRL
56
57 imx_usb->gadget.speed = USB_SPEED_FULL
58 }
59
60 void imx_udc_disable(struct imx_udc_struct *im
61 {
62 int temp = __raw_readl(imx_usb->base +
63
64 __raw_writel(temp & ~(CTRL_FE_ENA | CT
65 imx_usb->base + USB_CTRL);
66
67 ep0_chg_stat(__func__, imx_usb, EP0_ID
68 imx_usb->gadget.speed = USB_SPEED_UNKN
69 }
70
71 void imx_udc_reset(struct imx_udc_struct *imx_
72 {
73 int temp = __raw_readl(imx_usb->base +
74
75 /* set RST bit */
76 __raw_writel(temp | ENAB_RST, imx_usb-
77
78 /* wait RST bit to clear */
79 do {} while (__raw_readl(imx_usb->base
80
81 /* wait CFG bit to assert */
82 do {} while (!(__raw_readl(imx_usb->ba
83
84 /* udc module is now ready */
85 }
86
87 void imx_udc_config(struct imx_udc_struct *imx
88 {
89 u8 ep_conf[5];
90 u8 i, j, cfg;
91 struct imx_ep_struct *imx_ep;
92
93 /* wait CFG bit to assert */
94 do {} while (!(__raw_readl(imx_usb->ba
95
96 /* Download the endpoint buffer for en
97 for (j = 0; j < 5; j++) {
98 i = (j == 2 ? imx_usb->imx_ep[
99 __raw_writeb(i, imx_usb->base
100 do {} while (__raw_readl(imx_u
101 }
102
103 /* Download the endpoint buffers for e
104 * We specify two configurations, one
105 */
106 for (cfg = 1; cfg < 3; cfg++) {
107 for (i = 1; i < IMX_USB_NB_EP;
108 imx_ep = &imx_usb->imx
109 /* EP no | Config no *
110 ep_conf[0] = (i << 4)
111 /* Type | Direction */
112 ep_conf[1] = (imx_ep->
113 (EP_DI
114 /* Max packet size */
115 ep_conf[2] = imx_ep->f
116 /* TRXTYP */
117 ep_conf[3] = 0xC0;
118 /* FIFO no */
119 ep_conf[4] = i;
120
121 D_INI(imx_usb->dev,
122 "<%s> ep%d_con
123 "[%02x-%02x-%0
124 __func__, i, c
125 ep_conf[0], ep
126 ep_conf[3], ep
127
128 for (j = 0; j < 5; j++
129 __raw_writeb(e
130 imx_us
131 do {} while (_
132
133 & DADR
134 }
135 }
136 }
137
138 /* wait CFG bit to clear */
139 do {} while (__raw_readl(imx_usb->base
140 }
141
142 void imx_udc_init_irq(struct imx_udc_struct *i
143 {
144 int i;
145
146 /* Mask and clear all irqs */
147 __raw_writel(0xFFFFFFFF, imx_usb->base
148 __raw_writel(0xFFFFFFFF, imx_usb->base
149 for (i = 0; i < IMX_USB_NB_EP; i++) {
150 __raw_writel(0x1FF, imx_usb->b
151 __raw_writel(0x1FF, imx_usb->b
152 }
153
154 /* Enable USB irqs */
155 __raw_writel(INTR_MSOF | INTR_FRAME_MA
156
157 /* Enable EP0 irqs */
158 __raw_writel(0x1FF & ~(EPINTR_DEVREQ |
159 | EPINTR_EOF | EPINTR_FIFO_EMP
160 imx_usb->base + USB_EP_MASK(0)
161 }
162
163 void imx_udc_init_ep(struct imx_udc_struct *im
164 {
165 int i, max, temp;
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) {
170 case 8:
171 max = 0;
172 break;
173 case 16:
174 max = 1;
175 break;
176 case 32:
177 max = 2;
178 break;
179 case 64:
180 max = 3;
181 break;
182 default:
183 max = 1;
184 break;
185 }
186 temp = (EP_DIR(imx_ep) << 7) |
187 | (imx_ep->bmAttribute
188 __raw_writel(temp, imx_usb->ba
189 __raw_writel(temp | EPSTAT_FLU
190
191 D_INI(imx_usb->dev, "<%s> ep%d
192 __raw_readl(imx_usb->b
193 }
194 }
195
196 void imx_udc_init_fifo(struct imx_udc_struct *
197 {
198 int i, temp;
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];
202
203 /* Fifo control */
204 temp = EP_DIR(imx_ep) ? 0x0B00
205 __raw_writel(temp, imx_usb->ba
206 D_INI(imx_usb->dev, "<%s> ep%d
207 __raw_readl(imx_usb->b
208
209 /* Fifo alarm */
210 temp = (i ? imx_ep->fifosize /
211 __raw_writel(temp, imx_usb->ba
212 D_INI(imx_usb->dev, "<%s> ep%d
213 __raw_readl(imx_usb->b
214 }
215 }
216
217 static void imx_udc_init(struct imx_udc_struct
218 {
219 /* Reset UDC */
220 imx_udc_reset(imx_usb);
221
222 /* Download config to enpoint buffer *
223 imx_udc_config(imx_usb);
224
225 /* Setup interrups */
226 imx_udc_init_irq(imx_usb);
227
228 /* Setup endpoints */
229 imx_udc_init_ep(imx_usb);
230
231 /* Setup fifos */
232 imx_udc_init_fifo(imx_usb);
233 }
234
235 void imx_ep_irq_enable(struct imx_ep_struct *i
236 {
237
238 int i = EP_NO(imx_ep);
239
240 __raw_writel(0x1FF, imx_ep->imx_usb->b
241 __raw_writel(0x1FF, imx_ep->imx_usb->b
242 __raw_writel(0x1FF & ~(EPINTR_EOT | EP
243 imx_ep->imx_usb->base + USB_EP
244 }
245
246 void imx_ep_irq_disable(struct imx_ep_struct *
247 {
248
249 int i = EP_NO(imx_ep);
250
251 __raw_writel(0x1FF, imx_ep->imx_usb->b
252 __raw_writel(0x1FF, imx_ep->imx_usb->b
253 }
254
255 int imx_ep_empty(struct imx_ep_struct *imx_ep)
256 {
257 struct imx_udc_struct *imx_usb = imx_e
258
259 return __raw_readl(imx_usb->base + USB
260 & FSTAT_EMPTY;
261 }
262
263 unsigned imx_fifo_bcount(struct imx_ep_struct
264 {
265 struct imx_udc_struct *imx_usb = imx_e
266
267 return (__raw_readl(imx_usb->base + US
268 & EPSTAT_BCOUNT) >> 16
269 }
270
271 void imx_flush(struct imx_ep_struct *imx_ep)
272 {
273 struct imx_udc_struct *imx_usb = imx_e
274
275 int temp = __raw_readl(imx_usb->base +
276 __raw_writel(temp | EPSTAT_FLUSH,
277 imx_usb->base + USB_EP_STAT(EP
278 }
279
280 void imx_ep_stall(struct imx_ep_struct *imx_ep
281 {
282 struct imx_udc_struct *imx_usb = imx_e
283 int temp, i;
284
285 D_ERR(imx_usb->dev,
286 "<%s> Forced stall on %s\n", _
287
288 imx_flush(imx_ep);
289
290 /* Special care for ep0 */
291 if (!EP_NO(imx_ep)) {
292 temp = __raw_readl(imx_usb->ba
293 __raw_writel(temp | CTRL_CMDOV
294
295 do { } while (__raw_readl(imx_
296
297 temp = __raw_readl(imx_usb->ba
298 __raw_writel(temp & ~CTRL_CMDE
299 }
300 else {
301 temp = __raw_readl(imx_usb->ba
302 __raw_writel(temp | EPSTAT_STA
303 imx_usb->base + USB_EP
304
305 for (i = 0; i < 100; i ++) {
306 temp = __raw_readl(imx
307
308 if (!(temp & EPSTAT_ST
309 break;
310 udelay(20);
311 }
312 if (i == 100)
313 D_ERR(imx_usb->dev, "<
314 __func__, imx_
315 }
316 }
317
318 static int imx_udc_get_frame(struct usb_gadget
319 {
320 struct imx_udc_struct *imx_usb = conta
321 struct imx_udc_struct,
322
323 return __raw_readl(imx_usb->base + USB
324 }
325
326 static int imx_udc_wakeup(struct usb_gadget *_
327 {
328 return 0;
329 }
330
331 /*********************************************
332 * USB request control functions
333 *********************************************
334 */
335
336 static void ep_add_request(struct imx_ep_struc
337
338 {
339 if (unlikely(!req))
340 return;
341
342 req->in_use = 1;
343 list_add_tail(&req->queue, &imx_ep->qu
344 }
345
346 static void ep_del_request(struct imx_ep_struc
347
348 {
349 if (unlikely(!req))
350 return;
351
352 list_del_init(&req->queue);
353 req->in_use = 0;
354 }
355
356 static void done(struct imx_ep_struct *imx_ep,
357 struct
358 {
359 ep_del_request(imx_ep, req);
360
361 if (likely(req->req.status == -EINPROG
362 req->req.status = status;
363 else
364 status = req->req.status;
365
366 if (status && status != -ESHUTDOWN)
367 D_ERR(imx_ep->imx_usb->dev,
368 "<%s> complete %s req
369 imx_ep->ep.name, &req-
370 req->req.actual, req->
371
372 req->req.complete(&imx_ep->ep, &req->r
373 }
374
375 static void nuke(struct imx_ep_struct *imx_ep,
376 {
377 struct imx_request *req;
378
379 while (!list_empty(&imx_ep->queue)) {
380 req = list_entry(imx_ep->queue
381 done(imx_ep, req, status);
382 }
383 }
384
385 /*********************************************
386 * Data tansfer over USB functions
387 *********************************************
388 */
389 static int read_packet(struct imx_ep_struct *i
390 {
391 u8 *buf;
392 int bytes_ep, bufferspace, count,
393
394 bytes_ep = imx_fifo_bcount(imx_ep);
395 bufferspace = req->req.length - req->r
396
397 buf = req->req.buf + req->req.actual;
398 prefetchw(buf);
399
400 if (unlikely(imx_ep_empty(imx_ep)))
401 count = 0; /* zlp */
402 else
403 count = min(bytes_ep, buffersp
404
405 for (i = count; i > 0; i--)
406 *buf++ = __raw_readb(imx_ep->i
407
408 req->req.actual += count;
409
410 return count;
411 }
412
413 static int write_packet(struct imx_ep_struct *
414 {
415 u8 *buf;
416 int length, count, temp;
417
418 if (unlikely(__raw_readl(imx_ep->imx_u
419 USB_EP_STAT(E
420 D_TRX(imx_ep->imx_usb->dev, "<
421 __func__, imx_ep->ep.n
422 return -1;
423 }
424
425 buf = req->req.buf + req->req.actual;
426 prefetch(buf);
427
428 length = min(req->req.length - req->re
429
430 if (imx_fifo_bcount(imx_ep) + length >
431 D_TRX(imx_ep->imx_usb->dev, "<
432 __func__, imx_ep->ep.n
433 return -1;
434 }
435
436 req->req.actual += length;
437 count = length;
438
439 if (!count && req->req.zero) { /* zlp
440 temp = __raw_readl(imx_ep->imx
441 + USB_EP_STAT(EP_NO(im
442 __raw_writel(temp | EPSTAT_ZLP
443 + USB_EP_STAT(EP_NO(im
444 D_TRX(imx_ep->imx_usb->dev, "<
445 return 0;
446 }
447
448 while (count--) {
449 if (count == 0) { /* las
450 temp = __raw_readl(imx
451 + USB_EP_FCTRL
452 __raw_writel(temp | FC
453 + USB_EP_FCTRL
454 }
455 __raw_writeb(*buf++,
456 imx_ep->imx_usb->base
457 }
458
459 return length;
460 }
461
462 static int read_fifo(struct imx_ep_struct *imx
463 {
464 int bytes = 0,
465 count,
466 completed = 0;
467
468 while (__raw_readl(imx_ep->imx_usb->ba
469 & FSTAT_FR) {
470 count = read_packet(im
471 bytes += count;
472
473 completed = (count !=
474 if (completed || req->
475 completed = 1;
476 break;
477 }
478 }
479
480 if (completed || !req->req.length) {
481 done(imx_ep, req, 0);
482 D_REQ(imx_ep->imx_usb->dev, "<
483 __func__, imx_ep->ep.n
484 completed ? "completed
485 if (!EP_NO(imx_ep))
486 ep0_chg_stat(__func__,
487 }
488
489 D_TRX(imx_ep->imx_usb->dev, "<%s> byte
490
491 return completed;
492 }
493
494 static int write_fifo(struct imx_ep_struct *im
495 {
496 int bytes = 0,
497 count,
498 completed = 0;
499
500 while (!completed) {
501 count = write_packet(imx_ep, r
502 if (count < 0)
503 break; /* busy */
504 bytes += count;
505
506 /* last packet "must be" short
507 completed = (count != imx_ep->
508
509 if (unlikely(completed)) {
510 done(imx_ep, req, 0);
511 D_REQ(imx_ep->imx_usb-
512 __func__, imx_
513 completed ? "c
514 if (!EP_NO(imx_ep))
515 ep0_chg_stat(_
516
517 }
518 }
519
520 D_TRX(imx_ep->imx_usb->dev, "<%s> byte
521
522 return completed;
523 }
524
525 /*********************************************
526 * Endpoint handlers
527 *********************************************
528 */
529 static int handle_ep(struct imx_ep_struct *imx
530 {
531 struct imx_request *req;
532 int completed = 0;
533
534 do {
535 if (!list_empty(&imx_ep->queue
536 req = list_entry(imx_e
537 struct imx_req
538 else {
539 D_REQ(imx_ep->imx_usb-
540 __func__, imx_
541 return 0;
542 }
543
544 if (EP_DIR(imx_ep)) /* to
545 completed = write_fifo
546 else /* to
547 completed = read_fifo(
548
549 dump_ep_stat(__func__, imx_ep)
550
551 } while (completed);
552
553 return 0;
554 }
555
556 static int handle_ep0(struct imx_ep_struct *im
557 {
558 struct imx_request *req = NULL;
559 int ret = 0;
560
561 if (!list_empty(&imx_ep->queue)) {
562 req = list_entry(imx_ep->queue
563
564 switch (imx_ep->imx_usb->ep0st
565
566 case EP0_IN_DATA_PHASE:
567 write_fifo(imx_ep, req
568 break;
569 case EP0_OUT_DATA_PHASE:
570 read_fifo(imx_ep, req)
571 break;
572 default:
573 D_EP0(imx_ep->imx_usb-
574 "<%s> ep0 i/o,
575 __func__, imx_
576 ep_del_request(imx_ep,
577 ret = -EL2HLT;
578 break;
579 }
580 }
581
582 else
583 D_ERR(imx_ep->imx_usb->dev, "<
584
585
586 return ret;
587 }
588
589 static void handle_ep0_devreq(struct imx_udc_s
590 {
591 struct imx_ep_struct *imx_ep = &imx_us
592 union {
593 struct usb_ctrlrequest r;
594 u8 raw[8]
595 u32 word[2
596 } u;
597 int temp, i;
598
599 nuke(imx_ep, -EPROTO);
600
601 /* read SETUP packet */
602 for (i = 0; i < 2; i++) {
603 if (imx_ep_empty(imx_ep)) {
604 D_ERR(imx_usb->dev,
605 "<%s> no setup
606 goto stall;
607 }
608 u.word[i] = __raw_readl(imx_us
609
610 }
611
612 temp = imx_ep_empty(imx_ep);
613 while (!imx_ep_empty(imx_ep)) {
614 i = __raw_readl(imx_usb->base
615 D_ERR(imx_usb->dev,
616 "<%s> wrong to have ex
617 __func__, i);
618 }
619 if (!temp)
620 goto stall;
621
622 le16_to_cpus(&u.r.wValue);
623 le16_to_cpus(&u.r.wIndex);
624 le16_to_cpus(&u.r.wLength);
625
626 D_REQ(imx_usb->dev, "<%s> SETUP %02x.%
627 __func__, u.r.bRequestType, u.
628 u.r.wValue, u.r.wIndex, u.r.wL
629
630 if (imx_usb->set_config) {
631 /* NACK the host by using CMDO
632 temp = __raw_readl(imx_usb->ba
633 __raw_writel(temp | CTRL_CMDOV
634
635 D_ERR(imx_usb->dev,
636 "<%s> set config req i
637 __func__);
638 return;
639 }
640
641 if (u.r.bRequestType & USB_DIR_IN)
642 ep0_chg_stat(__func__, imx_usb
643 else
644 ep0_chg_stat(__func__, imx_usb
645
646 i = imx_usb->driver->setup(&imx_usb->g
647 if (i < 0) {
648 D_ERR(imx_usb->dev, "<%s> devi
649 __func__, i);
650 goto stall;
651 }
652
653 return;
654 stall:
655 D_ERR(imx_usb->dev, "<%s> protocol STA
656 imx_ep_stall(imx_ep);
657 ep0_chg_stat(__func__, imx_usb, EP0_ST
658 return;
659 }
660
661 /*********************************************
662 * USB gadget callback functions
663 *********************************************
664 */
665
666 static int imx_ep_enable(struct usb_ep *usb_ep
667 const struct u
668 {
669 struct imx_ep_struct *imx_ep = contain
670
671 struct imx_udc_struct *imx_usb = imx_e
672 unsigned long flags;
673
674 if (!usb_ep
675 || !desc
676 || !EP_NO(imx_ep)
677 || desc->bDescriptorType != US
678 || imx_ep->bEndpointAddress !=
679 D_ERR(imx_usb->dev,
680 "<%s> bad ep o
681 return -EINVAL;
682 }
683
684 if (imx_ep->bmAttributes != desc->bmAt
685 D_ERR(imx_usb->dev,
686 "<%s> %s type mismatch
687 return -EINVAL;
688 }
689
690 if (imx_ep->fifosize < le16_to_cpu(des
691 D_ERR(imx_usb->dev,
692 "<%s> bad %s maxpacket
693 return -ERANGE;
694 }
695
696 if (!imx_usb->driver || imx_usb->gadge
697 D_ERR(imx_usb->dev, "<%s> bogu
698 return -ESHUTDOWN;
699 }
700
701 local_irq_save(flags);
702
703 imx_ep->stopped = 0;
704 imx_flush(imx_ep);
705 imx_ep_irq_enable(imx_ep);
706
707 local_irq_restore(flags);
708
709 D_EPX(imx_usb->dev, "<%s> ENABLED %s\n
710 return 0;
711 }
712
713 static int imx_ep_disable(struct usb_ep *usb_e
714 {
715 struct imx_ep_struct *imx_ep = contain
716
717 unsigned long flags;
718
719 if (!usb_ep || !EP_NO(imx_ep) || !list
720 D_ERR(imx_ep->imx_usb->dev, "<
721 __func__, usb_ep ? imx
722 return -EINVAL;
723 }
724
725 local_irq_save(flags);
726
727 imx_ep->stopped = 1;
728 nuke(imx_ep, -ESHUTDOWN);
729 imx_flush(imx_ep);
730 imx_ep_irq_disable(imx_ep);
731
732 local_irq_restore(flags);
733
734 D_EPX(imx_ep->imx_usb->dev,
735 "<%s> DISABLED %s\n", __func__
736 return 0;
737 }
738
739 static struct usb_request *imx_ep_alloc_reques
740 (struc
741 {
742 struct imx_request *req;
743
744 if (!usb_ep)
745 return NULL;
746
747 req = kzalloc(sizeof *req, gfp_flags);
748 if (!req)
749 return NULL;
750
751 INIT_LIST_HEAD(&req->queue);
752 req->in_use = 0;
753
754 return &req->req;
755 }
756
757 static void imx_ep_free_request
758 (struct usb_ep *usb_ep
759 {
760 struct imx_request *req;
761
762 req = container_of(usb_req, struct imx
763 WARN_ON(!list_empty(&req->queue));
764 kfree(req);
765 }
766
767 static int imx_ep_queue
768 (struct usb_ep *usb_ep, struct usb_req
769 {
770 struct imx_ep_struct *imx_ep;
771 struct imx_udc_struct *imx_usb;
772 struct imx_request *req;
773 unsigned long flags;
774 int ret = 0;
775
776 imx_ep = container_of(usb_ep, struct i
777 imx_usb = imx_ep->imx_usb;
778 req = container_of(usb_req, struct imx
779
780 /*
781 Special care on IMX udc.
782 Ignore enqueue when after set config
783 host. This assume all gadget drivers
784 configuration with the next ep0 req
785 */
786 if (imx_usb->set_config && !EP_NO(imx_
787 imx_usb->set_config = 0;
788 D_ERR(imx_usb->dev,
789 "<%s> gadget reply set
790 return 0;
791 }
792
793 if (unlikely(!usb_req || !req || !usb_
794 D_ERR(imx_usb->dev, "<%s> bad
795 return -EINVAL;
796 }
797
798 if (unlikely(!usb_ep || !imx_ep)) {
799 D_ERR(imx_usb->dev, "<%s> bad
800 return -EINVAL;
801 }
802
803 if (!imx_usb->driver || imx_usb->gadge
804 D_ERR(imx_usb->dev, "<%s> bogu
805 return -ESHUTDOWN;
806 }
807
808 /* Debug */
809 D_REQ(imx_usb->dev, "<%s> ep%d %s requ
810 __func__, EP_NO(imx_ep),
811 ((!EP_NO(imx_ep) && imx_ep->im
812
813 || (EP_NO(imx_ep) && EP_DIR(im
814 ? "IN"
815 dump_req(__func__, imx_ep, usb_req);
816
817 if (imx_ep->stopped) {
818 usb_req->status = -ESHUTDOWN;
819 return -ESHUTDOWN;
820 }
821
822 if (req->in_use) {
823 D_ERR(imx_usb->dev,
824 "<%s> refusing to queu
825 __func__, req);
826 return 0;
827 }
828
829 local_irq_save(flags);
830
831 usb_req->status = -EINPROGRESS;
832 usb_req->actual = 0;
833
834 ep_add_request(imx_ep, req);
835
836 if (!EP_NO(imx_ep))
837 ret = handle_ep0(imx_ep);
838 else
839 ret = handle_ep(imx_ep);
840
841 local_irq_restore(flags);
842 return ret;
843 }
844
845 static int imx_ep_dequeue(struct usb_ep *usb_e
846 {
847
848 struct imx_ep_struct *imx_ep = contain
849 (usb_e
850 struct imx_request *req;
851 unsigned long flags;
852
853 if (unlikely(!usb_ep || !EP_NO(imx_ep)
854 D_ERR(imx_ep->imx_usb->dev, "<
855 return -EINVAL;
856 }
857
858 local_irq_save(flags);
859
860 /* make sure it's actually queued on t
861 list_for_each_entry(req, &imx_ep->queu
862 if (&req->req == usb_req)
863 break;
864 }
865 if (&req->req != usb_req) {
866 local_irq_restore(flags);
867 return -EINVAL;
868 }
869
870 done(imx_ep, req, -ECONNRESET);
871
872 local_irq_restore(flags);
873 return 0;
874 }
875
876 static int imx_ep_set_halt(struct usb_ep *usb_
877 {
878 struct imx_ep_struct *imx_ep = contain
879 (usb_e
880 unsigned long flags;
881
882 if (unlikely(!usb_ep || !EP_NO(imx_ep)
883 D_ERR(imx_ep->imx_usb->dev, "<
884 return -EINVAL;
885 }
886
887 local_irq_save(flags);
888
889 if ((imx_ep->bEndpointAddress & USB_DI
890 && !list_empty(&imx_ep->queue)
891 local_irq_restore(flag
892 return -EAGAIN;
893 }
894
895 imx_ep_stall(imx_ep);
896
897 local_irq_restore(flags);
898
899 D_EPX(imx_ep->imx_usb->dev, "<%s> %s h
900 return 0;
901 }
902
903 static int imx_ep_fifo_status(struct usb_ep *u
904 {
905 struct imx_ep_struct *imx_ep = contain
906 (usb_e
907
908 if (!usb_ep) {
909 D_ERR(imx_ep->imx_usb->dev, "<
910 return -ENODEV;
911 }
912
913 if (imx_ep->imx_usb->gadget.speed == U
914 return 0;
915 else
916 return imx_fifo_bcount(imx_ep)
917 }
918
919 static void imx_ep_fifo_flush(struct usb_ep *u
920 {
921 struct imx_ep_struct *imx_ep = contain
922 (usb_e
923 unsigned long flags;
924
925 local_irq_save(flags);
926
927 if (!usb_ep || !EP_NO(imx_ep) || !list
928 D_ERR(imx_ep->imx_usb->dev, "<
929 local_irq_restore(flags);
930 return;
931 }
932
933 /* toggle and halt bits stay unchanged
934 imx_flush(imx_ep);
935
936 local_irq_restore(flags);
937 }
938
939 static struct usb_ep_ops imx_ep_ops = {
940 .enable = imx_ep_enable,
941 .disable = imx_ep_disable,
942
943 .alloc_request = imx_ep_alloc_request
944 .free_request = imx_ep_free_request,
945
946 .queue = imx_ep_queue,
947 .dequeue = imx_ep_dequeue,
948
949 .set_halt = imx_ep_set_halt,
950 .fifo_status = imx_ep_fifo_status,
951 .fifo_flush = imx_ep_fifo_flush,
952 };
953
954 /*********************************************
955 * USB endpoint control functions
956 *********************************************
957 */
958
959 void ep0_chg_stat(const char *label,
960 struct imx_udc_struct
961 {
962 D_EP0(imx_usb->dev, "<%s> from %15s to
963 label, state_name[imx_usb->ep0
964
965 if (imx_usb->ep0state == stat)
966 return;
967
968 imx_usb->ep0state = stat;
969 }
970
971 static void usb_init_data(struct imx_udc_struc
972 {
973 struct imx_ep_struct *imx_ep;
974 u8 i;
975
976 /* device/ep0 records init */
977 INIT_LIST_HEAD(&imx_usb->gadget.ep_lis
978 INIT_LIST_HEAD(&imx_usb->gadget.ep0->e
979 ep0_chg_stat(__func__, imx_usb, EP0_ID
980
981 /* basic endpoint records init */
982 for (i = 0; i < IMX_USB_NB_EP; i++) {
983 imx_ep = &imx_usb->imx_ep[i];
984
985 if (i) {
986 list_add_tail(&imx_ep-
987 &imx_usb->gadg
988 imx_ep->stopped = 1;
989 } else
990 imx_ep->stopped = 0;
991
992 INIT_LIST_HEAD(&imx_ep->queue)
993 }
994 }
995
996 static void udc_stop_activity(struct imx_udc_s
997 struct
998 {
999 struct imx_ep_struct *imx_ep;
1000 int i;
1001
1002 if (imx_usb->gadget.speed == USB_SPEE
1003 driver = NULL;
1004
1005 /* prevent new request submissions, k
1006 for (i = 1; i < IMX_USB_NB_EP; i++) {
1007 imx_ep = &imx_usb->imx_ep[i];
1008 imx_flush(imx_ep);
1009 imx_ep->stopped = 1;
1010 imx_ep_irq_disable(imx_ep);
1011 nuke(imx_ep, -ESHUTDOWN);
1012 }
1013
1014 imx_usb->cfg = 0;
1015 imx_usb->intf = 0;
1016 imx_usb->alt = 0;
1017
1018 if (driver)
1019 driver->disconnect(&imx_usb->
1020 }
1021
1022 /********************************************
1023 * Interrupt handlers
1024 ********************************************
1025 */
1026
1027 /*
1028 * Called when timer expires.
1029 * Timer is started when CFG_CHG is received.
1030 */
1031 static void handle_config(unsigned long data)
1032 {
1033 struct imx_udc_struct *imx_usb = (voi
1034 struct usb_ctrlrequest u;
1035 int temp, cfg, intf, alt;
1036
1037 local_irq_disable();
1038
1039 temp = __raw_readl(imx_usb->base + US
1040 cfg = (temp & STAT_CFG) >> 5;
1041 intf = (temp & STAT_INTF) >> 3;
1042 alt = temp & STAT_ALTSET;
1043
1044 D_REQ(imx_usb->dev,
1045 "<%s> orig config C=%d, I=%d,
1046 "req config C=%d, I=%d, A=%d\
1047 __func__, imx_usb->cfg, imx_u
1048 cfg, intf, alt);
1049
1050 if (cfg == 1 || cfg == 2) {
1051
1052 if (imx_usb->cfg != cfg) {
1053 u.bRequest = USB_REQ_
1054 u.bRequestType = USB_
1055 USB_T
1056 USB_R
1057 u.wValue = cfg;
1058 u.wIndex = 0;
1059 u.wLength = 0;
1060 imx_usb->cfg = cfg;
1061 imx_usb->driver->setu
1062
1063 }
1064 if (imx_usb->intf != intf ||
1065 u.bRequest = USB_REQ_
1066 u.bRequestType = USB_
1067 USB
1068 USB
1069 u.wValue = alt;
1070 u.wIndex = intf;
1071 u.wLength = 0;
1072 imx_usb->intf = intf;
1073 imx_usb->alt = alt;
1074 imx_usb->driver->setu
1075 }
1076 }
1077
1078 imx_usb->set_config = 0;
1079
1080 local_irq_enable();
1081 }
1082
1083 static irqreturn_t imx_udc_irq(int irq, void
1084 {
1085 struct imx_udc_struct *imx_usb = dev;
1086 int intr = __raw_readl(imx_usb->base
1087 int temp;
1088
1089 if (intr & (INTR_WAKEUP | INTR_SUSPEN
1090 | INTR_RESET_STOP | I
1091 dump_intr(__f
1092 dump_usb_stat
1093 }
1094
1095 if (!imx_usb->driver)
1096 goto end_irq;
1097
1098 if (intr & INTR_SOF) {
1099 /* Copy from Freescale BSP.
1100 We must enable SOF intr an
1101 Datasheet don't specifiy t
1102 is done in Freescale BSP,
1103 */
1104 if (imx_usb->ep0state == EP0_
1105 temp = __raw_readl(im
1106 __raw_writel(temp | C
1107
1108 }
1109 }
1110
1111 if (intr & INTR_CFG_CHG) {
1112 /* A workaround of serious IM
1113 Handling of CFG_CHG should
1114 IMX does not NACK the host
1115 There is no time to handle
1116 if next CFG_CHG or SETUP p
1117 We have to clear CFG_CHG,
1118 NACK the host by setting C
1119 if it sends any SETUP pack
1120 When timer expires, handle
1121 changes. While CFG_CHG is
1122 we must NACK the host to e
1123 This delay prevents from g
1124 */
1125 __raw_writel(INTR_CFG_CHG, im
1126 imx_usb->set_config = 1;
1127 mod_timer(&imx_usb->timer, ji
1128 goto end_irq;
1129 }
1130
1131 if (intr & INTR_WAKEUP) {
1132 if (imx_usb->gadget.speed ==
1133 && imx_usb->driver &&
1134 imx_usb->driv
1135 imx_usb->set_config = 0;
1136 del_timer(&imx_usb->timer);
1137 imx_usb->gadget.speed = USB_S
1138 }
1139
1140 if (intr & INTR_SUSPEND) {
1141 if (imx_usb->gadget.speed !=
1142 && imx_usb->driver &&
1143 imx_usb->driv
1144 imx_usb->set_config = 0;
1145 del_timer(&imx_usb->timer);
1146 imx_usb->gadget.speed = USB_S
1147 }
1148
1149 if (intr & INTR_RESET_START) {
1150 __raw_writel(intr, imx_usb->b
1151 udc_stop_activity(imx_usb, im
1152 imx_usb->set_config = 0;
1153 del_timer(&imx_usb->timer);
1154 imx_usb->gadget.speed = USB_S
1155 }
1156
1157 if (intr & INTR_RESET_STOP)
1158 imx_usb->gadget.speed = USB_S
1159
1160 end_irq:
1161 __raw_writel(intr, imx_usb->base + US
1162 return IRQ_HANDLED;
1163 }
1164
1165 static irqreturn_t imx_udc_ctrl_irq(int irq,
1166 {
1167 struct imx_udc_struct *imx_usb = dev;
1168 struct imx_ep_struct *imx_ep = &imx_u
1169 int intr = __raw_readl(imx_usb->base
1170
1171 dump_ep_intr(__func__, 0, intr, imx_u
1172
1173 if (!imx_usb->driver) {
1174 __raw_writel(intr, imx_usb->b
1175 return IRQ_HANDLED;
1176 }
1177
1178 /* DEVREQ has highest priority */
1179 if (intr & (EPINTR_DEVREQ | EPINTR_MD
1180 handle_ep0_devreq(imx_usb);
1181 /* Seem i.MX is missing EOF interrupt
1182 * Therefore we don't monitor EOF.
1183 * We call handle_ep0() only if a req
1184 */
1185 else if (!list_empty(&imx_ep->queue))
1186 handle_ep0(imx_ep);
1187
1188 __raw_writel(intr, imx_usb->base + US
1189
1190 return IRQ_HANDLED;
1191 }
1192
1193 static irqreturn_t imx_udc_bulk_irq(int irq,
1194 {
1195 struct imx_udc_struct *imx_usb = dev;
1196 struct imx_ep_struct *imx_ep = &imx_u
1197 int intr = __raw_readl(imx_usb->base
1198
1199 dump_ep_intr(__func__, irq - USBD_INT
1200
1201 if (!imx_usb->driver) {
1202 __raw_writel(intr, imx_usb->b
1203 return IRQ_HANDLED;
1204 }
1205
1206 handle_ep(imx_ep);
1207
1208 __raw_writel(intr, imx_usb->base + US
1209
1210 return IRQ_HANDLED;
1211 }
1212
1213 irq_handler_t intr_handler(int i)
1214 {
1215 switch (i) {
1216 case 0:
1217 return imx_udc_ctrl_irq;
1218 case 1:
1219 case 2:
1220 case 3:
1221 case 4:
1222 case 5:
1223 return imx_udc_bulk_irq;
1224 default:
1225 return imx_udc_irq;
1226 }
1227 }
1228
1229 /********************************************
1230 * Static defined IMX UDC structure
1231 ********************************************
1232 */
1233
1234 static const struct usb_gadget_ops imx_udc_op
1235 .get_frame = imx_udc_get_frame,
1236 .wakeup = imx_udc_wakeup,
1237 };
1238
1239 static struct imx_udc_struct controller = {
1240 .gadget = {
1241 .ops = &imx_udc_op
1242 .ep0 = &controller
1243 .name = driver_name
1244 .dev = {
1245 .init_name = "ga
1246 },
1247 },
1248
1249 .imx_ep[0] = {
1250 .ep = {
1251 .name = ep0
1252 .ops = &im
1253 .maxpacket = 32,
1254 },
1255 .imx_usb = &co
1256 .fifosize = 32,
1257 .bEndpointAddress = 0,
1258 .bmAttributes = USB
1259 },
1260 .imx_ep[1] = {
1261 .ep = {
1262 .name = "ep
1263 .ops = &im
1264 .maxpacket = 64,
1265 },
1266 .imx_usb = &co
1267 .fifosize = 64,
1268 .bEndpointAddress = USB
1269 .bmAttributes = USB
1270 },
1271 .imx_ep[2] = {
1272 .ep = {
1273 .name = "ep
1274 .ops = &im
1275 .maxpacket = 64,
1276 },
1277 .imx_usb = &co
1278 .fifosize = 64,
1279 .bEndpointAddress = USB
1280 .bmAttributes = USB
1281 },
1282 .imx_ep[3] = {
1283 .ep = {
1284 .name = "ep
1285 .ops = &im
1286 .maxpacket = 32,
1287 },
1288 .imx_usb = &co
1289 .fifosize = 32,
1290 .bEndpointAddress = USB
1291 .bmAttributes = USB
1292 },
1293 .imx_ep[4] = {
1294 .ep = {
1295 .name = "ep
1296 .ops = &im
1297 .maxpacket = 32,
1298 },
1299 .imx_usb = &co
1300 .fifosize = 32,
1301 .bEndpointAddress = USB
1302 .bmAttributes = USB
1303 },
1304 .imx_ep[5] = {
1305 .ep = {
1306 .name = "ep
1307 .ops = &im
1308 .maxpacket = 32,
1309 },
1310 .imx_usb = &co
1311 .fifosize = 32,
1312 .bEndpointAddress = USB
1313 .bmAttributes = USB
1314 },
1315 };
1316
1317 /********************************************
1318 * USB gadged driver functions
1319 ********************************************
1320 */
1321 int usb_gadget_register_driver(struct usb_gad
1322 {
1323 struct imx_udc_struct *imx_usb = &con
1324 int retval;
1325
1326 if (!driver
1327 || driver->speed < USB_SPEED_
1328 || !driver->bind
1329 || !driver->disconnect
1330 || !driver->setup)
1331 return -EINVAL;
1332 if (!imx_usb)
1333 return -ENODEV;
1334 if (imx_usb->driver)
1335 return -EBUSY;
1336
1337 /* first hook up the driver ... */
1338 imx_usb->driver = driver;
1339 imx_usb->gadget.dev.driver = &driver-
1340
1341 retval = device_add(&imx_usb->gadget.
1342 if (retval)
1343 goto fail;
1344 retval = driver->bind(&imx_usb->gadge
1345 if (retval) {
1346 D_ERR(imx_usb->dev, "<%s> bin
1347 __func__, driver->dri
1348 device_del(&imx_usb->gadget.d
1349
1350 goto fail;
1351 }
1352
1353 D_INI(imx_usb->dev, "<%s> registered
1354 __func__, driver->driver.name
1355
1356 imx_udc_enable(imx_usb);
1357
1358 return 0;
1359 fail:
1360 imx_usb->driver = NULL;
1361 imx_usb->gadget.dev.driver = NULL;
1362 return retval;
1363 }
1364 EXPORT_SYMBOL(usb_gadget_register_driver);
1365
1366 int usb_gadget_unregister_driver(struct usb_g
1367 {
1368 struct imx_udc_struct *imx_usb = &con
1369
1370 if (!imx_usb)
1371 return -ENODEV;
1372 if (!driver || driver != imx_usb->dri
1373 return -EINVAL;
1374
1375 udc_stop_activity(imx_usb, driver);
1376 imx_udc_disable(imx_usb);
1377 del_timer(&imx_usb->timer);
1378
1379 driver->unbind(&imx_usb->gadget);
1380 imx_usb->gadget.dev.driver = NULL;
1381 imx_usb->driver = NULL;
1382
1383 device_del(&imx_usb->gadget.dev);
1384
1385 D_INI(imx_usb->dev, "<%s> unregistere
1386 __func__, driver->driver.name
1387
1388 return 0;
1389 }
1390 EXPORT_SYMBOL(usb_gadget_unregister_driver);
1391
1392 /********************************************
1393 * Module functions
1394 ********************************************
1395 */
1396
1397 static int __init imx_udc_probe(struct platfo
1398 {
1399 struct imx_udc_struct *imx_usb = &con
1400 struct resource *res;
1401 struct imxusb_platform_data *pdata;
1402 struct clk *clk;
1403 void __iomem *base;
1404 int ret = 0;
1405 int i, res_size;
1406
1407 res = platform_get_resource(pdev, IOR
1408 if (!res) {
1409 dev_err(&pdev->dev, "can't ge
1410 return -ENODEV;
1411 }
1412
1413 pdata = pdev->dev.platform_data;
1414 if (!pdata) {
1415 dev_err(&pdev->dev, "driver n
1416 return -ENODEV;
1417 }
1418
1419 res_size = res->end - res->start + 1;
1420 if (!request_mem_region(res->start, r
1421 dev_err(&pdev->dev, "can't al
1422 res_size, res->start)
1423 return -ENOMEM;
1424 }
1425
1426 if (pdata->init) {
1427 ret = pdata->init(&pdev->dev)
1428 if (ret)
1429 goto fail0;
1430 }
1431
1432 base = ioremap(res->start, res_size);
1433 if (!base) {
1434 dev_err(&pdev->dev, "ioremap
1435 ret = -EIO;
1436 goto fail1;
1437 }
1438
1439 clk = clk_get(NULL, "usbd_clk");
1440 if (IS_ERR(clk)) {
1441 ret = PTR_ERR(clk);
1442 dev_err(&pdev->dev, "can't ge
1443 goto fail2;
1444 }
1445 clk_enable(clk);
1446
1447 if (clk_get_rate(clk) != 48000000) {
1448 D_INI(&pdev->dev,
1449 "Bad USB clock (%d Hz
1450 (int)clk_get_rate(clk
1451 if (clk_set_rate(clk, 4800000
1452 dev_err(&pdev->dev,
1453 "Unable to se
1454 ret = -EIO;
1455 goto fail3;
1456 }
1457 }
1458
1459 for (i = 0; i < IMX_USB_NB_EP + 1; i+
1460 imx_usb->usbd_int[i] = platfo
1461 if (imx_usb->usbd_int[i] < 0)
1462 dev_err(&pdev->dev, "
1463 ret = -ENODEV;
1464 goto fail3;
1465 }
1466 }
1467
1468 for (i = 0; i < IMX_USB_NB_EP + 1; i+
1469 ret = request_irq(imx_usb->us
1470 IRQF_DIS
1471 if (ret) {
1472 dev_err(&pdev->dev, "
1473 imx_usb->usbd
1474 for (--i; i >= 0; i--
1475 free_irq(imx_
1476 goto fail3;
1477 }
1478 }
1479
1480 imx_usb->res = res;
1481 imx_usb->base = base;
1482 imx_usb->clk = clk;
1483 imx_usb->dev = &pdev->dev;
1484
1485 device_initialize(&imx_usb->gadget.de
1486
1487 imx_usb->gadget.dev.parent = &pdev->d
1488 imx_usb->gadget.dev.dma_mask = pdev->
1489
1490 platform_set_drvdata(pdev, imx_usb);
1491
1492 usb_init_data(imx_usb);
1493 imx_udc_init(imx_usb);
1494
1495 init_timer(&imx_usb->timer);
1496 imx_usb->timer.function = handle_conf
1497 imx_usb->timer.data = (unsigned long)
1498
1499 return 0;
1500
1501 fail3:
1502 clk_put(clk);
1503 clk_disable(clk);
1504 fail2:
1505 iounmap(base);
1506 fail1:
1507 if (pdata->exit)
1508 pdata->exit(&pdev->dev);
1509 fail0:
1510 release_mem_region(res->start, res_si
1511 return ret;
1512 }
1513
1514 static int __exit imx_udc_remove(struct platf
1515 {
1516 struct imx_udc_struct *imx_usb = plat
1517 struct imxusb_platform_data *pdata =
1518 int i;
1519
1520 imx_udc_disable(imx_usb);
1521 del_timer(&imx_usb->timer);
1522
1523 for (i = 0; i < IMX_USB_NB_EP + 1; i+
1524 free_irq(imx_usb->usbd_int[i]
1525
1526 clk_put(imx_usb->clk);
1527 clk_disable(imx_usb->clk);
1528 iounmap(imx_usb->base);
1529
1530 release_mem_region(imx_usb->res->star
1531 imx_usb->res->end - imx_usb->
1532
1533 if (pdata->exit)
1534 pdata->exit(&pdev->dev);
1535
1536 platform_set_drvdata(pdev, NULL);
1537
1538 return 0;
1539 }
1540
1541 /*-------------------------------------------
1542
1543 #ifdef CONFIG_PM
1544 #define imx_udc_suspend NULL
1545 #define imx_udc_resume NULL
1546 #else
1547 #define imx_udc_suspend NULL
1548 #define imx_udc_resume NULL
1549 #endif
1550
1551 /*-------------------------------------------
1552
1553 static struct platform_driver udc_driver = {
1554 .driver = {
1555 .name = driver_name,
1556 .owner = THIS_MODULE,
1557 },
1558 .remove = __exit_p(imx_udc_re
1559 .suspend = imx_udc_suspend,
1560 .resume = imx_udc_resume,
1561 };
1562
1563 static int __init udc_init(void)
1564 {
1565 return platform_driver_probe(&udc_dri
1566 }
1567 module_init(udc_init);
1568
1569 static void __exit udc_exit(void)
1570 {
1571 platform_driver_unregister(&udc_drive
1572 }
1573 module_exit(udc_exit);
1574
1575 MODULE_DESCRIPTION("IMX USB Device Controller
1576 MODULE_AUTHOR("Darius Augulis <augulis.darius
1577 MODULE_LICENSE("GPL");
1578 MODULE_ALIAS("platform:imx_udc");
1579
| This page was automatically generated by the LXR engine. |