Home | History | Annotate | Download | only in arch_msm7k
      1 /*
      2  * Copyright (c) 2008, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <boot/boot.h>
     30 #include <msm7k/hsusb.h>
     31 #include <boot/usb.h>
     32 #include <boot/usb_descriptors.h>
     33 
     34 #if 1
     35 #define DBG(x...) do {} while(0)
     36 #else
     37 #define DBG(x...) dprintf(x)
     38 #endif
     39 
     40 struct usb_endpoint
     41 {
     42     struct usb_endpoint *next;
     43     unsigned bit;
     44     struct ept_queue_head *head;
     45     struct usb_request *req;
     46     unsigned char num;
     47     unsigned char in;
     48 };
     49 
     50 struct usb_endpoint *ept_list = 0;
     51 struct ept_queue_head *epts = 0;
     52 
     53 static int usb_online = 0;
     54 static int usb_highspeed = 0;
     55 
     56 struct usb_endpoint *usb_endpoint_alloc(unsigned num, unsigned in, unsigned max_pkt)
     57 {
     58     struct usb_endpoint *ept;
     59     unsigned cfg;
     60 
     61     ept = alloc(sizeof(*ept));
     62 
     63     ept->num = num;
     64     ept->in = !!in;
     65     ept->req = 0;
     66 
     67     cfg = CONFIG_MAX_PKT(max_pkt) | CONFIG_ZLT;
     68 
     69     if(ept->in) {
     70         ept->bit = EPT_TX(ept->num);
     71     } else {
     72         ept->bit = EPT_RX(ept->num);
     73         if(num == 0)
     74             cfg |= CONFIG_IOS;
     75     }
     76 
     77     ept->head = epts + (num * 2) + (ept->in);
     78     ept->head->config = cfg;
     79 
     80     ept->next = ept_list;
     81     ept_list = ept;
     82 
     83     DBG("ept%d %s @%p/%p max=%d bit=%x\n",
     84             num, in ? "in":"out", ept, ept->head, max_pkt, ept->bit);
     85 
     86     return ept;
     87 }
     88 
     89 static void endpoint_enable(struct usb_endpoint *ept, unsigned yes)
     90 {
     91     unsigned n = readl(USB_ENDPTCTRL(ept->num));
     92 
     93     if(yes) {
     94         if(ept->in) {
     95             n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK);
     96         } else {
     97             n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK);
     98         }
     99 
    100         if(ept->num != 0) {
    101                 /* XXX should be more dynamic... */
    102             if(usb_highspeed) {
    103                 ept->head->config = CONFIG_MAX_PKT(512) | CONFIG_ZLT;
    104             } else {
    105                 ept->head->config = CONFIG_MAX_PKT(64) | CONFIG_ZLT;
    106             }
    107         }
    108     }
    109     writel(n, USB_ENDPTCTRL(ept->num));
    110 }
    111 
    112 struct usb_request *usb_request_alloc(unsigned bufsiz)
    113 {
    114     struct usb_request *req;
    115     req = alloc(sizeof(*req));
    116     req->buf = alloc(bufsiz);
    117     req->item = alloc(32);
    118     return req;
    119 }
    120 
    121 int usb_queue_req(struct usb_endpoint *ept, struct usb_request *req)
    122 {
    123     struct ept_queue_item *item = req->item;
    124     unsigned phys = (unsigned) req->buf;
    125 
    126     item->next = TERMINATE;
    127     item->info = INFO_BYTES(req->length) | INFO_IOC | INFO_ACTIVE;
    128     item->page0 = phys;
    129     item->page1 = (phys & 0xfffff000) + 0x1000;
    130 
    131     ept->head->next = (unsigned) item;
    132     ept->head->info = 0;
    133     ept->req = req;
    134 
    135     DBG("ept%d %s queue req=%p\n",
    136             ept->num, ept->in ? "in" : "out", req);
    137 
    138     writel(ept->bit, USB_ENDPTPRIME);
    139     return 0;
    140 }
    141 
    142 static void handle_ept_complete(struct usb_endpoint *ept)
    143 {
    144     struct ept_queue_item *item;
    145     unsigned actual;
    146     int status;
    147     struct usb_request *req;
    148 
    149     DBG("ept%d %s complete req=%p\n",
    150             ept->num, ept->in ? "in" : "out", ept->req);
    151 
    152     req = ept->req;
    153     if(req) {
    154         ept->req = 0;
    155 
    156         item = req->item;
    157 
    158         if(item->info & 0xff) {
    159             actual = 0;
    160             status = -1;
    161             dprintf("EP%d/%s FAIL nfo=%x pg0=%x\n",
    162                     ept->num, ept->in ? "in" : "out", item->info, item->page0);
    163         } else {
    164             actual = req->length - ((item->info >> 16) & 0x7fff);
    165             status = 0;
    166         }
    167         if(req->complete)
    168             req->complete(req, actual, status);
    169     }
    170 }
    171 
    172 static const char *reqname(unsigned r)
    173 {
    174     switch(r) {
    175     case GET_STATUS: return "GET_STATUS";
    176     case CLEAR_FEATURE: return "CLEAR_FEATURE";
    177     case SET_FEATURE: return "SET_FEATURE";
    178     case SET_ADDRESS: return "SET_ADDRESS";
    179     case GET_DESCRIPTOR: return "GET_DESCRIPTOR";
    180     case SET_DESCRIPTOR: return "SET_DESCRIPTOR";
    181     case GET_CONFIGURATION: return "GET_CONFIGURATION";
    182     case SET_CONFIGURATION: return "SET_CONFIGURATION";
    183     case GET_INTERFACE: return "GET_INTERFACE";
    184     case SET_INTERFACE: return "SET_INTERFACE";
    185     default: return "*UNKNOWN*";
    186     }
    187 }
    188 
    189 static struct usb_endpoint *ep0in, *ep0out;
    190 static struct usb_request *ep0req;
    191 
    192 static void setup_ack(void)
    193 {
    194     ep0req->complete = 0;
    195     ep0req->length = 0;
    196     usb_queue_req(ep0in, ep0req);
    197 }
    198 
    199 static void ep0in_complete(struct usb_request *req, unsigned actual, int status)
    200 {
    201     DBG("ep0in_complete %p %d %d\n", req, actual, status);
    202     if(status == 0) {
    203         req->length = 0;
    204         req->complete = 0;
    205         usb_queue_req(ep0out, req);
    206     }
    207 }
    208 
    209 static void setup_tx(void *buf, unsigned len)
    210 {
    211     DBG("setup_tx %p %d\n", buf, len);
    212     memcpy(ep0req->buf, buf, len);
    213     ep0req->complete = ep0in_complete;
    214     ep0req->length = len;
    215     usb_queue_req(ep0in, ep0req);
    216 }
    217 
    218 static unsigned char usb_config_value = 0;
    219 
    220 #define SETUP(type,request) (((type) << 8) | (request))
    221 
    222 static void handle_setup(struct usb_endpoint *ept)
    223 {
    224     setup_packet s;
    225 
    226     memcpy(&s, ept->head->setup_data, sizeof(s));
    227     writel(ept->bit, USB_ENDPTSETUPSTAT);
    228 
    229     DBG("handle_setup type=0x%b req=0x%b val=%d idx=%d len=%d (%s)\n",
    230             s.type, s.request, s.value, s.index, s.length,
    231             reqname(s.request));
    232 
    233     switch(SETUP(s.type,s.request)) {
    234     case SETUP(DEVICE_READ, GET_STATUS): {
    235         unsigned zero = 0;
    236         if(s.length == 2) {
    237             setup_tx(&zero, 2);
    238             return;
    239         }
    240         break;
    241     }
    242     case SETUP(DEVICE_READ, GET_DESCRIPTOR): {
    243         dtable *d = usb_highspeed ? descr_hs : descr_fs;
    244         while(d->data) {
    245             if(s.value == d->id) {
    246                 unsigned len = d->length;
    247                 if(len > s.length) len = s.length;
    248                 setup_tx(d->data, len);
    249                 return;
    250             }
    251             d++;
    252         }
    253         break;
    254     }
    255     case SETUP(DEVICE_READ, GET_CONFIGURATION):
    256             /* disabling this causes data transaction failures on OSX. Why?
    257              */
    258         if((s.value == 0) && (s.index == 0) && (s.length == 1)) {
    259             setup_tx(&usb_config_value, 1);
    260             return;
    261         }
    262         break;
    263     case SETUP(DEVICE_WRITE, SET_CONFIGURATION):
    264         if(s.value == 1) {
    265             struct usb_endpoint *ept;
    266                 /* enable endpoints */
    267             for(ept = ept_list; ept; ept = ept->next){
    268                 if(ept->num == 0)
    269                     continue;
    270                 endpoint_enable(ept, s.value);
    271             }
    272             usb_config_value = 1;
    273         } else {
    274             writel(0, USB_ENDPTCTRL(1));
    275             usb_config_value = 0;
    276         }
    277         setup_ack();
    278         usb_online = s.value ? 1 : 0;
    279         usb_status(s.value ? 1 : 0, usb_highspeed);
    280         return;
    281     case SETUP(DEVICE_WRITE, SET_ADDRESS):
    282             /* write address delayed (will take effect
    283             ** after the next IN txn)
    284             */
    285         writel((s.value << 25) | (1 << 24), USB_DEVICEADDR);
    286         setup_ack();
    287         return;
    288     case SETUP(INTERFACE_WRITE, SET_INTERFACE):
    289             /* if we ack this everything hangs */
    290             /* per spec, STALL is valid if there is not alt func */
    291         goto stall;
    292     case SETUP(ENDPOINT_WRITE, CLEAR_FEATURE): {
    293         struct usb_endpoint *ept;
    294         unsigned num = s.index & 15;
    295         unsigned in = !!(s.index & 0x80);
    296 
    297         if((s.value == 0) && (s.length == 0)) {
    298             DBG("clr feat %d %d\n", num, in);
    299             for(ept = ept_list; ept; ept = ept->next) {
    300                 if((ept->num == num) && (ept->in == in)) {
    301                     endpoint_enable(ept, 1);
    302                     setup_ack();
    303                     return;
    304                 }
    305             }
    306         }
    307         break;
    308     }
    309     }
    310 
    311     dprintf("STALL %s %b %b %d %d %d\n",
    312             reqname(s.request),
    313             s.type, s.request, s.value, s.index, s.length);
    314 
    315 stall:
    316     writel((1<<16) | (1 << 0), USB_ENDPTCTRL(ept->num));
    317 }
    318 
    319 unsigned ulpi_read(unsigned reg)
    320 {
    321         /* initiate read operation */
    322     writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
    323                USB_ULPI_VIEWPORT);
    324 
    325         /* wait for completion */
    326     while(readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ;
    327 
    328     return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
    329 }
    330 
    331 void ulpi_write(unsigned val, unsigned reg)
    332 {
    333         /* initiate write operation */
    334     writel(ULPI_RUN | ULPI_WRITE |
    335                ULPI_ADDR(reg) | ULPI_DATA(val),
    336                USB_ULPI_VIEWPORT);
    337 
    338         /* wait for completion */
    339     while(readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ;
    340 }
    341 
    342 void board_usb_init(void);
    343 void board_ulpi_init(void);
    344 
    345 void usb_init(void)
    346 {
    347     epts = alloc_page_aligned(4096);
    348 
    349     memset(epts, 0, 32 * sizeof(struct ept_queue_head));
    350 
    351     board_usb_init();
    352 
    353         /* select ULPI phy */
    354     writel(0x81000000, USB_PORTSC);
    355 
    356         /* RESET */
    357     writel(0x00080002, USB_USBCMD);
    358     mdelay(20);
    359 
    360     board_ulpi_init();
    361 
    362     writel((unsigned) epts, USB_ENDPOINTLISTADDR);
    363 
    364         /* select DEVICE mode */
    365     writel(0x02, USB_USBMODE);
    366 
    367     writel(0xffffffff, USB_ENDPTFLUSH);
    368 
    369         /* go to RUN mode (D+ pullup enable) */
    370     writel(0x00080001, USB_USBCMD);
    371 
    372 
    373     ep0out = usb_endpoint_alloc(0, 0, 64);
    374     ep0in = usb_endpoint_alloc(0, 1, 64);
    375     ep0req = usb_request_alloc(4096);
    376 }
    377 
    378 void usb_shutdown(void)
    379 {
    380         /* disable pullup */
    381     writel(0x0008000, USB_USBCMD);
    382     mdelay(10);
    383 }
    384 
    385 void usb_poll(void)
    386 {
    387     struct usb_endpoint *ept;
    388     unsigned n = readl(USB_USBSTS);
    389     writel(n, USB_USBSTS);
    390 
    391     n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI);
    392 
    393     if(n == 0) return;
    394 
    395     if(n & STS_URI) {
    396         writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE);
    397         writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT);
    398         writel(0xffffffff, USB_ENDPTFLUSH);
    399         writel(0, USB_ENDPTCTRL(1));
    400         DBG("-- reset --\n");
    401         usb_online = 0;
    402         usb_config_value = 0;
    403 
    404             /* error out any pending reqs */
    405         for(ept = ept_list; ept; ept = ept->next) {
    406             ept->head->info = INFO_ACTIVE;
    407             handle_ept_complete(ept);
    408         }
    409         usb_status(0, usb_highspeed);
    410     }
    411     if(n & STS_SLI) {
    412         DBG("-- suspend --\n");
    413     }
    414     if(n & STS_PCI) {
    415         DBG("-- portchange --\n");
    416         unsigned spd = (readl(USB_PORTSC) >> 26) & 3;
    417         if(spd == 2) {
    418             usb_highspeed = 1;
    419         } else {
    420             usb_highspeed = 0;
    421         }
    422     }
    423     if(n & STS_UEI) dprintf("<UEI %x>\n", readl(USB_ENDPTCOMPLETE));
    424 #if 0
    425     DBG("STS: ");
    426     if(n & STS_UEI) DBG("ERROR ");
    427     if(n & STS_SLI) DBG("SUSPEND ");
    428     if(n & STS_URI) DBG("RESET ");
    429     if(n & STS_PCI) DBG("PORTCHANGE ");
    430     if(n & STS_UI) DBG("USB ");
    431     DBG("\n");
    432 #endif
    433     if((n & STS_UI) || (n & STS_UEI)) {
    434         n = readl(USB_ENDPTSETUPSTAT);
    435         if(n & EPT_RX(0)) {
    436             handle_setup(ep0out);
    437         }
    438 
    439         n = readl(USB_ENDPTCOMPLETE);
    440         if(n != 0) {
    441             writel(n, USB_ENDPTCOMPLETE);
    442         }
    443 
    444         for(ept = ept_list; ept; ept = ept->next){
    445             if(n & ept->bit) {
    446                 handle_ept_complete(ept);
    447             }
    448         }
    449     }
    450 //    dprintf("@\n");
    451 }
    452 
    453 
    454