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