]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/usbdcore_omap1510.c
Add support for Siemens SX1 mobile phone;
[karo-tx-uboot.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 #include <i2c.h>
35
36 #include "usbdcore.h"
37 #include "usbdcore_omap1510.h"
38 #include "usbdcore_ep0.h"
39
40
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 void 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
1083 /* Start to initialize h/w stuff */
1084 int udc_init (void)
1085 {
1086         u16 udc_rev;
1087         uchar wert;
1088
1089         udc_device = NULL;
1090
1091         UDCDBG ("starting");
1092
1093         /* Check peripheral reset. Must be 1 to make sure
1094            MPU TIPB peripheral reset is inactive */
1095         UDCREG (ARM_RSTCT2);
1096
1097         /* Set and check clock control.
1098          * We might ought to be using the clock control API to do
1099          * this instead of fiddling with the clock registers directly
1100          * here.
1101          */
1102         outw ((1 << 4) | (1 << 5), CLOCK_CTRL);
1103         UDCREG (CLOCK_CTRL);
1104         /* Set and check APLL */
1105         outw (0x0008, APLL_CTRL);
1106         UDCREG (APLL_CTRL);
1107         /* Set and check DPLL */
1108         outw (0x2210, DPLL_CTRL);
1109         UDCREG (DPLL_CTRL);
1110         /* Set and check SOFT */
1111         outw ((1 << 4) | (1 << 3) | 1, SOFT_REQ);
1112         /* Short delay to wait for DPLL */
1113         udelay (1000);
1114
1115         /* Print banner with device revision */
1116         udc_rev = inw (UDC_REV) & 0xff;
1117         printf ("USB:   TI OMAP1510 USB function module rev %d.%d\n",
1118                 udc_rev >> 4, udc_rev & 0xf);
1119
1120         /* Configure some Sofia Unit registers
1121          */
1122         i2c_read (0x32, 0x04, 1, &wert, 1);     /* SOF_Control_CONT */
1123         wert |= 0x04;           /* SOF_ContCONT_dis_usb_det */
1124         i2c_write (0x32, 0x04, 1, &wert, 1);
1125
1126         i2c_read (0x32, 0x03, 1, &wert, 1);     /* SOF_Control */
1127         wert |= 0x01;           /* SOF_Cont_usb_en */
1128         i2c_write (0x32, 0x03, 1, &wert, 1);
1129
1130         /* The VBUS_MODE bit selects whether VBUS detection is done via
1131          * software (1) or hardware (0).  When software detection is
1132          * selected, VBUS_CTRL selects whether USB is not connected (0)
1133          * or connected (1).
1134          */
1135         outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1136         outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1137         UDCREGL (FUNC_MUX_CTRL_0);
1138
1139         /*
1140          * At this point, device is ready for configuration...
1141          */
1142
1143         UDCDBG ("disable USB interrupts");
1144         outw (0, UDC_IRQ_EN);
1145         UDCREG (UDC_IRQ_EN);
1146
1147         UDCDBG ("disable USB DMA");
1148         outw (0, UDC_DMA_IRQ_EN);
1149         UDCREG (UDC_DMA_IRQ_EN);
1150
1151         UDCDBG ("initialize SYSCON1");
1152         outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
1153         UDCREG (UDC_SYSCON1);
1154
1155         return 0;
1156 }
1157
1158 /* Stall endpoint */
1159 static void udc_stall_ep (unsigned int ep_addr)
1160 {
1161         /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1162         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1163
1164         UDCDBGA ("stall ep_addr %d", ep_addr);
1165
1166         /* REVISIT?
1167          * The OMAP TRM section 14.2.4.2 says we must check that the FIFO
1168          * is empty before halting the endpoint.  The current implementation
1169          * doesn't check that the FIFO is empty.
1170          */
1171
1172         if (!ep_num) {
1173                 outw (UDC_Stall_Cmd, UDC_SYSCON2);
1174         } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1175                 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1176                         /* we have a valid rx endpoint, so halt it */
1177                         outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
1178                         outw (UDC_Set_Halt, UDC_CTRL);
1179                         outw (ep_num, UDC_EP_NUM);
1180                 }
1181         } else {
1182                 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1183                         /* we have a valid tx endpoint, so halt it */
1184                         outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
1185                         outw (UDC_Set_Halt, UDC_CTRL);
1186                         outw (ep_num, UDC_EP_NUM);
1187                 }
1188         }
1189 }
1190
1191 /* Reset endpoint */
1192 #if 0
1193 static void udc_reset_ep (unsigned int ep_addr)
1194 {
1195         /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1196         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1197
1198         UDCDBGA ("reset ep_addr %d", ep_addr);
1199
1200         if (!ep_num) {
1201                 /* control endpoint 0 can't be reset */
1202         } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1203                 UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
1204                          inw (UDC_EP_RX (ep_num)));
1205                 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1206                         /* we have a valid rx endpoint, so reset it */
1207                         outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
1208                         outw (UDC_Reset_EP, UDC_CTRL);
1209                         outw (ep_num, UDC_EP_NUM);
1210                         UDCDBGA ("OUT endpoint %d reset", ep_num);
1211                 }
1212         } else {
1213                 UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
1214                          inw (UDC_EP_TX (ep_num)));
1215                 /* Resetting of tx endpoints seems to be causing the USB function
1216                  * module to fail, which causes problems when the driver is
1217                  * uninstalled.  We'll skip resetting tx endpoints for now until
1218                  * we figure out what the problem is.
1219                  */
1220 #if 0
1221                 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1222                         /* we have a valid tx endpoint, so reset it */
1223                         outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
1224                         outw (UDC_Reset_EP, UDC_CTRL);
1225                         outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
1226                         UDCDBGA ("IN endpoint %d reset", ep_num);
1227                 }
1228 #endif
1229         }
1230 }
1231 #endif
1232
1233 /* ************************************************************************** */
1234
1235 /**
1236  * udc_check_ep - check logical endpoint
1237   *
1238  * Return physical endpoint number to use for this logical endpoint or zero if not valid.
1239  */
1240 #if 0
1241 int udc_check_ep (int logical_endpoint, int packetsize)
1242 {
1243         if ((logical_endpoint == 0x80) ||
1244             ((logical_endpoint & 0x8f) != logical_endpoint)) {
1245                 return 0;
1246         }
1247
1248         switch (packetsize) {
1249         case 8:
1250         case 16:
1251         case 32:
1252         case 64:
1253         case 128:
1254         case 256:
1255         case 512:
1256                 break;
1257         default:
1258                 return 0;
1259         }
1260
1261         return EP_ADDR_TO_PHYS_EP (logical_endpoint);
1262 }
1263 #endif
1264
1265 /*
1266  * udc_setup_ep - setup endpoint
1267  *
1268  * Associate a physical endpoint with endpoint_instance
1269  */
1270 void udc_setup_ep (struct usb_device_instance *device,
1271                    unsigned int ep, struct usb_endpoint_instance *endpoint)
1272 {
1273         UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
1274
1275         /* This routine gets called by bi_modinit for endpoint 0 and from
1276          * bi_config for all of the other endpoints.  bi_config gets called
1277          * during the DEVICE_CREATE, DEVICE_CONFIGURED, and
1278          * DEVICE_SET_INTERFACE events.  We need to reconfigure the OMAP packet
1279          * RAM after bi_config scans the selected device configuration and
1280          * initializes the endpoint structures, but before this routine enables
1281          * the OUT endpoint FIFOs.  Since bi_config calls this routine in a
1282          * loop for endpoints 1 through UDC_MAX_ENDPOINTS, we reconfigure our
1283          * packet RAM here when ep==1.
1284          * I really hate to do this here, but it seems like the API exported
1285          * by the USB bus interface controller driver to the usbd-bi module
1286          * isn't quite right so there is no good place to do this.
1287          */
1288         if (ep == 1) {
1289                 omap1510_deconfigure_device ();
1290                 omap1510_configure_device (device);
1291         }
1292
1293         if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
1294                 int ep_addr = endpoint->endpoint_address;
1295
1296                 if (!ep_addr) {
1297                         /* nothing to do for endpoint 0 */
1298                 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1299                         /* nothing to do for IN (tx) endpoints */
1300                 } else {        /* OUT (rx) endpoint */
1301                         if (endpoint->rcv_packetSize) {
1302                                 /*struct urb* urb = &(urb_out_array[ep&0xFF]); */
1303                                 /*urb->endpoint = endpoint; */
1304                                 /*urb->device = device; */
1305                                 /*urb->buffer_length = sizeof(urb->buffer); */
1306
1307                                 /*endpoint->rcv_urb = urb; */
1308                                 omap1510_prepare_endpoint_for_rx (ep_addr);
1309                         }
1310                 }
1311         }
1312 }
1313
1314 /**
1315  * udc_disable_ep - disable endpoint
1316  * @ep:
1317  *
1318  * Disable specified endpoint
1319  */
1320 #if 0
1321 void udc_disable_ep (unsigned int ep_addr)
1322 {
1323         /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1324         int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1325         struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr);    /*udc_device->bus->endpoint_array + ep; */
1326
1327         UDCDBGA ("disable ep_addr %d", ep_addr);
1328
1329         if (!ep_num) {
1330                 /* nothing to do for endpoint 0 */ ;
1331         } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1332                 if (endpoint->tx_packetSize) {
1333                         /* we have a valid tx endpoint */
1334                         /*usbd_flush_tx(endpoint); */
1335                         endpoint->tx_urb = NULL;
1336                 }
1337         } else {
1338                 if (endpoint->rcv_packetSize) {
1339                         /* we have a valid rx endpoint */
1340                         /*usbd_flush_rcv(endpoint); */
1341                         endpoint->rcv_urb = NULL;
1342                 }
1343         }
1344 }
1345 #endif
1346
1347 /* ************************************************************************** */
1348
1349 /**
1350  * udc_connected - is the USB cable connected
1351  *
1352  * Return non-zero if cable is connected.
1353  */
1354 #if 0
1355 int udc_connected (void)
1356 {
1357         return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
1358 }
1359 #endif
1360
1361 /* Turn on the USB connection by enabling the pullup resistor */
1362 void udc_connect (void)
1363 {
1364         UDCDBG ("connect, enable Pullup");
1365         outl (0x00000018, FUNC_MUX_CTRL_D);
1366 }
1367
1368 /* Turn off the USB connection by disabling the pullup resistor */
1369 void udc_disconnect (void)
1370 {
1371         UDCDBG ("disconnect, disable Pullup");
1372         outl (0x00000000, FUNC_MUX_CTRL_D);
1373 }
1374
1375 /* ************************************************************************** */
1376
1377
1378 /*
1379  * udc_disable_interrupts - disable interrupts
1380  * switch off interrupts
1381  */
1382 #if 0
1383 void udc_disable_interrupts (struct usb_device_instance *device)
1384 {
1385         UDCDBG ("disabling all interrupts");
1386         outw (0, UDC_IRQ_EN);
1387 }
1388 #endif
1389
1390 /* ************************************************************************** */
1391
1392 /**
1393  * udc_ep0_packetsize - return ep0 packetsize
1394  */
1395 #if 0
1396 int udc_ep0_packetsize (void)
1397 {
1398         return EP0_PACKETSIZE;
1399 }
1400 #endif
1401
1402 /* Switch on the UDC */
1403 void udc_enable (struct usb_device_instance *device)
1404 {
1405         UDCDBGA ("enable device %p, status %d", device, device->status);
1406
1407         /* initialize driver state variables */
1408         udc_devstat = 0;
1409
1410         /* Save the device structure pointer */
1411         udc_device = device;
1412
1413         /* Setup ep0 urb */
1414         if (!ep0_urb) {
1415                 ep0_urb =
1416                         usbd_alloc_urb (udc_device,
1417                                         udc_device->bus->endpoint_array);
1418         } else {
1419                 serial_printf ("udc_enable: ep0_urb already allocated %p\n",
1420                                ep0_urb);
1421         }
1422
1423         UDCDBG ("Check clock status");
1424         UDCREG (STATUS_REQ);
1425
1426         /* The VBUS_MODE bit selects whether VBUS detection is done via
1427          * software (1) or hardware (0).  When software detection is
1428          * selected, VBUS_CTRL selects whether USB is not connected (0)
1429          * or connected (1).
1430          */
1431         outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
1432               FUNC_MUX_CTRL_0);
1433         UDCREGL (FUNC_MUX_CTRL_0);
1434
1435         omap1510_configure_device (device);
1436 }
1437
1438 /* Switch off the UDC */
1439 void udc_disable (void)
1440 {
1441         UDCDBG ("disable UDC");
1442
1443         omap1510_deconfigure_device ();
1444
1445         /* The VBUS_MODE bit selects whether VBUS detection is done via
1446          * software (1) or hardware (0).  When software detection is
1447          * selected, VBUS_CTRL selects whether USB is not connected (0)
1448          * or connected (1).
1449          */
1450         outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1451         outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1452         UDCREGL (FUNC_MUX_CTRL_0);
1453
1454         /* Free ep0 URB */
1455         if (ep0_urb) {
1456                 /*usbd_dealloc_urb(ep0_urb); */
1457                 ep0_urb = NULL;
1458         }
1459
1460         /* Reset device pointer.
1461          * We ought to do this here to balance the initialization of udc_device
1462          * in udc_enable, but some of our other exported functions get called
1463          * by the bus interface driver after udc_disable, so we have to hang on
1464          * to the device pointer to avoid a null pointer dereference. */
1465         /* udc_device = NULL; */
1466 }
1467
1468 /**
1469  * udc_startup - allow udc code to do any additional startup
1470  */
1471 void udc_startup_events (struct usb_device_instance *device)
1472 {
1473         /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
1474         usbd_device_event_irq (device, DEVICE_INIT, 0);
1475
1476         /* The DEVICE_CREATE event puts the USB device in the state
1477          * STATE_ATTACHED.
1478          */
1479         usbd_device_event_irq (device, DEVICE_CREATE, 0);
1480
1481         /* Some USB controller driver implementations signal
1482          * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
1483          * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
1484          * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
1485          * The OMAP USB client controller has the capability to detect when the
1486          * USB cable is connected to a powered USB bus via the ATT bit in the
1487          * DEVSTAT register, so we will defer the DEVICE_HUB_CONFIGURED and
1488          * DEVICE_RESET events until later.
1489          */
1490
1491         udc_enable (device);
1492 }
1493
1494 #endif