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