uboot/drivers/usb/gadget/pxa27x_udc.c
<<
>>
Prefs
   1/*
   2 * PXA27x USB device driver for u-boot.
   3 *
   4 * Copyright (C) 2007 Rodolfo Giometti <giometti@linux.it>
   5 * Copyright (C) 2007 Eurotech S.p.A.  <info@eurotech.it>
   6 * Copyright (C) 2008 Vivek Kutal      <vivek.kutal@azingo.com>
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License as
  10 * published by the Free Software Foundation; either version 2 of
  11 * the License, or (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21 * MA 02111-1307 USA
  22 *
  23 */
  24
  25
  26#include <common.h>
  27#include <config.h>
  28#include <asm/byteorder.h>
  29#include <usbdcore.h>
  30#include <usbdcore_ep0.h>
  31#include <asm/arch/hardware.h>
  32#include <usb/pxa27x_udc.h>
  33
  34/* number of endpoints on this UDC */
  35#define UDC_MAX_ENDPOINTS       24
  36
  37static struct urb *ep0_urb;
  38static struct usb_device_instance *udc_device;
  39static int ep0state = EP0_IDLE;
  40
  41#ifdef USBDDBG
  42static void udc_dump_buffer(char *name, u8 *buf, int len)
  43{
  44        usbdbg("%s - buf %p, len %d", name, buf, len);
  45        print_buffer(0, buf, 1, len, 0);
  46}
  47#else
  48#define udc_dump_buffer(name, buf, len)         /* void */
  49#endif
  50
  51static inline void udc_ack_int_UDCCR(int mask)
  52{
  53        USIR1   = mask | USIR1;
  54}
  55
  56/*
  57 * If the endpoint has an active tx_urb, then the next packet of data from the
  58 * URB is written to the tx FIFO.
  59 * The total amount of data in the urb is given by urb->actual_length.
  60 * The maximum amount of data that can be sent in any one packet is given by
  61 * endpoint->tx_packetSize.
  62 * The number of data bytes from this URB that have already been transmitted
  63 * is given by endpoint->sent.
  64 * endpoint->last is updated by this routine with the number of data bytes
  65 * transmitted in this packet.
  66 */
  67static int udc_write_urb(struct usb_endpoint_instance *endpoint)
  68{
  69        struct urb *urb = endpoint->tx_urb;
  70        int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
  71        u32 *addr32 = (u32 *) &UDCDN(ep_num);
  72        u32 *data32 = (u32 *) urb->buffer;
  73        u8  *addr8 = (u8 *) &UDCDN(ep_num);
  74        u8  *data8 = (u8 *) urb->buffer;
  75        unsigned int i, n, w, b, is_short;
  76        int timeout = 2000;     /* 2ms */
  77
  78        if (!urb || !urb->actual_length)
  79                return -1;
  80
  81        n = MIN(urb->actual_length - endpoint->sent, endpoint->tx_packetSize);
  82        if (n <= 0)
  83                return -1;
  84
  85        usbdbg("write urb on ep %d", ep_num);
  86#if defined(USBDDBG) && defined(USBDPARANOIA)
  87        usbdbg("urb: buf %p, buf_len %d, actual_len %d",
  88                urb->buffer, urb->buffer_length, urb->actual_length);
  89        usbdbg("endpoint: sent %d, tx_packetSize %d, last %d",
  90                endpoint->sent, endpoint->tx_packetSize, endpoint->last);
  91#endif
  92
  93        is_short = n != endpoint->tx_packetSize;
  94        w = n / 4;
  95        b = n % 4;
  96        usbdbg("n %d%s w %d b %d", n, is_short ? "-s" : "", w, b);
  97        udc_dump_buffer("urb write", data8 + endpoint->sent, n);
  98
  99        /* Prepare for data send */
 100        if (ep_num)
 101                UDCCSN(ep_num) = UDCCSR_PC;
 102
 103        for (i = 0; i < w; i++)
 104                *addr32 = data32[endpoint->sent/4 + i];
 105        for (i = 0; i < b; i++)
 106                *addr8 = data8[endpoint->sent + w*4 + i];
 107
 108        /* Set "Packet Complete" if less data then tx_packetSize */
 109        if (is_short)
 110                UDCCSN(ep_num) = ep_num ? UDCCSR_SP : UDCCSR0_IPR;
 111
 112        /* Wait for data sent */
 113        while (!(UDCCSN(ep_num) & (ep_num ? UDCCSR_PC : UDCCSR0_IPR))) {
 114                if (ep_num) {
 115                        if (timeout-- == 0)
 116                                return -1;
 117                        else
 118                                udelay(1);
 119                };
 120        }
 121        endpoint->last = n;
 122
 123        if (ep_num) {
 124                usbd_tx_complete(endpoint);
 125        } else {
 126                endpoint->sent += n;
 127                endpoint->last -= n;
 128        }
 129
 130        if ((endpoint->tx_urb->actual_length - endpoint->sent) <= 0) {
 131                urb->actual_length = 0;
 132                endpoint->sent = 0;
 133                endpoint->last = 0;
 134        }
 135
 136        if ((endpoint->sent >= urb->actual_length) && (!ep_num)) {
 137                usbdbg("ep0 IN stage done");
 138                if (is_short)
 139                        ep0state = EP0_IDLE;
 140                else
 141                        ep0state = EP0_XFER_COMPLETE;
 142        }
 143
 144        return 0;
 145}
 146
 147static int udc_read_urb(struct usb_endpoint_instance *endpoint)
 148{
 149        struct urb *urb = endpoint->rcv_urb;
 150        int ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
 151        u32 *addr32 = (u32 *) &UDCDN(ep_num);
 152        u32 *data32 = (u32 *) urb->buffer;
 153        unsigned int i, n, is_short ;
 154
 155        usbdbg("read urb on ep %d", ep_num);
 156#if defined(USBDDBG) && defined(USBDPARANOIA)
 157        usbdbg("urb: buf %p, buf_len %d, actual_len %d",
 158                urb->buffer, urb->buffer_length, urb->actual_length);
 159        usbdbg("endpoint: rcv_packetSize %d",
 160                endpoint->rcv_packetSize);
 161#endif
 162
 163        if (UDCCSN(ep_num) & UDCCSR_BNE)
 164                n = UDCBCN(ep_num) & 0x3ff;
 165        else /* zlp */
 166                n = 0;
 167        is_short = n != endpoint->rcv_packetSize;
 168
 169        usbdbg("n %d%s", n, is_short ? "-s" : "");
 170        for (i = 0; i < n; i += 4)
 171                data32[urb->actual_length/4 + i/4] = *addr32;
 172
 173        udc_dump_buffer("urb read", (u8 *) data32, urb->actual_length + n);
 174        usbd_rcv_complete(endpoint, n, 0);
 175
 176        return 0;
 177}
 178
 179static int udc_read_urb_ep0(void)
 180{
 181        u32 *addr32 = (u32 *) &UDCDN(0);
 182        u32 *data32 = (u32 *) ep0_urb->buffer;
 183        u8 *addr8 = (u8 *) &UDCDN(0);
 184        u8 *data8 = (u8 *) ep0_urb->buffer;
 185        unsigned int i, n, w, b;
 186
 187        n = UDCBCR0;
 188        w = n / 4;
 189        b = n % 4;
 190
 191        for (i = 0; i < w; i++) {
 192                data32[ep0_urb->actual_length/4 + i] = *addr32;
 193                ep0_urb->actual_length += 4;
 194        }
 195
 196        for (i = 0; i < b; i++) {
 197                data8[ep0_urb->actual_length + w*4 + i] = *addr8;
 198                ep0_urb->actual_length++;
 199        }
 200
 201        UDCCSR0 = UDCCSR0_OPC | UDCCSR0_IPR;
 202        if (ep0_urb->actual_length == ep0_urb->device_request.wLength)
 203                return 1;
 204
 205        return 0;
 206}
 207
 208static void udc_handle_ep0(struct usb_endpoint_instance *endpoint)
 209{
 210        u32 udccsr0 = UDCCSR0;
 211        u32 *data = (u32 *) &ep0_urb->device_request;
 212        int i;
 213
 214        usbdbg("udccsr0 %x", udccsr0);
 215
 216        /* Clear stall status */
 217        if (udccsr0 & UDCCSR0_SST) {
 218                usberr("clear stall status");
 219                UDCCSR0 = UDCCSR0_SST;
 220                ep0state = EP0_IDLE;
 221        }
 222
 223        /* previous request unfinished?  non-error iff back-to-back ... */
 224        if ((udccsr0 & UDCCSR0_SA) != 0 && ep0state != EP0_IDLE)
 225                ep0state = EP0_IDLE;
 226
 227        switch (ep0state) {
 228
 229        case EP0_IDLE:
 230
 231                udccsr0 = UDCCSR0;
 232                /* Start control request? */
 233                if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE))
 234                        == (UDCCSR0_OPC | UDCCSR0_SA | UDCCSR0_RNE)) {
 235
 236                        /* Read SETUP packet.
 237                         * SETUP packet size is 8 bytes (aka 2 words)
 238                         */
 239                        usbdbg("try reading SETUP packet");
 240                        for (i = 0; i < 2; i++) {
 241                                if ((UDCCSR0 & UDCCSR0_RNE) == 0) {
 242                                        usberr("setup packet too short:%d", i);
 243                                        goto stall;
 244                                }
 245                                data[i] = UDCDR0;
 246                        }
 247
 248                        UDCCSR0 |= (UDCCSR0_OPC | UDCCSR0_SA);
 249                        if ((UDCCSR0 & UDCCSR0_RNE) != 0) {
 250                                usberr("setup packet too long");
 251                                goto stall;
 252                        }
 253
 254                        udc_dump_buffer("ep0 setup read", (u8 *) data, 8);
 255
 256                        if (ep0_urb->device_request.wLength == 0) {
 257                                usbdbg("Zero Data control Packet\n");
 258                                if (ep0_recv_setup(ep0_urb)) {
 259                                        usberr("Invalid Setup Packet\n");
 260                                        udc_dump_buffer("ep0 setup read",
 261                                                                (u8 *)data, 8);
 262                                        goto stall;
 263                                }
 264                                UDCCSR0 = UDCCSR0_IPR;
 265                                ep0state = EP0_IDLE;
 266                        } else {
 267                                /* Check direction */
 268                                if ((ep0_urb->device_request.bmRequestType &
 269                                                USB_REQ_DIRECTION_MASK)
 270                                                == USB_REQ_HOST2DEVICE) {
 271                                        ep0state = EP0_OUT_DATA;
 272                                        ep0_urb->buffer =
 273                                                (u8 *)ep0_urb->buffer_data;
 274                                        ep0_urb->buffer_length =
 275                                                sizeof(ep0_urb->buffer_data);
 276                                        ep0_urb->actual_length = 0;
 277                                        UDCCSR0 = UDCCSR0_IPR;
 278                                } else {
 279                                        /* The ep0_recv_setup function has
 280                                         * already placed our response packet
 281                                         * data in ep0_urb->buffer and the
 282                                         * packet length in
 283                                         * ep0_urb->actual_length.
 284                                         */
 285                                        if (ep0_recv_setup(ep0_urb)) {
 286stall:
 287                                                usberr("Invalid setup packet");
 288                                                udc_dump_buffer("ep0 setup read"
 289                                                        , (u8 *) data, 8);
 290                                                ep0state = EP0_IDLE;
 291
 292                                                UDCCSR0 = UDCCSR0_SA |
 293                                                UDCCSR0_OPC | UDCCSR0_FST |
 294                                                UDCCS0_FTF;
 295
 296                                                return;
 297                                        }
 298
 299                                        endpoint->tx_urb = ep0_urb;
 300                                        endpoint->sent = 0;
 301                                        usbdbg("EP0_IN_DATA");
 302                                        ep0state = EP0_IN_DATA;
 303                                        if (udc_write_urb(endpoint) < 0)
 304                                                goto stall;
 305
 306                                }
 307                        }
 308                        return;
 309                } else if ((udccsr0 & (UDCCSR0_OPC | UDCCSR0_SA))
 310                        == (UDCCSR0_OPC|UDCCSR0_SA)) {
 311                        usberr("Setup Active but no data. Stalling ....\n");
 312                        goto stall;
 313                } else {
 314                        usbdbg("random early IRQs");
 315                        /* Some random early IRQs:
 316                         * - we acked FST
 317                         * - IPR cleared
 318                         * - OPC got set, without SA (likely status stage)
 319                         */
 320                        UDCCSR0 = udccsr0 & (UDCCSR0_SA | UDCCSR0_OPC);
 321                }
 322                break;
 323
 324        case EP0_OUT_DATA:
 325
 326                if ((udccsr0 & UDCCSR0_OPC) && !(udccsr0 & UDCCSR0_SA)) {
 327                        if (udc_read_urb_ep0()) {
 328read_complete:
 329                                ep0state = EP0_IDLE;
 330                                if (ep0_recv_setup(ep0_urb)) {
 331                                        /* Not a setup packet, stall next
 332                                         * EP0 transaction
 333                                         */
 334                                        udc_dump_buffer("ep0 setup read",
 335                                                        (u8 *) data, 8);
 336                                        usberr("can't parse setup packet\n");
 337                                        goto stall;
 338                                }
 339                        }
 340                } else if (!(udccsr0 & UDCCSR0_OPC) &&
 341                                !(udccsr0 & UDCCSR0_IPR)) {
 342                        if (ep0_urb->device_request.wLength ==
 343                                ep0_urb->actual_length)
 344                                goto read_complete;
 345
 346                        usberr("Premature Status\n");
 347                        ep0state = EP0_IDLE;
 348                }
 349                break;
 350
 351        case EP0_IN_DATA:
 352                /* GET_DESCRIPTOR etc */
 353                if (udccsr0 & UDCCSR0_OPC) {
 354                        UDCCSR0 = UDCCSR0_OPC | UDCCSR0_FTF;
 355                        usberr("ep0in premature status");
 356                        ep0state = EP0_IDLE;
 357                } else {
 358                        /* irq was IPR clearing */
 359                        if (udc_write_urb(endpoint) < 0) {
 360                                usberr("ep0_write_error\n");
 361                                goto stall;
 362                        }
 363                }
 364                break;
 365
 366        case EP0_XFER_COMPLETE:
 367                UDCCSR0 = UDCCSR0_IPR;
 368                ep0state = EP0_IDLE;
 369                break;
 370
 371        default:
 372                usbdbg("Default\n");
 373        }
 374        USIR0 = USIR0_IR0;
 375}
 376
 377static void udc_handle_ep(struct usb_endpoint_instance *endpoint)
 378{
 379        int ep_addr = endpoint->endpoint_address;
 380        int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
 381        int ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
 382
 383        u32 flags = UDCCSN(ep_num) & (UDCCSR_SST | UDCCSR_TRN);
 384        if (flags)
 385                UDCCSN(ep_num) = flags;
 386
 387        if (ep_isout)
 388                udc_read_urb(endpoint);
 389        else
 390                udc_write_urb(endpoint);
 391
 392        UDCCSN(ep_num) = UDCCSR_PC;
 393}
 394
 395static void udc_state_changed(void)
 396{
 397        int config, interface, alternate;
 398
 399        UDCCR |= UDCCR_SMAC;
 400
 401        config = (UDCCR & UDCCR_ACN) >> UDCCR_ACN_S;
 402        interface = (UDCCR & UDCCR_AIN) >> UDCCR_AIN_S;
 403        alternate = (UDCCR & UDCCR_AAISN) >> UDCCR_AAISN_S;
 404
 405        usbdbg("New UDC settings are: conf %d - inter %d - alter %d",
 406                config, interface, alternate);
 407
 408        usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
 409        UDCISR1 = UDCISR1_IRCC;
 410}
 411
 412void udc_irq(void)
 413{
 414        int handled;
 415        struct usb_endpoint_instance *endpoint;
 416        int ep_num, i;
 417        u32 udcisr0;
 418
 419        do {
 420                handled = 0;
 421                /* Suspend Interrupt Request */
 422                if (USIR1 & UDCCR_SUSIR) {
 423                        usbdbg("Suspend\n");
 424                        udc_ack_int_UDCCR(UDCCR_SUSIR);
 425                        handled = 1;
 426                        ep0state = EP0_IDLE;
 427                }
 428
 429                /* Resume Interrupt Request */
 430                if (USIR1 & UDCCR_RESIR) {
 431                        udc_ack_int_UDCCR(UDCCR_RESIR);
 432                        handled = 1;
 433                        usbdbg("USB resume\n");
 434                }
 435
 436                if (USIR1 & (1<<31)) {
 437                        handled = 1;
 438                        udc_state_changed();
 439                }
 440
 441                /* Reset Interrupt Request */
 442                if (USIR1 & UDCCR_RSTIR) {
 443                        udc_ack_int_UDCCR(UDCCR_RSTIR);
 444                        handled = 1;
 445                        usbdbg("Reset\n");
 446                        usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
 447                } else {
 448                        if (USIR0)
 449                                usbdbg("UISR0: %x \n", USIR0);
 450
 451                        if (USIR0 & 0x2)
 452                                USIR0 = 0x2;
 453
 454                        /* Control traffic */
 455                        if (USIR0  & USIR0_IR0) {
 456                                handled = 1;
 457                                udc_handle_ep0(udc_device->bus->endpoint_array);
 458                                USIR0 = USIR0_IR0;
 459                        }
 460
 461                        endpoint = udc_device->bus->endpoint_array;
 462                        for (i = 0; i < udc_device->bus->max_endpoints; i++) {
 463                                ep_num = (endpoint[i].endpoint_address) &
 464                                                USB_ENDPOINT_NUMBER_MASK;
 465                                if (!ep_num)
 466                                        continue;
 467                                udcisr0 = UDCISR0;
 468                                if (udcisr0 &
 469                                        UDCISR_INT(ep_num, UDC_INT_PACKETCMP)) {
 470                                        UDCISR0 = UDCISR_INT(ep_num,
 471                                                         UDC_INT_PACKETCMP);
 472                                        udc_handle_ep(&endpoint[i]);
 473                                }
 474                        }
 475                }
 476
 477        } while (handled);
 478}
 479
 480/* The UDCCR reg contains mask and interrupt status bits,
 481 * so using '|=' isn't safe as it may ack an interrupt.
 482 */
 483#define UDCCR_OEN               (1 << 31)   /* On-the-Go Enable */
 484#define UDCCR_MASK_BITS         (UDCCR_OEN | UDCCR_UDE)
 485
 486static inline void udc_set_mask_UDCCR(int mask)
 487{
 488    UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS);
 489}
 490
 491static inline void udc_clear_mask_UDCCR(int mask)
 492{
 493    UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS);
 494}
 495
 496static void pio_irq_enable(int ep_num)
 497{
 498        if (ep_num < 16)
 499                UDCICR0 |= 3 << (ep_num * 2);
 500        else {
 501                ep_num -= 16;
 502                UDCICR1 |= 3 << (ep_num * 2);
 503        }
 504}
 505
 506/*
 507 * udc_set_nak
 508 *
 509 * Allow upper layers to signal lower layers should not accept more RX data
 510 */
 511void udc_set_nak(int ep_num)
 512{
 513        /* TODO */
 514}
 515
 516/*
 517 * udc_unset_nak
 518 *
 519 * Suspend sending of NAK tokens for DATA OUT tokens on a given endpoint.
 520 * Switch off NAKing on this endpoint to accept more data output from host.
 521 */
 522void udc_unset_nak(int ep_num)
 523{
 524        /* TODO */
 525}
 526
 527int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
 528{
 529        return udc_write_urb(endpoint);
 530}
 531
 532/* Associate a physical endpoint with endpoint instance */
 533void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
 534                                struct usb_endpoint_instance *endpoint)
 535{
 536        int ep_num, ep_addr, ep_isout, ep_type, ep_size;
 537        int config, interface, alternate;
 538        u32 tmp;
 539
 540        usbdbg("setting up endpoint id %d", id);
 541
 542        if (!endpoint) {
 543                usberr("endpoint void!");
 544                return;
 545        }
 546
 547        ep_num = endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
 548        if (ep_num >= UDC_MAX_ENDPOINTS) {
 549                usberr("unable to setup ep %d!", ep_num);
 550                return;
 551        }
 552
 553        pio_irq_enable(ep_num);
 554        if (ep_num == 0) {
 555                /* Done for ep0 */
 556                return;
 557        }
 558
 559        config = 1;
 560        interface = 0;
 561        alternate = 0;
 562
 563        usbdbg("config %d - interface %d - alternate %d",
 564                config, interface, alternate);
 565
 566        ep_addr = endpoint->endpoint_address;
 567        ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
 568        ep_isout = (ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT;
 569        ep_type = ep_isout ? endpoint->rcv_attributes : endpoint->tx_attributes;
 570        ep_size = ep_isout ? endpoint->rcv_packetSize : endpoint->tx_packetSize;
 571
 572        usbdbg("addr %x, num %d, dir %s, type %s, packet size %d",
 573                ep_addr, ep_num,
 574                ep_isout ? "out" : "in",
 575                ep_type == USB_ENDPOINT_XFER_ISOC ? "isoc" :
 576                ep_type == USB_ENDPOINT_XFER_BULK ? "bulk" :
 577                ep_type == USB_ENDPOINT_XFER_INT ? "int" : "???",
 578                ep_size
 579                );
 580
 581        /* Configure UDCCRx */
 582        tmp = 0;
 583        tmp |= (config << UDCCONR_CN_S) & UDCCONR_CN;
 584        tmp |= (interface << UDCCONR_IN_S) & UDCCONR_IN;
 585        tmp |= (alternate << UDCCONR_AISN_S) & UDCCONR_AISN;
 586        tmp |= (ep_num << UDCCONR_EN_S) & UDCCONR_EN;
 587        tmp |= (ep_type << UDCCONR_ET_S) & UDCCONR_ET;
 588        tmp |= ep_isout ? 0 : UDCCONR_ED;
 589        tmp |= (ep_size << UDCCONR_MPS_S) & UDCCONR_MPS;
 590        tmp |= UDCCONR_EE;
 591
 592        UDCCN(ep_num) = tmp;
 593
 594        usbdbg("UDCCR%c = %x", 'A' + ep_num-1, UDCCN(ep_num));
 595        usbdbg("UDCCSR%c = %x", 'A' + ep_num-1, UDCCSN(ep_num));
 596}
 597
 598#define CONFIG_USB_DEV_PULLUP_GPIO 87
 599
 600/* Connect the USB device to the bus */
 601void udc_connect(void)
 602{
 603        usbdbg("UDC connect");
 604
 605        /* Turn on the USB connection by enabling the pullup resistor */
 606        set_GPIO_mode(CONFIG_USB_DEV_PULLUP_GPIO | GPIO_OUT);
 607        GPSR(CONFIG_USB_DEV_PULLUP_GPIO) = GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO);
 608}
 609
 610/* Disconnect the USB device to the bus */
 611void udc_disconnect(void)
 612{
 613        usbdbg("UDC disconnect");
 614
 615        /* Turn off the USB connection by disabling the pullup resistor */
 616        GPCR(CONFIG_USB_DEV_PULLUP_GPIO) = GPIO_bit(CONFIG_USB_DEV_PULLUP_GPIO);
 617}
 618
 619/* Switch on the UDC */
 620void udc_enable(struct usb_device_instance *device)
 621{
 622
 623        ep0state = EP0_IDLE;
 624        CKEN |= CKEN11_USB;
 625
 626        /* enable endpoint 0, A, B's Packet Complete Interrupt. */
 627        UDCICR0 = 0x0000003f;
 628        UDCICR1 = 0xa8000000;
 629
 630        /* clear the interrupt status/control registers */
 631        UDCISR0 = 0xffffffff;
 632        UDCISR1 = 0xffffffff;
 633
 634        /* set UDC-enable */
 635        udc_set_mask_UDCCR(UDCCR_UDE);
 636
 637        udc_device = device;
 638        if (!ep0_urb)
 639                ep0_urb = usbd_alloc_urb(udc_device,
 640                                udc_device->bus->endpoint_array);
 641        else
 642                usbinfo("ep0_urb %p already allocated", ep0_urb);
 643
 644        usbdbg("UDC Enabled\n");
 645}
 646
 647/* Need to check this again */
 648void udc_disable(void)
 649{
 650        usbdbg("disable UDC");
 651
 652        udc_clear_mask_UDCCR(UDCCR_UDE);
 653
 654        /* Disable clock for USB device */
 655        CKEN &= ~CKEN11_USB;
 656
 657        /* Free ep0 URB */
 658        if (ep0_urb) {
 659                usbd_dealloc_urb(ep0_urb);
 660                ep0_urb = NULL;
 661        }
 662
 663        /* Reset device pointer */
 664        udc_device = NULL;
 665}
 666
 667/* Allow udc code to do any additional startup */
 668void udc_startup_events(struct usb_device_instance *device)
 669{
 670        /* The DEVICE_INIT event puts the USB device in the state STATE_INIT */
 671        usbd_device_event_irq(device, DEVICE_INIT, 0);
 672
 673        /* The DEVICE_CREATE event puts the USB device in the state
 674         * STATE_ATTACHED */
 675        usbd_device_event_irq(device, DEVICE_CREATE, 0);
 676
 677        /* Some USB controller driver implementations signal
 678         * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
 679         * DEVICE_HUB_CONFIGURED causes a transition to the state
 680         * STATE_POWERED, and DEVICE_RESET causes a transition to
 681         * the state STATE_DEFAULT.
 682         */
 683        udc_enable(device);
 684}
 685
 686/* Initialize h/w stuff */
 687int udc_init(void)
 688{
 689        udc_device = NULL;
 690        usbdbg("PXA27x usbd start");
 691
 692        /* Disable the UDC */
 693        udc_clear_mask_UDCCR(UDCCR_UDE);
 694
 695        /* Disable clock for USB device */
 696        CKEN &= ~CKEN11_USB;
 697
 698        /* Disable IRQs: we don't use them */
 699        UDCICR0 = UDCICR1 = 0;
 700
 701        return 0;
 702}
 703