Brian Swetland | 3e7e21a | 2009-01-19 19:41:24 -0800 | [diff] [blame^] | 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 <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <debug.h> |
| 32 | #include <platform/iomap.h> |
| 33 | #include <platform/irqs.h> |
| 34 | #include <platform/interrupts.h> |
| 35 | #include <kernel/thread.h> |
| 36 | #include <reg.h> |
| 37 | |
| 38 | #include <dev/udc.h> |
| 39 | |
| 40 | #include "hsusb.h" |
| 41 | |
| 42 | /* common code - factor out into a shared file */ |
| 43 | |
| 44 | struct udc_descriptor { |
| 45 | struct udc_descriptor *next; |
| 46 | unsigned short tag; /* ((TYPE << 8) | NUM) */ |
| 47 | unsigned short len; /* total length */ |
| 48 | unsigned char data[0]; |
| 49 | }; |
| 50 | |
| 51 | struct udc_descriptor *udc_descriptor_alloc(unsigned type, unsigned num, unsigned len) |
| 52 | { |
| 53 | struct udc_descriptor *desc; |
| 54 | if ((len > 255) || (len < 2) || (num > 255) || (type > 255)) |
| 55 | return 0; |
| 56 | |
| 57 | if(!(desc = malloc(sizeof(struct udc_descriptor) + len))) |
| 58 | return 0; |
| 59 | |
| 60 | desc->next = 0; |
| 61 | desc->tag = (type << 8) | num; |
| 62 | desc->len = len; |
| 63 | desc->data[0] = len; |
| 64 | desc->data[1] = type; |
| 65 | |
| 66 | return desc; |
| 67 | } |
| 68 | |
| 69 | static struct udc_descriptor *desc_list = 0; |
| 70 | static unsigned next_string_id = 1; |
| 71 | |
| 72 | void udc_descriptor_register(struct udc_descriptor *desc) |
| 73 | { |
| 74 | desc->next = desc_list; |
| 75 | desc_list = desc; |
| 76 | } |
| 77 | |
| 78 | unsigned udc_string_desc_alloc(const char *str) |
| 79 | { |
| 80 | unsigned len; |
| 81 | struct udc_descriptor *desc; |
| 82 | unsigned char *data; |
| 83 | |
| 84 | if (next_string_id > 255) |
| 85 | return 0; |
| 86 | |
| 87 | if (!str) |
| 88 | return 0; |
| 89 | |
| 90 | len = strlen(str); |
| 91 | desc = udc_descriptor_alloc(TYPE_STRING, next_string_id, len * 2 + 2); |
| 92 | if (!desc) |
| 93 | return 0; |
| 94 | next_string_id++; |
| 95 | |
| 96 | /* expand ascii string to utf16 */ |
| 97 | data = desc->data + 2; |
| 98 | while (len-- > 0) { |
| 99 | *data++ = *str++; |
| 100 | *data++ = 0; |
| 101 | } |
| 102 | |
| 103 | udc_descriptor_register(desc); |
| 104 | return desc->tag & 0xff; |
| 105 | } |
| 106 | |
| 107 | /* end of common code */ |
| 108 | |
| 109 | void hsusb_clock_init(void); |
| 110 | |
| 111 | #if 1 |
| 112 | #define DBG(x...) do {} while(0) |
| 113 | #else |
| 114 | #define DBG(x...) dprintf(INFO, x) |
| 115 | #endif |
| 116 | |
| 117 | #define DBG1(x...) dprintf(INFO, x) |
| 118 | |
| 119 | #define usb_status(a,b) |
| 120 | |
| 121 | struct usb_request { |
| 122 | struct udc_request req; |
| 123 | struct ept_queue_item *item; |
| 124 | }; |
| 125 | |
| 126 | struct udc_endpoint |
| 127 | { |
| 128 | struct udc_endpoint *next; |
| 129 | unsigned bit; |
| 130 | struct ept_queue_head *head; |
| 131 | struct usb_request *req; |
| 132 | unsigned char num; |
| 133 | unsigned char in; |
| 134 | unsigned short maxpkt; |
| 135 | }; |
| 136 | |
| 137 | struct udc_endpoint *ept_list = 0; |
| 138 | struct ept_queue_head *epts = 0; |
| 139 | |
| 140 | static int usb_online = 0; |
| 141 | static int usb_highspeed = 0; |
| 142 | |
| 143 | static struct udc_device *the_device; |
| 144 | static struct udc_gadget *the_gadget; |
| 145 | |
| 146 | struct udc_endpoint *_udc_endpoint_alloc(unsigned num, unsigned in, unsigned max_pkt) |
| 147 | { |
| 148 | struct udc_endpoint *ept; |
| 149 | unsigned cfg; |
| 150 | |
| 151 | ept = malloc(sizeof(*ept)); |
| 152 | |
| 153 | ept->maxpkt = max_pkt; |
| 154 | ept->num = num; |
| 155 | ept->in = !!in; |
| 156 | ept->req = 0; |
| 157 | |
| 158 | cfg = CONFIG_MAX_PKT(max_pkt) | CONFIG_ZLT; |
| 159 | |
| 160 | if(ept->in) { |
| 161 | ept->bit = EPT_TX(ept->num); |
| 162 | } else { |
| 163 | ept->bit = EPT_RX(ept->num); |
| 164 | if(num == 0) |
| 165 | cfg |= CONFIG_IOS; |
| 166 | } |
| 167 | |
| 168 | ept->head = epts + (num * 2) + (ept->in); |
| 169 | ept->head->config = cfg; |
| 170 | |
| 171 | ept->next = ept_list; |
| 172 | ept_list = ept; |
| 173 | |
| 174 | // arch_clean_invalidate_cache_range(ept->head, 64); |
| 175 | DBG("ept%d %s @%p/%p max=%d bit=%x\n", |
| 176 | num, in ? "in":"out", ept, ept->head, max_pkt, ept->bit); |
| 177 | |
| 178 | return ept; |
| 179 | } |
| 180 | |
| 181 | static unsigned ept_alloc_table = EPT_TX(0) | EPT_RX(0); |
| 182 | |
| 183 | struct udc_endpoint *udc_endpoint_alloc(unsigned type, unsigned maxpkt) |
| 184 | { |
| 185 | struct udc_endpoint *ept; |
| 186 | unsigned n; |
| 187 | unsigned in; |
| 188 | |
| 189 | if (type == UDC_TYPE_BULK_IN) { |
| 190 | in = 1; |
| 191 | } else if (type == UDC_TYPE_BULK_OUT) { |
| 192 | in = 0; |
| 193 | } else { |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | for (n = 1; n < 16; n++) { |
| 198 | unsigned bit = in ? EPT_TX(n) : EPT_RX(n); |
| 199 | if (ept_alloc_table & bit) |
| 200 | continue; |
| 201 | ept = _udc_endpoint_alloc(n, in, maxpkt); |
| 202 | if (ept) |
| 203 | ept_alloc_table |= bit; |
| 204 | return ept; |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | void udc_endpoint_free(struct udc_endpoint *ept) |
| 210 | { |
| 211 | /* todo */ |
| 212 | } |
| 213 | |
| 214 | static void endpoint_enable(struct udc_endpoint *ept, unsigned yes) |
| 215 | { |
| 216 | unsigned n = readl(USB_ENDPTCTRL(ept->num)); |
| 217 | |
| 218 | if(yes) { |
| 219 | if(ept->in) { |
| 220 | n |= (CTRL_TXE | CTRL_TXR | CTRL_TXT_BULK); |
| 221 | } else { |
| 222 | n |= (CTRL_RXE | CTRL_RXR | CTRL_RXT_BULK); |
| 223 | } |
| 224 | |
| 225 | if(ept->num != 0) { |
| 226 | /* XXX should be more dynamic... */ |
| 227 | if(usb_highspeed) { |
| 228 | ept->head->config = CONFIG_MAX_PKT(512) | CONFIG_ZLT; |
| 229 | } else { |
| 230 | ept->head->config = CONFIG_MAX_PKT(64) | CONFIG_ZLT; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | writel(n, USB_ENDPTCTRL(ept->num)); |
| 235 | } |
| 236 | |
| 237 | struct udc_request *udc_request_alloc(void) |
| 238 | { |
| 239 | struct usb_request *req; |
| 240 | req = malloc(sizeof(*req)); |
| 241 | req->req.buf = 0; |
| 242 | req->req.length = 0; |
| 243 | req->item = memalign(32, 32); |
| 244 | return &req->req; |
| 245 | } |
| 246 | |
| 247 | void udc_request_free(struct udc_request *req) |
| 248 | { |
| 249 | free(req); |
| 250 | } |
| 251 | |
| 252 | int udc_request_queue(struct udc_endpoint *ept, struct udc_request *_req) |
| 253 | { |
| 254 | struct usb_request *req = (struct usb_request *) _req; |
| 255 | struct ept_queue_item *item = req->item; |
| 256 | unsigned phys = (unsigned) req->req.buf; |
| 257 | |
| 258 | item->next = TERMINATE; |
| 259 | item->info = INFO_BYTES(req->req.length) | INFO_IOC | INFO_ACTIVE; |
| 260 | item->page0 = phys; |
| 261 | item->page1 = (phys & 0xfffff000) + 0x1000; |
| 262 | |
| 263 | enter_critical_section(); |
| 264 | ept->head->next = (unsigned) item; |
| 265 | ept->head->info = 0; |
| 266 | ept->req = req; |
| 267 | |
| 268 | // arch_clean_invalidate_cache_range(item, 32); |
| 269 | // arch_clean_invalidate_cache_range(ept->head, 64); |
| 270 | // arch_clean_invalidate_cache_range(req->req.buf, req->req.length); |
| 271 | DBG("ept%d %s queue req=%p\n", |
| 272 | ept->num, ept->in ? "in" : "out", req); |
| 273 | |
| 274 | writel(ept->bit, USB_ENDPTPRIME); |
| 275 | exit_critical_section(); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static void handle_ept_complete(struct udc_endpoint *ept) |
| 280 | { |
| 281 | struct ept_queue_item *item; |
| 282 | unsigned actual; |
| 283 | int status; |
| 284 | struct usb_request *req; |
| 285 | |
| 286 | DBG("ept%d %s complete req=%p\n", |
| 287 | ept->num, ept->in ? "in" : "out", ept->req); |
| 288 | |
| 289 | req = ept->req; |
| 290 | if(req) { |
| 291 | ept->req = 0; |
| 292 | |
| 293 | item = req->item; |
| 294 | |
| 295 | /* For some reason we are getting the notification for |
| 296 | * transfer completion before the active bit has cleared. |
| 297 | * HACK: wait for the ACTIVE bit to clear: |
| 298 | */ |
| 299 | while (readl(&(item->info)) & INFO_ACTIVE) ; |
| 300 | |
| 301 | // arch_clean_invalidate_cache_range(item, 32); |
| 302 | // arch_clean_invalidate_cache_range(req->req.buf, req->req.length); |
| 303 | |
| 304 | if(item->info & 0xff) { |
| 305 | actual = 0; |
| 306 | status = -1; |
| 307 | dprintf(INFO, "EP%d/%s FAIL nfo=%x pg0=%x\n", |
| 308 | ept->num, ept->in ? "in" : "out", item->info, item->page0); |
| 309 | } else { |
| 310 | actual = req->req.length - ((item->info >> 16) & 0x7fff); |
| 311 | status = 0; |
| 312 | } |
| 313 | if(req->req.complete) |
| 314 | req->req.complete(&req->req, actual, status); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | static const char *reqname(unsigned r) |
| 319 | { |
| 320 | switch(r) { |
| 321 | case GET_STATUS: return "GET_STATUS"; |
| 322 | case CLEAR_FEATURE: return "CLEAR_FEATURE"; |
| 323 | case SET_FEATURE: return "SET_FEATURE"; |
| 324 | case SET_ADDRESS: return "SET_ADDRESS"; |
| 325 | case GET_DESCRIPTOR: return "GET_DESCRIPTOR"; |
| 326 | case SET_DESCRIPTOR: return "SET_DESCRIPTOR"; |
| 327 | case GET_CONFIGURATION: return "GET_CONFIGURATION"; |
| 328 | case SET_CONFIGURATION: return "SET_CONFIGURATION"; |
| 329 | case GET_INTERFACE: return "GET_INTERFACE"; |
| 330 | case SET_INTERFACE: return "SET_INTERFACE"; |
| 331 | default: return "*UNKNOWN*"; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static struct udc_endpoint *ep0in, *ep0out; |
| 336 | static struct udc_request *ep0req; |
| 337 | |
| 338 | static void setup_ack(void) |
| 339 | { |
| 340 | ep0req->complete = 0; |
| 341 | ep0req->length = 0; |
| 342 | udc_request_queue(ep0in, ep0req); |
| 343 | } |
| 344 | |
| 345 | static void ep0in_complete(struct udc_request *req, unsigned actual, int status) |
| 346 | { |
| 347 | DBG("ep0in_complete %p %d %d\n", req, actual, status); |
| 348 | if(status == 0) { |
| 349 | req->length = 0; |
| 350 | req->complete = 0; |
| 351 | udc_request_queue(ep0out, req); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void setup_tx(void *buf, unsigned len) |
| 356 | { |
| 357 | DBG("setup_tx %p %d\n", buf, len); |
| 358 | memcpy(ep0req->buf, buf, len); |
| 359 | ep0req->complete = ep0in_complete; |
| 360 | ep0req->length = len; |
| 361 | udc_request_queue(ep0in, ep0req); |
| 362 | } |
| 363 | |
| 364 | static unsigned char usb_config_value = 0; |
| 365 | |
| 366 | #define SETUP(type,request) (((type) << 8) | (request)) |
| 367 | |
| 368 | static void handle_setup(struct udc_endpoint *ept) |
| 369 | { |
| 370 | struct setup_packet s; |
| 371 | |
| 372 | memcpy(&s, ept->head->setup_data, sizeof(s)); |
| 373 | writel(ept->bit, USB_ENDPTSETUPSTAT); |
| 374 | |
| 375 | #if 0 |
| 376 | DBG("handle_setup type=0x%02x req=0x%02x val=%d idx=%d len=%d (%s)\n", |
| 377 | s.type, s.request, s.value, s.index, s.length, |
| 378 | reqname(s.request)); |
| 379 | #endif |
| 380 | switch (SETUP(s.type,s.request)) { |
| 381 | case SETUP(DEVICE_READ, GET_STATUS): { |
| 382 | unsigned zero = 0; |
| 383 | if (s.length == 2) { |
| 384 | setup_tx(&zero, 2); |
| 385 | return; |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | case SETUP(DEVICE_READ, GET_DESCRIPTOR): { |
| 390 | struct udc_descriptor *desc; |
| 391 | /* usb_highspeed? */ |
| 392 | for (desc = desc_list; desc; desc = desc->next) { |
| 393 | if (desc->tag == s.value) { |
| 394 | unsigned len = desc->len; |
| 395 | if (len > s.length) len = s.length; |
| 396 | setup_tx(desc->data, len); |
| 397 | return; |
| 398 | } |
| 399 | } |
| 400 | break; |
| 401 | } |
| 402 | case SETUP(DEVICE_READ, GET_CONFIGURATION): |
| 403 | /* disabling this causes data transaction failures on OSX. Why? */ |
| 404 | if ((s.value == 0) && (s.index == 0) && (s.length == 1)) { |
| 405 | setup_tx(&usb_config_value, 1); |
| 406 | return; |
| 407 | } |
| 408 | break; |
| 409 | case SETUP(DEVICE_WRITE, SET_CONFIGURATION): |
| 410 | if (s.value == 1) { |
| 411 | struct udc_endpoint *ept; |
| 412 | /* enable endpoints */ |
| 413 | for (ept = ept_list; ept; ept = ept->next){ |
| 414 | if (ept->num == 0) |
| 415 | continue; |
| 416 | endpoint_enable(ept, s.value); |
| 417 | } |
| 418 | usb_config_value = 1; |
| 419 | the_gadget->notify(the_gadget, UDC_EVENT_ONLINE); |
| 420 | } else { |
| 421 | writel(0, USB_ENDPTCTRL(1)); |
| 422 | usb_config_value = 0; |
| 423 | the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE); |
| 424 | } |
| 425 | setup_ack(); |
| 426 | usb_online = s.value ? 1 : 0; |
| 427 | usb_status(s.value ? 1 : 0, usb_highspeed); |
| 428 | return; |
| 429 | case SETUP(DEVICE_WRITE, SET_ADDRESS): |
| 430 | /* write address delayed (will take effect |
| 431 | ** after the next IN txn) |
| 432 | */ |
| 433 | writel((s.value << 25) | (1 << 24), USB_DEVICEADDR); |
| 434 | setup_ack(); |
| 435 | return; |
| 436 | case SETUP(INTERFACE_WRITE, SET_INTERFACE): |
| 437 | /* if we ack this everything hangs */ |
| 438 | /* per spec, STALL is valid if there is not alt func */ |
| 439 | goto stall; |
| 440 | case SETUP(ENDPOINT_WRITE, CLEAR_FEATURE): { |
| 441 | struct udc_endpoint *ept; |
| 442 | unsigned num = s.index & 15; |
| 443 | unsigned in = !!(s.index & 0x80); |
| 444 | |
| 445 | if ((s.value == 0) && (s.length == 0)) { |
| 446 | DBG("clr feat %d %d\n", num, in); |
| 447 | for (ept = ept_list; ept; ept = ept->next) { |
| 448 | if ((ept->num == num) && (ept->in == in)) { |
| 449 | endpoint_enable(ept, 1); |
| 450 | setup_ack(); |
| 451 | return; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | dprintf(INFO, "STALL %s %d %d %d %d %d\n", |
| 460 | reqname(s.request), |
| 461 | s.type, s.request, s.value, s.index, s.length); |
| 462 | |
| 463 | stall: |
| 464 | writel((1<<16) | (1 << 0), USB_ENDPTCTRL(ept->num)); |
| 465 | } |
| 466 | |
| 467 | unsigned ulpi_read(unsigned reg) |
| 468 | { |
| 469 | /* initiate read operation */ |
| 470 | writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg), |
| 471 | USB_ULPI_VIEWPORT); |
| 472 | |
| 473 | /* wait for completion */ |
| 474 | while(readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ; |
| 475 | |
| 476 | return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT)); |
| 477 | } |
| 478 | |
| 479 | void ulpi_write(unsigned val, unsigned reg) |
| 480 | { |
| 481 | /* initiate write operation */ |
| 482 | writel(ULPI_RUN | ULPI_WRITE | |
| 483 | ULPI_ADDR(reg) | ULPI_DATA(val), |
| 484 | USB_ULPI_VIEWPORT); |
| 485 | |
| 486 | /* wait for completion */ |
| 487 | while(readl(USB_ULPI_VIEWPORT) & ULPI_RUN) ; |
| 488 | } |
| 489 | |
| 490 | void board_usb_init(void); |
| 491 | void board_ulpi_init(void); |
| 492 | |
| 493 | int udc_init(struct udc_device *dev) |
| 494 | { |
| 495 | hsusb_clock_init(); |
| 496 | |
| 497 | epts = memalign(4096, 4096); |
| 498 | |
| 499 | dprintf(INFO, "USB init ept @ %p\n", epts); |
| 500 | memset(epts, 0, 32 * sizeof(struct ept_queue_head)); |
| 501 | |
| 502 | dprintf(INFO, "USB ID %08x\n", readl(USB_ID)); |
| 503 | // board_usb_init(); |
| 504 | |
| 505 | /* select ULPI phy */ |
| 506 | writel(0x81000000, USB_PORTSC); |
| 507 | |
| 508 | /* RESET */ |
| 509 | writel(0x00080002, USB_USBCMD); |
| 510 | |
| 511 | thread_sleep(20); |
| 512 | |
| 513 | // board_ulpi_init(); |
| 514 | |
| 515 | // arch_clean_invalidate_cache_range(epts, 32 * sizeof(struct ept_queue_head)); |
| 516 | writel((unsigned) epts, USB_ENDPOINTLISTADDR); |
| 517 | |
| 518 | /* select DEVICE mode */ |
| 519 | writel(0x02, USB_USBMODE); |
| 520 | |
| 521 | writel(0xffffffff, USB_ENDPTFLUSH); |
| 522 | thread_sleep(20); |
| 523 | |
| 524 | ep0out = _udc_endpoint_alloc(0, 0, 64); |
| 525 | ep0in = _udc_endpoint_alloc(0, 1, 64); |
| 526 | ep0req = udc_request_alloc(); |
| 527 | ep0req->buf = malloc(4096); |
| 528 | |
| 529 | { |
| 530 | /* create and register a language table descriptor */ |
| 531 | /* language 0x0409 is US English */ |
| 532 | struct udc_descriptor *desc = udc_descriptor_alloc(TYPE_STRING, 0, 4); |
| 533 | desc->data[2] = 0x09; |
| 534 | desc->data[3] = 0x04; |
| 535 | udc_descriptor_register(desc); |
| 536 | } |
| 537 | |
| 538 | the_device = dev; |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | enum handler_return udc_interrupt(void *arg) |
| 543 | { |
| 544 | struct udc_endpoint *ept; |
| 545 | unsigned ret = INT_NO_RESCHEDULE; |
| 546 | unsigned n = readl(USB_USBSTS); |
| 547 | writel(n, USB_USBSTS); |
| 548 | |
| 549 | n &= (STS_SLI | STS_URI | STS_PCI | STS_UI | STS_UEI); |
| 550 | |
| 551 | if (n == 0) |
| 552 | return ret; |
| 553 | |
| 554 | if (n & STS_URI) { |
| 555 | writel(readl(USB_ENDPTCOMPLETE), USB_ENDPTCOMPLETE); |
| 556 | writel(readl(USB_ENDPTSETUPSTAT), USB_ENDPTSETUPSTAT); |
| 557 | writel(0xffffffff, USB_ENDPTFLUSH); |
| 558 | writel(0, USB_ENDPTCTRL(1)); |
| 559 | DBG1("-- reset --\n"); |
| 560 | usb_online = 0; |
| 561 | usb_config_value = 0; |
| 562 | the_gadget->notify(the_gadget, UDC_EVENT_OFFLINE); |
| 563 | |
| 564 | /* error out any pending reqs */ |
| 565 | for (ept = ept_list; ept; ept = ept->next) { |
| 566 | /* ensure that ept_complete considers |
| 567 | * this to be an error state |
| 568 | */ |
| 569 | if (ept->req) { |
| 570 | ept->req->item->info = INFO_HALTED; |
| 571 | handle_ept_complete(ept); |
| 572 | } |
| 573 | } |
| 574 | usb_status(0, usb_highspeed); |
| 575 | } |
| 576 | if (n & STS_SLI) { |
| 577 | DBG1("-- suspend --\n"); |
| 578 | } |
| 579 | if (n & STS_PCI) { |
| 580 | DBG1("-- portchange --\n"); |
| 581 | unsigned spd = (readl(USB_PORTSC) >> 26) & 3; |
| 582 | if(spd == 2) { |
| 583 | usb_highspeed = 1; |
| 584 | } else { |
| 585 | usb_highspeed = 0; |
| 586 | } |
| 587 | } |
| 588 | if (n & STS_UEI) { |
| 589 | dprintf(INFO, "<UEI %x>\n", readl(USB_ENDPTCOMPLETE)); |
| 590 | } |
| 591 | #if 0 |
| 592 | DBG("STS: "); |
| 593 | if (n & STS_UEI) DBG("ERROR "); |
| 594 | if (n & STS_SLI) DBG("SUSPEND "); |
| 595 | if (n & STS_URI) DBG("RESET "); |
| 596 | if (n & STS_PCI) DBG("PORTCHANGE "); |
| 597 | if (n & STS_UI) DBG("USB "); |
| 598 | DBG("\n"); |
| 599 | #endif |
| 600 | if ((n & STS_UI) || (n & STS_UEI)) { |
| 601 | n = readl(USB_ENDPTSETUPSTAT); |
| 602 | if (n & EPT_RX(0)) { |
| 603 | handle_setup(ep0out); |
| 604 | ret = INT_RESCHEDULE; |
| 605 | } |
| 606 | |
| 607 | n = readl(USB_ENDPTCOMPLETE); |
| 608 | if (n != 0) { |
| 609 | writel(n, USB_ENDPTCOMPLETE); |
| 610 | } |
| 611 | |
| 612 | for (ept = ept_list; ept; ept = ept->next){ |
| 613 | if (n & ept->bit) { |
| 614 | handle_ept_complete(ept); |
| 615 | ret = INT_RESCHEDULE; |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | int udc_register_gadget(struct udc_gadget *gadget) |
| 623 | { |
| 624 | if (the_gadget) { |
| 625 | dprintf(CRITICAL, "only one gadget supported\n"); |
| 626 | return -1; |
| 627 | } |
| 628 | the_gadget = gadget; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static void udc_ept_desc_fill(struct udc_endpoint *ept, unsigned char *data) |
| 633 | { |
| 634 | data[0] = 7; |
| 635 | data[1] = TYPE_ENDPOINT; |
| 636 | data[2] = ept->num | (ept->in ? 0x80 : 0x00); |
| 637 | data[3] = 0x02; /* bulk -- the only kind we support */ |
| 638 | data[4] = ept->maxpkt; |
| 639 | data[5] = ept->maxpkt >> 8; |
| 640 | data[6] = ept->in ? 0x00 : 0x01; |
| 641 | } |
| 642 | |
| 643 | static unsigned udc_ifc_desc_size(struct udc_gadget *g) |
| 644 | { |
| 645 | return 9 + g->ifc_endpoints * 7; |
| 646 | } |
| 647 | |
| 648 | static void udc_ifc_desc_fill(struct udc_gadget *g, unsigned char *data) |
| 649 | { |
| 650 | unsigned n; |
| 651 | |
| 652 | data[0] = 0x09; |
| 653 | data[1] = TYPE_INTERFACE; |
| 654 | data[2] = 0x00; /* ifc number */ |
| 655 | data[3] = 0x00; /* alt number */ |
| 656 | data[4] = g->ifc_endpoints; |
| 657 | data[5] = g->ifc_class; |
| 658 | data[6] = g->ifc_subclass; |
| 659 | data[7] = g->ifc_protocol; |
| 660 | data[8] = udc_string_desc_alloc(g->ifc_string); |
| 661 | |
| 662 | data += 9; |
| 663 | for (n = 0; n < g->ifc_endpoints; n++) { |
| 664 | udc_ept_desc_fill(g->ept[n], data); |
| 665 | data += 7; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | int udc_start(void) |
| 670 | { |
| 671 | struct udc_descriptor *desc; |
| 672 | unsigned char *data; |
| 673 | unsigned size; |
| 674 | |
| 675 | dprintf(INFO, "udc_start()\n"); |
| 676 | |
| 677 | if (!the_device) { |
| 678 | dprintf(CRITICAL, "udc cannot start before init\n"); |
| 679 | return -1; |
| 680 | } |
| 681 | if (!the_gadget) { |
| 682 | dprintf(CRITICAL, "udc has no gadget registered\n"); |
| 683 | return -1; |
| 684 | } |
| 685 | |
| 686 | /* create our device descriptor */ |
| 687 | desc = udc_descriptor_alloc(TYPE_DEVICE, 0, 18); |
| 688 | data = desc->data; |
| 689 | data[2] = 0x10; /* usb spec rev 2.10 */ |
| 690 | data[3] = 0x02; |
| 691 | data[4] = 0x00; /* class */ |
| 692 | data[5] = 0x00; /* subclass */ |
| 693 | data[6] = 0x00; /* protocol */ |
| 694 | data[7] = 0x40; /* max packet size on ept 0 */ |
| 695 | memcpy(data + 8, &the_device->vendor_id, sizeof(short)); |
| 696 | memcpy(data + 10, &the_device->product_id, sizeof(short)); |
| 697 | memcpy(data + 12, &the_device->version_id, sizeof(short)); |
| 698 | data[14] = udc_string_desc_alloc(the_device->manufacturer); |
| 699 | data[15] = udc_string_desc_alloc(the_device->product); |
| 700 | data[16] = udc_string_desc_alloc(the_device->serialno); |
| 701 | data[17] = 1; /* number of configurations */ |
| 702 | udc_descriptor_register(desc); |
| 703 | |
| 704 | /* create our configuration descriptor */ |
| 705 | size = 9 + udc_ifc_desc_size(the_gadget); |
| 706 | desc = udc_descriptor_alloc(TYPE_CONFIGURATION, 0, size); |
| 707 | data = desc->data; |
| 708 | data[0] = 0x09; |
| 709 | data[2] = size; |
| 710 | data[3] = size >> 8; |
| 711 | data[4] = 0x01; /* number of interfaces */ |
| 712 | data[5] = 0x01; /* configuration value */ |
| 713 | data[6] = 0x00; /* configuration string */ |
| 714 | data[7] = 0x80; /* attributes */ |
| 715 | data[8] = 0x80; /* max power (250ma) -- todo fix this */ |
| 716 | udc_ifc_desc_fill(the_gadget, data + 9); |
| 717 | udc_descriptor_register(desc); |
| 718 | |
| 719 | /* go to RUN mode (D+ pullup enable) */ |
| 720 | writel(0x00080001, USB_USBCMD); |
| 721 | register_int_handler(INT_USB_HS, udc_interrupt, (void*) 0); |
| 722 | unmask_interrupt(INT_USB_HS); |
| 723 | writel(STS_URI | STS_SLI | STS_UI | STS_PCI, USB_USBINTR); |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | int udc_stop(void) |
| 728 | { |
| 729 | writel(0, USB_USBINTR); |
| 730 | mask_interrupt(INT_USB_HS); |
| 731 | |
| 732 | /* disable pullup */ |
| 733 | writel(0x0008000, USB_USBCMD); |
| 734 | thread_sleep(10); |
| 735 | |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | |
| 740 | |