summaryrefslogtreecommitdiff
path: root/drivers/usb/gadget/musb_udc.c
blob: a46a6e948c318f331bd7b405c2e4b883e68f0848 (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
/*
 * (C) Copyright 2009 Texas Instruments Incorporated.
 *
 * Based on
 * u-boot OMAP1510 USB drivers (drivers/usbdcore_omap1510.c)
 * twl4030 init based on linux (drivers/i2c/chips/twl4030_usb.c)
 *
 * Author:	Diego Dompe (diego.dompe@ridgerun.com)
 *		Atin Malaviya (atin.malaviya@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <common.h>

#include <asm/io.h>
// #include <asm/arch/clocks.h>
// #include <asm/arch/clocks_omap3.h>
// #include <asm/arch/sys_proto.h>
#include <usbdevice.h>
#include <usb/musb_udc.h>
#include "ep0.h"

/* Private definitions */
enum ep0_status { IDLE, DATA_STAGE, DATA_COMPLETE };

/* Private variables */
static struct usb_device_instance *udc_device;
static enum ep0_status ep0status = IDLE;
static unsigned char do_set_address = 0;
static struct urb *ep0_urb = NULL;

/* Helper functions */

/* sr32 from cpu/arm_cortexa8/omap3/syslib.c */
/*****************************************************************
 * sr32 - clear & set a value in a bit range for a 32 bit address
 *****************************************************************/
static void sr32(void *addr, u32 start_bit, u32 num_bits, u32 value)
{
	u32 tmp, msk = 0;
	msk = 1 << num_bits;
	--msk;
	tmp = readl((u32)addr) & ~(msk << start_bit);
	tmp |= value << start_bit;
	writel(tmp, (u32)addr);
}
 
static void insl(u32 reg, u32 *data, u32 size)
{
	u32 t;

	for (t = 0; t < size; t++, data++)
		*data = inl(reg);
}

static void outsl(u32 reg, u32 *data, u32 size)
{
	u32 t;

	for (t = 0; t < size; t++, data++)
		outl(*data, reg);
}

static void outsb(u32 reg, u8 *data, u32 size)
{
	u32 t;

	for (t = 0; t < size; t++, data++)
		outb(*data, reg);
}

static void musb_fifo_read(int epnumber, u8 *data, u32 size)
{
	if ((u32)data & 0x3) {		/* Not aligned data */
		insb((UDC_FIFO0 + (epnumber << 2)), data, size);
	} else {			/* 32 bits aligned data */
		int i;

		insl(UDC_FIFO0 + (epnumber << 2), (u32 *)data, size >> 2);
		data += size & ~0x3;
		i = size & 0x3;
		while (i--) {
			*data = inb(UDC_FIFO0 + (epnumber << 2));
			data++;
		}
	}
}

static void musb_fifo_write(int epnumber, u8 *data, u32 size)
{
	if ((u32)data & 0x3) {		/* Not aligned data */
		outsb(UDC_FIFO0 + (epnumber << 2), data, size);
	} else {			/* 32 bits aligned data */
		int i;

		outsl(UDC_FIFO0 + (epnumber << 2), (u32 *)data, size >> 2);
		data += size & ~0x3;
		i = size & 0x3;
		while (i--) {
			outb(*data, UDC_FIFO0 + (epnumber << 2));
			data++;
		}
	}
}

static void musb_fifos_configure(struct usb_device_instance *device)
{
	int ep;
	struct usb_bus_instance *bus;
	struct usb_endpoint_instance *endpoint;
	unsigned short ep_ptr, ep_size, ep_doublebuffer;
	int ep_addr, packet_size, buffer_size, attributes;

	bus = device->bus;

	ep_ptr = 0;

	for (ep = 0; ep < bus->max_endpoints; ep++) {
		endpoint = bus->endpoint_array + ep;
		ep_addr = endpoint->endpoint_address;
		if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
			/* IN endpoint */
			packet_size = endpoint->tx_packetSize;
			attributes = endpoint->tx_attributes;
		} else {
			/* OUT endpoint */
			packet_size = endpoint->rcv_packetSize;
			attributes = endpoint->rcv_attributes;
		}

		switch (packet_size) {
		case 0:
			ep_size = 0;
			break;
		case 8:
			ep_size = 0;
			break;
		case 16:
			ep_size = 1;
			break;
		case 32:
			ep_size = 2;
			break;
		case 64:
			ep_size = 3;
			break;
		case 128:
			ep_size = 4;
			break;
		case 256:
			ep_size = 5;
			break;
		case 512:
			ep_size = 6;
			break;
		default:
			serial_printf("ep 0x%02x has bad packet size %d",
				ep_addr, packet_size);
			packet_size = 0;
			ep_size = 0;
			break;
		}

		switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
		case USB_ENDPOINT_XFER_CONTROL:
		case USB_ENDPOINT_XFER_BULK:
		case USB_ENDPOINT_XFER_INT:
		default:
			/* A non-isochronous endpoint may optionally be
			 * double-buffered. For now we disable
			 * double-buffering.
			 */
			ep_doublebuffer = 0;
			if (packet_size > 64)
				packet_size = 0;
			if (!ep || !ep_doublebuffer)
				buffer_size = packet_size;
			else
				buffer_size = packet_size * 2;
			break;
		case USB_ENDPOINT_XFER_ISOC:
			/* Isochronous endpoints are always double-
			 * buffered
			 */
			ep_doublebuffer = 1;
			buffer_size = packet_size * 2;
			break;
		}

		/* check to see if our packet buffer RAM is exhausted */
		if ((ep_ptr + buffer_size) > UDC_MAX_FIFO_SIZE) {
			serial_printf("out of packet RAM for ep 0x%02x buf size %d",
				ep_addr, buffer_size);
			buffer_size = packet_size = 0;
		}

		/* force a default configuration for endpoint 0 since it is
		 * always enabled
		 */
		if (!ep && ((packet_size < 8) || (packet_size > 64))) {
			buffer_size = packet_size = 64;
			ep_size = 3;
		}

		outb(ep & 0xF, UDC_INDEX);
		if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
			/* IN endpoint */
			outb((ep_doublebuffer << 4) | (ep_size & 0xf),
				UDC_TXFIFOSZ);
			outw(ep_ptr >> 3, UDC_TXFIFOADDR);
			if (!ep) {	/* This only apply for ep != 0 */
				outw(packet_size & 0x3FF, UDC_TXMAXP);
			}
		} else {
			/* OUT endpoint */
			outb((ep_doublebuffer << 4) | (ep_size & 0xf),
				UDC_RXFIFOSZ);
			outw(ep_ptr >> 3, UDC_RXFIFOADDR);
			if (!ep) {	/* This only apply for ep != 0 */
				outw(packet_size & 0x3FF, UDC_RXMAXP);
			}
		}
		ep_ptr += buffer_size;
	}
}

static void musb_ep0_tx(struct usb_endpoint_instance *endpoint)
{
	unsigned int size = 0;
	struct urb *urb = endpoint->tx_urb;

	outb(0, UDC_INDEX);

	if (urb) {

		if ((size =
			MIN(urb->actual_length - endpoint->sent,
				endpoint->tx_packetSize))) {

			musb_fifo_write(0, urb->buffer + endpoint->sent, size);
		}
		endpoint->last = size;

		if (((endpoint->sent + size) == ep0_urb->device_request.wLength)
			|| (size != endpoint->tx_packetSize)) {
			ep0status = DATA_COMPLETE;
			/* Transmit packet and set data end */
			outw(0xA, UDC_CSR0);
		} else {
			outw(0x2, UDC_CSR0);	/* Transmit packet */
		}
	}
}

static void musb_ep0_handler(struct usb_endpoint_instance *endpoint)
{
	u16 csr0;

	outb(0, UDC_INDEX);

	/* Check errors */
	csr0 = inw(UDC_CSR0);

	if (csr0 & 0x4) {			/* Sent stall */
		outw(csr0 & ~0x4, UDC_CSR0);	/* Clear stall */
		serial_printf("%s: stall received on EP0!, csr0 %hx\n",
				__FUNCTION__, csr0);
		csr0 = inw(UDC_CSR0);
		serial_printf("state %d -> %d (IDLE), csr0 0x%hx\n",
				ep0status, IDLE, csr0);
		ep0status = IDLE;
	}

	if (csr0 & 0x10) {		/* Setup end */
		outw(0x80, UDC_CSR0);	/* Clear setup end */
		serial_printf("%s: setup END early happened! status is %d\n",
			__FUNCTION__, ep0status);
		ep0status = IDLE;
		return;
	}

	switch (ep0status) {
	case DATA_COMPLETE:
		if (do_set_address) {
			/*
			 * We need to set the address only after
			 * the status stage is complete
			 */
			outb(udc_device->address, UDC_FADDR);
			do_set_address = 0;
		}
		ep0status = IDLE;
		/* Fallthrough */
	case IDLE:		/* Receiving a setup packet */
		if (csr0 & 0x1) {
			insl(UDC_FIFO0,
				(unsigned int *) &ep0_urb->device_request, 2);

			/* If we have data, then don't go to IDLE state */
			if (ep0_urb->device_request.wLength) {
				ep0status = DATA_STAGE;

				outw(0x40, UDC_CSR0);	/* Clear RXPKTRDY */
				if ((ep0_urb->device_request.
					bmRequestType & USB_REQ_DIRECTION_MASK)
					== USB_REQ_DEVICE2HOST) {

					/* Try to process setup packet */
					if (ep0_recv_setup(ep0_urb)) {
						/*
						 * Not a setup packet, stall
						 * next EP0 transaction
						 */
						outw(0x20, UDC_CSR0);
						serial_printf("not a setup packed 1, stalling\n");
						ep0status = IDLE;
						return;
					}
					/*
					 * If we are sending data, do it now, as
					 * ep0_recv_setup should have prepare
					 * them
					 */
					endpoint->tx_urb = ep0_urb;
					endpoint->sent = 0;

					musb_ep0_tx(endpoint);
				} else {
					endpoint->rcv_urb = ep0_urb;
					ep0_urb->actual_length = 0;
				}
			} else {	/* Processing zero-length packet */
				/*
				 * The www.linux-usb.org/usbtest 'test 14'
				 * fails with error for zero length request.
				 * If the SETUP packet requests ZERO length data
				 * from device-to-host, the TXPKTRDY bit needs
				 * to be set in TXCSR otherwise the STATUS stage
				 * of control transfer will never complete.
				 */
				if ((ep0_urb->device_request.
					bmRequestType & USB_REQ_DIRECTION_MASK)
					== USB_REQ_DEVICE2HOST) {
					/*
					 * Clear RXPKTRDY and DATAEND and
					 * TXPKTRDY
					 */
					outw(0x4A, UDC_CSR0);
				} else {
					/* Clear RXPKTRDY and DATAEND */
					outw(0x48, UDC_CSR0);
				}

				/* Try to process setup packet */
				if (ep0_recv_setup(ep0_urb)) {
					/*
					 * Not a setup packet, stall next EP0
					 * transaction
					 */
					outw(0x20, UDC_CSR0);
					serial_printf("not a setup packed 2, stalling\n");
					ep0status = IDLE;
					return;
				}

				switch (ep0_urb->device_request.bRequest) {
				case USB_REQ_SET_ADDRESS:
					usbd_device_event_irq(udc_device,
						DEVICE_ADDRESS_ASSIGNED, 0);
					do_set_address = 1;
					break;
				case USB_REQ_SET_CONFIGURATION:
					usbd_device_event_irq(udc_device,
						DEVICE_CONFIGURED, 0);
					break;
				}

				ep0status = DATA_COMPLETE;
			}
		}
		break;
	case DATA_STAGE:
		if ((ep0_urb->device_request.
			bmRequestType & USB_REQ_DIRECTION_MASK)
			== USB_REQ_DEVICE2HOST) {
			if (!(csr0 & 0x2)) {	/* There packet was send? */
				endpoint->sent += endpoint->last;
				/*
				 * If we finished sending data we would not
				 * be on the DATA_STAGE
				 */
				musb_ep0_tx(endpoint);
			}
		} else {
			/* Receiving data */
			u16 length = inw(UDC_COUNT0);

			if (length) {
				if (ep0_urb->actual_length + length >
					ep0_urb->device_request.wLength)
					length =
					ep0_urb->device_request.wLength -
					ep0_urb->actual_length;

				endpoint->last = length;

				musb_fifo_read(0, &ep0_urb->
					buffer[ep0_urb->actual_length], length);
				ep0_urb->actual_length += length;
			}

			/*
			 * We finish if we received the amount of data expected,
			 * or less of the packet size
			 */
			if ((ep0_urb->actual_length ==
				ep0_urb->device_request.wLength) ||
				(endpoint->last != endpoint->tx_packetSize)) {
				ep0status = DATA_COMPLETE;
				/* Clear RXPKTRDY and DATAEND */
				outw(0x48, UDC_CSR0);
				/* This will process the incoming data */
				if (ep0_recv_setup(ep0_urb)) {
					/*
					 * Not a setup packet, stall next EP0
					 * transaction
					 */
					outw(0x20, UDC_CSR0);
					serial_printf("not a setup packed 3, stalling\n");
					return;
				}
			} else
				outw(0x40, UDC_CSR0);	/* Clear RXPKTRDY */
		}
		break;
	}
}

static void musb_ep_tx(struct usb_endpoint_instance *endpoint)
{
	unsigned int size = 0, epnumber =
		endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
	struct urb *urb = endpoint->tx_urb;

	outb(epnumber, UDC_INDEX);
	if (urb) {
		if ((size =
			MIN(urb->actual_length - endpoint->sent,
			endpoint->tx_packetSize))) {
			musb_fifo_write(epnumber, urb->buffer + endpoint->sent,
					size);
		}
		endpoint->last = size;
		endpoint->state = 1; /* Transmit hardware is busy */

		outw(inw(UDC_TXCSR) | 0x1, UDC_TXCSR); /* Transmit packet */
	}
}


static void musb_tx_handler(struct usb_endpoint_instance *endpoint)
{
	unsigned int epnumber =
		endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
	u16 txcsr;

	outb(epnumber, UDC_INDEX);

	/* Check errors */
	txcsr = inw(UDC_TXCSR);

	if (txcsr & 0x4) {		/* Clear underrun */
		txcsr &= ~0x4;
	}
	if (txcsr & 0x20) {		/* SENTSTALL */
		outw(txcsr & ~0x20, UDC_TXCSR);	/* Clear stall */
		serial_printf("musb_tx_handler: SENTSTALL, txcsr 0x%hx\n",
				txcsr);
		return;
	}

	if (endpoint->tx_urb && !(txcsr & 0x1)) { /* The packet was send? */
		if ((endpoint->sent + endpoint->last == endpoint->tx_urb->
			actual_length)	/* Send a zero length packet? */
			&& (endpoint->last == endpoint->tx_packetSize)) {
			/* Prepare to transmit a zero-length packet. */
			endpoint->sent += endpoint->last;
			musb_ep_tx(endpoint);
		} else if (endpoint->tx_urb->actual_length) {
			/* retire the data that was just sent */
			usbd_tx_complete(endpoint);
			endpoint->state = 0; /* Transmit hardware is free */

			/*
			 * Check to see if we have more data ready to transmit
			 * now.
			 */
			if (endpoint->tx_urb && endpoint->tx_urb->
				actual_length) {
				musb_ep_tx(endpoint);
			}
		}
	}
}

static void musb_rx_handler(struct usb_endpoint_instance *endpoint)
{
	unsigned int epnumber =
		endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
	u16 rxcsr;
	u16 length;

	outb(epnumber, UDC_INDEX);

	/* Check errors */
	rxcsr = inw(UDC_RXCSR);

	if (!(rxcsr & 0x1))		/* There is a package received? */
		return;

	if (rxcsr & 0x40) {		/* SENTSTALL */
		outw(rxcsr & ~0x40, UDC_RXCSR);	/* Clear stall */
		serial_printf("musb_rx_handler: SENTSTALL, rxcsr 0x%hx\n",
				rxcsr);
		return;
	}

	length = inw(UDC_RXCOUNT);

	if (endpoint->rcv_urb) {
		/* Receiving data */
		if (length) {
			musb_fifo_read(epnumber, &endpoint->rcv_urb->
				buffer[endpoint->rcv_urb->actual_length],
				length);
			outw(rxcsr & ~0x1, UDC_RXCSR);	/* Clear RXPKTRDY */
			usbd_rcv_complete(endpoint, length, 0);
		}
	} else {
		serial_printf("%s: no receive URB!\n", __FUNCTION__);
	}
}

static void musb_reset(void)
{
	usbd_device_event_irq(udc_device, DEVICE_HUB_CONFIGURED, 0);
	usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
	ep0status = IDLE;
	do_set_address = 0;
}

/* Public functions - called by usbdcore, usbtty, etc. */
void udc_irq(void)
{
	unsigned char int_usb = inb(UDC_INTRUSB);
	unsigned short int_tx = inw(UDC_INTRTX);
	unsigned short int_rx = inw(UDC_INTRRX);
	int ep;

	if (int_usb) {
		if (int_usb & 0x4) {	/* Reset */
			/* The controller clears FADDR, INDEX, and FIFOs */
			musb_reset();
		}
		if (int_usb & 0x20) {	/* Disconnected */
			usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
		}
		if (int_usb & 0x1)
			usbd_device_event_irq(udc_device,
					DEVICE_BUS_INACTIVE, 0);
		if (int_usb & 0x2)
			usbd_device_event_irq(udc_device,
					DEVICE_BUS_ACTIVITY, 0);
	}

	/* Note: IRQ values auto clear so read just before processing */
	if (int_rx) {		/* OUT endpoints */
		ep = 1;
		int_rx >>= 1;
		while (int_rx) {
			if (int_rx & 1)
				musb_rx_handler(udc_device->bus->endpoint_array
						+ ep);
			int_rx >>= 1;
			ep++;
		}
	}
	if (int_tx) {		/* IN endpoints */
		if (int_tx & 1)
			musb_ep0_handler(udc_device->bus->endpoint_array);

		ep = 1;
		int_tx >>= 1;
		while (int_tx) {
			if (int_tx & 1)
				musb_tx_handler(udc_device->bus->endpoint_array
						+ ep);
			int_tx >>= 1;
			ep++;
		}
	}
}

/* Turn on the USB connection */
void udc_connect(void)
{
	outb(0x1, UDC_DEVCTL);

	if (!(inb(UDC_DEVCTL) & 0x80)) {
		serial_printf("Error, the USB hardware is not on B mode\n");
		outb(0x0, UDC_DEVCTL);
		return;
	}
}

/* Turn off the USB connection */
void udc_disconnect(void)
{
	if (!(inb(UDC_DEVCTL) & 0x80)) {
		serial_printf("Error, the USB hardware is not on B mode");
		return;
	}

	outb(0x0, UDC_DEVCTL);
}

int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
{
	/* Transmit only if the hardware is available */
	if (endpoint->tx_urb && endpoint->state == 0)
		musb_ep_tx(endpoint);

	return 0;
}

/*
 * udc_setup_ep - setup endpoint
 *
 * Associate a physical endpoint with endpoint_instance
 */
void udc_setup_ep(struct usb_device_instance *device, unsigned int ep,
			struct usb_endpoint_instance *endpoint)
{
	int ep_addr;
	int attributes;

	/*
	 * We dont' have a way to identify if the endpoint definitions changed,
	 * so we have to always reconfigure the FIFOs to avoid problems
	 */
	musb_fifos_configure(device);

	ep_addr = endpoint->endpoint_address;
	if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
		/* IN endpoint */
		attributes = endpoint->tx_attributes;
	} else {
		/* OUT endpoint */
		attributes = endpoint->rcv_attributes;
	}

	outb(ep & 0xF, UDC_INDEX);
	if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
		/* IN endpoint */
		if (!ep) {		/* This only apply for ep != 0 */
			/* Empty fifo twice on case of previous double buffer */
			outw(1<<3, UDC_TXCSR);
			outw(1<<3, UDC_TXCSR);

			if (attributes & USB_ENDPOINT_XFER_ISOC)
				outw(inw(UDC_TXCSR) | (1 << 13) | (1 << 14) |
					0x4, UDC_TXCSR);
			else
				outw((inw(UDC_TXCSR) | (1 << 13) | 0x4) &
					~(1 << 14), UDC_TXCSR);
		}
		/* Enable interrupt */
		outw(inw(UDC_INTRTXE) | (1 << ep), UDC_INTRTXE);
	} else {
		/* OUT endpoint */
		if (!ep) {		/* This only apply for ep != 0 */
			if (attributes & USB_ENDPOINT_XFER_ISOC)
				outw((inw(UDC_RXCSR) | (1 << 14)) & ~(1 << 13),
					UDC_RXCSR);
			else
				outw(inw(UDC_RXCSR) & ~(1 << 14) & ~(1 << 13),
					UDC_RXCSR);
		}
		/* Enable interrupt */
		outw(inw(UDC_INTRRXE) | (1 << ep), UDC_INTRRXE);
	}
}

/*
 * udc_startup_events - 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 MUSB 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.
	 */

	/* 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);
	}

	/* Enable control interrupts */
	outb(0xf7, UDC_INTRUSBE);
}

void udc_set_nak(int epid)
{
/*
 * On MUSB the NAKing is controlled by the USB controller buffers,
 * so as long as we don't read data from the FIFO, the controller will NAK.
 * Nothing to see here, move along...
 */
}

void udc_unset_nak(int epid)
{
/*
 * On MUSB the NAKing is controlled by the USB controller buffers,
 * so as long as we don't read data from the FIFO, the controller will NAK.
 * Nothing to see here, move along...
 */
}

/* Start to initialize h/w stuff */
int udc_init(void)
{
	/* Clock is initialized on the board code */

#if 0
	/* XXX: this is platform specific, move to udc_musb_platform_init */
	/* MUSB soft-reset */
	outl(2, UDC_SYSCONFIG);
	/* MUSB end any previous session */
	outb(0x0, UDC_DEVCTL);
#endif

	if (udc_musb_platform_init()) {
		serial_printf("udc_init: platform init failed\n");
		return -1;
	}

#if 0
	/* XXX: this is platform specific, move to udc_musb_platform_init */
	outl(inl(UDC_FORCESTDBY) & ~1, UDC_FORCESTDBY); /* disable MSTANDBY */
	outl(inl(UDC_SYSCONFIG) | (2<<12), UDC_SYSCONFIG); /* ena SMARTSTDBY */
	outl(inl(UDC_SYSCONFIG) & ~1, UDC_SYSCONFIG); /* disable AUTOIDLE */
	outl(inl(UDC_SYSCONFIG) | (2<<3), UDC_SYSCONFIG); /* enable SMARTIDLE */
	outl(inl(UDC_SYSCONFIG) | 1, UDC_SYSCONFIG); /* enable AUTOIDLE */

	/* Configure the PHY as PHY interface is 12-pin, 8-bit SDR ULPI */
	sr32((void *)UDC_INTERFSEL, 0, 1, 1);
#endif

#if 0
	/* Turn off interrupts */
	outw(0x00, UDC_INTRTXE);
	outw(0x00, UDC_INTRRXE);

#if CONFIG_MUSB_FULL_SPEED
	/*
	 * Use Full speed for debugging proposes, useful so most USB
	 * analyzers can catch the transactions
	 */
	outb(0, UDC_POWER);
	serial_printf("MUSB: using full speed\n");
#else
	serial_printf("MUSB: using high speed\n");
#endif
#endif

	return 0;
}