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