aboutsummaryrefslogtreecommitdiff
path: root/drivers/usb/gadget/designware_udc.c
blob: a25f5019e8a70b482f9f3a6fe35e7340053bef2e (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
/*
 * Based on drivers/usb/gadget/omap1510_udc.c
 * TI OMAP1510 USB bus interface driver
 *
 * (C) Copyright 2009
 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <asm/io.h>

#include <usbdevice.h>
#include "ep0.h"
#include <usb/designware_udc.h>
#include <usb/udc.h>
#include <asm/arch/hardware.h>

#define UDC_INIT_MDELAY		80	/* Device settle delay */

/* Some kind of debugging output... */
#ifndef DEBUG_DWUSBTTY
#define UDCDBG(str)
#define UDCDBGA(fmt, args...)
#else
#define UDCDBG(str) serial_printf(str "\n")
#define UDCDBGA(fmt, args...) serial_printf(fmt "\n", ##args)
#endif

static struct urb *ep0_urb;
static struct usb_device_instance *udc_device;

static struct plug_regs *const plug_regs_p =
    (struct plug_regs * const)CONFIG_SYS_PLUG_BASE;
static struct udc_regs *const udc_regs_p =
    (struct udc_regs * const)CONFIG_SYS_USBD_BASE;
static struct udc_endp_regs *const outep_regs_p =
    &((struct udc_regs * const)CONFIG_SYS_USBD_BASE)->out_regs[0];
static struct udc_endp_regs *const inep_regs_p =
    &((struct udc_regs * const)CONFIG_SYS_USBD_BASE)->in_regs[0];

/*
 * udc_state_transition - Write the next packet to TxFIFO.
 * @initial:	Initial state.
 * @final:	Final state.
 *
 * Helper function to implement device state changes. The device states and
 * the events that transition between them are:
 *
 *				STATE_ATTACHED
 *				||	/\
 *				\/	||
 *	DEVICE_HUB_CONFIGURED			DEVICE_HUB_RESET
 *				||	/\
 *				\/	||
 *				STATE_POWERED
 *				||	/\
 *				\/	||
 *	DEVICE_RESET				DEVICE_POWER_INTERRUPTION
 *				||	/\
 *				\/	||
 *				STATE_DEFAULT
 *				||	/\
 *				\/	||
 *	DEVICE_ADDRESS_ASSIGNED			DEVICE_RESET
 *				||	/\
 *				\/	||
 *				STATE_ADDRESSED
 *				||	/\
 *				\/	||
 *	DEVICE_CONFIGURED			DEVICE_DE_CONFIGURED
 *				||	/\
 *				\/	||
 *				STATE_CONFIGURED
 *
 * udc_state_transition transitions up (in the direction from STATE_ATTACHED
 * to STATE_CONFIGURED) from the specified initial state to the specified final
 * state, passing through each intermediate state on the way. If the initial
 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
 * no state transitions will take place.
 *
 * udc_state_transition also transitions down (in the direction from
 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
 * specified final state, passing through each intermediate state on the way.
 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
 * state, then no state transitions will take place.
 *
 * This function must only be called with interrupts disabled.
 */
static void udc_state_transition(usb_device_state_t initial,
				 usb_device_state_t final)
{
	if (initial < final) {
		switch (initial) {
		case STATE_ATTACHED:
			usbd_device_event_irq(udc_device,
					      DEVICE_HUB_CONFIGURED, 0);
			if (final == STATE_POWERED)
				break;
		case STATE_POWERED:
			usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
			if (final == STATE_DEFAULT)
				break;
		case STATE_DEFAULT:
			usbd_device_event_irq(udc_device,
					      DEVICE_ADDRESS_ASSIGNED, 0);
			if (final == STATE_ADDRESSED)
				break;
		case STATE_ADDRESSED:
			usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
		case STATE_CONFIGURED:
			break;
		default:
			break;
		}
	} else if (initial > final) {
		switch (initial) {
		case STATE_CONFIGURED:
			usbd_device_event_irq(udc_device,
					      DEVICE_DE_CONFIGURED, 0);
			if (final == STATE_ADDRESSED)
				break;
		case STATE_ADDRESSED:
			usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
			if (final == STATE_DEFAULT)
				break;
		case STATE_DEFAULT:
			usbd_device_event_irq(udc_device,
					      DEVICE_POWER_INTERRUPTION, 0);
			if (final == STATE_POWERED)
				break;
		case STATE_POWERED:
			usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
		case STATE_ATTACHED:
			break;
		default:
			break;
		}
	}
}

/* Stall endpoint */
static void udc_stall_ep(u32 ep_num)
{
	writel(readl(&inep_regs_p[ep_num].endp_cntl) | ENDP_CNTL_STALL,
	       &inep_regs_p[ep_num].endp_cntl);

	writel(readl(&outep_regs_p[ep_num].endp_cntl) | ENDP_CNTL_STALL,
	       &outep_regs_p[ep_num].endp_cntl);
}

static void *get_fifo(int ep_num, int in)
{
	u32 *fifo_ptr = (u32 *)CONFIG_SYS_FIFO_BASE;

	switch (ep_num) {
	case UDC_EP3:
		fifo_ptr += readl(&inep_regs_p[1].endp_bsorfn);
		/* break intentionally left out */

	case UDC_EP1:
		fifo_ptr += readl(&inep_regs_p[0].endp_bsorfn);
		/* break intentionally left out */

	case UDC_EP0:
	default:
		if (in) {
			fifo_ptr +=
			    readl(&outep_regs_p[2].endp_maxpacksize) >> 16;
			/* break intentionally left out */
		} else {
			break;
		}

	case UDC_EP2:
		fifo_ptr += readl(&outep_regs_p[0].endp_maxpacksize) >> 16;
		/* break intentionally left out */
	}

	return (void *)fifo_ptr;
}

static int usbgetpckfromfifo(int epNum, u8 *bufp, u32 len)
{
	u8 *fifo_ptr = (u8 *)get_fifo(epNum, 0);
	u32 i, nw, nb;
	u32 *wrdp;
	u8 *bytp;
	u32 tmp[128];

	if (readl(&udc_regs_p->dev_stat) & DEV_STAT_RXFIFO_EMPTY)
		return -1;

	nw = len / sizeof(u32);
	nb = len % sizeof(u32);

	/* use tmp buf if bufp is not word aligned */
	if ((int)bufp & 0x3)
		wrdp = (u32 *)&tmp[0];
	else
		wrdp = (u32 *)bufp;

	for (i = 0; i < nw; i++) {
		writel(readl(fifo_ptr), wrdp);
		wrdp++;
	}

	bytp = (u8 *)wrdp;
	for (i = 0; i < nb; i++) {
		writeb(readb(fifo_ptr), bytp);
		fifo_ptr++;
		bytp++;
	}
	readl(&outep_regs_p[epNum].write_done);

	/* copy back tmp buffer to bufp if bufp is not word aligned */
	if ((int)bufp & 0x3)
		memcpy(bufp, tmp, len);

	return 0;
}

static void usbputpcktofifo(int epNum, u8 *bufp, u32 len)
{
	u32 i, nw, nb;
	u32 *wrdp;
	u8 *bytp;
	u8 *fifo_ptr = get_fifo(epNum, 1);

	nw = len / sizeof(int);
	nb = len % sizeof(int);
	wrdp = (u32 *)bufp;
	for (i = 0; i < nw; i++) {
		writel(*wrdp, fifo_ptr);
		wrdp++;
	}

	bytp = (u8 *)wrdp;
	for (i = 0; i < nb; i++) {
		writeb(*bytp, fifo_ptr);
		fifo_ptr++;
		bytp++;
	}
}

/*
 * dw_write_noniso_tx_fifo - Write the next packet to TxFIFO.
 * @endpoint:		Endpoint pointer.
 *
 * If the endpoint has an active tx_urb, then the next packet of data from the
 * URB is written to the tx FIFO.  The total amount of data in the urb is given
 * by urb->actual_length.  The maximum amount of data that can be sent in any
 * one packet is given by endpoint->tx_packetSize.  The number of data bytes
 * from this URB that have already been transmitted is given by endpoint->sent.
 * endpoint->last is updated by this routine with the number of data bytes
 * transmitted in this packet.
 *
 */
static void dw_write_noniso_tx_fifo(struct usb_endpoint_instance
				       *endpoint)
{
	struct urb *urb = endpoint->tx_urb;
	int align;

	if (urb) {
		u32 last;

		UDCDBGA("urb->buffer %p, buffer_length %d, actual_length %d",
			urb->buffer, urb->buffer_length, urb->actual_length);

		last = min_t(u32, urb->actual_length - endpoint->sent,
			     endpoint->tx_packetSize);

		if (last) {
			u8 *cp = urb->buffer + endpoint->sent;

			/*
			 * This ensures that USBD packet fifo is accessed
			 * - through word aligned pointer or
			 * - through non word aligned pointer but only
			 *   with a max length to make the next packet
			 *   word aligned
			 */

			align = ((ulong)cp % sizeof(int));
			if (align)
				last = min(last, sizeof(int) - align);

			UDCDBGA("endpoint->sent %d, tx_packetSize %d, last %d",
				endpoint->sent, endpoint->tx_packetSize, last);

			usbputpcktofifo(endpoint->endpoint_address &
					USB_ENDPOINT_NUMBER_MASK, cp, last);
		}
		endpoint->last = last;
	}
}

/*
 * Handle SETUP USB interrupt.
 * This function implements TRM Figure 14-14.
 */
static void dw_udc_setup(struct usb_endpoint_instance *endpoint)
{
	u8 *datap = (u8 *)&ep0_urb->device_request;
	int ep_addr = endpoint->endpoint_address;

	UDCDBG("-> Entering device setup");
	usbgetpckfromfifo(ep_addr, datap, 8);

	/* Try to process setup packet */
	if (ep0_recv_setup(ep0_urb)) {
		/* Not a setup packet, stall next EP0 transaction */
		udc_stall_ep(0);
		UDCDBG("can't parse setup packet, still waiting for setup");
		return;
	}

	/* Check direction */
	if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
	    == USB_REQ_HOST2DEVICE) {
		UDCDBG("control write on EP0");
		if (le16_to_cpu(ep0_urb->device_request.wLength)) {
			/* Stall this request */
			UDCDBG("Stalling unsupported EP0 control write data "
			       "stage.");
			udc_stall_ep(0);
		}
	} else {

		UDCDBG("control read on EP0");
		/*
		 * The ep0_recv_setup function has already placed our response
		 * packet data in ep0_urb->buffer and the packet length in
		 * ep0_urb->actual_length.
		 */
		endpoint->tx_urb = ep0_urb;
		endpoint->sent = 0;
		/*
		 * Write packet data to the FIFO.  dw_write_noniso_tx_fifo
		 * will update endpoint->last with the number of bytes written
		 * to the FIFO.
		 */
		dw_write_noniso_tx_fifo(endpoint);

		writel(0x0, &inep_regs_p[ep_addr].write_done);
	}

	udc_unset_nak(endpoint->endpoint_address);

	UDCDBG("<- Leaving device setup");
}

/*
 * Handle endpoint 0 RX interrupt
 */
static void dw_udc_ep0_rx(struct usb_endpoint_instance *endpoint)
{
	u8 dummy[64];

	UDCDBG("RX on EP0");

	/* Check direction */
	if ((ep0_urb->device_request.bmRequestType
	     & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
		/*
		 * This rx interrupt must be for a control write data
		 * stage packet.
		 *
		 * We don't support control write data stages.
		 * We should never end up here.
		 */

		UDCDBG("Stalling unexpected EP0 control write "
		       "data stage packet");
		udc_stall_ep(0);
	} else {
		/*
		 * This rx interrupt must be for a control read status
		 * stage packet.
		 */
		UDCDBG("ACK on EP0 control read status stage packet");
		u32 len = (readl(&outep_regs_p[0].endp_status) >> 11) & 0xfff;
		usbgetpckfromfifo(0, dummy, len);
	}
}

/*
 * Handle endpoint 0 TX interrupt
 */
static void dw_udc_ep0_tx(struct usb_endpoint_instance *endpoint)
{
	struct usb_device_request *request = &ep0_urb->device_request;
	int ep_addr;

	UDCDBG("TX on EP0");

	/* Check direction */
	if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
	    USB_REQ_HOST2DEVICE) {
		/*
		 * This tx interrupt must be for a control write status
		 * stage packet.
		 */
		UDCDBG("ACK on EP0 control write status stage packet");
	} else {
		/*
		 * This tx interrupt must be for a control read data
		 * stage packet.
		 */
		int wLength = le16_to_cpu(request->wLength);

		/*
		 * Update our count of bytes sent so far in this
		 * transfer.
		 */
		endpoint->sent += endpoint->last;

		/*
		 * We are finished with this transfer if we have sent
		 * all of the bytes in our tx urb (urb->actual_length)
		 * unless we need a zero-length terminating packet.  We
		 * need a zero-length terminating packet if we returned
		 * fewer bytes than were requested (wLength) by the host,
		 * and the number of bytes we returned is an exact
		 * multiple of the packet size endpoint->tx_packetSize.
		 */
		if ((endpoint->sent == ep0_urb->actual_length) &&
		    ((ep0_urb->actual_length == wLength) ||
		     (endpoint->last != endpoint->tx_packetSize))) {
			/* Done with control read data stage. */
			UDCDBG("control read data stage complete");
		} else {
			/*
			 * We still have another packet of data to send
			 * in this control read data stage or else we
			 * need a zero-length terminating packet.
			 */
			UDCDBG("ACK control read data stage packet");
			dw_write_noniso_tx_fifo(endpoint);

			ep_addr = endpoint->endpoint_address;
			writel(0x0, &inep_regs_p[ep_addr].write_done);
		}
	}
}

static struct usb_endpoint_instance *dw_find_ep(int ep)
{
	int i;

	for (i = 0; i < udc_device->bus->max_endpoints; i++) {
		if ((udc_device->bus->endpoint_array[i].endpoint_address &
		     USB_ENDPOINT_NUMBER_MASK) == ep)
			return &udc_device->bus->endpoint_array[i];
	}
	return NULL;
}

/*
 * Handle RX transaction on non-ISO endpoint.
 * The ep argument is a physical endpoint number for a non-ISO IN endpoint
 * in the range 1 to 15.
 */
static void dw_udc_epn_rx(int ep)
{
	int nbytes = 0;
	struct urb *urb;
	struct usb_endpoint_instance *endpoint = dw_find_ep(ep);

	if (endpoint) {
		urb = endpoint->rcv_urb;

		if (urb) {
			u8 *cp = urb->buffer + urb->actual_length;

			nbytes = (readl(&outep_regs_p[ep].endp_status) >> 11) &
			    0xfff;
			usbgetpckfromfifo(ep, cp, nbytes);
			usbd_rcv_complete(endpoint, nbytes, 0);
		}
	}
}

/*
 * Handle TX transaction on non-ISO endpoint.
 * The ep argument is a physical endpoint number for a non-ISO IN endpoint
 * in the range 16 to 30.
 */
static void dw_udc_epn_tx(int ep)
{
	struct usb_endpoint_instance *endpoint = dw_find_ep(ep);

	if (!endpoint)
		return;

	/*
	 * We need to transmit a terminating zero-length packet now if
	 * we have sent all of the data in this URB and the transfer
	 * size was an exact multiple of the packet size.
	 */
	if (endpoint->tx_urb &&
	    (endpoint->last == endpoint->tx_packetSize) &&
	    (endpoint->tx_urb->actual_length - endpoint->sent -
	     endpoint->last == 0)) {
		/* handle zero length packet here */
		writel(0x0, &inep_regs_p[ep].write_done);

	}

	if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
		/* retire the data that was just sent */
		usbd_tx_complete(endpoint);
		/*
		 * Check to see if we have more data ready to transmit
		 * now.
		 */
		if (endpoint->tx_urb && endpoint->tx_urb->actual_length) {
			/* write data to FIFO */
			dw_write_noniso_tx_fifo(endpoint);
			writel(0x0, &inep_regs_p[ep].write_done);

		} else if (endpoint->tx_urb
			   && (endpoint->tx_urb->actual_length == 0)) {
			/* udc_set_nak(ep); */
		}
	}
}

/*
 * Start of public functions.
 */

/* Called to start packet transmission. */
int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
{
	udc_unset_nak(endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK);
	return 0;
}

/* Start to initialize h/w stuff */
int udc_init(void)
{
	int i;
	u32 plug_st;

	udc_device = NULL;

	UDCDBG("starting");

	readl(&plug_regs_p->plug_pending);

	for (i = 0; i < UDC_INIT_MDELAY; i++)
		udelay(1000);

	plug_st = readl(&plug_regs_p->plug_state);
	writel(plug_st | PLUG_STATUS_EN, &plug_regs_p->plug_state);

	writel(~0x0, &udc_regs_p->endp_int);
	writel(~0x0, &udc_regs_p->dev_int_mask);
	writel(~0x0, &udc_regs_p->endp_int_mask);

#ifndef CONFIG_USBD_HS
	writel(DEV_CONF_FS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
	       DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
#else
	writel(DEV_CONF_HS_SPEED | DEV_CONF_REMWAKEUP | DEV_CONF_SELFPOW |
			DEV_CONF_PHYINT_16, &udc_regs_p->dev_conf);
#endif

	writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);

	/* Clear all interrupts pending */
	writel(DEV_INT_MSK, &udc_regs_p->dev_int);

	return 0;
}

int is_usbd_high_speed(void)
{
	return (readl(&udc_regs_p->dev_stat) & DEV_STAT_ENUM) ? 0 : 1;
}

/*
 * udc_setup_ep - setup endpoint
 * Associate a physical endpoint with endpoint_instance
 */
void udc_setup_ep(struct usb_device_instance *device,
		  u32 ep, struct usb_endpoint_instance *endpoint)
{
	UDCDBGA("setting up endpoint addr %x", endpoint->endpoint_address);
	int ep_addr;
	int ep_num, ep_type;
	int packet_size;
	int buffer_size;
	int attributes;
	char *tt;
	u32 endp_intmask;

	if ((ep != 0) && (udc_device->device_state < STATE_ADDRESSED))
		return;

	tt = env_get("usbtty");
	if (!tt)
		tt = "generic";

	ep_addr = endpoint->endpoint_address;
	ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;

	if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
		/* IN endpoint */
		packet_size = endpoint->tx_packetSize;
		buffer_size = packet_size * 2;
		attributes = endpoint->tx_attributes;
	} else {
		/* OUT endpoint */
		packet_size = endpoint->rcv_packetSize;
		buffer_size = packet_size * 2;
		attributes = endpoint->rcv_attributes;
	}

	switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
	case USB_ENDPOINT_XFER_CONTROL:
		ep_type = ENDP_EPTYPE_CNTL;
		break;
	case USB_ENDPOINT_XFER_BULK:
	default:
		ep_type = ENDP_EPTYPE_BULK;
		break;
	case USB_ENDPOINT_XFER_INT:
		ep_type = ENDP_EPTYPE_INT;
		break;
	case USB_ENDPOINT_XFER_ISOC:
		ep_type = ENDP_EPTYPE_ISO;
		break;
	}

	struct udc_endp_regs *out_p = &outep_regs_p[ep_num];
	struct udc_endp_regs *in_p = &inep_regs_p[ep_num];

	if (!ep_addr) {
		/* Setup endpoint 0 */
		buffer_size = packet_size;

		writel(readl(&in_p->endp_cntl) | ENDP_CNTL_CNAK,
		       &in_p->endp_cntl);

		writel(readl(&out_p->endp_cntl) | ENDP_CNTL_CNAK,
		       &out_p->endp_cntl);

		writel(ENDP_CNTL_CONTROL | ENDP_CNTL_FLUSH, &in_p->endp_cntl);

		writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);

		writel(packet_size, &in_p->endp_maxpacksize);

		writel(ENDP_CNTL_CONTROL | ENDP_CNTL_RRDY, &out_p->endp_cntl);

		writel(packet_size | ((buffer_size / sizeof(int)) << 16),
		       &out_p->endp_maxpacksize);

	} else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
		/* Setup the IN endpoint */
		writel(0x0, &in_p->endp_status);
		writel((ep_type << 4) | ENDP_CNTL_RRDY, &in_p->endp_cntl);
		writel(buffer_size / sizeof(int), &in_p->endp_bsorfn);
		writel(packet_size, &in_p->endp_maxpacksize);

		if (!strcmp(tt, "cdc_acm")) {
			if (ep_type == ENDP_EPTYPE_INT) {
				/* Conf no. 1 Interface no. 0 */
				writel((packet_size << 19) |
				       ENDP_EPDIR_IN | (1 << 7) |
				       (0 << 11) | (ep_type << 5) | ep_num,
				       &udc_regs_p->udc_endp_reg[ep_num]);
			} else {
				/* Conf no. 1 Interface no. 1 */
				writel((packet_size << 19) |
				       ENDP_EPDIR_IN | (1 << 7) |
				       (1 << 11) | (ep_type << 5) | ep_num,
				       &udc_regs_p->udc_endp_reg[ep_num]);
			}
		} else {
			/* Conf no. 1 Interface no. 0 */
			writel((packet_size << 19) |
			       ENDP_EPDIR_IN | (1 << 7) |
			       (0 << 11) | (ep_type << 5) | ep_num,
			       &udc_regs_p->udc_endp_reg[ep_num]);
		}

	} else {
		/* Setup the OUT endpoint */
		writel(0x0, &out_p->endp_status);
		writel((ep_type << 4) | ENDP_CNTL_RRDY, &out_p->endp_cntl);
		writel(packet_size | ((buffer_size / sizeof(int)) << 16),
		       &out_p->endp_maxpacksize);

		if (!strcmp(tt, "cdc_acm")) {
			writel((packet_size << 19) |
			       ENDP_EPDIR_OUT | (1 << 7) |
			       (1 << 11) | (ep_type << 5) | ep_num,
			       &udc_regs_p->udc_endp_reg[ep_num]);
		} else {
			writel((packet_size << 19) |
			       ENDP_EPDIR_OUT | (1 << 7) |
			       (0 << 11) | (ep_type << 5) | ep_num,
			       &udc_regs_p->udc_endp_reg[ep_num]);
		}

	}

	endp_intmask = readl(&udc_regs_p->endp_int_mask);
	endp_intmask &= ~((1 << ep_num) | 0x10000 << ep_num);
	writel(endp_intmask, &udc_regs_p->endp_int_mask);
}

/* Turn on the USB connection by enabling the pullup resistor */
void udc_connect(void)
{
	u32 plug_st, dev_cntl;

	dev_cntl = readl(&udc_regs_p->dev_cntl);
	dev_cntl |= DEV_CNTL_SOFTDISCONNECT;
	writel(dev_cntl, &udc_regs_p->dev_cntl);

	udelay(1000);

	dev_cntl = readl(&udc_regs_p->dev_cntl);
	dev_cntl &= ~DEV_CNTL_SOFTDISCONNECT;
	writel(dev_cntl, &udc_regs_p->dev_cntl);

	plug_st = readl(&plug_regs_p->plug_state);
	plug_st &= ~(PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
	writel(plug_st, &plug_regs_p->plug_state);
}

/* Turn off the USB connection by disabling the pullup resistor */
void udc_disconnect(void)
{
	u32 plug_st;

	writel(DEV_CNTL_SOFTDISCONNECT, &udc_regs_p->dev_cntl);

	plug_st = readl(&plug_regs_p->plug_state);
	plug_st |= (PLUG_STATUS_PHY_RESET | PLUG_STATUS_PHY_MODE);
	writel(plug_st, &plug_regs_p->plug_state);
}

/* Switch on the UDC */
void udc_enable(struct usb_device_instance *device)
{
	UDCDBGA("enable device %p, status %d", device, device->status);

	/* Save the device structure pointer */
	udc_device = device;

	/* Setup ep0 urb */
	if (!ep0_urb) {
		ep0_urb =
		    usbd_alloc_urb(udc_device, udc_device->bus->endpoint_array);
	} else {
		serial_printf("udc_enable: ep0_urb already allocated %p\n",
			      ep0_urb);
	}

	writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);
}

/**
 * udc_startup - allow udc code to do any additional startup
 */
void udc_startup_events(struct usb_device_instance *device)
{
	/* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
	usbd_device_event_irq(device, DEVICE_INIT, 0);

	/*
	 * The DEVICE_CREATE event puts the USB device in the state
	 * STATE_ATTACHED.
	 */
	usbd_device_event_irq(device, DEVICE_CREATE, 0);

	/*
	 * Some USB controller driver implementations signal
	 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
	 * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
	 * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
	 * The DW USB client controller has the capability to detect when the
	 * USB cable is connected to a powered USB bus, so we will defer the
	 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events until later.
	 */

	udc_enable(device);
}

/*
 * Plug detection interrupt handling
 */
static void dw_udc_plug_irq(void)
{
	if (readl(&plug_regs_p->plug_state) & PLUG_STATUS_ATTACHED) {
		/*
		 * USB cable attached
		 * Turn off PHY reset bit (PLUG detect).
		 * Switch PHY opmode to normal operation (PLUG detect).
		 */
		udc_connect();
		writel(DEV_INT_SOF, &udc_regs_p->dev_int_mask);

		UDCDBG("device attached and powered");
		udc_state_transition(udc_device->device_state, STATE_POWERED);
	} else {
		writel(~0x0, &udc_regs_p->dev_int_mask);

		UDCDBG("device detached or unpowered");
		udc_state_transition(udc_device->device_state, STATE_ATTACHED);
	}
}

/*
 * Device interrupt handling
 */
static void dw_udc_dev_irq(void)
{
	if (readl(&udc_regs_p->dev_int) & DEV_INT_USBRESET) {
		writel(~0x0, &udc_regs_p->endp_int_mask);

		writel(readl(&inep_regs_p[0].endp_cntl) | ENDP_CNTL_FLUSH,
		       &inep_regs_p[0].endp_cntl);

		writel(DEV_INT_USBRESET, &udc_regs_p->dev_int);

		/*
		 * This endpoint0 specific register can be programmed only
		 * after the phy clock is initialized
		 */
		writel((EP0_MAX_PACKET_SIZE << 19) | ENDP_EPTYPE_CNTL,
				&udc_regs_p->udc_endp_reg[0]);

		UDCDBG("device reset in progess");
		udc_state_transition(udc_device->device_state, STATE_DEFAULT);
	}

	/* Device Enumeration completed */
	if (readl(&udc_regs_p->dev_int) & DEV_INT_ENUM) {
		writel(DEV_INT_ENUM, &udc_regs_p->dev_int);

		/* Endpoint interrupt enabled for Ctrl IN & Ctrl OUT */
		writel(readl(&udc_regs_p->endp_int_mask) & ~0x10001,
		       &udc_regs_p->endp_int_mask);

		UDCDBG("default -> addressed");
		udc_state_transition(udc_device->device_state, STATE_ADDRESSED);
	}

	/* The USB will be in SUSPEND in 3 ms */
	if (readl(&udc_regs_p->dev_int) & DEV_INT_INACTIVE) {
		writel(DEV_INT_INACTIVE, &udc_regs_p->dev_int);

		UDCDBG("entering inactive state");
		/* usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0); */
	}

	/* SetConfiguration command received */
	if (readl(&udc_regs_p->dev_int) & DEV_INT_SETCFG) {
		writel(DEV_INT_SETCFG, &udc_regs_p->dev_int);

		UDCDBG("entering configured state");
		udc_state_transition(udc_device->device_state,
				     STATE_CONFIGURED);
	}

	/* SetInterface command received */
	if (readl(&udc_regs_p->dev_int) & DEV_INT_SETINTF)
		writel(DEV_INT_SETINTF, &udc_regs_p->dev_int);

	/* USB Suspend detected on cable */
	if (readl(&udc_regs_p->dev_int) & DEV_INT_SUSPUSB) {
		writel(DEV_INT_SUSPUSB, &udc_regs_p->dev_int);

		UDCDBG("entering suspended state");
		usbd_device_event_irq(udc_device, DEVICE_BUS_INACTIVE, 0);
	}

	/* USB Start-Of-Frame detected on cable */
	if (readl(&udc_regs_p->dev_int) & DEV_INT_SOF)
		writel(DEV_INT_SOF, &udc_regs_p->dev_int);
}

/*
 * Endpoint interrupt handling
 */
static void dw_udc_endpoint_irq(void)
{
	while (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLOUT) {

		writel(ENDP0_INT_CTRLOUT, &udc_regs_p->endp_int);

		if ((readl(&outep_regs_p[0].endp_status) & ENDP_STATUS_OUTMSK)
		    == ENDP_STATUS_OUT_SETUP) {
			dw_udc_setup(udc_device->bus->endpoint_array + 0);
			writel(ENDP_STATUS_OUT_SETUP,
			       &outep_regs_p[0].endp_status);

		} else if ((readl(&outep_regs_p[0].endp_status) &
			    ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {
			dw_udc_ep0_rx(udc_device->bus->endpoint_array + 0);
			writel(ENDP_STATUS_OUT_DATA,
			       &outep_regs_p[0].endp_status);

		} else if ((readl(&outep_regs_p[0].endp_status) &
			    ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
			/* NONE received */
		}

		writel(0x0, &outep_regs_p[0].endp_status);
	}

	if (readl(&udc_regs_p->endp_int) & ENDP0_INT_CTRLIN) {
		dw_udc_ep0_tx(udc_device->bus->endpoint_array + 0);

		writel(ENDP_STATUS_IN, &inep_regs_p[0].endp_status);
		writel(ENDP0_INT_CTRLIN, &udc_regs_p->endp_int);
	}

	if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOOUT_MSK) {
		u32 epnum = 0;
		u32 ep_int = readl(&udc_regs_p->endp_int) &
		    ENDP_INT_NONISOOUT_MSK;

		ep_int >>= 16;
		while (0x0 == (ep_int & 0x1)) {
			ep_int >>= 1;
			epnum++;
		}

		writel((1 << 16) << epnum, &udc_regs_p->endp_int);

		if ((readl(&outep_regs_p[epnum].endp_status) &
		     ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_DATA) {

			dw_udc_epn_rx(epnum);
			writel(ENDP_STATUS_OUT_DATA,
			       &outep_regs_p[epnum].endp_status);
		} else if ((readl(&outep_regs_p[epnum].endp_status) &
			    ENDP_STATUS_OUTMSK) == ENDP_STATUS_OUT_NONE) {
			writel(0x0, &outep_regs_p[epnum].endp_status);
		}
	}

	if (readl(&udc_regs_p->endp_int) & ENDP_INT_NONISOIN_MSK) {
		u32 epnum = 0;
		u32 ep_int = readl(&udc_regs_p->endp_int) &
		    ENDP_INT_NONISOIN_MSK;

		while (0x0 == (ep_int & 0x1)) {
			ep_int >>= 1;
			epnum++;
		}

		if (readl(&inep_regs_p[epnum].endp_status) & ENDP_STATUS_IN) {
			writel(ENDP_STATUS_IN,
			       &outep_regs_p[epnum].endp_status);
			dw_udc_epn_tx(epnum);

			writel(ENDP_STATUS_IN,
			       &outep_regs_p[epnum].endp_status);
		}

		writel((1 << epnum), &udc_regs_p->endp_int);
	}
}

/*
 * UDC interrupts
 */
void udc_irq(void)
{
	/*
	 * Loop while we have interrupts.
	 * If we don't do this, the input chain
	 * polling delay is likely to miss
	 * host requests.
	 */
	while (readl(&plug_regs_p->plug_pending))
		dw_udc_plug_irq();

	while (readl(&udc_regs_p->dev_int))
		dw_udc_dev_irq();

	if (readl(&udc_regs_p->endp_int))
		dw_udc_endpoint_irq();
}

/* Flow control */
void udc_set_nak(int epid)
{
	writel(readl(&inep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
	       &inep_regs_p[epid].endp_cntl);

	writel(readl(&outep_regs_p[epid].endp_cntl) | ENDP_CNTL_SNAK,
	       &outep_regs_p[epid].endp_cntl);
}

void udc_unset_nak(int epid)
{
	u32 val;

	val = readl(&inep_regs_p[epid].endp_cntl);
	val &= ~ENDP_CNTL_SNAK;
	val |= ENDP_CNTL_CNAK;
	writel(val, &inep_regs_p[epid].endp_cntl);

	val = readl(&outep_regs_p[epid].endp_cntl);
	val &= ~ENDP_CNTL_SNAK;
	val |= ENDP_CNTL_CNAK;
	writel(val, &outep_regs_p[epid].endp_cntl);
}