]> git.sur5r.net Git - u-boot/blob - drivers/usb/gadget/omap1510_udc.c
omap: clean-up dead configs
[u-boot] / drivers / usb / gadget / omap1510_udc.c
1 /*
2  * (C) Copyright 2003
3  * Gerry Hamel, geh@ti.com, Texas Instruments
4  *
5  * Based on
6  * linux/drivers/usb/device/bi/omap.c
7  * TI OMAP1510 USB bus interface driver
8  *
9  * Author: MontaVista Software, Inc.
10  *         source@mvista.com
11  *         (C) Copyright 2002
12  *
13  * SPDX-License-Identifier:     GPL-2.0+
14  */
15
16 #include <common.h>
17 #include <asm/io.h>
18 #include <usbdevice.h>
19 #include <usb/omap1510_udc.h>
20 #include <usb/udc.h>
21
22 #include "ep0.h"
23
24
25 #define UDC_INIT_MDELAY              80 /* Device settle delay */
26 #define UDC_MAX_ENDPOINTS            31 /* Number of endpoints on this UDC */
27
28 /* Some kind of debugging output... */
29 #if 1
30 #define UDCDBG(str)
31 #define UDCDBGA(fmt,args...)
32 #else  /* The bugs still exists... */
33 #define UDCDBG(str) serial_printf("[%s] %s:%d: " str "\n", __FILE__,__FUNCTION__,__LINE__)
34 #define UDCDBGA(fmt,args...) serial_printf("[%s] %s:%d: " fmt "\n", __FILE__,__FUNCTION__,__LINE__, ##args)
35 #endif
36
37 #if 1
38 #define UDCREG(name)
39 #define UDCREGL(name)
40 #else  /* The bugs still exists... */
41 #define UDCREG(name)     serial_printf("%s():%d: %s[%08x]=%.4x\n",__FUNCTION__,__LINE__, (#name), name, inw(name))      /* For 16-bit regs */
42 #define UDCREGL(name)    serial_printf("%s():%d: %s[%08x]=%.8x\n",__FUNCTION__,__LINE__, (#name), name, inl(name))      /* For 32-bit regs */
43 #endif
44
45
46 static struct urb *ep0_urb = NULL;
47
48 static struct usb_device_instance *udc_device;  /* Used in interrupt handler */
49 static u16 udc_devstat = 0;     /* UDC status (DEVSTAT) */
50 static u32 udc_interrupts = 0;
51
52 static void udc_stall_ep (unsigned int ep_addr);
53
54
55 static struct usb_endpoint_instance *omap1510_find_ep (int ep)
56 {
57         int i;
58
59         for (i = 0; i < udc_device->bus->max_endpoints; i++) {
60                 if (udc_device->bus->endpoint_array[i].endpoint_address == ep)
61                         return &udc_device->bus->endpoint_array[i];
62         }
63         return NULL;
64 }
65
66 /* ************************************************************************** */
67 /* IO
68  */
69
70 /*
71  * omap1510_prepare_endpoint_for_rx
72  *
73  * This function implements TRM Figure 14-11.
74  *
75  * The endpoint to prepare for transfer is specified as a physical endpoint
76  * number.  For OUT (rx) endpoints 1 through 15, the corresponding endpoint
77  * configuration register is checked to see if the endpoint is ISO or not.
78  * If the OUT endpoint is valid and is non-ISO then its FIFO is enabled.
79  * No action is taken for endpoint 0 or for IN (tx) endpoints 16 through 30.
80  */
81 static void omap1510_prepare_endpoint_for_rx (int ep_addr)
82 {
83         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
84
85         UDCDBGA ("omap1510_prepare_endpoint %x", ep_addr);
86         if (((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) {
87                 if ((inw (UDC_EP_RX (ep_num)) &
88                      (UDC_EPn_RX_Valid | UDC_EPn_RX_Iso)) ==
89                     UDC_EPn_RX_Valid) {
90                         /* rx endpoint is valid, non-ISO, so enable its FIFO */
91                         outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
92                         outw (UDC_Set_FIFO_En, UDC_CTRL);
93                         outw (0, UDC_EP_NUM);
94                 }
95         }
96 }
97
98 /* omap1510_configure_endpoints
99  *
100  * This function implements TRM Figure 14-10.
101  */
102 static void omap1510_configure_endpoints (struct usb_device_instance *device)
103 {
104         int ep;
105         struct usb_bus_instance *bus;
106         struct usb_endpoint_instance *endpoint;
107         unsigned short ep_ptr;
108         unsigned short ep_size;
109         unsigned short ep_isoc;
110         unsigned short ep_doublebuffer;
111         int ep_addr;
112         int packet_size;
113         int buffer_size;
114         int attributes;
115
116         bus = device->bus;
117
118         /* There is a dedicated 2048 byte buffer for USB packets that may be
119          * arbitrarily partitioned among the endpoints on 8-byte boundaries.
120          * The first 8 bytes are reserved for receiving setup packets on
121          * endpoint 0.
122          */
123         ep_ptr = 8;             /* reserve the first 8 bytes for the setup fifo */
124
125         for (ep = 0; ep < bus->max_endpoints; ep++) {
126                 endpoint = bus->endpoint_array + ep;
127                 ep_addr = endpoint->endpoint_address;
128                 if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
129                         /* IN endpoint */
130                         packet_size = endpoint->tx_packetSize;
131                         attributes = endpoint->tx_attributes;
132                 } else {
133                         /* OUT endpoint */
134                         packet_size = endpoint->rcv_packetSize;
135                         attributes = endpoint->rcv_attributes;
136                 }
137
138                 switch (packet_size) {
139                 case 0:
140                         ep_size = 0;
141                         break;
142                 case 8:
143                         ep_size = 0;
144                         break;
145                 case 16:
146                         ep_size = 1;
147                         break;
148                 case 32:
149                         ep_size = 2;
150                         break;
151                 case 64:
152                         ep_size = 3;
153                         break;
154                 case 128:
155                         ep_size = 4;
156                         break;
157                 case 256:
158                         ep_size = 5;
159                         break;
160                 case 512:
161                         ep_size = 6;
162                         break;
163                 default:
164                         UDCDBGA ("ep 0x%02x has bad packet size %d",
165                                  ep_addr, packet_size);
166                         packet_size = 0;
167                         ep_size = 0;
168                         break;
169                 }
170
171                 switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
172                 case USB_ENDPOINT_XFER_CONTROL:
173                 case USB_ENDPOINT_XFER_BULK:
174                 case USB_ENDPOINT_XFER_INT:
175                 default:
176                         /* A non-isochronous endpoint may optionally be
177                          * double-buffered. For now we disable
178                          * double-buffering.
179                          */
180                         ep_doublebuffer = 0;
181                         ep_isoc = 0;
182                         if (packet_size > 64)
183                                 packet_size = 0;
184                         if (!ep || !ep_doublebuffer)
185                                 buffer_size = packet_size;
186                         else
187                                 buffer_size = packet_size * 2;
188                         break;
189                 case USB_ENDPOINT_XFER_ISOC:
190                         /* Isochronous endpoints are always double-
191                          * buffered, but the double-buffering bit
192                          * in the endpoint configuration register
193                          * becomes the msb of the endpoint size so we
194                          * set the double-buffering flag to zero.
195                          */
196                         ep_doublebuffer = 0;
197                         ep_isoc = 1;
198                         buffer_size = packet_size * 2;
199                         break;
200                 }
201
202                 /* check to see if our packet buffer RAM is exhausted */
203                 if ((ep_ptr + buffer_size) > 2048) {
204                         UDCDBGA ("out of packet RAM for ep 0x%02x buf size %d", ep_addr, buffer_size);
205                         buffer_size = packet_size = 0;
206                 }
207
208                 /* force a default configuration for endpoint 0 since it is
209                  * always enabled
210                  */
211                 if (!ep && ((packet_size < 8) || (packet_size > 64))) {
212                         buffer_size = packet_size = 64;
213                         ep_size = 3;
214                 }
215
216                 if (!ep) {
217                         /* configure endpoint 0 */
218                         outw ((ep_size << 12) | (ep_ptr >> 3), UDC_EP0);
219                         /*UDCDBGA("ep 0 buffer offset 0x%03x packet size 0x%03x", */
220                         /*      ep_ptr, packet_size); */
221                 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
222                         /* IN endpoint */
223                         if (packet_size) {
224                                 outw ((1 << 15) | (ep_doublebuffer << 14) |
225                                       (ep_size << 12) | (ep_isoc << 11) |
226                                       (ep_ptr >> 3),
227                                       UDC_EP_TX (ep_addr &
228                                                  USB_ENDPOINT_NUMBER_MASK));
229                                 UDCDBGA ("IN ep %d buffer offset 0x%03x"
230                                          " packet size 0x%03x",
231                                          ep_addr & USB_ENDPOINT_NUMBER_MASK,
232                                          ep_ptr, packet_size);
233                         } else {
234                                 outw (0,
235                                       UDC_EP_TX (ep_addr &
236                                                  USB_ENDPOINT_NUMBER_MASK));
237                         }
238                 } else {
239                         /* OUT endpoint */
240                         if (packet_size) {
241                                 outw ((1 << 15) | (ep_doublebuffer << 14) |
242                                       (ep_size << 12) | (ep_isoc << 11) |
243                                       (ep_ptr >> 3),
244                                       UDC_EP_RX (ep_addr &
245                                                  USB_ENDPOINT_NUMBER_MASK));
246                                 UDCDBGA ("OUT ep %d buffer offset 0x%03x"
247                                          " packet size 0x%03x",
248                                          ep_addr & USB_ENDPOINT_NUMBER_MASK,
249                                          ep_ptr, packet_size);
250                         } else {
251                                 outw (0,
252                                       UDC_EP_RX (ep_addr &
253                                                  USB_ENDPOINT_NUMBER_MASK));
254                         }
255                 }
256                 ep_ptr += buffer_size;
257         }
258 }
259
260 /* omap1510_deconfigure_device
261  *
262  * This function balances omap1510_configure_device.
263  */
264 static void omap1510_deconfigure_device (void)
265 {
266         int epnum;
267
268         UDCDBG ("clear Cfg_Lock");
269         outw (inw (UDC_SYSCON1) & ~UDC_Cfg_Lock, UDC_SYSCON1);
270         UDCREG (UDC_SYSCON1);
271
272         /* deconfigure all endpoints */
273         for (epnum = 1; epnum <= 15; epnum++) {
274                 outw (0, UDC_EP_RX (epnum));
275                 outw (0, UDC_EP_TX (epnum));
276         }
277 }
278
279 /* omap1510_configure_device
280  *
281  * This function implements TRM Figure 14-9.
282  */
283 static void omap1510_configure_device (struct usb_device_instance *device)
284 {
285         omap1510_configure_endpoints (device);
286
287
288         /* Figure 14-9 indicates we should enable interrupts here, but we have
289          * other routines (udc_all_interrupts, udc_suspended_interrupts) to
290          * do that.
291          */
292
293         UDCDBG ("set Cfg_Lock");
294         outw (inw (UDC_SYSCON1) | UDC_Cfg_Lock, UDC_SYSCON1);
295         UDCREG (UDC_SYSCON1);
296 }
297
298 /* omap1510_write_noniso_tx_fifo
299  *
300  * This function implements TRM Figure 14-30.
301  *
302  * If the endpoint has an active tx_urb, then the next packet of data from the
303  * URB is written to the tx FIFO.  The total amount of data in the urb is given
304  * by urb->actual_length.  The maximum amount of data that can be sent in any
305  * one packet is given by endpoint->tx_packetSize.  The number of data bytes
306  * from this URB that have already been transmitted is given by endpoint->sent.
307  * endpoint->last is updated by this routine with the number of data bytes
308  * transmitted in this packet.
309  *
310  * In accordance with Figure 14-30, the EP_NUM register must already have been
311  * written with the value to select the appropriate tx FIFO before this routine
312  * is called.
313  */
314 static void omap1510_write_noniso_tx_fifo (struct usb_endpoint_instance
315                                            *endpoint)
316 {
317         struct urb *urb = endpoint->tx_urb;
318
319         if (urb) {
320                 unsigned int last, i;
321
322                 UDCDBGA ("urb->buffer %p, buffer_length %d, actual_length %d",
323                          urb->buffer, urb->buffer_length, urb->actual_length);
324                 if ((last =
325                      MIN (urb->actual_length - endpoint->sent,
326                           endpoint->tx_packetSize))) {
327                         u8 *cp = urb->buffer + endpoint->sent;
328
329                         UDCDBGA ("endpoint->sent %d, tx_packetSize %d, last %d", endpoint->sent, endpoint->tx_packetSize, last);
330
331                         if (((u32) cp & 1) == 0) {      /* word aligned? */
332                                 outsw (UDC_DATA, cp, last >> 1);
333                         } else {        /* byte aligned. */
334                                 for (i = 0; i < (last >> 1); i++) {
335                                         u16 w = ((u16) cp[2 * i + 1] << 8) |
336                                                 (u16) cp[2 * i];
337                                         outw (w, UDC_DATA);
338                                 }
339                         }
340                         if (last & 1) {
341                                 outb (*(cp + last - 1), UDC_DATA);
342                         }
343                 }
344                 endpoint->last = last;
345         }
346 }
347
348 /* omap1510_read_noniso_rx_fifo
349  *
350  * This function implements TRM Figure 14-28.
351  *
352  * If the endpoint has an active rcv_urb, then the next packet of data is read
353  * from the rcv FIFO and written to rcv_urb->buffer at offset
354  * rcv_urb->actual_length to append the packet data to the data from any
355  * previous packets for this transfer.  We assume that there is sufficient room
356  * left in the buffer to hold an entire packet of data.
357  *
358  * The return value is the number of bytes read from the FIFO for this packet.
359  *
360  * In accordance with Figure 14-28, the EP_NUM register must already have been
361  * written with the value to select the appropriate rcv FIFO before this routine
362  * is called.
363  */
364 static int omap1510_read_noniso_rx_fifo (struct usb_endpoint_instance
365                                          *endpoint)
366 {
367         struct urb *urb = endpoint->rcv_urb;
368         int len = 0;
369
370         if (urb) {
371                 len = inw (UDC_RXFSTAT);
372
373                 if (len) {
374                         unsigned char *cp = urb->buffer + urb->actual_length;
375
376                         insw (UDC_DATA, cp, len >> 1);
377                         if (len & 1)
378                                 *(cp + len - 1) = inb (UDC_DATA);
379                 }
380         }
381         return len;
382 }
383
384 /* omap1510_prepare_for_control_write_status
385  *
386  * This function implements TRM Figure 14-17.
387  *
388  * We have to deal here with non-autodecoded control writes that haven't already
389  * been dealt with by ep0_recv_setup.  The non-autodecoded standard control
390  * write requests are:  set/clear endpoint feature, set configuration, set
391  * interface, and set descriptor.  ep0_recv_setup handles set/clear requests for
392  * ENDPOINT_HALT by halting the endpoint for a set request and resetting the
393  * endpoint for a clear request.  ep0_recv_setup returns an error for
394  * SET_DESCRIPTOR requests which causes them to be terminated with a stall by
395  * the setup handler.  A SET_INTERFACE request is handled by ep0_recv_setup by
396  * generating a DEVICE_SET_INTERFACE event.  This leaves only the
397  * SET_CONFIGURATION event for us to deal with here.
398  *
399  */
400 static void omap1510_prepare_for_control_write_status (struct urb *urb)
401 {
402         struct usb_device_request *request = &urb->device_request;;
403
404         /* check for a SET_CONFIGURATION request */
405         if (request->bRequest == USB_REQ_SET_CONFIGURATION) {
406                 int configuration = le16_to_cpu (request->wValue) & 0xff;
407                 unsigned short devstat = inw (UDC_DEVSTAT);
408
409                 if ((devstat & (UDC_ADD | UDC_CFG)) == UDC_ADD) {
410                         /* device is currently in ADDRESSED state */
411                         if (configuration) {
412                                 /* Assume the specified non-zero configuration
413                                  * value is valid and switch to the CONFIGURED
414                                  * state.
415                                  */
416                                 outw (UDC_Dev_Cfg, UDC_SYSCON2);
417                         }
418                 } else if ((devstat & UDC_CFG) == UDC_CFG) {
419                         /* device is currently in CONFIGURED state */
420                         if (!configuration) {
421                                 /* Switch to ADDRESSED state. */
422                                 outw (UDC_Clr_Cfg, UDC_SYSCON2);
423                         }
424                 }
425         }
426
427         /* select EP0 tx FIFO */
428         outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
429         /* clear endpoint (no data bytes in status stage) */
430         outw (UDC_Clr_EP, UDC_CTRL);
431         /* enable the EP0 tx FIFO */
432         outw (UDC_Set_FIFO_En, UDC_CTRL);
433         /* deselect the endpoint */
434         outw (UDC_EP_Dir, UDC_EP_NUM);
435 }
436
437 /* udc_state_transition_up
438  * udc_state_transition_down
439  *
440  * Helper functions to implement device state changes.  The device states and
441  * the events that transition between them are:
442  *
443  *                              STATE_ATTACHED
444  *                              ||      /\
445  *                              \/      ||
446  *      DEVICE_HUB_CONFIGURED                   DEVICE_HUB_RESET
447  *                              ||      /\
448  *                              \/      ||
449  *                              STATE_POWERED
450  *                              ||      /\
451  *                              \/      ||
452  *      DEVICE_RESET                            DEVICE_POWER_INTERRUPTION
453  *                              ||      /\
454  *                              \/      ||
455  *                              STATE_DEFAULT
456  *                              ||      /\
457  *                              \/      ||
458  *      DEVICE_ADDRESS_ASSIGNED                 DEVICE_RESET
459  *                              ||      /\
460  *                              \/      ||
461  *                              STATE_ADDRESSED
462  *                              ||      /\
463  *                              \/      ||
464  *      DEVICE_CONFIGURED                       DEVICE_DE_CONFIGURED
465  *                              ||      /\
466  *                              \/      ||
467  *                              STATE_CONFIGURED
468  *
469  * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
470  * to STATE_CONFIGURED) from the specified initial state to the specified final
471  * state, passing through each intermediate state on the way.  If the initial
472  * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
473  * no state transitions will take place.
474  *
475  * udc_state_transition_down transitions down (in the direction from
476  * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
477  * specified final state, passing through each intermediate state on the way.
478  * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
479  * state, then no state transitions will take place.
480  *
481  * These functions must only be called with interrupts disabled.
482  */
483 static void udc_state_transition_up (usb_device_state_t initial,
484                                      usb_device_state_t final)
485 {
486         if (initial < final) {
487                 switch (initial) {
488                 case STATE_ATTACHED:
489                         usbd_device_event_irq (udc_device,
490                                                DEVICE_HUB_CONFIGURED, 0);
491                         if (final == STATE_POWERED)
492                                 break;
493                 case STATE_POWERED:
494                         usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
495                         if (final == STATE_DEFAULT)
496                                 break;
497                 case STATE_DEFAULT:
498                         usbd_device_event_irq (udc_device,
499                                                DEVICE_ADDRESS_ASSIGNED, 0);
500                         if (final == STATE_ADDRESSED)
501                                 break;
502                 case STATE_ADDRESSED:
503                         usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
504                                                0);
505                 case STATE_CONFIGURED:
506                         break;
507                 default:
508                         break;
509                 }
510         }
511 }
512
513 static void udc_state_transition_down (usb_device_state_t initial,
514                                        usb_device_state_t final)
515 {
516         if (initial > final) {
517                 switch (initial) {
518                 case STATE_CONFIGURED:
519                         usbd_device_event_irq (udc_device, DEVICE_DE_CONFIGURED, 0);
520                         if (final == STATE_ADDRESSED)
521                                 break;
522                 case STATE_ADDRESSED:
523                         usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
524                         if (final == STATE_DEFAULT)
525                                 break;
526                 case STATE_DEFAULT:
527                         usbd_device_event_irq (udc_device, DEVICE_POWER_INTERRUPTION, 0);
528                         if (final == STATE_POWERED)
529                                 break;
530                 case STATE_POWERED:
531                         usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, 0);
532                 case STATE_ATTACHED:
533                         break;
534                 default:
535                         break;
536                 }
537         }
538 }
539
540 /* Handle all device state changes.
541  * This function implements TRM Figure 14-21.
542  */
543 static void omap1510_udc_state_changed (void)
544 {
545         u16 bits;
546         u16 devstat = inw (UDC_DEVSTAT);
547
548         UDCDBGA ("state changed, devstat %x, old %x", devstat, udc_devstat);
549
550         bits = devstat ^ udc_devstat;
551         if (bits) {
552                 if (bits & UDC_ATT) {
553                         if (devstat & UDC_ATT) {
554                                 UDCDBG ("device attached and powered");
555                                 udc_state_transition_up (udc_device->device_state, STATE_POWERED);
556                         } else {
557                                 UDCDBG ("device detached or unpowered");
558                                 udc_state_transition_down (udc_device->device_state, STATE_ATTACHED);
559                         }
560                 }
561                 if (bits & UDC_USB_Reset) {
562                         if (devstat & UDC_USB_Reset) {
563                                 UDCDBG ("device reset in progess");
564                                 udc_state_transition_down (udc_device->device_state, STATE_POWERED);
565                         } else {
566                                 UDCDBG ("device reset completed");
567                         }
568                 }
569                 if (bits & UDC_DEF) {
570                         if (devstat & UDC_DEF) {
571                                 UDCDBG ("device entering default state");
572                                 udc_state_transition_up (udc_device->device_state, STATE_DEFAULT);
573                         } else {
574                                 UDCDBG ("device leaving default state");
575                                 udc_state_transition_down (udc_device->device_state, STATE_POWERED);
576                         }
577                 }
578                 if (bits & UDC_SUS) {
579                         if (devstat & UDC_SUS) {
580                                 UDCDBG ("entering suspended state");
581                                 usbd_device_event_irq (udc_device, DEVICE_BUS_INACTIVE, 0);
582                         } else {
583                                 UDCDBG ("leaving suspended state");
584                                 usbd_device_event_irq (udc_device, DEVICE_BUS_ACTIVITY, 0);
585                         }
586                 }
587                 if (bits & UDC_R_WK_OK) {
588                         UDCDBGA ("remote wakeup %s", (devstat & UDC_R_WK_OK)
589                                  ? "enabled" : "disabled");
590                 }
591                 if (bits & UDC_ADD) {
592                         if (devstat & UDC_ADD) {
593                                 UDCDBG ("default -> addressed");
594                                 udc_state_transition_up (udc_device->device_state, STATE_ADDRESSED);
595                         } else {
596                                 UDCDBG ("addressed -> default");
597                                 udc_state_transition_down (udc_device->device_state, STATE_DEFAULT);
598                         }
599                 }
600                 if (bits & UDC_CFG) {
601                         if (devstat & UDC_CFG) {
602                                 UDCDBG ("device configured");
603                                 /* The ep0_recv_setup function generates the
604                                  * DEVICE_CONFIGURED event when a
605                                  * USB_REQ_SET_CONFIGURATION setup packet is
606                                  * received, so we should already be in the
607                                  * state STATE_CONFIGURED.
608                                  */
609                                 udc_state_transition_up (udc_device->device_state, STATE_CONFIGURED);
610                         } else {
611                                 UDCDBG ("device deconfigured");
612                                 udc_state_transition_down (udc_device->device_state, STATE_ADDRESSED);
613                         }
614                 }
615         }
616
617         /* Clear interrupt source */
618         outw (UDC_DS_Chg, UDC_IRQ_SRC);
619
620         /* Save current DEVSTAT */
621         udc_devstat = devstat;
622 }
623
624 /* Handle SETUP USB interrupt.
625  * This function implements TRM Figure 14-14.
626  */
627 static void omap1510_udc_setup (struct usb_endpoint_instance *endpoint)
628 {
629         UDCDBG ("-> Entering device setup");
630
631         do {
632                 const int setup_pktsize = 8;
633                 unsigned char *datap =
634                         (unsigned char *) &ep0_urb->device_request;
635
636                 /* Gain access to EP 0 setup FIFO */
637                 outw (UDC_Setup_Sel, UDC_EP_NUM);
638
639                 /* Read control request data */
640                 insb (UDC_DATA, datap, setup_pktsize);
641
642                 UDCDBGA ("EP0 setup read [%x %x %x %x %x %x %x %x]",
643                          *(datap + 0), *(datap + 1), *(datap + 2),
644                          *(datap + 3), *(datap + 4), *(datap + 5),
645                          *(datap + 6), *(datap + 7));
646
647                 /* Reset EP0 setup FIFO */
648                 outw (0, UDC_EP_NUM);
649         } while (inw (UDC_IRQ_SRC) & UDC_Setup);
650
651         /* Try to process setup packet */
652         if (ep0_recv_setup (ep0_urb)) {
653                 /* Not a setup packet, stall next EP0 transaction */
654                 udc_stall_ep (0);
655                 UDCDBG ("can't parse setup packet, still waiting for setup");
656                 return;
657         }
658
659         /* Check direction */
660         if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
661             == USB_REQ_HOST2DEVICE) {
662                 UDCDBG ("control write on EP0");
663                 if (le16_to_cpu (ep0_urb->device_request.wLength)) {
664                         /* We don't support control write data stages.
665                          * The only standard control write request with a data
666                          * stage is SET_DESCRIPTOR, and ep0_recv_setup doesn't
667                          * support that so we just stall those requests.  A
668                          * function driver might support a non-standard
669                          * write request with a data stage, but it isn't
670                          * obvious what we would do with the data if we read it
671                          * so we'll just stall it.  It seems like the API isn't
672                          * quite right here.
673                          */
674 #if 0
675                         /* Here is what we would do if we did support control
676                          * write data stages.
677                          */
678                         ep0_urb->actual_length = 0;
679                         outw (0, UDC_EP_NUM);
680                         /* enable the EP0 rx FIFO */
681                         outw (UDC_Set_FIFO_En, UDC_CTRL);
682 #else
683                         /* Stall this request */
684                         UDCDBG ("Stalling unsupported EP0 control write data "
685                                 "stage.");
686                         udc_stall_ep (0);
687 #endif
688                 } else {
689                         omap1510_prepare_for_control_write_status (ep0_urb);
690                 }
691         } else {
692                 UDCDBG ("control read on EP0");
693                 /* The ep0_recv_setup function has already placed our response
694                  * packet data in ep0_urb->buffer and the packet length in
695                  * ep0_urb->actual_length.
696                  */
697                 endpoint->tx_urb = ep0_urb;
698                 endpoint->sent = 0;
699                 /* select the EP0 tx FIFO */
700                 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
701                 /* Write packet data to the FIFO.  omap1510_write_noniso_tx_fifo
702                  * will update endpoint->last with the number of bytes written
703                  * to the FIFO.
704                  */
705                 omap1510_write_noniso_tx_fifo (endpoint);
706                 /* enable the FIFO to start the packet transmission */
707                 outw (UDC_Set_FIFO_En, UDC_CTRL);
708                 /* deselect the EP0 tx FIFO */
709                 outw (UDC_EP_Dir, UDC_EP_NUM);
710         }
711
712         UDCDBG ("<- Leaving device setup");
713 }
714
715 /* Handle endpoint 0 RX interrupt
716  * This routine implements TRM Figure 14-16.
717  */
718 static void omap1510_udc_ep0_rx (struct usb_endpoint_instance *endpoint)
719 {
720         unsigned short status;
721
722         UDCDBG ("RX on EP0");
723         /* select EP0 rx FIFO */
724         outw (UDC_EP_Sel, UDC_EP_NUM);
725
726         status = inw (UDC_STAT_FLG);
727
728         if (status & UDC_ACK) {
729                 /* Check direction */
730                 if ((ep0_urb->device_request.bmRequestType
731                      & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
732                         /* This rx interrupt must be for a control write data
733                          * stage packet.
734                          *
735                          * We don't support control write data stages.
736                          * We should never end up here.
737                          */
738
739                         /* clear the EP0 rx FIFO */
740                         outw (UDC_Clr_EP, UDC_CTRL);
741
742                         /* deselect the EP0 rx FIFO */
743                         outw (0, UDC_EP_NUM);
744
745                         UDCDBG ("Stalling unexpected EP0 control write "
746                                 "data stage packet");
747                         udc_stall_ep (0);
748                 } else {
749                         /* This rx interrupt must be for a control read status
750                          * stage packet.
751                          */
752                         UDCDBG ("ACK on EP0 control read status stage packet");
753                         /* deselect EP0 rx FIFO */
754                         outw (0, UDC_EP_NUM);
755                 }
756         } else if (status & UDC_STALL) {
757                 UDCDBG ("EP0 stall during RX");
758                 /* deselect EP0 rx FIFO */
759                 outw (0, UDC_EP_NUM);
760         } else {
761                 /* deselect EP0 rx FIFO */
762                 outw (0, UDC_EP_NUM);
763         }
764 }
765
766 /* Handle endpoint 0 TX interrupt
767  * This routine implements TRM Figure 14-18.
768  */
769 static void omap1510_udc_ep0_tx (struct usb_endpoint_instance *endpoint)
770 {
771         unsigned short status;
772         struct usb_device_request *request = &ep0_urb->device_request;
773
774         UDCDBG ("TX on EP0");
775         /* select EP0 TX FIFO */
776         outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
777
778         status = inw (UDC_STAT_FLG);
779         if (status & UDC_ACK) {
780                 /* Check direction */
781                 if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
782                     USB_REQ_HOST2DEVICE) {
783                         /* This tx interrupt must be for a control write status
784                          * stage packet.
785                          */
786                         UDCDBG ("ACK on EP0 control write status stage packet");
787                         /* deselect EP0 TX FIFO */
788                         outw (UDC_EP_Dir, UDC_EP_NUM);
789                 } else {
790                         /* This tx interrupt must be for a control read data
791                          * stage packet.
792                          */
793                         int wLength = le16_to_cpu (request->wLength);
794
795                         /* Update our count of bytes sent so far in this
796                          * transfer.
797                          */
798                         endpoint->sent += endpoint->last;
799
800                         /* We are finished with this transfer if we have sent
801                          * all of the bytes in our tx urb (urb->actual_length)
802                          * unless we need a zero-length terminating packet.  We
803                          * need a zero-length terminating packet if we returned
804                          * fewer bytes than were requested (wLength) by the host,
805                          * and the number of bytes we returned is an exact
806                          * multiple of the packet size endpoint->tx_packetSize.
807                          */
808                         if ((endpoint->sent == ep0_urb->actual_length)
809                             && ((ep0_urb->actual_length == wLength)
810                                 || (endpoint->last !=
811                                     endpoint->tx_packetSize))) {
812                                 /* Done with control read data stage. */
813                                 UDCDBG ("control read data stage complete");
814                                 /* deselect EP0 TX FIFO */
815                                 outw (UDC_EP_Dir, UDC_EP_NUM);
816                                 /* select EP0 RX FIFO to prepare for control
817                                  * read status stage.
818                                  */
819                                 outw (UDC_EP_Sel, UDC_EP_NUM);
820                                 /* clear the EP0 RX FIFO */
821                                 outw (UDC_Clr_EP, UDC_CTRL);
822                                 /* enable the EP0 RX FIFO */
823                                 outw (UDC_Set_FIFO_En, UDC_CTRL);
824                                 /* deselect the EP0 RX FIFO */
825                                 outw (0, UDC_EP_NUM);
826                         } else {
827                                 /* We still have another packet of data to send
828                                  * in this control read data stage or else we
829                                  * need a zero-length terminating packet.
830                                  */
831                                 UDCDBG ("ACK control read data stage packet");
832                                 omap1510_write_noniso_tx_fifo (endpoint);
833                                 /* enable the EP0 tx FIFO to start transmission */
834                                 outw (UDC_Set_FIFO_En, UDC_CTRL);
835                                 /* deselect EP0 TX FIFO */
836                                 outw (UDC_EP_Dir, UDC_EP_NUM);
837                         }
838                 }
839         } else if (status & UDC_STALL) {
840                 UDCDBG ("EP0 stall during TX");
841                 /* deselect EP0 TX FIFO */
842                 outw (UDC_EP_Dir, UDC_EP_NUM);
843         } else {
844                 /* deselect EP0 TX FIFO */
845                 outw (UDC_EP_Dir, UDC_EP_NUM);
846         }
847 }
848
849 /* Handle RX transaction on non-ISO endpoint.
850  * This function implements TRM Figure 14-27.
851  * The ep argument is a physical endpoint number for a non-ISO OUT endpoint
852  * in the range 1 to 15.
853  */
854 static void omap1510_udc_epn_rx (int ep)
855 {
856         unsigned short status;
857
858         /* Check endpoint status */
859         status = inw (UDC_STAT_FLG);
860
861         if (status & UDC_ACK) {
862                 int nbytes;
863                 struct usb_endpoint_instance *endpoint =
864                         omap1510_find_ep (ep);
865
866                 nbytes = omap1510_read_noniso_rx_fifo (endpoint);
867                 usbd_rcv_complete (endpoint, nbytes, 0);
868
869                 /* enable rx FIFO to prepare for next packet */
870                 outw (UDC_Set_FIFO_En, UDC_CTRL);
871         } else if (status & UDC_STALL) {
872                 UDCDBGA ("STALL on RX endpoint %d", ep);
873         } else if (status & UDC_NAK) {
874                 UDCDBGA ("NAK on RX ep %d", ep);
875         } else {
876                 serial_printf ("omap-bi: RX on ep %d with status %x", ep,
877                                status);
878         }
879 }
880
881 /* Handle TX transaction on non-ISO endpoint.
882  * This function implements TRM Figure 14-29.
883  * The ep argument is a physical endpoint number for a non-ISO IN endpoint
884  * in the range 16 to 30.
885  */
886 static void omap1510_udc_epn_tx (int ep)
887 {
888         unsigned short status;
889
890         /*serial_printf("omap1510_udc_epn_tx( %x )\n",ep); */
891
892         /* Check endpoint status */
893         status = inw (UDC_STAT_FLG);
894
895         if (status & UDC_ACK) {
896                 struct usb_endpoint_instance *endpoint =
897                         omap1510_find_ep (ep);
898
899                 /* We need to transmit a terminating zero-length packet now if
900                  * we have sent all of the data in this URB and the transfer
901                  * size was an exact multiple of the packet size.
902                  */
903                 if (endpoint->tx_urb
904                     && (endpoint->last == endpoint->tx_packetSize)
905                     && (endpoint->tx_urb->actual_length - endpoint->sent -
906                         endpoint->last == 0)) {
907                         /* Prepare to transmit a zero-length packet. */
908                         endpoint->sent += endpoint->last;
909                         /* write 0 bytes of data to FIFO */
910                         omap1510_write_noniso_tx_fifo (endpoint);
911                         /* enable tx FIFO to start transmission */
912                         outw (UDC_Set_FIFO_En, UDC_CTRL);
913                 } else if (endpoint->tx_urb
914                            && endpoint->tx_urb->actual_length) {
915                         /* retire the data that was just sent */
916                         usbd_tx_complete (endpoint);
917                         /* Check to see if we have more data ready to transmit
918                          * now.
919                          */
920                         if (endpoint->tx_urb
921                             && endpoint->tx_urb->actual_length) {
922                                 /* write data to FIFO */
923                                 omap1510_write_noniso_tx_fifo (endpoint);
924                                 /* enable tx FIFO to start transmission */
925                                 outw (UDC_Set_FIFO_En, UDC_CTRL);
926                         }
927                 }
928         } else if (status & UDC_STALL) {
929                 UDCDBGA ("STALL on TX endpoint %d", ep);
930         } else if (status & UDC_NAK) {
931                 UDCDBGA ("NAK on TX endpoint %d", ep);
932         } else {
933                 /*serial_printf("omap-bi: TX on ep %d with status %x\n", ep, status); */
934         }
935 }
936
937
938 /*
939 -------------------------------------------------------------------------------
940 */
941
942 /* Handle general USB interrupts and dispatch according to type.
943  * This function implements TRM Figure 14-13.
944  */
945 void omap1510_udc_irq (void)
946 {
947         u16 irq_src = inw (UDC_IRQ_SRC);
948         int valid_irq = 0;
949
950         if (!(irq_src & ~UDC_SOF_Flg))  /* ignore SOF interrupts ) */
951                 return;
952
953         UDCDBGA ("< IRQ #%d start >- %x", udc_interrupts, irq_src);
954         /*serial_printf("< IRQ #%d start >- %x\n", udc_interrupts, irq_src); */
955
956         if (irq_src & UDC_DS_Chg) {
957                 /* Device status changed */
958                 omap1510_udc_state_changed ();
959                 valid_irq++;
960         }
961         if (irq_src & UDC_EP0_RX) {
962                 /* Endpoint 0 receive */
963                 outw (UDC_EP0_RX, UDC_IRQ_SRC); /* ack interrupt */
964                 omap1510_udc_ep0_rx (udc_device->bus->endpoint_array + 0);
965                 valid_irq++;
966         }
967         if (irq_src & UDC_EP0_TX) {
968                 /* Endpoint 0 transmit */
969                 outw (UDC_EP0_TX, UDC_IRQ_SRC); /* ack interrupt */
970                 omap1510_udc_ep0_tx (udc_device->bus->endpoint_array + 0);
971                 valid_irq++;
972         }
973         if (irq_src & UDC_Setup) {
974                 /* Device setup */
975                 omap1510_udc_setup (udc_device->bus->endpoint_array + 0);
976                 valid_irq++;
977         }
978         /*if (!valid_irq) */
979         /*      serial_printf("unknown interrupt, IRQ_SRC %.4x\n", irq_src); */
980         UDCDBGA ("< IRQ #%d end >", udc_interrupts);
981         udc_interrupts++;
982 }
983
984 /* This function implements TRM Figure 14-26. */
985 void omap1510_udc_noniso_irq (void)
986 {
987         unsigned short epnum;
988         unsigned short irq_src = inw (UDC_IRQ_SRC);
989         int valid_irq = 0;
990
991         if (!(irq_src & (UDC_EPn_RX | UDC_EPn_TX)))
992                 return;
993
994         UDCDBGA ("non-ISO IRQ, IRQ_SRC %x", inw (UDC_IRQ_SRC));
995
996         if (irq_src & UDC_EPn_RX) {     /* Endpoint N OUT transaction */
997                 /* Determine the endpoint number for this interrupt */
998                 epnum = (inw (UDC_EPN_STAT) & 0x0f00) >> 8;
999                 UDCDBGA ("RX on ep %x", epnum);
1000
1001                 /* acknowledge interrupt */
1002                 outw (UDC_EPn_RX, UDC_IRQ_SRC);
1003
1004                 if (epnum) {
1005                         /* select the endpoint FIFO */
1006                         outw (UDC_EP_Sel | epnum, UDC_EP_NUM);
1007
1008                         omap1510_udc_epn_rx (epnum);
1009
1010                         /* deselect the endpoint FIFO */
1011                         outw (epnum, UDC_EP_NUM);
1012                 }
1013                 valid_irq++;
1014         }
1015         if (irq_src & UDC_EPn_TX) {     /* Endpoint N IN transaction */
1016                 /* Determine the endpoint number for this interrupt */
1017                 epnum = (inw (UDC_EPN_STAT) & 0x000f) | USB_DIR_IN;
1018                 UDCDBGA ("TX on ep %x", epnum);
1019
1020                 /* acknowledge interrupt */
1021                 outw (UDC_EPn_TX, UDC_IRQ_SRC);
1022
1023                 if (epnum) {
1024                         /* select the endpoint FIFO */
1025                         outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
1026
1027                         omap1510_udc_epn_tx (epnum);
1028
1029                         /* deselect the endpoint FIFO */
1030                         outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
1031                 }
1032                 valid_irq++;
1033         }
1034         if (!valid_irq)
1035                 serial_printf (": unknown non-ISO interrupt, IRQ_SRC %.4x\n",
1036                                irq_src);
1037 }
1038
1039 /*
1040 -------------------------------------------------------------------------------
1041 */
1042
1043
1044 /*
1045  * Start of public functions.
1046  */
1047
1048 /* Called to start packet transmission. */
1049 int udc_endpoint_write (struct usb_endpoint_instance *endpoint)
1050 {
1051         unsigned short epnum =
1052                 endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
1053
1054         UDCDBGA ("Starting transmit on ep %x", epnum);
1055
1056         if (endpoint->tx_urb) {
1057                 /* select the endpoint FIFO */
1058                 outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
1059                 /* write data to FIFO */
1060                 omap1510_write_noniso_tx_fifo (endpoint);
1061                 /* enable tx FIFO to start transmission */
1062                 outw (UDC_Set_FIFO_En, UDC_CTRL);
1063                 /* deselect the endpoint FIFO */
1064                 outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
1065         }
1066
1067         return 0;
1068 }
1069
1070 /* Start to initialize h/w stuff */
1071 int udc_init (void)
1072 {
1073         u16 udc_rev;
1074         uchar value;
1075         ulong gpio;
1076         int i;
1077
1078         /* Let the device settle down before we start */
1079         for (i = 0; i < UDC_INIT_MDELAY; i++) udelay(1000);
1080
1081         udc_device = NULL;
1082
1083         UDCDBG ("starting");
1084
1085         /* Check peripheral reset. Must be 1 to make sure
1086            MPU TIPB peripheral reset is inactive */
1087         UDCREG (ARM_RSTCT2);
1088
1089         /* Set and check clock control.
1090          * We might ought to be using the clock control API to do
1091          * this instead of fiddling with the clock registers directly
1092          * here.
1093          */
1094         outw ((1 << 4) | (1 << 5), CLOCK_CTRL);
1095         UDCREG (CLOCK_CTRL);
1096
1097         /* Set and check SOFT
1098          * The below line of code has been changed to perform a
1099          * read-modify-write instead of a simple write for
1100          * configuring the SOFT_REQ register. This allows the code
1101          * to be compatible with OMAP5912 and OMAP16xx devices
1102          */
1103         outw ((1 << 4) | (1 << 3) | 1 | (inw(SOFT_REQ)), SOFT_REQ);
1104
1105         /* Short delay to wait for DPLL */
1106         udelay (1000);
1107
1108         /* Print banner with device revision */
1109         udc_rev = inw (UDC_REV) & 0xff;
1110
1111 #ifdef CONFIG_OMAP1610
1112         printf ("USB:   TI OMAP5912 USB function module rev %d.%d\n",
1113                 udc_rev >> 4, udc_rev & 0xf);
1114 #endif
1115
1116         /* The VBUS_MODE bit selects whether VBUS detection is done via
1117          * software (1) or hardware (0).  When software detection is
1118          * selected, VBUS_CTRL selects whether USB is not connected (0)
1119          * or connected (1).
1120          */
1121         outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1122         outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1123         UDCREGL (FUNC_MUX_CTRL_0);
1124
1125         /*
1126          * At this point, device is ready for configuration...
1127          */
1128
1129         UDCDBG ("disable USB interrupts");
1130         outw (0, UDC_IRQ_EN);
1131         UDCREG (UDC_IRQ_EN);
1132
1133         UDCDBG ("disable USB DMA");
1134         outw (0, UDC_DMA_IRQ_EN);
1135         UDCREG (UDC_DMA_IRQ_EN);
1136
1137         UDCDBG ("initialize SYSCON1");
1138         outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
1139         UDCREG (UDC_SYSCON1);
1140
1141         return 0;
1142 }
1143
1144 /* Stall endpoint */
1145 static void udc_stall_ep (unsigned int ep_addr)
1146 {
1147         /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1148         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1149
1150         UDCDBGA ("stall ep_addr %d", ep_addr);
1151
1152         /* REVISIT?
1153          * The OMAP TRM section 14.2.4.2 says we must check that the FIFO
1154          * is empty before halting the endpoint.  The current implementation
1155          * doesn't check that the FIFO is empty.
1156          */
1157
1158         if (!ep_num) {
1159                 outw (UDC_Stall_Cmd, UDC_SYSCON2);
1160         } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1161                 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1162                         /* we have a valid rx endpoint, so halt it */
1163                         outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
1164                         outw (UDC_Set_Halt, UDC_CTRL);
1165                         outw (ep_num, UDC_EP_NUM);
1166                 }
1167         } else {
1168                 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1169                         /* we have a valid tx endpoint, so halt it */
1170                         outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
1171                         outw (UDC_Set_Halt, UDC_CTRL);
1172                         outw (ep_num, UDC_EP_NUM);
1173                 }
1174         }
1175 }
1176
1177 /* Reset endpoint */
1178 #if 0
1179 static void udc_reset_ep (unsigned int ep_addr)
1180 {
1181         /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1182         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1183
1184         UDCDBGA ("reset ep_addr %d", ep_addr);
1185
1186         if (!ep_num) {
1187                 /* control endpoint 0 can't be reset */
1188         } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1189                 UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
1190                          inw (UDC_EP_RX (ep_num)));
1191                 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1192                         /* we have a valid rx endpoint, so reset it */
1193                         outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
1194                         outw (UDC_Reset_EP, UDC_CTRL);
1195                         outw (ep_num, UDC_EP_NUM);
1196                         UDCDBGA ("OUT endpoint %d reset", ep_num);
1197                 }
1198         } else {
1199                 UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
1200                          inw (UDC_EP_TX (ep_num)));
1201                 /* Resetting of tx endpoints seems to be causing the USB function
1202                  * module to fail, which causes problems when the driver is
1203                  * uninstalled.  We'll skip resetting tx endpoints for now until
1204                  * we figure out what the problem is.
1205                  */
1206 #if 0
1207                 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1208                         /* we have a valid tx endpoint, so reset it */
1209                         outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
1210                         outw (UDC_Reset_EP, UDC_CTRL);
1211                         outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
1212                         UDCDBGA ("IN endpoint %d reset", ep_num);
1213                 }
1214 #endif
1215         }
1216 }
1217 #endif
1218
1219 /* ************************************************************************** */
1220
1221 /**
1222  * udc_check_ep - check logical endpoint
1223   *
1224  * Return physical endpoint number to use for this logical endpoint or zero if not valid.
1225  */
1226 #if 0
1227 int udc_check_ep (int logical_endpoint, int packetsize)
1228 {
1229         if ((logical_endpoint == 0x80) ||
1230             ((logical_endpoint & 0x8f) != logical_endpoint)) {
1231                 return 0;
1232         }
1233
1234         switch (packetsize) {
1235         case 8:
1236         case 16:
1237         case 32:
1238         case 64:
1239         case 128:
1240         case 256:
1241         case 512:
1242                 break;
1243         default:
1244                 return 0;
1245         }
1246
1247         return EP_ADDR_TO_PHYS_EP (logical_endpoint);
1248 }
1249 #endif
1250
1251 /*
1252  * udc_setup_ep - setup endpoint
1253  *
1254  * Associate a physical endpoint with endpoint_instance
1255  */
1256 void udc_setup_ep (struct usb_device_instance *device,
1257                    unsigned int ep, struct usb_endpoint_instance *endpoint)
1258 {
1259         UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
1260
1261         /* This routine gets called by bi_modinit for endpoint 0 and from
1262          * bi_config for all of the other endpoints.  bi_config gets called
1263          * during the DEVICE_CREATE, DEVICE_CONFIGURED, and
1264          * DEVICE_SET_INTERFACE events.  We need to reconfigure the OMAP packet
1265          * RAM after bi_config scans the selected device configuration and
1266          * initializes the endpoint structures, but before this routine enables
1267          * the OUT endpoint FIFOs.  Since bi_config calls this routine in a
1268          * loop for endpoints 1 through UDC_MAX_ENDPOINTS, we reconfigure our
1269          * packet RAM here when ep==1.
1270          * I really hate to do this here, but it seems like the API exported
1271          * by the USB bus interface controller driver to the usbd-bi module
1272          * isn't quite right so there is no good place to do this.
1273          */
1274         if (ep == 1) {
1275                 omap1510_deconfigure_device ();
1276                 omap1510_configure_device (device);
1277         }
1278
1279         if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
1280                 int ep_addr = endpoint->endpoint_address;
1281
1282                 if (!ep_addr) {
1283                         /* nothing to do for endpoint 0 */
1284                 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1285                         /* nothing to do for IN (tx) endpoints */
1286                 } else {        /* OUT (rx) endpoint */
1287                         if (endpoint->rcv_packetSize) {
1288                                 /*struct urb* urb = &(urb_out_array[ep&0xFF]); */
1289                                 /*urb->endpoint = endpoint; */
1290                                 /*urb->device = device; */
1291                                 /*urb->buffer_length = sizeof(urb->buffer); */
1292
1293                                 /*endpoint->rcv_urb = urb; */
1294                                 omap1510_prepare_endpoint_for_rx (ep_addr);
1295                         }
1296                 }
1297         }
1298 }
1299
1300 /**
1301  * udc_disable_ep - disable endpoint
1302  * @ep:
1303  *
1304  * Disable specified endpoint
1305  */
1306 #if 0
1307 void udc_disable_ep (unsigned int ep_addr)
1308 {
1309         /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1310         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1311         struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr);    /*udc_device->bus->endpoint_array + ep; */
1312
1313         UDCDBGA ("disable ep_addr %d", ep_addr);
1314
1315         if (!ep_num) {
1316                 /* nothing to do for endpoint 0 */ ;
1317         } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1318                 if (endpoint->tx_packetSize) {
1319                         /* we have a valid tx endpoint */
1320                         /*usbd_flush_tx(endpoint); */
1321                         endpoint->tx_urb = NULL;
1322                 }
1323         } else {
1324                 if (endpoint->rcv_packetSize) {
1325                         /* we have a valid rx endpoint */
1326                         /*usbd_flush_rcv(endpoint); */
1327                         endpoint->rcv_urb = NULL;
1328                 }
1329         }
1330 }
1331 #endif
1332
1333 /* ************************************************************************** */
1334
1335 /**
1336  * udc_connected - is the USB cable connected
1337  *
1338  * Return non-zero if cable is connected.
1339  */
1340 #if 0
1341 int udc_connected (void)
1342 {
1343         return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
1344 }
1345 #endif
1346
1347 /* Turn on the USB connection by enabling the pullup resistor */
1348 void udc_connect (void)
1349 {
1350         UDCDBG ("connect, enable Pullup");
1351         outl (0x00000018, FUNC_MUX_CTRL_D);
1352 }
1353
1354 /* Turn off the USB connection by disabling the pullup resistor */
1355 void udc_disconnect (void)
1356 {
1357         UDCDBG ("disconnect, disable Pullup");
1358         outl (0x00000000, FUNC_MUX_CTRL_D);
1359 }
1360
1361 /* ************************************************************************** */
1362
1363
1364 /*
1365  * udc_disable_interrupts - disable interrupts
1366  * switch off interrupts
1367  */
1368 #if 0
1369 void udc_disable_interrupts (struct usb_device_instance *device)
1370 {
1371         UDCDBG ("disabling all interrupts");
1372         outw (0, UDC_IRQ_EN);
1373 }
1374 #endif
1375
1376 /* ************************************************************************** */
1377
1378 /**
1379  * udc_ep0_packetsize - return ep0 packetsize
1380  */
1381 #if 0
1382 int udc_ep0_packetsize (void)
1383 {
1384         return EP0_PACKETSIZE;
1385 }
1386 #endif
1387
1388 /* Switch on the UDC */
1389 void udc_enable (struct usb_device_instance *device)
1390 {
1391         UDCDBGA ("enable device %p, status %d", device, device->status);
1392
1393         /* initialize driver state variables */
1394         udc_devstat = 0;
1395
1396         /* Save the device structure pointer */
1397         udc_device = device;
1398
1399         /* Setup ep0 urb */
1400         if (!ep0_urb) {
1401                 ep0_urb =
1402                         usbd_alloc_urb (udc_device,
1403                                         udc_device->bus->endpoint_array);
1404         } else {
1405                 serial_printf ("udc_enable: ep0_urb already allocated %p\n",
1406                                ep0_urb);
1407         }
1408
1409         UDCDBG ("Check clock status");
1410         UDCREG (STATUS_REQ);
1411
1412         /* The VBUS_MODE bit selects whether VBUS detection is done via
1413          * software (1) or hardware (0).  When software detection is
1414          * selected, VBUS_CTRL selects whether USB is not connected (0)
1415          * or connected (1).
1416          */
1417         outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
1418               FUNC_MUX_CTRL_0);
1419         UDCREGL (FUNC_MUX_CTRL_0);
1420
1421         omap1510_configure_device (device);
1422 }
1423
1424 /* Switch off the UDC */
1425 void udc_disable (void)
1426 {
1427         UDCDBG ("disable UDC");
1428
1429         omap1510_deconfigure_device ();
1430
1431         /* The VBUS_MODE bit selects whether VBUS detection is done via
1432          * software (1) or hardware (0).  When software detection is
1433          * selected, VBUS_CTRL selects whether USB is not connected (0)
1434          * or connected (1).
1435          */
1436         outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1437         outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1438         UDCREGL (FUNC_MUX_CTRL_0);
1439
1440         /* Free ep0 URB */
1441         if (ep0_urb) {
1442                 /*usbd_dealloc_urb(ep0_urb); */
1443                 ep0_urb = NULL;
1444         }
1445
1446         /* Reset device pointer.
1447          * We ought to do this here to balance the initialization of udc_device
1448          * in udc_enable, but some of our other exported functions get called
1449          * by the bus interface driver after udc_disable, so we have to hang on
1450          * to the device pointer to avoid a null pointer dereference. */
1451         /* udc_device = NULL; */
1452 }
1453
1454 /**
1455  * udc_startup - allow udc code to do any additional startup
1456  */
1457 void udc_startup_events (struct usb_device_instance *device)
1458 {
1459         /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
1460         usbd_device_event_irq (device, DEVICE_INIT, 0);
1461
1462         /* The DEVICE_CREATE event puts the USB device in the state
1463          * STATE_ATTACHED.
1464          */
1465         usbd_device_event_irq (device, DEVICE_CREATE, 0);
1466
1467         /* Some USB controller driver implementations signal
1468          * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
1469          * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
1470          * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
1471          * The OMAP USB client controller has the capability to detect when the
1472          * USB cable is connected to a powered USB bus via the ATT bit in the
1473          * DEVSTAT register, so we will defer the DEVICE_HUB_CONFIGURED and
1474          * DEVICE_RESET events until later.
1475          */
1476
1477         udc_enable (device);
1478 }
1479
1480 /**
1481  * udc_irq - do pseudo interrupts
1482  */
1483 void udc_irq(void)
1484 {
1485         /* Loop while we have interrupts.
1486          * If we don't do this, the input chain
1487          * polling delay is likely to miss
1488          * host requests.
1489          */
1490         while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
1491                 /* Handle any new IRQs */
1492                 omap1510_udc_irq ();
1493                 omap1510_udc_noniso_irq ();
1494         }
1495 }
1496
1497 /* Flow control */
1498 void udc_set_nak(int epid)
1499 {
1500         /* TODO: implement this functionality in omap1510 */
1501 }
1502
1503 void udc_unset_nak (int epid)
1504 {
1505         /* TODO: implement this functionality in omap1510 */
1506 }