1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link 4 * 5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com 6 * 7 * Authors: Felipe Balbi <balbi@ti.com>, 8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de> 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/delay.h> 13 #include <linux/slab.h> 14 #include <linux/spinlock.h> 15 #include <linux/platform_device.h> 16 #include <linux/pm_runtime.h> 17 #include <linux/interrupt.h> 18 #include <linux/io.h> 19 #include <linux/list.h> 20 #include <linux/dma-mapping.h> 21 22 #include <linux/usb/ch9.h> 23 #include <linux/usb/gadget.h> 24 25 #include "debug.h" 26 #include "core.h" 27 #include "gadget.h" 28 #include "io.h" 29 30 #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \ 31 & ~((d)->interval - 1)) 32 33 /** 34 * dwc3_gadget_set_test_mode - enables usb2 test modes 35 * @dwc: pointer to our context structure 36 * @mode: the mode to set (J, K SE0 NAK, Force Enable) 37 * 38 * Caller should take care of locking. This function will return 0 on 39 * success or -EINVAL if wrong Test Selector is passed. 40 */ 41 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) 42 { 43 u32 reg; 44 45 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 46 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 47 48 switch (mode) { 49 case TEST_J: 50 case TEST_K: 51 case TEST_SE0_NAK: 52 case TEST_PACKET: 53 case TEST_FORCE_EN: 54 reg |= mode << 1; 55 break; 56 default: 57 return -EINVAL; 58 } 59 60 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 61 62 return 0; 63 } 64 65 /** 66 * dwc3_gadget_get_link_state - gets current state of usb link 67 * @dwc: pointer to our context structure 68 * 69 * Caller should take care of locking. This function will 70 * return the link state on success (>= 0) or -ETIMEDOUT. 71 */ 72 int dwc3_gadget_get_link_state(struct dwc3 *dwc) 73 { 74 u32 reg; 75 76 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 77 78 return DWC3_DSTS_USBLNKST(reg); 79 } 80 81 /** 82 * dwc3_gadget_set_link_state - sets usb link to a particular state 83 * @dwc: pointer to our context structure 84 * @state: the state to put link into 85 * 86 * Caller should take care of locking. This function will 87 * return 0 on success or -ETIMEDOUT. 88 */ 89 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) 90 { 91 int retries = 10000; 92 u32 reg; 93 94 /* 95 * Wait until device controller is ready. Only applies to 1.94a and 96 * later RTL. 97 */ 98 if (dwc->revision >= DWC3_REVISION_194A) { 99 while (--retries) { 100 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 101 if (reg & DWC3_DSTS_DCNRD) 102 udelay(5); 103 else 104 break; 105 } 106 107 if (retries <= 0) 108 return -ETIMEDOUT; 109 } 110 111 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 113 114 /* set requested state */ 115 reg |= DWC3_DCTL_ULSTCHNGREQ(state); 116 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 117 118 /* 119 * The following code is racy when called from dwc3_gadget_wakeup, 120 * and is not needed, at least on newer versions 121 */ 122 if (dwc->revision >= DWC3_REVISION_194A) 123 return 0; 124 125 /* wait for a change in DSTS */ 126 retries = 10000; 127 while (--retries) { 128 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 129 130 if (DWC3_DSTS_USBLNKST(reg) == state) 131 return 0; 132 133 udelay(5); 134 } 135 136 return -ETIMEDOUT; 137 } 138 139 /** 140 * dwc3_ep_inc_trb - increment a trb index. 141 * @index: Pointer to the TRB index to increment. 142 * 143 * The index should never point to the link TRB. After incrementing, 144 * if it is point to the link TRB, wrap around to the beginning. The 145 * link TRB is always at the last TRB entry. 146 */ 147 static void dwc3_ep_inc_trb(u8 *index) 148 { 149 (*index)++; 150 if (*index == (DWC3_TRB_NUM - 1)) 151 *index = 0; 152 } 153 154 /** 155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer 156 * @dep: The endpoint whose enqueue pointer we're incrementing 157 */ 158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep) 159 { 160 dwc3_ep_inc_trb(&dep->trb_enqueue); 161 } 162 163 /** 164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer 165 * @dep: The endpoint whose enqueue pointer we're incrementing 166 */ 167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep) 168 { 169 dwc3_ep_inc_trb(&dep->trb_dequeue); 170 } 171 172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, 173 struct dwc3_request *req, int status) 174 { 175 struct dwc3 *dwc = dep->dwc; 176 177 req->started = false; 178 list_del(&req->list); 179 req->remaining = 0; 180 181 if (req->request.status == -EINPROGRESS) 182 req->request.status = status; 183 184 if (req->trb) 185 usb_gadget_unmap_request_by_dev(dwc->sysdev, 186 &req->request, req->direction); 187 188 req->trb = NULL; 189 trace_dwc3_gadget_giveback(req); 190 191 if (dep->number > 1) 192 pm_runtime_put(dwc->dev); 193 } 194 195 /** 196 * dwc3_gadget_giveback - call struct usb_request's ->complete callback 197 * @dep: The endpoint to whom the request belongs to 198 * @req: The request we're giving back 199 * @status: completion code for the request 200 * 201 * Must be called with controller's lock held and interrupts disabled. This 202 * function will unmap @req and call its ->complete() callback to notify upper 203 * layers that it has completed. 204 */ 205 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, 206 int status) 207 { 208 struct dwc3 *dwc = dep->dwc; 209 210 dwc3_gadget_del_and_unmap_request(dep, req, status); 211 212 spin_unlock(&dwc->lock); 213 usb_gadget_giveback_request(&dep->endpoint, &req->request); 214 spin_lock(&dwc->lock); 215 } 216 217 /** 218 * dwc3_send_gadget_generic_command - issue a generic command for the controller 219 * @dwc: pointer to the controller context 220 * @cmd: the command to be issued 221 * @param: command parameter 222 * 223 * Caller should take care of locking. Issue @cmd with a given @param to @dwc 224 * and wait for its completion. 225 */ 226 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) 227 { 228 u32 timeout = 500; 229 int status = 0; 230 int ret = 0; 231 u32 reg; 232 233 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); 234 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); 235 236 do { 237 reg = dwc3_readl(dwc->regs, DWC3_DGCMD); 238 if (!(reg & DWC3_DGCMD_CMDACT)) { 239 status = DWC3_DGCMD_STATUS(reg); 240 if (status) 241 ret = -EINVAL; 242 break; 243 } 244 } while (--timeout); 245 246 if (!timeout) { 247 ret = -ETIMEDOUT; 248 status = -ETIMEDOUT; 249 } 250 251 trace_dwc3_gadget_generic_cmd(cmd, param, status); 252 253 return ret; 254 } 255 256 static int __dwc3_gadget_wakeup(struct dwc3 *dwc); 257 258 /** 259 * dwc3_send_gadget_ep_cmd - issue an endpoint command 260 * @dep: the endpoint to which the command is going to be issued 261 * @cmd: the command to be issued 262 * @params: parameters to the command 263 * 264 * Caller should handle locking. This function will issue @cmd with given 265 * @params to @dep and wait for its completion. 266 */ 267 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, 268 struct dwc3_gadget_ep_cmd_params *params) 269 { 270 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 271 struct dwc3 *dwc = dep->dwc; 272 u32 timeout = 1000; 273 u32 saved_config = 0; 274 u32 reg; 275 276 int cmd_status = 0; 277 int ret = -EINVAL; 278 279 /* 280 * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or 281 * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an 282 * endpoint command. 283 * 284 * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY 285 * settings. Restore them after the command is completed. 286 * 287 * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 288 */ 289 if (dwc->gadget.speed <= USB_SPEED_HIGH) { 290 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 291 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { 292 saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; 293 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; 294 } 295 296 if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { 297 saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; 298 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; 299 } 300 301 if (saved_config) 302 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 303 } 304 305 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { 306 int needs_wakeup; 307 308 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 || 309 dwc->link_state == DWC3_LINK_STATE_U2 || 310 dwc->link_state == DWC3_LINK_STATE_U3); 311 312 if (unlikely(needs_wakeup)) { 313 ret = __dwc3_gadget_wakeup(dwc); 314 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", 315 ret); 316 } 317 } 318 319 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); 320 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); 321 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); 322 323 /* 324 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're 325 * not relying on XferNotReady, we can make use of a special "No 326 * Response Update Transfer" command where we should clear both CmdAct 327 * and CmdIOC bits. 328 * 329 * With this, we don't need to wait for command completion and can 330 * straight away issue further commands to the endpoint. 331 * 332 * NOTICE: We're making an assumption that control endpoints will never 333 * make use of Update Transfer command. This is a safe assumption 334 * because we can never have more than one request at a time with 335 * Control Endpoints. If anybody changes that assumption, this chunk 336 * needs to be updated accordingly. 337 */ 338 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && 339 !usb_endpoint_xfer_isoc(desc)) 340 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); 341 else 342 cmd |= DWC3_DEPCMD_CMDACT; 343 344 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); 345 do { 346 reg = dwc3_readl(dep->regs, DWC3_DEPCMD); 347 if (!(reg & DWC3_DEPCMD_CMDACT)) { 348 cmd_status = DWC3_DEPCMD_STATUS(reg); 349 350 switch (cmd_status) { 351 case 0: 352 ret = 0; 353 break; 354 case DEPEVT_TRANSFER_NO_RESOURCE: 355 ret = -EINVAL; 356 break; 357 case DEPEVT_TRANSFER_BUS_EXPIRY: 358 /* 359 * SW issues START TRANSFER command to 360 * isochronous ep with future frame interval. If 361 * future interval time has already passed when 362 * core receives the command, it will respond 363 * with an error status of 'Bus Expiry'. 364 * 365 * Instead of always returning -EINVAL, let's 366 * give a hint to the gadget driver that this is 367 * the case by returning -EAGAIN. 368 */ 369 ret = -EAGAIN; 370 break; 371 default: 372 dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); 373 } 374 375 break; 376 } 377 } while (--timeout); 378 379 if (timeout == 0) { 380 ret = -ETIMEDOUT; 381 cmd_status = -ETIMEDOUT; 382 } 383 384 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); 385 386 if (ret == 0) { 387 switch (DWC3_DEPCMD_CMD(cmd)) { 388 case DWC3_DEPCMD_STARTTRANSFER: 389 dep->flags |= DWC3_EP_TRANSFER_STARTED; 390 dwc3_gadget_ep_get_transfer_index(dep); 391 break; 392 case DWC3_DEPCMD_ENDTRANSFER: 393 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; 394 break; 395 default: 396 /* nothing */ 397 break; 398 } 399 } 400 401 if (saved_config) { 402 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 403 reg |= saved_config; 404 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 405 } 406 407 return ret; 408 } 409 410 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) 411 { 412 struct dwc3 *dwc = dep->dwc; 413 struct dwc3_gadget_ep_cmd_params params; 414 u32 cmd = DWC3_DEPCMD_CLEARSTALL; 415 416 /* 417 * As of core revision 2.60a the recommended programming model 418 * is to set the ClearPendIN bit when issuing a Clear Stall EP 419 * command for IN endpoints. This is to prevent an issue where 420 * some (non-compliant) hosts may not send ACK TPs for pending 421 * IN transfers due to a mishandled error condition. Synopsys 422 * STAR 9000614252. 423 */ 424 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) && 425 (dwc->gadget.speed >= USB_SPEED_SUPER)) 426 cmd |= DWC3_DEPCMD_CLEARPENDIN; 427 428 memset(¶ms, 0, sizeof(params)); 429 430 return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 431 } 432 433 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, 434 struct dwc3_trb *trb) 435 { 436 u32 offset = (char *) trb - (char *) dep->trb_pool; 437 438 return dep->trb_pool_dma + offset; 439 } 440 441 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) 442 { 443 struct dwc3 *dwc = dep->dwc; 444 445 if (dep->trb_pool) 446 return 0; 447 448 dep->trb_pool = dma_alloc_coherent(dwc->sysdev, 449 sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 450 &dep->trb_pool_dma, GFP_KERNEL); 451 if (!dep->trb_pool) { 452 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", 453 dep->name); 454 return -ENOMEM; 455 } 456 457 return 0; 458 } 459 460 static void dwc3_free_trb_pool(struct dwc3_ep *dep) 461 { 462 struct dwc3 *dwc = dep->dwc; 463 464 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, 465 dep->trb_pool, dep->trb_pool_dma); 466 467 dep->trb_pool = NULL; 468 dep->trb_pool_dma = 0; 469 } 470 471 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) 472 { 473 struct dwc3_gadget_ep_cmd_params params; 474 475 memset(¶ms, 0x00, sizeof(params)); 476 477 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); 478 479 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, 480 ¶ms); 481 } 482 483 /** 484 * dwc3_gadget_start_config - configure ep resources 485 * @dep: endpoint that is being enabled 486 * 487 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's 488 * completion, it will set Transfer Resource for all available endpoints. 489 * 490 * The assignment of transfer resources cannot perfectly follow the data book 491 * due to the fact that the controller driver does not have all knowledge of the 492 * configuration in advance. It is given this information piecemeal by the 493 * composite gadget framework after every SET_CONFIGURATION and 494 * SET_INTERFACE. Trying to follow the databook programming model in this 495 * scenario can cause errors. For two reasons: 496 * 497 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every 498 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is 499 * incorrect in the scenario of multiple interfaces. 500 * 501 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new 502 * endpoint on alt setting (8.1.6). 503 * 504 * The following simplified method is used instead: 505 * 506 * All hardware endpoints can be assigned a transfer resource and this setting 507 * will stay persistent until either a core reset or hibernation. So whenever we 508 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do 509 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are 510 * guaranteed that there are as many transfer resources as endpoints. 511 * 512 * This function is called for each endpoint when it is being enabled but is 513 * triggered only when called for EP0-out, which always happens first, and which 514 * should only happen in one of the above conditions. 515 */ 516 static int dwc3_gadget_start_config(struct dwc3_ep *dep) 517 { 518 struct dwc3_gadget_ep_cmd_params params; 519 struct dwc3 *dwc; 520 u32 cmd; 521 int i; 522 int ret; 523 524 if (dep->number) 525 return 0; 526 527 memset(¶ms, 0x00, sizeof(params)); 528 cmd = DWC3_DEPCMD_DEPSTARTCFG; 529 dwc = dep->dwc; 530 531 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 532 if (ret) 533 return ret; 534 535 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 536 struct dwc3_ep *dep = dwc->eps[i]; 537 538 if (!dep) 539 continue; 540 541 ret = dwc3_gadget_set_xfer_resource(dep); 542 if (ret) 543 return ret; 544 } 545 546 return 0; 547 } 548 549 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) 550 { 551 const struct usb_ss_ep_comp_descriptor *comp_desc; 552 const struct usb_endpoint_descriptor *desc; 553 struct dwc3_gadget_ep_cmd_params params; 554 struct dwc3 *dwc = dep->dwc; 555 556 comp_desc = dep->endpoint.comp_desc; 557 desc = dep->endpoint.desc; 558 559 memset(¶ms, 0x00, sizeof(params)); 560 561 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) 562 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); 563 564 /* Burst size is only needed in SuperSpeed mode */ 565 if (dwc->gadget.speed >= USB_SPEED_SUPER) { 566 u32 burst = dep->endpoint.maxburst; 567 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); 568 } 569 570 params.param0 |= action; 571 if (action == DWC3_DEPCFG_ACTION_RESTORE) 572 params.param2 |= dep->saved_state; 573 574 if (usb_endpoint_xfer_control(desc)) 575 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; 576 577 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) 578 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; 579 580 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { 581 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE 582 | DWC3_DEPCFG_STREAM_EVENT_EN; 583 dep->stream_capable = true; 584 } 585 586 if (!usb_endpoint_xfer_control(desc)) 587 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; 588 589 /* 590 * We are doing 1:1 mapping for endpoints, meaning 591 * Physical Endpoints 2 maps to Logical Endpoint 2 and 592 * so on. We consider the direction bit as part of the physical 593 * endpoint number. So USB endpoint 0x81 is 0x03. 594 */ 595 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); 596 597 /* 598 * We must use the lower 16 TX FIFOs even though 599 * HW might have more 600 */ 601 if (dep->direction) 602 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); 603 604 if (desc->bInterval) { 605 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); 606 dep->interval = 1 << (desc->bInterval - 1); 607 } 608 609 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); 610 } 611 612 /** 613 * __dwc3_gadget_ep_enable - initializes a hw endpoint 614 * @dep: endpoint to be initialized 615 * @action: one of INIT, MODIFY or RESTORE 616 * 617 * Caller should take care of locking. Execute all necessary commands to 618 * initialize a HW endpoint so it can be used by a gadget driver. 619 */ 620 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) 621 { 622 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; 623 struct dwc3 *dwc = dep->dwc; 624 625 u32 reg; 626 int ret; 627 628 if (!(dep->flags & DWC3_EP_ENABLED)) { 629 ret = dwc3_gadget_start_config(dep); 630 if (ret) 631 return ret; 632 } 633 634 ret = dwc3_gadget_set_ep_config(dep, action); 635 if (ret) 636 return ret; 637 638 if (!(dep->flags & DWC3_EP_ENABLED)) { 639 struct dwc3_trb *trb_st_hw; 640 struct dwc3_trb *trb_link; 641 642 dep->type = usb_endpoint_type(desc); 643 dep->flags |= DWC3_EP_ENABLED; 644 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 645 646 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); 647 reg |= DWC3_DALEPENA_EP(dep->number); 648 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); 649 650 if (usb_endpoint_xfer_control(desc)) 651 goto out; 652 653 /* Initialize the TRB ring */ 654 dep->trb_dequeue = 0; 655 dep->trb_enqueue = 0; 656 memset(dep->trb_pool, 0, 657 sizeof(struct dwc3_trb) * DWC3_TRB_NUM); 658 659 /* Link TRB. The HWO bit is never reset */ 660 trb_st_hw = &dep->trb_pool[0]; 661 662 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; 663 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 664 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); 665 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; 666 trb_link->ctrl |= DWC3_TRB_CTRL_HWO; 667 } 668 669 /* 670 * Issue StartTransfer here with no-op TRB so we can always rely on No 671 * Response Update Transfer command. 672 */ 673 if ((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) || 674 usb_endpoint_xfer_int(desc)) { 675 struct dwc3_gadget_ep_cmd_params params; 676 struct dwc3_trb *trb; 677 dma_addr_t trb_dma; 678 u32 cmd; 679 680 memset(¶ms, 0, sizeof(params)); 681 trb = &dep->trb_pool[0]; 682 trb_dma = dwc3_trb_dma_offset(dep, trb); 683 684 params.param0 = upper_32_bits(trb_dma); 685 params.param1 = lower_32_bits(trb_dma); 686 687 cmd = DWC3_DEPCMD_STARTTRANSFER; 688 689 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 690 if (ret < 0) 691 return ret; 692 } 693 694 out: 695 trace_dwc3_gadget_ep_enable(dep); 696 697 return 0; 698 } 699 700 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force); 701 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) 702 { 703 struct dwc3_request *req; 704 705 dwc3_stop_active_transfer(dep, true); 706 707 /* - giveback all requests to gadget driver */ 708 while (!list_empty(&dep->started_list)) { 709 req = next_request(&dep->started_list); 710 711 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); 712 } 713 714 while (!list_empty(&dep->pending_list)) { 715 req = next_request(&dep->pending_list); 716 717 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); 718 } 719 } 720 721 /** 722 * __dwc3_gadget_ep_disable - disables a hw endpoint 723 * @dep: the endpoint to disable 724 * 725 * This function undoes what __dwc3_gadget_ep_enable did and also removes 726 * requests which are currently being processed by the hardware and those which 727 * are not yet scheduled. 728 * 729 * Caller should take care of locking. 730 */ 731 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) 732 { 733 struct dwc3 *dwc = dep->dwc; 734 u32 reg; 735 736 trace_dwc3_gadget_ep_disable(dep); 737 738 dwc3_remove_requests(dwc, dep); 739 740 /* make sure HW endpoint isn't stalled */ 741 if (dep->flags & DWC3_EP_STALL) 742 __dwc3_gadget_ep_set_halt(dep, 0, false); 743 744 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); 745 reg &= ~DWC3_DALEPENA_EP(dep->number); 746 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); 747 748 dep->stream_capable = false; 749 dep->type = 0; 750 dep->flags &= DWC3_EP_END_TRANSFER_PENDING; 751 752 /* Clear out the ep descriptors for non-ep0 */ 753 if (dep->number > 1) { 754 dep->endpoint.comp_desc = NULL; 755 dep->endpoint.desc = NULL; 756 } 757 758 return 0; 759 } 760 761 /* -------------------------------------------------------------------------- */ 762 763 static int dwc3_gadget_ep0_enable(struct usb_ep *ep, 764 const struct usb_endpoint_descriptor *desc) 765 { 766 return -EINVAL; 767 } 768 769 static int dwc3_gadget_ep0_disable(struct usb_ep *ep) 770 { 771 return -EINVAL; 772 } 773 774 /* -------------------------------------------------------------------------- */ 775 776 static int dwc3_gadget_ep_enable(struct usb_ep *ep, 777 const struct usb_endpoint_descriptor *desc) 778 { 779 struct dwc3_ep *dep; 780 struct dwc3 *dwc; 781 unsigned long flags; 782 int ret; 783 784 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { 785 pr_debug("dwc3: invalid parameters\n"); 786 return -EINVAL; 787 } 788 789 if (!desc->wMaxPacketSize) { 790 pr_debug("dwc3: missing wMaxPacketSize\n"); 791 return -EINVAL; 792 } 793 794 dep = to_dwc3_ep(ep); 795 dwc = dep->dwc; 796 797 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, 798 "%s is already enabled\n", 799 dep->name)) 800 return 0; 801 802 spin_lock_irqsave(&dwc->lock, flags); 803 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 804 spin_unlock_irqrestore(&dwc->lock, flags); 805 806 return ret; 807 } 808 809 static int dwc3_gadget_ep_disable(struct usb_ep *ep) 810 { 811 struct dwc3_ep *dep; 812 struct dwc3 *dwc; 813 unsigned long flags; 814 int ret; 815 816 if (!ep) { 817 pr_debug("dwc3: invalid parameters\n"); 818 return -EINVAL; 819 } 820 821 dep = to_dwc3_ep(ep); 822 dwc = dep->dwc; 823 824 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), 825 "%s is already disabled\n", 826 dep->name)) 827 return 0; 828 829 spin_lock_irqsave(&dwc->lock, flags); 830 ret = __dwc3_gadget_ep_disable(dep); 831 spin_unlock_irqrestore(&dwc->lock, flags); 832 833 return ret; 834 } 835 836 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, 837 gfp_t gfp_flags) 838 { 839 struct dwc3_request *req; 840 struct dwc3_ep *dep = to_dwc3_ep(ep); 841 842 req = kzalloc(sizeof(*req), gfp_flags); 843 if (!req) 844 return NULL; 845 846 req->direction = dep->direction; 847 req->epnum = dep->number; 848 req->dep = dep; 849 850 trace_dwc3_alloc_request(req); 851 852 return &req->request; 853 } 854 855 static void dwc3_gadget_ep_free_request(struct usb_ep *ep, 856 struct usb_request *request) 857 { 858 struct dwc3_request *req = to_dwc3_request(request); 859 860 trace_dwc3_free_request(req); 861 kfree(req); 862 } 863 864 /** 865 * dwc3_ep_prev_trb - returns the previous TRB in the ring 866 * @dep: The endpoint with the TRB ring 867 * @index: The index of the current TRB in the ring 868 * 869 * Returns the TRB prior to the one pointed to by the index. If the 870 * index is 0, we will wrap backwards, skip the link TRB, and return 871 * the one just before that. 872 */ 873 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) 874 { 875 u8 tmp = index; 876 877 if (!tmp) 878 tmp = DWC3_TRB_NUM - 1; 879 880 return &dep->trb_pool[tmp - 1]; 881 } 882 883 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) 884 { 885 struct dwc3_trb *tmp; 886 u8 trbs_left; 887 888 /* 889 * If enqueue & dequeue are equal than it is either full or empty. 890 * 891 * One way to know for sure is if the TRB right before us has HWO bit 892 * set or not. If it has, then we're definitely full and can't fit any 893 * more transfers in our ring. 894 */ 895 if (dep->trb_enqueue == dep->trb_dequeue) { 896 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 897 if (tmp->ctrl & DWC3_TRB_CTRL_HWO) 898 return 0; 899 900 return DWC3_TRB_NUM - 1; 901 } 902 903 trbs_left = dep->trb_dequeue - dep->trb_enqueue; 904 trbs_left &= (DWC3_TRB_NUM - 1); 905 906 if (dep->trb_dequeue < dep->trb_enqueue) 907 trbs_left--; 908 909 return trbs_left; 910 } 911 912 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, 913 dma_addr_t dma, unsigned length, unsigned chain, unsigned node, 914 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt) 915 { 916 struct dwc3 *dwc = dep->dwc; 917 struct usb_gadget *gadget = &dwc->gadget; 918 enum usb_device_speed speed = gadget->speed; 919 920 trb->size = DWC3_TRB_SIZE_LENGTH(length); 921 trb->bpl = lower_32_bits(dma); 922 trb->bph = upper_32_bits(dma); 923 924 switch (usb_endpoint_type(dep->endpoint.desc)) { 925 case USB_ENDPOINT_XFER_CONTROL: 926 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; 927 break; 928 929 case USB_ENDPOINT_XFER_ISOC: 930 if (!node) { 931 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 932 933 /* 934 * USB Specification 2.0 Section 5.9.2 states that: "If 935 * there is only a single transaction in the microframe, 936 * only a DATA0 data packet PID is used. If there are 937 * two transactions per microframe, DATA1 is used for 938 * the first transaction data packet and DATA0 is used 939 * for the second transaction data packet. If there are 940 * three transactions per microframe, DATA2 is used for 941 * the first transaction data packet, DATA1 is used for 942 * the second, and DATA0 is used for the third." 943 * 944 * IOW, we should satisfy the following cases: 945 * 946 * 1) length <= maxpacket 947 * - DATA0 948 * 949 * 2) maxpacket < length <= (2 * maxpacket) 950 * - DATA1, DATA0 951 * 952 * 3) (2 * maxpacket) < length <= (3 * maxpacket) 953 * - DATA2, DATA1, DATA0 954 */ 955 if (speed == USB_SPEED_HIGH) { 956 struct usb_ep *ep = &dep->endpoint; 957 unsigned int mult = 2; 958 unsigned int maxp = usb_endpoint_maxp(ep->desc); 959 960 if (length <= (2 * maxp)) 961 mult--; 962 963 if (length <= maxp) 964 mult--; 965 966 trb->size |= DWC3_TRB_SIZE_PCM1(mult); 967 } 968 } else { 969 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; 970 } 971 972 /* always enable Interrupt on Missed ISOC */ 973 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 974 break; 975 976 case USB_ENDPOINT_XFER_BULK: 977 case USB_ENDPOINT_XFER_INT: 978 trb->ctrl = DWC3_TRBCTL_NORMAL; 979 break; 980 default: 981 /* 982 * This is only possible with faulty memory because we 983 * checked it already :) 984 */ 985 dev_WARN(dwc->dev, "Unknown endpoint type %d\n", 986 usb_endpoint_type(dep->endpoint.desc)); 987 } 988 989 /* 990 * Enable Continue on Short Packet 991 * when endpoint is not a stream capable 992 */ 993 if (usb_endpoint_dir_out(dep->endpoint.desc)) { 994 if (!dep->stream_capable) 995 trb->ctrl |= DWC3_TRB_CTRL_CSP; 996 997 if (short_not_ok) 998 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; 999 } 1000 1001 if ((!no_interrupt && !chain) || 1002 (dwc3_calc_trbs_left(dep) == 1)) 1003 trb->ctrl |= DWC3_TRB_CTRL_IOC; 1004 1005 if (chain) 1006 trb->ctrl |= DWC3_TRB_CTRL_CHN; 1007 1008 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) 1009 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); 1010 1011 trb->ctrl |= DWC3_TRB_CTRL_HWO; 1012 1013 dwc3_ep_inc_enq(dep); 1014 1015 trace_dwc3_prepare_trb(dep, trb); 1016 } 1017 1018 /** 1019 * dwc3_prepare_one_trb - setup one TRB from one request 1020 * @dep: endpoint for which this request is prepared 1021 * @req: dwc3_request pointer 1022 * @chain: should this TRB be chained to the next? 1023 * @node: only for isochronous endpoints. First TRB needs different type. 1024 */ 1025 static void dwc3_prepare_one_trb(struct dwc3_ep *dep, 1026 struct dwc3_request *req, unsigned chain, unsigned node) 1027 { 1028 struct dwc3_trb *trb; 1029 unsigned int length; 1030 dma_addr_t dma; 1031 unsigned stream_id = req->request.stream_id; 1032 unsigned short_not_ok = req->request.short_not_ok; 1033 unsigned no_interrupt = req->request.no_interrupt; 1034 1035 if (req->request.num_sgs > 0) { 1036 length = sg_dma_len(req->start_sg); 1037 dma = sg_dma_address(req->start_sg); 1038 } else { 1039 length = req->request.length; 1040 dma = req->request.dma; 1041 } 1042 1043 trb = &dep->trb_pool[dep->trb_enqueue]; 1044 1045 if (!req->trb) { 1046 dwc3_gadget_move_started_request(req); 1047 req->trb = trb; 1048 req->trb_dma = dwc3_trb_dma_offset(dep, trb); 1049 } 1050 1051 req->num_trbs++; 1052 1053 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node, 1054 stream_id, short_not_ok, no_interrupt); 1055 } 1056 1057 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, 1058 struct dwc3_request *req) 1059 { 1060 struct scatterlist *sg = req->start_sg; 1061 struct scatterlist *s; 1062 int i; 1063 1064 unsigned int remaining = req->request.num_mapped_sgs 1065 - req->num_queued_sgs; 1066 1067 for_each_sg(sg, s, remaining, i) { 1068 unsigned int length = req->request.length; 1069 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1070 unsigned int rem = length % maxp; 1071 unsigned chain = true; 1072 1073 if (sg_is_last(s)) 1074 chain = false; 1075 1076 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) { 1077 struct dwc3 *dwc = dep->dwc; 1078 struct dwc3_trb *trb; 1079 1080 req->needs_extra_trb = true; 1081 1082 /* prepare normal TRB */ 1083 dwc3_prepare_one_trb(dep, req, true, i); 1084 1085 /* Now prepare one extra TRB to align transfer size */ 1086 trb = &dep->trb_pool[dep->trb_enqueue]; 1087 req->num_trbs++; 1088 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 1089 maxp - rem, false, 1, 1090 req->request.stream_id, 1091 req->request.short_not_ok, 1092 req->request.no_interrupt); 1093 } else { 1094 dwc3_prepare_one_trb(dep, req, chain, i); 1095 } 1096 1097 /* 1098 * There can be a situation where all sgs in sglist are not 1099 * queued because of insufficient trb number. To handle this 1100 * case, update start_sg to next sg to be queued, so that 1101 * we have free trbs we can continue queuing from where we 1102 * previously stopped 1103 */ 1104 if (chain) 1105 req->start_sg = sg_next(s); 1106 1107 req->num_queued_sgs++; 1108 1109 if (!dwc3_calc_trbs_left(dep)) 1110 break; 1111 } 1112 } 1113 1114 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep, 1115 struct dwc3_request *req) 1116 { 1117 unsigned int length = req->request.length; 1118 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); 1119 unsigned int rem = length % maxp; 1120 1121 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) { 1122 struct dwc3 *dwc = dep->dwc; 1123 struct dwc3_trb *trb; 1124 1125 req->needs_extra_trb = true; 1126 1127 /* prepare normal TRB */ 1128 dwc3_prepare_one_trb(dep, req, true, 0); 1129 1130 /* Now prepare one extra TRB to align transfer size */ 1131 trb = &dep->trb_pool[dep->trb_enqueue]; 1132 req->num_trbs++; 1133 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem, 1134 false, 1, req->request.stream_id, 1135 req->request.short_not_ok, 1136 req->request.no_interrupt); 1137 } else if (req->request.zero && req->request.length && 1138 (IS_ALIGNED(req->request.length, maxp))) { 1139 struct dwc3 *dwc = dep->dwc; 1140 struct dwc3_trb *trb; 1141 1142 req->needs_extra_trb = true; 1143 1144 /* prepare normal TRB */ 1145 dwc3_prepare_one_trb(dep, req, true, 0); 1146 1147 /* Now prepare one extra TRB to handle ZLP */ 1148 trb = &dep->trb_pool[dep->trb_enqueue]; 1149 req->num_trbs++; 1150 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0, 1151 false, 1, req->request.stream_id, 1152 req->request.short_not_ok, 1153 req->request.no_interrupt); 1154 } else { 1155 dwc3_prepare_one_trb(dep, req, false, 0); 1156 } 1157 } 1158 1159 /* 1160 * dwc3_prepare_trbs - setup TRBs from requests 1161 * @dep: endpoint for which requests are being prepared 1162 * 1163 * The function goes through the requests list and sets up TRBs for the 1164 * transfers. The function returns once there are no more TRBs available or 1165 * it runs out of requests. 1166 */ 1167 static void dwc3_prepare_trbs(struct dwc3_ep *dep) 1168 { 1169 struct dwc3_request *req, *n; 1170 1171 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); 1172 1173 /* 1174 * We can get in a situation where there's a request in the started list 1175 * but there weren't enough TRBs to fully kick it in the first time 1176 * around, so it has been waiting for more TRBs to be freed up. 1177 * 1178 * In that case, we should check if we have a request with pending_sgs 1179 * in the started list and prepare TRBs for that request first, 1180 * otherwise we will prepare TRBs completely out of order and that will 1181 * break things. 1182 */ 1183 list_for_each_entry(req, &dep->started_list, list) { 1184 if (req->num_pending_sgs > 0) 1185 dwc3_prepare_one_trb_sg(dep, req); 1186 1187 if (!dwc3_calc_trbs_left(dep)) 1188 return; 1189 } 1190 1191 list_for_each_entry_safe(req, n, &dep->pending_list, list) { 1192 struct dwc3 *dwc = dep->dwc; 1193 int ret; 1194 1195 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, 1196 dep->direction); 1197 if (ret) 1198 return; 1199 1200 req->sg = req->request.sg; 1201 req->start_sg = req->sg; 1202 req->num_queued_sgs = 0; 1203 req->num_pending_sgs = req->request.num_mapped_sgs; 1204 1205 if (req->num_pending_sgs > 0) 1206 dwc3_prepare_one_trb_sg(dep, req); 1207 else 1208 dwc3_prepare_one_trb_linear(dep, req); 1209 1210 if (!dwc3_calc_trbs_left(dep)) 1211 return; 1212 } 1213 } 1214 1215 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) 1216 { 1217 struct dwc3_gadget_ep_cmd_params params; 1218 struct dwc3_request *req; 1219 int starting; 1220 int ret; 1221 u32 cmd; 1222 1223 if (!dwc3_calc_trbs_left(dep)) 1224 return 0; 1225 1226 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); 1227 1228 dwc3_prepare_trbs(dep); 1229 req = next_request(&dep->started_list); 1230 if (!req) { 1231 dep->flags |= DWC3_EP_PENDING_REQUEST; 1232 return 0; 1233 } 1234 1235 memset(¶ms, 0, sizeof(params)); 1236 1237 if (starting) { 1238 params.param0 = upper_32_bits(req->trb_dma); 1239 params.param1 = lower_32_bits(req->trb_dma); 1240 cmd = DWC3_DEPCMD_STARTTRANSFER; 1241 1242 if (dep->stream_capable) 1243 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); 1244 1245 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) 1246 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); 1247 } else { 1248 cmd = DWC3_DEPCMD_UPDATETRANSFER | 1249 DWC3_DEPCMD_PARAM(dep->resource_index); 1250 } 1251 1252 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1253 if (ret < 0) { 1254 /* 1255 * FIXME we need to iterate over the list of requests 1256 * here and stop, unmap, free and del each of the linked 1257 * requests instead of what we do now. 1258 */ 1259 if (req->trb) 1260 memset(req->trb, 0, sizeof(struct dwc3_trb)); 1261 dwc3_gadget_del_and_unmap_request(dep, req, ret); 1262 return ret; 1263 } 1264 1265 return 0; 1266 } 1267 1268 static int __dwc3_gadget_get_frame(struct dwc3 *dwc) 1269 { 1270 u32 reg; 1271 1272 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1273 return DWC3_DSTS_SOFFN(reg); 1274 } 1275 1276 /** 1277 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number 1278 * @dep: isoc endpoint 1279 * 1280 * This function tests for the correct combination of BIT[15:14] from the 16-bit 1281 * microframe number reported by the XferNotReady event for the future frame 1282 * number to start the isoc transfer. 1283 * 1284 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed 1285 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the 1286 * XferNotReady event are invalid. The driver uses this number to schedule the 1287 * isochronous transfer and passes it to the START TRANSFER command. Because 1288 * this number is invalid, the command may fail. If BIT[15:14] matches the 1289 * internal 16-bit microframe, the START TRANSFER command will pass and the 1290 * transfer will start at the scheduled time, if it is off by 1, the command 1291 * will still pass, but the transfer will start 2 seconds in the future. For all 1292 * other conditions, the START TRANSFER command will fail with bus-expiry. 1293 * 1294 * In order to workaround this issue, we can test for the correct combination of 1295 * BIT[15:14] by sending START TRANSFER commands with different values of 1296 * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart 1297 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. 1298 * As the result, within the 4 possible combinations for BIT[15:14], there will 1299 * be 2 successful and 2 failure START COMMAND status. One of the 2 successful 1300 * command status will result in a 2-second delay start. The smaller BIT[15:14] 1301 * value is the correct combination. 1302 * 1303 * Since there are only 4 outcomes and the results are ordered, we can simply 1304 * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to 1305 * deduce the smaller successful combination. 1306 * 1307 * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 1308 * of BIT[15:14]. The correct combination is as follow: 1309 * 1310 * if test0 fails and test1 passes, BIT[15:14] is 'b01 1311 * if test0 fails and test1 fails, BIT[15:14] is 'b10 1312 * if test0 passes and test1 fails, BIT[15:14] is 'b11 1313 * if test0 passes and test1 passes, BIT[15:14] is 'b00 1314 * 1315 * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN 1316 * endpoints. 1317 */ 1318 static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) 1319 { 1320 int cmd_status = 0; 1321 bool test0; 1322 bool test1; 1323 1324 while (dep->combo_num < 2) { 1325 struct dwc3_gadget_ep_cmd_params params; 1326 u32 test_frame_number; 1327 u32 cmd; 1328 1329 /* 1330 * Check if we can start isoc transfer on the next interval or 1331 * 4 uframes in the future with BIT[15:14] as dep->combo_num 1332 */ 1333 test_frame_number = dep->frame_number & 0x3fff; 1334 test_frame_number |= dep->combo_num << 14; 1335 test_frame_number += max_t(u32, 4, dep->interval); 1336 1337 params.param0 = upper_32_bits(dep->dwc->bounce_addr); 1338 params.param1 = lower_32_bits(dep->dwc->bounce_addr); 1339 1340 cmd = DWC3_DEPCMD_STARTTRANSFER; 1341 cmd |= DWC3_DEPCMD_PARAM(test_frame_number); 1342 cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 1343 1344 /* Redo if some other failure beside bus-expiry is received */ 1345 if (cmd_status && cmd_status != -EAGAIN) { 1346 dep->start_cmd_status = 0; 1347 dep->combo_num = 0; 1348 return 0; 1349 } 1350 1351 /* Store the first test status */ 1352 if (dep->combo_num == 0) 1353 dep->start_cmd_status = cmd_status; 1354 1355 dep->combo_num++; 1356 1357 /* 1358 * End the transfer if the START_TRANSFER command is successful 1359 * to wait for the next XferNotReady to test the command again 1360 */ 1361 if (cmd_status == 0) { 1362 dwc3_stop_active_transfer(dep, true); 1363 return 0; 1364 } 1365 } 1366 1367 /* test0 and test1 are both completed at this point */ 1368 test0 = (dep->start_cmd_status == 0); 1369 test1 = (cmd_status == 0); 1370 1371 if (!test0 && test1) 1372 dep->combo_num = 1; 1373 else if (!test0 && !test1) 1374 dep->combo_num = 2; 1375 else if (test0 && !test1) 1376 dep->combo_num = 3; 1377 else if (test0 && test1) 1378 dep->combo_num = 0; 1379 1380 dep->frame_number &= 0x3fff; 1381 dep->frame_number |= dep->combo_num << 14; 1382 dep->frame_number += max_t(u32, 4, dep->interval); 1383 1384 /* Reinitialize test variables */ 1385 dep->start_cmd_status = 0; 1386 dep->combo_num = 0; 1387 1388 return __dwc3_gadget_kick_transfer(dep); 1389 } 1390 1391 static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) 1392 { 1393 struct dwc3 *dwc = dep->dwc; 1394 int ret; 1395 int i; 1396 1397 if (list_empty(&dep->pending_list)) { 1398 dep->flags |= DWC3_EP_PENDING_REQUEST; 1399 return -EAGAIN; 1400 } 1401 1402 if (!dwc->dis_start_transfer_quirk && dwc3_is_usb31(dwc) && 1403 (dwc->revision <= DWC3_USB31_REVISION_160A || 1404 (dwc->revision == DWC3_USB31_REVISION_170A && 1405 dwc->version_type >= DWC31_VERSIONTYPE_EA01 && 1406 dwc->version_type <= DWC31_VERSIONTYPE_EA06))) { 1407 1408 if (dwc->gadget.speed <= USB_SPEED_HIGH && dep->direction) 1409 return dwc3_gadget_start_isoc_quirk(dep); 1410 } 1411 1412 for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { 1413 dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1); 1414 1415 ret = __dwc3_gadget_kick_transfer(dep); 1416 if (ret != -EAGAIN) 1417 break; 1418 } 1419 1420 return ret; 1421 } 1422 1423 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) 1424 { 1425 struct dwc3 *dwc = dep->dwc; 1426 1427 if (!dep->endpoint.desc) { 1428 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n", 1429 dep->name); 1430 return -ESHUTDOWN; 1431 } 1432 1433 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", 1434 &req->request, req->dep->name)) 1435 return -EINVAL; 1436 1437 pm_runtime_get(dwc->dev); 1438 1439 req->request.actual = 0; 1440 req->request.status = -EINPROGRESS; 1441 1442 trace_dwc3_ep_queue(req); 1443 1444 list_add_tail(&req->list, &dep->pending_list); 1445 1446 /* 1447 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must 1448 * wait for a XferNotReady event so we will know what's the current 1449 * (micro-)frame number. 1450 * 1451 * Without this trick, we are very, very likely gonna get Bus Expiry 1452 * errors which will force us issue EndTransfer command. 1453 */ 1454 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 1455 if (!(dep->flags & DWC3_EP_PENDING_REQUEST) && 1456 !(dep->flags & DWC3_EP_TRANSFER_STARTED)) 1457 return 0; 1458 1459 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) { 1460 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { 1461 return __dwc3_gadget_start_isoc(dep); 1462 } 1463 } 1464 } 1465 1466 return __dwc3_gadget_kick_transfer(dep); 1467 } 1468 1469 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, 1470 gfp_t gfp_flags) 1471 { 1472 struct dwc3_request *req = to_dwc3_request(request); 1473 struct dwc3_ep *dep = to_dwc3_ep(ep); 1474 struct dwc3 *dwc = dep->dwc; 1475 1476 unsigned long flags; 1477 1478 int ret; 1479 1480 spin_lock_irqsave(&dwc->lock, flags); 1481 ret = __dwc3_gadget_ep_queue(dep, req); 1482 spin_unlock_irqrestore(&dwc->lock, flags); 1483 1484 return ret; 1485 } 1486 1487 static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) 1488 { 1489 int i; 1490 1491 /* 1492 * If request was already started, this means we had to 1493 * stop the transfer. With that we also need to ignore 1494 * all TRBs used by the request, however TRBs can only 1495 * be modified after completion of END_TRANSFER 1496 * command. So what we do here is that we wait for 1497 * END_TRANSFER completion and only after that, we jump 1498 * over TRBs by clearing HWO and incrementing dequeue 1499 * pointer. 1500 */ 1501 for (i = 0; i < req->num_trbs; i++) { 1502 struct dwc3_trb *trb; 1503 1504 trb = req->trb + i; 1505 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 1506 dwc3_ep_inc_deq(dep); 1507 } 1508 } 1509 1510 static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) 1511 { 1512 struct dwc3_request *req; 1513 struct dwc3_request *tmp; 1514 1515 list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) { 1516 dwc3_gadget_ep_skip_trbs(dep, req); 1517 dwc3_gadget_giveback(dep, req, -ECONNRESET); 1518 } 1519 } 1520 1521 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, 1522 struct usb_request *request) 1523 { 1524 struct dwc3_request *req = to_dwc3_request(request); 1525 struct dwc3_request *r = NULL; 1526 1527 struct dwc3_ep *dep = to_dwc3_ep(ep); 1528 struct dwc3 *dwc = dep->dwc; 1529 1530 unsigned long flags; 1531 int ret = 0; 1532 1533 trace_dwc3_ep_dequeue(req); 1534 1535 spin_lock_irqsave(&dwc->lock, flags); 1536 1537 list_for_each_entry(r, &dep->pending_list, list) { 1538 if (r == req) 1539 break; 1540 } 1541 1542 if (r != req) { 1543 list_for_each_entry(r, &dep->started_list, list) { 1544 if (r == req) 1545 break; 1546 } 1547 if (r == req) { 1548 /* wait until it is processed */ 1549 dwc3_stop_active_transfer(dep, true); 1550 1551 if (!r->trb) 1552 goto out0; 1553 1554 dwc3_gadget_move_cancelled_request(req); 1555 goto out0; 1556 } 1557 dev_err(dwc->dev, "request %pK was not queued to %s\n", 1558 request, ep->name); 1559 ret = -EINVAL; 1560 goto out0; 1561 } 1562 1563 dwc3_gadget_giveback(dep, req, -ECONNRESET); 1564 1565 out0: 1566 spin_unlock_irqrestore(&dwc->lock, flags); 1567 1568 return ret; 1569 } 1570 1571 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) 1572 { 1573 struct dwc3_gadget_ep_cmd_params params; 1574 struct dwc3 *dwc = dep->dwc; 1575 int ret; 1576 1577 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 1578 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); 1579 return -EINVAL; 1580 } 1581 1582 memset(¶ms, 0x00, sizeof(params)); 1583 1584 if (value) { 1585 struct dwc3_trb *trb; 1586 1587 unsigned transfer_in_flight; 1588 unsigned started; 1589 1590 if (dep->number > 1) 1591 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); 1592 else 1593 trb = &dwc->ep0_trb[dep->trb_enqueue]; 1594 1595 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; 1596 started = !list_empty(&dep->started_list); 1597 1598 if (!protocol && ((dep->direction && transfer_in_flight) || 1599 (!dep->direction && started))) { 1600 return -EAGAIN; 1601 } 1602 1603 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, 1604 ¶ms); 1605 if (ret) 1606 dev_err(dwc->dev, "failed to set STALL on %s\n", 1607 dep->name); 1608 else 1609 dep->flags |= DWC3_EP_STALL; 1610 } else { 1611 1612 ret = dwc3_send_clear_stall_ep_cmd(dep); 1613 if (ret) 1614 dev_err(dwc->dev, "failed to clear STALL on %s\n", 1615 dep->name); 1616 else 1617 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); 1618 } 1619 1620 return ret; 1621 } 1622 1623 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) 1624 { 1625 struct dwc3_ep *dep = to_dwc3_ep(ep); 1626 struct dwc3 *dwc = dep->dwc; 1627 1628 unsigned long flags; 1629 1630 int ret; 1631 1632 spin_lock_irqsave(&dwc->lock, flags); 1633 ret = __dwc3_gadget_ep_set_halt(dep, value, false); 1634 spin_unlock_irqrestore(&dwc->lock, flags); 1635 1636 return ret; 1637 } 1638 1639 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) 1640 { 1641 struct dwc3_ep *dep = to_dwc3_ep(ep); 1642 struct dwc3 *dwc = dep->dwc; 1643 unsigned long flags; 1644 int ret; 1645 1646 spin_lock_irqsave(&dwc->lock, flags); 1647 dep->flags |= DWC3_EP_WEDGE; 1648 1649 if (dep->number == 0 || dep->number == 1) 1650 ret = __dwc3_gadget_ep0_set_halt(ep, 1); 1651 else 1652 ret = __dwc3_gadget_ep_set_halt(dep, 1, false); 1653 spin_unlock_irqrestore(&dwc->lock, flags); 1654 1655 return ret; 1656 } 1657 1658 /* -------------------------------------------------------------------------- */ 1659 1660 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { 1661 .bLength = USB_DT_ENDPOINT_SIZE, 1662 .bDescriptorType = USB_DT_ENDPOINT, 1663 .bmAttributes = USB_ENDPOINT_XFER_CONTROL, 1664 }; 1665 1666 static const struct usb_ep_ops dwc3_gadget_ep0_ops = { 1667 .enable = dwc3_gadget_ep0_enable, 1668 .disable = dwc3_gadget_ep0_disable, 1669 .alloc_request = dwc3_gadget_ep_alloc_request, 1670 .free_request = dwc3_gadget_ep_free_request, 1671 .queue = dwc3_gadget_ep0_queue, 1672 .dequeue = dwc3_gadget_ep_dequeue, 1673 .set_halt = dwc3_gadget_ep0_set_halt, 1674 .set_wedge = dwc3_gadget_ep_set_wedge, 1675 }; 1676 1677 static const struct usb_ep_ops dwc3_gadget_ep_ops = { 1678 .enable = dwc3_gadget_ep_enable, 1679 .disable = dwc3_gadget_ep_disable, 1680 .alloc_request = dwc3_gadget_ep_alloc_request, 1681 .free_request = dwc3_gadget_ep_free_request, 1682 .queue = dwc3_gadget_ep_queue, 1683 .dequeue = dwc3_gadget_ep_dequeue, 1684 .set_halt = dwc3_gadget_ep_set_halt, 1685 .set_wedge = dwc3_gadget_ep_set_wedge, 1686 }; 1687 1688 /* -------------------------------------------------------------------------- */ 1689 1690 static int dwc3_gadget_get_frame(struct usb_gadget *g) 1691 { 1692 struct dwc3 *dwc = gadget_to_dwc(g); 1693 1694 return __dwc3_gadget_get_frame(dwc); 1695 } 1696 1697 static int __dwc3_gadget_wakeup(struct dwc3 *dwc) 1698 { 1699 int retries; 1700 1701 int ret; 1702 u32 reg; 1703 1704 u8 link_state; 1705 u8 speed; 1706 1707 /* 1708 * According to the Databook Remote wakeup request should 1709 * be issued only when the device is in early suspend state. 1710 * 1711 * We can check that via USB Link State bits in DSTS register. 1712 */ 1713 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1714 1715 speed = reg & DWC3_DSTS_CONNECTSPD; 1716 if ((speed == DWC3_DSTS_SUPERSPEED) || 1717 (speed == DWC3_DSTS_SUPERSPEED_PLUS)) 1718 return 0; 1719 1720 link_state = DWC3_DSTS_USBLNKST(reg); 1721 1722 switch (link_state) { 1723 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ 1724 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ 1725 break; 1726 default: 1727 return -EINVAL; 1728 } 1729 1730 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); 1731 if (ret < 0) { 1732 dev_err(dwc->dev, "failed to put link in Recovery\n"); 1733 return ret; 1734 } 1735 1736 /* Recent versions do this automatically */ 1737 if (dwc->revision < DWC3_REVISION_194A) { 1738 /* write zeroes to Link Change Request */ 1739 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 1740 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 1741 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 1742 } 1743 1744 /* poll until Link State changes to ON */ 1745 retries = 20000; 1746 1747 while (retries--) { 1748 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1749 1750 /* in HS, means ON */ 1751 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) 1752 break; 1753 } 1754 1755 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { 1756 dev_err(dwc->dev, "failed to send remote wakeup\n"); 1757 return -EINVAL; 1758 } 1759 1760 return 0; 1761 } 1762 1763 static int dwc3_gadget_wakeup(struct usb_gadget *g) 1764 { 1765 struct dwc3 *dwc = gadget_to_dwc(g); 1766 unsigned long flags; 1767 int ret; 1768 1769 spin_lock_irqsave(&dwc->lock, flags); 1770 ret = __dwc3_gadget_wakeup(dwc); 1771 spin_unlock_irqrestore(&dwc->lock, flags); 1772 1773 return ret; 1774 } 1775 1776 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, 1777 int is_selfpowered) 1778 { 1779 struct dwc3 *dwc = gadget_to_dwc(g); 1780 unsigned long flags; 1781 1782 spin_lock_irqsave(&dwc->lock, flags); 1783 g->is_selfpowered = !!is_selfpowered; 1784 spin_unlock_irqrestore(&dwc->lock, flags); 1785 1786 return 0; 1787 } 1788 1789 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) 1790 { 1791 u32 reg; 1792 u32 timeout = 500; 1793 1794 if (pm_runtime_suspended(dwc->dev)) 1795 return 0; 1796 1797 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 1798 if (is_on) { 1799 if (dwc->revision <= DWC3_REVISION_187A) { 1800 reg &= ~DWC3_DCTL_TRGTULST_MASK; 1801 reg |= DWC3_DCTL_TRGTULST_RX_DET; 1802 } 1803 1804 if (dwc->revision >= DWC3_REVISION_194A) 1805 reg &= ~DWC3_DCTL_KEEP_CONNECT; 1806 reg |= DWC3_DCTL_RUN_STOP; 1807 1808 if (dwc->has_hibernation) 1809 reg |= DWC3_DCTL_KEEP_CONNECT; 1810 1811 dwc->pullups_connected = true; 1812 } else { 1813 reg &= ~DWC3_DCTL_RUN_STOP; 1814 1815 if (dwc->has_hibernation && !suspend) 1816 reg &= ~DWC3_DCTL_KEEP_CONNECT; 1817 1818 dwc->pullups_connected = false; 1819 } 1820 1821 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 1822 1823 do { 1824 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 1825 reg &= DWC3_DSTS_DEVCTRLHLT; 1826 } while (--timeout && !(!is_on ^ !reg)); 1827 1828 if (!timeout) 1829 return -ETIMEDOUT; 1830 1831 return 0; 1832 } 1833 1834 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) 1835 { 1836 struct dwc3 *dwc = gadget_to_dwc(g); 1837 unsigned long flags; 1838 int ret; 1839 1840 is_on = !!is_on; 1841 1842 /* 1843 * Per databook, when we want to stop the gadget, if a control transfer 1844 * is still in process, complete it and get the core into setup phase. 1845 */ 1846 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) { 1847 reinit_completion(&dwc->ep0_in_setup); 1848 1849 ret = wait_for_completion_timeout(&dwc->ep0_in_setup, 1850 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); 1851 if (ret == 0) { 1852 dev_err(dwc->dev, "timed out waiting for SETUP phase\n"); 1853 return -ETIMEDOUT; 1854 } 1855 } 1856 1857 spin_lock_irqsave(&dwc->lock, flags); 1858 ret = dwc3_gadget_run_stop(dwc, is_on, false); 1859 spin_unlock_irqrestore(&dwc->lock, flags); 1860 1861 return ret; 1862 } 1863 1864 static void dwc3_gadget_enable_irq(struct dwc3 *dwc) 1865 { 1866 u32 reg; 1867 1868 /* Enable all but Start and End of Frame IRQs */ 1869 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | 1870 DWC3_DEVTEN_EVNTOVERFLOWEN | 1871 DWC3_DEVTEN_CMDCMPLTEN | 1872 DWC3_DEVTEN_ERRTICERREN | 1873 DWC3_DEVTEN_WKUPEVTEN | 1874 DWC3_DEVTEN_CONNECTDONEEN | 1875 DWC3_DEVTEN_USBRSTEN | 1876 DWC3_DEVTEN_DISCONNEVTEN); 1877 1878 if (dwc->revision < DWC3_REVISION_250A) 1879 reg |= DWC3_DEVTEN_ULSTCNGEN; 1880 1881 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); 1882 } 1883 1884 static void dwc3_gadget_disable_irq(struct dwc3 *dwc) 1885 { 1886 /* mask all interrupts */ 1887 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); 1888 } 1889 1890 static irqreturn_t dwc3_interrupt(int irq, void *_dwc); 1891 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); 1892 1893 /** 1894 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG 1895 * @dwc: pointer to our context structure 1896 * 1897 * The following looks like complex but it's actually very simple. In order to 1898 * calculate the number of packets we can burst at once on OUT transfers, we're 1899 * gonna use RxFIFO size. 1900 * 1901 * To calculate RxFIFO size we need two numbers: 1902 * MDWIDTH = size, in bits, of the internal memory bus 1903 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) 1904 * 1905 * Given these two numbers, the formula is simple: 1906 * 1907 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; 1908 * 1909 * 24 bytes is for 3x SETUP packets 1910 * 16 bytes is a clock domain crossing tolerance 1911 * 1912 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; 1913 */ 1914 static void dwc3_gadget_setup_nump(struct dwc3 *dwc) 1915 { 1916 u32 ram2_depth; 1917 u32 mdwidth; 1918 u32 nump; 1919 u32 reg; 1920 1921 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); 1922 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0); 1923 1924 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; 1925 nump = min_t(u32, nump, 16); 1926 1927 /* update NumP */ 1928 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 1929 reg &= ~DWC3_DCFG_NUMP_MASK; 1930 reg |= nump << DWC3_DCFG_NUMP_SHIFT; 1931 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 1932 } 1933 1934 static int __dwc3_gadget_start(struct dwc3 *dwc) 1935 { 1936 struct dwc3_ep *dep; 1937 int ret = 0; 1938 u32 reg; 1939 1940 /* 1941 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if 1942 * the core supports IMOD, disable it. 1943 */ 1944 if (dwc->imod_interval) { 1945 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 1946 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 1947 } else if (dwc3_has_imod(dwc)) { 1948 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); 1949 } 1950 1951 /* 1952 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP 1953 * field instead of letting dwc3 itself calculate that automatically. 1954 * 1955 * This way, we maximize the chances that we'll be able to get several 1956 * bursts of data without going through any sort of endpoint throttling. 1957 */ 1958 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); 1959 if (dwc3_is_usb31(dwc)) 1960 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; 1961 else 1962 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; 1963 1964 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); 1965 1966 dwc3_gadget_setup_nump(dwc); 1967 1968 /* Start with SuperSpeed Default */ 1969 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 1970 1971 dep = dwc->eps[0]; 1972 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 1973 if (ret) { 1974 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 1975 goto err0; 1976 } 1977 1978 dep = dwc->eps[1]; 1979 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); 1980 if (ret) { 1981 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 1982 goto err1; 1983 } 1984 1985 /* begin to receive SETUP packets */ 1986 dwc->ep0state = EP0_SETUP_PHASE; 1987 dwc3_ep0_out_start(dwc); 1988 1989 dwc3_gadget_enable_irq(dwc); 1990 1991 return 0; 1992 1993 err1: 1994 __dwc3_gadget_ep_disable(dwc->eps[0]); 1995 1996 err0: 1997 return ret; 1998 } 1999 2000 static int dwc3_gadget_start(struct usb_gadget *g, 2001 struct usb_gadget_driver *driver) 2002 { 2003 struct dwc3 *dwc = gadget_to_dwc(g); 2004 unsigned long flags; 2005 int ret = 0; 2006 int irq; 2007 2008 irq = dwc->irq_gadget; 2009 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, 2010 IRQF_SHARED, "dwc3", dwc->ev_buf); 2011 if (ret) { 2012 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", 2013 irq, ret); 2014 goto err0; 2015 } 2016 2017 spin_lock_irqsave(&dwc->lock, flags); 2018 if (dwc->gadget_driver) { 2019 dev_err(dwc->dev, "%s is already bound to %s\n", 2020 dwc->gadget.name, 2021 dwc->gadget_driver->driver.name); 2022 ret = -EBUSY; 2023 goto err1; 2024 } 2025 2026 dwc->gadget_driver = driver; 2027 2028 if (pm_runtime_active(dwc->dev)) 2029 __dwc3_gadget_start(dwc); 2030 2031 spin_unlock_irqrestore(&dwc->lock, flags); 2032 2033 return 0; 2034 2035 err1: 2036 spin_unlock_irqrestore(&dwc->lock, flags); 2037 free_irq(irq, dwc); 2038 2039 err0: 2040 return ret; 2041 } 2042 2043 static void __dwc3_gadget_stop(struct dwc3 *dwc) 2044 { 2045 dwc3_gadget_disable_irq(dwc); 2046 __dwc3_gadget_ep_disable(dwc->eps[0]); 2047 __dwc3_gadget_ep_disable(dwc->eps[1]); 2048 } 2049 2050 static int dwc3_gadget_stop(struct usb_gadget *g) 2051 { 2052 struct dwc3 *dwc = gadget_to_dwc(g); 2053 unsigned long flags; 2054 2055 spin_lock_irqsave(&dwc->lock, flags); 2056 2057 if (pm_runtime_suspended(dwc->dev)) 2058 goto out; 2059 2060 __dwc3_gadget_stop(dwc); 2061 2062 out: 2063 dwc->gadget_driver = NULL; 2064 spin_unlock_irqrestore(&dwc->lock, flags); 2065 2066 free_irq(dwc->irq_gadget, dwc->ev_buf); 2067 2068 return 0; 2069 } 2070 2071 static void dwc3_gadget_set_speed(struct usb_gadget *g, 2072 enum usb_device_speed speed) 2073 { 2074 struct dwc3 *dwc = gadget_to_dwc(g); 2075 unsigned long flags; 2076 u32 reg; 2077 2078 spin_lock_irqsave(&dwc->lock, flags); 2079 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2080 reg &= ~(DWC3_DCFG_SPEED_MASK); 2081 2082 /* 2083 * WORKAROUND: DWC3 revision < 2.20a have an issue 2084 * which would cause metastability state on Run/Stop 2085 * bit if we try to force the IP to USB2-only mode. 2086 * 2087 * Because of that, we cannot configure the IP to any 2088 * speed other than the SuperSpeed 2089 * 2090 * Refers to: 2091 * 2092 * STAR#9000525659: Clock Domain Crossing on DCTL in 2093 * USB 2.0 Mode 2094 */ 2095 if (dwc->revision < DWC3_REVISION_220A && 2096 !dwc->dis_metastability_quirk) { 2097 reg |= DWC3_DCFG_SUPERSPEED; 2098 } else { 2099 switch (speed) { 2100 case USB_SPEED_LOW: 2101 reg |= DWC3_DCFG_LOWSPEED; 2102 break; 2103 case USB_SPEED_FULL: 2104 reg |= DWC3_DCFG_FULLSPEED; 2105 break; 2106 case USB_SPEED_HIGH: 2107 reg |= DWC3_DCFG_HIGHSPEED; 2108 break; 2109 case USB_SPEED_SUPER: 2110 reg |= DWC3_DCFG_SUPERSPEED; 2111 break; 2112 case USB_SPEED_SUPER_PLUS: 2113 if (dwc3_is_usb31(dwc)) 2114 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2115 else 2116 reg |= DWC3_DCFG_SUPERSPEED; 2117 break; 2118 default: 2119 dev_err(dwc->dev, "invalid speed (%d)\n", speed); 2120 2121 if (dwc->revision & DWC3_REVISION_IS_DWC31) 2122 reg |= DWC3_DCFG_SUPERSPEED_PLUS; 2123 else 2124 reg |= DWC3_DCFG_SUPERSPEED; 2125 } 2126 } 2127 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2128 2129 spin_unlock_irqrestore(&dwc->lock, flags); 2130 } 2131 2132 static const struct usb_gadget_ops dwc3_gadget_ops = { 2133 .get_frame = dwc3_gadget_get_frame, 2134 .wakeup = dwc3_gadget_wakeup, 2135 .set_selfpowered = dwc3_gadget_set_selfpowered, 2136 .pullup = dwc3_gadget_pullup, 2137 .udc_start = dwc3_gadget_start, 2138 .udc_stop = dwc3_gadget_stop, 2139 .udc_set_speed = dwc3_gadget_set_speed, 2140 }; 2141 2142 /* -------------------------------------------------------------------------- */ 2143 2144 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) 2145 { 2146 struct dwc3 *dwc = dep->dwc; 2147 2148 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); 2149 dep->endpoint.maxburst = 1; 2150 dep->endpoint.ops = &dwc3_gadget_ep0_ops; 2151 if (!dep->direction) 2152 dwc->gadget.ep0 = &dep->endpoint; 2153 2154 dep->endpoint.caps.type_control = true; 2155 2156 return 0; 2157 } 2158 2159 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) 2160 { 2161 struct dwc3 *dwc = dep->dwc; 2162 int mdwidth; 2163 int kbytes; 2164 int size; 2165 2166 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); 2167 /* MDWIDTH is represented in bits, we need it in bytes */ 2168 mdwidth /= 8; 2169 2170 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); 2171 if (dwc3_is_usb31(dwc)) 2172 size = DWC31_GTXFIFOSIZ_TXFDEF(size); 2173 else 2174 size = DWC3_GTXFIFOSIZ_TXFDEF(size); 2175 2176 /* FIFO Depth is in MDWDITH bytes. Multiply */ 2177 size *= mdwidth; 2178 2179 kbytes = size / 1024; 2180 if (kbytes == 0) 2181 kbytes = 1; 2182 2183 /* 2184 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for 2185 * internal overhead. We don't really know how these are used, 2186 * but documentation say it exists. 2187 */ 2188 size -= mdwidth * (kbytes + 1); 2189 size /= kbytes; 2190 2191 usb_ep_set_maxpacket_limit(&dep->endpoint, size); 2192 2193 dep->endpoint.max_streams = 15; 2194 dep->endpoint.ops = &dwc3_gadget_ep_ops; 2195 list_add_tail(&dep->endpoint.ep_list, 2196 &dwc->gadget.ep_list); 2197 dep->endpoint.caps.type_iso = true; 2198 dep->endpoint.caps.type_bulk = true; 2199 dep->endpoint.caps.type_int = true; 2200 2201 return dwc3_alloc_trb_pool(dep); 2202 } 2203 2204 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) 2205 { 2206 struct dwc3 *dwc = dep->dwc; 2207 2208 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); 2209 dep->endpoint.max_streams = 15; 2210 dep->endpoint.ops = &dwc3_gadget_ep_ops; 2211 list_add_tail(&dep->endpoint.ep_list, 2212 &dwc->gadget.ep_list); 2213 dep->endpoint.caps.type_iso = true; 2214 dep->endpoint.caps.type_bulk = true; 2215 dep->endpoint.caps.type_int = true; 2216 2217 return dwc3_alloc_trb_pool(dep); 2218 } 2219 2220 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) 2221 { 2222 struct dwc3_ep *dep; 2223 bool direction = epnum & 1; 2224 int ret; 2225 u8 num = epnum >> 1; 2226 2227 dep = kzalloc(sizeof(*dep), GFP_KERNEL); 2228 if (!dep) 2229 return -ENOMEM; 2230 2231 dep->dwc = dwc; 2232 dep->number = epnum; 2233 dep->direction = direction; 2234 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); 2235 dwc->eps[epnum] = dep; 2236 dep->combo_num = 0; 2237 dep->start_cmd_status = 0; 2238 2239 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, 2240 direction ? "in" : "out"); 2241 2242 dep->endpoint.name = dep->name; 2243 2244 if (!(dep->number > 1)) { 2245 dep->endpoint.desc = &dwc3_gadget_ep0_desc; 2246 dep->endpoint.comp_desc = NULL; 2247 } 2248 2249 spin_lock_init(&dep->lock); 2250 2251 if (num == 0) 2252 ret = dwc3_gadget_init_control_endpoint(dep); 2253 else if (direction) 2254 ret = dwc3_gadget_init_in_endpoint(dep); 2255 else 2256 ret = dwc3_gadget_init_out_endpoint(dep); 2257 2258 if (ret) 2259 return ret; 2260 2261 dep->endpoint.caps.dir_in = direction; 2262 dep->endpoint.caps.dir_out = !direction; 2263 2264 INIT_LIST_HEAD(&dep->pending_list); 2265 INIT_LIST_HEAD(&dep->started_list); 2266 INIT_LIST_HEAD(&dep->cancelled_list); 2267 2268 return 0; 2269 } 2270 2271 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) 2272 { 2273 u8 epnum; 2274 2275 INIT_LIST_HEAD(&dwc->gadget.ep_list); 2276 2277 for (epnum = 0; epnum < total; epnum++) { 2278 int ret; 2279 2280 ret = dwc3_gadget_init_endpoint(dwc, epnum); 2281 if (ret) 2282 return ret; 2283 } 2284 2285 return 0; 2286 } 2287 2288 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) 2289 { 2290 struct dwc3_ep *dep; 2291 u8 epnum; 2292 2293 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 2294 dep = dwc->eps[epnum]; 2295 if (!dep) 2296 continue; 2297 /* 2298 * Physical endpoints 0 and 1 are special; they form the 2299 * bi-directional USB endpoint 0. 2300 * 2301 * For those two physical endpoints, we don't allocate a TRB 2302 * pool nor do we add them the endpoints list. Due to that, we 2303 * shouldn't do these two operations otherwise we would end up 2304 * with all sorts of bugs when removing dwc3.ko. 2305 */ 2306 if (epnum != 0 && epnum != 1) { 2307 dwc3_free_trb_pool(dep); 2308 list_del(&dep->endpoint.ep_list); 2309 } 2310 2311 kfree(dep); 2312 } 2313 } 2314 2315 /* -------------------------------------------------------------------------- */ 2316 2317 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, 2318 struct dwc3_request *req, struct dwc3_trb *trb, 2319 const struct dwc3_event_depevt *event, int status, int chain) 2320 { 2321 unsigned int count; 2322 2323 dwc3_ep_inc_deq(dep); 2324 2325 trace_dwc3_complete_trb(dep, trb); 2326 req->num_trbs--; 2327 2328 /* 2329 * If we're in the middle of series of chained TRBs and we 2330 * receive a short transfer along the way, DWC3 will skip 2331 * through all TRBs including the last TRB in the chain (the 2332 * where CHN bit is zero. DWC3 will also avoid clearing HWO 2333 * bit and SW has to do it manually. 2334 * 2335 * We're going to do that here to avoid problems of HW trying 2336 * to use bogus TRBs for transfers. 2337 */ 2338 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) 2339 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 2340 2341 /* 2342 * For isochronous transfers, the first TRB in a service interval must 2343 * have the Isoc-First type. Track and report its interval frame number. 2344 */ 2345 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 2346 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { 2347 unsigned int frame_number; 2348 2349 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); 2350 frame_number &= ~(dep->interval - 1); 2351 req->request.frame_number = frame_number; 2352 } 2353 2354 /* 2355 * If we're dealing with unaligned size OUT transfer, we will be left 2356 * with one TRB pending in the ring. We need to manually clear HWO bit 2357 * from that TRB. 2358 */ 2359 2360 if (req->needs_extra_trb && !(trb->ctrl & DWC3_TRB_CTRL_CHN)) { 2361 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; 2362 return 1; 2363 } 2364 2365 count = trb->size & DWC3_TRB_SIZE_MASK; 2366 req->remaining += count; 2367 2368 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) 2369 return 1; 2370 2371 if (event->status & DEPEVT_STATUS_SHORT && !chain) 2372 return 1; 2373 2374 if (event->status & DEPEVT_STATUS_IOC) 2375 return 1; 2376 2377 return 0; 2378 } 2379 2380 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, 2381 struct dwc3_request *req, const struct dwc3_event_depevt *event, 2382 int status) 2383 { 2384 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; 2385 struct scatterlist *sg = req->sg; 2386 struct scatterlist *s; 2387 unsigned int pending = req->num_pending_sgs; 2388 unsigned int i; 2389 int ret = 0; 2390 2391 for_each_sg(sg, s, pending, i) { 2392 trb = &dep->trb_pool[dep->trb_dequeue]; 2393 2394 if (trb->ctrl & DWC3_TRB_CTRL_HWO) 2395 break; 2396 2397 req->sg = sg_next(s); 2398 req->num_pending_sgs--; 2399 2400 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, 2401 trb, event, status, true); 2402 if (ret) 2403 break; 2404 } 2405 2406 return ret; 2407 } 2408 2409 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, 2410 struct dwc3_request *req, const struct dwc3_event_depevt *event, 2411 int status) 2412 { 2413 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; 2414 2415 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, 2416 event, status, false); 2417 } 2418 2419 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) 2420 { 2421 return req->request.actual == req->request.length; 2422 } 2423 2424 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, 2425 const struct dwc3_event_depevt *event, 2426 struct dwc3_request *req, int status) 2427 { 2428 int ret; 2429 2430 if (req->num_pending_sgs) 2431 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, 2432 status); 2433 else 2434 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, 2435 status); 2436 2437 if (req->needs_extra_trb) { 2438 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, 2439 status); 2440 req->needs_extra_trb = false; 2441 } 2442 2443 req->request.actual = req->request.length - req->remaining; 2444 2445 if (!dwc3_gadget_ep_request_completed(req) && 2446 req->num_pending_sgs) { 2447 __dwc3_gadget_kick_transfer(dep); 2448 goto out; 2449 } 2450 2451 dwc3_gadget_giveback(dep, req, status); 2452 2453 out: 2454 return ret; 2455 } 2456 2457 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, 2458 const struct dwc3_event_depevt *event, int status) 2459 { 2460 struct dwc3_request *req; 2461 struct dwc3_request *tmp; 2462 2463 list_for_each_entry_safe(req, tmp, &dep->started_list, list) { 2464 int ret; 2465 2466 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, 2467 req, status); 2468 if (ret) 2469 break; 2470 } 2471 } 2472 2473 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, 2474 const struct dwc3_event_depevt *event) 2475 { 2476 dep->frame_number = event->parameters; 2477 } 2478 2479 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, 2480 const struct dwc3_event_depevt *event) 2481 { 2482 struct dwc3 *dwc = dep->dwc; 2483 unsigned status = 0; 2484 bool stop = false; 2485 2486 dwc3_gadget_endpoint_frame_from_event(dep, event); 2487 2488 if (event->status & DEPEVT_STATUS_BUSERR) 2489 status = -ECONNRESET; 2490 2491 if (event->status & DEPEVT_STATUS_MISSED_ISOC) { 2492 status = -EXDEV; 2493 2494 if (list_empty(&dep->started_list)) 2495 stop = true; 2496 } 2497 2498 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); 2499 2500 if (stop) { 2501 dwc3_stop_active_transfer(dep, true); 2502 dep->flags = DWC3_EP_ENABLED; 2503 } 2504 2505 /* 2506 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. 2507 * See dwc3_gadget_linksts_change_interrupt() for 1st half. 2508 */ 2509 if (dwc->revision < DWC3_REVISION_183A) { 2510 u32 reg; 2511 int i; 2512 2513 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { 2514 dep = dwc->eps[i]; 2515 2516 if (!(dep->flags & DWC3_EP_ENABLED)) 2517 continue; 2518 2519 if (!list_empty(&dep->started_list)) 2520 return; 2521 } 2522 2523 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2524 reg |= dwc->u1u2; 2525 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2526 2527 dwc->u1u2 = 0; 2528 } 2529 } 2530 2531 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, 2532 const struct dwc3_event_depevt *event) 2533 { 2534 dwc3_gadget_endpoint_frame_from_event(dep, event); 2535 (void) __dwc3_gadget_start_isoc(dep); 2536 } 2537 2538 static void dwc3_endpoint_interrupt(struct dwc3 *dwc, 2539 const struct dwc3_event_depevt *event) 2540 { 2541 struct dwc3_ep *dep; 2542 u8 epnum = event->endpoint_number; 2543 u8 cmd; 2544 2545 dep = dwc->eps[epnum]; 2546 2547 if (!(dep->flags & DWC3_EP_ENABLED)) { 2548 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) 2549 return; 2550 2551 /* Handle only EPCMDCMPLT when EP disabled */ 2552 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) 2553 return; 2554 } 2555 2556 if (epnum == 0 || epnum == 1) { 2557 dwc3_ep0_interrupt(dwc, event); 2558 return; 2559 } 2560 2561 switch (event->endpoint_event) { 2562 case DWC3_DEPEVT_XFERINPROGRESS: 2563 dwc3_gadget_endpoint_transfer_in_progress(dep, event); 2564 break; 2565 case DWC3_DEPEVT_XFERNOTREADY: 2566 dwc3_gadget_endpoint_transfer_not_ready(dep, event); 2567 break; 2568 case DWC3_DEPEVT_EPCMDCMPLT: 2569 cmd = DEPEVT_PARAMETER_CMD(event->parameters); 2570 2571 if (cmd == DWC3_DEPCMD_ENDTRANSFER) { 2572 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; 2573 dwc3_gadget_ep_cleanup_cancelled_requests(dep); 2574 } 2575 break; 2576 case DWC3_DEPEVT_STREAMEVT: 2577 case DWC3_DEPEVT_XFERCOMPLETE: 2578 case DWC3_DEPEVT_RXTXFIFOEVT: 2579 break; 2580 } 2581 } 2582 2583 static void dwc3_disconnect_gadget(struct dwc3 *dwc) 2584 { 2585 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { 2586 spin_unlock(&dwc->lock); 2587 dwc->gadget_driver->disconnect(&dwc->gadget); 2588 spin_lock(&dwc->lock); 2589 } 2590 } 2591 2592 static void dwc3_suspend_gadget(struct dwc3 *dwc) 2593 { 2594 if (dwc->gadget_driver && dwc->gadget_driver->suspend) { 2595 spin_unlock(&dwc->lock); 2596 dwc->gadget_driver->suspend(&dwc->gadget); 2597 spin_lock(&dwc->lock); 2598 } 2599 } 2600 2601 static void dwc3_resume_gadget(struct dwc3 *dwc) 2602 { 2603 if (dwc->gadget_driver && dwc->gadget_driver->resume) { 2604 spin_unlock(&dwc->lock); 2605 dwc->gadget_driver->resume(&dwc->gadget); 2606 spin_lock(&dwc->lock); 2607 } 2608 } 2609 2610 static void dwc3_reset_gadget(struct dwc3 *dwc) 2611 { 2612 if (!dwc->gadget_driver) 2613 return; 2614 2615 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { 2616 spin_unlock(&dwc->lock); 2617 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); 2618 spin_lock(&dwc->lock); 2619 } 2620 } 2621 2622 static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force) 2623 { 2624 struct dwc3 *dwc = dep->dwc; 2625 struct dwc3_gadget_ep_cmd_params params; 2626 u32 cmd; 2627 int ret; 2628 2629 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || 2630 !dep->resource_index) 2631 return; 2632 2633 /* 2634 * NOTICE: We are violating what the Databook says about the 2635 * EndTransfer command. Ideally we would _always_ wait for the 2636 * EndTransfer Command Completion IRQ, but that's causing too 2637 * much trouble synchronizing between us and gadget driver. 2638 * 2639 * We have discussed this with the IP Provider and it was 2640 * suggested to giveback all requests here, but give HW some 2641 * extra time to synchronize with the interconnect. We're using 2642 * an arbitrary 100us delay for that. 2643 * 2644 * Note also that a similar handling was tested by Synopsys 2645 * (thanks a lot Paul) and nothing bad has come out of it. 2646 * In short, what we're doing is: 2647 * 2648 * - Issue EndTransfer WITH CMDIOC bit set 2649 * - Wait 100us 2650 * 2651 * As of IP version 3.10a of the DWC_usb3 IP, the controller 2652 * supports a mode to work around the above limitation. The 2653 * software can poll the CMDACT bit in the DEPCMD register 2654 * after issuing a EndTransfer command. This mode is enabled 2655 * by writing GUCTL2[14]. This polling is already done in the 2656 * dwc3_send_gadget_ep_cmd() function so if the mode is 2657 * enabled, the EndTransfer command will have completed upon 2658 * returning from this function and we don't need to delay for 2659 * 100us. 2660 * 2661 * This mode is NOT available on the DWC_usb31 IP. 2662 */ 2663 2664 cmd = DWC3_DEPCMD_ENDTRANSFER; 2665 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; 2666 cmd |= DWC3_DEPCMD_CMDIOC; 2667 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); 2668 memset(¶ms, 0, sizeof(params)); 2669 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); 2670 WARN_ON_ONCE(ret); 2671 dep->resource_index = 0; 2672 2673 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) { 2674 dep->flags |= DWC3_EP_END_TRANSFER_PENDING; 2675 udelay(100); 2676 } 2677 } 2678 2679 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) 2680 { 2681 u32 epnum; 2682 2683 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { 2684 struct dwc3_ep *dep; 2685 int ret; 2686 2687 dep = dwc->eps[epnum]; 2688 if (!dep) 2689 continue; 2690 2691 if (!(dep->flags & DWC3_EP_STALL)) 2692 continue; 2693 2694 dep->flags &= ~DWC3_EP_STALL; 2695 2696 ret = dwc3_send_clear_stall_ep_cmd(dep); 2697 WARN_ON_ONCE(ret); 2698 } 2699 } 2700 2701 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) 2702 { 2703 int reg; 2704 2705 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2706 reg &= ~DWC3_DCTL_INITU1ENA; 2707 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2708 2709 reg &= ~DWC3_DCTL_INITU2ENA; 2710 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2711 2712 dwc3_disconnect_gadget(dwc); 2713 2714 dwc->gadget.speed = USB_SPEED_UNKNOWN; 2715 dwc->setup_packet_pending = false; 2716 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); 2717 2718 dwc->connected = false; 2719 } 2720 2721 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) 2722 { 2723 u32 reg; 2724 2725 dwc->connected = true; 2726 2727 /* 2728 * WORKAROUND: DWC3 revisions <1.88a have an issue which 2729 * would cause a missing Disconnect Event if there's a 2730 * pending Setup Packet in the FIFO. 2731 * 2732 * There's no suggested workaround on the official Bug 2733 * report, which states that "unless the driver/application 2734 * is doing any special handling of a disconnect event, 2735 * there is no functional issue". 2736 * 2737 * Unfortunately, it turns out that we _do_ some special 2738 * handling of a disconnect event, namely complete all 2739 * pending transfers, notify gadget driver of the 2740 * disconnection, and so on. 2741 * 2742 * Our suggested workaround is to follow the Disconnect 2743 * Event steps here, instead, based on a setup_packet_pending 2744 * flag. Such flag gets set whenever we have a SETUP_PENDING 2745 * status for EP0 TRBs and gets cleared on XferComplete for the 2746 * same endpoint. 2747 * 2748 * Refers to: 2749 * 2750 * STAR#9000466709: RTL: Device : Disconnect event not 2751 * generated if setup packet pending in FIFO 2752 */ 2753 if (dwc->revision < DWC3_REVISION_188A) { 2754 if (dwc->setup_packet_pending) 2755 dwc3_gadget_disconnect_interrupt(dwc); 2756 } 2757 2758 dwc3_reset_gadget(dwc); 2759 2760 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2761 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 2762 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2763 dwc->test_mode = false; 2764 dwc3_clear_stall_all_ep(dwc); 2765 2766 /* Reset device address to zero */ 2767 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2768 reg &= ~(DWC3_DCFG_DEVADDR_MASK); 2769 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2770 } 2771 2772 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) 2773 { 2774 struct dwc3_ep *dep; 2775 int ret; 2776 u32 reg; 2777 u8 speed; 2778 2779 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 2780 speed = reg & DWC3_DSTS_CONNECTSPD; 2781 dwc->speed = speed; 2782 2783 /* 2784 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed 2785 * each time on Connect Done. 2786 * 2787 * Currently we always use the reset value. If any platform 2788 * wants to set this to a different value, we need to add a 2789 * setting and update GCTL.RAMCLKSEL here. 2790 */ 2791 2792 switch (speed) { 2793 case DWC3_DSTS_SUPERSPEED_PLUS: 2794 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2795 dwc->gadget.ep0->maxpacket = 512; 2796 dwc->gadget.speed = USB_SPEED_SUPER_PLUS; 2797 break; 2798 case DWC3_DSTS_SUPERSPEED: 2799 /* 2800 * WORKAROUND: DWC3 revisions <1.90a have an issue which 2801 * would cause a missing USB3 Reset event. 2802 * 2803 * In such situations, we should force a USB3 Reset 2804 * event by calling our dwc3_gadget_reset_interrupt() 2805 * routine. 2806 * 2807 * Refers to: 2808 * 2809 * STAR#9000483510: RTL: SS : USB3 reset event may 2810 * not be generated always when the link enters poll 2811 */ 2812 if (dwc->revision < DWC3_REVISION_190A) 2813 dwc3_gadget_reset_interrupt(dwc); 2814 2815 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); 2816 dwc->gadget.ep0->maxpacket = 512; 2817 dwc->gadget.speed = USB_SPEED_SUPER; 2818 break; 2819 case DWC3_DSTS_HIGHSPEED: 2820 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 2821 dwc->gadget.ep0->maxpacket = 64; 2822 dwc->gadget.speed = USB_SPEED_HIGH; 2823 break; 2824 case DWC3_DSTS_FULLSPEED: 2825 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); 2826 dwc->gadget.ep0->maxpacket = 64; 2827 dwc->gadget.speed = USB_SPEED_FULL; 2828 break; 2829 case DWC3_DSTS_LOWSPEED: 2830 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); 2831 dwc->gadget.ep0->maxpacket = 8; 2832 dwc->gadget.speed = USB_SPEED_LOW; 2833 break; 2834 } 2835 2836 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket; 2837 2838 /* Enable USB2 LPM Capability */ 2839 2840 if ((dwc->revision > DWC3_REVISION_194A) && 2841 (speed != DWC3_DSTS_SUPERSPEED) && 2842 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { 2843 reg = dwc3_readl(dwc->regs, DWC3_DCFG); 2844 reg |= DWC3_DCFG_LPM_CAP; 2845 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2846 2847 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2848 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); 2849 2850 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); 2851 2852 /* 2853 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and 2854 * DCFG.LPMCap is set, core responses with an ACK and the 2855 * BESL value in the LPM token is less than or equal to LPM 2856 * NYET threshold. 2857 */ 2858 WARN_ONCE(dwc->revision < DWC3_REVISION_240A 2859 && dwc->has_lpm_erratum, 2860 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); 2861 2862 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) 2863 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); 2864 2865 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2866 } else { 2867 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2868 reg &= ~DWC3_DCTL_HIRD_THRES_MASK; 2869 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2870 } 2871 2872 dep = dwc->eps[0]; 2873 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 2874 if (ret) { 2875 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2876 return; 2877 } 2878 2879 dep = dwc->eps[1]; 2880 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); 2881 if (ret) { 2882 dev_err(dwc->dev, "failed to enable %s\n", dep->name); 2883 return; 2884 } 2885 2886 /* 2887 * Configure PHY via GUSB3PIPECTLn if required. 2888 * 2889 * Update GTXFIFOSIZn 2890 * 2891 * In both cases reset values should be sufficient. 2892 */ 2893 } 2894 2895 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) 2896 { 2897 /* 2898 * TODO take core out of low power mode when that's 2899 * implemented. 2900 */ 2901 2902 if (dwc->gadget_driver && dwc->gadget_driver->resume) { 2903 spin_unlock(&dwc->lock); 2904 dwc->gadget_driver->resume(&dwc->gadget); 2905 spin_lock(&dwc->lock); 2906 } 2907 } 2908 2909 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, 2910 unsigned int evtinfo) 2911 { 2912 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 2913 unsigned int pwropt; 2914 2915 /* 2916 * WORKAROUND: DWC3 < 2.50a have an issue when configured without 2917 * Hibernation mode enabled which would show up when device detects 2918 * host-initiated U3 exit. 2919 * 2920 * In that case, device will generate a Link State Change Interrupt 2921 * from U3 to RESUME which is only necessary if Hibernation is 2922 * configured in. 2923 * 2924 * There are no functional changes due to such spurious event and we 2925 * just need to ignore it. 2926 * 2927 * Refers to: 2928 * 2929 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation 2930 * operational mode 2931 */ 2932 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); 2933 if ((dwc->revision < DWC3_REVISION_250A) && 2934 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { 2935 if ((dwc->link_state == DWC3_LINK_STATE_U3) && 2936 (next == DWC3_LINK_STATE_RESUME)) { 2937 return; 2938 } 2939 } 2940 2941 /* 2942 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending 2943 * on the link partner, the USB session might do multiple entry/exit 2944 * of low power states before a transfer takes place. 2945 * 2946 * Due to this problem, we might experience lower throughput. The 2947 * suggested workaround is to disable DCTL[12:9] bits if we're 2948 * transitioning from U1/U2 to U0 and enable those bits again 2949 * after a transfer completes and there are no pending transfers 2950 * on any of the enabled endpoints. 2951 * 2952 * This is the first half of that workaround. 2953 * 2954 * Refers to: 2955 * 2956 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us 2957 * core send LGO_Ux entering U0 2958 */ 2959 if (dwc->revision < DWC3_REVISION_183A) { 2960 if (next == DWC3_LINK_STATE_U0) { 2961 u32 u1u2; 2962 u32 reg; 2963 2964 switch (dwc->link_state) { 2965 case DWC3_LINK_STATE_U1: 2966 case DWC3_LINK_STATE_U2: 2967 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2968 u1u2 = reg & (DWC3_DCTL_INITU2ENA 2969 | DWC3_DCTL_ACCEPTU2ENA 2970 | DWC3_DCTL_INITU1ENA 2971 | DWC3_DCTL_ACCEPTU1ENA); 2972 2973 if (!dwc->u1u2) 2974 dwc->u1u2 = reg & u1u2; 2975 2976 reg &= ~u1u2; 2977 2978 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2979 break; 2980 default: 2981 /* do nothing */ 2982 break; 2983 } 2984 } 2985 } 2986 2987 switch (next) { 2988 case DWC3_LINK_STATE_U1: 2989 if (dwc->speed == USB_SPEED_SUPER) 2990 dwc3_suspend_gadget(dwc); 2991 break; 2992 case DWC3_LINK_STATE_U2: 2993 case DWC3_LINK_STATE_U3: 2994 dwc3_suspend_gadget(dwc); 2995 break; 2996 case DWC3_LINK_STATE_RESUME: 2997 dwc3_resume_gadget(dwc); 2998 break; 2999 default: 3000 /* do nothing */ 3001 break; 3002 } 3003 3004 dwc->link_state = next; 3005 } 3006 3007 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, 3008 unsigned int evtinfo) 3009 { 3010 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; 3011 3012 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) 3013 dwc3_suspend_gadget(dwc); 3014 3015 dwc->link_state = next; 3016 } 3017 3018 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, 3019 unsigned int evtinfo) 3020 { 3021 unsigned int is_ss = evtinfo & BIT(4); 3022 3023 /* 3024 * WORKAROUND: DWC3 revison 2.20a with hibernation support 3025 * have a known issue which can cause USB CV TD.9.23 to fail 3026 * randomly. 3027 * 3028 * Because of this issue, core could generate bogus hibernation 3029 * events which SW needs to ignore. 3030 * 3031 * Refers to: 3032 * 3033 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 3034 * Device Fallback from SuperSpeed 3035 */ 3036 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) 3037 return; 3038 3039 /* enter hibernation here */ 3040 } 3041 3042 static void dwc3_gadget_interrupt(struct dwc3 *dwc, 3043 const struct dwc3_event_devt *event) 3044 { 3045 switch (event->type) { 3046 case DWC3_DEVICE_EVENT_DISCONNECT: 3047 dwc3_gadget_disconnect_interrupt(dwc); 3048 break; 3049 case DWC3_DEVICE_EVENT_RESET: 3050 dwc3_gadget_reset_interrupt(dwc); 3051 break; 3052 case DWC3_DEVICE_EVENT_CONNECT_DONE: 3053 dwc3_gadget_conndone_interrupt(dwc); 3054 break; 3055 case DWC3_DEVICE_EVENT_WAKEUP: 3056 dwc3_gadget_wakeup_interrupt(dwc); 3057 break; 3058 case DWC3_DEVICE_EVENT_HIBER_REQ: 3059 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, 3060 "unexpected hibernation event\n")) 3061 break; 3062 3063 dwc3_gadget_hibernation_interrupt(dwc, event->event_info); 3064 break; 3065 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: 3066 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); 3067 break; 3068 case DWC3_DEVICE_EVENT_EOPF: 3069 /* It changed to be suspend event for version 2.30a and above */ 3070 if (dwc->revision >= DWC3_REVISION_230A) { 3071 /* 3072 * Ignore suspend event until the gadget enters into 3073 * USB_STATE_CONFIGURED state. 3074 */ 3075 if (dwc->gadget.state >= USB_STATE_CONFIGURED) 3076 dwc3_gadget_suspend_interrupt(dwc, 3077 event->event_info); 3078 } 3079 break; 3080 case DWC3_DEVICE_EVENT_SOF: 3081 case DWC3_DEVICE_EVENT_ERRATIC_ERROR: 3082 case DWC3_DEVICE_EVENT_CMD_CMPL: 3083 case DWC3_DEVICE_EVENT_OVERFLOW: 3084 break; 3085 default: 3086 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); 3087 } 3088 } 3089 3090 static void dwc3_process_event_entry(struct dwc3 *dwc, 3091 const union dwc3_event *event) 3092 { 3093 trace_dwc3_event(event->raw, dwc); 3094 3095 if (!event->type.is_devspec) 3096 dwc3_endpoint_interrupt(dwc, &event->depevt); 3097 else if (event->type.type == DWC3_EVENT_TYPE_DEV) 3098 dwc3_gadget_interrupt(dwc, &event->devt); 3099 else 3100 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); 3101 } 3102 3103 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) 3104 { 3105 struct dwc3 *dwc = evt->dwc; 3106 irqreturn_t ret = IRQ_NONE; 3107 int left; 3108 u32 reg; 3109 3110 left = evt->count; 3111 3112 if (!(evt->flags & DWC3_EVENT_PENDING)) 3113 return IRQ_NONE; 3114 3115 while (left > 0) { 3116 union dwc3_event event; 3117 3118 event.raw = *(u32 *) (evt->cache + evt->lpos); 3119 3120 dwc3_process_event_entry(dwc, &event); 3121 3122 /* 3123 * FIXME we wrap around correctly to the next entry as 3124 * almost all entries are 4 bytes in size. There is one 3125 * entry which has 12 bytes which is a regular entry 3126 * followed by 8 bytes data. ATM I don't know how 3127 * things are organized if we get next to the a 3128 * boundary so I worry about that once we try to handle 3129 * that. 3130 */ 3131 evt->lpos = (evt->lpos + 4) % evt->length; 3132 left -= 4; 3133 } 3134 3135 evt->count = 0; 3136 evt->flags &= ~DWC3_EVENT_PENDING; 3137 ret = IRQ_HANDLED; 3138 3139 /* Unmask interrupt */ 3140 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); 3141 reg &= ~DWC3_GEVNTSIZ_INTMASK; 3142 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); 3143 3144 if (dwc->imod_interval) { 3145 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); 3146 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); 3147 } 3148 3149 return ret; 3150 } 3151 3152 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) 3153 { 3154 struct dwc3_event_buffer *evt = _evt; 3155 struct dwc3 *dwc = evt->dwc; 3156 unsigned long flags; 3157 irqreturn_t ret = IRQ_NONE; 3158 3159 spin_lock_irqsave(&dwc->lock, flags); 3160 ret = dwc3_process_event_buf(evt); 3161 spin_unlock_irqrestore(&dwc->lock, flags); 3162 3163 return ret; 3164 } 3165 3166 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) 3167 { 3168 struct dwc3 *dwc = evt->dwc; 3169 u32 amount; 3170 u32 count; 3171 u32 reg; 3172 3173 if (pm_runtime_suspended(dwc->dev)) { 3174 pm_runtime_get(dwc->dev); 3175 disable_irq_nosync(dwc->irq_gadget); 3176 dwc->pending_events = true; 3177 return IRQ_HANDLED; 3178 } 3179 3180 /* 3181 * With PCIe legacy interrupt, test shows that top-half irq handler can 3182 * be called again after HW interrupt deassertion. Check if bottom-half 3183 * irq event handler completes before caching new event to prevent 3184 * losing events. 3185 */ 3186 if (evt->flags & DWC3_EVENT_PENDING) 3187 return IRQ_HANDLED; 3188 3189 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); 3190 count &= DWC3_GEVNTCOUNT_MASK; 3191 if (!count) 3192 return IRQ_NONE; 3193 3194 evt->count = count; 3195 evt->flags |= DWC3_EVENT_PENDING; 3196 3197 /* Mask interrupt */ 3198 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); 3199 reg |= DWC3_GEVNTSIZ_INTMASK; 3200 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); 3201 3202 amount = min(count, evt->length - evt->lpos); 3203 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); 3204 3205 if (amount < count) 3206 memcpy(evt->cache, evt->buf, count - amount); 3207 3208 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); 3209 3210 return IRQ_WAKE_THREAD; 3211 } 3212 3213 static irqreturn_t dwc3_interrupt(int irq, void *_evt) 3214 { 3215 struct dwc3_event_buffer *evt = _evt; 3216 3217 return dwc3_check_event_buf(evt); 3218 } 3219 3220 static int dwc3_gadget_get_irq(struct dwc3 *dwc) 3221 { 3222 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); 3223 int irq; 3224 3225 irq = platform_get_irq_byname(dwc3_pdev, "peripheral"); 3226 if (irq > 0) 3227 goto out; 3228 3229 if (irq == -EPROBE_DEFER) 3230 goto out; 3231 3232 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3"); 3233 if (irq > 0) 3234 goto out; 3235 3236 if (irq == -EPROBE_DEFER) 3237 goto out; 3238 3239 irq = platform_get_irq(dwc3_pdev, 0); 3240 if (irq > 0) 3241 goto out; 3242 3243 if (irq != -EPROBE_DEFER) 3244 dev_err(dwc->dev, "missing peripheral IRQ\n"); 3245 3246 if (!irq) 3247 irq = -EINVAL; 3248 3249 out: 3250 return irq; 3251 } 3252 3253 /** 3254 * dwc3_gadget_init - initializes gadget related registers 3255 * @dwc: pointer to our controller context structure 3256 * 3257 * Returns 0 on success otherwise negative errno. 3258 */ 3259 int dwc3_gadget_init(struct dwc3 *dwc) 3260 { 3261 int ret; 3262 int irq; 3263 3264 irq = dwc3_gadget_get_irq(dwc); 3265 if (irq < 0) { 3266 ret = irq; 3267 goto err0; 3268 } 3269 3270 dwc->irq_gadget = irq; 3271 3272 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, 3273 sizeof(*dwc->ep0_trb) * 2, 3274 &dwc->ep0_trb_addr, GFP_KERNEL); 3275 if (!dwc->ep0_trb) { 3276 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); 3277 ret = -ENOMEM; 3278 goto err0; 3279 } 3280 3281 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); 3282 if (!dwc->setup_buf) { 3283 ret = -ENOMEM; 3284 goto err1; 3285 } 3286 3287 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, 3288 &dwc->bounce_addr, GFP_KERNEL); 3289 if (!dwc->bounce) { 3290 ret = -ENOMEM; 3291 goto err2; 3292 } 3293 3294 init_completion(&dwc->ep0_in_setup); 3295 3296 dwc->gadget.ops = &dwc3_gadget_ops; 3297 dwc->gadget.speed = USB_SPEED_UNKNOWN; 3298 dwc->gadget.sg_supported = true; 3299 dwc->gadget.name = "dwc3-gadget"; 3300 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG; 3301 3302 /* 3303 * FIXME We might be setting max_speed to <SUPER, however versions 3304 * <2.20a of dwc3 have an issue with metastability (documented 3305 * elsewhere in this driver) which tells us we can't set max speed to 3306 * anything lower than SUPER. 3307 * 3308 * Because gadget.max_speed is only used by composite.c and function 3309 * drivers (i.e. it won't go into dwc3's registers) we are allowing this 3310 * to happen so we avoid sending SuperSpeed Capability descriptor 3311 * together with our BOS descriptor as that could confuse host into 3312 * thinking we can handle super speed. 3313 * 3314 * Note that, in fact, we won't even support GetBOS requests when speed 3315 * is less than super speed because we don't have means, yet, to tell 3316 * composite.c that we are USB 2.0 + LPM ECN. 3317 */ 3318 if (dwc->revision < DWC3_REVISION_220A && 3319 !dwc->dis_metastability_quirk) 3320 dev_info(dwc->dev, "changing max_speed on rev %08x\n", 3321 dwc->revision); 3322 3323 dwc->gadget.max_speed = dwc->maximum_speed; 3324 3325 /* 3326 * REVISIT: Here we should clear all pending IRQs to be 3327 * sure we're starting from a well known location. 3328 */ 3329 3330 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); 3331 if (ret) 3332 goto err3; 3333 3334 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); 3335 if (ret) { 3336 dev_err(dwc->dev, "failed to register udc\n"); 3337 goto err4; 3338 } 3339 3340 return 0; 3341 3342 err4: 3343 dwc3_gadget_free_endpoints(dwc); 3344 3345 err3: 3346 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 3347 dwc->bounce_addr); 3348 3349 err2: 3350 kfree(dwc->setup_buf); 3351 3352 err1: 3353 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 3354 dwc->ep0_trb, dwc->ep0_trb_addr); 3355 3356 err0: 3357 return ret; 3358 } 3359 3360 /* -------------------------------------------------------------------------- */ 3361 3362 void dwc3_gadget_exit(struct dwc3 *dwc) 3363 { 3364 usb_del_gadget_udc(&dwc->gadget); 3365 dwc3_gadget_free_endpoints(dwc); 3366 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, 3367 dwc->bounce_addr); 3368 kfree(dwc->setup_buf); 3369 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, 3370 dwc->ep0_trb, dwc->ep0_trb_addr); 3371 } 3372 3373 int dwc3_gadget_suspend(struct dwc3 *dwc) 3374 { 3375 if (!dwc->gadget_driver) 3376 return 0; 3377 3378 dwc3_gadget_run_stop(dwc, false, false); 3379 dwc3_disconnect_gadget(dwc); 3380 __dwc3_gadget_stop(dwc); 3381 3382 return 0; 3383 } 3384 3385 int dwc3_gadget_resume(struct dwc3 *dwc) 3386 { 3387 int ret; 3388 3389 if (!dwc->gadget_driver) 3390 return 0; 3391 3392 ret = __dwc3_gadget_start(dwc); 3393 if (ret < 0) 3394 goto err0; 3395 3396 ret = dwc3_gadget_run_stop(dwc, true, false); 3397 if (ret < 0) 3398 goto err1; 3399 3400 return 0; 3401 3402 err1: 3403 __dwc3_gadget_stop(dwc); 3404 3405 err0: 3406 return ret; 3407 } 3408 3409 void dwc3_gadget_process_pending_events(struct dwc3 *dwc) 3410 { 3411 if (dwc->pending_events) { 3412 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); 3413 dwc->pending_events = false; 3414 enable_irq(dwc->irq_gadget); 3415 } 3416 } 3417