xref: /openbmc/u-boot/drivers/usb/musb/musb_udc.c (revision fbbbc86e)
1 /*
2  * Copyright (c) 2009 Wind River Systems, Inc.
3  * Tom Rix <Tom.Rix@windriver.com>
4  *
5  * This file is a rewrite of the usb device part of
6  * repository git.omapzoom.org/repo/u-boot.git, branch master,
7  * file cpu/omap3/fastboot.c
8  *
9  * This is the unique part of its copyright :
10  *
11  * -------------------------------------------------------------------------
12  *
13  * (C) Copyright 2008 - 2009
14  * Windriver, <www.windriver.com>
15  * Tom Rix <Tom.Rix@windriver.com>
16  *
17  * -------------------------------------------------------------------------
18  *
19  * The details of connecting the device to the uboot usb device subsystem
20  * came from the old omap3 repository www.sakoman.net/u-boot-omap3.git,
21  * branch omap3-dev-usb, file drivers/usb/usbdcore_musb.c
22  *
23  * This is the unique part of its copyright :
24  *
25  * -------------------------------------------------------------------------
26  *
27  * (C) Copyright 2008 Texas Instruments Incorporated.
28  *
29  * Based on
30  * u-boot OMAP1510 USB drivers (drivers/usbdcore_omap1510.c)
31  * twl4030 init based on linux (drivers/i2c/chips/twl4030_usb.c)
32  *
33  * Author: Diego Dompe (diego.dompe@ridgerun.com)
34  *         Atin Malaviya (atin.malaviya@gmail.com)
35  *
36  * -------------------------------------------------------------------------
37  *
38  * This program is free software; you can redistribute it and/or
39  * modify it under the terms of the GNU General Public License as
40  * published by the Free Software Foundation; either version 2 of
41  * the License, or (at your option) any later version.
42  *
43  * This program is distributed in the hope that it will be useful,
44  * but WITHOUT ANY WARRANTY; without even the implied warranty of
45  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46  * GNU General Public License for more details.
47  *
48  * You should have received a copy of the GNU General Public License
49  * along with this program; if not, write to the Free Software
50  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
51  * MA 02111-1307 USA
52  */
53 
54 #include <common.h>
55 #include <usb/musb_udc.h>
56 #include "../gadget/ep0.h"
57 #include "musb_core.h"
58 #if defined(CONFIG_USB_OMAP3)
59 #include "omap3.h"
60 #elif defined(CONFIG_USB_AM35X)
61 #include "am35x.h"
62 #elif defined(CONFIG_USB_DAVINCI)
63 #include "davinci.h"
64 #endif
65 
66 /* Define MUSB_DEBUG for debugging */
67 /* #define MUSB_DEBUG */
68 #include "musb_debug.h"
69 
70 #define MAX_ENDPOINT 15
71 
72 #define GET_ENDPOINT(dev,ep)						\
73 (((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
74 
75 #define SET_EP0_STATE(s)						\
76 do {									\
77 	if ((0 <= (s)) && (SET_ADDRESS >= (s))) {			\
78 		if ((s) != ep0_state) {					\
79 			if ((debug_setup) && (debug_level > 1))		\
80 				serial_printf("INFO : Changing state "  \
81 					      "from %s to %s in %s at " \
82 					      "line %d\n",		\
83 					      ep0_state_strings[ep0_state],\
84 					      ep0_state_strings[s],	\
85 					      __PRETTY_FUNCTION__,	\
86 					      __LINE__);		\
87 			ep0_state = s;					\
88 		}							\
89 	} else {							\
90 		if (debug_level > 0)					\
91 			serial_printf("Error at %s %d with setting "	\
92 				      "state %d is invalid\n",		\
93 				      __PRETTY_FUNCTION__, __LINE__, s); \
94 	}								\
95 } while (0)
96 
97 /* static implies these initialized to 0 or NULL */
98 static int debug_setup;
99 static int debug_level;
100 static struct musb_epinfo epinfo[MAX_ENDPOINT * 2];
101 static enum ep0_state_enum {
102 	IDLE = 0,
103 	TX,
104 	RX,
105 	SET_ADDRESS
106 } ep0_state = IDLE;
107 static char *ep0_state_strings[4] = {
108 	"IDLE",
109 	"TX",
110 	"RX",
111 	"SET_ADDRESS",
112 };
113 
114 static struct urb *ep0_urb;
115 struct usb_endpoint_instance *ep0_endpoint;
116 static struct usb_device_instance *udc_device;
117 static int enabled;
118 
119 #ifdef MUSB_DEBUG
120 static void musb_db_regs(void)
121 {
122 	u8 b;
123 	u16 w;
124 
125 	b = readb(&musbr->faddr);
126 	serial_printf("\tfaddr   0x%2.2x\n", b);
127 
128 	b = readb(&musbr->power);
129 	musb_print_pwr(b);
130 
131 	w = readw(&musbr->ep[0].ep0.csr0);
132 	musb_print_csr0(w);
133 
134 	b = readb(&musbr->devctl);
135 	musb_print_devctl(b);
136 
137 	b = readb(&musbr->ep[0].ep0.configdata);
138 	musb_print_config(b);
139 
140 	w = readw(&musbr->frame);
141 	serial_printf("\tframe   0x%4.4x\n", w);
142 
143 	b = readb(&musbr->index);
144 	serial_printf("\tindex   0x%2.2x\n", b);
145 
146 	w = readw(&musbr->ep[1].epN.rxmaxp);
147 	musb_print_rxmaxp(w);
148 
149 	w = readw(&musbr->ep[1].epN.rxcsr);
150 	musb_print_rxcsr(w);
151 
152 	w = readw(&musbr->ep[1].epN.txmaxp);
153 	musb_print_txmaxp(w);
154 
155 	w = readw(&musbr->ep[1].epN.txcsr);
156 	musb_print_txcsr(w);
157 }
158 #else
159 #define musb_db_regs()
160 #endif /* DEBUG_MUSB */
161 
162 static void musb_peri_softconnect(void)
163 {
164 	u8 power, devctl;
165 
166 	/* Power off MUSB */
167 	power = readb(&musbr->power);
168 	power &= ~MUSB_POWER_SOFTCONN;
169 	writeb(power, &musbr->power);
170 
171 	/* Read intr to clear */
172 	readb(&musbr->intrusb);
173 	readw(&musbr->intrrx);
174 	readw(&musbr->intrtx);
175 
176 	udelay(1000 * 1000); /* 1 sec */
177 
178 	/* Power on MUSB */
179 	power = readb(&musbr->power);
180 	power |= MUSB_POWER_SOFTCONN;
181 	/*
182 	 * The usb device interface is usb 1.1
183 	 * Disable 2.0 high speed by clearring the hsenable bit.
184 	 */
185 	power &= ~MUSB_POWER_HSENAB;
186 	writeb(power, &musbr->power);
187 
188 	/* Check if device is in b-peripheral mode */
189 	devctl = readb(&musbr->devctl);
190 	if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
191 	    (devctl & MUSB_DEVCTL_HM)) {
192 		serial_printf("ERROR : Unsupport USB mode\n");
193 		serial_printf("Check that mini-B USB cable is attached "
194 			      "to the device\n");
195 	}
196 
197 	if (debug_setup && (debug_level > 1))
198 		musb_db_regs();
199 }
200 
201 static void musb_peri_reset(void)
202 {
203 	if ((debug_setup) && (debug_level > 1))
204 		serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
205 
206 	if (ep0_endpoint)
207 		ep0_endpoint->endpoint_address = 0xff;
208 
209 	/* Sync sw and hw addresses */
210 	writeb(udc_device->address, &musbr->faddr);
211 
212 	SET_EP0_STATE(IDLE);
213 }
214 
215 static void musb_peri_resume(void)
216 {
217 	/* noop */
218 }
219 
220 static void musb_peri_ep0_stall(void)
221 {
222 	u16 csr0;
223 
224 	csr0 = readw(&musbr->ep[0].ep0.csr0);
225 	csr0 |= MUSB_CSR0_P_SENDSTALL;
226 	writew(csr0, &musbr->ep[0].ep0.csr0);
227 	if ((debug_setup) && (debug_level > 1))
228 		serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
229 }
230 
231 static void musb_peri_ep0_ack_req(void)
232 {
233 	u16 csr0;
234 
235 	csr0 = readw(&musbr->ep[0].ep0.csr0);
236 	csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
237 	writew(csr0, &musbr->ep[0].ep0.csr0);
238 }
239 
240 static void musb_ep0_tx_ready(void)
241 {
242 	u16 csr0;
243 
244 	csr0 = readw(&musbr->ep[0].ep0.csr0);
245 	csr0 |= MUSB_CSR0_TXPKTRDY;
246 	writew(csr0, &musbr->ep[0].ep0.csr0);
247 }
248 
249 static void musb_ep0_tx_ready_and_last(void)
250 {
251 	u16 csr0;
252 
253 	csr0 = readw(&musbr->ep[0].ep0.csr0);
254 	csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
255 	writew(csr0, &musbr->ep[0].ep0.csr0);
256 }
257 
258 static void musb_peri_ep0_last(void)
259 {
260 	u16 csr0;
261 
262 	csr0 = readw(&musbr->ep[0].ep0.csr0);
263 	csr0 |= MUSB_CSR0_P_DATAEND;
264 	writew(csr0, &musbr->ep[0].ep0.csr0);
265 }
266 
267 static void musb_peri_ep0_set_address(void)
268 {
269 	u8 faddr;
270 	writeb(udc_device->address, &musbr->faddr);
271 
272 	/* Verify */
273 	faddr = readb(&musbr->faddr);
274 	if (udc_device->address == faddr) {
275 		SET_EP0_STATE(IDLE);
276 		usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
277 		if ((debug_setup) && (debug_level > 1))
278 			serial_printf("INFO : %s Address set to %d\n",
279 				      __PRETTY_FUNCTION__, udc_device->address);
280 	} else {
281 		if (debug_level > 0)
282 			serial_printf("ERROR : %s Address missmatch "
283 				      "sw %d vs hw %d\n",
284 				      __PRETTY_FUNCTION__,
285 				      udc_device->address, faddr);
286 	}
287 }
288 
289 static void musb_peri_rx_ack(unsigned int ep)
290 {
291 	u16 peri_rxcsr;
292 
293 	peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
294 	peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
295 	writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
296 }
297 
298 static void musb_peri_tx_ready(unsigned int ep)
299 {
300 	u16 peri_txcsr;
301 
302 	peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
303 	peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
304 	writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
305 }
306 
307 static void musb_peri_ep0_zero_data_request(int err)
308 {
309 	musb_peri_ep0_ack_req();
310 
311 	if (err) {
312 		musb_peri_ep0_stall();
313 		SET_EP0_STATE(IDLE);
314 	} else {
315 
316 		musb_peri_ep0_last();
317 
318 		/* USBD state */
319 		switch (ep0_urb->device_request.bRequest) {
320 		case USB_REQ_SET_ADDRESS:
321 			if ((debug_setup) && (debug_level > 1))
322 				serial_printf("INFO : %s received set "
323 					      "address\n", __PRETTY_FUNCTION__);
324 			break;
325 
326 		case USB_REQ_SET_CONFIGURATION:
327 			if ((debug_setup) && (debug_level > 1))
328 				serial_printf("INFO : %s Configured\n",
329 					      __PRETTY_FUNCTION__);
330 			usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
331 			break;
332 		}
333 
334 		/* EP0 state */
335 		if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
336 			SET_EP0_STATE(SET_ADDRESS);
337 		} else {
338 			SET_EP0_STATE(IDLE);
339 		}
340 	}
341 }
342 
343 static void musb_peri_ep0_rx_data_request(void)
344 {
345 	/*
346 	 * This is the completion of the data OUT / RX
347 	 *
348 	 * Host is sending data to ep0 that is not
349 	 * part of setup.  This comes from the cdc_recv_setup
350 	 * op that is device specific.
351 	 *
352 	 */
353 	musb_peri_ep0_ack_req();
354 
355 	ep0_endpoint->rcv_urb = ep0_urb;
356 	ep0_urb->actual_length = 0;
357 	SET_EP0_STATE(RX);
358 }
359 
360 static void musb_peri_ep0_tx_data_request(int err)
361 {
362 	if (err) {
363 		musb_peri_ep0_stall();
364 		SET_EP0_STATE(IDLE);
365 	} else {
366 		musb_peri_ep0_ack_req();
367 
368 		ep0_endpoint->tx_urb = ep0_urb;
369 		ep0_endpoint->sent = 0;
370 		SET_EP0_STATE(TX);
371 	}
372 }
373 
374 static void musb_peri_ep0_idle(void)
375 {
376 	u16 count0;
377 	int err;
378 	u16 csr0;
379 
380 	/*
381 	 * Verify addresses
382 	 * A lot of confusion can be caused if the address
383 	 * in software, udc layer, does not agree with the
384 	 * hardware.  Since the setting of the hardware address
385 	 * must be set after the set address request, the
386 	 * usb state machine is out of sync for a few frame.
387 	 * It is a good idea to run this check when changes
388 	 * are made to the state machine.
389 	 */
390 	if ((debug_level > 0) &&
391 	    (ep0_state != SET_ADDRESS)) {
392 		u8 faddr;
393 
394 		faddr = readb(&musbr->faddr);
395 		if (udc_device->address != faddr) {
396 			serial_printf("ERROR : %s addresses do not"
397 				      "match sw %d vs hw %d\n",
398 				      __PRETTY_FUNCTION__,
399 				      udc_device->address, faddr);
400 			udelay(1000 * 1000);
401 			hang();
402 		}
403 	}
404 
405 	csr0 = readw(&musbr->ep[0].ep0.csr0);
406 
407 	if (!(MUSB_CSR0_RXPKTRDY & csr0))
408 		goto end;
409 
410 	count0 = readw(&musbr->ep[0].ep0.count0);
411 	if (count0 == 0)
412 		goto end;
413 
414 	if (count0 != 8) {
415 		if ((debug_setup) && (debug_level > 1))
416 			serial_printf("WARN : %s SETUP incorrect size %d\n",
417 				      __PRETTY_FUNCTION__, count0);
418 		musb_peri_ep0_stall();
419 		goto end;
420 	}
421 
422 	read_fifo(0, count0, &ep0_urb->device_request);
423 
424 	if (debug_level > 2)
425 		print_usb_device_request(&ep0_urb->device_request);
426 
427 	if (ep0_urb->device_request.wLength == 0) {
428 		err = ep0_recv_setup(ep0_urb);
429 
430 		/* Zero data request */
431 		musb_peri_ep0_zero_data_request(err);
432 	} else {
433 		/* Is data coming or going ? */
434 		u8 reqType = ep0_urb->device_request.bmRequestType;
435 
436 		if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
437 			err = ep0_recv_setup(ep0_urb);
438 			/* Device to host */
439 			musb_peri_ep0_tx_data_request(err);
440 		} else {
441 			/*
442 			 * Host to device
443 			 *
444 			 * The RX routine will call ep0_recv_setup
445 			 * when the data packet has arrived.
446 			 */
447 			musb_peri_ep0_rx_data_request();
448 		}
449 	}
450 
451 end:
452 	return;
453 }
454 
455 static void musb_peri_ep0_rx(void)
456 {
457 	/*
458 	 * This is the completion of the data OUT / RX
459 	 *
460 	 * Host is sending data to ep0 that is not
461 	 * part of setup.  This comes from the cdc_recv_setup
462 	 * op that is device specific.
463 	 *
464 	 * Pass the data back to driver ep0_recv_setup which
465 	 * should give the cdc_recv_setup the chance to handle
466 	 * the rx
467 	 */
468 	u16 csr0;
469 	u16 count0;
470 
471 	if (debug_level > 3) {
472 		if (0 != ep0_urb->actual_length) {
473 			serial_printf("%s finished ? %d of %d\n",
474 				      __PRETTY_FUNCTION__,
475 				      ep0_urb->actual_length,
476 				      ep0_urb->device_request.wLength);
477 		}
478 	}
479 
480 	if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
481 		musb_peri_ep0_last();
482 		SET_EP0_STATE(IDLE);
483 		ep0_recv_setup(ep0_urb);
484 		return;
485 	}
486 
487 	csr0 = readw(&musbr->ep[0].ep0.csr0);
488 	if (!(MUSB_CSR0_RXPKTRDY & csr0))
489 		return;
490 
491 	count0 = readw(&musbr->ep[0].ep0.count0);
492 
493 	if (count0) {
494 		struct usb_endpoint_instance *endpoint;
495 		u32 length;
496 		u8 *data;
497 
498 		endpoint = ep0_endpoint;
499 		if (endpoint && endpoint->rcv_urb) {
500 			struct urb *urb = endpoint->rcv_urb;
501 			unsigned int remaining_space = urb->buffer_length -
502 				urb->actual_length;
503 
504 			if (remaining_space) {
505 				int urb_bad = 0; /* urb is good */
506 
507 				if (count0 > remaining_space)
508 					length = remaining_space;
509 				else
510 					length = count0;
511 
512 				data = (u8 *) urb->buffer_data;
513 				data += urb->actual_length;
514 
515 				/* The common musb fifo reader */
516 				read_fifo(0, length, data);
517 
518 				musb_peri_ep0_ack_req();
519 
520 				/*
521 				 * urb's actual_length is updated in
522 				 * usbd_rcv_complete
523 				 */
524 				usbd_rcv_complete(endpoint, length, urb_bad);
525 
526 			} else {
527 				if (debug_level > 0)
528 					serial_printf("ERROR : %s no space in "
529 						      "rcv buffer\n",
530 						      __PRETTY_FUNCTION__);
531 			}
532 		} else {
533 			if (debug_level > 0)
534 				serial_printf("ERROR : %s problem with "
535 					      "endpoint\n",
536 					      __PRETTY_FUNCTION__);
537 		}
538 	} else {
539 		if (debug_level > 0)
540 			serial_printf("ERROR : %s with nothing to do\n",
541 				      __PRETTY_FUNCTION__);
542 	}
543 }
544 
545 static void musb_peri_ep0_tx(void)
546 {
547 	u16 csr0;
548 	int transfer_size = 0;
549 	unsigned int p, pm;
550 
551 	csr0 = readw(&musbr->ep[0].ep0.csr0);
552 
553 	/* Check for pending tx */
554 	if (csr0 & MUSB_CSR0_TXPKTRDY)
555 		goto end;
556 
557 	/* Check if this is the last packet sent */
558 	if (ep0_endpoint->sent >= ep0_urb->actual_length) {
559 		SET_EP0_STATE(IDLE);
560 		goto end;
561 	}
562 
563 	transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
564 	/* Is the transfer size negative ? */
565 	if (transfer_size <= 0) {
566 		if (debug_level > 0)
567 			serial_printf("ERROR : %s problem with the"
568 				      " transfer size %d\n",
569 				      __PRETTY_FUNCTION__,
570 				      transfer_size);
571 		SET_EP0_STATE(IDLE);
572 		goto end;
573 	}
574 
575 	/* Truncate large transfers to the fifo size */
576 	if (transfer_size > ep0_endpoint->tx_packetSize)
577 		transfer_size = ep0_endpoint->tx_packetSize;
578 
579 	write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
580 	ep0_endpoint->sent += transfer_size;
581 
582 	/* Done or more to send ? */
583 	if (ep0_endpoint->sent >= ep0_urb->actual_length)
584 		musb_ep0_tx_ready_and_last();
585 	else
586 		musb_ep0_tx_ready();
587 
588 	/* Wait a bit */
589 	pm = 10;
590 	for (p = 0; p < pm; p++) {
591 		csr0 = readw(&musbr->ep[0].ep0.csr0);
592 		if (!(csr0 & MUSB_CSR0_TXPKTRDY))
593 			break;
594 
595 		/* Double the delay. */
596 		udelay(1 << pm);
597 	}
598 
599 	if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
600 		SET_EP0_STATE(IDLE);
601 
602 end:
603 	return;
604 }
605 
606 static void musb_peri_ep0(void)
607 {
608 	u16 csr0;
609 
610 	if (SET_ADDRESS == ep0_state)
611 		return;
612 
613 	csr0 = readw(&musbr->ep[0].ep0.csr0);
614 
615 	/* Error conditions */
616 	if (MUSB_CSR0_P_SENTSTALL & csr0) {
617 		csr0 &= ~MUSB_CSR0_P_SENTSTALL;
618 		writew(csr0, &musbr->ep[0].ep0.csr0);
619 		SET_EP0_STATE(IDLE);
620 	}
621 	if (MUSB_CSR0_P_SETUPEND & csr0) {
622 		csr0 |= MUSB_CSR0_P_SVDSETUPEND;
623 		writew(csr0, &musbr->ep[0].ep0.csr0);
624 		SET_EP0_STATE(IDLE);
625 		if ((debug_setup) && (debug_level > 1))
626 			serial_printf("WARN: %s SETUPEND\n",
627 				      __PRETTY_FUNCTION__);
628 	}
629 
630 	/* Normal states */
631 	if (IDLE == ep0_state)
632 		musb_peri_ep0_idle();
633 
634 	if (TX == ep0_state)
635 		musb_peri_ep0_tx();
636 
637 	if (RX == ep0_state)
638 		musb_peri_ep0_rx();
639 }
640 
641 static void musb_peri_rx_ep(unsigned int ep)
642 {
643 	u16 peri_rxcount;
644 	u8 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
645 
646 	if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
647 		if (debug_level > 0)
648 			serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
649 				      __PRETTY_FUNCTION__, ep);
650 		return;
651 	}
652 
653 	peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
654 	if (peri_rxcount) {
655 		struct usb_endpoint_instance *endpoint;
656 		u32 length;
657 		u8 *data;
658 
659 		endpoint = GET_ENDPOINT(udc_device, ep);
660 		if (endpoint && endpoint->rcv_urb) {
661 			struct urb *urb = endpoint->rcv_urb;
662 			unsigned int remaining_space = urb->buffer_length -
663 				urb->actual_length;
664 
665 			if (remaining_space) {
666 				int urb_bad = 0; /* urb is good */
667 
668 				if (peri_rxcount > remaining_space)
669 					length = remaining_space;
670 				else
671 					length = peri_rxcount;
672 
673 				data = (u8 *) urb->buffer_data;
674 				data += urb->actual_length;
675 
676 				/* The common musb fifo reader */
677 				read_fifo(ep, length, data);
678 
679 				musb_peri_rx_ack(ep);
680 
681 				/*
682 				 * urb's actual_length is updated in
683 				 * usbd_rcv_complete
684 				 */
685 				usbd_rcv_complete(endpoint, length, urb_bad);
686 
687 			} else {
688 				if (debug_level > 0)
689 					serial_printf("ERROR : %s %d no space "
690 						      "in rcv buffer\n",
691 						      __PRETTY_FUNCTION__, ep);
692 			}
693 		} else {
694 			if (debug_level > 0)
695 				serial_printf("ERROR : %s %d problem with "
696 					      "endpoint\n",
697 					      __PRETTY_FUNCTION__, ep);
698 		}
699 
700 	} else {
701 		if (debug_level > 0)
702 			serial_printf("ERROR : %s %d with nothing to do\n",
703 				      __PRETTY_FUNCTION__, ep);
704 	}
705 }
706 
707 static void musb_peri_rx(u16 intr)
708 {
709 	unsigned int ep;
710 
711 	/* Check for EP0 */
712 	if (0x01 & intr)
713 		musb_peri_ep0();
714 
715 	for (ep = 1; ep < 16; ep++) {
716 		if ((1 << ep) & intr)
717 			musb_peri_rx_ep(ep);
718 	}
719 }
720 
721 static void musb_peri_tx(u16 intr)
722 {
723 	/* Check for EP0 */
724 	if (0x01 & intr)
725 		musb_peri_ep0_tx();
726 
727 	/*
728 	 * Use this in the future when handling epN tx
729 	 *
730 	 * u8 ep;
731 	 *
732 	 * for (ep = 1; ep < 16; ep++) {
733 	 *	if ((1 << ep) & intr) {
734 	 *		/ * handle tx for this endpoint * /
735 	 *	}
736 	 * }
737 	 */
738 }
739 
740 void udc_irq(void)
741 {
742 	/* This is a high freq called function */
743 	if (enabled) {
744 		u8 intrusb;
745 
746 		intrusb = readb(&musbr->intrusb);
747 
748 		/*
749 		 * See drivers/usb/gadget/mpc8xx_udc.c for
750 		 * state diagram going from detached through
751 		 * configuration.
752 		 */
753 		if (MUSB_INTR_RESUME & intrusb) {
754 			usbd_device_event_irq(udc_device,
755 					      DEVICE_BUS_ACTIVITY, 0);
756 			musb_peri_resume();
757 		}
758 
759 		musb_peri_ep0();
760 
761 		if (MUSB_INTR_RESET & intrusb) {
762 			usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
763 			musb_peri_reset();
764 		}
765 
766 		if (MUSB_INTR_DISCONNECT & intrusb) {
767 			/* cable unplugged from hub/host */
768 			usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
769 			musb_peri_reset();
770 			usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
771 		}
772 
773 		if (MUSB_INTR_SOF & intrusb) {
774 			usbd_device_event_irq(udc_device,
775 					      DEVICE_BUS_ACTIVITY, 0);
776 			musb_peri_resume();
777 		}
778 
779 		if (MUSB_INTR_SUSPEND & intrusb) {
780 			usbd_device_event_irq(udc_device,
781 					      DEVICE_BUS_INACTIVE, 0);
782 		}
783 
784 		if (ep0_state != SET_ADDRESS) {
785 			u16 intrrx, intrtx;
786 
787 			intrrx = readw(&musbr->intrrx);
788 			intrtx = readw(&musbr->intrtx);
789 
790 			if (intrrx)
791 				musb_peri_rx(intrrx);
792 
793 			if (intrtx)
794 				musb_peri_tx(intrtx);
795 		} else {
796 			if (MUSB_INTR_SOF & intrusb) {
797 				u8 faddr;
798 				faddr = readb(&musbr->faddr);
799 				/*
800 				 * Setting of the address can fail.
801 				 * Normally it succeeds the second time.
802 				 */
803 				if (udc_device->address != faddr)
804 					musb_peri_ep0_set_address();
805 			}
806 		}
807 	}
808 }
809 
810 void udc_set_nak(int ep_num)
811 {
812 	/* noop */
813 }
814 
815 void udc_unset_nak(int ep_num)
816 {
817 	/* noop */
818 }
819 
820 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
821 {
822 	int ret = 0;
823 
824 	/* Transmit only if the hardware is available */
825 	if (endpoint->tx_urb && endpoint->state == 0) {
826 		unsigned int ep = endpoint->endpoint_address &
827 			USB_ENDPOINT_NUMBER_MASK;
828 
829 		u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
830 
831 		/* Error conditions */
832 		if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
833 			peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
834 			writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
835 		}
836 
837 		if (debug_level > 1)
838 			musb_print_txcsr(peri_txcsr);
839 
840 		/* Check if a packet is waiting to be sent */
841 		if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
842 			u32 length;
843 			u8 *data;
844 			struct urb *urb = endpoint->tx_urb;
845 			unsigned int remaining_packet = urb->actual_length -
846 				endpoint->sent;
847 
848 			if (endpoint->tx_packetSize < remaining_packet)
849 				length = endpoint->tx_packetSize;
850 			else
851 				length = remaining_packet;
852 
853 			data = (u8 *) urb->buffer;
854 			data += endpoint->sent;
855 
856 			/* common musb fifo function */
857 			write_fifo(ep, length, data);
858 
859 			musb_peri_tx_ready(ep);
860 
861 			endpoint->last = length;
862 			/* usbd_tx_complete will take care of updating 'sent' */
863 			usbd_tx_complete(endpoint);
864 		}
865 	} else {
866 		if (debug_level > 0)
867 			serial_printf("ERROR : %s Problem with urb %p "
868 				      "or ep state %d\n",
869 				      __PRETTY_FUNCTION__,
870 				      endpoint->tx_urb, endpoint->state);
871 	}
872 
873 	return ret;
874 }
875 
876 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
877 		  struct usb_endpoint_instance *endpoint)
878 {
879 	if (0 == id) {
880 		/* EP0 */
881 		ep0_endpoint = endpoint;
882 		ep0_endpoint->endpoint_address = 0xff;
883 		ep0_urb = usbd_alloc_urb(device, endpoint);
884 	} else if (MAX_ENDPOINT >= id) {
885 		int ep_addr;
886 
887 		/* Check the direction */
888 		ep_addr = endpoint->endpoint_address;
889 		if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
890 			/* IN */
891 			epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
892 		} else {
893 			/* OUT */
894 			epinfo[id * 2].epsize = endpoint->rcv_packetSize;
895 		}
896 
897 		musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
898 	} else {
899 		if (debug_level > 0)
900 			serial_printf("ERROR : %s endpoint request %d "
901 				      "exceeds maximum %d\n",
902 				      __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
903 	}
904 }
905 
906 void udc_connect(void)
907 {
908 	/* noop */
909 }
910 
911 void udc_disconnect(void)
912 {
913 	/* noop */
914 }
915 
916 void udc_enable(struct usb_device_instance *device)
917 {
918 	/* Save the device structure pointer */
919 	udc_device = device;
920 
921 	enabled = 1;
922 }
923 
924 void udc_disable(void)
925 {
926 	enabled = 0;
927 }
928 
929 void udc_startup_events(struct usb_device_instance *device)
930 {
931 	/* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
932 	usbd_device_event_irq(device, DEVICE_INIT, 0);
933 
934 	/*
935 	 * The DEVICE_CREATE event puts the USB device in the state
936 	 * STATE_ATTACHED.
937 	 */
938 	usbd_device_event_irq(device, DEVICE_CREATE, 0);
939 
940 	/* Resets the address to 0 */
941 	usbd_device_event_irq(device, DEVICE_RESET, 0);
942 
943 	udc_enable(device);
944 }
945 
946 int udc_init(void)
947 {
948 	int ret;
949 	int ep_loop;
950 
951 	ret = musb_platform_init();
952 	if (ret < 0)
953 		goto end;
954 
955 	/* Configure all the endpoint FIFO's and start usb controller */
956 	musbr = musb_cfg.regs;
957 
958 	/* Initialize the endpoints */
959 	for (ep_loop = 0; ep_loop < MAX_ENDPOINT * 2; ep_loop++) {
960 		epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
961 		epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
962 		epinfo[ep_loop].epsize = 0;
963 	}
964 
965 	musb_peri_softconnect();
966 
967 	ret = 0;
968 end:
969 
970 	return ret;
971 }
972