1 /* 2 * Intel PXA25x and IXP4xx on-chip full speed USB device controllers 3 * 4 * Copyright (C) 2002 Intrinsyc, Inc. (Frank Becker) 5 * Copyright (C) 2003 Robert Schwebel, Pengutronix 6 * Copyright (C) 2003 Benedikt Spranger, Pengutronix 7 * Copyright (C) 2003 David Brownell 8 * Copyright (C) 2003 Joshua Wise 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 */ 15 16 /* #define VERBOSE_DEBUG */ 17 18 #include <linux/device.h> 19 #include <linux/gpio.h> 20 #include <linux/module.h> 21 #include <linux/kernel.h> 22 #include <linux/ioport.h> 23 #include <linux/types.h> 24 #include <linux/errno.h> 25 #include <linux/err.h> 26 #include <linux/delay.h> 27 #include <linux/slab.h> 28 #include <linux/timer.h> 29 #include <linux/list.h> 30 #include <linux/interrupt.h> 31 #include <linux/mm.h> 32 #include <linux/platform_data/pxa2xx_udc.h> 33 #include <linux/platform_device.h> 34 #include <linux/dma-mapping.h> 35 #include <linux/irq.h> 36 #include <linux/clk.h> 37 #include <linux/seq_file.h> 38 #include <linux/debugfs.h> 39 #include <linux/io.h> 40 #include <linux/prefetch.h> 41 42 #include <asm/byteorder.h> 43 #include <asm/dma.h> 44 #include <asm/mach-types.h> 45 #include <asm/unaligned.h> 46 47 #include <linux/usb/ch9.h> 48 #include <linux/usb/gadget.h> 49 #include <linux/usb/otg.h> 50 51 /* 52 * This driver is PXA25x only. Grab the right register definitions. 53 */ 54 #ifdef CONFIG_ARCH_PXA 55 #include <mach/pxa25x-udc.h> 56 #include <mach/hardware.h> 57 #endif 58 59 #ifdef CONFIG_ARCH_LUBBOCK 60 #include <mach/lubbock.h> 61 #endif 62 63 /* 64 * This driver handles the USB Device Controller (UDC) in Intel's PXA 25x 65 * series processors. The UDC for the IXP 4xx series is very similar. 66 * There are fifteen endpoints, in addition to ep0. 67 * 68 * Such controller drivers work with a gadget driver. The gadget driver 69 * returns descriptors, implements configuration and data protocols used 70 * by the host to interact with this device, and allocates endpoints to 71 * the different protocol interfaces. The controller driver virtualizes 72 * usb hardware so that the gadget drivers will be more portable. 73 * 74 * This UDC hardware wants to implement a bit too much USB protocol, so 75 * it constrains the sorts of USB configuration change events that work. 76 * The errata for these chips are misleading; some "fixed" bugs from 77 * pxa250 a0/a1 b0/b1/b2 sure act like they're still there. 78 * 79 * Note that the UDC hardware supports DMA (except on IXP) but that's 80 * not used here. IN-DMA (to host) is simple enough, when the data is 81 * suitably aligned (16 bytes) ... the network stack doesn't do that, 82 * other software can. OUT-DMA is buggy in most chip versions, as well 83 * as poorly designed (data toggle not automatic). So this driver won't 84 * bother using DMA. (Mostly-working IN-DMA support was available in 85 * kernels before 2.6.23, but was never enabled or well tested.) 86 */ 87 88 #define DRIVER_VERSION "30-June-2007" 89 #define DRIVER_DESC "PXA 25x USB Device Controller driver" 90 91 92 static const char driver_name [] = "pxa25x_udc"; 93 94 static const char ep0name [] = "ep0"; 95 96 97 #ifdef CONFIG_ARCH_IXP4XX 98 99 /* cpu-specific register addresses are compiled in to this code */ 100 #ifdef CONFIG_ARCH_PXA 101 #error "Can't configure both IXP and PXA" 102 #endif 103 104 /* IXP doesn't yet support <linux/clk.h> */ 105 #define clk_get(dev,name) NULL 106 #define clk_enable(clk) do { } while (0) 107 #define clk_disable(clk) do { } while (0) 108 #define clk_put(clk) do { } while (0) 109 110 #endif 111 112 #include "pxa25x_udc.h" 113 114 115 #ifdef CONFIG_USB_PXA25X_SMALL 116 #define SIZE_STR " (small)" 117 #else 118 #define SIZE_STR "" 119 #endif 120 121 /* --------------------------------------------------------------------------- 122 * endpoint related parts of the api to the usb controller hardware, 123 * used by gadget driver; and the inner talker-to-hardware core. 124 * --------------------------------------------------------------------------- 125 */ 126 127 static void pxa25x_ep_fifo_flush (struct usb_ep *ep); 128 static void nuke (struct pxa25x_ep *, int status); 129 130 /* one GPIO should control a D+ pullup, so host sees this device (or not) */ 131 static void pullup_off(void) 132 { 133 struct pxa2xx_udc_mach_info *mach = the_controller->mach; 134 int off_level = mach->gpio_pullup_inverted; 135 136 if (gpio_is_valid(mach->gpio_pullup)) 137 gpio_set_value(mach->gpio_pullup, off_level); 138 else if (mach->udc_command) 139 mach->udc_command(PXA2XX_UDC_CMD_DISCONNECT); 140 } 141 142 static void pullup_on(void) 143 { 144 struct pxa2xx_udc_mach_info *mach = the_controller->mach; 145 int on_level = !mach->gpio_pullup_inverted; 146 147 if (gpio_is_valid(mach->gpio_pullup)) 148 gpio_set_value(mach->gpio_pullup, on_level); 149 else if (mach->udc_command) 150 mach->udc_command(PXA2XX_UDC_CMD_CONNECT); 151 } 152 153 static void pio_irq_enable(int bEndpointAddress) 154 { 155 bEndpointAddress &= 0xf; 156 if (bEndpointAddress < 8) 157 UICR0 &= ~(1 << bEndpointAddress); 158 else { 159 bEndpointAddress -= 8; 160 UICR1 &= ~(1 << bEndpointAddress); 161 } 162 } 163 164 static void pio_irq_disable(int bEndpointAddress) 165 { 166 bEndpointAddress &= 0xf; 167 if (bEndpointAddress < 8) 168 UICR0 |= 1 << bEndpointAddress; 169 else { 170 bEndpointAddress -= 8; 171 UICR1 |= 1 << bEndpointAddress; 172 } 173 } 174 175 /* The UDCCR reg contains mask and interrupt status bits, 176 * so using '|=' isn't safe as it may ack an interrupt. 177 */ 178 #define UDCCR_MASK_BITS (UDCCR_REM | UDCCR_SRM | UDCCR_UDE) 179 180 static inline void udc_set_mask_UDCCR(int mask) 181 { 182 UDCCR = (UDCCR & UDCCR_MASK_BITS) | (mask & UDCCR_MASK_BITS); 183 } 184 185 static inline void udc_clear_mask_UDCCR(int mask) 186 { 187 UDCCR = (UDCCR & UDCCR_MASK_BITS) & ~(mask & UDCCR_MASK_BITS); 188 } 189 190 static inline void udc_ack_int_UDCCR(int mask) 191 { 192 /* udccr contains the bits we dont want to change */ 193 __u32 udccr = UDCCR & UDCCR_MASK_BITS; 194 195 UDCCR = udccr | (mask & ~UDCCR_MASK_BITS); 196 } 197 198 /* 199 * endpoint enable/disable 200 * 201 * we need to verify the descriptors used to enable endpoints. since pxa25x 202 * endpoint configurations are fixed, and are pretty much always enabled, 203 * there's not a lot to manage here. 204 * 205 * because pxa25x can't selectively initialize bulk (or interrupt) endpoints, 206 * (resetting endpoint halt and toggle), SET_INTERFACE is unusable except 207 * for a single interface (with only the default altsetting) and for gadget 208 * drivers that don't halt endpoints (not reset by set_interface). that also 209 * means that if you use ISO, you must violate the USB spec rule that all 210 * iso endpoints must be in non-default altsettings. 211 */ 212 static int pxa25x_ep_enable (struct usb_ep *_ep, 213 const struct usb_endpoint_descriptor *desc) 214 { 215 struct pxa25x_ep *ep; 216 struct pxa25x_udc *dev; 217 218 ep = container_of (_ep, struct pxa25x_ep, ep); 219 if (!_ep || !desc || _ep->name == ep0name 220 || desc->bDescriptorType != USB_DT_ENDPOINT 221 || ep->bEndpointAddress != desc->bEndpointAddress 222 || ep->fifo_size < usb_endpoint_maxp (desc)) { 223 DMSG("%s, bad ep or descriptor\n", __func__); 224 return -EINVAL; 225 } 226 227 /* xfer types must match, except that interrupt ~= bulk */ 228 if (ep->bmAttributes != desc->bmAttributes 229 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK 230 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) { 231 DMSG("%s, %s type mismatch\n", __func__, _ep->name); 232 return -EINVAL; 233 } 234 235 /* hardware _could_ do smaller, but driver doesn't */ 236 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK 237 && usb_endpoint_maxp (desc) 238 != BULK_FIFO_SIZE) 239 || !desc->wMaxPacketSize) { 240 DMSG("%s, bad %s maxpacket\n", __func__, _ep->name); 241 return -ERANGE; 242 } 243 244 dev = ep->dev; 245 if (!dev->driver || dev->gadget.speed == USB_SPEED_UNKNOWN) { 246 DMSG("%s, bogus device state\n", __func__); 247 return -ESHUTDOWN; 248 } 249 250 ep->ep.desc = desc; 251 ep->stopped = 0; 252 ep->pio_irqs = 0; 253 ep->ep.maxpacket = usb_endpoint_maxp (desc); 254 255 /* flush fifo (mostly for OUT buffers) */ 256 pxa25x_ep_fifo_flush (_ep); 257 258 /* ... reset halt state too, if we could ... */ 259 260 DBG(DBG_VERBOSE, "enabled %s\n", _ep->name); 261 return 0; 262 } 263 264 static int pxa25x_ep_disable (struct usb_ep *_ep) 265 { 266 struct pxa25x_ep *ep; 267 unsigned long flags; 268 269 ep = container_of (_ep, struct pxa25x_ep, ep); 270 if (!_ep || !ep->ep.desc) { 271 DMSG("%s, %s not enabled\n", __func__, 272 _ep ? ep->ep.name : NULL); 273 return -EINVAL; 274 } 275 local_irq_save(flags); 276 277 nuke (ep, -ESHUTDOWN); 278 279 /* flush fifo (mostly for IN buffers) */ 280 pxa25x_ep_fifo_flush (_ep); 281 282 ep->ep.desc = NULL; 283 ep->stopped = 1; 284 285 local_irq_restore(flags); 286 DBG(DBG_VERBOSE, "%s disabled\n", _ep->name); 287 return 0; 288 } 289 290 /*-------------------------------------------------------------------------*/ 291 292 /* for the pxa25x, these can just wrap kmalloc/kfree. gadget drivers 293 * must still pass correctly initialized endpoints, since other controller 294 * drivers may care about how it's currently set up (dma issues etc). 295 */ 296 297 /* 298 * pxa25x_ep_alloc_request - allocate a request data structure 299 */ 300 static struct usb_request * 301 pxa25x_ep_alloc_request (struct usb_ep *_ep, gfp_t gfp_flags) 302 { 303 struct pxa25x_request *req; 304 305 req = kzalloc(sizeof(*req), gfp_flags); 306 if (!req) 307 return NULL; 308 309 INIT_LIST_HEAD (&req->queue); 310 return &req->req; 311 } 312 313 314 /* 315 * pxa25x_ep_free_request - deallocate a request data structure 316 */ 317 static void 318 pxa25x_ep_free_request (struct usb_ep *_ep, struct usb_request *_req) 319 { 320 struct pxa25x_request *req; 321 322 req = container_of (_req, struct pxa25x_request, req); 323 WARN_ON(!list_empty (&req->queue)); 324 kfree(req); 325 } 326 327 /*-------------------------------------------------------------------------*/ 328 329 /* 330 * done - retire a request; caller blocked irqs 331 */ 332 static void done(struct pxa25x_ep *ep, struct pxa25x_request *req, int status) 333 { 334 unsigned stopped = ep->stopped; 335 336 list_del_init(&req->queue); 337 338 if (likely (req->req.status == -EINPROGRESS)) 339 req->req.status = status; 340 else 341 status = req->req.status; 342 343 if (status && status != -ESHUTDOWN) 344 DBG(DBG_VERBOSE, "complete %s req %p stat %d len %u/%u\n", 345 ep->ep.name, &req->req, status, 346 req->req.actual, req->req.length); 347 348 /* don't modify queue heads during completion callback */ 349 ep->stopped = 1; 350 usb_gadget_giveback_request(&ep->ep, &req->req); 351 ep->stopped = stopped; 352 } 353 354 355 static inline void ep0_idle (struct pxa25x_udc *dev) 356 { 357 dev->ep0state = EP0_IDLE; 358 } 359 360 static int 361 write_packet(volatile u32 *uddr, struct pxa25x_request *req, unsigned max) 362 { 363 u8 *buf; 364 unsigned length, count; 365 366 buf = req->req.buf + req->req.actual; 367 prefetch(buf); 368 369 /* how big will this packet be? */ 370 length = min(req->req.length - req->req.actual, max); 371 req->req.actual += length; 372 373 count = length; 374 while (likely(count--)) 375 *uddr = *buf++; 376 377 return length; 378 } 379 380 /* 381 * write to an IN endpoint fifo, as many packets as possible. 382 * irqs will use this to write the rest later. 383 * caller guarantees at least one packet buffer is ready (or a zlp). 384 */ 385 static int 386 write_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req) 387 { 388 unsigned max; 389 390 max = usb_endpoint_maxp(ep->ep.desc); 391 do { 392 unsigned count; 393 int is_last, is_short; 394 395 count = write_packet(ep->reg_uddr, req, max); 396 397 /* last packet is usually short (or a zlp) */ 398 if (unlikely (count != max)) 399 is_last = is_short = 1; 400 else { 401 if (likely(req->req.length != req->req.actual) 402 || req->req.zero) 403 is_last = 0; 404 else 405 is_last = 1; 406 /* interrupt/iso maxpacket may not fill the fifo */ 407 is_short = unlikely (max < ep->fifo_size); 408 } 409 410 DBG(DBG_VERY_NOISY, "wrote %s %d bytes%s%s %d left %p\n", 411 ep->ep.name, count, 412 is_last ? "/L" : "", is_short ? "/S" : "", 413 req->req.length - req->req.actual, req); 414 415 /* let loose that packet. maybe try writing another one, 416 * double buffering might work. TSP, TPC, and TFS 417 * bit values are the same for all normal IN endpoints. 418 */ 419 *ep->reg_udccs = UDCCS_BI_TPC; 420 if (is_short) 421 *ep->reg_udccs = UDCCS_BI_TSP; 422 423 /* requests complete when all IN data is in the FIFO */ 424 if (is_last) { 425 done (ep, req, 0); 426 if (list_empty(&ep->queue)) 427 pio_irq_disable (ep->bEndpointAddress); 428 return 1; 429 } 430 431 // TODO experiment: how robust can fifo mode tweaking be? 432 // double buffering is off in the default fifo mode, which 433 // prevents TFS from being set here. 434 435 } while (*ep->reg_udccs & UDCCS_BI_TFS); 436 return 0; 437 } 438 439 /* caller asserts req->pending (ep0 irq status nyet cleared); starts 440 * ep0 data stage. these chips want very simple state transitions. 441 */ 442 static inline 443 void ep0start(struct pxa25x_udc *dev, u32 flags, const char *tag) 444 { 445 UDCCS0 = flags|UDCCS0_SA|UDCCS0_OPR; 446 USIR0 = USIR0_IR0; 447 dev->req_pending = 0; 448 DBG(DBG_VERY_NOISY, "%s %s, %02x/%02x\n", 449 __func__, tag, UDCCS0, flags); 450 } 451 452 static int 453 write_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req) 454 { 455 unsigned count; 456 int is_short; 457 458 count = write_packet(&UDDR0, req, EP0_FIFO_SIZE); 459 ep->dev->stats.write.bytes += count; 460 461 /* last packet "must be" short (or a zlp) */ 462 is_short = (count != EP0_FIFO_SIZE); 463 464 DBG(DBG_VERY_NOISY, "ep0in %d bytes %d left %p\n", count, 465 req->req.length - req->req.actual, req); 466 467 if (unlikely (is_short)) { 468 if (ep->dev->req_pending) 469 ep0start(ep->dev, UDCCS0_IPR, "short IN"); 470 else 471 UDCCS0 = UDCCS0_IPR; 472 473 count = req->req.length; 474 done (ep, req, 0); 475 ep0_idle(ep->dev); 476 #ifndef CONFIG_ARCH_IXP4XX 477 #if 1 478 /* This seems to get rid of lost status irqs in some cases: 479 * host responds quickly, or next request involves config 480 * change automagic, or should have been hidden, or ... 481 * 482 * FIXME get rid of all udelays possible... 483 */ 484 if (count >= EP0_FIFO_SIZE) { 485 count = 100; 486 do { 487 if ((UDCCS0 & UDCCS0_OPR) != 0) { 488 /* clear OPR, generate ack */ 489 UDCCS0 = UDCCS0_OPR; 490 break; 491 } 492 count--; 493 udelay(1); 494 } while (count); 495 } 496 #endif 497 #endif 498 } else if (ep->dev->req_pending) 499 ep0start(ep->dev, 0, "IN"); 500 return is_short; 501 } 502 503 504 /* 505 * read_fifo - unload packet(s) from the fifo we use for usb OUT 506 * transfers and put them into the request. caller should have made 507 * sure there's at least one packet ready. 508 * 509 * returns true if the request completed because of short packet or the 510 * request buffer having filled (and maybe overran till end-of-packet). 511 */ 512 static int 513 read_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req) 514 { 515 for (;;) { 516 u32 udccs; 517 u8 *buf; 518 unsigned bufferspace, count, is_short; 519 520 /* make sure there's a packet in the FIFO. 521 * UDCCS_{BO,IO}_RPC are all the same bit value. 522 * UDCCS_{BO,IO}_RNE are all the same bit value. 523 */ 524 udccs = *ep->reg_udccs; 525 if (unlikely ((udccs & UDCCS_BO_RPC) == 0)) 526 break; 527 buf = req->req.buf + req->req.actual; 528 prefetchw(buf); 529 bufferspace = req->req.length - req->req.actual; 530 531 /* read all bytes from this packet */ 532 if (likely (udccs & UDCCS_BO_RNE)) { 533 count = 1 + (0x0ff & *ep->reg_ubcr); 534 req->req.actual += min (count, bufferspace); 535 } else /* zlp */ 536 count = 0; 537 is_short = (count < ep->ep.maxpacket); 538 DBG(DBG_VERY_NOISY, "read %s %02x, %d bytes%s req %p %d/%d\n", 539 ep->ep.name, udccs, count, 540 is_short ? "/S" : "", 541 req, req->req.actual, req->req.length); 542 while (likely (count-- != 0)) { 543 u8 byte = (u8) *ep->reg_uddr; 544 545 if (unlikely (bufferspace == 0)) { 546 /* this happens when the driver's buffer 547 * is smaller than what the host sent. 548 * discard the extra data. 549 */ 550 if (req->req.status != -EOVERFLOW) 551 DMSG("%s overflow %d\n", 552 ep->ep.name, count); 553 req->req.status = -EOVERFLOW; 554 } else { 555 *buf++ = byte; 556 bufferspace--; 557 } 558 } 559 *ep->reg_udccs = UDCCS_BO_RPC; 560 /* RPC/RSP/RNE could now reflect the other packet buffer */ 561 562 /* iso is one request per packet */ 563 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) { 564 if (udccs & UDCCS_IO_ROF) 565 req->req.status = -EHOSTUNREACH; 566 /* more like "is_done" */ 567 is_short = 1; 568 } 569 570 /* completion */ 571 if (is_short || req->req.actual == req->req.length) { 572 done (ep, req, 0); 573 if (list_empty(&ep->queue)) 574 pio_irq_disable (ep->bEndpointAddress); 575 return 1; 576 } 577 578 /* finished that packet. the next one may be waiting... */ 579 } 580 return 0; 581 } 582 583 /* 584 * special ep0 version of the above. no UBCR0 or double buffering; status 585 * handshaking is magic. most device protocols don't need control-OUT. 586 * CDC vendor commands (and RNDIS), mass storage CB/CBI, and some other 587 * protocols do use them. 588 */ 589 static int 590 read_ep0_fifo (struct pxa25x_ep *ep, struct pxa25x_request *req) 591 { 592 u8 *buf, byte; 593 unsigned bufferspace; 594 595 buf = req->req.buf + req->req.actual; 596 bufferspace = req->req.length - req->req.actual; 597 598 while (UDCCS0 & UDCCS0_RNE) { 599 byte = (u8) UDDR0; 600 601 if (unlikely (bufferspace == 0)) { 602 /* this happens when the driver's buffer 603 * is smaller than what the host sent. 604 * discard the extra data. 605 */ 606 if (req->req.status != -EOVERFLOW) 607 DMSG("%s overflow\n", ep->ep.name); 608 req->req.status = -EOVERFLOW; 609 } else { 610 *buf++ = byte; 611 req->req.actual++; 612 bufferspace--; 613 } 614 } 615 616 UDCCS0 = UDCCS0_OPR | UDCCS0_IPR; 617 618 /* completion */ 619 if (req->req.actual >= req->req.length) 620 return 1; 621 622 /* finished that packet. the next one may be waiting... */ 623 return 0; 624 } 625 626 /*-------------------------------------------------------------------------*/ 627 628 static int 629 pxa25x_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags) 630 { 631 struct pxa25x_request *req; 632 struct pxa25x_ep *ep; 633 struct pxa25x_udc *dev; 634 unsigned long flags; 635 636 req = container_of(_req, struct pxa25x_request, req); 637 if (unlikely (!_req || !_req->complete || !_req->buf 638 || !list_empty(&req->queue))) { 639 DMSG("%s, bad params\n", __func__); 640 return -EINVAL; 641 } 642 643 ep = container_of(_ep, struct pxa25x_ep, ep); 644 if (unlikely(!_ep || (!ep->ep.desc && ep->ep.name != ep0name))) { 645 DMSG("%s, bad ep\n", __func__); 646 return -EINVAL; 647 } 648 649 dev = ep->dev; 650 if (unlikely (!dev->driver 651 || dev->gadget.speed == USB_SPEED_UNKNOWN)) { 652 DMSG("%s, bogus device state\n", __func__); 653 return -ESHUTDOWN; 654 } 655 656 /* iso is always one packet per request, that's the only way 657 * we can report per-packet status. that also helps with dma. 658 */ 659 if (unlikely (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC 660 && req->req.length > usb_endpoint_maxp(ep->ep.desc))) 661 return -EMSGSIZE; 662 663 DBG(DBG_NOISY, "%s queue req %p, len %d buf %p\n", 664 _ep->name, _req, _req->length, _req->buf); 665 666 local_irq_save(flags); 667 668 _req->status = -EINPROGRESS; 669 _req->actual = 0; 670 671 /* kickstart this i/o queue? */ 672 if (list_empty(&ep->queue) && !ep->stopped) { 673 if (ep->ep.desc == NULL/* ep0 */) { 674 unsigned length = _req->length; 675 676 switch (dev->ep0state) { 677 case EP0_IN_DATA_PHASE: 678 dev->stats.write.ops++; 679 if (write_ep0_fifo(ep, req)) 680 req = NULL; 681 break; 682 683 case EP0_OUT_DATA_PHASE: 684 dev->stats.read.ops++; 685 /* messy ... */ 686 if (dev->req_config) { 687 DBG(DBG_VERBOSE, "ep0 config ack%s\n", 688 dev->has_cfr ? "" : " raced"); 689 if (dev->has_cfr) 690 UDCCFR = UDCCFR_AREN|UDCCFR_ACM 691 |UDCCFR_MB1; 692 done(ep, req, 0); 693 dev->ep0state = EP0_END_XFER; 694 local_irq_restore (flags); 695 return 0; 696 } 697 if (dev->req_pending) 698 ep0start(dev, UDCCS0_IPR, "OUT"); 699 if (length == 0 || ((UDCCS0 & UDCCS0_RNE) != 0 700 && read_ep0_fifo(ep, req))) { 701 ep0_idle(dev); 702 done(ep, req, 0); 703 req = NULL; 704 } 705 break; 706 707 default: 708 DMSG("ep0 i/o, odd state %d\n", dev->ep0state); 709 local_irq_restore (flags); 710 return -EL2HLT; 711 } 712 /* can the FIFO can satisfy the request immediately? */ 713 } else if ((ep->bEndpointAddress & USB_DIR_IN) != 0) { 714 if ((*ep->reg_udccs & UDCCS_BI_TFS) != 0 715 && write_fifo(ep, req)) 716 req = NULL; 717 } else if ((*ep->reg_udccs & UDCCS_BO_RFS) != 0 718 && read_fifo(ep, req)) { 719 req = NULL; 720 } 721 722 if (likely(req && ep->ep.desc)) 723 pio_irq_enable(ep->bEndpointAddress); 724 } 725 726 /* pio or dma irq handler advances the queue. */ 727 if (likely(req != NULL)) 728 list_add_tail(&req->queue, &ep->queue); 729 local_irq_restore(flags); 730 731 return 0; 732 } 733 734 735 /* 736 * nuke - dequeue ALL requests 737 */ 738 static void nuke(struct pxa25x_ep *ep, int status) 739 { 740 struct pxa25x_request *req; 741 742 /* called with irqs blocked */ 743 while (!list_empty(&ep->queue)) { 744 req = list_entry(ep->queue.next, 745 struct pxa25x_request, 746 queue); 747 done(ep, req, status); 748 } 749 if (ep->ep.desc) 750 pio_irq_disable (ep->bEndpointAddress); 751 } 752 753 754 /* dequeue JUST ONE request */ 755 static int pxa25x_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req) 756 { 757 struct pxa25x_ep *ep; 758 struct pxa25x_request *req; 759 unsigned long flags; 760 761 ep = container_of(_ep, struct pxa25x_ep, ep); 762 if (!_ep || ep->ep.name == ep0name) 763 return -EINVAL; 764 765 local_irq_save(flags); 766 767 /* make sure it's actually queued on this endpoint */ 768 list_for_each_entry (req, &ep->queue, queue) { 769 if (&req->req == _req) 770 break; 771 } 772 if (&req->req != _req) { 773 local_irq_restore(flags); 774 return -EINVAL; 775 } 776 777 done(ep, req, -ECONNRESET); 778 779 local_irq_restore(flags); 780 return 0; 781 } 782 783 /*-------------------------------------------------------------------------*/ 784 785 static int pxa25x_ep_set_halt(struct usb_ep *_ep, int value) 786 { 787 struct pxa25x_ep *ep; 788 unsigned long flags; 789 790 ep = container_of(_ep, struct pxa25x_ep, ep); 791 if (unlikely (!_ep 792 || (!ep->ep.desc && ep->ep.name != ep0name)) 793 || ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) { 794 DMSG("%s, bad ep\n", __func__); 795 return -EINVAL; 796 } 797 if (value == 0) { 798 /* this path (reset toggle+halt) is needed to implement 799 * SET_INTERFACE on normal hardware. but it can't be 800 * done from software on the PXA UDC, and the hardware 801 * forgets to do it as part of SET_INTERFACE automagic. 802 */ 803 DMSG("only host can clear %s halt\n", _ep->name); 804 return -EROFS; 805 } 806 807 local_irq_save(flags); 808 809 if ((ep->bEndpointAddress & USB_DIR_IN) != 0 810 && ((*ep->reg_udccs & UDCCS_BI_TFS) == 0 811 || !list_empty(&ep->queue))) { 812 local_irq_restore(flags); 813 return -EAGAIN; 814 } 815 816 /* FST bit is the same for control, bulk in, bulk out, interrupt in */ 817 *ep->reg_udccs = UDCCS_BI_FST|UDCCS_BI_FTF; 818 819 /* ep0 needs special care */ 820 if (!ep->ep.desc) { 821 start_watchdog(ep->dev); 822 ep->dev->req_pending = 0; 823 ep->dev->ep0state = EP0_STALL; 824 825 /* and bulk/intr endpoints like dropping stalls too */ 826 } else { 827 unsigned i; 828 for (i = 0; i < 1000; i += 20) { 829 if (*ep->reg_udccs & UDCCS_BI_SST) 830 break; 831 udelay(20); 832 } 833 } 834 local_irq_restore(flags); 835 836 DBG(DBG_VERBOSE, "%s halt\n", _ep->name); 837 return 0; 838 } 839 840 static int pxa25x_ep_fifo_status(struct usb_ep *_ep) 841 { 842 struct pxa25x_ep *ep; 843 844 ep = container_of(_ep, struct pxa25x_ep, ep); 845 if (!_ep) { 846 DMSG("%s, bad ep\n", __func__); 847 return -ENODEV; 848 } 849 /* pxa can't report unclaimed bytes from IN fifos */ 850 if ((ep->bEndpointAddress & USB_DIR_IN) != 0) 851 return -EOPNOTSUPP; 852 if (ep->dev->gadget.speed == USB_SPEED_UNKNOWN 853 || (*ep->reg_udccs & UDCCS_BO_RFS) == 0) 854 return 0; 855 else 856 return (*ep->reg_ubcr & 0xfff) + 1; 857 } 858 859 static void pxa25x_ep_fifo_flush(struct usb_ep *_ep) 860 { 861 struct pxa25x_ep *ep; 862 863 ep = container_of(_ep, struct pxa25x_ep, ep); 864 if (!_ep || ep->ep.name == ep0name || !list_empty(&ep->queue)) { 865 DMSG("%s, bad ep\n", __func__); 866 return; 867 } 868 869 /* toggle and halt bits stay unchanged */ 870 871 /* for OUT, just read and discard the FIFO contents. */ 872 if ((ep->bEndpointAddress & USB_DIR_IN) == 0) { 873 while (((*ep->reg_udccs) & UDCCS_BO_RNE) != 0) 874 (void) *ep->reg_uddr; 875 return; 876 } 877 878 /* most IN status is the same, but ISO can't stall */ 879 *ep->reg_udccs = UDCCS_BI_TPC|UDCCS_BI_FTF|UDCCS_BI_TUR 880 | (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC 881 ? 0 : UDCCS_BI_SST); 882 } 883 884 885 static struct usb_ep_ops pxa25x_ep_ops = { 886 .enable = pxa25x_ep_enable, 887 .disable = pxa25x_ep_disable, 888 889 .alloc_request = pxa25x_ep_alloc_request, 890 .free_request = pxa25x_ep_free_request, 891 892 .queue = pxa25x_ep_queue, 893 .dequeue = pxa25x_ep_dequeue, 894 895 .set_halt = pxa25x_ep_set_halt, 896 .fifo_status = pxa25x_ep_fifo_status, 897 .fifo_flush = pxa25x_ep_fifo_flush, 898 }; 899 900 901 /* --------------------------------------------------------------------------- 902 * device-scoped parts of the api to the usb controller hardware 903 * --------------------------------------------------------------------------- 904 */ 905 906 static int pxa25x_udc_get_frame(struct usb_gadget *_gadget) 907 { 908 return ((UFNRH & 0x07) << 8) | (UFNRL & 0xff); 909 } 910 911 static int pxa25x_udc_wakeup(struct usb_gadget *_gadget) 912 { 913 /* host may not have enabled remote wakeup */ 914 if ((UDCCS0 & UDCCS0_DRWF) == 0) 915 return -EHOSTUNREACH; 916 udc_set_mask_UDCCR(UDCCR_RSM); 917 return 0; 918 } 919 920 static void stop_activity(struct pxa25x_udc *, struct usb_gadget_driver *); 921 static void udc_enable (struct pxa25x_udc *); 922 static void udc_disable(struct pxa25x_udc *); 923 924 /* We disable the UDC -- and its 48 MHz clock -- whenever it's not 925 * in active use. 926 */ 927 static int pullup(struct pxa25x_udc *udc) 928 { 929 int is_active = udc->vbus && udc->pullup && !udc->suspended; 930 DMSG("%s\n", is_active ? "active" : "inactive"); 931 if (is_active) { 932 if (!udc->active) { 933 udc->active = 1; 934 /* Enable clock for USB device */ 935 clk_enable(udc->clk); 936 udc_enable(udc); 937 } 938 } else { 939 if (udc->active) { 940 if (udc->gadget.speed != USB_SPEED_UNKNOWN) { 941 DMSG("disconnect %s\n", udc->driver 942 ? udc->driver->driver.name 943 : "(no driver)"); 944 stop_activity(udc, udc->driver); 945 } 946 udc_disable(udc); 947 /* Disable clock for USB device */ 948 clk_disable(udc->clk); 949 udc->active = 0; 950 } 951 952 } 953 return 0; 954 } 955 956 /* VBUS reporting logically comes from a transceiver */ 957 static int pxa25x_udc_vbus_session(struct usb_gadget *_gadget, int is_active) 958 { 959 struct pxa25x_udc *udc; 960 961 udc = container_of(_gadget, struct pxa25x_udc, gadget); 962 udc->vbus = is_active; 963 DMSG("vbus %s\n", is_active ? "supplied" : "inactive"); 964 pullup(udc); 965 return 0; 966 } 967 968 /* drivers may have software control over D+ pullup */ 969 static int pxa25x_udc_pullup(struct usb_gadget *_gadget, int is_active) 970 { 971 struct pxa25x_udc *udc; 972 973 udc = container_of(_gadget, struct pxa25x_udc, gadget); 974 975 /* not all boards support pullup control */ 976 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command) 977 return -EOPNOTSUPP; 978 979 udc->pullup = (is_active != 0); 980 pullup(udc); 981 return 0; 982 } 983 984 /* boards may consume current from VBUS, up to 100-500mA based on config. 985 * the 500uA suspend ceiling means that exclusively vbus-powered PXA designs 986 * violate USB specs. 987 */ 988 static int pxa25x_udc_vbus_draw(struct usb_gadget *_gadget, unsigned mA) 989 { 990 struct pxa25x_udc *udc; 991 992 udc = container_of(_gadget, struct pxa25x_udc, gadget); 993 994 if (!IS_ERR_OR_NULL(udc->transceiver)) 995 return usb_phy_set_power(udc->transceiver, mA); 996 return -EOPNOTSUPP; 997 } 998 999 static int pxa25x_udc_start(struct usb_gadget *g, 1000 struct usb_gadget_driver *driver); 1001 static int pxa25x_udc_stop(struct usb_gadget *g); 1002 1003 static const struct usb_gadget_ops pxa25x_udc_ops = { 1004 .get_frame = pxa25x_udc_get_frame, 1005 .wakeup = pxa25x_udc_wakeup, 1006 .vbus_session = pxa25x_udc_vbus_session, 1007 .pullup = pxa25x_udc_pullup, 1008 .vbus_draw = pxa25x_udc_vbus_draw, 1009 .udc_start = pxa25x_udc_start, 1010 .udc_stop = pxa25x_udc_stop, 1011 }; 1012 1013 /*-------------------------------------------------------------------------*/ 1014 1015 #ifdef CONFIG_USB_GADGET_DEBUG_FS 1016 1017 static int 1018 udc_seq_show(struct seq_file *m, void *_d) 1019 { 1020 struct pxa25x_udc *dev = m->private; 1021 unsigned long flags; 1022 int i; 1023 u32 tmp; 1024 1025 local_irq_save(flags); 1026 1027 /* basic device status */ 1028 seq_printf(m, DRIVER_DESC "\n" 1029 "%s version: %s\nGadget driver: %s\nHost %s\n\n", 1030 driver_name, DRIVER_VERSION SIZE_STR "(pio)", 1031 dev->driver ? dev->driver->driver.name : "(none)", 1032 dev->gadget.speed == USB_SPEED_FULL ? "full speed" : "disconnected"); 1033 1034 /* registers for device and ep0 */ 1035 seq_printf(m, 1036 "uicr %02X.%02X, usir %02X.%02x, ufnr %02X.%02X\n", 1037 UICR1, UICR0, USIR1, USIR0, UFNRH, UFNRL); 1038 1039 tmp = UDCCR; 1040 seq_printf(m, 1041 "udccr %02X =%s%s%s%s%s%s%s%s\n", tmp, 1042 (tmp & UDCCR_REM) ? " rem" : "", 1043 (tmp & UDCCR_RSTIR) ? " rstir" : "", 1044 (tmp & UDCCR_SRM) ? " srm" : "", 1045 (tmp & UDCCR_SUSIR) ? " susir" : "", 1046 (tmp & UDCCR_RESIR) ? " resir" : "", 1047 (tmp & UDCCR_RSM) ? " rsm" : "", 1048 (tmp & UDCCR_UDA) ? " uda" : "", 1049 (tmp & UDCCR_UDE) ? " ude" : ""); 1050 1051 tmp = UDCCS0; 1052 seq_printf(m, 1053 "udccs0 %02X =%s%s%s%s%s%s%s%s\n", tmp, 1054 (tmp & UDCCS0_SA) ? " sa" : "", 1055 (tmp & UDCCS0_RNE) ? " rne" : "", 1056 (tmp & UDCCS0_FST) ? " fst" : "", 1057 (tmp & UDCCS0_SST) ? " sst" : "", 1058 (tmp & UDCCS0_DRWF) ? " dwrf" : "", 1059 (tmp & UDCCS0_FTF) ? " ftf" : "", 1060 (tmp & UDCCS0_IPR) ? " ipr" : "", 1061 (tmp & UDCCS0_OPR) ? " opr" : ""); 1062 1063 if (dev->has_cfr) { 1064 tmp = UDCCFR; 1065 seq_printf(m, 1066 "udccfr %02X =%s%s\n", tmp, 1067 (tmp & UDCCFR_AREN) ? " aren" : "", 1068 (tmp & UDCCFR_ACM) ? " acm" : ""); 1069 } 1070 1071 if (dev->gadget.speed != USB_SPEED_FULL || !dev->driver) 1072 goto done; 1073 1074 seq_printf(m, "ep0 IN %lu/%lu, OUT %lu/%lu\nirqs %lu\n\n", 1075 dev->stats.write.bytes, dev->stats.write.ops, 1076 dev->stats.read.bytes, dev->stats.read.ops, 1077 dev->stats.irqs); 1078 1079 /* dump endpoint queues */ 1080 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) { 1081 struct pxa25x_ep *ep = &dev->ep [i]; 1082 struct pxa25x_request *req; 1083 1084 if (i != 0) { 1085 const struct usb_endpoint_descriptor *desc; 1086 1087 desc = ep->ep.desc; 1088 if (!desc) 1089 continue; 1090 tmp = *dev->ep [i].reg_udccs; 1091 seq_printf(m, 1092 "%s max %d %s udccs %02x irqs %lu\n", 1093 ep->ep.name, usb_endpoint_maxp(desc), 1094 "pio", tmp, ep->pio_irqs); 1095 /* TODO translate all five groups of udccs bits! */ 1096 1097 } else /* ep0 should only have one transfer queued */ 1098 seq_printf(m, "ep0 max 16 pio irqs %lu\n", 1099 ep->pio_irqs); 1100 1101 if (list_empty(&ep->queue)) { 1102 seq_printf(m, "\t(nothing queued)\n"); 1103 continue; 1104 } 1105 list_for_each_entry(req, &ep->queue, queue) { 1106 seq_printf(m, 1107 "\treq %p len %d/%d buf %p\n", 1108 &req->req, req->req.actual, 1109 req->req.length, req->req.buf); 1110 } 1111 } 1112 1113 done: 1114 local_irq_restore(flags); 1115 return 0; 1116 } 1117 1118 static int 1119 udc_debugfs_open(struct inode *inode, struct file *file) 1120 { 1121 return single_open(file, udc_seq_show, inode->i_private); 1122 } 1123 1124 static const struct file_operations debug_fops = { 1125 .open = udc_debugfs_open, 1126 .read = seq_read, 1127 .llseek = seq_lseek, 1128 .release = single_release, 1129 .owner = THIS_MODULE, 1130 }; 1131 1132 #define create_debug_files(dev) \ 1133 do { \ 1134 dev->debugfs_udc = debugfs_create_file(dev->gadget.name, \ 1135 S_IRUGO, NULL, dev, &debug_fops); \ 1136 } while (0) 1137 #define remove_debug_files(dev) debugfs_remove(dev->debugfs_udc) 1138 1139 #else /* !CONFIG_USB_GADGET_DEBUG_FILES */ 1140 1141 #define create_debug_files(dev) do {} while (0) 1142 #define remove_debug_files(dev) do {} while (0) 1143 1144 #endif /* CONFIG_USB_GADGET_DEBUG_FILES */ 1145 1146 /*-------------------------------------------------------------------------*/ 1147 1148 /* 1149 * udc_disable - disable USB device controller 1150 */ 1151 static void udc_disable(struct pxa25x_udc *dev) 1152 { 1153 /* block all irqs */ 1154 udc_set_mask_UDCCR(UDCCR_SRM|UDCCR_REM); 1155 UICR0 = UICR1 = 0xff; 1156 UFNRH = UFNRH_SIM; 1157 1158 /* if hardware supports it, disconnect from usb */ 1159 pullup_off(); 1160 1161 udc_clear_mask_UDCCR(UDCCR_UDE); 1162 1163 ep0_idle (dev); 1164 dev->gadget.speed = USB_SPEED_UNKNOWN; 1165 } 1166 1167 1168 /* 1169 * udc_reinit - initialize software state 1170 */ 1171 static void udc_reinit(struct pxa25x_udc *dev) 1172 { 1173 u32 i; 1174 1175 /* device/ep0 records init */ 1176 INIT_LIST_HEAD (&dev->gadget.ep_list); 1177 INIT_LIST_HEAD (&dev->gadget.ep0->ep_list); 1178 dev->ep0state = EP0_IDLE; 1179 1180 /* basic endpoint records init */ 1181 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) { 1182 struct pxa25x_ep *ep = &dev->ep[i]; 1183 1184 if (i != 0) 1185 list_add_tail (&ep->ep.ep_list, &dev->gadget.ep_list); 1186 1187 ep->ep.desc = NULL; 1188 ep->stopped = 0; 1189 INIT_LIST_HEAD (&ep->queue); 1190 ep->pio_irqs = 0; 1191 usb_ep_set_maxpacket_limit(&ep->ep, ep->ep.maxpacket); 1192 } 1193 1194 /* the rest was statically initialized, and is read-only */ 1195 } 1196 1197 /* until it's enabled, this UDC should be completely invisible 1198 * to any USB host. 1199 */ 1200 static void udc_enable (struct pxa25x_udc *dev) 1201 { 1202 udc_clear_mask_UDCCR(UDCCR_UDE); 1203 1204 /* try to clear these bits before we enable the udc */ 1205 udc_ack_int_UDCCR(UDCCR_SUSIR|/*UDCCR_RSTIR|*/UDCCR_RESIR); 1206 1207 ep0_idle(dev); 1208 dev->gadget.speed = USB_SPEED_UNKNOWN; 1209 dev->stats.irqs = 0; 1210 1211 /* 1212 * sequence taken from chapter 12.5.10, PXA250 AppProcDevManual: 1213 * - enable UDC 1214 * - if RESET is already in progress, ack interrupt 1215 * - unmask reset interrupt 1216 */ 1217 udc_set_mask_UDCCR(UDCCR_UDE); 1218 if (!(UDCCR & UDCCR_UDA)) 1219 udc_ack_int_UDCCR(UDCCR_RSTIR); 1220 1221 if (dev->has_cfr /* UDC_RES2 is defined */) { 1222 /* pxa255 (a0+) can avoid a set_config race that could 1223 * prevent gadget drivers from configuring correctly 1224 */ 1225 UDCCFR = UDCCFR_ACM | UDCCFR_MB1; 1226 } else { 1227 /* "USB test mode" for pxa250 errata 40-42 (stepping a0, a1) 1228 * which could result in missing packets and interrupts. 1229 * supposedly one bit per endpoint, controlling whether it 1230 * double buffers or not; ACM/AREN bits fit into the holes. 1231 * zero bits (like USIR0_IRx) disable double buffering. 1232 */ 1233 UDC_RES1 = 0x00; 1234 UDC_RES2 = 0x00; 1235 } 1236 1237 /* enable suspend/resume and reset irqs */ 1238 udc_clear_mask_UDCCR(UDCCR_SRM | UDCCR_REM); 1239 1240 /* enable ep0 irqs */ 1241 UICR0 &= ~UICR0_IM0; 1242 1243 /* if hardware supports it, pullup D+ and wait for reset */ 1244 pullup_on(); 1245 } 1246 1247 1248 /* when a driver is successfully registered, it will receive 1249 * control requests including set_configuration(), which enables 1250 * non-control requests. then usb traffic follows until a 1251 * disconnect is reported. then a host may connect again, or 1252 * the driver might get unbound. 1253 */ 1254 static int pxa25x_udc_start(struct usb_gadget *g, 1255 struct usb_gadget_driver *driver) 1256 { 1257 struct pxa25x_udc *dev = to_pxa25x(g); 1258 int retval; 1259 1260 /* first hook up the driver ... */ 1261 dev->driver = driver; 1262 dev->pullup = 1; 1263 1264 /* ... then enable host detection and ep0; and we're ready 1265 * for set_configuration as well as eventual disconnect. 1266 */ 1267 /* connect to bus through transceiver */ 1268 if (!IS_ERR_OR_NULL(dev->transceiver)) { 1269 retval = otg_set_peripheral(dev->transceiver->otg, 1270 &dev->gadget); 1271 if (retval) 1272 goto bind_fail; 1273 } 1274 1275 pullup(dev); 1276 dump_state(dev); 1277 return 0; 1278 bind_fail: 1279 return retval; 1280 } 1281 1282 static void 1283 reset_gadget(struct pxa25x_udc *dev, struct usb_gadget_driver *driver) 1284 { 1285 int i; 1286 1287 /* don't disconnect drivers more than once */ 1288 if (dev->gadget.speed == USB_SPEED_UNKNOWN) 1289 driver = NULL; 1290 dev->gadget.speed = USB_SPEED_UNKNOWN; 1291 1292 /* prevent new request submissions, kill any outstanding requests */ 1293 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) { 1294 struct pxa25x_ep *ep = &dev->ep[i]; 1295 1296 ep->stopped = 1; 1297 nuke(ep, -ESHUTDOWN); 1298 } 1299 del_timer_sync(&dev->timer); 1300 1301 /* report reset; the driver is already quiesced */ 1302 if (driver) 1303 usb_gadget_udc_reset(&dev->gadget, driver); 1304 1305 /* re-init driver-visible data structures */ 1306 udc_reinit(dev); 1307 } 1308 1309 static void 1310 stop_activity(struct pxa25x_udc *dev, struct usb_gadget_driver *driver) 1311 { 1312 int i; 1313 1314 /* don't disconnect drivers more than once */ 1315 if (dev->gadget.speed == USB_SPEED_UNKNOWN) 1316 driver = NULL; 1317 dev->gadget.speed = USB_SPEED_UNKNOWN; 1318 1319 /* prevent new request submissions, kill any outstanding requests */ 1320 for (i = 0; i < PXA_UDC_NUM_ENDPOINTS; i++) { 1321 struct pxa25x_ep *ep = &dev->ep[i]; 1322 1323 ep->stopped = 1; 1324 nuke(ep, -ESHUTDOWN); 1325 } 1326 del_timer_sync(&dev->timer); 1327 1328 /* report disconnect; the driver is already quiesced */ 1329 if (driver) 1330 driver->disconnect(&dev->gadget); 1331 1332 /* re-init driver-visible data structures */ 1333 udc_reinit(dev); 1334 } 1335 1336 static int pxa25x_udc_stop(struct usb_gadget*g) 1337 { 1338 struct pxa25x_udc *dev = to_pxa25x(g); 1339 1340 local_irq_disable(); 1341 dev->pullup = 0; 1342 pullup(dev); 1343 stop_activity(dev, NULL); 1344 local_irq_enable(); 1345 1346 if (!IS_ERR_OR_NULL(dev->transceiver)) 1347 (void) otg_set_peripheral(dev->transceiver->otg, NULL); 1348 1349 dev->driver = NULL; 1350 1351 dump_state(dev); 1352 1353 return 0; 1354 } 1355 1356 /*-------------------------------------------------------------------------*/ 1357 1358 #ifdef CONFIG_ARCH_LUBBOCK 1359 1360 /* Lubbock has separate connect and disconnect irqs. More typical designs 1361 * use one GPIO as the VBUS IRQ, and another to control the D+ pullup. 1362 */ 1363 1364 static irqreturn_t 1365 lubbock_vbus_irq(int irq, void *_dev) 1366 { 1367 struct pxa25x_udc *dev = _dev; 1368 int vbus; 1369 1370 dev->stats.irqs++; 1371 switch (irq) { 1372 case LUBBOCK_USB_IRQ: 1373 vbus = 1; 1374 disable_irq(LUBBOCK_USB_IRQ); 1375 enable_irq(LUBBOCK_USB_DISC_IRQ); 1376 break; 1377 case LUBBOCK_USB_DISC_IRQ: 1378 vbus = 0; 1379 disable_irq(LUBBOCK_USB_DISC_IRQ); 1380 enable_irq(LUBBOCK_USB_IRQ); 1381 break; 1382 default: 1383 return IRQ_NONE; 1384 } 1385 1386 pxa25x_udc_vbus_session(&dev->gadget, vbus); 1387 return IRQ_HANDLED; 1388 } 1389 1390 #endif 1391 1392 1393 /*-------------------------------------------------------------------------*/ 1394 1395 static inline void clear_ep_state (struct pxa25x_udc *dev) 1396 { 1397 unsigned i; 1398 1399 /* hardware SET_{CONFIGURATION,INTERFACE} automagic resets endpoint 1400 * fifos, and pending transactions mustn't be continued in any case. 1401 */ 1402 for (i = 1; i < PXA_UDC_NUM_ENDPOINTS; i++) 1403 nuke(&dev->ep[i], -ECONNABORTED); 1404 } 1405 1406 static void udc_watchdog(unsigned long _dev) 1407 { 1408 struct pxa25x_udc *dev = (void *)_dev; 1409 1410 local_irq_disable(); 1411 if (dev->ep0state == EP0_STALL 1412 && (UDCCS0 & UDCCS0_FST) == 0 1413 && (UDCCS0 & UDCCS0_SST) == 0) { 1414 UDCCS0 = UDCCS0_FST|UDCCS0_FTF; 1415 DBG(DBG_VERBOSE, "ep0 re-stall\n"); 1416 start_watchdog(dev); 1417 } 1418 local_irq_enable(); 1419 } 1420 1421 static void handle_ep0 (struct pxa25x_udc *dev) 1422 { 1423 u32 udccs0 = UDCCS0; 1424 struct pxa25x_ep *ep = &dev->ep [0]; 1425 struct pxa25x_request *req; 1426 union { 1427 struct usb_ctrlrequest r; 1428 u8 raw [8]; 1429 u32 word [2]; 1430 } u; 1431 1432 if (list_empty(&ep->queue)) 1433 req = NULL; 1434 else 1435 req = list_entry(ep->queue.next, struct pxa25x_request, queue); 1436 1437 /* clear stall status */ 1438 if (udccs0 & UDCCS0_SST) { 1439 nuke(ep, -EPIPE); 1440 UDCCS0 = UDCCS0_SST; 1441 del_timer(&dev->timer); 1442 ep0_idle(dev); 1443 } 1444 1445 /* previous request unfinished? non-error iff back-to-back ... */ 1446 if ((udccs0 & UDCCS0_SA) != 0 && dev->ep0state != EP0_IDLE) { 1447 nuke(ep, 0); 1448 del_timer(&dev->timer); 1449 ep0_idle(dev); 1450 } 1451 1452 switch (dev->ep0state) { 1453 case EP0_IDLE: 1454 /* late-breaking status? */ 1455 udccs0 = UDCCS0; 1456 1457 /* start control request? */ 1458 if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE)) 1459 == (UDCCS0_OPR|UDCCS0_SA|UDCCS0_RNE))) { 1460 int i; 1461 1462 nuke (ep, -EPROTO); 1463 1464 /* read SETUP packet */ 1465 for (i = 0; i < 8; i++) { 1466 if (unlikely(!(UDCCS0 & UDCCS0_RNE))) { 1467 bad_setup: 1468 DMSG("SETUP %d!\n", i); 1469 goto stall; 1470 } 1471 u.raw [i] = (u8) UDDR0; 1472 } 1473 if (unlikely((UDCCS0 & UDCCS0_RNE) != 0)) 1474 goto bad_setup; 1475 1476 got_setup: 1477 DBG(DBG_VERBOSE, "SETUP %02x.%02x v%04x i%04x l%04x\n", 1478 u.r.bRequestType, u.r.bRequest, 1479 le16_to_cpu(u.r.wValue), 1480 le16_to_cpu(u.r.wIndex), 1481 le16_to_cpu(u.r.wLength)); 1482 1483 /* cope with automagic for some standard requests. */ 1484 dev->req_std = (u.r.bRequestType & USB_TYPE_MASK) 1485 == USB_TYPE_STANDARD; 1486 dev->req_config = 0; 1487 dev->req_pending = 1; 1488 switch (u.r.bRequest) { 1489 /* hardware restricts gadget drivers here! */ 1490 case USB_REQ_SET_CONFIGURATION: 1491 if (u.r.bRequestType == USB_RECIP_DEVICE) { 1492 /* reflect hardware's automagic 1493 * up to the gadget driver. 1494 */ 1495 config_change: 1496 dev->req_config = 1; 1497 clear_ep_state(dev); 1498 /* if !has_cfr, there's no synch 1499 * else use AREN (later) not SA|OPR 1500 * USIR0_IR0 acts edge sensitive 1501 */ 1502 } 1503 break; 1504 /* ... and here, even more ... */ 1505 case USB_REQ_SET_INTERFACE: 1506 if (u.r.bRequestType == USB_RECIP_INTERFACE) { 1507 /* udc hardware is broken by design: 1508 * - altsetting may only be zero; 1509 * - hw resets all interfaces' eps; 1510 * - ep reset doesn't include halt(?). 1511 */ 1512 DMSG("broken set_interface (%d/%d)\n", 1513 le16_to_cpu(u.r.wIndex), 1514 le16_to_cpu(u.r.wValue)); 1515 goto config_change; 1516 } 1517 break; 1518 /* hardware was supposed to hide this */ 1519 case USB_REQ_SET_ADDRESS: 1520 if (u.r.bRequestType == USB_RECIP_DEVICE) { 1521 ep0start(dev, 0, "address"); 1522 return; 1523 } 1524 break; 1525 } 1526 1527 if (u.r.bRequestType & USB_DIR_IN) 1528 dev->ep0state = EP0_IN_DATA_PHASE; 1529 else 1530 dev->ep0state = EP0_OUT_DATA_PHASE; 1531 1532 i = dev->driver->setup(&dev->gadget, &u.r); 1533 if (i < 0) { 1534 /* hardware automagic preventing STALL... */ 1535 if (dev->req_config) { 1536 /* hardware sometimes neglects to tell 1537 * tell us about config change events, 1538 * so later ones may fail... 1539 */ 1540 WARNING("config change %02x fail %d?\n", 1541 u.r.bRequest, i); 1542 return; 1543 /* TODO experiment: if has_cfr, 1544 * hardware didn't ACK; maybe we 1545 * could actually STALL! 1546 */ 1547 } 1548 DBG(DBG_VERBOSE, "protocol STALL, " 1549 "%02x err %d\n", UDCCS0, i); 1550 stall: 1551 /* the watchdog timer helps deal with cases 1552 * where udc seems to clear FST wrongly, and 1553 * then NAKs instead of STALLing. 1554 */ 1555 ep0start(dev, UDCCS0_FST|UDCCS0_FTF, "stall"); 1556 start_watchdog(dev); 1557 dev->ep0state = EP0_STALL; 1558 1559 /* deferred i/o == no response yet */ 1560 } else if (dev->req_pending) { 1561 if (likely(dev->ep0state == EP0_IN_DATA_PHASE 1562 || dev->req_std || u.r.wLength)) 1563 ep0start(dev, 0, "defer"); 1564 else 1565 ep0start(dev, UDCCS0_IPR, "defer/IPR"); 1566 } 1567 1568 /* expect at least one data or status stage irq */ 1569 return; 1570 1571 } else if (likely((udccs0 & (UDCCS0_OPR|UDCCS0_SA)) 1572 == (UDCCS0_OPR|UDCCS0_SA))) { 1573 unsigned i; 1574 1575 /* pxa210/250 erratum 131 for B0/B1 says RNE lies. 1576 * still observed on a pxa255 a0. 1577 */ 1578 DBG(DBG_VERBOSE, "e131\n"); 1579 nuke(ep, -EPROTO); 1580 1581 /* read SETUP data, but don't trust it too much */ 1582 for (i = 0; i < 8; i++) 1583 u.raw [i] = (u8) UDDR0; 1584 if ((u.r.bRequestType & USB_RECIP_MASK) 1585 > USB_RECIP_OTHER) 1586 goto stall; 1587 if (u.word [0] == 0 && u.word [1] == 0) 1588 goto stall; 1589 goto got_setup; 1590 } else { 1591 /* some random early IRQ: 1592 * - we acked FST 1593 * - IPR cleared 1594 * - OPR got set, without SA (likely status stage) 1595 */ 1596 UDCCS0 = udccs0 & (UDCCS0_SA|UDCCS0_OPR); 1597 } 1598 break; 1599 case EP0_IN_DATA_PHASE: /* GET_DESCRIPTOR etc */ 1600 if (udccs0 & UDCCS0_OPR) { 1601 UDCCS0 = UDCCS0_OPR|UDCCS0_FTF; 1602 DBG(DBG_VERBOSE, "ep0in premature status\n"); 1603 if (req) 1604 done(ep, req, 0); 1605 ep0_idle(dev); 1606 } else /* irq was IPR clearing */ { 1607 if (req) { 1608 /* this IN packet might finish the request */ 1609 (void) write_ep0_fifo(ep, req); 1610 } /* else IN token before response was written */ 1611 } 1612 break; 1613 case EP0_OUT_DATA_PHASE: /* SET_DESCRIPTOR etc */ 1614 if (udccs0 & UDCCS0_OPR) { 1615 if (req) { 1616 /* this OUT packet might finish the request */ 1617 if (read_ep0_fifo(ep, req)) 1618 done(ep, req, 0); 1619 /* else more OUT packets expected */ 1620 } /* else OUT token before read was issued */ 1621 } else /* irq was IPR clearing */ { 1622 DBG(DBG_VERBOSE, "ep0out premature status\n"); 1623 if (req) 1624 done(ep, req, 0); 1625 ep0_idle(dev); 1626 } 1627 break; 1628 case EP0_END_XFER: 1629 if (req) 1630 done(ep, req, 0); 1631 /* ack control-IN status (maybe in-zlp was skipped) 1632 * also appears after some config change events. 1633 */ 1634 if (udccs0 & UDCCS0_OPR) 1635 UDCCS0 = UDCCS0_OPR; 1636 ep0_idle(dev); 1637 break; 1638 case EP0_STALL: 1639 UDCCS0 = UDCCS0_FST; 1640 break; 1641 } 1642 USIR0 = USIR0_IR0; 1643 } 1644 1645 static void handle_ep(struct pxa25x_ep *ep) 1646 { 1647 struct pxa25x_request *req; 1648 int is_in = ep->bEndpointAddress & USB_DIR_IN; 1649 int completed; 1650 u32 udccs, tmp; 1651 1652 do { 1653 completed = 0; 1654 if (likely (!list_empty(&ep->queue))) 1655 req = list_entry(ep->queue.next, 1656 struct pxa25x_request, queue); 1657 else 1658 req = NULL; 1659 1660 // TODO check FST handling 1661 1662 udccs = *ep->reg_udccs; 1663 if (unlikely(is_in)) { /* irq from TPC, SST, or (ISO) TUR */ 1664 tmp = UDCCS_BI_TUR; 1665 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK)) 1666 tmp |= UDCCS_BI_SST; 1667 tmp &= udccs; 1668 if (likely (tmp)) 1669 *ep->reg_udccs = tmp; 1670 if (req && likely ((udccs & UDCCS_BI_TFS) != 0)) 1671 completed = write_fifo(ep, req); 1672 1673 } else { /* irq from RPC (or for ISO, ROF) */ 1674 if (likely(ep->bmAttributes == USB_ENDPOINT_XFER_BULK)) 1675 tmp = UDCCS_BO_SST | UDCCS_BO_DME; 1676 else 1677 tmp = UDCCS_IO_ROF | UDCCS_IO_DME; 1678 tmp &= udccs; 1679 if (likely(tmp)) 1680 *ep->reg_udccs = tmp; 1681 1682 /* fifos can hold packets, ready for reading... */ 1683 if (likely(req)) { 1684 completed = read_fifo(ep, req); 1685 } else 1686 pio_irq_disable (ep->bEndpointAddress); 1687 } 1688 ep->pio_irqs++; 1689 } while (completed); 1690 } 1691 1692 /* 1693 * pxa25x_udc_irq - interrupt handler 1694 * 1695 * avoid delays in ep0 processing. the control handshaking isn't always 1696 * under software control (pxa250c0 and the pxa255 are better), and delays 1697 * could cause usb protocol errors. 1698 */ 1699 static irqreturn_t 1700 pxa25x_udc_irq(int irq, void *_dev) 1701 { 1702 struct pxa25x_udc *dev = _dev; 1703 int handled; 1704 1705 dev->stats.irqs++; 1706 do { 1707 u32 udccr = UDCCR; 1708 1709 handled = 0; 1710 1711 /* SUSpend Interrupt Request */ 1712 if (unlikely(udccr & UDCCR_SUSIR)) { 1713 udc_ack_int_UDCCR(UDCCR_SUSIR); 1714 handled = 1; 1715 DBG(DBG_VERBOSE, "USB suspend\n"); 1716 1717 if (dev->gadget.speed != USB_SPEED_UNKNOWN 1718 && dev->driver 1719 && dev->driver->suspend) 1720 dev->driver->suspend(&dev->gadget); 1721 ep0_idle (dev); 1722 } 1723 1724 /* RESume Interrupt Request */ 1725 if (unlikely(udccr & UDCCR_RESIR)) { 1726 udc_ack_int_UDCCR(UDCCR_RESIR); 1727 handled = 1; 1728 DBG(DBG_VERBOSE, "USB resume\n"); 1729 1730 if (dev->gadget.speed != USB_SPEED_UNKNOWN 1731 && dev->driver 1732 && dev->driver->resume) 1733 dev->driver->resume(&dev->gadget); 1734 } 1735 1736 /* ReSeT Interrupt Request - USB reset */ 1737 if (unlikely(udccr & UDCCR_RSTIR)) { 1738 udc_ack_int_UDCCR(UDCCR_RSTIR); 1739 handled = 1; 1740 1741 if ((UDCCR & UDCCR_UDA) == 0) { 1742 DBG(DBG_VERBOSE, "USB reset start\n"); 1743 1744 /* reset driver and endpoints, 1745 * in case that's not yet done 1746 */ 1747 reset_gadget(dev, dev->driver); 1748 1749 } else { 1750 DBG(DBG_VERBOSE, "USB reset end\n"); 1751 dev->gadget.speed = USB_SPEED_FULL; 1752 memset(&dev->stats, 0, sizeof dev->stats); 1753 /* driver and endpoints are still reset */ 1754 } 1755 1756 } else { 1757 u32 usir0 = USIR0 & ~UICR0; 1758 u32 usir1 = USIR1 & ~UICR1; 1759 int i; 1760 1761 if (unlikely (!usir0 && !usir1)) 1762 continue; 1763 1764 DBG(DBG_VERY_NOISY, "irq %02x.%02x\n", usir1, usir0); 1765 1766 /* control traffic */ 1767 if (usir0 & USIR0_IR0) { 1768 dev->ep[0].pio_irqs++; 1769 handle_ep0(dev); 1770 handled = 1; 1771 } 1772 1773 /* endpoint data transfers */ 1774 for (i = 0; i < 8; i++) { 1775 u32 tmp = 1 << i; 1776 1777 if (i && (usir0 & tmp)) { 1778 handle_ep(&dev->ep[i]); 1779 USIR0 |= tmp; 1780 handled = 1; 1781 } 1782 #ifndef CONFIG_USB_PXA25X_SMALL 1783 if (usir1 & tmp) { 1784 handle_ep(&dev->ep[i+8]); 1785 USIR1 |= tmp; 1786 handled = 1; 1787 } 1788 #endif 1789 } 1790 } 1791 1792 /* we could also ask for 1 msec SOF (SIR) interrupts */ 1793 1794 } while (handled); 1795 return IRQ_HANDLED; 1796 } 1797 1798 /*-------------------------------------------------------------------------*/ 1799 1800 static void nop_release (struct device *dev) 1801 { 1802 DMSG("%s %s\n", __func__, dev_name(dev)); 1803 } 1804 1805 /* this uses load-time allocation and initialization (instead of 1806 * doing it at run-time) to save code, eliminate fault paths, and 1807 * be more obviously correct. 1808 */ 1809 static struct pxa25x_udc memory = { 1810 .gadget = { 1811 .ops = &pxa25x_udc_ops, 1812 .ep0 = &memory.ep[0].ep, 1813 .name = driver_name, 1814 .dev = { 1815 .init_name = "gadget", 1816 .release = nop_release, 1817 }, 1818 }, 1819 1820 /* control endpoint */ 1821 .ep[0] = { 1822 .ep = { 1823 .name = ep0name, 1824 .ops = &pxa25x_ep_ops, 1825 .maxpacket = EP0_FIFO_SIZE, 1826 }, 1827 .dev = &memory, 1828 .reg_udccs = &UDCCS0, 1829 .reg_uddr = &UDDR0, 1830 }, 1831 1832 /* first group of endpoints */ 1833 .ep[1] = { 1834 .ep = { 1835 .name = "ep1in-bulk", 1836 .ops = &pxa25x_ep_ops, 1837 .maxpacket = BULK_FIFO_SIZE, 1838 }, 1839 .dev = &memory, 1840 .fifo_size = BULK_FIFO_SIZE, 1841 .bEndpointAddress = USB_DIR_IN | 1, 1842 .bmAttributes = USB_ENDPOINT_XFER_BULK, 1843 .reg_udccs = &UDCCS1, 1844 .reg_uddr = &UDDR1, 1845 }, 1846 .ep[2] = { 1847 .ep = { 1848 .name = "ep2out-bulk", 1849 .ops = &pxa25x_ep_ops, 1850 .maxpacket = BULK_FIFO_SIZE, 1851 }, 1852 .dev = &memory, 1853 .fifo_size = BULK_FIFO_SIZE, 1854 .bEndpointAddress = 2, 1855 .bmAttributes = USB_ENDPOINT_XFER_BULK, 1856 .reg_udccs = &UDCCS2, 1857 .reg_ubcr = &UBCR2, 1858 .reg_uddr = &UDDR2, 1859 }, 1860 #ifndef CONFIG_USB_PXA25X_SMALL 1861 .ep[3] = { 1862 .ep = { 1863 .name = "ep3in-iso", 1864 .ops = &pxa25x_ep_ops, 1865 .maxpacket = ISO_FIFO_SIZE, 1866 }, 1867 .dev = &memory, 1868 .fifo_size = ISO_FIFO_SIZE, 1869 .bEndpointAddress = USB_DIR_IN | 3, 1870 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 1871 .reg_udccs = &UDCCS3, 1872 .reg_uddr = &UDDR3, 1873 }, 1874 .ep[4] = { 1875 .ep = { 1876 .name = "ep4out-iso", 1877 .ops = &pxa25x_ep_ops, 1878 .maxpacket = ISO_FIFO_SIZE, 1879 }, 1880 .dev = &memory, 1881 .fifo_size = ISO_FIFO_SIZE, 1882 .bEndpointAddress = 4, 1883 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 1884 .reg_udccs = &UDCCS4, 1885 .reg_ubcr = &UBCR4, 1886 .reg_uddr = &UDDR4, 1887 }, 1888 .ep[5] = { 1889 .ep = { 1890 .name = "ep5in-int", 1891 .ops = &pxa25x_ep_ops, 1892 .maxpacket = INT_FIFO_SIZE, 1893 }, 1894 .dev = &memory, 1895 .fifo_size = INT_FIFO_SIZE, 1896 .bEndpointAddress = USB_DIR_IN | 5, 1897 .bmAttributes = USB_ENDPOINT_XFER_INT, 1898 .reg_udccs = &UDCCS5, 1899 .reg_uddr = &UDDR5, 1900 }, 1901 1902 /* second group of endpoints */ 1903 .ep[6] = { 1904 .ep = { 1905 .name = "ep6in-bulk", 1906 .ops = &pxa25x_ep_ops, 1907 .maxpacket = BULK_FIFO_SIZE, 1908 }, 1909 .dev = &memory, 1910 .fifo_size = BULK_FIFO_SIZE, 1911 .bEndpointAddress = USB_DIR_IN | 6, 1912 .bmAttributes = USB_ENDPOINT_XFER_BULK, 1913 .reg_udccs = &UDCCS6, 1914 .reg_uddr = &UDDR6, 1915 }, 1916 .ep[7] = { 1917 .ep = { 1918 .name = "ep7out-bulk", 1919 .ops = &pxa25x_ep_ops, 1920 .maxpacket = BULK_FIFO_SIZE, 1921 }, 1922 .dev = &memory, 1923 .fifo_size = BULK_FIFO_SIZE, 1924 .bEndpointAddress = 7, 1925 .bmAttributes = USB_ENDPOINT_XFER_BULK, 1926 .reg_udccs = &UDCCS7, 1927 .reg_ubcr = &UBCR7, 1928 .reg_uddr = &UDDR7, 1929 }, 1930 .ep[8] = { 1931 .ep = { 1932 .name = "ep8in-iso", 1933 .ops = &pxa25x_ep_ops, 1934 .maxpacket = ISO_FIFO_SIZE, 1935 }, 1936 .dev = &memory, 1937 .fifo_size = ISO_FIFO_SIZE, 1938 .bEndpointAddress = USB_DIR_IN | 8, 1939 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 1940 .reg_udccs = &UDCCS8, 1941 .reg_uddr = &UDDR8, 1942 }, 1943 .ep[9] = { 1944 .ep = { 1945 .name = "ep9out-iso", 1946 .ops = &pxa25x_ep_ops, 1947 .maxpacket = ISO_FIFO_SIZE, 1948 }, 1949 .dev = &memory, 1950 .fifo_size = ISO_FIFO_SIZE, 1951 .bEndpointAddress = 9, 1952 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 1953 .reg_udccs = &UDCCS9, 1954 .reg_ubcr = &UBCR9, 1955 .reg_uddr = &UDDR9, 1956 }, 1957 .ep[10] = { 1958 .ep = { 1959 .name = "ep10in-int", 1960 .ops = &pxa25x_ep_ops, 1961 .maxpacket = INT_FIFO_SIZE, 1962 }, 1963 .dev = &memory, 1964 .fifo_size = INT_FIFO_SIZE, 1965 .bEndpointAddress = USB_DIR_IN | 10, 1966 .bmAttributes = USB_ENDPOINT_XFER_INT, 1967 .reg_udccs = &UDCCS10, 1968 .reg_uddr = &UDDR10, 1969 }, 1970 1971 /* third group of endpoints */ 1972 .ep[11] = { 1973 .ep = { 1974 .name = "ep11in-bulk", 1975 .ops = &pxa25x_ep_ops, 1976 .maxpacket = BULK_FIFO_SIZE, 1977 }, 1978 .dev = &memory, 1979 .fifo_size = BULK_FIFO_SIZE, 1980 .bEndpointAddress = USB_DIR_IN | 11, 1981 .bmAttributes = USB_ENDPOINT_XFER_BULK, 1982 .reg_udccs = &UDCCS11, 1983 .reg_uddr = &UDDR11, 1984 }, 1985 .ep[12] = { 1986 .ep = { 1987 .name = "ep12out-bulk", 1988 .ops = &pxa25x_ep_ops, 1989 .maxpacket = BULK_FIFO_SIZE, 1990 }, 1991 .dev = &memory, 1992 .fifo_size = BULK_FIFO_SIZE, 1993 .bEndpointAddress = 12, 1994 .bmAttributes = USB_ENDPOINT_XFER_BULK, 1995 .reg_udccs = &UDCCS12, 1996 .reg_ubcr = &UBCR12, 1997 .reg_uddr = &UDDR12, 1998 }, 1999 .ep[13] = { 2000 .ep = { 2001 .name = "ep13in-iso", 2002 .ops = &pxa25x_ep_ops, 2003 .maxpacket = ISO_FIFO_SIZE, 2004 }, 2005 .dev = &memory, 2006 .fifo_size = ISO_FIFO_SIZE, 2007 .bEndpointAddress = USB_DIR_IN | 13, 2008 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 2009 .reg_udccs = &UDCCS13, 2010 .reg_uddr = &UDDR13, 2011 }, 2012 .ep[14] = { 2013 .ep = { 2014 .name = "ep14out-iso", 2015 .ops = &pxa25x_ep_ops, 2016 .maxpacket = ISO_FIFO_SIZE, 2017 }, 2018 .dev = &memory, 2019 .fifo_size = ISO_FIFO_SIZE, 2020 .bEndpointAddress = 14, 2021 .bmAttributes = USB_ENDPOINT_XFER_ISOC, 2022 .reg_udccs = &UDCCS14, 2023 .reg_ubcr = &UBCR14, 2024 .reg_uddr = &UDDR14, 2025 }, 2026 .ep[15] = { 2027 .ep = { 2028 .name = "ep15in-int", 2029 .ops = &pxa25x_ep_ops, 2030 .maxpacket = INT_FIFO_SIZE, 2031 }, 2032 .dev = &memory, 2033 .fifo_size = INT_FIFO_SIZE, 2034 .bEndpointAddress = USB_DIR_IN | 15, 2035 .bmAttributes = USB_ENDPOINT_XFER_INT, 2036 .reg_udccs = &UDCCS15, 2037 .reg_uddr = &UDDR15, 2038 }, 2039 #endif /* !CONFIG_USB_PXA25X_SMALL */ 2040 }; 2041 2042 #define CP15R0_VENDOR_MASK 0xffffe000 2043 2044 #if defined(CONFIG_ARCH_PXA) 2045 #define CP15R0_XSCALE_VALUE 0x69052000 /* intel/arm/xscale */ 2046 2047 #elif defined(CONFIG_ARCH_IXP4XX) 2048 #define CP15R0_XSCALE_VALUE 0x69054000 /* intel/arm/ixp4xx */ 2049 2050 #endif 2051 2052 #define CP15R0_PROD_MASK 0x000003f0 2053 #define PXA25x 0x00000100 /* and PXA26x */ 2054 #define PXA210 0x00000120 2055 2056 #define CP15R0_REV_MASK 0x0000000f 2057 2058 #define CP15R0_PRODREV_MASK (CP15R0_PROD_MASK | CP15R0_REV_MASK) 2059 2060 #define PXA255_A0 0x00000106 /* or PXA260_B1 */ 2061 #define PXA250_C0 0x00000105 /* or PXA26x_B0 */ 2062 #define PXA250_B2 0x00000104 2063 #define PXA250_B1 0x00000103 /* or PXA260_A0 */ 2064 #define PXA250_B0 0x00000102 2065 #define PXA250_A1 0x00000101 2066 #define PXA250_A0 0x00000100 2067 2068 #define PXA210_C0 0x00000125 2069 #define PXA210_B2 0x00000124 2070 #define PXA210_B1 0x00000123 2071 #define PXA210_B0 0x00000122 2072 #define IXP425_A0 0x000001c1 2073 #define IXP425_B0 0x000001f1 2074 #define IXP465_AD 0x00000200 2075 2076 /* 2077 * probe - binds to the platform device 2078 */ 2079 static int pxa25x_udc_probe(struct platform_device *pdev) 2080 { 2081 struct pxa25x_udc *dev = &memory; 2082 int retval, irq; 2083 u32 chiprev; 2084 2085 pr_info("%s: version %s\n", driver_name, DRIVER_VERSION); 2086 2087 /* insist on Intel/ARM/XScale */ 2088 asm("mrc%? p15, 0, %0, c0, c0" : "=r" (chiprev)); 2089 if ((chiprev & CP15R0_VENDOR_MASK) != CP15R0_XSCALE_VALUE) { 2090 pr_err("%s: not XScale!\n", driver_name); 2091 return -ENODEV; 2092 } 2093 2094 /* trigger chiprev-specific logic */ 2095 switch (chiprev & CP15R0_PRODREV_MASK) { 2096 #if defined(CONFIG_ARCH_PXA) 2097 case PXA255_A0: 2098 dev->has_cfr = 1; 2099 break; 2100 case PXA250_A0: 2101 case PXA250_A1: 2102 /* A0/A1 "not released"; ep 13, 15 unusable */ 2103 /* fall through */ 2104 case PXA250_B2: case PXA210_B2: 2105 case PXA250_B1: case PXA210_B1: 2106 case PXA250_B0: case PXA210_B0: 2107 /* OUT-DMA is broken ... */ 2108 /* fall through */ 2109 case PXA250_C0: case PXA210_C0: 2110 break; 2111 #elif defined(CONFIG_ARCH_IXP4XX) 2112 case IXP425_A0: 2113 case IXP425_B0: 2114 case IXP465_AD: 2115 dev->has_cfr = 1; 2116 break; 2117 #endif 2118 default: 2119 pr_err("%s: unrecognized processor: %08x\n", 2120 driver_name, chiprev); 2121 /* iop3xx, ixp4xx, ... */ 2122 return -ENODEV; 2123 } 2124 2125 irq = platform_get_irq(pdev, 0); 2126 if (irq < 0) 2127 return -ENODEV; 2128 2129 dev->clk = devm_clk_get(&pdev->dev, NULL); 2130 if (IS_ERR(dev->clk)) 2131 return PTR_ERR(dev->clk); 2132 2133 pr_debug("%s: IRQ %d%s%s\n", driver_name, irq, 2134 dev->has_cfr ? "" : " (!cfr)", 2135 SIZE_STR "(pio)" 2136 ); 2137 2138 /* other non-static parts of init */ 2139 dev->dev = &pdev->dev; 2140 dev->mach = dev_get_platdata(&pdev->dev); 2141 2142 dev->transceiver = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2); 2143 2144 if (gpio_is_valid(dev->mach->gpio_pullup)) { 2145 retval = devm_gpio_request(&pdev->dev, dev->mach->gpio_pullup, 2146 "pca25x_udc GPIO PULLUP"); 2147 if (retval) { 2148 dev_dbg(&pdev->dev, 2149 "can't get pullup gpio %d, err: %d\n", 2150 dev->mach->gpio_pullup, retval); 2151 goto err; 2152 } 2153 gpio_direction_output(dev->mach->gpio_pullup, 0); 2154 } 2155 2156 init_timer(&dev->timer); 2157 dev->timer.function = udc_watchdog; 2158 dev->timer.data = (unsigned long) dev; 2159 2160 the_controller = dev; 2161 platform_set_drvdata(pdev, dev); 2162 2163 udc_disable(dev); 2164 udc_reinit(dev); 2165 2166 dev->vbus = 0; 2167 2168 /* irq setup after old hardware state is cleaned up */ 2169 retval = devm_request_irq(&pdev->dev, irq, pxa25x_udc_irq, 0, 2170 driver_name, dev); 2171 if (retval != 0) { 2172 pr_err("%s: can't get irq %d, err %d\n", 2173 driver_name, irq, retval); 2174 goto err; 2175 } 2176 dev->got_irq = 1; 2177 2178 #ifdef CONFIG_ARCH_LUBBOCK 2179 if (machine_is_lubbock()) { 2180 retval = devm_request_irq(&pdev->dev, LUBBOCK_USB_DISC_IRQ, 2181 lubbock_vbus_irq, 0, driver_name, 2182 dev); 2183 if (retval != 0) { 2184 pr_err("%s: can't get irq %i, err %d\n", 2185 driver_name, LUBBOCK_USB_DISC_IRQ, retval); 2186 goto err; 2187 } 2188 retval = devm_request_irq(&pdev->dev, LUBBOCK_USB_IRQ, 2189 lubbock_vbus_irq, 0, driver_name, 2190 dev); 2191 if (retval != 0) { 2192 pr_err("%s: can't get irq %i, err %d\n", 2193 driver_name, LUBBOCK_USB_IRQ, retval); 2194 goto err; 2195 } 2196 } else 2197 #endif 2198 create_debug_files(dev); 2199 2200 retval = usb_add_gadget_udc(&pdev->dev, &dev->gadget); 2201 if (!retval) 2202 return retval; 2203 2204 remove_debug_files(dev); 2205 err: 2206 if (!IS_ERR_OR_NULL(dev->transceiver)) 2207 dev->transceiver = NULL; 2208 return retval; 2209 } 2210 2211 static void pxa25x_udc_shutdown(struct platform_device *_dev) 2212 { 2213 pullup_off(); 2214 } 2215 2216 static int pxa25x_udc_remove(struct platform_device *pdev) 2217 { 2218 struct pxa25x_udc *dev = platform_get_drvdata(pdev); 2219 2220 if (dev->driver) 2221 return -EBUSY; 2222 2223 usb_del_gadget_udc(&dev->gadget); 2224 dev->pullup = 0; 2225 pullup(dev); 2226 2227 remove_debug_files(dev); 2228 2229 if (!IS_ERR_OR_NULL(dev->transceiver)) 2230 dev->transceiver = NULL; 2231 2232 the_controller = NULL; 2233 return 0; 2234 } 2235 2236 /*-------------------------------------------------------------------------*/ 2237 2238 #ifdef CONFIG_PM 2239 2240 /* USB suspend (controlled by the host) and system suspend (controlled 2241 * by the PXA) don't necessarily work well together. If USB is active, 2242 * the 48 MHz clock is required; so the system can't enter 33 MHz idle 2243 * mode, or any deeper PM saving state. 2244 * 2245 * For now, we punt and forcibly disconnect from the USB host when PXA 2246 * enters any suspend state. While we're disconnected, we always disable 2247 * the 48MHz USB clock ... allowing PXA sleep and/or 33 MHz idle states. 2248 * Boards without software pullup control shouldn't use those states. 2249 * VBUS IRQs should probably be ignored so that the PXA device just acts 2250 * "dead" to USB hosts until system resume. 2251 */ 2252 static int pxa25x_udc_suspend(struct platform_device *dev, pm_message_t state) 2253 { 2254 struct pxa25x_udc *udc = platform_get_drvdata(dev); 2255 unsigned long flags; 2256 2257 if (!gpio_is_valid(udc->mach->gpio_pullup) && !udc->mach->udc_command) 2258 WARNING("USB host won't detect disconnect!\n"); 2259 udc->suspended = 1; 2260 2261 local_irq_save(flags); 2262 pullup(udc); 2263 local_irq_restore(flags); 2264 2265 return 0; 2266 } 2267 2268 static int pxa25x_udc_resume(struct platform_device *dev) 2269 { 2270 struct pxa25x_udc *udc = platform_get_drvdata(dev); 2271 unsigned long flags; 2272 2273 udc->suspended = 0; 2274 local_irq_save(flags); 2275 pullup(udc); 2276 local_irq_restore(flags); 2277 2278 return 0; 2279 } 2280 2281 #else 2282 #define pxa25x_udc_suspend NULL 2283 #define pxa25x_udc_resume NULL 2284 #endif 2285 2286 /*-------------------------------------------------------------------------*/ 2287 2288 static struct platform_driver udc_driver = { 2289 .shutdown = pxa25x_udc_shutdown, 2290 .probe = pxa25x_udc_probe, 2291 .remove = pxa25x_udc_remove, 2292 .suspend = pxa25x_udc_suspend, 2293 .resume = pxa25x_udc_resume, 2294 .driver = { 2295 .name = "pxa25x-udc", 2296 }, 2297 }; 2298 2299 module_platform_driver(udc_driver); 2300 2301 MODULE_DESCRIPTION(DRIVER_DESC); 2302 MODULE_AUTHOR("Frank Becker, Robert Schwebel, David Brownell"); 2303 MODULE_LICENSE("GPL"); 2304 MODULE_ALIAS("platform:pxa25x-udc"); 2305