xref: /openbmc/u-boot/drivers/usb/musb/musb_udc.c (revision bfacf466)
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 = readw(&musbr->ep[ep].epN.rxcount);
644 
645 	if (peri_rxcount) {
646 		struct usb_endpoint_instance *endpoint;
647 		u32 length;
648 		u8 *data;
649 
650 		endpoint = GET_ENDPOINT(udc_device, ep);
651 		if (endpoint && endpoint->rcv_urb) {
652 			struct urb *urb = endpoint->rcv_urb;
653 			unsigned int remaining_space = urb->buffer_length -
654 				urb->actual_length;
655 
656 			if (remaining_space) {
657 				int urb_bad = 0; /* urb is good */
658 
659 				if (peri_rxcount > remaining_space)
660 					length = remaining_space;
661 				else
662 					length = peri_rxcount;
663 
664 				data = (u8 *) urb->buffer_data;
665 				data += urb->actual_length;
666 
667 				/* The common musb fifo reader */
668 				read_fifo(ep, length, data);
669 
670 				musb_peri_rx_ack(ep);
671 
672 				/*
673 				 * urb's actual_length is updated in
674 				 * usbd_rcv_complete
675 				 */
676 				usbd_rcv_complete(endpoint, length, urb_bad);
677 
678 			} else {
679 				if (debug_level > 0)
680 					serial_printf("ERROR : %s %d no space "
681 						      "in rcv buffer\n",
682 						      __PRETTY_FUNCTION__, ep);
683 			}
684 		} else {
685 			if (debug_level > 0)
686 				serial_printf("ERROR : %s %d problem with "
687 					      "endpoint\n",
688 					      __PRETTY_FUNCTION__, ep);
689 		}
690 
691 	} else {
692 		if (debug_level > 0)
693 			serial_printf("ERROR : %s %d with nothing to do\n",
694 				      __PRETTY_FUNCTION__, ep);
695 	}
696 }
697 
698 static void musb_peri_rx(u16 intr)
699 {
700 	unsigned int ep;
701 
702 	/* Check for EP0 */
703 	if (0x01 & intr)
704 		musb_peri_ep0();
705 
706 	for (ep = 1; ep < 16; ep++) {
707 		if ((1 << ep) & intr)
708 			musb_peri_rx_ep(ep);
709 	}
710 }
711 
712 static void musb_peri_tx(u16 intr)
713 {
714 	/* Check for EP0 */
715 	if (0x01 & intr)
716 		musb_peri_ep0_tx();
717 
718 	/*
719 	 * Use this in the future when handling epN tx
720 	 *
721 	 * u8 ep;
722 	 *
723 	 * for (ep = 1; ep < 16; ep++) {
724 	 *	if ((1 << ep) & intr) {
725 	 *		/ * handle tx for this endpoint * /
726 	 *	}
727 	 * }
728 	 */
729 }
730 
731 void udc_irq(void)
732 {
733 	/* This is a high freq called function */
734 	if (enabled) {
735 		u8 intrusb;
736 
737 		intrusb = readb(&musbr->intrusb);
738 
739 		/*
740 		 * See drivers/usb/gadget/mpc8xx_udc.c for
741 		 * state diagram going from detached through
742 		 * configuration.
743 		 */
744 		if (MUSB_INTR_RESUME & intrusb) {
745 			usbd_device_event_irq(udc_device,
746 					      DEVICE_BUS_ACTIVITY, 0);
747 			musb_peri_resume();
748 		}
749 
750 		musb_peri_ep0();
751 
752 		if (MUSB_INTR_RESET & intrusb) {
753 			usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
754 			musb_peri_reset();
755 		}
756 
757 		if (MUSB_INTR_DISCONNECT & intrusb) {
758 			/* cable unplugged from hub/host */
759 			usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
760 			musb_peri_reset();
761 			usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
762 		}
763 
764 		if (MUSB_INTR_SOF & intrusb) {
765 			usbd_device_event_irq(udc_device,
766 					      DEVICE_BUS_ACTIVITY, 0);
767 			musb_peri_resume();
768 		}
769 
770 		if (MUSB_INTR_SUSPEND & intrusb) {
771 			usbd_device_event_irq(udc_device,
772 					      DEVICE_BUS_INACTIVE, 0);
773 		}
774 
775 		if (ep0_state != SET_ADDRESS) {
776 			u16 intrrx, intrtx;
777 
778 			intrrx = readw(&musbr->intrrx);
779 			intrtx = readw(&musbr->intrtx);
780 
781 			if (intrrx)
782 				musb_peri_rx(intrrx);
783 
784 			if (intrtx)
785 				musb_peri_tx(intrtx);
786 		} else {
787 			if (MUSB_INTR_SOF & intrusb) {
788 				u8 faddr;
789 				faddr = readb(&musbr->faddr);
790 				/*
791 				 * Setting of the address can fail.
792 				 * Normally it succeeds the second time.
793 				 */
794 				if (udc_device->address != faddr)
795 					musb_peri_ep0_set_address();
796 			}
797 		}
798 	}
799 }
800 
801 void udc_set_nak(int ep_num)
802 {
803 	/* noop */
804 }
805 
806 void udc_unset_nak(int ep_num)
807 {
808 	/* noop */
809 }
810 
811 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
812 {
813 	int ret = 0;
814 
815 	/* Transmit only if the hardware is available */
816 	if (endpoint->tx_urb && endpoint->state == 0) {
817 		unsigned int ep = endpoint->endpoint_address &
818 			USB_ENDPOINT_NUMBER_MASK;
819 
820 		u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
821 
822 		/* Error conditions */
823 		if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
824 			peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
825 			writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
826 		}
827 
828 		if (debug_level > 1)
829 			musb_print_txcsr(peri_txcsr);
830 
831 		/* Check if a packet is waiting to be sent */
832 		if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
833 			u32 length;
834 			u8 *data;
835 			struct urb *urb = endpoint->tx_urb;
836 			unsigned int remaining_packet = urb->actual_length -
837 				endpoint->sent;
838 
839 			if (endpoint->tx_packetSize < remaining_packet)
840 				length = endpoint->tx_packetSize;
841 			else
842 				length = remaining_packet;
843 
844 			data = (u8 *) urb->buffer;
845 			data += endpoint->sent;
846 
847 			/* common musb fifo function */
848 			write_fifo(ep, length, data);
849 
850 			musb_peri_tx_ready(ep);
851 
852 			endpoint->last = length;
853 			/* usbd_tx_complete will take care of updating 'sent' */
854 			usbd_tx_complete(endpoint);
855 		}
856 	} else {
857 		if (debug_level > 0)
858 			serial_printf("ERROR : %s Problem with urb %p "
859 				      "or ep state %d\n",
860 				      __PRETTY_FUNCTION__,
861 				      endpoint->tx_urb, endpoint->state);
862 	}
863 
864 	return ret;
865 }
866 
867 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
868 		  struct usb_endpoint_instance *endpoint)
869 {
870 	if (0 == id) {
871 		/* EP0 */
872 		ep0_endpoint = endpoint;
873 		ep0_endpoint->endpoint_address = 0xff;
874 		ep0_urb = usbd_alloc_urb(device, endpoint);
875 	} else if (MAX_ENDPOINT >= id) {
876 		int ep_addr;
877 
878 		/* Check the direction */
879 		ep_addr = endpoint->endpoint_address;
880 		if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
881 			/* IN */
882 			epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
883 		} else {
884 			/* OUT */
885 			epinfo[id * 2].epsize = endpoint->rcv_packetSize;
886 		}
887 
888 		musb_configure_ep(&epinfo[0],
889 				  sizeof(epinfo) / sizeof(struct musb_epinfo));
890 	} else {
891 		if (debug_level > 0)
892 			serial_printf("ERROR : %s endpoint request %d "
893 				      "exceeds maximum %d\n",
894 				      __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
895 	}
896 }
897 
898 void udc_connect(void)
899 {
900 	/* noop */
901 }
902 
903 void udc_disconnect(void)
904 {
905 	/* noop */
906 }
907 
908 void udc_enable(struct usb_device_instance *device)
909 {
910 	/* Save the device structure pointer */
911 	udc_device = device;
912 
913 	enabled = 1;
914 }
915 
916 void udc_disable(void)
917 {
918 	enabled = 0;
919 }
920 
921 void udc_startup_events(struct usb_device_instance *device)
922 {
923 	/* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
924 	usbd_device_event_irq(device, DEVICE_INIT, 0);
925 
926 	/*
927 	 * The DEVICE_CREATE event puts the USB device in the state
928 	 * STATE_ATTACHED.
929 	 */
930 	usbd_device_event_irq(device, DEVICE_CREATE, 0);
931 
932 	/* Resets the address to 0 */
933 	usbd_device_event_irq(device, DEVICE_RESET, 0);
934 
935 	udc_enable(device);
936 }
937 
938 int udc_init(void)
939 {
940 	int ret;
941 	int ep_loop;
942 
943 	ret = musb_platform_init();
944 	if (ret < 0)
945 		goto end;
946 
947 	/* Configure all the endpoint FIFO's and start usb controller */
948 	musbr = musb_cfg.regs;
949 
950 	/* Initialize the endpoints */
951 	for (ep_loop = 0; ep_loop < MAX_ENDPOINT * 2; ep_loop++) {
952 		epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
953 		epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
954 		epinfo[ep_loop].epsize = 0;
955 	}
956 
957 	musb_peri_softconnect();
958 
959 	ret = 0;
960 end:
961 
962 	return ret;
963 }
964