1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <common.h>
30#include <asm/io.h>
31#ifdef CONFIG_OMAP_SX1
32#include <i2c.h>
33#endif
34#include <usbdevice.h>
35#include <usb/omap1510_udc.h>
36
37#include "ep0.h"
38
39
40#define UDC_INIT_MDELAY 80
41#define UDC_MAX_ENDPOINTS 31
42
43
44#if 1
45#define UDCDBG(str)
46#define UDCDBGA(fmt,args...)
47#else
48#define UDCDBG(str) serial_printf("[%s] %s:%d: " str "\n", __FILE__,__FUNCTION__,__LINE__)
49#define UDCDBGA(fmt,args...) serial_printf("[%s] %s:%d: " fmt "\n", __FILE__,__FUNCTION__,__LINE__, ##args)
50#endif
51
52#if 1
53#define UDCREG(name)
54#define UDCREGL(name)
55#else
56#define UDCREG(name) serial_printf("%s():%d: %s[%08x]=%.4x\n",__FUNCTION__,__LINE__, (#name), name, inw(name))
57#define UDCREGL(name) serial_printf("%s():%d: %s[%08x]=%.8x\n",__FUNCTION__,__LINE__, (#name), name, inl(name))
58#endif
59
60
61static struct urb *ep0_urb = NULL;
62
63static struct usb_device_instance *udc_device;
64static u16 udc_devstat = 0;
65static u32 udc_interrupts = 0;
66
67static void udc_stall_ep (unsigned int ep_addr);
68
69
70static struct usb_endpoint_instance *omap1510_find_ep (int ep)
71{
72 int i;
73
74 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
75 if (udc_device->bus->endpoint_array[i].endpoint_address == ep)
76 return &udc_device->bus->endpoint_array[i];
77 }
78 return NULL;
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96static void omap1510_prepare_endpoint_for_rx (int ep_addr)
97{
98 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
99
100 UDCDBGA ("omap1510_prepare_endpoint %x", ep_addr);
101 if (((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) {
102 if ((inw (UDC_EP_RX (ep_num)) &
103 (UDC_EPn_RX_Valid | UDC_EPn_RX_Iso)) ==
104 UDC_EPn_RX_Valid) {
105
106 outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
107 outw (UDC_Set_FIFO_En, UDC_CTRL);
108 outw (0, UDC_EP_NUM);
109 }
110 }
111}
112
113
114
115
116
117static void omap1510_configure_endpoints (struct usb_device_instance *device)
118{
119 int ep;
120 struct usb_bus_instance *bus;
121 struct usb_endpoint_instance *endpoint;
122 unsigned short ep_ptr;
123 unsigned short ep_size;
124 unsigned short ep_isoc;
125 unsigned short ep_doublebuffer;
126 int ep_addr;
127 int packet_size;
128 int buffer_size;
129 int attributes;
130
131 bus = device->bus;
132
133
134
135
136
137
138 ep_ptr = 8;
139
140 for (ep = 0; ep < bus->max_endpoints; ep++) {
141 endpoint = bus->endpoint_array + ep;
142 ep_addr = endpoint->endpoint_address;
143 if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
144
145 packet_size = endpoint->tx_packetSize;
146 attributes = endpoint->tx_attributes;
147 } else {
148
149 packet_size = endpoint->rcv_packetSize;
150 attributes = endpoint->rcv_attributes;
151 }
152
153 switch (packet_size) {
154 case 0:
155 ep_size = 0;
156 break;
157 case 8:
158 ep_size = 0;
159 break;
160 case 16:
161 ep_size = 1;
162 break;
163 case 32:
164 ep_size = 2;
165 break;
166 case 64:
167 ep_size = 3;
168 break;
169 case 128:
170 ep_size = 4;
171 break;
172 case 256:
173 ep_size = 5;
174 break;
175 case 512:
176 ep_size = 6;
177 break;
178 default:
179 UDCDBGA ("ep 0x%02x has bad packet size %d",
180 ep_addr, packet_size);
181 packet_size = 0;
182 ep_size = 0;
183 break;
184 }
185
186 switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
187 case USB_ENDPOINT_XFER_CONTROL:
188 case USB_ENDPOINT_XFER_BULK:
189 case USB_ENDPOINT_XFER_INT:
190 default:
191
192
193
194
195 ep_doublebuffer = 0;
196 ep_isoc = 0;
197 if (packet_size > 64)
198 packet_size = 0;
199 if (!ep || !ep_doublebuffer)
200 buffer_size = packet_size;
201 else
202 buffer_size = packet_size * 2;
203 break;
204 case USB_ENDPOINT_XFER_ISOC:
205
206
207
208
209
210
211 ep_doublebuffer = 0;
212 ep_isoc = 1;
213 buffer_size = packet_size * 2;
214 break;
215 }
216
217
218 if ((ep_ptr + buffer_size) > 2048) {
219 UDCDBGA ("out of packet RAM for ep 0x%02x buf size %d", ep_addr, buffer_size);
220 buffer_size = packet_size = 0;
221 }
222
223
224
225
226 if (!ep && ((packet_size < 8) || (packet_size > 64))) {
227 buffer_size = packet_size = 64;
228 ep_size = 3;
229 }
230
231 if (!ep) {
232
233 outw ((ep_size << 12) | (ep_ptr >> 3), UDC_EP0);
234
235
236 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
237
238 if (packet_size) {
239 outw ((1 << 15) | (ep_doublebuffer << 14) |
240 (ep_size << 12) | (ep_isoc << 11) |
241 (ep_ptr >> 3),
242 UDC_EP_TX (ep_addr &
243 USB_ENDPOINT_NUMBER_MASK));
244 UDCDBGA ("IN ep %d buffer offset 0x%03x"
245 " packet size 0x%03x",
246 ep_addr & USB_ENDPOINT_NUMBER_MASK,
247 ep_ptr, packet_size);
248 } else {
249 outw (0,
250 UDC_EP_TX (ep_addr &
251 USB_ENDPOINT_NUMBER_MASK));
252 }
253 } else {
254
255 if (packet_size) {
256 outw ((1 << 15) | (ep_doublebuffer << 14) |
257 (ep_size << 12) | (ep_isoc << 11) |
258 (ep_ptr >> 3),
259 UDC_EP_RX (ep_addr &
260 USB_ENDPOINT_NUMBER_MASK));
261 UDCDBGA ("OUT ep %d buffer offset 0x%03x"
262 " packet size 0x%03x",
263 ep_addr & USB_ENDPOINT_NUMBER_MASK,
264 ep_ptr, packet_size);
265 } else {
266 outw (0,
267 UDC_EP_RX (ep_addr &
268 USB_ENDPOINT_NUMBER_MASK));
269 }
270 }
271 ep_ptr += buffer_size;
272 }
273}
274
275
276
277
278
279static void omap1510_deconfigure_device (void)
280{
281 int epnum;
282
283 UDCDBG ("clear Cfg_Lock");
284 outw (inw (UDC_SYSCON1) & ~UDC_Cfg_Lock, UDC_SYSCON1);
285 UDCREG (UDC_SYSCON1);
286
287
288 for (epnum = 1; epnum <= 15; epnum++) {
289 outw (0, UDC_EP_RX (epnum));
290 outw (0, UDC_EP_TX (epnum));
291 }
292}
293
294
295
296
297
298static void omap1510_configure_device (struct usb_device_instance *device)
299{
300 omap1510_configure_endpoints (device);
301
302
303
304
305
306
307
308 UDCDBG ("set Cfg_Lock");
309 outw (inw (UDC_SYSCON1) | UDC_Cfg_Lock, UDC_SYSCON1);
310 UDCREG (UDC_SYSCON1);
311}
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329static void omap1510_write_noniso_tx_fifo (struct usb_endpoint_instance
330 *endpoint)
331{
332 struct urb *urb = endpoint->tx_urb;
333
334 if (urb) {
335 unsigned int last, i;
336
337 UDCDBGA ("urb->buffer %p, buffer_length %d, actual_length %d",
338 urb->buffer, urb->buffer_length, urb->actual_length);
339 if ((last =
340 MIN (urb->actual_length - endpoint->sent,
341 endpoint->tx_packetSize))) {
342 u8 *cp = urb->buffer + endpoint->sent;
343
344 UDCDBGA ("endpoint->sent %d, tx_packetSize %d, last %d", endpoint->sent, endpoint->tx_packetSize, last);
345
346 if (((u32) cp & 1) == 0) {
347 outsw (UDC_DATA, cp, last >> 1);
348 } else {
349 for (i = 0; i < (last >> 1); i++) {
350 u16 w = ((u16) cp[2 * i + 1] << 8) |
351 (u16) cp[2 * i];
352 outw (w, UDC_DATA);
353 }
354 }
355 if (last & 1) {
356 outb (*(cp + last - 1), UDC_DATA);
357 }
358 }
359 endpoint->last = last;
360 }
361}
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379static int omap1510_read_noniso_rx_fifo (struct usb_endpoint_instance
380 *endpoint)
381{
382 struct urb *urb = endpoint->rcv_urb;
383 int len = 0;
384
385 if (urb) {
386 len = inw (UDC_RXFSTAT);
387
388 if (len) {
389 unsigned char *cp = urb->buffer + urb->actual_length;
390
391 insw (UDC_DATA, cp, len >> 1);
392 if (len & 1)
393 *(cp + len - 1) = inb (UDC_DATA);
394 }
395 }
396 return len;
397}
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415static void omap1510_prepare_for_control_write_status (struct urb *urb)
416{
417 struct usb_device_request *request = &urb->device_request;;
418
419
420 if (request->bRequest == USB_REQ_SET_CONFIGURATION) {
421 int configuration = le16_to_cpu (request->wValue) & 0xff;
422 unsigned short devstat = inw (UDC_DEVSTAT);
423
424 if ((devstat & (UDC_ADD | UDC_CFG)) == UDC_ADD) {
425
426 if (configuration) {
427
428
429
430
431 outw (UDC_Dev_Cfg, UDC_SYSCON2);
432 }
433 } else if ((devstat & UDC_CFG) == UDC_CFG) {
434
435 if (!configuration) {
436
437 outw (UDC_Clr_Cfg, UDC_SYSCON2);
438 }
439 }
440 }
441
442
443 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
444
445 outw (UDC_Clr_EP, UDC_CTRL);
446
447 outw (UDC_Set_FIFO_En, UDC_CTRL);
448
449 outw (UDC_EP_Dir, UDC_EP_NUM);
450}
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498static void udc_state_transition_up (usb_device_state_t initial,
499 usb_device_state_t final)
500{
501 if (initial < final) {
502 switch (initial) {
503 case STATE_ATTACHED:
504 usbd_device_event_irq (udc_device,
505 DEVICE_HUB_CONFIGURED, 0);
506 if (final == STATE_POWERED)
507 break;
508 case STATE_POWERED:
509 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
510 if (final == STATE_DEFAULT)
511 break;
512 case STATE_DEFAULT:
513 usbd_device_event_irq (udc_device,
514 DEVICE_ADDRESS_ASSIGNED, 0);
515 if (final == STATE_ADDRESSED)
516 break;
517 case STATE_ADDRESSED:
518 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
519 0);
520 case STATE_CONFIGURED:
521 break;
522 default:
523 break;
524 }
525 }
526}
527
528static void udc_state_transition_down (usb_device_state_t initial,
529 usb_device_state_t final)
530{
531 if (initial > final) {
532 switch (initial) {
533 case STATE_CONFIGURED:
534 usbd_device_event_irq (udc_device, DEVICE_DE_CONFIGURED, 0);
535 if (final == STATE_ADDRESSED)
536 break;
537 case STATE_ADDRESSED:
538 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
539 if (final == STATE_DEFAULT)
540 break;
541 case STATE_DEFAULT:
542 usbd_device_event_irq (udc_device, DEVICE_POWER_INTERRUPTION, 0);
543 if (final == STATE_POWERED)
544 break;
545 case STATE_POWERED:
546 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, 0);
547 case STATE_ATTACHED:
548 break;
549 default:
550 break;
551 }
552 }
553}
554
555
556
557
558static void omap1510_udc_state_changed (void)
559{
560 u16 bits;
561 u16 devstat = inw (UDC_DEVSTAT);
562
563 UDCDBGA ("state changed, devstat %x, old %x", devstat, udc_devstat);
564
565 bits = devstat ^ udc_devstat;
566 if (bits) {
567 if (bits & UDC_ATT) {
568 if (devstat & UDC_ATT) {
569 UDCDBG ("device attached and powered");
570 udc_state_transition_up (udc_device->device_state, STATE_POWERED);
571 } else {
572 UDCDBG ("device detached or unpowered");
573 udc_state_transition_down (udc_device->device_state, STATE_ATTACHED);
574 }
575 }
576 if (bits & UDC_USB_Reset) {
577 if (devstat & UDC_USB_Reset) {
578 UDCDBG ("device reset in progess");
579 udc_state_transition_down (udc_device->device_state, STATE_POWERED);
580 } else {
581 UDCDBG ("device reset completed");
582 }
583 }
584 if (bits & UDC_DEF) {
585 if (devstat & UDC_DEF) {
586 UDCDBG ("device entering default state");
587 udc_state_transition_up (udc_device->device_state, STATE_DEFAULT);
588 } else {
589 UDCDBG ("device leaving default state");
590 udc_state_transition_down (udc_device->device_state, STATE_POWERED);
591 }
592 }
593 if (bits & UDC_SUS) {
594 if (devstat & UDC_SUS) {
595 UDCDBG ("entering suspended state");
596 usbd_device_event_irq (udc_device, DEVICE_BUS_INACTIVE, 0);
597 } else {
598 UDCDBG ("leaving suspended state");
599 usbd_device_event_irq (udc_device, DEVICE_BUS_ACTIVITY, 0);
600 }
601 }
602 if (bits & UDC_R_WK_OK) {
603 UDCDBGA ("remote wakeup %s", (devstat & UDC_R_WK_OK)
604 ? "enabled" : "disabled");
605 }
606 if (bits & UDC_ADD) {
607 if (devstat & UDC_ADD) {
608 UDCDBG ("default -> addressed");
609 udc_state_transition_up (udc_device->device_state, STATE_ADDRESSED);
610 } else {
611 UDCDBG ("addressed -> default");
612 udc_state_transition_down (udc_device->device_state, STATE_DEFAULT);
613 }
614 }
615 if (bits & UDC_CFG) {
616 if (devstat & UDC_CFG) {
617 UDCDBG ("device configured");
618
619
620
621
622
623
624 udc_state_transition_up (udc_device->device_state, STATE_CONFIGURED);
625 } else {
626 UDCDBG ("device deconfigured");
627 udc_state_transition_down (udc_device->device_state, STATE_ADDRESSED);
628 }
629 }
630 }
631
632
633 outw (UDC_DS_Chg, UDC_IRQ_SRC);
634
635
636 udc_devstat = devstat;
637}
638
639
640
641
642static void omap1510_udc_setup (struct usb_endpoint_instance *endpoint)
643{
644 UDCDBG ("-> Entering device setup");
645
646 do {
647 const int setup_pktsize = 8;
648 unsigned char *datap =
649 (unsigned char *) &ep0_urb->device_request;
650
651
652 outw (UDC_Setup_Sel, UDC_EP_NUM);
653
654
655 insb (UDC_DATA, datap, setup_pktsize);
656
657 UDCDBGA ("EP0 setup read [%x %x %x %x %x %x %x %x]",
658 *(datap + 0), *(datap + 1), *(datap + 2),
659 *(datap + 3), *(datap + 4), *(datap + 5),
660 *(datap + 6), *(datap + 7));
661
662
663 outw (0, UDC_EP_NUM);
664 } while (inw (UDC_IRQ_SRC) & UDC_Setup);
665
666
667 if (ep0_recv_setup (ep0_urb)) {
668
669 udc_stall_ep (0);
670 UDCDBG ("can't parse setup packet, still waiting for setup");
671 return;
672 }
673
674
675 if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
676 == USB_REQ_HOST2DEVICE) {
677 UDCDBG ("control write on EP0");
678 if (le16_to_cpu (ep0_urb->device_request.wLength)) {
679
680
681
682
683
684
685
686
687
688
689#if 0
690
691
692
693 ep0_urb->actual_length = 0;
694 outw (0, UDC_EP_NUM);
695
696 outw (UDC_Set_FIFO_En, UDC_CTRL);
697#else
698
699 UDCDBG ("Stalling unsupported EP0 control write data "
700 "stage.");
701 udc_stall_ep (0);
702#endif
703 } else {
704 omap1510_prepare_for_control_write_status (ep0_urb);
705 }
706 } else {
707 UDCDBG ("control read on EP0");
708
709
710
711
712 endpoint->tx_urb = ep0_urb;
713 endpoint->sent = 0;
714
715 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
716
717
718
719
720 omap1510_write_noniso_tx_fifo (endpoint);
721
722 outw (UDC_Set_FIFO_En, UDC_CTRL);
723
724 outw (UDC_EP_Dir, UDC_EP_NUM);
725 }
726
727 UDCDBG ("<- Leaving device setup");
728}
729
730
731
732
733static void omap1510_udc_ep0_rx (struct usb_endpoint_instance *endpoint)
734{
735 unsigned short status;
736
737 UDCDBG ("RX on EP0");
738
739 outw (UDC_EP_Sel, UDC_EP_NUM);
740
741 status = inw (UDC_STAT_FLG);
742
743 if (status & UDC_ACK) {
744
745 if ((ep0_urb->device_request.bmRequestType
746 & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
747
748
749
750
751
752
753
754
755 outw (UDC_Clr_EP, UDC_CTRL);
756
757
758 outw (0, UDC_EP_NUM);
759
760 UDCDBG ("Stalling unexpected EP0 control write "
761 "data stage packet");
762 udc_stall_ep (0);
763 } else {
764
765
766
767 UDCDBG ("ACK on EP0 control read status stage packet");
768
769 outw (0, UDC_EP_NUM);
770 }
771 } else if (status & UDC_STALL) {
772 UDCDBG ("EP0 stall during RX");
773
774 outw (0, UDC_EP_NUM);
775 } else {
776
777 outw (0, UDC_EP_NUM);
778 }
779}
780
781
782
783
784static void omap1510_udc_ep0_tx (struct usb_endpoint_instance *endpoint)
785{
786 unsigned short status;
787 struct usb_device_request *request = &ep0_urb->device_request;
788
789 UDCDBG ("TX on EP0");
790
791 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
792
793 status = inw (UDC_STAT_FLG);
794 if (status & UDC_ACK) {
795
796 if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
797 USB_REQ_HOST2DEVICE) {
798
799
800
801 UDCDBG ("ACK on EP0 control write status stage packet");
802
803 outw (UDC_EP_Dir, UDC_EP_NUM);
804 } else {
805
806
807
808 int wLength = le16_to_cpu (request->wLength);
809
810
811
812
813 endpoint->sent += endpoint->last;
814
815
816
817
818
819
820
821
822
823 if ((endpoint->sent == ep0_urb->actual_length)
824 && ((ep0_urb->actual_length == wLength)
825 || (endpoint->last !=
826 endpoint->tx_packetSize))) {
827
828 UDCDBG ("control read data stage complete");
829
830 outw (UDC_EP_Dir, UDC_EP_NUM);
831
832
833
834 outw (UDC_EP_Sel, UDC_EP_NUM);
835
836 outw (UDC_Clr_EP, UDC_CTRL);
837
838 outw (UDC_Set_FIFO_En, UDC_CTRL);
839
840 outw (0, UDC_EP_NUM);
841 } else {
842
843
844
845
846 UDCDBG ("ACK control read data stage packet");
847 omap1510_write_noniso_tx_fifo (endpoint);
848
849 outw (UDC_Set_FIFO_En, UDC_CTRL);
850
851 outw (UDC_EP_Dir, UDC_EP_NUM);
852 }
853 }
854 } else if (status & UDC_STALL) {
855 UDCDBG ("EP0 stall during TX");
856
857 outw (UDC_EP_Dir, UDC_EP_NUM);
858 } else {
859
860 outw (UDC_EP_Dir, UDC_EP_NUM);
861 }
862}
863
864
865
866
867
868
869static void omap1510_udc_epn_rx (int ep)
870{
871 unsigned short status;
872
873
874 status = inw (UDC_STAT_FLG);
875
876 if (status & UDC_ACK) {
877 int nbytes;
878 struct usb_endpoint_instance *endpoint =
879 omap1510_find_ep (ep);
880
881 nbytes = omap1510_read_noniso_rx_fifo (endpoint);
882 usbd_rcv_complete (endpoint, nbytes, 0);
883
884
885 outw (UDC_Set_FIFO_En, UDC_CTRL);
886 } else if (status & UDC_STALL) {
887 UDCDBGA ("STALL on RX endpoint %d", ep);
888 } else if (status & UDC_NAK) {
889 UDCDBGA ("NAK on RX ep %d", ep);
890 } else {
891 serial_printf ("omap-bi: RX on ep %d with status %x", ep,
892 status);
893 }
894}
895
896
897
898
899
900
901static void omap1510_udc_epn_tx (int ep)
902{
903 unsigned short status;
904
905
906
907
908 status = inw (UDC_STAT_FLG);
909
910 if (status & UDC_ACK) {
911 struct usb_endpoint_instance *endpoint =
912 omap1510_find_ep (ep);
913
914
915
916
917
918 if (endpoint->tx_urb
919 && (endpoint->last == endpoint->tx_packetSize)
920 && (endpoint->tx_urb->actual_length - endpoint->sent -
921 endpoint->last == 0)) {
922
923 endpoint->sent += endpoint->last;
924
925 omap1510_write_noniso_tx_fifo (endpoint);
926
927 outw (UDC_Set_FIFO_En, UDC_CTRL);
928 } else if (endpoint->tx_urb
929 && endpoint->tx_urb->actual_length) {
930
931 usbd_tx_complete (endpoint);
932
933
934
935 if (endpoint->tx_urb
936 && endpoint->tx_urb->actual_length) {
937
938 omap1510_write_noniso_tx_fifo (endpoint);
939
940 outw (UDC_Set_FIFO_En, UDC_CTRL);
941 }
942 }
943 } else if (status & UDC_STALL) {
944 UDCDBGA ("STALL on TX endpoint %d", ep);
945 } else if (status & UDC_NAK) {
946 UDCDBGA ("NAK on TX endpoint %d", ep);
947 } else {
948
949 }
950}
951
952
953
954
955
956
957
958
959
960void omap1510_udc_irq (void)
961{
962 u16 irq_src = inw (UDC_IRQ_SRC);
963 int valid_irq = 0;
964
965 if (!(irq_src & ~UDC_SOF_Flg))
966 return;
967
968 UDCDBGA ("< IRQ #%d start >- %x", udc_interrupts, irq_src);
969
970
971 if (irq_src & UDC_DS_Chg) {
972
973 omap1510_udc_state_changed ();
974 valid_irq++;
975 }
976 if (irq_src & UDC_EP0_RX) {
977
978 outw (UDC_EP0_RX, UDC_IRQ_SRC);
979 omap1510_udc_ep0_rx (udc_device->bus->endpoint_array + 0);
980 valid_irq++;
981 }
982 if (irq_src & UDC_EP0_TX) {
983
984 outw (UDC_EP0_TX, UDC_IRQ_SRC);
985 omap1510_udc_ep0_tx (udc_device->bus->endpoint_array + 0);
986 valid_irq++;
987 }
988 if (irq_src & UDC_Setup) {
989
990 omap1510_udc_setup (udc_device->bus->endpoint_array + 0);
991 valid_irq++;
992 }
993
994
995 UDCDBGA ("< IRQ #%d end >", udc_interrupts);
996 udc_interrupts++;
997}
998
999
1000void omap1510_udc_noniso_irq (void)
1001{
1002 unsigned short epnum;
1003 unsigned short irq_src = inw (UDC_IRQ_SRC);
1004 int valid_irq = 0;
1005
1006 if (!(irq_src & (UDC_EPn_RX | UDC_EPn_TX)))
1007 return;
1008
1009 UDCDBGA ("non-ISO IRQ, IRQ_SRC %x", inw (UDC_IRQ_SRC));
1010
1011 if (irq_src & UDC_EPn_RX) {
1012
1013 epnum = (inw (UDC_EPN_STAT) & 0x0f00) >> 8;
1014 UDCDBGA ("RX on ep %x", epnum);
1015
1016
1017 outw (UDC_EPn_RX, UDC_IRQ_SRC);
1018
1019 if (epnum) {
1020
1021 outw (UDC_EP_Sel | epnum, UDC_EP_NUM);
1022
1023 omap1510_udc_epn_rx (epnum);
1024
1025
1026 outw (epnum, UDC_EP_NUM);
1027 }
1028 valid_irq++;
1029 }
1030 if (irq_src & UDC_EPn_TX) {
1031
1032 epnum = (inw (UDC_EPN_STAT) & 0x000f) | USB_DIR_IN;
1033 UDCDBGA ("TX on ep %x", epnum);
1034
1035
1036 outw (UDC_EPn_TX, UDC_IRQ_SRC);
1037
1038 if (epnum) {
1039
1040 outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
1041
1042 omap1510_udc_epn_tx (epnum);
1043
1044
1045 outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
1046 }
1047 valid_irq++;
1048 }
1049 if (!valid_irq)
1050 serial_printf (": unknown non-ISO interrupt, IRQ_SRC %.4x\n",
1051 irq_src);
1052}
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064int udc_endpoint_write (struct usb_endpoint_instance *endpoint)
1065{
1066 unsigned short epnum =
1067 endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
1068
1069 UDCDBGA ("Starting transmit on ep %x", epnum);
1070
1071 if (endpoint->tx_urb) {
1072
1073 outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
1074
1075 omap1510_write_noniso_tx_fifo (endpoint);
1076
1077 outw (UDC_Set_FIFO_En, UDC_CTRL);
1078
1079 outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
1080 }
1081
1082 return 0;
1083}
1084
1085
1086int udc_init (void)
1087{
1088 u16 udc_rev;
1089 uchar value;
1090 ulong gpio;
1091 int i;
1092
1093
1094 for (i = 0; i < UDC_INIT_MDELAY; i++) udelay(1000);
1095
1096 udc_device = NULL;
1097
1098 UDCDBG ("starting");
1099
1100
1101
1102 UDCREG (ARM_RSTCT2);
1103
1104
1105
1106
1107
1108
1109 outw ((1 << 4) | (1 << 5), CLOCK_CTRL);
1110 UDCREG (CLOCK_CTRL);
1111
1112#ifdef CONFIG_OMAP1510
1113
1114
1115
1116
1117
1118
1119
1120 outw (0x0008, APLL_CTRL);
1121 UDCREG (APLL_CTRL);
1122
1123 outw (0x2210, DPLL_CTRL);
1124 UDCREG (DPLL_CTRL);
1125#endif
1126
1127
1128
1129
1130
1131
1132 outw ((1 << 4) | (1 << 3) | 1 | (inw(SOFT_REQ)), SOFT_REQ);
1133
1134
1135 udelay (1000);
1136
1137
1138 udc_rev = inw (UDC_REV) & 0xff;
1139#ifdef CONFIG_OMAP1510
1140 printf ("USB: TI OMAP1510 USB function module rev %d.%d\n",
1141 udc_rev >> 4, udc_rev & 0xf);
1142#endif
1143
1144#ifdef CONFIG_OMAP1610
1145 printf ("USB: TI OMAP5912 USB function module rev %d.%d\n",
1146 udc_rev >> 4, udc_rev & 0xf);
1147#endif
1148
1149#ifdef CONFIG_OMAP_SX1
1150 i2c_read (0x32, 0x04, 1, &value, 1);
1151 value |= 0x04;
1152 i2c_write (0x32, 0x04, 1, &value, 1);
1153
1154 i2c_read (0x32, 0x03, 1, &value, 1);
1155 value |= 0x01;
1156 i2c_write (0x32, 0x03, 1, &value, 1);
1157
1158 gpio = inl(GPIO_PIN_CONTROL_REG);
1159 gpio |= 0x0002;
1160 gpio |= 0x0800;
1161 gpio |= 0x8000;
1162 outl (gpio, GPIO_PIN_CONTROL_REG);
1163
1164 gpio = inl(GPIO_DIR_CONTROL_REG);
1165 gpio &= ~0x0002;
1166 gpio &= ~0x0800;
1167 gpio &= ~0x8000;
1168 outl (gpio, GPIO_DIR_CONTROL_REG);
1169
1170 gpio = inl(GPIO_DATA_OUTPUT_REG);
1171 gpio |= 0x0002;
1172 gpio &= ~0x0800;
1173 gpio &= ~0x8000;
1174 outl (gpio, GPIO_DATA_OUTPUT_REG);
1175#endif
1176
1177
1178
1179
1180
1181
1182 outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1183 outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1184 UDCREGL (FUNC_MUX_CTRL_0);
1185
1186
1187
1188
1189
1190 UDCDBG ("disable USB interrupts");
1191 outw (0, UDC_IRQ_EN);
1192 UDCREG (UDC_IRQ_EN);
1193
1194 UDCDBG ("disable USB DMA");
1195 outw (0, UDC_DMA_IRQ_EN);
1196 UDCREG (UDC_DMA_IRQ_EN);
1197
1198 UDCDBG ("initialize SYSCON1");
1199 outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
1200 UDCREG (UDC_SYSCON1);
1201
1202 return 0;
1203}
1204
1205
1206static void udc_stall_ep (unsigned int ep_addr)
1207{
1208
1209 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1210
1211 UDCDBGA ("stall ep_addr %d", ep_addr);
1212
1213
1214
1215
1216
1217
1218
1219 if (!ep_num) {
1220 outw (UDC_Stall_Cmd, UDC_SYSCON2);
1221 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1222 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1223
1224 outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
1225 outw (UDC_Set_Halt, UDC_CTRL);
1226 outw (ep_num, UDC_EP_NUM);
1227 }
1228 } else {
1229 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1230
1231 outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
1232 outw (UDC_Set_Halt, UDC_CTRL);
1233 outw (ep_num, UDC_EP_NUM);
1234 }
1235 }
1236}
1237
1238
1239#if 0
1240static void udc_reset_ep (unsigned int ep_addr)
1241{
1242
1243 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1244
1245 UDCDBGA ("reset ep_addr %d", ep_addr);
1246
1247 if (!ep_num) {
1248
1249 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1250 UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
1251 inw (UDC_EP_RX (ep_num)));
1252 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1253
1254 outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
1255 outw (UDC_Reset_EP, UDC_CTRL);
1256 outw (ep_num, UDC_EP_NUM);
1257 UDCDBGA ("OUT endpoint %d reset", ep_num);
1258 }
1259 } else {
1260 UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
1261 inw (UDC_EP_TX (ep_num)));
1262
1263
1264
1265
1266
1267#if 0
1268 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1269
1270 outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
1271 outw (UDC_Reset_EP, UDC_CTRL);
1272 outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
1273 UDCDBGA ("IN endpoint %d reset", ep_num);
1274 }
1275#endif
1276 }
1277}
1278#endif
1279
1280
1281
1282
1283
1284
1285
1286
1287#if 0
1288int udc_check_ep (int logical_endpoint, int packetsize)
1289{
1290 if ((logical_endpoint == 0x80) ||
1291 ((logical_endpoint & 0x8f) != logical_endpoint)) {
1292 return 0;
1293 }
1294
1295 switch (packetsize) {
1296 case 8:
1297 case 16:
1298 case 32:
1299 case 64:
1300 case 128:
1301 case 256:
1302 case 512:
1303 break;
1304 default:
1305 return 0;
1306 }
1307
1308 return EP_ADDR_TO_PHYS_EP (logical_endpoint);
1309}
1310#endif
1311
1312
1313
1314
1315
1316
1317void udc_setup_ep (struct usb_device_instance *device,
1318 unsigned int ep, struct usb_endpoint_instance *endpoint)
1319{
1320 UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335 if (ep == 1) {
1336 omap1510_deconfigure_device ();
1337 omap1510_configure_device (device);
1338 }
1339
1340 if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
1341 int ep_addr = endpoint->endpoint_address;
1342
1343 if (!ep_addr) {
1344
1345 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1346
1347 } else {
1348 if (endpoint->rcv_packetSize) {
1349
1350
1351
1352
1353
1354
1355 omap1510_prepare_endpoint_for_rx (ep_addr);
1356 }
1357 }
1358 }
1359}
1360
1361
1362
1363
1364
1365
1366
1367#if 0
1368void udc_disable_ep (unsigned int ep_addr)
1369{
1370
1371 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1372 struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr);
1373
1374 UDCDBGA ("disable ep_addr %d", ep_addr);
1375
1376 if (!ep_num) {
1377 ;
1378 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1379 if (endpoint->tx_packetSize) {
1380
1381
1382 endpoint->tx_urb = NULL;
1383 }
1384 } else {
1385 if (endpoint->rcv_packetSize) {
1386
1387
1388 endpoint->rcv_urb = NULL;
1389 }
1390 }
1391}
1392#endif
1393
1394
1395
1396
1397
1398
1399
1400
1401#if 0
1402int udc_connected (void)
1403{
1404 return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
1405}
1406#endif
1407
1408
1409void udc_connect (void)
1410{
1411 UDCDBG ("connect, enable Pullup");
1412 outl (0x00000018, FUNC_MUX_CTRL_D);
1413}
1414
1415
1416void udc_disconnect (void)
1417{
1418 UDCDBG ("disconnect, disable Pullup");
1419 outl (0x00000000, FUNC_MUX_CTRL_D);
1420}
1421
1422
1423
1424
1425
1426
1427
1428
1429#if 0
1430void udc_disable_interrupts (struct usb_device_instance *device)
1431{
1432 UDCDBG ("disabling all interrupts");
1433 outw (0, UDC_IRQ_EN);
1434}
1435#endif
1436
1437
1438
1439
1440
1441
1442#if 0
1443int udc_ep0_packetsize (void)
1444{
1445 return EP0_PACKETSIZE;
1446}
1447#endif
1448
1449
1450void udc_enable (struct usb_device_instance *device)
1451{
1452 UDCDBGA ("enable device %p, status %d", device, device->status);
1453
1454
1455 udc_devstat = 0;
1456
1457
1458 udc_device = device;
1459
1460
1461 if (!ep0_urb) {
1462 ep0_urb =
1463 usbd_alloc_urb (udc_device,
1464 udc_device->bus->endpoint_array);
1465 } else {
1466 serial_printf ("udc_enable: ep0_urb already allocated %p\n",
1467 ep0_urb);
1468 }
1469
1470 UDCDBG ("Check clock status");
1471 UDCREG (STATUS_REQ);
1472
1473
1474
1475
1476
1477
1478 outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
1479 FUNC_MUX_CTRL_0);
1480 UDCREGL (FUNC_MUX_CTRL_0);
1481
1482 omap1510_configure_device (device);
1483}
1484
1485
1486void udc_disable (void)
1487{
1488 UDCDBG ("disable UDC");
1489
1490 omap1510_deconfigure_device ();
1491
1492
1493
1494
1495
1496
1497 outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1498 outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1499 UDCREGL (FUNC_MUX_CTRL_0);
1500
1501
1502 if (ep0_urb) {
1503
1504 ep0_urb = NULL;
1505 }
1506
1507
1508
1509
1510
1511
1512
1513}
1514
1515
1516
1517
1518void udc_startup_events (struct usb_device_instance *device)
1519{
1520
1521 usbd_device_event_irq (device, DEVICE_INIT, 0);
1522
1523
1524
1525
1526 usbd_device_event_irq (device, DEVICE_CREATE, 0);
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538 udc_enable (device);
1539}
1540
1541
1542
1543
1544void udc_irq(void)
1545{
1546
1547
1548
1549
1550
1551 while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
1552
1553 omap1510_udc_irq ();
1554 omap1510_udc_noniso_irq ();
1555 }
1556}
1557
1558
1559void udc_set_nak(int epid)
1560{
1561
1562}
1563
1564void udc_unset_nak (int epid)
1565{
1566
1567}
1568